| 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_action_icon_factory.h" |
| 10 #include "chrome/browser/extensions/extension_context_menu_model.h" | 11 #include "chrome/browser/extensions/extension_context_menu_model.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/extensions/extension_tab_util.h" | 13 #include "chrome/browser/extensions/extension_tab_util.h" |
| 13 #include "chrome/browser/extensions/location_bar_controller.h" | 14 #include "chrome/browser/extensions/location_bar_controller.h" |
| 14 #include "chrome/browser/extensions/tab_helper.h" | 15 #include "chrome/browser/extensions/tab_helper.h" |
| 15 #include "chrome/browser/platform_util.h" | 16 #include "chrome/browser/platform_util.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/sessions/session_id.h" | 18 #include "chrome/browser/sessions/session_id.h" |
| 18 #include "chrome/browser/ui/browser_list.h" | 19 #include "chrome/browser/ui/browser_list.h" |
| 19 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 20 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 36 using content::WebContents; | 37 using content::WebContents; |
| 37 using extensions::LocationBarController; | 38 using extensions::LocationBarController; |
| 38 using extensions::Extension; | 39 using extensions::Extension; |
| 39 | 40 |
| 40 PageActionImageView::PageActionImageView(LocationBarView* owner, | 41 PageActionImageView::PageActionImageView(LocationBarView* owner, |
| 41 ExtensionAction* page_action, | 42 ExtensionAction* page_action, |
| 42 Browser* browser) | 43 Browser* browser) |
| 43 : owner_(owner), | 44 : owner_(owner), |
| 44 page_action_(page_action), | 45 page_action_(page_action), |
| 45 browser_(browser), | 46 browser_(browser), |
| 46 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), | |
| 47 current_tab_id_(-1), | 47 current_tab_id_(-1), |
| 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())), | 52 SessionID::IdForTab(owner->GetTabContents())), |
| 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 icon_factory_.reset(new ExtensionActionIconFactory(extension, this)); |
| 59 // icons array, plus the default_icon property, if any. | |
| 60 std::vector<std::string> icon_paths(*page_action->icon_paths()); | |
| 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, | |
| 68 Extension::kPageActionIconMaxSize), | |
| 69 ImageLoadingTracker::DONT_CACHE); | |
| 70 } | |
| 71 | 59 |
| 72 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 60 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 73 content::Source<Profile>( | 61 content::Source<Profile>( |
| 74 owner_->profile()->GetOriginalProfile())); | 62 owner_->profile()->GetOriginalProfile())); |
| 75 | 63 |
| 76 set_accessibility_focusable(true); | 64 set_accessibility_focusable(true); |
| 77 set_context_menu_controller(this); | 65 set_context_menu_controller(this); |
| 78 | 66 |
| 79 extensions::CommandService* command_service = | 67 extensions::CommandService* command_service = |
| 80 extensions::CommandServiceFactory::GetForProfile( | 68 extensions::CommandServiceFactory::GetForProfile( |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 | 177 |
| 190 bool PageActionImageView::OnKeyPressed(const ui::KeyEvent& event) { | 178 bool PageActionImageView::OnKeyPressed(const ui::KeyEvent& event) { |
| 191 if (event.key_code() == ui::VKEY_SPACE || | 179 if (event.key_code() == ui::VKEY_SPACE || |
| 192 event.key_code() == ui::VKEY_RETURN) { | 180 event.key_code() == ui::VKEY_RETURN) { |
| 193 ExecuteAction(1); | 181 ExecuteAction(1); |
| 194 return true; | 182 return true; |
| 195 } | 183 } |
| 196 return false; | 184 return false; |
| 197 } | 185 } |
| 198 | 186 |
| 199 void PageActionImageView::OnImageLoaded(const gfx::Image& image, | |
| 200 const std::string& extension_id, | |
| 201 int index) { | |
| 202 // We loaded icons()->size() icons, plus one extra if the page action had | |
| 203 // a default icon. | |
| 204 int total_icons = static_cast<int>(page_action_->icon_paths()->size()); | |
| 205 if (!page_action_->default_icon_path().empty()) | |
| 206 total_icons++; | |
| 207 DCHECK(index < total_icons); | |
| 208 | |
| 209 // Map the index of the loaded image back to its name. If we ever get an | |
| 210 // index greater than the number of icons, it must be the default icon. | |
| 211 if (index < static_cast<int>(page_action_->icon_paths()->size())) | |
| 212 page_action_->CacheIcon(page_action_->icon_paths()->at(index), image); | |
| 213 else | |
| 214 page_action_->CacheIcon(page_action_->default_icon_path(), image); | |
| 215 | |
| 216 // During object construction owner_ will be NULL. | |
| 217 TabContents* tab_contents = owner_ ? owner_->GetTabContents() : NULL; | |
| 218 if (tab_contents) | |
| 219 UpdateVisibility(tab_contents->web_contents(), current_url_); | |
| 220 } | |
| 221 | |
| 222 void PageActionImageView::ShowContextMenuForView(View* source, | 187 void PageActionImageView::ShowContextMenuForView(View* source, |
| 223 const gfx::Point& point) { | 188 const gfx::Point& point) { |
| 224 const Extension* extension = owner_->profile()->GetExtensionService()-> | 189 const Extension* extension = owner_->profile()->GetExtensionService()-> |
| 225 GetExtensionById(page_action()->extension_id(), false); | 190 GetExtensionById(page_action()->extension_id(), false); |
| 226 if (!extension->ShowConfigureContextMenus()) | 191 if (!extension->ShowConfigureContextMenus()) |
| 227 return; | 192 return; |
| 228 | 193 |
| 229 scoped_refptr<ExtensionContextMenuModel> context_menu_model( | 194 scoped_refptr<ExtensionContextMenuModel> context_menu_model( |
| 230 new ExtensionContextMenuModel(extension, browser_)); | 195 new ExtensionContextMenuModel(extension, browser_)); |
| 231 views::MenuModelAdapter menu_model_adapter(context_menu_model.get()); | 196 views::MenuModelAdapter menu_model_adapter(context_menu_model.get()); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 (!preview_enabled_ && !page_action_->GetIsVisible(current_tab_id_))) { | 228 (!preview_enabled_ && !page_action_->GetIsVisible(current_tab_id_))) { |
| 264 SetVisible(false); | 229 SetVisible(false); |
| 265 return; | 230 return; |
| 266 } | 231 } |
| 267 | 232 |
| 268 // Set the tooltip. | 233 // Set the tooltip. |
| 269 tooltip_ = page_action_->GetTitle(current_tab_id_); | 234 tooltip_ = page_action_->GetTitle(current_tab_id_); |
| 270 SetTooltipText(UTF8ToUTF16(tooltip_)); | 235 SetTooltipText(UTF8ToUTF16(tooltip_)); |
| 271 | 236 |
| 272 // Set the image. | 237 // Set the image. |
| 273 gfx::Image icon = page_action_->GetIcon(current_tab_id_); | 238 gfx::Image icon = page_action_->GetIcon(current_tab_id_, icon_factory_.get()); |
| 274 if (!icon.IsEmpty()) | 239 if (!icon.IsEmpty()) |
| 275 SetImage(*icon.ToImageSkia()); | 240 SetImage(*icon.ToImageSkia()); |
| 276 | 241 |
| 277 SetVisible(true); | 242 SetVisible(true); |
| 278 } | 243 } |
| 279 | 244 |
| 280 void PageActionImageView::OnWidgetClosing(views::Widget* widget) { | 245 void PageActionImageView::OnWidgetClosing(views::Widget* widget) { |
| 281 DCHECK_EQ(popup_->GetWidget(), widget); | 246 DCHECK_EQ(popup_->GetWidget(), widget); |
| 282 popup_->GetWidget()->RemoveObserver(this); | 247 popup_->GetWidget()->RemoveObserver(this); |
| 283 popup_ = NULL; | 248 popup_ = NULL; |
| 284 } | 249 } |
| 285 | 250 |
| 286 void PageActionImageView::Observe(int type, | 251 void PageActionImageView::Observe(int type, |
| 287 const content::NotificationSource& source, | 252 const content::NotificationSource& source, |
| 288 const content::NotificationDetails& details) { | 253 const content::NotificationDetails& details) { |
| 289 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UNLOADED, type); | 254 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UNLOADED, type); |
| 290 const Extension* unloaded_extension = | 255 const Extension* unloaded_extension = |
| 291 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 256 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
| 292 if (page_action_ == unloaded_extension ->page_action()) | 257 if (page_action_ == unloaded_extension->page_action()) |
| 293 owner_->UpdatePageActions(); | 258 owner_->UpdatePageActions(); |
| 294 } | 259 } |
| 295 | 260 |
| 296 void PageActionImageView::OnIconChanged( | 261 void PageActionImageView::OnIconUpdated() { |
| 297 const ExtensionAction::IconAnimation& animation) { | |
| 298 TabContents* tab_contents = owner_->GetTabContents(); | 262 TabContents* tab_contents = owner_->GetTabContents(); |
| 299 if (tab_contents) | 263 if (tab_contents) |
| 300 UpdateVisibility(tab_contents->web_contents(), current_url_); | 264 UpdateVisibility(tab_contents->web_contents(), current_url_); |
| 301 } | 265 } |
| 302 | 266 |
| 267 void PageActionImageView::OnIconChanged( |
| 268 const ExtensionAction::IconAnimation& animation) { |
| 269 OnIconUpdated(); |
| 270 } |
| 271 |
| 303 void PageActionImageView::ShowPopupWithURL(const GURL& popup_url) { | 272 void PageActionImageView::ShowPopupWithURL(const GURL& popup_url) { |
| 304 bool popup_showing = popup_ != NULL; | 273 bool popup_showing = popup_ != NULL; |
| 305 | 274 |
| 306 // Always hide the current popup. Only one popup at a time. | 275 // Always hide the current popup. Only one popup at a time. |
| 307 HidePopup(); | 276 HidePopup(); |
| 308 | 277 |
| 309 // If we were already showing, then treat this click as a dismiss. | 278 // If we were already showing, then treat this click as a dismiss. |
| 310 if (popup_showing) | 279 if (popup_showing) |
| 311 return; | 280 return; |
| 312 | 281 |
| 313 views::BubbleBorder::ArrowLocation arrow_location = base::i18n::IsRTL() ? | 282 views::BubbleBorder::ArrowLocation arrow_location = base::i18n::IsRTL() ? |
| 314 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; | 283 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; |
| 315 | 284 |
| 316 popup_ = ExtensionPopup::ShowPopup(popup_url, browser_, this, arrow_location); | 285 popup_ = ExtensionPopup::ShowPopup(popup_url, browser_, this, arrow_location); |
| 317 popup_->GetWidget()->AddObserver(this); | 286 popup_->GetWidget()->AddObserver(this); |
| 318 } | 287 } |
| 319 | 288 |
| 320 void PageActionImageView::HidePopup() { | 289 void PageActionImageView::HidePopup() { |
| 321 if (popup_) | 290 if (popup_) |
| 322 popup_->GetWidget()->Close(); | 291 popup_->GetWidget()->Close(); |
| 323 } | 292 } |
| OLD | NEW |