1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/views/location_bar/page_action_image_view.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/extensions/extension_browser_event_router.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/views/frame/browser_view.h"
#include "chrome/browser/views/location_bar/location_bar_view.h"
#include "chrome/common/platform_util.h"
#include "views/controls/menu/menu_2.h"
PageActionImageView::PageActionImageView(LocationBarView* owner,
Profile* profile,
ExtensionAction* page_action)
: owner_(owner),
profile_(profile),
page_action_(page_action),
ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)),
current_tab_id_(-1),
preview_enabled_(false),
popup_(NULL) {
Extension* extension = profile->GetExtensionsService()->GetExtensionById(
page_action->extension_id(), false);
DCHECK(extension);
// Load all the icons declared in the manifest. This is the contents of the
// icons array, plus the default_icon property, if any.
std::vector<std::string> icon_paths(*page_action->icon_paths());
if (!page_action_->default_icon_path().empty())
icon_paths.push_back(page_action_->default_icon_path());
for (std::vector<std::string>::iterator iter = icon_paths.begin();
iter != icon_paths.end(); ++iter) {
tracker_.LoadImage(extension, extension->GetResource(*iter),
gfx::Size(Extension::kPageActionIconMaxSize,
Extension::kPageActionIconMaxSize),
ImageLoadingTracker::DONT_CACHE);
}
}
PageActionImageView::~PageActionImageView() {
if (popup_)
HidePopup();
}
void PageActionImageView::ExecuteAction(int button,
bool inspect_with_devtools) {
if (current_tab_id_ < 0) {
NOTREACHED() << "No current tab.";
return;
}
if (page_action_->HasPopup(current_tab_id_)) {
// In tests, GetLastActive could return NULL, so we need to have
// a fallback.
// TODO(erikkay): Find a better way to get the Browser that this
// button is in.
Browser* browser = BrowserList::GetLastActiveWithProfile(profile_);
if (!browser)
browser = BrowserList::FindBrowserWithProfile(profile_);
DCHECK(browser);
bool popup_showing = popup_ != NULL;
// Always hide the current popup. Only one popup at a time.
HidePopup();
// If we were already showing, then treat this click as a dismiss.
if (popup_showing)
return;
gfx::Rect screen_bounds(GetImageBounds());
gfx::Point origin(screen_bounds.origin());
View::ConvertPointToScreen(this, &origin);
screen_bounds.set_origin(origin);
popup_ = ExtensionPopup::Show(
page_action_->GetPopupUrl(current_tab_id_),
browser,
browser->profile(),
browser->window()->GetNativeHandle(),
screen_bounds,
BubbleBorder::TOP_RIGHT,
true, // Activate the popup window.
inspect_with_devtools,
ExtensionPopup::BUBBLE_CHROME,
this); // ExtensionPopup::Observer
} else {
ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted(
profile_, page_action_->extension_id(), page_action_->id(),
current_tab_id_, current_url_.spec(), button);
}
}
bool PageActionImageView::OnMousePressed(const views::MouseEvent& event) {
// We want to show the bubble on mouse release; that is the standard behavior
// for buttons. (Also, triggering on mouse press causes bugs like
// http://crbug.com/33155.)
return true;
}
void PageActionImageView::OnMouseReleased(const views::MouseEvent& event,
bool canceled) {
if (canceled || !HitTest(event.location()))
return;
int button = -1;
if (event.IsLeftMouseButton()) {
button = 1;
} else if (event.IsMiddleMouseButton()) {
button = 2;
} else if (event.IsRightMouseButton()) {
// Get the top left point of this button in screen coordinates.
gfx::Point menu_origin;
ConvertPointToScreen(this, &menu_origin);
// Make the menu appear below the button.
menu_origin.Offset(0, height());
Extension* extension = profile_->GetExtensionsService()->GetExtensionById(
page_action()->extension_id(), false);
Browser* browser = BrowserView::GetBrowserViewForNativeWindow(
platform_util::GetTopLevel(GetWidget()->GetNativeView()))->browser();
context_menu_contents_ =
new ExtensionContextMenuModel(extension, browser, this);
context_menu_menu_.reset(new views::Menu2(context_menu_contents_.get()));
context_menu_menu_->RunContextMenuAt(menu_origin);
return;
}
ExecuteAction(button, false); // inspect_with_devtools
}
void PageActionImageView::OnImageLoaded(
SkBitmap* image, ExtensionResource resource, int index) {
// We loaded icons()->size() icons, plus one extra if the page action had
// a default icon.
int total_icons = static_cast<int>(page_action_->icon_paths()->size());
if (!page_action_->default_icon_path().empty())
total_icons++;
DCHECK(index < total_icons);
// Map the index of the loaded image back to its name. If we ever get an
// index greater than the number of icons, it must be the default icon.
if (image) {
if (index < static_cast<int>(page_action_->icon_paths()->size()))
page_action_icons_[page_action_->icon_paths()->at(index)] = *image;
else
page_action_icons_[page_action_->default_icon_path()] = *image;
}
owner_->UpdatePageActions();
}
void PageActionImageView::UpdateVisibility(TabContents* contents,
const GURL& url) {
// Save this off so we can pass it back to the extension when the action gets
// executed. See PageActionImageView::OnMousePressed.
current_tab_id_ = ExtensionTabUtil::GetTabId(contents);
current_url_ = url;
bool visible =
preview_enabled_ || page_action_->GetIsVisible(current_tab_id_);
if (visible) {
// Set the tooltip.
tooltip_ = page_action_->GetTitle(current_tab_id_);
SetTooltipText(UTF8ToWide(tooltip_));
// Set the image.
// It can come from three places. In descending order of priority:
// - The developer can set it dynamically by path or bitmap. It will be in
// page_action_->GetIcon().
// - The developer can set it dynamically by index. It will be in
// page_action_->GetIconIndex().
// - It can be set in the manifest by path. It will be in page_action_->
// default_icon_path().
// First look for a dynamically set bitmap.
SkBitmap icon = page_action_->GetIcon(current_tab_id_);
if (icon.isNull()) {
int icon_index = page_action_->GetIconIndex(current_tab_id_);
std::string icon_path;
if (icon_index >= 0)
icon_path = page_action_->icon_paths()->at(icon_index);
else
icon_path = page_action_->default_icon_path();
if (!icon_path.empty()) {
PageActionMap::iterator iter = page_action_icons_.find(icon_path);
if (iter != page_action_icons_.end())
icon = iter->second;
}
}
if (!icon.isNull())
SetImage(&icon);
}
SetVisible(visible);
}
void PageActionImageView::InspectPopup(ExtensionAction* action) {
ExecuteAction(1, // left-click
true); // inspect_with_devtools
}
void PageActionImageView::ExtensionPopupClosed(ExtensionPopup* popup) {
DCHECK_EQ(popup_, popup);
// ExtensionPopup is ref-counted, so we don't need to delete it.
popup_ = NULL;
}
void PageActionImageView::HidePopup() {
if (popup_)
popup_->Close();
}
|