| 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/ui/views/location_bar/page_action_image_view.h" | 5 #include "chrome/browser/ui/views/location_bar/page_action_image_view.h" |
| 6 | 6 |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/extensions/api/commands/command_service.h" | 8 #include "chrome/browser/extensions/api/commands/command_service.h" |
| 9 #include "chrome/browser/extensions/api/commands/command_service_factory.h" | 9 #include "chrome/browser/extensions/api/commands/command_service_factory.h" |
| 10 #include "chrome/browser/extensions/extension_context_menu_model.h" | 10 #include "chrome/browser/extensions/extension_context_menu_model.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 preview_enabled_(false), | 48 preview_enabled_(false), |
| 49 popup_(NULL), | 49 popup_(NULL), |
| 50 ALLOW_THIS_IN_INITIALIZER_LIST(scoped_icon_animation_observer_( | 50 ALLOW_THIS_IN_INITIALIZER_LIST(scoped_icon_animation_observer_( |
| 51 page_action->GetIconAnimation( | 51 page_action->GetIconAnimation( |
| 52 SessionID::IdForTab(owner->GetTabContents()->web_contents())), | 52 SessionID::IdForTab(owner->GetTabContents()->web_contents())), |
| 53 this)) { | 53 this)) { |
| 54 const Extension* extension = owner_->profile()->GetExtensionService()-> | 54 const Extension* extension = owner_->profile()->GetExtensionService()-> |
| 55 GetExtensionById(page_action->extension_id(), false); | 55 GetExtensionById(page_action->extension_id(), false); |
| 56 DCHECK(extension); | 56 DCHECK(extension); |
| 57 | 57 |
| 58 // Load all the icons declared in the manifest. This is the contents of the | 58 std::string path = page_action_->default_icon_path(); |
| 59 // icons array, plus the default_icon property, if any. | 59 if (!path.empty()) { |
| 60 std::vector<std::string> icon_paths(*page_action->icon_paths()); | 60 tracker_.LoadImage(extension, extension->GetResource(path), |
| 61 if (!page_action_->default_icon_path().empty()) | |
| 62 icon_paths.push_back(page_action_->default_icon_path()); | |
| 63 | |
| 64 for (std::vector<std::string>::iterator i(icon_paths.begin()); | |
| 65 i != icon_paths.end(); ++i) { | |
| 66 tracker_.LoadImage(extension, extension->GetResource(*i), | |
| 67 gfx::Size(Extension::kPageActionIconMaxSize, | 61 gfx::Size(Extension::kPageActionIconMaxSize, |
| 68 Extension::kPageActionIconMaxSize), | 62 Extension::kPageActionIconMaxSize), |
| 69 ImageLoadingTracker::DONT_CACHE); | 63 ImageLoadingTracker::DONT_CACHE); |
| 70 } | 64 } |
| 71 | 65 |
| 72 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 66 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 73 content::Source<Profile>( | 67 content::Source<Profile>( |
| 74 owner_->profile()->GetOriginalProfile())); | 68 owner_->profile()->GetOriginalProfile())); |
| 75 | 69 |
| 76 set_accessibility_focusable(true); | 70 set_accessibility_focusable(true); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 event.key_code() == ui::VKEY_RETURN) { | 184 event.key_code() == ui::VKEY_RETURN) { |
| 191 ExecuteAction(ExtensionPopup::SHOW); | 185 ExecuteAction(ExtensionPopup::SHOW); |
| 192 return true; | 186 return true; |
| 193 } | 187 } |
| 194 return false; | 188 return false; |
| 195 } | 189 } |
| 196 | 190 |
| 197 void PageActionImageView::OnImageLoaded(const gfx::Image& image, | 191 void PageActionImageView::OnImageLoaded(const gfx::Image& image, |
| 198 const std::string& extension_id, | 192 const std::string& extension_id, |
| 199 int index) { | 193 int index) { |
| 200 // We loaded icons()->size() icons, plus one extra if the page action had | 194 page_action_->CacheIcon(image); |
| 201 // a default icon. | |
| 202 int total_icons = static_cast<int>(page_action_->icon_paths()->size()); | |
| 203 if (!page_action_->default_icon_path().empty()) | |
| 204 total_icons++; | |
| 205 DCHECK(index < total_icons); | |
| 206 | |
| 207 // Map the index of the loaded image back to its name. If we ever get an | |
| 208 // index greater than the number of icons, it must be the default icon. | |
| 209 if (index < static_cast<int>(page_action_->icon_paths()->size())) | |
| 210 page_action_->CacheIcon(page_action_->icon_paths()->at(index), image); | |
| 211 else | |
| 212 page_action_->CacheIcon(page_action_->default_icon_path(), image); | |
| 213 | 195 |
| 214 // During object construction owner_ will be NULL. | 196 // During object construction owner_ will be NULL. |
| 215 TabContents* tab_contents = owner_ ? owner_->GetTabContents() : NULL; | 197 TabContents* tab_contents = owner_ ? owner_->GetTabContents() : NULL; |
| 216 if (tab_contents) | 198 if (tab_contents) |
| 217 UpdateVisibility(tab_contents->web_contents(), current_url_); | 199 UpdateVisibility(tab_contents->web_contents(), current_url_); |
| 218 } | 200 } |
| 219 | 201 |
| 220 void PageActionImageView::ShowContextMenuForView(View* source, | 202 void PageActionImageView::ShowContextMenuForView(View* source, |
| 221 const gfx::Point& point) { | 203 const gfx::Point& point) { |
| 222 const Extension* extension = owner_->profile()->GetExtensionService()-> | 204 const Extension* extension = owner_->profile()->GetExtensionService()-> |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 | 300 |
| 319 popup_ = ExtensionPopup::ShowPopup(popup_url, browser_, this, arrow_location, | 301 popup_ = ExtensionPopup::ShowPopup(popup_url, browser_, this, arrow_location, |
| 320 show_action); | 302 show_action); |
| 321 popup_->GetWidget()->AddObserver(this); | 303 popup_->GetWidget()->AddObserver(this); |
| 322 } | 304 } |
| 323 | 305 |
| 324 void PageActionImageView::HidePopup() { | 306 void PageActionImageView::HidePopup() { |
| 325 if (popup_) | 307 if (popup_) |
| 326 popup_->GetWidget()->Close(); | 308 popup_->GetWidget()->Close(); |
| 327 } | 309 } |
| OLD | NEW |