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 // 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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 | 175 |
188 bool PageActionImageView::OnKeyPressed(const ui::KeyEvent& event) { | 176 bool PageActionImageView::OnKeyPressed(const ui::KeyEvent& event) { |
189 if (event.key_code() == ui::VKEY_SPACE || | 177 if (event.key_code() == ui::VKEY_SPACE || |
190 event.key_code() == ui::VKEY_RETURN) { | 178 event.key_code() == ui::VKEY_RETURN) { |
191 ExecuteAction(ExtensionPopup::SHOW); | 179 ExecuteAction(ExtensionPopup::SHOW); |
192 return true; | 180 return true; |
193 } | 181 } |
194 return false; | 182 return false; |
195 } | 183 } |
196 | 184 |
197 void PageActionImageView::OnImageLoaded(const gfx::Image& image, | |
198 const std::string& extension_id, | |
199 int index) { | |
200 // We loaded icons()->size() icons, plus one extra if the page action had | |
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 | |
214 // During object construction owner_ will be NULL. | |
215 TabContents* tab_contents = owner_ ? owner_->GetTabContents() : NULL; | |
216 if (tab_contents) | |
217 UpdateVisibility(tab_contents->web_contents(), current_url_); | |
218 } | |
219 | |
220 void PageActionImageView::ShowContextMenuForView(View* source, | 185 void PageActionImageView::ShowContextMenuForView(View* source, |
221 const gfx::Point& point) { | 186 const gfx::Point& point) { |
222 const Extension* extension = owner_->profile()->GetExtensionService()-> | 187 const Extension* extension = owner_->profile()->GetExtensionService()-> |
223 GetExtensionById(page_action()->extension_id(), false); | 188 GetExtensionById(page_action()->extension_id(), false); |
224 if (!extension->ShowConfigureContextMenus()) | 189 if (!extension->ShowConfigureContextMenus()) |
225 return; | 190 return; |
226 | 191 |
227 scoped_refptr<ExtensionContextMenuModel> context_menu_model( | 192 scoped_refptr<ExtensionContextMenuModel> context_menu_model( |
228 new ExtensionContextMenuModel(extension, browser_, this)); | 193 new ExtensionContextMenuModel(extension, browser_, this)); |
229 views::MenuModelAdapter menu_model_adapter(context_menu_model.get()); | 194 views::MenuModelAdapter menu_model_adapter(context_menu_model.get()); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 (!preview_enabled_ && !page_action_->GetIsVisible(current_tab_id_))) { | 226 (!preview_enabled_ && !page_action_->GetIsVisible(current_tab_id_))) { |
262 SetVisible(false); | 227 SetVisible(false); |
263 return; | 228 return; |
264 } | 229 } |
265 | 230 |
266 // Set the tooltip. | 231 // Set the tooltip. |
267 tooltip_ = page_action_->GetTitle(current_tab_id_); | 232 tooltip_ = page_action_->GetTitle(current_tab_id_); |
268 SetTooltipText(UTF8ToUTF16(tooltip_)); | 233 SetTooltipText(UTF8ToUTF16(tooltip_)); |
269 | 234 |
270 // Set the image. | 235 // Set the image. |
271 gfx::Image icon = page_action_->GetIcon(current_tab_id_); | 236 gfx::Image icon = page_action_->GetIcon(current_tab_id_, icon_factory_.get()); |
272 if (!icon.IsEmpty()) | 237 if (!icon.IsEmpty()) |
273 SetImage(*icon.ToImageSkia()); | 238 SetImage(*icon.ToImageSkia()); |
274 | 239 |
275 SetVisible(true); | 240 SetVisible(true); |
276 } | 241 } |
277 | 242 |
278 void PageActionImageView::InspectPopup(ExtensionAction* action) { | 243 void PageActionImageView::InspectPopup(ExtensionAction* action) { |
279 ExecuteAction(ExtensionPopup::SHOW_AND_INSPECT); | 244 ExecuteAction(ExtensionPopup::SHOW_AND_INSPECT); |
280 } | 245 } |
281 | 246 |
282 void PageActionImageView::OnWidgetClosing(views::Widget* widget) { | 247 void PageActionImageView::OnWidgetClosing(views::Widget* widget) { |
283 DCHECK_EQ(popup_->GetWidget(), widget); | 248 DCHECK_EQ(popup_->GetWidget(), widget); |
284 popup_->GetWidget()->RemoveObserver(this); | 249 popup_->GetWidget()->RemoveObserver(this); |
285 popup_ = NULL; | 250 popup_ = NULL; |
286 } | 251 } |
287 | 252 |
288 void PageActionImageView::Observe(int type, | 253 void PageActionImageView::Observe(int type, |
289 const content::NotificationSource& source, | 254 const content::NotificationSource& source, |
290 const content::NotificationDetails& details) { | 255 const content::NotificationDetails& details) { |
291 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UNLOADED, type); | 256 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UNLOADED, type); |
292 const Extension* unloaded_extension = | 257 const Extension* unloaded_extension = |
293 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 258 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
294 if (page_action_ == unloaded_extension ->page_action()) | 259 if (page_action_ == unloaded_extension->page_action()) |
295 owner_->UpdatePageActions(); | 260 owner_->UpdatePageActions(); |
296 } | 261 } |
297 | 262 |
298 void PageActionImageView::OnIconChanged() { | 263 void PageActionImageView::OnIconUpdated() { |
299 TabContents* tab_contents = owner_->GetTabContents(); | 264 TabContents* tab_contents = owner_->GetTabContents(); |
300 if (tab_contents) | 265 if (tab_contents) |
301 UpdateVisibility(tab_contents->web_contents(), current_url_); | 266 UpdateVisibility(tab_contents->web_contents(), current_url_); |
302 } | 267 } |
303 | 268 |
| 269 void PageActionImageView::OnIconChanged() { |
| 270 OnIconUpdated(); |
| 271 } |
| 272 |
304 void PageActionImageView::ShowPopupWithURL( | 273 void PageActionImageView::ShowPopupWithURL( |
305 const GURL& popup_url, | 274 const GURL& popup_url, |
306 ExtensionPopup::ShowAction show_action) { | 275 ExtensionPopup::ShowAction show_action) { |
307 bool popup_showing = popup_ != NULL; | 276 bool popup_showing = popup_ != NULL; |
308 | 277 |
309 // Always hide the current popup. Only one popup at a time. | 278 // Always hide the current popup. Only one popup at a time. |
310 HidePopup(); | 279 HidePopup(); |
311 | 280 |
312 // If we were already showing, then treat this click as a dismiss. | 281 // If we were already showing, then treat this click as a dismiss. |
313 if (popup_showing) | 282 if (popup_showing) |
314 return; | 283 return; |
315 | 284 |
316 views::BubbleBorder::ArrowLocation arrow_location = base::i18n::IsRTL() ? | 285 views::BubbleBorder::ArrowLocation arrow_location = base::i18n::IsRTL() ? |
317 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; | 286 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; |
318 | 287 |
319 popup_ = ExtensionPopup::ShowPopup(popup_url, browser_, this, arrow_location, | 288 popup_ = ExtensionPopup::ShowPopup(popup_url, browser_, this, arrow_location, |
320 show_action); | 289 show_action); |
321 popup_->GetWidget()->AddObserver(this); | 290 popup_->GetWidget()->AddObserver(this); |
322 } | 291 } |
323 | 292 |
324 void PageActionImageView::HidePopup() { | 293 void PageActionImageView::HidePopup() { |
325 if (popup_) | 294 if (popup_) |
326 popup_->GetWidget()->Close(); | 295 popup_->GetWidget()->Close(); |
327 } | 296 } |
OLD | NEW |