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