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

Side by Side Diff: chrome/browser/extensions/extension_page_actions_module.cc

Issue 269079: Implement new page action API. (Closed)
Patch Set: compile fixes Created 11 years, 2 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
OLDNEW
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_page_actions_module.h" 5 #include "chrome/browser/extensions/extension_page_actions_module.h"
6 6
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "chrome/browser/browser.h" 8 #include "chrome/browser/browser.h"
9 #include "chrome/browser/browser_list.h" 9 #include "chrome/browser/browser_list.h"
10 #include "chrome/browser/profile.h" 10 #include "chrome/browser/profile.h"
11 #include "chrome/browser/extensions/extension_page_actions_module_constants.h" 11 #include "chrome/browser/extensions/extension_page_actions_module_constants.h"
12 #include "chrome/browser/extensions/extension_tabs_module.h" 12 #include "chrome/browser/extensions/extension_tabs_module.h"
13 #include "chrome/browser/extensions/extensions_service.h" 13 #include "chrome/browser/extensions/extensions_service.h"
14 #include "chrome/browser/tab_contents/navigation_entry.h" 14 #include "chrome/browser/tab_contents/navigation_entry.h"
15 #include "chrome/browser/tab_contents/tab_contents.h" 15 #include "chrome/browser/tab_contents/tab_contents.h"
16 #include "chrome/common/extensions/extension.h" 16 #include "chrome/common/extensions/extension.h"
17 #include "chrome/common/extensions/extension_error_utils.h" 17 #include "chrome/common/extensions/extension_error_utils.h"
18 #include "chrome/common/render_messages.h"
18 19
19 namespace keys = extension_page_actions_module_constants; 20 namespace keys = extension_page_actions_module_constants;
20 21
22 namespace {
23 // Errors.
24 const char kNoExtensionError[] = "No extension with id: *.";
25 const char kNoTabError[] = "No tab with id: *.";
26 const char kNoPageActionError[] =
27 "This extension has no page action specified.";
28 const char kUrlNotActiveError[] = "This url is no longer active: *.";
29 const char kIconIndexOutOfBounds[] = "Page action icon index out of bounds.";
30 }
31
32 // TODO(EXTENSIONS_DEPRECATED): obsolete API.
21 bool PageActionFunction::SetPageActionEnabled(bool enable) { 33 bool PageActionFunction::SetPageActionEnabled(bool enable) {
22 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); 34 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST));
23 const ListValue* args = static_cast<const ListValue*>(args_); 35 const ListValue* args = static_cast<const ListValue*>(args_);
24 36
25 std::string page_action_id; 37 std::string page_action_id;
26 EXTENSION_FUNCTION_VALIDATE(args->GetString(0, &page_action_id)); 38 EXTENSION_FUNCTION_VALIDATE(args->GetString(0, &page_action_id));
27 DictionaryValue* action; 39 DictionaryValue* action;
28 EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &action)); 40 EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &action));
29 41
30 int tab_id; 42 int tab_id;
(...skipping 10 matching lines...) Expand all
41 if (action->HasKey(keys::kIconIdKey)) { 53 if (action->HasKey(keys::kIconIdKey)) {
42 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kIconIdKey, 54 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kIconIdKey,
43 &icon_id)); 55 &icon_id));
44 } 56 }
45 } 57 }
46 58
47 // Find the TabContents that contains this tab id. 59 // Find the TabContents that contains this tab id.
48 TabContents* contents = NULL; 60 TabContents* contents = NULL;
49 ExtensionTabUtil::GetTabById(tab_id, profile(), NULL, NULL, &contents, NULL); 61 ExtensionTabUtil::GetTabById(tab_id, profile(), NULL, NULL, &contents, NULL);
50 if (!contents) { 62 if (!contents) {
51 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kNoTabError, 63 error_ = ExtensionErrorUtils::FormatErrorMessage(kNoTabError,
52 IntToString(tab_id)); 64 IntToString(tab_id));
53 return false; 65 return false;
54 } 66 }
55 67
56 // Make sure the URL hasn't changed. 68 // Make sure the URL hasn't changed.
57 NavigationEntry* entry = contents->controller().GetActiveEntry(); 69 NavigationEntry* entry = contents->controller().GetActiveEntry();
58 if (!entry || url != entry->url().spec()) { 70 if (!entry || url != entry->url().spec()) {
59 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kUrlNotActiveError, 71 error_ = ExtensionErrorUtils::FormatErrorMessage(kUrlNotActiveError, url);
60 url);
61 return false; 72 return false;
62 } 73 }
63 74
64 // Find our extension. 75 // Find our extension.
65 Extension* extension = NULL; 76 Extension* extension = NULL;
66 ExtensionsService* service = profile()->GetExtensionsService(); 77 ExtensionsService* service = profile()->GetExtensionsService();
67 extension = service->GetExtensionById(extension_id()); 78 extension = service->GetExtensionById(extension_id());
68 if (!extension) { 79 if (!extension) {
69 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kNoExtensionError, 80 error_ = ExtensionErrorUtils::FormatErrorMessage(kNoExtensionError,
70 extension_id()); 81 extension_id());
71 return false; 82 return false;
72 } 83 }
73 84
74 const ExtensionAction* page_action = extension->page_action(); 85 const ExtensionAction* page_action = extension->page_action();
75 if (!page_action) { 86 if (!page_action) {
76 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kNoPageActionError, 87 error_ = kNoPageActionError;
77 page_action_id);
78 return false; 88 return false;
79 } 89 }
80 90
81 // Set visibility and broadcast notifications that the UI should be updated. 91 // Set visibility and broadcast notifications that the UI should be updated.
82 contents->SetPageActionEnabled(page_action, enable, title, icon_id); 92 contents->SetPageActionEnabled(page_action, enable, title, icon_id);
83 contents->NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS); 93 contents->PageActionStateChanged();
84 94
85 return true; 95 return true;
86 } 96 }
87 97
98 bool PageActionFunction::InitCommon(int tab_id) {
99 page_action_ = dispatcher()->GetExtension()->page_action();
100 if (!page_action_) {
101 error_ = kNoPageActionError;
102 return false;
103 }
104
105 // Find the TabContents that contains this tab id.
106 contents_ = NULL;
107 ExtensionTabUtil::GetTabById(tab_id, profile(), NULL, NULL, &contents_, NULL);
108 if (!contents_) {
109 error_ = ExtensionErrorUtils::FormatErrorMessage(kNoTabError,
110 IntToString(tab_id));
111 return false;
112 }
113
114 state_ = contents_->GetOrCreatePageActionState(page_action_);
115 return true;
116 }
117
118 bool PageActionFunction::SetHidden(bool hidden) {
119 int tab_id;
120 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id));
121 if (!InitCommon(tab_id))
122 return false;
123
124 state_->set_hidden(hidden);
125 contents_->PageActionStateChanged();
126 return true;
127 }
128
88 bool EnablePageActionFunction::RunImpl() { 129 bool EnablePageActionFunction::RunImpl() {
89 return SetPageActionEnabled(true); 130 return SetPageActionEnabled(true);
90 } 131 }
91 132
92 bool DisablePageActionFunction::RunImpl() { 133 bool DisablePageActionFunction::RunImpl() {
93 return SetPageActionEnabled(false); 134 return SetPageActionEnabled(false);
94 } 135 }
136
137 bool PageActionShowFunction::RunImpl() {
138 return SetHidden(false);
139 }
140
141 bool PageActionHideFunction::RunImpl() {
142 return SetHidden(true);
143 }
144
145 bool PageActionSetIconFunction::RunImpl() {
146 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
147 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_);
148
149 int tab_id;
150 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id));
151 if (!InitCommon(tab_id))
152 return false;
153
154 // setIcon can take a variant argument: either a canvas ImageData, or an
155 // icon index.
156 BinaryValue* binary;
157 int icon_index;
158 if (args->GetBinary(L"imageData", &binary)) {
159 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize());
160 void* iter = NULL;
161 scoped_ptr<SkBitmap> bitmap(new SkBitmap);
162 EXTENSION_FUNCTION_VALIDATE(
163 IPC::ReadParam(&bitmap_pickle, &iter, bitmap.get()));
164 state_->set_icon(bitmap.release());
165 } else if (args->GetInteger(L"iconIndex", &icon_index)) {
166 if (icon_index < 0 ||
167 static_cast<size_t>(icon_index) >= page_action_->icon_paths().size()) {
168 error_ = kIconIndexOutOfBounds;
169 return false;
170 }
171 state_->set_icon(NULL);
172 state_->set_icon_index(icon_index);
173 } else {
174 EXTENSION_FUNCTION_VALIDATE(false);
175 }
176
177 contents_->PageActionStateChanged();
178 return true;
179 }
180
181 bool PageActionSetTitleFunction::RunImpl() {
182 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
183 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_);
184
185 int tab_id;
186 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id));
187 if (!InitCommon(tab_id))
188 return false;
189
190 std::string title;
191 EXTENSION_FUNCTION_VALIDATE(args->GetString(L"title", &title));
192
193 state_->set_title(title);
194 contents_->PageActionStateChanged();
195 return true;
196 }
197
198 bool PageActionSetBadgeBackgroundColorFunction::RunImpl() {
199 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
200 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_);
201
202 int tab_id;
203 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id));
204 if (!InitCommon(tab_id))
205 return false;
206
207 ListValue* color_value;
208 EXTENSION_FUNCTION_VALIDATE(args->GetList(L"color", &color_value));
209 EXTENSION_FUNCTION_VALIDATE(color_value->GetSize() == 4);
210
211 int color_array[4] = {0};
212 for (size_t i = 0; i < arraysize(color_array); ++i)
213 EXTENSION_FUNCTION_VALIDATE(color_value->GetInteger(i, &color_array[i]));
214
215 SkColor color = SkColorSetARGB(color_array[0], color_array[1], color_array[2],
216 color_array[3]);
217 state_->set_badge_background_color(color);
218 contents_->PageActionStateChanged();
219 return true;
220 }
221
222 bool PageActionSetBadgeTextColorFunction::RunImpl() {
223 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
224 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_);
225
226 int tab_id;
227 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id));
228 if (!InitCommon(tab_id))
229 return false;
230
231 ListValue* color_value;
232 EXTENSION_FUNCTION_VALIDATE(args->GetList(L"color", &color_value));
233 EXTENSION_FUNCTION_VALIDATE(color_value->GetSize() == 4);
234
235 int color_array[4] = {0};
236 for (size_t i = 0; i < arraysize(color_array); ++i)
237 EXTENSION_FUNCTION_VALIDATE(color_value->GetInteger(i, &color_array[i]));
238
239 // TODO(mpcomplete): implement text coloring.
240 return true;
241 }
242
243 bool PageActionSetBadgeTextFunction::RunImpl() {
244 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
245 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_);
246
247 int tab_id;
248 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id));
249 if (!InitCommon(tab_id))
250 return false;
251
252 std::string text;
253 EXTENSION_FUNCTION_VALIDATE(args->GetString(L"text", &text));
254
255 state_->set_badge_text(text);
256 contents_->PageActionStateChanged();
257 return true;
258 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698