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