| 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/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/extensions/extension_action.h" | 8 #include "chrome/browser/extensions/extension_action.h" |
| 9 #include "chrome/browser/platform_util.h" | 9 #include "chrome/browser/platform_util.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/sessions/session_tab_helper.h" |
| 11 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 13 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
| 13 #include "extensions/browser/extension_registry.h" | 14 #include "extensions/browser/extension_registry.h" |
| 14 #include "ui/accessibility/ax_view_state.h" | 15 #include "ui/accessibility/ax_view_state.h" |
| 15 #include "ui/events/event.h" | 16 #include "ui/events/event.h" |
| 16 #include "ui/gfx/canvas.h" | 17 #include "ui/gfx/canvas.h" |
| 17 #include "ui/gfx/image/image.h" | 18 #include "ui/gfx/image/image.h" |
| 18 | 19 |
| 19 // static | 20 // static |
| 20 const char PageActionImageView::kViewClassName[] = "PageActionImageView"; | 21 const char PageActionImageView::kViewClassName[] = "PageActionImageView"; |
| 21 | 22 |
| 22 PageActionImageView::PageActionImageView(LocationBarView* owner, | 23 PageActionImageView::PageActionImageView(LocationBarView* owner, |
| 23 ExtensionAction* page_action, | 24 ExtensionAction* page_action, |
| 24 Browser* browser) | 25 Browser* browser) |
| 25 : view_controller_(new ExtensionActionViewController( | 26 : view_controller_(new ExtensionActionViewController( |
| 26 extensions::ExtensionRegistry::Get(browser->profile())-> | 27 extensions::ExtensionRegistry::Get(browser->profile())-> |
| 27 enabled_extensions().GetByID(page_action->extension_id()), | 28 enabled_extensions().GetByID(page_action->extension_id()), |
| 28 browser, | 29 browser, |
| 29 page_action, | 30 page_action)), |
| 30 this)), | |
| 31 owner_(owner), | 31 owner_(owner), |
| 32 preview_enabled_(false) { | 32 preview_enabled_(false) { |
| 33 // There should be an associated focus manager so that we can safely register | 33 // There should be an associated focus manager so that we can safely register |
| 34 // accelerators for commands. | 34 // accelerators for commands. |
| 35 DCHECK(GetFocusManagerForAccelerator()); | 35 DCHECK(GetFocusManagerForAccelerator()); |
| 36 SetAccessibilityFocusable(true); | 36 SetAccessibilityFocusable(true); |
| 37 view_controller_->SetDelegate(this); |
| 37 view_controller_->RegisterCommand(); | 38 view_controller_->RegisterCommand(); |
| 38 set_context_menu_controller(view_controller_.get()); | |
| 39 } | 39 } |
| 40 | 40 |
| 41 PageActionImageView::~PageActionImageView() { | 41 PageActionImageView::~PageActionImageView() { |
| 42 } | 42 } |
| 43 | 43 |
| 44 const char* PageActionImageView::GetClassName() const { | 44 const char* PageActionImageView::GetClassName() const { |
| 45 return kViewClassName; | 45 return kViewClassName; |
| 46 } | 46 } |
| 47 | 47 |
| 48 void PageActionImageView::GetAccessibleState(ui::AXViewState* state) { | 48 void PageActionImageView::GetAccessibleState(ui::AXViewState* state) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 60 void PageActionImageView::OnMouseReleased(const ui::MouseEvent& event) { | 60 void PageActionImageView::OnMouseReleased(const ui::MouseEvent& event) { |
| 61 if (!HitTestPoint(event.location())) | 61 if (!HitTestPoint(event.location())) |
| 62 return; | 62 return; |
| 63 | 63 |
| 64 if (event.IsRightMouseButton()) { | 64 if (event.IsRightMouseButton()) { |
| 65 // Don't show a menu here, its handled in View::ProcessMouseReleased. We | 65 // Don't show a menu here, its handled in View::ProcessMouseReleased. We |
| 66 // show the context menu by way of being the ContextMenuController. | 66 // show the context menu by way of being the ContextMenuController. |
| 67 return; | 67 return; |
| 68 } | 68 } |
| 69 | 69 |
| 70 view_controller_->ExecuteActionByUser(); | 70 view_controller_->ExecuteAction(true); |
| 71 } | 71 } |
| 72 | 72 |
| 73 bool PageActionImageView::OnKeyPressed(const ui::KeyEvent& event) { | 73 bool PageActionImageView::OnKeyPressed(const ui::KeyEvent& event) { |
| 74 if (event.key_code() == ui::VKEY_SPACE || | 74 if (event.key_code() == ui::VKEY_SPACE || |
| 75 event.key_code() == ui::VKEY_RETURN) { | 75 event.key_code() == ui::VKEY_RETURN) { |
| 76 view_controller_->ExecuteActionByUser(); | 76 view_controller_->ExecuteAction(true); |
| 77 return true; | 77 return true; |
| 78 } | 78 } |
| 79 return false; | 79 return false; |
| 80 } | 80 } |
| 81 | 81 |
| 82 void PageActionImageView::OnGestureEvent(ui::GestureEvent* event) { | 82 void PageActionImageView::OnGestureEvent(ui::GestureEvent* event) { |
| 83 if (event->type() == ui::ET_GESTURE_TAP) { | 83 if (event->type() == ui::ET_GESTURE_TAP) { |
| 84 view_controller_->ExecuteActionByUser(); | 84 view_controller_->ExecuteAction(true); |
| 85 event->SetHandled(); | 85 event->SetHandled(); |
| 86 } | 86 } |
| 87 } | 87 } |
| 88 | 88 |
| 89 void PageActionImageView::UpdateVisibility(content::WebContents* contents) { | 89 void PageActionImageView::UpdateVisibility(content::WebContents* contents) { |
| 90 int tab_id = view_controller_->GetCurrentTabId(); | 90 int tab_id = SessionTabHelper::IdForTab(contents); |
| 91 | |
| 92 if (!contents || | 91 if (!contents || |
| 93 tab_id == -1 || | 92 tab_id == -1 || |
| 94 (!preview_enabled_ && !extension_action()->GetIsVisible(tab_id))) { | 93 (!preview_enabled_ && !extension_action()->GetIsVisible(tab_id))) { |
| 95 SetVisible(false); | 94 SetVisible(false); |
| 96 return; | 95 return; |
| 97 } | 96 } |
| 98 | 97 |
| 99 // Set the tooltip. | 98 // Set the tooltip. |
| 100 tooltip_ = extension_action()->GetTitle(tab_id); | 99 tooltip_ = extension_action()->GetTitle(tab_id); |
| 101 SetTooltipText(base::UTF8ToUTF16(tooltip_)); | 100 SetTooltipText(base::UTF8ToUTF16(tooltip_)); |
| 102 | 101 |
| 103 // Set the image. | 102 // Set the image. |
| 104 gfx::Image icon = view_controller_->GetIcon(tab_id); | 103 gfx::Image icon = view_controller_->GetIcon(contents); |
| 105 if (!icon.IsEmpty()) | 104 if (!icon.IsEmpty()) |
| 106 SetImage(*icon.ToImageSkia()); | 105 SetImage(*icon.ToImageSkia()); |
| 107 | 106 |
| 108 SetVisible(true); | 107 SetVisible(true); |
| 109 } | 108 } |
| 110 | 109 |
| 111 void PageActionImageView::PaintChildren(gfx::Canvas* canvas, | 110 void PageActionImageView::PaintChildren(gfx::Canvas* canvas, |
| 112 const views::CullSet& cull_set) { | 111 const views::CullSet& cull_set) { |
| 113 View::PaintChildren(canvas, cull_set); | 112 View::PaintChildren(canvas, cull_set); |
| 114 int tab_id = view_controller_->GetCurrentTabId(); | 113 int tab_id = SessionTabHelper::IdForTab(GetCurrentWebContents()); |
| 115 if (tab_id >= 0) { | 114 if (tab_id >= 0) { |
| 116 view_controller_->extension_action()->PaintBadge( | 115 view_controller_->extension_action()->PaintBadge( |
| 117 canvas, GetLocalBounds(), tab_id); | 116 canvas, GetLocalBounds(), tab_id); |
| 118 } | 117 } |
| 119 } | 118 } |
| 120 | 119 |
| 121 void PageActionImageView::OnIconUpdated() { | 120 void PageActionImageView::OnIconUpdated() { |
| 122 UpdateVisibility(GetCurrentWebContents()); | 121 UpdateVisibility(GetCurrentWebContents()); |
| 123 } | 122 } |
| 124 | 123 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 144 } | 143 } |
| 145 | 144 |
| 146 views::View* PageActionImageView::GetReferenceViewForPopup() { | 145 views::View* PageActionImageView::GetReferenceViewForPopup() { |
| 147 return this; | 146 return this; |
| 148 } | 147 } |
| 149 | 148 |
| 150 views::MenuButton* PageActionImageView::GetContextMenuButton() { | 149 views::MenuButton* PageActionImageView::GetContextMenuButton() { |
| 151 return NULL; // No menu button for page action views. | 150 return NULL; // No menu button for page action views. |
| 152 } | 151 } |
| 153 | 152 |
| 154 content::WebContents* PageActionImageView::GetCurrentWebContents() { | 153 content::WebContents* PageActionImageView::GetCurrentWebContents() const { |
| 155 return owner_->GetWebContents(); | 154 return owner_->GetWebContents(); |
| 156 } | 155 } |
| 157 | 156 |
| 158 void PageActionImageView::HideActivePopup() { | 157 void PageActionImageView::HideActivePopup() { |
| 159 // The only popup that will be active is this popup. | 158 // The only popup that will be active is this popup. |
| 160 view_controller_->HidePopup(); | 159 view_controller_->HidePopup(); |
| 161 } | 160 } |
| OLD | NEW |