summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_browser_actions_api.cc
blob: 2a3aa2e26e36b4e1590572a8e3605399ac861b16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/extension_browser_actions_api.h"

#include "chrome/browser/browser.h"
#include "chrome/browser/browser_list.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/render_messages.h"

namespace extension_browser_actions_api_constants {

const char kSetNameFunction[] = "browserAction.setName";
const char kSetIconFunction[] = "browserAction.setIcon";
const char kSetBadgeTextFunction[] = "browserAction.setBadgeText";
const char kSetBadgeBackgroundColorFunction[] =
    "browserAction.setBadgeBackgroundColor";

}  // namespace extension_browser_actions_api_constants

namespace {
// Errors.
const char kNoBrowserActionError[] =
    "This extension has no browser action specified.";
}

bool BrowserActionSetNameFunction::RunImpl() {
  std::string name;
  EXTENSION_FUNCTION_VALIDATE(args_->GetAsString(&name));

  Extension* extension = dispatcher()->GetExtension();
  if (!extension->browser_action()) {
    error_ = kNoBrowserActionError;
    return false;
  }

  extension->browser_action_state()->set_title(name);

  NotificationService::current()->Notify(
      NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
      Source<ExtensionAction>(extension->browser_action()),
      Details<ExtensionActionState>(extension->browser_action_state()));
  return true;
}

bool BrowserActionSetIconFunction::RunImpl() {
  // setIcon can take a variant argument: either a canvas ImageData, or an
  // icon index.
  EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_BINARY) ||
                              args_->IsType(Value::TYPE_INTEGER));

  Extension* extension = dispatcher()->GetExtension();
  if (!extension->browser_action()) {
    error_ = kNoBrowserActionError;
    return false;
  }

  if (args_->IsType(Value::TYPE_BINARY)) {
    BinaryValue* binary = static_cast<BinaryValue*>(args_);
    IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize());
    void* iter = NULL;
    scoped_ptr<SkBitmap> bitmap(new SkBitmap);
    EXTENSION_FUNCTION_VALIDATE(
        IPC::ReadParam(&bitmap_pickle, &iter, bitmap.get()));
    extension->browser_action_state()->set_icon(bitmap.release());
  } else {
    int icon_index = -1;
    EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&icon_index));
    extension->browser_action_state()->set_icon_index(icon_index);
  }

  NotificationService::current()->Notify(
      NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
      Source<ExtensionAction>(extension->browser_action()),
      Details<ExtensionActionState>(extension->browser_action_state()));
  return true;
}

bool BrowserActionSetBadgeTextFunction::RunImpl() {
  std::string badge_text;
  EXTENSION_FUNCTION_VALIDATE(args_->GetAsString(&badge_text));

  Extension* extension = dispatcher()->GetExtension();
  if (!extension->browser_action()) {
    error_ = kNoBrowserActionError;
    return false;
  }

  extension->browser_action_state()->set_badge_text(badge_text);

  NotificationService::current()->Notify(
      NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
      Source<ExtensionAction>(extension->browser_action()),
      Details<ExtensionActionState>(extension->browser_action_state()));
  return true;
}

bool BrowserActionSetBadgeBackgroundColorFunction::RunImpl() {
  EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST));
  ListValue* list = static_cast<ListValue*>(args_);
  EXTENSION_FUNCTION_VALIDATE(list->GetSize() == 4);

  int color_array[4] = {0};
  for (size_t i = 0; i < arraysize(color_array); ++i) {
    EXTENSION_FUNCTION_VALIDATE(list->GetInteger(i, &color_array[i]));
  }

  SkColor color = SkColorSetARGB(color_array[0], color_array[1], color_array[2],
                                 color_array[3]);

  Extension* extension = dispatcher()->GetExtension();
  if (!extension->browser_action()) {
    error_ = kNoBrowserActionError;
    return false;
  }

  extension->browser_action_state()->set_badge_background_color(color);

  NotificationService::current()->Notify(
      NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
      Source<ExtensionAction>(extension->browser_action()),
      Details<ExtensionActionState>(extension->browser_action_state()));
  return true;
}