| 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()->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 std::string path = page_action_->default_icon_path(); | 58 icon_factory_.reset( |
| 59 if (!path.empty()) { | 59 new ExtensionActionIconFactory(extension, page_action, this)); |
| 60 tracker_.LoadImage(extension, extension->GetResource(path), | |
| 61 gfx::Size(Extension::kPageActionIconMaxSize, | |
| 62 Extension::kPageActionIconMaxSize), | |
| 63 ImageLoadingTracker::DONT_CACHE); | |
| 64 } | |
| 65 | 60 |
| 66 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 61 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 67 content::Source<Profile>( | 62 content::Source<Profile>( |
| 68 owner_->profile()->GetOriginalProfile())); | 63 owner_->profile()->GetOriginalProfile())); |
| 69 | 64 |
| 70 set_accessibility_focusable(true); | 65 set_accessibility_focusable(true); |
| 71 set_context_menu_controller(this); | 66 set_context_menu_controller(this); |
| 72 | 67 |
| 73 extensions::CommandService* command_service = | 68 extensions::CommandService* command_service = |
| 74 extensions::CommandServiceFactory::GetForProfile( | 69 extensions::CommandServiceFactory::GetForProfile( |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 | 176 |
| 182 bool PageActionImageView::OnKeyPressed(const ui::KeyEvent& event) { | 177 bool PageActionImageView::OnKeyPressed(const ui::KeyEvent& event) { |
| 183 if (event.key_code() == ui::VKEY_SPACE || | 178 if (event.key_code() == ui::VKEY_SPACE || |
| 184 event.key_code() == ui::VKEY_RETURN) { | 179 event.key_code() == ui::VKEY_RETURN) { |
| 185 ExecuteAction(ExtensionPopup::SHOW); | 180 ExecuteAction(ExtensionPopup::SHOW); |
| 186 return true; | 181 return true; |
| 187 } | 182 } |
| 188 return false; | 183 return false; |
| 189 } | 184 } |
| 190 | 185 |
| 191 void PageActionImageView::OnImageLoaded(const gfx::Image& image, | |
| 192 const std::string& extension_id, | |
| 193 int index) { | |
| 194 page_action_->CacheIcon(image); | |
| 195 | |
| 196 // During object construction owner_ will be NULL. | |
| 197 TabContents* tab_contents = owner_ ? owner_->GetTabContents() : NULL; | |
| 198 if (tab_contents) | |
| 199 UpdateVisibility(tab_contents->web_contents(), current_url_); | |
| 200 } | |
| 201 | |
| 202 void PageActionImageView::ShowContextMenuForView(View* source, | 186 void PageActionImageView::ShowContextMenuForView(View* source, |
| 203 const gfx::Point& point) { | 187 const gfx::Point& point) { |
| 204 const Extension* extension = owner_->profile()->GetExtensionService()-> | 188 const Extension* extension = owner_->profile()->GetExtensionService()-> |
| 205 GetExtensionById(page_action()->extension_id(), false); | 189 GetExtensionById(page_action()->extension_id(), false); |
| 206 if (!extension->ShowConfigureContextMenus()) | 190 if (!extension->ShowConfigureContextMenus()) |
| 207 return; | 191 return; |
| 208 | 192 |
| 209 scoped_refptr<ExtensionContextMenuModel> context_menu_model( | 193 scoped_refptr<ExtensionContextMenuModel> context_menu_model( |
| 210 new ExtensionContextMenuModel(extension, browser_, this)); | 194 new ExtensionContextMenuModel(extension, browser_, this)); |
| 211 views::MenuModelAdapter menu_model_adapter(context_menu_model.get()); | 195 views::MenuModelAdapter menu_model_adapter(context_menu_model.get()); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 (!preview_enabled_ && !page_action_->GetIsVisible(current_tab_id_))) { | 227 (!preview_enabled_ && !page_action_->GetIsVisible(current_tab_id_))) { |
| 244 SetVisible(false); | 228 SetVisible(false); |
| 245 return; | 229 return; |
| 246 } | 230 } |
| 247 | 231 |
| 248 // Set the tooltip. | 232 // Set the tooltip. |
| 249 tooltip_ = page_action_->GetTitle(current_tab_id_); | 233 tooltip_ = page_action_->GetTitle(current_tab_id_); |
| 250 SetTooltipText(UTF8ToUTF16(tooltip_)); | 234 SetTooltipText(UTF8ToUTF16(tooltip_)); |
| 251 | 235 |
| 252 // Set the image. | 236 // Set the image. |
| 253 gfx::Image icon = page_action_->GetIcon(current_tab_id_); | 237 gfx::Image icon = icon_factory_->GetIcon(current_tab_id_); |
| 254 if (!icon.IsEmpty()) | 238 if (!icon.IsEmpty()) |
| 255 SetImage(*icon.ToImageSkia()); | 239 SetImage(*icon.ToImageSkia()); |
| 256 | 240 |
| 257 SetVisible(true); | 241 SetVisible(true); |
| 258 } | 242 } |
| 259 | 243 |
| 260 void PageActionImageView::InspectPopup(ExtensionAction* action) { | 244 void PageActionImageView::InspectPopup(ExtensionAction* action) { |
| 261 ExecuteAction(ExtensionPopup::SHOW_AND_INSPECT); | 245 ExecuteAction(ExtensionPopup::SHOW_AND_INSPECT); |
| 262 } | 246 } |
| 263 | 247 |
| 264 void PageActionImageView::OnWidgetClosing(views::Widget* widget) { | 248 void PageActionImageView::OnWidgetClosing(views::Widget* widget) { |
| 265 DCHECK_EQ(popup_->GetWidget(), widget); | 249 DCHECK_EQ(popup_->GetWidget(), widget); |
| 266 popup_->GetWidget()->RemoveObserver(this); | 250 popup_->GetWidget()->RemoveObserver(this); |
| 267 popup_ = NULL; | 251 popup_ = NULL; |
| 268 } | 252 } |
| 269 | 253 |
| 270 void PageActionImageView::Observe(int type, | 254 void PageActionImageView::Observe(int type, |
| 271 const content::NotificationSource& source, | 255 const content::NotificationSource& source, |
| 272 const content::NotificationDetails& details) { | 256 const content::NotificationDetails& details) { |
| 273 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UNLOADED, type); | 257 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UNLOADED, type); |
| 274 const Extension* unloaded_extension = | 258 const Extension* unloaded_extension = |
| 275 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 259 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
| 276 if (page_action_ == unloaded_extension ->page_action()) | 260 if (page_action_ == unloaded_extension->page_action()) |
| 277 owner_->UpdatePageActions(); | 261 owner_->UpdatePageActions(); |
| 278 } | 262 } |
| 279 | 263 |
| 280 void PageActionImageView::OnIconChanged() { | 264 void PageActionImageView::OnIconUpdated() { |
| 281 TabContents* tab_contents = owner_->GetTabContents(); | 265 TabContents* tab_contents = owner_->GetTabContents(); |
| 282 if (tab_contents) | 266 if (tab_contents) |
| 283 UpdateVisibility(tab_contents->web_contents(), current_url_); | 267 UpdateVisibility(tab_contents->web_contents(), current_url_); |
| 284 } | 268 } |
| 285 | 269 |
| 270 void PageActionImageView::OnIconChanged() { |
| 271 OnIconUpdated(); |
| 272 } |
| 273 |
| 286 void PageActionImageView::ShowPopupWithURL( | 274 void PageActionImageView::ShowPopupWithURL( |
| 287 const GURL& popup_url, | 275 const GURL& popup_url, |
| 288 ExtensionPopup::ShowAction show_action) { | 276 ExtensionPopup::ShowAction show_action) { |
| 289 bool popup_showing = popup_ != NULL; | 277 bool popup_showing = popup_ != NULL; |
| 290 | 278 |
| 291 // Always hide the current popup. Only one popup at a time. | 279 // Always hide the current popup. Only one popup at a time. |
| 292 HidePopup(); | 280 HidePopup(); |
| 293 | 281 |
| 294 // If we were already showing, then treat this click as a dismiss. | 282 // If we were already showing, then treat this click as a dismiss. |
| 295 if (popup_showing) | 283 if (popup_showing) |
| 296 return; | 284 return; |
| 297 | 285 |
| 298 views::BubbleBorder::ArrowLocation arrow_location = base::i18n::IsRTL() ? | 286 views::BubbleBorder::ArrowLocation arrow_location = base::i18n::IsRTL() ? |
| 299 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; | 287 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; |
| 300 | 288 |
| 301 popup_ = ExtensionPopup::ShowPopup(popup_url, browser_, this, arrow_location, | 289 popup_ = ExtensionPopup::ShowPopup(popup_url, browser_, this, arrow_location, |
| 302 show_action); | 290 show_action); |
| 303 popup_->GetWidget()->AddObserver(this); | 291 popup_->GetWidget()->AddObserver(this); |
| 304 } | 292 } |
| 305 | 293 |
| 306 void PageActionImageView::HidePopup() { | 294 void PageActionImageView::HidePopup() { |
| 307 if (popup_) | 295 if (popup_) |
| 308 popup_->GetWidget()->Close(); | 296 popup_->GetWidget()->Close(); |
| 309 } | 297 } |
| OLD | NEW |