OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_browser_actions_api.h" | 5 #include "chrome/browser/extensions/extension_browser_actions_api.h" |
6 | 6 |
7 #include "chrome/browser/browser.h" | 7 #include "chrome/browser/browser.h" |
8 #include "chrome/browser/browser_list.h" | 8 #include "chrome/browser/browser_list.h" |
9 #include "chrome/common/notification_service.h" | 9 #include "chrome/common/notification_service.h" |
10 #include "chrome/common/render_messages.h" | 10 #include "chrome/common/render_messages.h" |
11 | 11 |
12 namespace { | 12 namespace { |
13 // Errors. | 13 // Errors. |
14 const char kNoBrowserActionError[] = | 14 const char kNoBrowserActionError[] = |
15 "This extension has no browser action specified."; | 15 "This extension has no browser action specified."; |
16 const char kIconIndexOutOfBounds[] = | 16 const char kIconIndexOutOfBounds[] = |
17 "Browser action icon index out of bounds."; | 17 "Browser action icon index out of bounds."; |
18 } | 18 } |
19 | 19 |
20 bool BrowserActionSetIconFunction::RunImpl() { | 20 bool BrowserActionFunction::RunImpl() { |
21 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); | 21 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
22 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); | 22 details_ = static_cast<DictionaryValue*>(args_); |
| 23 |
| 24 if (details_->HasKey(L"tabId")) |
| 25 EXTENSION_FUNCTION_VALIDATE(details_->GetInteger(L"tabId", &tab_id_)); |
23 | 26 |
24 Extension* extension = dispatcher()->GetExtension(); | 27 Extension* extension = dispatcher()->GetExtension(); |
25 if (!extension->browser_action()) { | 28 browser_action_ = extension->browser_action(); |
| 29 if (!browser_action_) { |
26 error_ = kNoBrowserActionError; | 30 error_ = kNoBrowserActionError; |
27 return false; | 31 return false; |
28 } | 32 } |
29 | 33 |
30 // setIcon can take a variant argument: either a canvas ImageData, or an | 34 if (!RunBrowserAction()) |
31 // icon index. | 35 return false; |
32 BinaryValue* binary; | |
33 int icon_index; | |
34 if (args->GetBinary(L"imageData", &binary)) { | |
35 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize()); | |
36 void* iter = NULL; | |
37 scoped_ptr<SkBitmap> bitmap(new SkBitmap); | |
38 EXTENSION_FUNCTION_VALIDATE( | |
39 IPC::ReadParam(&bitmap_pickle, &iter, bitmap.get())); | |
40 extension->browser_action_state()->set_icon(bitmap.release()); | |
41 } else if (args->GetInteger(L"iconIndex", &icon_index)) { | |
42 if (icon_index < 0 || static_cast<size_t>(icon_index) >= | |
43 | |
44 extension->browser_action()->icon_paths().size()) { | |
45 error_ = kIconIndexOutOfBounds; | |
46 return false; | |
47 } | |
48 extension->browser_action_state()->set_icon_index(icon_index); | |
49 extension->browser_action_state()->set_icon(NULL); | |
50 } else { | |
51 EXTENSION_FUNCTION_VALIDATE(false); | |
52 } | |
53 | 36 |
54 NotificationService::current()->Notify( | 37 NotificationService::current()->Notify( |
55 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, | 38 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, |
56 Source<ExtensionAction>(extension->browser_action()), | 39 Source<ExtensionAction2>(browser_action_), |
57 Details<ExtensionActionState>(extension->browser_action_state())); | 40 NotificationService::NoDetails()); |
58 return true; | 41 return true; |
59 } | 42 } |
60 | 43 |
61 bool BrowserActionSetTitleFunction::RunImpl() { | 44 bool BrowserActionSetIconFunction::RunBrowserAction() { |
62 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); | 45 BinaryValue* binary = NULL; |
63 DictionaryValue* details = static_cast<DictionaryValue*>(args_); | 46 EXTENSION_FUNCTION_VALIDATE(details_->GetBinary(L"imageData", &binary)); |
64 | 47 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize()); |
65 std::string title; | 48 void* iter = NULL; |
66 EXTENSION_FUNCTION_VALIDATE(details->GetString(L"title", &title)); | 49 SkBitmap bitmap; |
67 | 50 EXTENSION_FUNCTION_VALIDATE( |
68 Extension* extension = dispatcher()->GetExtension(); | 51 IPC::ReadParam(&bitmap_pickle, &iter, &bitmap)); |
69 if (!extension->browser_action()) { | 52 browser_action_->SetIcon(tab_id_, bitmap); |
70 error_ = kNoBrowserActionError; | |
71 return false; | |
72 } | |
73 | |
74 extension->browser_action_state()->set_title(title); | |
75 | |
76 NotificationService::current()->Notify( | |
77 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, | |
78 Source<ExtensionAction>(extension->browser_action()), | |
79 Details<ExtensionActionState>(extension->browser_action_state())); | |
80 return true; | 53 return true; |
81 } | 54 } |
82 | 55 |
83 bool BrowserActionSetBadgeTextFunction::RunImpl() { | 56 bool BrowserActionSetTitleFunction::RunBrowserAction() { |
84 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); | 57 std::string title; |
85 DictionaryValue* details = static_cast<DictionaryValue*>(args_); | 58 EXTENSION_FUNCTION_VALIDATE(details_->GetString(L"title", &title)); |
86 | 59 browser_action_->SetTitle(tab_id_, title); |
87 std::string badge_text; | |
88 EXTENSION_FUNCTION_VALIDATE(details->GetString(L"text", &badge_text)); | |
89 | |
90 Extension* extension = dispatcher()->GetExtension(); | |
91 if (!extension->browser_action()) { | |
92 error_ = kNoBrowserActionError; | |
93 return false; | |
94 } | |
95 | |
96 extension->browser_action_state()->set_badge_text(badge_text); | |
97 | |
98 NotificationService::current()->Notify( | |
99 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, | |
100 Source<ExtensionAction>(extension->browser_action()), | |
101 Details<ExtensionActionState>(extension->browser_action_state())); | |
102 return true; | 60 return true; |
103 } | 61 } |
104 | 62 |
105 bool BrowserActionSetBadgeBackgroundColorFunction::RunImpl() { | 63 bool BrowserActionSetBadgeTextFunction::RunBrowserAction() { |
106 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); | 64 std::string badge_text; |
107 DictionaryValue* details = static_cast<DictionaryValue*>(args_); | 65 EXTENSION_FUNCTION_VALIDATE(details_->GetString(L"text", &badge_text)); |
| 66 browser_action_->SetBadgeText(tab_id_, badge_text); |
| 67 return true; |
| 68 } |
108 | 69 |
| 70 bool BrowserActionSetBadgeBackgroundColorFunction::RunBrowserAction() { |
109 ListValue* list = NULL; | 71 ListValue* list = NULL; |
110 EXTENSION_FUNCTION_VALIDATE(details->GetList(L"color", &list)); | 72 EXTENSION_FUNCTION_VALIDATE(details_->GetList(L"color", &list)); |
111 EXTENSION_FUNCTION_VALIDATE(list->GetSize() == 4); | 73 EXTENSION_FUNCTION_VALIDATE(list->GetSize() == 4); |
112 | 74 |
113 int color_array[4] = {0}; | 75 int color_array[4] = {0}; |
114 for (size_t i = 0; i < arraysize(color_array); ++i) { | 76 for (size_t i = 0; i < arraysize(color_array); ++i) { |
115 EXTENSION_FUNCTION_VALIDATE(list->GetInteger(i, &color_array[i])); | 77 EXTENSION_FUNCTION_VALIDATE(list->GetInteger(i, &color_array[i])); |
116 } | 78 } |
117 | 79 |
118 SkColor color = SkColorSetARGB(color_array[3], color_array[0], color_array[1], | 80 SkColor color = SkColorSetARGB(color_array[3], color_array[0], color_array[1], |
119 color_array[2]); | 81 color_array[2]); |
| 82 browser_action_->SetBadgeBackgroundColor(tab_id_, color); |
120 | 83 |
121 Extension* extension = dispatcher()->GetExtension(); | |
122 if (!extension->browser_action()) { | |
123 error_ = kNoBrowserActionError; | |
124 return false; | |
125 } | |
126 | |
127 extension->browser_action_state()->set_badge_background_color(color); | |
128 | |
129 NotificationService::current()->Notify( | |
130 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, | |
131 Source<ExtensionAction>(extension->browser_action()), | |
132 Details<ExtensionActionState>(extension->browser_action_state())); | |
133 return true; | 84 return true; |
134 } | 85 } |
OLD | NEW |