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/api/extension_action/extension_action_api.cc

Issue 186013003: Persist browseraction properties across restarts (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove reliance on ID Created 6 years, 9 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) 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_action_api.h" 5 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/lazy_instance.h" 8 #include "base/lazy_instance.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 base::StringPiece raw_str( 131 base::StringPiece raw_str(
132 reinterpret_cast<const char*>(&data[0]), data.size()); 132 reinterpret_cast<const char*>(&data[0]), data.size());
133 std::string base64_str; 133 std::string base64_str;
134 base::Base64Encode(raw_str, &base64_str); 134 base::Base64Encode(raw_str, &base64_str);
135 return base64_str; 135 return base64_str;
136 } 136 }
137 137
138 // Set |action|'s default values to those specified in |dict|. 138 // Set |action|'s default values to those specified in |dict|.
139 void SetDefaultsFromValue(const base::DictionaryValue* dict, 139 void SetDefaultsFromValue(const base::DictionaryValue* dict,
140 ExtensionAction* action) { 140 ExtensionAction* action) {
141 const int kTabId = ExtensionAction::kDefaultTabId; 141 const int kTabId = ExtensionAction::kDefaultTabId;
Jeffrey Yasskin 2014/03/04 22:37:28 Arguably, this should be named 'kDefaultTabId'.
Devlin 2014/03/05 18:27:17 Done.
142 std::string str_value; 142 std::string str_value;
143 int int_value; 143 int int_value;
144 SkBitmap bitmap; 144 SkBitmap bitmap;
145 gfx::ImageSkia icon; 145 gfx::ImageSkia icon;
146 146
147 if (dict->GetString(kPopupUrlStorageKey, &str_value)) 147 // For each value, don't set it if it has been modified already.
148 if (dict->GetString(kPopupUrlStorageKey, &str_value) &&
149 !action->HasDefaultPopupUrl()) {
Jeffrey Yasskin 2014/03/04 22:11:11 I've gotten lost in the multiple-negatives. You ha
Devlin 2014/03/04 22:38:40 Note that there is _only_ HasPopupUrl, not Has*.
Jeffrey Yasskin 2014/03/04 22:49:20 I think that'll be more readable, so yes please.
Devlin 2014/03/05 18:27:17 Done.
148 action->SetPopupUrl(kTabId, GURL(str_value)); 150 action->SetPopupUrl(kTabId, GURL(str_value));
149 if (dict->GetString(kTitleStorageKey, &str_value)) 151 }
152 if (dict->GetString(kTitleStorageKey, &str_value) &&
153 !action->HasDefaultTitle()) {
150 action->SetTitle(kTabId, str_value); 154 action->SetTitle(kTabId, str_value);
151 if (dict->GetString(kBadgeTextStorageKey, &str_value)) 155 }
156 if (dict->GetString(kBadgeTextStorageKey, &str_value) &&
157 !action->HasDefaultBadgeText()) {
152 action->SetBadgeText(kTabId, str_value); 158 action->SetBadgeText(kTabId, str_value);
153 if (dict->GetString(kBadgeBackgroundColorStorageKey, &str_value)) 159 }
160 if (dict->GetString(kBadgeBackgroundColorStorageKey, &str_value) &&
161 !action->HasDefaultBadgeBackgroundColor()) {
154 action->SetBadgeBackgroundColor(kTabId, RawStringToSkColor(str_value)); 162 action->SetBadgeBackgroundColor(kTabId, RawStringToSkColor(str_value));
155 if (dict->GetString(kBadgeTextColorStorageKey, &str_value)) 163 }
164 if (dict->GetString(kBadgeTextColorStorageKey, &str_value) &&
165 !action->HasDefaultBadgeTextColor()) {
156 action->SetBadgeTextColor(kTabId, RawStringToSkColor(str_value)); 166 action->SetBadgeTextColor(kTabId, RawStringToSkColor(str_value));
157 if (dict->GetInteger(kAppearanceStorageKey, &int_value)) { 167 }
168 if (dict->GetInteger(kAppearanceStorageKey, &int_value) &&
169 !action->HasDefaultIsVisible()) {
158 switch (int_value) { 170 switch (int_value) {
159 case INVISIBLE: 171 case INVISIBLE:
160 case OBSOLETE_WANTS_ATTENTION: 172 case OBSOLETE_WANTS_ATTENTION:
161 action->SetIsVisible(kTabId, false); 173 action->SetIsVisible(kTabId, false);
162 case ACTIVE: 174 case ACTIVE:
163 action->SetIsVisible(kTabId, true); 175 action->SetIsVisible(kTabId, true);
164 } 176 }
165 } 177 }
166 178
167 const base::DictionaryValue* icon_value = NULL; 179 const base::DictionaryValue* icon_value = NULL;
168 if (dict->GetDictionary(kIconStorageKey, &icon_value)) { 180 if (dict->GetDictionary(kIconStorageKey, &icon_value) &&
181 !action->HasDefaultIcon()) {
169 for (size_t i = 0; i < arraysize(kIconSizes); i++) { 182 for (size_t i = 0; i < arraysize(kIconSizes); i++) {
170 if (icon_value->GetString(kIconSizes[i].size_string, &str_value) && 183 if (icon_value->GetString(kIconSizes[i].size_string, &str_value) &&
171 StringToSkBitmap(str_value, &bitmap)) { 184 StringToSkBitmap(str_value, &bitmap)) {
172 CHECK(!bitmap.isNull()); 185 CHECK(!bitmap.isNull());
173 float scale = ui::GetImageScale(kIconSizes[i].scale); 186 float scale = ui::GetImageScale(kIconSizes[i].scale);
174 icon.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale)); 187 icon.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale));
175 } 188 }
176 } 189 }
177 action->SetIcon(kTabId, gfx::Image(icon)); 190 action->SetIcon(kTabId, gfx::Image(icon));
178 } 191 }
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 } 446 }
434 break; 447 break;
435 } 448 }
436 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED: { 449 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED: {
437 ExtensionAction* extension_action = 450 ExtensionAction* extension_action =
438 content::Source<ExtensionAction>(source).ptr(); 451 content::Source<ExtensionAction>(source).ptr();
439 Profile* profile = content::Details<Profile>(details).ptr(); 452 Profile* profile = content::Details<Profile>(details).ptr();
440 if (profile != profile_) 453 if (profile != profile_)
441 break; 454 break;
442 455
443 extension_action->set_has_changed(true);
444 WriteToStorage(extension_action); 456 WriteToStorage(extension_action);
445 break; 457 break;
446 } 458 }
447 default: 459 default:
448 NOTREACHED(); 460 NOTREACHED();
449 break; 461 break;
450 } 462 }
451 } 463 }
452 464
453 void ExtensionActionStorageManager::WriteToStorage( 465 void ExtensionActionStorageManager::WriteToStorage(
(...skipping 14 matching lines...) Expand all
468 const Extension* extension = 480 const Extension* extension =
469 ExtensionSystem::Get(profile_)->extension_service()-> 481 ExtensionSystem::Get(profile_)->extension_service()->
470 extensions()->GetByID(extension_id); 482 extensions()->GetByID(extension_id);
471 if (!extension) 483 if (!extension)
472 return; 484 return;
473 485
474 ExtensionAction* browser_action = 486 ExtensionAction* browser_action =
475 ExtensionActionManager::Get(profile_)->GetBrowserAction(*extension); 487 ExtensionActionManager::Get(profile_)->GetBrowserAction(*extension);
476 CHECK(browser_action); 488 CHECK(browser_action);
477 489
478 // Don't load values from storage if the extension has updated a value
479 // already. The extension may have only updated some of the values, but
480 // this is a good first approximation. If the extension is doing stuff
481 // to the browser action, we can assume it is ready to take over.
482 if (browser_action->has_changed())
483 return;
484
485 const base::DictionaryValue* dict = NULL; 490 const base::DictionaryValue* dict = NULL;
486 if (!value.get() || !value->GetAsDictionary(&dict)) 491 if (!value.get() || !value->GetAsDictionary(&dict))
487 return; 492 return;
488 493
489 SetDefaultsFromValue(dict, browser_action); 494 SetDefaultsFromValue(dict, browser_action);
490 } 495 }
491 496
492 // 497 //
493 // ExtensionActionFunction 498 // ExtensionActionFunction
494 // 499 //
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after
938 return true; 943 return true;
939 } 944 }
940 945
941 bool EnablePageActionsFunction::RunImpl() { 946 bool EnablePageActionsFunction::RunImpl() {
942 return SetPageActionEnabled(true); 947 return SetPageActionEnabled(true);
943 } 948 }
944 949
945 bool DisablePageActionsFunction::RunImpl() { 950 bool DisablePageActionsFunction::RunImpl() {
946 return SetPageActionEnabled(false); 951 return SetPageActionEnabled(false);
947 } 952 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698