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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
// Copyright (c) 2012 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/api/extension_action/extension_actions_api.h"
#include <string>
#include "base/base64.h"
#include "base/string_number_conversions.h"
#include "base/string_piece.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/extension_action/extension_page_actions_api_constants.h"
#include "chrome/browser/extensions/extension_action.h"
#include "chrome/browser/extensions/extension_action_manager.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/extensions/location_bar_controller.h"
#include "chrome/browser/extensions/state_store.h"
#include "chrome/browser/extensions/tab_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_error_utils.h"
#include "chrome/common/render_messages.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_service.h"
namespace {
const char kBrowserActionStorageKey[] = "browser_action";
const char kPopupUrlStorageKey[] = "poupup_url";
const char kTitleStorageKey[] = "title";
const char kIconStorageKey[] = "icon";
const char kBadgeTextStorageKey[] = "badge_text";
const char kBadgeBackgroundColorStorageKey[] = "badge_background_color";
const char kBadgeTextColorStorageKey[] = "badge_text_color";
const char kAppearanceStorageKey[] = "appearance";
// Errors.
const char kNoExtensionActionError[] =
"This extension has no action specified.";
const char kNoTabError[] = "No tab with id: *.";
struct IconRepresentationInfo {
// Size as a string that will be used to retrieve representation value from
// SetIcon function arguments.
const char* size_string;
// Scale factor for which the represantion should be used.
ui::ScaleFactor scale;
};
const IconRepresentationInfo kIconSizes[] = {
{ "19", ui::SCALE_FACTOR_100P },
{ "38", ui::SCALE_FACTOR_200P }
};
// Conversion function for reading/writing to storage.
SkColor RawStringToSkColor(const std::string& str) {
uint64 value = 0;
base::StringToUint64(str, &value);
SkColor color = static_cast<SkColor>(value);
DCHECK(value == color); // ensure value fits into color's 32 bits
return color;
}
// Conversion function for reading/writing to storage.
std::string SkColorToRawString(SkColor color) {
return base::Uint64ToString(color);
}
// Conversion function for reading/writing to storage.
bool StringToSkBitmap(const std::string& str, SkBitmap* bitmap) {
// TODO(mpcomplete): Remove the base64 encode/decode step when
// http://crbug.com/140546 is fixed.
std::string raw_str;
if (!base::Base64Decode(str, &raw_str))
return false;
IPC::Message bitmap_pickle(raw_str.data(), raw_str.size());
PickleIterator iter(bitmap_pickle);
return IPC::ReadParam(&bitmap_pickle, &iter, bitmap);
}
// Conversion function for reading/writing to storage.
std::string RepresentationToString(const gfx::ImageSkia& image,
ui::ScaleFactor scale) {
SkBitmap bitmap = image.GetRepresentation(scale).sk_bitmap();
IPC::Message bitmap_pickle;
IPC::WriteParam(&bitmap_pickle, bitmap);
std::string raw_str(static_cast<const char*>(bitmap_pickle.data()),
bitmap_pickle.size());
std::string base64_str;
if (!base::Base64Encode(raw_str, &base64_str))
return std::string();
return base64_str;
}
// Set |action|'s default values to those specified in |dict|.
void SetDefaultsFromValue(const base::DictionaryValue* dict,
ExtensionAction* action) {
const int kTabId = ExtensionAction::kDefaultTabId;
std::string str_value;
int int_value;
SkBitmap bitmap;
gfx::ImageSkia icon;
if (dict->GetString(kPopupUrlStorageKey, &str_value))
action->SetPopupUrl(kTabId, GURL(str_value));
if (dict->GetString(kTitleStorageKey, &str_value))
action->SetTitle(kTabId, str_value);
if (dict->GetString(kBadgeTextStorageKey, &str_value))
action->SetBadgeText(kTabId, str_value);
if (dict->GetString(kBadgeBackgroundColorStorageKey, &str_value))
action->SetBadgeBackgroundColor(kTabId, RawStringToSkColor(str_value));
if (dict->GetString(kBadgeTextColorStorageKey, &str_value))
action->SetBadgeTextColor(kTabId, RawStringToSkColor(str_value));
if (dict->GetInteger(kAppearanceStorageKey, &int_value))
action->SetAppearance(kTabId,
static_cast<ExtensionAction::Appearance>(int_value));
const base::DictionaryValue* icon_value = NULL;
if (dict->GetDictionary(kIconStorageKey, &icon_value)) {
for (size_t i = 0; i < arraysize(kIconSizes); i++) {
if (icon_value->GetString(kIconSizes[i].size_string, &str_value) &&
StringToSkBitmap(str_value, &bitmap)) {
CHECK(!bitmap.isNull());
icon.AddRepresentation(gfx::ImageSkiaRep(bitmap, kIconSizes[i].scale));
}
}
action->SetIcon(kTabId, gfx::Image(icon));
}
}
// Store |action|'s default values in a DictionaryValue for use in storing to
// disk.
scoped_ptr<base::DictionaryValue> DefaultsToValue(ExtensionAction* action) {
const int kTabId = ExtensionAction::kDefaultTabId;
scoped_ptr<base::DictionaryValue> dict(new DictionaryValue());
dict->SetString(kPopupUrlStorageKey, action->GetPopupUrl(kTabId).spec());
dict->SetString(kTitleStorageKey, action->GetTitle(kTabId));
dict->SetString(kBadgeTextStorageKey, action->GetBadgeText(kTabId));
dict->SetString(kBadgeBackgroundColorStorageKey,
SkColorToRawString(action->GetBadgeBackgroundColor(kTabId)));
dict->SetString(kBadgeTextColorStorageKey,
SkColorToRawString(action->GetBadgeTextColor(kTabId)));
dict->SetInteger(kAppearanceStorageKey,
action->GetIsVisible(kTabId) ?
ExtensionAction::ACTIVE : ExtensionAction::INVISIBLE);
gfx::ImageSkia icon = action->GetExplicitlySetIcon(kTabId);
if (!icon.isNull()) {
base::DictionaryValue* icon_value = new base::DictionaryValue();
for (size_t i = 0; i < arraysize(kIconSizes); i++) {
if (icon.HasRepresentation(kIconSizes[i].scale)) {
icon_value->SetString(
kIconSizes[i].size_string,
RepresentationToString(icon, kIconSizes[i].scale));
}
}
dict->Set(kIconStorageKey, icon_value);
}
return dict.Pass();
}
} // namespace
namespace extensions {
//
// ExtensionActionStorageManager
//
ExtensionActionStorageManager::ExtensionActionStorageManager(Profile* profile)
: profile_(profile) {
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
content::Source<Profile>(profile_));
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED,
content::NotificationService::AllBrowserContextsAndSources());
StateStore* storage = ExtensionSystem::Get(profile_)->state_store();
if (storage)
storage->RegisterKey(kBrowserActionStorageKey);
}
ExtensionActionStorageManager::~ExtensionActionStorageManager() {
}
void ExtensionActionStorageManager::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_EXTENSION_LOADED: {
const Extension* extension =
content::Details<const Extension>(details).ptr();
if (!extensions::ExtensionActionManager::Get(profile_)->
GetBrowserAction(*extension)) {
break;
}
StateStore* storage = ExtensionSystem::Get(profile_)->state_store();
if (storage) {
storage->GetExtensionValue(extension->id(), kBrowserActionStorageKey,
base::Bind(&ExtensionActionStorageManager::ReadFromStorage,
AsWeakPtr(), extension->id()));
}
break;
}
case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED: {
ExtensionAction* extension_action =
content::Source<ExtensionAction>(source).ptr();
Profile* profile = content::Details<Profile>(details).ptr();
if (profile != profile_)
break;
extension_action->set_has_changed(true);
WriteToStorage(extension_action);
break;
}
default:
NOTREACHED();
break;
}
}
void ExtensionActionStorageManager::WriteToStorage(
ExtensionAction* extension_action) {
StateStore* storage = ExtensionSystem::Get(profile_)->state_store();
if (!storage)
return;
scoped_ptr<base::DictionaryValue> defaults =
DefaultsToValue(extension_action);
storage->SetExtensionValue(extension_action->extension_id(),
kBrowserActionStorageKey,
defaults.PassAs<base::Value>());
}
void ExtensionActionStorageManager::ReadFromStorage(
const std::string& extension_id, scoped_ptr<base::Value> value) {
const Extension* extension =
ExtensionSystem::Get(profile_)->extension_service()->
extensions()->GetByID(extension_id);
if (!extension)
return;
ExtensionAction* browser_action =
ExtensionActionManager::Get(profile_)->GetBrowserAction(*extension);
CHECK(browser_action);
// Don't load values from storage if the extension has updated a value
// already. The extension may have only updated some of the values, but
// this is a good first approximation. If the extension is doing stuff
// to the browser action, we can assume it is ready to take over.
if (browser_action->has_changed())
return;
const base::DictionaryValue* dict = NULL;
if (!value.get() || !value->GetAsDictionary(&dict))
return;
SetDefaultsFromValue(dict, browser_action);
}
} // namespace extensions
//
// ExtensionActionFunction
//
ExtensionActionFunction::ExtensionActionFunction()
: details_(NULL),
tab_id_(ExtensionAction::kDefaultTabId),
contents_(NULL),
extension_action_(NULL) {
}
ExtensionActionFunction::~ExtensionActionFunction() {
}
bool ExtensionActionFunction::RunImpl() {
if (base::StringPiece(name()).starts_with("scriptBadge.")) {
extension_action_ = extensions::ExtensionActionManager::Get(profile_)->
GetScriptBadge(*GetExtension());
} else {
extension_action_ = extensions::ExtensionActionManager::Get(profile_)->
GetBrowserAction(*GetExtension());
if (!extension_action_) {
extension_action_ = extensions::ExtensionActionManager::Get(profile_)->
GetPageAction(*GetExtension());
}
}
if (!extension_action_) {
// TODO(kalman): ideally the browserAction/pageAction APIs wouldn't event
// exist for extensions that don't have one declared. This should come as
// part of the Feature system.
error_ = kNoExtensionActionError;
return false;
}
// There may or may not be details (depends on the function).
// The tabId might appear in details (if it exists) or as the first
// argument besides the action type (depends on the function).
{
base::Value* first_arg = NULL;
EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &first_arg));
switch (first_arg->GetType()) {
case Value::TYPE_INTEGER:
CHECK(first_arg->GetAsInteger(&tab_id_));
break;
case Value::TYPE_DICTIONARY: {
details_ = static_cast<base::DictionaryValue*>(first_arg);
base::Value* tab_id_value = NULL;
if (details_->Get("tabId", &tab_id_value)) {
switch (tab_id_value->GetType()) {
case Value::TYPE_NULL:
// Fine, equivalent to it being not-there, and tabId is optional.
break;
case Value::TYPE_INTEGER:
CHECK(tab_id_value->GetAsInteger(&tab_id_));
break;
default:
// Boom.
EXTENSION_FUNCTION_VALIDATE(false);
}
}
break;
}
case Value::TYPE_NULL:
// The tabId might be an optional argument.
break;
default:
EXTENSION_FUNCTION_VALIDATE(false);
}
}
// Find the WebContents that contains this tab id if one is required.
if (tab_id_ == ExtensionAction::kDefaultTabId) {
EXTENSION_FUNCTION_VALIDATE(
extensions::ExtensionActionManager::Get(profile_)->
GetBrowserAction(*GetExtension()));
} else {
ExtensionTabUtil::GetTabById(
tab_id_, profile(), include_incognito(), NULL, NULL, &contents_, NULL);
if (!contents_) {
error_ = ExtensionErrorUtils::FormatErrorMessage(
kNoTabError, base::IntToString(tab_id_));
return false;
}
}
return RunExtensionAction();
}
void ExtensionActionFunction::NotifyChange() {
switch (extension_action_->action_type()) {
case extensions::Extension::ActionInfo::TYPE_BROWSER:
case extensions::Extension::ActionInfo::TYPE_PAGE:
if (extensions::ExtensionActionManager::Get(profile_)->
GetBrowserAction(*extension_)) {
NotifyBrowserActionChange();
} else if (extensions::ExtensionActionManager::Get(profile_)->
GetPageAction(*extension_)) {
NotifyLocationBarChange();
}
return;
case extensions::Extension::ActionInfo::TYPE_SCRIPT_BADGE:
NotifyLocationBarChange();
return;
}
NOTREACHED();
}
void ExtensionActionFunction::NotifyBrowserActionChange() {
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED,
content::Source<ExtensionAction>(extension_action_),
content::Details<Profile>(profile()));
}
void ExtensionActionFunction::NotifyLocationBarChange() {
extensions::TabHelper::FromWebContents(contents_)->
location_bar_controller()->NotifyChange();
}
// static
bool ExtensionActionFunction::ParseCSSColorString(
const std::string& color_string,
SkColor* result) {
std::string formatted_color = "#";
// Check the string for incorrect formatting.
if (color_string[0] != '#')
return false;
// Convert the string from #FFF format to #FFFFFF format.
if (color_string.length() == 4) {
for (size_t i = 1; i < color_string.length(); i++) {
formatted_color += color_string[i];
formatted_color += color_string[i];
}
} else {
formatted_color = color_string;
}
if (formatted_color.length() != 7)
return false;
// Convert the string to an integer and make sure it is in the correct value
// range.
int color_ints[3] = {0};
for (int i = 0; i < 3; i++) {
if (!base::HexStringToInt(formatted_color.substr(1 + (2 * i), 2),
color_ints + i))
return false;
if (color_ints[i] > 255 || color_ints[i] < 0)
return false;
}
*result = SkColorSetARGB(255, color_ints[0], color_ints[1], color_ints[2]);
return true;
}
bool ExtensionActionFunction::SetVisible(bool visible) {
extension_action_->SetAppearance(
tab_id_, visible ? ExtensionAction::ACTIVE : ExtensionAction::INVISIBLE);
NotifyChange();
return true;
}
extensions::TabHelper& ExtensionActionFunction::tab_helper() const {
CHECK(contents_);
return *extensions::TabHelper::FromWebContents(contents_);
}
bool ExtensionActionShowFunction::RunExtensionAction() {
return SetVisible(true);
}
bool ExtensionActionHideFunction::RunExtensionAction() {
return SetVisible(false);
}
bool ExtensionActionSetIconFunction::RunExtensionAction() {
// setIcon can take a variant argument: either a dictionary of canvas
// ImageData, or an icon index.
base::DictionaryValue* canvas_set = NULL;
int icon_index;
if (details_->GetDictionary("imageData", &canvas_set)) {
gfx::ImageSkia icon;
// Extract icon representations from the ImageDataSet dictionary.
for (size_t i = 0; i < arraysize(kIconSizes); i++) {
base::BinaryValue* binary;
if (canvas_set->GetBinary(kIconSizes[i].size_string, &binary)) {
IPC::Message pickle(binary->GetBuffer(), binary->GetSize());
PickleIterator iter(pickle);
SkBitmap bitmap;
EXTENSION_FUNCTION_VALIDATE(IPC::ReadParam(&pickle, &iter, &bitmap));
CHECK(!bitmap.isNull());
icon.AddRepresentation(gfx::ImageSkiaRep(bitmap, kIconSizes[i].scale));
}
}
extension_action_->SetIcon(tab_id_, gfx::Image(icon));
} else if (details_->GetInteger("iconIndex", &icon_index)) {
// Obsolete argument: ignore it.
return true;
} else {
EXTENSION_FUNCTION_VALIDATE(false);
}
NotifyChange();
return true;
}
bool ExtensionActionSetTitleFunction::RunExtensionAction() {
std::string title;
EXTENSION_FUNCTION_VALIDATE(details_->GetString("title", &title));
extension_action_->SetTitle(tab_id_, title);
NotifyChange();
return true;
}
bool ExtensionActionSetPopupFunction::RunExtensionAction() {
std::string popup_string;
EXTENSION_FUNCTION_VALIDATE(details_->GetString("popup", &popup_string));
GURL popup_url;
if (!popup_string.empty())
popup_url = GetExtension()->GetResourceURL(popup_string);
extension_action_->SetPopupUrl(tab_id_, popup_url);
NotifyChange();
return true;
}
bool ExtensionActionSetBadgeTextFunction::RunExtensionAction() {
std::string badge_text;
EXTENSION_FUNCTION_VALIDATE(details_->GetString("text", &badge_text));
extension_action_->SetBadgeText(tab_id_, badge_text);
NotifyChange();
return true;
}
bool ExtensionActionSetBadgeBackgroundColorFunction::RunExtensionAction() {
Value* color_value = NULL;
details_->Get("color", &color_value);
SkColor color = 0;
if (color_value->IsType(Value::TYPE_LIST)) {
ListValue* list = NULL;
EXTENSION_FUNCTION_VALIDATE(details_->GetList("color", &list));
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]));
}
color = SkColorSetARGB(color_array[3], color_array[0],
color_array[1], color_array[2]);
} else if (color_value->IsType(Value::TYPE_STRING)) {
std::string color_string;
EXTENSION_FUNCTION_VALIDATE(details_->GetString("color", &color_string));
if (!ParseCSSColorString(color_string, &color))
return false;
}
extension_action_->SetBadgeBackgroundColor(tab_id_, color);
NotifyChange();
return true;
}
bool ExtensionActionGetTitleFunction::RunExtensionAction() {
SetResult(Value::CreateStringValue(extension_action_->GetTitle(tab_id_)));
return true;
}
bool ExtensionActionGetPopupFunction::RunExtensionAction() {
SetResult(
Value::CreateStringValue(extension_action_->GetPopupUrl(tab_id_).spec()));
return true;
}
bool ExtensionActionGetBadgeTextFunction::RunExtensionAction() {
SetResult(Value::CreateStringValue(extension_action_->GetBadgeText(tab_id_)));
return true;
}
bool ExtensionActionGetBadgeBackgroundColorFunction::RunExtensionAction() {
ListValue* list = new ListValue();
SkColor color = extension_action_->GetBadgeBackgroundColor(tab_id_);
list->Append(Value::CreateIntegerValue(SkColorGetR(color)));
list->Append(Value::CreateIntegerValue(SkColorGetG(color)));
list->Append(Value::CreateIntegerValue(SkColorGetB(color)));
list->Append(Value::CreateIntegerValue(SkColorGetA(color)));
SetResult(list);
return true;
}
|