OLD | NEW |
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 Loading... |
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 kDefaultTabId = ExtensionAction::kDefaultTabId; | 141 const int kTabId = ExtensionAction::kDefaultTabId; |
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 // For each value, don't set it if it has been modified already. | 147 if (dict->GetString(kPopupUrlStorageKey, &str_value)) |
148 if (dict->GetString(kPopupUrlStorageKey, &str_value) && | 148 action->SetPopupUrl(kTabId, GURL(str_value)); |
149 !action->HasPopupUrl(kDefaultTabId)) { | 149 if (dict->GetString(kTitleStorageKey, &str_value)) |
150 action->SetPopupUrl(kDefaultTabId, GURL(str_value)); | 150 action->SetTitle(kTabId, str_value); |
151 } | 151 if (dict->GetString(kBadgeTextStorageKey, &str_value)) |
152 if (dict->GetString(kTitleStorageKey, &str_value) && | 152 action->SetBadgeText(kTabId, str_value); |
153 !action->HasTitle(kDefaultTabId)) { | 153 if (dict->GetString(kBadgeBackgroundColorStorageKey, &str_value)) |
154 action->SetTitle(kDefaultTabId, str_value); | 154 action->SetBadgeBackgroundColor(kTabId, RawStringToSkColor(str_value)); |
155 } | 155 if (dict->GetString(kBadgeTextColorStorageKey, &str_value)) |
156 if (dict->GetString(kBadgeTextStorageKey, &str_value) && | 156 action->SetBadgeTextColor(kTabId, RawStringToSkColor(str_value)); |
157 !action->HasBadgeText(kDefaultTabId)) { | 157 if (dict->GetInteger(kAppearanceStorageKey, &int_value)) { |
158 action->SetBadgeText(kDefaultTabId, str_value); | |
159 } | |
160 if (dict->GetString(kBadgeBackgroundColorStorageKey, &str_value) && | |
161 !action->HasBadgeBackgroundColor(kDefaultTabId)) { | |
162 action->SetBadgeBackgroundColor(kDefaultTabId, | |
163 RawStringToSkColor(str_value)); | |
164 } | |
165 if (dict->GetString(kBadgeTextColorStorageKey, &str_value) && | |
166 !action->HasBadgeTextColor(kDefaultTabId)) { | |
167 action->SetBadgeTextColor(kDefaultTabId, RawStringToSkColor(str_value)); | |
168 } | |
169 if (dict->GetInteger(kAppearanceStorageKey, &int_value) && | |
170 !action->HasIsVisible(kDefaultTabId)) { | |
171 switch (int_value) { | 158 switch (int_value) { |
172 case INVISIBLE: | 159 case INVISIBLE: |
173 case OBSOLETE_WANTS_ATTENTION: | 160 case OBSOLETE_WANTS_ATTENTION: |
174 action->SetIsVisible(kDefaultTabId, false); | 161 action->SetIsVisible(kTabId, false); |
175 case ACTIVE: | 162 case ACTIVE: |
176 action->SetIsVisible(kDefaultTabId, true); | 163 action->SetIsVisible(kTabId, true); |
177 } | 164 } |
178 } | 165 } |
179 | 166 |
180 const base::DictionaryValue* icon_value = NULL; | 167 const base::DictionaryValue* icon_value = NULL; |
181 if (dict->GetDictionary(kIconStorageKey, &icon_value) && | 168 if (dict->GetDictionary(kIconStorageKey, &icon_value)) { |
182 !action->HasIcon(kDefaultTabId)) { | |
183 for (size_t i = 0; i < arraysize(kIconSizes); i++) { | 169 for (size_t i = 0; i < arraysize(kIconSizes); i++) { |
184 if (icon_value->GetString(kIconSizes[i].size_string, &str_value) && | 170 if (icon_value->GetString(kIconSizes[i].size_string, &str_value) && |
185 StringToSkBitmap(str_value, &bitmap)) { | 171 StringToSkBitmap(str_value, &bitmap)) { |
186 CHECK(!bitmap.isNull()); | 172 CHECK(!bitmap.isNull()); |
187 float scale = ui::GetImageScale(kIconSizes[i].scale); | 173 float scale = ui::GetImageScale(kIconSizes[i].scale); |
188 icon.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale)); | 174 icon.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale)); |
189 } | 175 } |
190 } | 176 } |
191 action->SetIcon(kDefaultTabId, gfx::Image(icon)); | 177 action->SetIcon(kTabId, gfx::Image(icon)); |
192 } | 178 } |
193 } | 179 } |
194 | 180 |
195 // Store |action|'s default values in a DictionaryValue for use in storing to | 181 // Store |action|'s default values in a DictionaryValue for use in storing to |
196 // disk. | 182 // disk. |
197 scoped_ptr<base::DictionaryValue> DefaultsToValue(ExtensionAction* action) { | 183 scoped_ptr<base::DictionaryValue> DefaultsToValue(ExtensionAction* action) { |
198 const int kDefaultTabId = ExtensionAction::kDefaultTabId; | 184 const int kTabId = ExtensionAction::kDefaultTabId; |
199 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 185 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
200 | 186 |
201 dict->SetString(kPopupUrlStorageKey, | 187 dict->SetString(kPopupUrlStorageKey, action->GetPopupUrl(kTabId).spec()); |
202 action->GetPopupUrl(kDefaultTabId).spec()); | 188 dict->SetString(kTitleStorageKey, action->GetTitle(kTabId)); |
203 dict->SetString(kTitleStorageKey, action->GetTitle(kDefaultTabId)); | 189 dict->SetString(kBadgeTextStorageKey, action->GetBadgeText(kTabId)); |
204 dict->SetString(kBadgeTextStorageKey, action->GetBadgeText(kDefaultTabId)); | 190 dict->SetString(kBadgeBackgroundColorStorageKey, |
205 dict->SetString( | 191 SkColorToRawString(action->GetBadgeBackgroundColor(kTabId))); |
206 kBadgeBackgroundColorStorageKey, | |
207 SkColorToRawString(action->GetBadgeBackgroundColor(kDefaultTabId))); | |
208 dict->SetString(kBadgeTextColorStorageKey, | 192 dict->SetString(kBadgeTextColorStorageKey, |
209 SkColorToRawString(action->GetBadgeTextColor(kDefaultTabId))); | 193 SkColorToRawString(action->GetBadgeTextColor(kTabId))); |
210 dict->SetInteger(kAppearanceStorageKey, | 194 dict->SetInteger(kAppearanceStorageKey, |
211 action->GetIsVisible(kDefaultTabId) ? ACTIVE : INVISIBLE); | 195 action->GetIsVisible(kTabId) ? ACTIVE : INVISIBLE); |
212 | 196 |
213 gfx::ImageSkia icon = action->GetExplicitlySetIcon(kDefaultTabId); | 197 gfx::ImageSkia icon = action->GetExplicitlySetIcon(kTabId); |
214 if (!icon.isNull()) { | 198 if (!icon.isNull()) { |
215 base::DictionaryValue* icon_value = new base::DictionaryValue(); | 199 base::DictionaryValue* icon_value = new base::DictionaryValue(); |
216 for (size_t i = 0; i < arraysize(kIconSizes); i++) { | 200 for (size_t i = 0; i < arraysize(kIconSizes); i++) { |
217 float scale = ui::GetImageScale(kIconSizes[i].scale); | 201 float scale = ui::GetImageScale(kIconSizes[i].scale); |
218 if (icon.HasRepresentation(scale)) { | 202 if (icon.HasRepresentation(scale)) { |
219 icon_value->SetString( | 203 icon_value->SetString( |
220 kIconSizes[i].size_string, | 204 kIconSizes[i].size_string, |
221 RepresentationToString(icon, scale)); | 205 RepresentationToString(icon, scale)); |
222 } | 206 } |
223 } | 207 } |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 } | 433 } |
450 break; | 434 break; |
451 } | 435 } |
452 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED: { | 436 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED: { |
453 ExtensionAction* extension_action = | 437 ExtensionAction* extension_action = |
454 content::Source<ExtensionAction>(source).ptr(); | 438 content::Source<ExtensionAction>(source).ptr(); |
455 Profile* profile = content::Details<Profile>(details).ptr(); | 439 Profile* profile = content::Details<Profile>(details).ptr(); |
456 if (profile != profile_) | 440 if (profile != profile_) |
457 break; | 441 break; |
458 | 442 |
| 443 extension_action->set_has_changed(true); |
459 WriteToStorage(extension_action); | 444 WriteToStorage(extension_action); |
460 break; | 445 break; |
461 } | 446 } |
462 default: | 447 default: |
463 NOTREACHED(); | 448 NOTREACHED(); |
464 break; | 449 break; |
465 } | 450 } |
466 } | 451 } |
467 | 452 |
468 void ExtensionActionStorageManager::WriteToStorage( | 453 void ExtensionActionStorageManager::WriteToStorage( |
(...skipping 19 matching lines...) Expand all Loading... |
488 | 473 |
489 ExtensionAction* browser_action = | 474 ExtensionAction* browser_action = |
490 ExtensionActionManager::Get(profile_)->GetBrowserAction(*extension); | 475 ExtensionActionManager::Get(profile_)->GetBrowserAction(*extension); |
491 if (!browser_action) { | 476 if (!browser_action) { |
492 // This can happen if the extension is updated between startup and when the | 477 // This can happen if the extension is updated between startup and when the |
493 // storage read comes back, and the update removes the browser action. | 478 // storage read comes back, and the update removes the browser action. |
494 // http://crbug.com/349371 | 479 // http://crbug.com/349371 |
495 return; | 480 return; |
496 } | 481 } |
497 | 482 |
| 483 // Don't load values from storage if the extension has updated a value |
| 484 // already. The extension may have only updated some of the values, but |
| 485 // this is a good first approximation. If the extension is doing stuff |
| 486 // to the browser action, we can assume it is ready to take over. |
| 487 if (browser_action->has_changed()) |
| 488 return; |
| 489 |
498 const base::DictionaryValue* dict = NULL; | 490 const base::DictionaryValue* dict = NULL; |
499 if (!value.get() || !value->GetAsDictionary(&dict)) | 491 if (!value.get() || !value->GetAsDictionary(&dict)) |
500 return; | 492 return; |
501 | 493 |
502 SetDefaultsFromValue(dict, browser_action); | 494 SetDefaultsFromValue(dict, browser_action); |
503 } | 495 } |
504 | 496 |
505 // | 497 // |
506 // ExtensionActionFunction | 498 // ExtensionActionFunction |
507 // | 499 // |
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
951 return true; | 943 return true; |
952 } | 944 } |
953 | 945 |
954 bool EnablePageActionsFunction::RunImpl() { | 946 bool EnablePageActionsFunction::RunImpl() { |
955 return SetPageActionEnabled(true); | 947 return SetPageActionEnabled(true); |
956 } | 948 } |
957 | 949 |
958 bool DisablePageActionsFunction::RunImpl() { | 950 bool DisablePageActionsFunction::RunImpl() { |
959 return SetPageActionEnabled(false); | 951 return SetPageActionEnabled(false); |
960 } | 952 } |
OLD | NEW |