| 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 // http://crbug.com/140546 is fixed. | 93 // http://crbug.com/140546 is fixed. |
| 94 std::string raw_str; | 94 std::string raw_str; |
| 95 if (!base::Base64Decode(str, &raw_str)) | 95 if (!base::Base64Decode(str, &raw_str)) |
| 96 return false; | 96 return false; |
| 97 IPC::Message bitmap_pickle(raw_str.data(), raw_str.size()); | 97 IPC::Message bitmap_pickle(raw_str.data(), raw_str.size()); |
| 98 PickleIterator iter(bitmap_pickle); | 98 PickleIterator iter(bitmap_pickle); |
| 99 return IPC::ReadParam(&bitmap_pickle, &iter, bitmap); | 99 return IPC::ReadParam(&bitmap_pickle, &iter, bitmap); |
| 100 } | 100 } |
| 101 | 101 |
| 102 // Conversion function for reading/writing to storage. | 102 // Conversion function for reading/writing to storage. |
| 103 std::string RepresentationToString(const gfx::ImageSkia& image, float scale) { | 103 std::string RepresentationToString(const gfx::ImageSkia& image, |
| 104 ui::ScaleFactor scale) { |
| 104 SkBitmap bitmap = image.GetRepresentation(scale).sk_bitmap(); | 105 SkBitmap bitmap = image.GetRepresentation(scale).sk_bitmap(); |
| 105 IPC::Message bitmap_pickle; | 106 IPC::Message bitmap_pickle; |
| 106 // Clear the header values so they don't vary in serialization. | 107 // Clear the header values so they don't vary in serialization. |
| 107 bitmap_pickle.SetHeaderValues(0, 0, 0); | 108 bitmap_pickle.SetHeaderValues(0, 0, 0); |
| 108 IPC::WriteParam(&bitmap_pickle, bitmap); | 109 IPC::WriteParam(&bitmap_pickle, bitmap); |
| 109 std::string raw_str(static_cast<const char*>(bitmap_pickle.data()), | 110 std::string raw_str(static_cast<const char*>(bitmap_pickle.data()), |
| 110 bitmap_pickle.size()); | 111 bitmap_pickle.size()); |
| 111 std::string base64_str; | 112 std::string base64_str; |
| 112 if (!base::Base64Encode(raw_str, &base64_str)) | 113 if (!base::Base64Encode(raw_str, &base64_str)) |
| 113 return std::string(); | 114 return std::string(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 136 if (dict->GetInteger(kAppearanceStorageKey, &int_value)) | 137 if (dict->GetInteger(kAppearanceStorageKey, &int_value)) |
| 137 action->SetAppearance(kTabId, | 138 action->SetAppearance(kTabId, |
| 138 static_cast<ExtensionAction::Appearance>(int_value)); | 139 static_cast<ExtensionAction::Appearance>(int_value)); |
| 139 | 140 |
| 140 const base::DictionaryValue* icon_value = NULL; | 141 const base::DictionaryValue* icon_value = NULL; |
| 141 if (dict->GetDictionary(kIconStorageKey, &icon_value)) { | 142 if (dict->GetDictionary(kIconStorageKey, &icon_value)) { |
| 142 for (size_t i = 0; i < arraysize(kIconSizes); i++) { | 143 for (size_t i = 0; i < arraysize(kIconSizes); i++) { |
| 143 if (icon_value->GetString(kIconSizes[i].size_string, &str_value) && | 144 if (icon_value->GetString(kIconSizes[i].size_string, &str_value) && |
| 144 StringToSkBitmap(str_value, &bitmap)) { | 145 StringToSkBitmap(str_value, &bitmap)) { |
| 145 CHECK(!bitmap.isNull()); | 146 CHECK(!bitmap.isNull()); |
| 146 float scale = ui::GetImageScale(kIconSizes[i].scale); | 147 icon.AddRepresentation(gfx::ImageSkiaRep(bitmap, kIconSizes[i].scale)); |
| 147 icon.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale)); | |
| 148 } | 148 } |
| 149 } | 149 } |
| 150 action->SetIcon(kTabId, gfx::Image(icon)); | 150 action->SetIcon(kTabId, gfx::Image(icon)); |
| 151 } | 151 } |
| 152 } | 152 } |
| 153 | 153 |
| 154 // Store |action|'s default values in a DictionaryValue for use in storing to | 154 // Store |action|'s default values in a DictionaryValue for use in storing to |
| 155 // disk. | 155 // disk. |
| 156 scoped_ptr<base::DictionaryValue> DefaultsToValue(ExtensionAction* action) { | 156 scoped_ptr<base::DictionaryValue> DefaultsToValue(ExtensionAction* action) { |
| 157 const int kTabId = ExtensionAction::kDefaultTabId; | 157 const int kTabId = ExtensionAction::kDefaultTabId; |
| 158 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 158 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 159 | 159 |
| 160 dict->SetString(kPopupUrlStorageKey, action->GetPopupUrl(kTabId).spec()); | 160 dict->SetString(kPopupUrlStorageKey, action->GetPopupUrl(kTabId).spec()); |
| 161 dict->SetString(kTitleStorageKey, action->GetTitle(kTabId)); | 161 dict->SetString(kTitleStorageKey, action->GetTitle(kTabId)); |
| 162 dict->SetString(kBadgeTextStorageKey, action->GetBadgeText(kTabId)); | 162 dict->SetString(kBadgeTextStorageKey, action->GetBadgeText(kTabId)); |
| 163 dict->SetString(kBadgeBackgroundColorStorageKey, | 163 dict->SetString(kBadgeBackgroundColorStorageKey, |
| 164 SkColorToRawString(action->GetBadgeBackgroundColor(kTabId))); | 164 SkColorToRawString(action->GetBadgeBackgroundColor(kTabId))); |
| 165 dict->SetString(kBadgeTextColorStorageKey, | 165 dict->SetString(kBadgeTextColorStorageKey, |
| 166 SkColorToRawString(action->GetBadgeTextColor(kTabId))); | 166 SkColorToRawString(action->GetBadgeTextColor(kTabId))); |
| 167 dict->SetInteger(kAppearanceStorageKey, | 167 dict->SetInteger(kAppearanceStorageKey, |
| 168 action->GetIsVisible(kTabId) ? | 168 action->GetIsVisible(kTabId) ? |
| 169 ExtensionAction::ACTIVE : ExtensionAction::INVISIBLE); | 169 ExtensionAction::ACTIVE : ExtensionAction::INVISIBLE); |
| 170 | 170 |
| 171 gfx::ImageSkia icon = action->GetExplicitlySetIcon(kTabId); | 171 gfx::ImageSkia icon = action->GetExplicitlySetIcon(kTabId); |
| 172 if (!icon.isNull()) { | 172 if (!icon.isNull()) { |
| 173 base::DictionaryValue* icon_value = new base::DictionaryValue(); | 173 base::DictionaryValue* icon_value = new base::DictionaryValue(); |
| 174 for (size_t i = 0; i < arraysize(kIconSizes); i++) { | 174 for (size_t i = 0; i < arraysize(kIconSizes); i++) { |
| 175 float scale = ui::GetImageScale(kIconSizes[i].scale); | 175 if (icon.HasRepresentation(kIconSizes[i].scale)) { |
| 176 if (icon.HasRepresentation(scale)) { | |
| 177 icon_value->SetString( | 176 icon_value->SetString( |
| 178 kIconSizes[i].size_string, | 177 kIconSizes[i].size_string, |
| 179 RepresentationToString(icon, scale)); | 178 RepresentationToString(icon, kIconSizes[i].scale)); |
| 180 } | 179 } |
| 181 } | 180 } |
| 182 dict->Set(kIconStorageKey, icon_value); | 181 dict->Set(kIconStorageKey, icon_value); |
| 183 } | 182 } |
| 184 return dict.Pass(); | 183 return dict.Pass(); |
| 185 } | 184 } |
| 186 | 185 |
| 187 } // namespace | 186 } // namespace |
| 188 | 187 |
| 189 // | 188 // |
| (...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 gfx::ImageSkia icon; | 689 gfx::ImageSkia icon; |
| 691 // Extract icon representations from the ImageDataSet dictionary. | 690 // Extract icon representations from the ImageDataSet dictionary. |
| 692 for (size_t i = 0; i < arraysize(kIconSizes); i++) { | 691 for (size_t i = 0; i < arraysize(kIconSizes); i++) { |
| 693 base::BinaryValue* binary; | 692 base::BinaryValue* binary; |
| 694 if (canvas_set->GetBinary(kIconSizes[i].size_string, &binary)) { | 693 if (canvas_set->GetBinary(kIconSizes[i].size_string, &binary)) { |
| 695 IPC::Message pickle(binary->GetBuffer(), binary->GetSize()); | 694 IPC::Message pickle(binary->GetBuffer(), binary->GetSize()); |
| 696 PickleIterator iter(pickle); | 695 PickleIterator iter(pickle); |
| 697 SkBitmap bitmap; | 696 SkBitmap bitmap; |
| 698 EXTENSION_FUNCTION_VALIDATE(IPC::ReadParam(&pickle, &iter, &bitmap)); | 697 EXTENSION_FUNCTION_VALIDATE(IPC::ReadParam(&pickle, &iter, &bitmap)); |
| 699 CHECK(!bitmap.isNull()); | 698 CHECK(!bitmap.isNull()); |
| 700 float scale = ui::GetImageScale(kIconSizes[i].scale); | 699 icon.AddRepresentation(gfx::ImageSkiaRep(bitmap, kIconSizes[i].scale)); |
| 701 icon.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale)); | |
| 702 } | 700 } |
| 703 } | 701 } |
| 704 | 702 |
| 705 extension_action_->SetIcon(tab_id_, gfx::Image(icon)); | 703 extension_action_->SetIcon(tab_id_, gfx::Image(icon)); |
| 706 } else if (details_->GetInteger("iconIndex", &icon_index)) { | 704 } else if (details_->GetInteger("iconIndex", &icon_index)) { |
| 707 // Obsolete argument: ignore it. | 705 // Obsolete argument: ignore it. |
| 708 return true; | 706 return true; |
| 709 } else { | 707 } else { |
| 710 EXTENSION_FUNCTION_VALIDATE(false); | 708 EXTENSION_FUNCTION_VALIDATE(false); |
| 711 } | 709 } |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 884 return true; | 882 return true; |
| 885 } | 883 } |
| 886 | 884 |
| 887 bool EnablePageActionsFunction::RunImpl() { | 885 bool EnablePageActionsFunction::RunImpl() { |
| 888 return SetPageActionEnabled(true); | 886 return SetPageActionEnabled(true); |
| 889 } | 887 } |
| 890 | 888 |
| 891 bool DisablePageActionsFunction::RunImpl() { | 889 bool DisablePageActionsFunction::RunImpl() { |
| 892 return SetPageActionEnabled(false); | 890 return SetPageActionEnabled(false); |
| 893 } | 891 } |
| OLD | NEW |