| 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/extension_action.h" | 5 #include "chrome/browser/extensions/extension_action.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/base64.h" | 10 #include "base/base64.h" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 122 |
| 123 void ExtensionAction::SetIcon(int tab_id, const gfx::Image& image) { | 123 void ExtensionAction::SetIcon(int tab_id, const gfx::Image& image) { |
| 124 SetValue(&icon_, tab_id, image); | 124 SetValue(&icon_, tab_id, image); |
| 125 } | 125 } |
| 126 | 126 |
| 127 bool ExtensionAction::ParseIconFromCanvasDictionary( | 127 bool ExtensionAction::ParseIconFromCanvasDictionary( |
| 128 const base::DictionaryValue& dict, | 128 const base::DictionaryValue& dict, |
| 129 gfx::ImageSkia* icon) { | 129 gfx::ImageSkia* icon) { |
| 130 for (base::DictionaryValue::Iterator iter(dict); !iter.IsAtEnd(); | 130 for (base::DictionaryValue::Iterator iter(dict); !iter.IsAtEnd(); |
| 131 iter.Advance()) { | 131 iter.Advance()) { |
| 132 int icon_size = 0; | |
| 133 // Chrome helpfully scales the provided icon(s), but let's not go overboard. | |
| 134 const int kActionIconMaxSize = 10 * extension_misc::EXTENSION_ICON_ACTION; | |
| 135 if (!base::StringToInt(iter.key(), &icon_size) || icon_size <= 0 || | |
| 136 icon_size > kActionIconMaxSize) { | |
| 137 continue; | |
| 138 } | |
| 139 | |
| 140 const base::BinaryValue* image_data; | 132 const base::BinaryValue* image_data; |
| 141 std::string binary_string64; | 133 std::string binary_string64; |
| 142 IPC::Message pickle; | 134 IPC::Message pickle; |
| 143 if (iter.value().GetAsBinary(&image_data)) { | 135 if (iter.value().GetAsBinary(&image_data)) { |
| 144 pickle = IPC::Message(image_data->GetBuffer(), image_data->GetSize()); | 136 pickle = IPC::Message(image_data->GetBuffer(), image_data->GetSize()); |
| 145 } else if (iter.value().GetAsString(&binary_string64)) { | 137 } else if (iter.value().GetAsString(&binary_string64)) { |
| 146 std::string binary_string; | 138 std::string binary_string; |
| 147 if (!base::Base64Decode(binary_string64, &binary_string)) | 139 if (!base::Base64Decode(binary_string64, &binary_string)) |
| 148 return false; | 140 return false; |
| 149 pickle = IPC::Message(binary_string.c_str(), binary_string.length()); | 141 pickle = IPC::Message(binary_string.c_str(), binary_string.length()); |
| 150 } else { | 142 } else { |
| 151 continue; | 143 continue; |
| 152 } | 144 } |
| 153 base::PickleIterator pickle_iter(pickle); | 145 base::PickleIterator pickle_iter(pickle); |
| 154 SkBitmap bitmap; | 146 SkBitmap bitmap; |
| 155 if (!IPC::ReadParam(&pickle, &pickle_iter, &bitmap)) | 147 if (!IPC::ReadParam(&pickle, &pickle_iter, &bitmap)) |
| 156 return false; | 148 return false; |
| 157 CHECK(!bitmap.isNull()); | 149 CHECK(!bitmap.isNull()); |
| 150 |
| 151 // Chrome helpfully scales the provided icon(s), but let's not go overboard. |
| 152 const int kActionIconMaxSize = 10 * extension_misc::EXTENSION_ICON_ACTION; |
| 153 if (bitmap.drawsNothing() || bitmap.width() != bitmap.height() || |
| 154 bitmap.width() > kActionIconMaxSize) { |
| 155 continue; |
| 156 } |
| 157 |
| 158 float scale = | 158 float scale = |
| 159 static_cast<float>(icon_size) / ExtensionAction::ActionIconSize(); | 159 static_cast<float>(bitmap.width()) / ExtensionAction::ActionIconSize(); |
| 160 icon->AddRepresentation(gfx::ImageSkiaRep(bitmap, scale)); | 160 icon->AddRepresentation(gfx::ImageSkiaRep(bitmap, scale)); |
| 161 } | 161 } |
| 162 return true; | 162 return true; |
| 163 } | 163 } |
| 164 | 164 |
| 165 gfx::Image ExtensionAction::GetExplicitlySetIcon(int tab_id) const { | 165 gfx::Image ExtensionAction::GetExplicitlySetIcon(int tab_id) const { |
| 166 return GetValue(&icon_, tab_id); | 166 return GetValue(&icon_, tab_id); |
| 167 } | 167 } |
| 168 | 168 |
| 169 bool ExtensionAction::SetIsVisible(int tab_id, bool new_visibility) { | 169 bool ExtensionAction::SetIsVisible(int tab_id, bool new_visibility) { |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 // If there is a default icon, the icon width will be set depending on our | 328 // If there is a default icon, the icon width will be set depending on our |
| 329 // action type. | 329 // action type. |
| 330 if (default_icon_) | 330 if (default_icon_) |
| 331 return ActionIconSize(); | 331 return ActionIconSize(); |
| 332 | 332 |
| 333 // If no icon has been set and there is no default icon, we need favicon | 333 // If no icon has been set and there is no default icon, we need favicon |
| 334 // width. | 334 // width. |
| 335 return ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 335 return ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 336 IDR_EXTENSIONS_FAVICON).Width(); | 336 IDR_EXTENSIONS_FAVICON).Width(); |
| 337 } | 337 } |
| OLD | NEW |