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

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

Issue 332021: Move page actions over to ExtensionAction2 (Closed)
Patch Set: Review feedback Created 11 years, 1 month 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"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 if (enable) { 49 if (enable) {
50 // Both of those are optional. 50 // Both of those are optional.
51 if (action->HasKey(keys::kTitleKey)) 51 if (action->HasKey(keys::kTitleKey))
52 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kTitleKey, &title)); 52 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kTitleKey, &title));
53 if (action->HasKey(keys::kIconIdKey)) { 53 if (action->HasKey(keys::kIconIdKey)) {
54 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kIconIdKey, 54 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kIconIdKey,
55 &icon_id)); 55 &icon_id));
56 } 56 }
57 } 57 }
58 58
59 const ExtensionAction* page_action = 59 ExtensionAction2* page_action = dispatcher()->GetExtension()->page_action();
60 dispatcher()->GetExtension()->page_action();
61 if (!page_action) { 60 if (!page_action) {
62 error_ = kNoPageActionError; 61 error_ = kNoPageActionError;
63 return false; 62 return false;
64 } 63 }
65 64
66 if (icon_id < 0 || 65 if (icon_id < 0 ||
67 static_cast<size_t>(icon_id) >= page_action->icon_paths().size()) { 66 static_cast<size_t>(icon_id) >= page_action->icon_paths()->size()) {
68 error_ = (icon_id == 0) ? kNoIconSpecified : kIconIndexOutOfBounds; 67 error_ = (icon_id == 0) ? kNoIconSpecified : kIconIndexOutOfBounds;
69 return false; 68 return false;
70 } 69 }
71 70
72 // Find the TabContents that contains this tab id. 71 // Find the TabContents that contains this tab id.
73 TabContents* contents = NULL; 72 TabContents* contents = NULL;
74 ExtensionTabUtil::GetTabById(tab_id, profile(), NULL, NULL, &contents, NULL); 73 ExtensionTabUtil::GetTabById(tab_id, profile(), NULL, NULL, &contents, NULL);
75 if (!contents) { 74 if (!contents) {
76 error_ = ExtensionErrorUtils::FormatErrorMessage(kNoTabError, 75 error_ = ExtensionErrorUtils::FormatErrorMessage(kNoTabError,
77 IntToString(tab_id)); 76 IntToString(tab_id));
78 return false; 77 return false;
79 } 78 }
80 79
81 // Make sure the URL hasn't changed. 80 // Make sure the URL hasn't changed.
82 NavigationEntry* entry = contents->controller().GetActiveEntry(); 81 NavigationEntry* entry = contents->controller().GetActiveEntry();
83 if (!entry || url != entry->url().spec()) { 82 if (!entry || url != entry->url().spec()) {
84 error_ = ExtensionErrorUtils::FormatErrorMessage(kUrlNotActiveError, url); 83 error_ = ExtensionErrorUtils::FormatErrorMessage(kUrlNotActiveError, url);
85 return false; 84 return false;
86 } 85 }
87 86
88 // Set visibility and broadcast notifications that the UI should be updated. 87 // Set visibility and broadcast notifications that the UI should be updated.
89 contents->SetPageActionEnabled(page_action, enable, title, icon_id); 88 page_action->SetIsVisible(tab_id, enable);
89 page_action->SetTitle(tab_id, title);
90 page_action->SetIconIndex(tab_id, icon_id);
90 contents->PageActionStateChanged(); 91 contents->PageActionStateChanged();
91 92
92 return true; 93 return true;
93 } 94 }
94 95
95 bool PageActionFunction::InitCommon(int tab_id) { 96 bool PageActionFunction::InitCommon(int tab_id) {
96 page_action_ = dispatcher()->GetExtension()->page_action(); 97 page_action_ = dispatcher()->GetExtension()->page_action();
97 if (!page_action_) { 98 if (!page_action_) {
98 error_ = kNoPageActionError; 99 error_ = kNoPageActionError;
99 return false; 100 return false;
100 } 101 }
101 102
102 // Find the TabContents that contains this tab id. 103 // Find the TabContents that contains this tab id.
103 contents_ = NULL; 104 contents_ = NULL;
104 ExtensionTabUtil::GetTabById(tab_id, profile(), NULL, NULL, &contents_, NULL); 105 ExtensionTabUtil::GetTabById(tab_id, profile(), NULL, NULL, &contents_, NULL);
105 if (!contents_) { 106 if (!contents_) {
106 error_ = ExtensionErrorUtils::FormatErrorMessage(kNoTabError, 107 error_ = ExtensionErrorUtils::FormatErrorMessage(kNoTabError,
107 IntToString(tab_id)); 108 IntToString(tab_id));
108 return false; 109 return false;
109 } 110 }
110 111
111 state_ = contents_->GetOrCreatePageActionState(page_action_);
112 return true; 112 return true;
113 } 113 }
114 114
115 bool PageActionFunction::SetHidden(bool hidden) { 115 bool PageActionFunction::SetVisible(bool visible) {
116 int tab_id; 116 int tab_id;
117 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id)); 117 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id));
118 if (!InitCommon(tab_id)) 118 if (!InitCommon(tab_id))
119 return false; 119 return false;
120 120
121 state_->set_hidden(hidden); 121 page_action_->SetIsVisible(tab_id, visible);
122 contents_->PageActionStateChanged(); 122 contents_->PageActionStateChanged();
123 return true; 123 return true;
124 } 124 }
125 125
126 bool EnablePageActionFunction::RunImpl() { 126 bool EnablePageActionFunction::RunImpl() {
127 return SetPageActionEnabled(true); 127 return SetPageActionEnabled(true);
128 } 128 }
129 129
130 bool DisablePageActionFunction::RunImpl() { 130 bool DisablePageActionFunction::RunImpl() {
131 return SetPageActionEnabled(false); 131 return SetPageActionEnabled(false);
132 } 132 }
133 133
134 bool PageActionShowFunction::RunImpl() { 134 bool PageActionShowFunction::RunImpl() {
135 return SetHidden(false); 135 return SetVisible(true);
136 } 136 }
137 137
138 bool PageActionHideFunction::RunImpl() { 138 bool PageActionHideFunction::RunImpl() {
139 return SetHidden(true); 139 return SetVisible(false);
140 } 140 }
141 141
142 bool PageActionSetIconFunction::RunImpl() { 142 bool PageActionSetIconFunction::RunImpl() {
143 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); 143 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
144 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); 144 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_);
145 145
146 int tab_id; 146 int tab_id;
147 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); 147 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id));
148 if (!InitCommon(tab_id)) 148 if (!InitCommon(tab_id))
149 return false; 149 return false;
150 150
151 // setIcon can take a variant argument: either a canvas ImageData, or an 151 // setIcon can take a variant argument: either a canvas ImageData, or an
152 // icon index. 152 // icon index.
153 BinaryValue* binary; 153 BinaryValue* binary;
154 int icon_index; 154 int icon_index;
155 if (args->GetBinary(L"imageData", &binary)) { 155 if (args->GetBinary(L"imageData", &binary)) {
156 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize()); 156 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize());
157 void* iter = NULL; 157 void* iter = NULL;
158 scoped_ptr<SkBitmap> bitmap(new SkBitmap); 158 scoped_ptr<SkBitmap> bitmap(new SkBitmap);
159 EXTENSION_FUNCTION_VALIDATE( 159 EXTENSION_FUNCTION_VALIDATE(
160 IPC::ReadParam(&bitmap_pickle, &iter, bitmap.get())); 160 IPC::ReadParam(&bitmap_pickle, &iter, bitmap.get()));
161 state_->set_icon(bitmap.release()); 161 page_action_->SetIcon(tab_id, *bitmap);
162 } else if (args->GetInteger(L"iconIndex", &icon_index)) { 162 } else if (args->GetInteger(L"iconIndex", &icon_index)) {
163 if (icon_index < 0 || 163 if (icon_index < 0 || static_cast<size_t>(icon_index) >=
164 static_cast<size_t>(icon_index) >= page_action_->icon_paths().size()) { 164 page_action_->icon_paths()->size()) {
165 error_ = kIconIndexOutOfBounds; 165 error_ = kIconIndexOutOfBounds;
166 return false; 166 return false;
167 } 167 }
168 state_->set_icon(NULL); 168 page_action_->SetIcon(tab_id, SkBitmap());
169 state_->set_icon_index(icon_index); 169 page_action_->SetIconIndex(tab_id, icon_index);
170 } else { 170 } else {
171 EXTENSION_FUNCTION_VALIDATE(false); 171 EXTENSION_FUNCTION_VALIDATE(false);
172 } 172 }
173 173
174 contents_->PageActionStateChanged(); 174 contents_->PageActionStateChanged();
175 return true; 175 return true;
176 } 176 }
177 177
178 bool PageActionSetTitleFunction::RunImpl() { 178 bool PageActionSetTitleFunction::RunImpl() {
179 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); 179 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
180 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); 180 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_);
181 181
182 int tab_id; 182 int tab_id;
183 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); 183 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id));
184 if (!InitCommon(tab_id)) 184 if (!InitCommon(tab_id))
185 return false; 185 return false;
186 186
187 std::string title; 187 std::string title;
188 EXTENSION_FUNCTION_VALIDATE(args->GetString(L"title", &title)); 188 EXTENSION_FUNCTION_VALIDATE(args->GetString(L"title", &title));
189 189
190 state_->set_title(title); 190 page_action_->SetTitle(tab_id, title);
191 contents_->PageActionStateChanged(); 191 contents_->PageActionStateChanged();
192 return true; 192 return true;
193 } 193 }
194 194
195 // Not currently exposed to extensions. To re-enable, add mapping in 195 // Not currently exposed to extensions. To re-enable, add mapping in
196 // extension_function_dispatcher. 196 // extension_function_dispatcher.
197 bool PageActionSetBadgeBackgroundColorFunction::RunImpl() { 197 bool PageActionSetBadgeBackgroundColorFunction::RunImpl() {
198 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); 198 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
199 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); 199 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_);
200 200
201 int tab_id; 201 int tab_id;
202 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); 202 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id));
203 if (!InitCommon(tab_id)) 203 if (!InitCommon(tab_id))
204 return false; 204 return false;
205 205
206 ListValue* color_value; 206 ListValue* color_value;
207 EXTENSION_FUNCTION_VALIDATE(args->GetList(L"color", &color_value)); 207 EXTENSION_FUNCTION_VALIDATE(args->GetList(L"color", &color_value));
208 EXTENSION_FUNCTION_VALIDATE(color_value->GetSize() == 4); 208 EXTENSION_FUNCTION_VALIDATE(color_value->GetSize() == 4);
209 209
210 int color_array[4] = {0}; 210 int color_array[4] = {0};
211 for (size_t i = 0; i < arraysize(color_array); ++i) 211 for (size_t i = 0; i < arraysize(color_array); ++i)
212 EXTENSION_FUNCTION_VALIDATE(color_value->GetInteger(i, &color_array[i])); 212 EXTENSION_FUNCTION_VALIDATE(color_value->GetInteger(i, &color_array[i]));
213 213
214 SkColor color = SkColorSetARGB(color_array[3], color_array[0], color_array[1], 214 SkColor color = SkColorSetARGB(color_array[3], color_array[0], color_array[1],
215 color_array[2]); 215 color_array[2]);
216 state_->set_badge_background_color(color); 216 page_action_->SetBadgeBackgroundColor(tab_id, color);
217 contents_->PageActionStateChanged(); 217 contents_->PageActionStateChanged();
218 return true; 218 return true;
219 } 219 }
220 220
221 // Not currently exposed to extensions. To re-enable, add mapping in 221 // Not currently exposed to extensions. To re-enable, add mapping in
222 // extension_function_dispatcher. 222 // extension_function_dispatcher.
223 bool PageActionSetBadgeTextColorFunction::RunImpl() { 223 bool PageActionSetBadgeTextColorFunction::RunImpl() {
224 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); 224 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
225 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); 225 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_);
226 226
227 int tab_id; 227 int tab_id;
228 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); 228 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id));
229 if (!InitCommon(tab_id)) 229 if (!InitCommon(tab_id))
230 return false; 230 return false;
231 231
232 ListValue* color_value; 232 ListValue* color_value;
233 EXTENSION_FUNCTION_VALIDATE(args->GetList(L"color", &color_value)); 233 EXTENSION_FUNCTION_VALIDATE(args->GetList(L"color", &color_value));
234 EXTENSION_FUNCTION_VALIDATE(color_value->GetSize() == 4); 234 EXTENSION_FUNCTION_VALIDATE(color_value->GetSize() == 4);
235 235
236 int color_array[4] = {0}; 236 int color_array[4] = {0};
237 for (size_t i = 0; i < arraysize(color_array); ++i) 237 for (size_t i = 0; i < arraysize(color_array); ++i)
238 EXTENSION_FUNCTION_VALIDATE(color_value->GetInteger(i, &color_array[i])); 238 EXTENSION_FUNCTION_VALIDATE(color_value->GetInteger(i, &color_array[i]));
239 239
240 SkColor color = SkColorSetARGB(color_array[3], color_array[0], color_array[1], 240 SkColor color = SkColorSetARGB(color_array[3], color_array[0], color_array[1],
241 color_array[2]); 241 color_array[2]);
242 state_->set_badge_text_color(color); 242 page_action_->SetBadgeTextColor(tab_id, color);
243 contents_->PageActionStateChanged(); 243 contents_->PageActionStateChanged();
244 return true; 244 return true;
245 } 245 }
246 246
247 // Not currently exposed to extensions. To re-enable, add mapping in 247 // Not currently exposed to extensions. To re-enable, add mapping in
248 // extension_function_dispatcher. 248 // extension_function_dispatcher.
249 bool PageActionSetBadgeTextFunction::RunImpl() { 249 bool PageActionSetBadgeTextFunction::RunImpl() {
250 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); 250 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
251 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_); 251 const DictionaryValue* args = static_cast<const DictionaryValue*>(args_);
252 252
253 int tab_id; 253 int tab_id;
254 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); 254 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id));
255 if (!InitCommon(tab_id)) 255 if (!InitCommon(tab_id))
256 return false; 256 return false;
257 257
258 std::string text; 258 std::string text;
259 EXTENSION_FUNCTION_VALIDATE(args->GetString(L"text", &text)); 259 EXTENSION_FUNCTION_VALIDATE(args->GetString(L"text", &text));
260 260
261 state_->set_badge_text(text); 261 page_action_->SetBadgeText(tab_id, text);
262 contents_->PageActionStateChanged(); 262 contents_->PageActionStateChanged();
263 return true; 263 return true;
264 } 264 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698