Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(370)

Side by Side Diff: chrome/browser/extensions/api/extension_action/extension_actions_api.cc

Issue 10231002: Fully merge the implementations of the browserAction and pageAction APIs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: youskc Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/api/extension_action/extension_actions_api.h " 5 #include "chrome/browser/extensions/api/extension_action/extension_actions_api.h "
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/string_number_conversions.h"
9 #include "base/values.h" 10 #include "base/values.h"
10 #include "base/string_number_conversions.h" 11 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_ api_constants.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_tab_helper.h"
14 #include "chrome/browser/extensions/extension_tab_util.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
17 #include "chrome/common/chrome_notification_types.h"
11 #include "chrome/common/extensions/extension.h" 18 #include "chrome/common/extensions/extension.h"
19 #include "chrome/common/extensions/extension_action.h"
20 #include "chrome/common/extensions/extension_error_utils.h"
12 #include "chrome/common/render_messages.h" 21 #include "chrome/common/render_messages.h"
22 #include "content/public/browser/navigation_entry.h"
23 #include "content/public/browser/notification_service.h"
24
25 namespace {
26
27 // Errors.
28 const char kNoExtensionActionError[] =
29 "This extension has no action specified.";
30 const char kNoTabError[] = "No tab with id: *.";
31 const char kIconIndexOutOfBounds[] = "Page action icon index out of bounds.";
32
33 }
13 34
14 ExtensionActionFunction::ExtensionActionFunction() 35 ExtensionActionFunction::ExtensionActionFunction()
15 : details_(NULL), 36 : details_(NULL),
16 tab_id_(ExtensionAction::kDefaultTabId), 37 tab_id_(ExtensionAction::kDefaultTabId),
38 contents_(NULL),
17 extension_action_(NULL) { 39 extension_action_(NULL) {
18 } 40 }
19 41
20 ExtensionActionFunction::~ExtensionActionFunction() { 42 ExtensionActionFunction::~ExtensionActionFunction() {
21 } 43 }
22 44
23 bool ExtensionActionFunction::RunImpl() { 45 bool ExtensionActionFunction::RunImpl() {
24 base::Value* arg; 46 extension_action_ = GetExtension()->browser_action();
25 args_->Get(0, &arg); 47 if (!extension_action_)
26 if (arg->IsType(base::Value::TYPE_DICTIONARY)) { 48 extension_action_ = GetExtension()->page_action();
27 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details_)); 49 EXTENSION_FUNCTION_VALIDATE(extension_action_);
28 EXTENSION_FUNCTION_VALIDATE(details_ != NULL); 50
29 if (details_->HasKey("tabId")) 51 // There may or may not be details (depends on the function).
30 EXTENSION_FUNCTION_VALIDATE(details_->GetInteger("tabId", &tab_id_)); 52 // The tabId might appear in details (if it exists) or as the first
53 // argument besides the action type (depends on the function).
54 {
55 base::Value* first_arg = NULL;
56 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &first_arg));
57
58 switch (first_arg->GetType()) {
59 case Value::TYPE_INTEGER:
60 CHECK(first_arg->GetAsInteger(&tab_id_));
61 break;
62
63 case Value::TYPE_DICTIONARY:
64 details_ = static_cast<base::DictionaryValue*>(first_arg);
65 if (details_->HasKey("tabId"))
66 EXTENSION_FUNCTION_VALIDATE(details_->GetInteger("tabId", &tab_id_));
67 break;
68
69 default:
70 EXTENSION_FUNCTION_VALIDATE(false);
71 }
31 } 72 }
32 return true; 73
74 // Find the TabContentsWrapper that contains this tab id if one is required.
75 if (tab_id_ == ExtensionAction::kDefaultTabId) {
76 EXTENSION_FUNCTION_VALIDATE(GetExtension()->browser_action());
77 } else {
78 ExtensionTabUtil::GetTabById(
79 tab_id_, profile(), include_incognito(), NULL, NULL, &contents_, NULL);
80 if (!contents_) {
81 error_ = ExtensionErrorUtils::FormatErrorMessage(
82 kNoTabError, base::IntToString(tab_id_));
83 return false;
84 }
85 }
86
87 return RunExtensionAction();
33 } 88 }
34 89
35 bool ExtensionActionFunction::SetIcon() { 90 void ExtensionActionFunction::NotifyChange() {
36 base::BinaryValue* binary = NULL; 91 if (GetExtension()->browser_action())
37 EXTENSION_FUNCTION_VALIDATE(details_->GetBinary("imageData", &binary)); 92 NotifyBrowserActionChange();
38 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize()); 93 else if (GetExtension()->page_action())
39 PickleIterator iter(bitmap_pickle); 94 NotifyPageActionChange();
40 SkBitmap bitmap; 95 else
41 EXTENSION_FUNCTION_VALIDATE( 96 NOTREACHED();
42 IPC::ReadParam(&bitmap_pickle, &iter, &bitmap));
43 extension_action_->SetIcon(tab_id_, bitmap);
44 return true;
45 } 97 }
46 98
47 bool ExtensionActionFunction::SetTitle() { 99 void ExtensionActionFunction::NotifyBrowserActionChange() {
48 std::string title; 100 content::NotificationService::current()->Notify(
49 EXTENSION_FUNCTION_VALIDATE(details_->GetString("title", &title)); 101 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED,
50 extension_action_->SetTitle(tab_id_, title); 102 content::Source<ExtensionAction>(extension_action_),
51 return true; 103 content::NotificationService::NoDetails());
52 } 104 }
53 105
54 bool ExtensionActionFunction::SetPopup() { 106 void ExtensionActionFunction::NotifyPageActionChange() {
55 std::string popup_string; 107 contents_->extension_tab_helper()->PageActionStateChanged();
56 EXTENSION_FUNCTION_VALIDATE(details_->GetString("popup", &popup_string));
57
58 GURL popup_url;
59 if (!popup_string.empty())
60 popup_url = GetExtension()->GetResourceURL(popup_string);
61
62 extension_action_->SetPopupUrl(tab_id_, popup_url);
63 return true;
64 } 108 }
65 109
66 bool ExtensionActionFunction::SetBadgeText() { 110 // static
67 std::string badge_text;
68 EXTENSION_FUNCTION_VALIDATE(details_->GetString("text", &badge_text));
69 extension_action_->SetBadgeText(tab_id_, badge_text);
70 return true;
71 }
72
73 bool ExtensionActionFunction::ParseCSSColorString( 111 bool ExtensionActionFunction::ParseCSSColorString(
74 const std::string& color_string, 112 const std::string& color_string,
75 SkColor* result) { 113 SkColor* result) {
76 std::string formatted_color = "#"; 114 std::string formatted_color = "#";
77 // Check the string for incorrect formatting. 115 // Check the string for incorrect formatting.
78 if (color_string[0] != '#') 116 if (color_string[0] != '#')
79 return false; 117 return false;
80 118
81 // Convert the string from #FFF format to #FFFFFF format. 119 // Convert the string from #FFF format to #FFFFFF format.
82 if (color_string.length() == 4) { 120 if (color_string.length() == 4) {
(...skipping 16 matching lines...) Expand all
99 color_ints + i)) 137 color_ints + i))
100 return false; 138 return false;
101 if (color_ints[i] > 255 || color_ints[i] < 0) 139 if (color_ints[i] > 255 || color_ints[i] < 0)
102 return false; 140 return false;
103 } 141 }
104 142
105 *result = SkColorSetARGB(255, color_ints[0], color_ints[1], color_ints[2]); 143 *result = SkColorSetARGB(255, color_ints[0], color_ints[1], color_ints[2]);
106 return true; 144 return true;
107 } 145 }
108 146
109 bool ExtensionActionFunction::SetBadgeBackgroundColor() { 147 bool ExtensionActionFunction::SetVisible(bool visible) {
148 extension_action_->SetIsVisible(tab_id_, visible);
149 NotifyChange();
150 return true;
151 }
152
153 bool ExtensionActionShowFunction::RunExtensionAction() {
154 return SetVisible(true);
155 }
156
157 bool ExtensionActionHideFunction::RunExtensionAction() {
158 return SetVisible(false);
159 }
160
161 bool ExtensionActionSetIconFunction::RunExtensionAction() {
162 // setIcon can take a variant argument: either a canvas ImageData, or an
163 // icon index.
164 base::BinaryValue* binary = NULL;
165 int icon_index;
166 if (details_->GetBinary("imageData", &binary)) {
167 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize());
168 PickleIterator iter(bitmap_pickle);
169 SkBitmap bitmap;
170 EXTENSION_FUNCTION_VALIDATE(
171 IPC::ReadParam(&bitmap_pickle, &iter, &bitmap));
172 extension_action_->SetIcon(tab_id_, bitmap);
173 } else if (details_->GetInteger("iconIndex", &icon_index)) {
174 EXTENSION_FUNCTION_VALIDATE(GetExtension()->page_action());
175 if (icon_index < 0 ||
176 static_cast<size_t>(icon_index) >=
177 extension_action_->icon_paths()->size()) {
178 error_ = kIconIndexOutOfBounds;
179 return false;
180 }
181 extension_action_->SetIcon(tab_id_, SkBitmap());
182 extension_action_->SetIconIndex(tab_id_, icon_index);
183 } else {
184 EXTENSION_FUNCTION_VALIDATE(false);
185 }
186 NotifyChange();
187 return true;
188 }
189
190 bool ExtensionActionSetTitleFunction::RunExtensionAction() {
191 std::string title;
192 EXTENSION_FUNCTION_VALIDATE(details_->GetString("title", &title));
193 extension_action_->SetTitle(tab_id_, title);
194 NotifyChange();
195 return true;
196 }
197
198 bool ExtensionActionSetPopupFunction::RunExtensionAction() {
199 std::string popup_string;
200 EXTENSION_FUNCTION_VALIDATE(details_->GetString("popup", &popup_string));
201
202 GURL popup_url;
203 if (!popup_string.empty())
204 popup_url = GetExtension()->GetResourceURL(popup_string);
205
206 extension_action_->SetPopupUrl(tab_id_, popup_url);
207 NotifyChange();
208 return true;
209 }
210
211 bool ExtensionActionSetBadgeTextFunction::RunExtensionAction() {
212 std::string badge_text;
213 EXTENSION_FUNCTION_VALIDATE(details_->GetString("text", &badge_text));
214 extension_action_->SetBadgeText(tab_id_, badge_text);
215 NotifyChange();
216 return true;
217 }
218
219 bool ExtensionActionSetBadgeBackgroundColorFunction::RunExtensionAction() {
110 Value* color_value = NULL; 220 Value* color_value = NULL;
111 details_->Get("color", &color_value); 221 details_->Get("color", &color_value);
112 SkColor color = 0; 222 SkColor color = 0;
113 if (color_value->IsType(Value::TYPE_LIST)) { 223 if (color_value->IsType(Value::TYPE_LIST)) {
114 ListValue* list = NULL; 224 ListValue* list = NULL;
115 EXTENSION_FUNCTION_VALIDATE(details_->GetList("color", &list)); 225 EXTENSION_FUNCTION_VALIDATE(details_->GetList("color", &list));
116 EXTENSION_FUNCTION_VALIDATE(list->GetSize() == 4); 226 EXTENSION_FUNCTION_VALIDATE(list->GetSize() == 4);
117 227
118 int color_array[4] = {0}; 228 int color_array[4] = {0};
119 for (size_t i = 0; i < arraysize(color_array); ++i) { 229 for (size_t i = 0; i < arraysize(color_array); ++i) {
120 EXTENSION_FUNCTION_VALIDATE(list->GetInteger(i, &color_array[i])); 230 EXTENSION_FUNCTION_VALIDATE(list->GetInteger(i, &color_array[i]));
121 } 231 }
122 232
123 color = SkColorSetARGB(color_array[3], color_array[0], 233 color = SkColorSetARGB(color_array[3], color_array[0],
124 color_array[1], color_array[2]); 234 color_array[1], color_array[2]);
125
126 } else if (color_value->IsType(Value::TYPE_STRING)) { 235 } else if (color_value->IsType(Value::TYPE_STRING)) {
127 std::string color_string; 236 std::string color_string;
128 EXTENSION_FUNCTION_VALIDATE(details_->GetString("color", &color_string)); 237 EXTENSION_FUNCTION_VALIDATE(details_->GetString("color", &color_string));
129 if (!ParseCSSColorString(color_string, &color)) 238 if (!ParseCSSColorString(color_string, &color))
130 return false; 239 return false;
131 } 240 }
132 241
133 extension_action_->SetBadgeBackgroundColor(tab_id_, color); 242 extension_action_->SetBadgeBackgroundColor(tab_id_, color);
134 243 NotifyChange();
135 return true; 244 return true;
136 } 245 }
137 246
138 bool ExtensionActionFunction::GetTitle() { 247 bool ExtensionActionGetTitleFunction::RunExtensionAction() {
139 result_.reset(Value::CreateStringValue(extension_action_->GetTitle(tab_id_))); 248 result_.reset(Value::CreateStringValue(extension_action_->GetTitle(tab_id_)));
140 return true; 249 return true;
141 } 250 }
142 251
143 bool ExtensionActionFunction::GetPopup() { 252 bool ExtensionActionGetPopupFunction::RunExtensionAction() {
144 result_.reset(Value::CreateStringValue( 253 result_.reset(Value::CreateStringValue(
145 extension_action_->GetPopupUrl(tab_id_).spec())); 254 extension_action_->GetPopupUrl(tab_id_).spec()));
146 return true; 255 return true;
147 } 256 }
148 257
149 bool ExtensionActionFunction::GetBadgeText() { 258 bool ExtensionActionGetBadgeTextFunction::RunExtensionAction() {
150 result_.reset(Value::CreateStringValue( 259 result_.reset(Value::CreateStringValue(
151 extension_action_->GetBadgeText(tab_id_))); 260 extension_action_->GetBadgeText(tab_id_)));
152 return true; 261 return true;
153 } 262 }
154 263
155 bool ExtensionActionFunction::GetBadgeBackgroundColor() { 264 bool ExtensionActionGetBadgeBackgroundColorFunction::RunExtensionAction() {
156 ListValue* list = new ListValue(); 265 ListValue* list = new ListValue();
157 SkColor color = extension_action_->GetBadgeBackgroundColor(tab_id_); 266 SkColor color = extension_action_->GetBadgeBackgroundColor(tab_id_);
158 list->Append(Value::CreateIntegerValue(SkColorGetR(color))); 267 list->Append(Value::CreateIntegerValue(SkColorGetR(color)));
159 list->Append(Value::CreateIntegerValue(SkColorGetG(color))); 268 list->Append(Value::CreateIntegerValue(SkColorGetG(color)));
160 list->Append(Value::CreateIntegerValue(SkColorGetB(color))); 269 list->Append(Value::CreateIntegerValue(SkColorGetB(color)));
161 list->Append(Value::CreateIntegerValue(SkColorGetA(color))); 270 list->Append(Value::CreateIntegerValue(SkColorGetA(color)));
162 result_.reset(list); 271 result_.reset(list);
163 return true; 272 return true;
164 } 273 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698