| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_action_storage_manager.h" | 5 #include "chrome/browser/extensions/extension_action_storage_manager.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 if (!base::Base64Decode(str, &raw_str)) | 68 if (!base::Base64Decode(str, &raw_str)) |
| 69 return false; | 69 return false; |
| 70 | 70 |
| 71 bool success = gfx::PNGCodec::Decode( | 71 bool success = gfx::PNGCodec::Decode( |
| 72 reinterpret_cast<unsigned const char*>(raw_str.data()), raw_str.size(), | 72 reinterpret_cast<unsigned const char*>(raw_str.data()), raw_str.size(), |
| 73 bitmap); | 73 bitmap); |
| 74 return success; | 74 return success; |
| 75 } | 75 } |
| 76 | 76 |
| 77 // Conversion function for reading/writing to storage. | 77 // Conversion function for reading/writing to storage. |
| 78 std::string RepresentationToString(const gfx::ImageSkia& image, float scale) { | 78 std::string BitmapToString(const SkBitmap& bitmap) { |
| 79 SkBitmap bitmap = image.GetRepresentation(scale).sk_bitmap(); | |
| 80 SkAutoLockPixels lock_image(bitmap); | 79 SkAutoLockPixels lock_image(bitmap); |
| 81 std::vector<unsigned char> data; | 80 std::vector<unsigned char> data; |
| 82 bool success = gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &data); | 81 bool success = gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &data); |
| 83 if (!success) | 82 if (!success) |
| 84 return std::string(); | 83 return std::string(); |
| 85 | 84 |
| 86 base::StringPiece raw_str( | 85 base::StringPiece raw_str( |
| 87 reinterpret_cast<const char*>(&data[0]), data.size()); | 86 reinterpret_cast<const char*>(&data[0]), data.size()); |
| 88 std::string base64_str; | 87 std::string base64_str; |
| 89 base::Base64Encode(raw_str, &base64_str); | 88 base::Base64Encode(raw_str, &base64_str); |
| 90 return base64_str; | 89 return base64_str; |
| 91 } | 90 } |
| 92 | 91 |
| 93 // Set |action|'s default values to those specified in |dict|. | 92 // Set |action|'s default values to those specified in |dict|. |
| 94 void SetDefaultsFromValue(const base::DictionaryValue* dict, | 93 void SetDefaultsFromValue(const base::DictionaryValue* dict, |
| 95 ExtensionAction* action) { | 94 ExtensionAction* action) { |
| 96 const int kDefaultTabId = ExtensionAction::kDefaultTabId; | 95 const int kDefaultTabId = ExtensionAction::kDefaultTabId; |
| 97 std::string str_value; | 96 std::string str_value; |
| 98 int int_value; | |
| 99 SkBitmap bitmap; | |
| 100 gfx::ImageSkia icon; | |
| 101 | 97 |
| 102 // For each value, don't set it if it has been modified already. | 98 // For each value, don't set it if it has been modified already. |
| 103 if (dict->GetString(kPopupUrlStorageKey, &str_value) && | 99 if (dict->GetString(kPopupUrlStorageKey, &str_value) && |
| 104 !action->HasPopupUrl(kDefaultTabId)) { | 100 !action->HasPopupUrl(kDefaultTabId)) { |
| 105 action->SetPopupUrl(kDefaultTabId, GURL(str_value)); | 101 action->SetPopupUrl(kDefaultTabId, GURL(str_value)); |
| 106 } | 102 } |
| 107 if (dict->GetString(kTitleStorageKey, &str_value) && | 103 if (dict->GetString(kTitleStorageKey, &str_value) && |
| 108 !action->HasTitle(kDefaultTabId)) { | 104 !action->HasTitle(kDefaultTabId)) { |
| 109 action->SetTitle(kDefaultTabId, str_value); | 105 action->SetTitle(kDefaultTabId, str_value); |
| 110 } | 106 } |
| 111 if (dict->GetString(kBadgeTextStorageKey, &str_value) && | 107 if (dict->GetString(kBadgeTextStorageKey, &str_value) && |
| 112 !action->HasBadgeText(kDefaultTabId)) { | 108 !action->HasBadgeText(kDefaultTabId)) { |
| 113 action->SetBadgeText(kDefaultTabId, str_value); | 109 action->SetBadgeText(kDefaultTabId, str_value); |
| 114 } | 110 } |
| 115 if (dict->GetString(kBadgeBackgroundColorStorageKey, &str_value) && | 111 if (dict->GetString(kBadgeBackgroundColorStorageKey, &str_value) && |
| 116 !action->HasBadgeBackgroundColor(kDefaultTabId)) { | 112 !action->HasBadgeBackgroundColor(kDefaultTabId)) { |
| 117 action->SetBadgeBackgroundColor(kDefaultTabId, | 113 action->SetBadgeBackgroundColor(kDefaultTabId, |
| 118 RawStringToSkColor(str_value)); | 114 RawStringToSkColor(str_value)); |
| 119 } | 115 } |
| 120 if (dict->GetString(kBadgeTextColorStorageKey, &str_value) && | 116 if (dict->GetString(kBadgeTextColorStorageKey, &str_value) && |
| 121 !action->HasBadgeTextColor(kDefaultTabId)) { | 117 !action->HasBadgeTextColor(kDefaultTabId)) { |
| 122 action->SetBadgeTextColor(kDefaultTabId, RawStringToSkColor(str_value)); | 118 action->SetBadgeTextColor(kDefaultTabId, RawStringToSkColor(str_value)); |
| 123 } | 119 } |
| 124 if (dict->GetInteger(kAppearanceStorageKey, &int_value) && | 120 |
| 121 int appearance_storage = 0; |
| 122 if (dict->GetInteger(kAppearanceStorageKey, &appearance_storage) && |
| 125 !action->HasIsVisible(kDefaultTabId)) { | 123 !action->HasIsVisible(kDefaultTabId)) { |
| 126 switch (int_value) { | 124 switch (appearance_storage) { |
| 127 case INVISIBLE: | 125 case INVISIBLE: |
| 128 case OBSOLETE_WANTS_ATTENTION: | 126 case OBSOLETE_WANTS_ATTENTION: |
| 129 action->SetIsVisible(kDefaultTabId, false); | 127 action->SetIsVisible(kDefaultTabId, false); |
| 130 break; | 128 break; |
| 131 case ACTIVE: | 129 case ACTIVE: |
| 132 action->SetIsVisible(kDefaultTabId, true); | 130 action->SetIsVisible(kDefaultTabId, true); |
| 133 break; | 131 break; |
| 134 } | 132 } |
| 135 } | 133 } |
| 136 | 134 |
| 137 const base::DictionaryValue* icon_value = NULL; | 135 const base::DictionaryValue* icon_value = NULL; |
| 138 if (dict->GetDictionary(kIconStorageKey, &icon_value) && | 136 if (dict->GetDictionary(kIconStorageKey, &icon_value) && |
| 139 !action->HasIcon(kDefaultTabId)) { | 137 !action->HasIcon(kDefaultTabId)) { |
| 140 for (size_t i = 0; i < extension_misc::kNumExtensionActionIconSizes; i++) { | 138 gfx::ImageSkia icon; |
| 141 const extension_misc::IconRepresentationInfo& icon_info = | 139 SkBitmap bitmap; |
| 142 extension_misc::kExtensionActionIconSizes[i]; | 140 for (base::DictionaryValue::Iterator iter(*icon_value); !iter.IsAtEnd(); |
| 143 if (icon_value->GetString(icon_info.size_string, &str_value) && | 141 iter.Advance()) { |
| 144 StringToSkBitmap(str_value, &bitmap)) { | 142 int icon_size = 0; |
| 143 std::string icon_string; |
| 144 if (base::StringToInt(iter.key(), &icon_size) && |
| 145 iter.value().GetAsString(&icon_string) && |
| 146 StringToSkBitmap(icon_string, &bitmap)) { |
| 145 CHECK(!bitmap.isNull()); | 147 CHECK(!bitmap.isNull()); |
| 146 float scale = ui::GetScaleForScaleFactor(icon_info.scale); | 148 float scale = |
| 149 static_cast<float>(icon_size) / ExtensionAction::ActionIconSize(); |
| 147 icon.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale)); | 150 icon.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale)); |
| 148 } | 151 } |
| 149 } | 152 } |
| 150 action->SetIcon(kDefaultTabId, gfx::Image(icon)); | 153 action->SetIcon(kDefaultTabId, gfx::Image(icon)); |
| 151 } | 154 } |
| 152 } | 155 } |
| 153 | 156 |
| 154 // Store |action|'s default values in a DictionaryValue for use in storing to | 157 // Store |action|'s default values in a DictionaryValue for use in storing to |
| 155 // disk. | 158 // disk. |
| 156 scoped_ptr<base::DictionaryValue> DefaultsToValue(ExtensionAction* action) { | 159 scoped_ptr<base::DictionaryValue> DefaultsToValue(ExtensionAction* action) { |
| 157 const int kDefaultTabId = ExtensionAction::kDefaultTabId; | 160 const int kDefaultTabId = ExtensionAction::kDefaultTabId; |
| 158 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 161 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 159 | 162 |
| 160 dict->SetString(kPopupUrlStorageKey, | 163 dict->SetString(kPopupUrlStorageKey, |
| 161 action->GetPopupUrl(kDefaultTabId).spec()); | 164 action->GetPopupUrl(kDefaultTabId).spec()); |
| 162 dict->SetString(kTitleStorageKey, action->GetTitle(kDefaultTabId)); | 165 dict->SetString(kTitleStorageKey, action->GetTitle(kDefaultTabId)); |
| 163 dict->SetString(kBadgeTextStorageKey, action->GetBadgeText(kDefaultTabId)); | 166 dict->SetString(kBadgeTextStorageKey, action->GetBadgeText(kDefaultTabId)); |
| 164 dict->SetString( | 167 dict->SetString( |
| 165 kBadgeBackgroundColorStorageKey, | 168 kBadgeBackgroundColorStorageKey, |
| 166 SkColorToRawString(action->GetBadgeBackgroundColor(kDefaultTabId))); | 169 SkColorToRawString(action->GetBadgeBackgroundColor(kDefaultTabId))); |
| 167 dict->SetString(kBadgeTextColorStorageKey, | 170 dict->SetString(kBadgeTextColorStorageKey, |
| 168 SkColorToRawString(action->GetBadgeTextColor(kDefaultTabId))); | 171 SkColorToRawString(action->GetBadgeTextColor(kDefaultTabId))); |
| 169 dict->SetInteger(kAppearanceStorageKey, | 172 dict->SetInteger(kAppearanceStorageKey, |
| 170 action->GetIsVisible(kDefaultTabId) ? ACTIVE : INVISIBLE); | 173 action->GetIsVisible(kDefaultTabId) ? ACTIVE : INVISIBLE); |
| 171 | 174 |
| 172 gfx::ImageSkia icon = | 175 gfx::ImageSkia icon = |
| 173 action->GetExplicitlySetIcon(kDefaultTabId).AsImageSkia(); | 176 action->GetExplicitlySetIcon(kDefaultTabId).AsImageSkia(); |
| 174 if (!icon.isNull()) { | 177 if (!icon.isNull()) { |
| 175 scoped_ptr<base::DictionaryValue> icon_value(new base::DictionaryValue()); | 178 scoped_ptr<base::DictionaryValue> icon_value(new base::DictionaryValue()); |
| 176 for (size_t i = 0; i < extension_misc::kNumExtensionActionIconSizes; i++) { | 179 std::vector<gfx::ImageSkiaRep> image_reps = icon.image_reps(); |
| 177 const extension_misc::IconRepresentationInfo& icon_info = | 180 for (const gfx::ImageSkiaRep& rep : image_reps) { |
| 178 extension_misc::kExtensionActionIconSizes[i]; | 181 int size = static_cast<int>(rep.scale() * icon.width()); |
| 179 float scale = ui::GetScaleForScaleFactor(icon_info.scale); | 182 std::string size_string = base::IntToString(size); |
| 180 if (icon.HasRepresentation(scale)) { | 183 icon_value->SetString(size_string, BitmapToString(rep.sk_bitmap())); |
| 181 icon_value->SetString(icon_info.size_string, | |
| 182 RepresentationToString(icon, scale)); | |
| 183 } | |
| 184 } | 184 } |
| 185 dict->Set(kIconStorageKey, icon_value.release()); | 185 dict->Set(kIconStorageKey, icon_value.Pass()); |
| 186 } | 186 } |
| 187 return dict.Pass(); | 187 return dict; |
| 188 } | 188 } |
| 189 | 189 |
| 190 } // namespace | 190 } // namespace |
| 191 | 191 |
| 192 ExtensionActionStorageManager::ExtensionActionStorageManager( | 192 ExtensionActionStorageManager::ExtensionActionStorageManager( |
| 193 content::BrowserContext* context) | 193 content::BrowserContext* context) |
| 194 : browser_context_(context), | 194 : browser_context_(context), |
| 195 extension_action_observer_(this), | 195 extension_action_observer_(this), |
| 196 extension_registry_observer_(this), | 196 extension_registry_observer_(this), |
| 197 weak_factory_(this) { | 197 weak_factory_(this) { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 return; | 271 return; |
| 272 | 272 |
| 273 SetDefaultsFromValue(dict, browser_action); | 273 SetDefaultsFromValue(dict, browser_action); |
| 274 } | 274 } |
| 275 | 275 |
| 276 StateStore* ExtensionActionStorageManager::GetStateStore() { | 276 StateStore* ExtensionActionStorageManager::GetStateStore() { |
| 277 return ExtensionSystem::Get(browser_context_)->state_store(); | 277 return ExtensionSystem::Get(browser_context_)->state_store(); |
| 278 } | 278 } |
| 279 | 279 |
| 280 } // namespace extensions | 280 } // namespace extensions |
| OLD | NEW |