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/gtk/location_bar_view_gtk.h" | 5 #include "chrome/browser/ui/gtk/location_bar_view_gtk.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 19 matching lines...) Expand all Loading... | |
30 #include "chrome/browser/profiles/profile.h" | 30 #include "chrome/browser/profiles/profile.h" |
31 #include "chrome/browser/search_engines/template_url.h" | 31 #include "chrome/browser/search_engines/template_url.h" |
32 #include "chrome/browser/search_engines/template_url_service.h" | 32 #include "chrome/browser/search_engines/template_url_service.h" |
33 #include "chrome/browser/search_engines/template_url_service_factory.h" | 33 #include "chrome/browser/search_engines/template_url_service_factory.h" |
34 #include "chrome/browser/ui/browser.h" | 34 #include "chrome/browser/ui/browser.h" |
35 #include "chrome/browser/ui/browser_list.h" | 35 #include "chrome/browser/ui/browser_list.h" |
36 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h" | 36 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h" |
37 #include "chrome/browser/ui/content_settings/content_setting_image_model.h" | 37 #include "chrome/browser/ui/content_settings/content_setting_image_model.h" |
38 #include "chrome/browser/ui/gtk/bookmarks/bookmark_bubble_gtk.h" | 38 #include "chrome/browser/ui/gtk/bookmarks/bookmark_bubble_gtk.h" |
39 #include "chrome/browser/ui/gtk/bookmarks/bookmark_utils_gtk.h" | 39 #include "chrome/browser/ui/gtk/bookmarks/bookmark_utils_gtk.h" |
40 #include "chrome/browser/ui/gtk/browser_window_gtk.h" | |
40 #include "chrome/browser/ui/gtk/content_setting_bubble_gtk.h" | 41 #include "chrome/browser/ui/gtk/content_setting_bubble_gtk.h" |
41 #include "chrome/browser/ui/gtk/extensions/extension_popup_gtk.h" | 42 #include "chrome/browser/ui/gtk/extensions/extension_popup_gtk.h" |
42 #include "chrome/browser/ui/gtk/first_run_bubble.h" | 43 #include "chrome/browser/ui/gtk/first_run_bubble.h" |
43 #include "chrome/browser/ui/gtk/gtk_util.h" | 44 #include "chrome/browser/ui/gtk/gtk_util.h" |
44 #include "chrome/browser/ui/gtk/nine_box.h" | 45 #include "chrome/browser/ui/gtk/nine_box.h" |
45 #include "chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h" | 46 #include "chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.h" |
46 #include "chrome/browser/ui/gtk/rounded_window.h" | 47 #include "chrome/browser/ui/gtk/rounded_window.h" |
47 #include "chrome/browser/ui/gtk/theme_service_gtk.h" | 48 #include "chrome/browser/ui/gtk/theme_service_gtk.h" |
48 #include "chrome/browser/ui/gtk/view_id_util.h" | 49 #include "chrome/browser/ui/gtk/view_id_util.h" |
49 #include "chrome/browser/ui/omnibox/location_bar_util.h" | 50 #include "chrome/browser/ui/omnibox/location_bar_util.h" |
50 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 51 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
51 #include "chrome/common/chrome_notification_types.h" | 52 #include "chrome/common/chrome_notification_types.h" |
52 #include "chrome/common/extensions/extension.h" | 53 #include "chrome/common/extensions/extension.h" |
53 #include "chrome/common/extensions/extension_action.h" | 54 #include "chrome/common/extensions/extension_action.h" |
54 #include "chrome/common/extensions/extension_resource.h" | 55 #include "chrome/common/extensions/extension_resource.h" |
55 #include "chrome/common/pref_names.h" | 56 #include "chrome/common/pref_names.h" |
56 #include "content/public/browser/navigation_entry.h" | 57 #include "content/public/browser/navigation_entry.h" |
57 #include "content/public/browser/notification_service.h" | 58 #include "content/public/browser/notification_service.h" |
58 #include "content/public/browser/web_contents.h" | 59 #include "content/public/browser/web_contents.h" |
59 #include "grit/generated_resources.h" | 60 #include "grit/generated_resources.h" |
60 #include "grit/theme_resources.h" | 61 #include "grit/theme_resources.h" |
61 #include "grit/theme_resources_standard.h" | 62 #include "grit/theme_resources_standard.h" |
62 #include "net/base/net_util.h" | 63 #include "net/base/net_util.h" |
64 #include "ui/base/accelerators/accelerator_gtk.h" | |
63 #include "ui/base/dragdrop/gtk_dnd_util.h" | 65 #include "ui/base/dragdrop/gtk_dnd_util.h" |
64 #include "ui/base/gtk/gtk_hig_constants.h" | 66 #include "ui/base/gtk/gtk_hig_constants.h" |
65 #include "ui/base/l10n/l10n_util.h" | 67 #include "ui/base/l10n/l10n_util.h" |
66 #include "ui/base/resource/resource_bundle.h" | 68 #include "ui/base/resource/resource_bundle.h" |
67 #include "ui/gfx/canvas_skia_paint.h" | 69 #include "ui/gfx/canvas_skia_paint.h" |
68 #include "ui/gfx/font.h" | 70 #include "ui/gfx/font.h" |
69 #include "ui/gfx/gtk_util.h" | 71 #include "ui/gfx/gtk_util.h" |
70 #include "ui/gfx/image/image.h" | 72 #include "ui/gfx/image/image.h" |
71 #include "webkit/glue/window_open_disposition.h" | 73 #include "webkit/glue/window_open_disposition.h" |
72 | 74 |
(...skipping 591 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
664 page_action_views_[i]->widget(), FALSE, FALSE, 0); | 666 page_action_views_[i]->widget(), FALSE, FALSE, 0); |
665 } | 667 } |
666 content::NotificationService::current()->Notify( | 668 content::NotificationService::current()->Notify( |
667 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED, | 669 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED, |
668 content::Source<LocationBar>(this), | 670 content::Source<LocationBar>(this), |
669 content::NotificationService::NoDetails()); | 671 content::NotificationService::NoDetails()); |
670 } | 672 } |
671 | 673 |
672 WebContents* contents = GetWebContents(); | 674 WebContents* contents = GetWebContents(); |
673 if (!page_action_views_.empty() && contents) { | 675 if (!page_action_views_.empty() && contents) { |
674 GURL url = GURL(toolbar_model_->GetText()); | 676 GURL url = browser()->GetSelectedWebContents()->GetURL(); |
675 | 677 |
676 for (size_t i = 0; i < page_action_views_.size(); i++) { | 678 for (size_t i = 0; i < page_action_views_.size(); i++) { |
677 page_action_views_[i]->UpdateVisibility( | 679 page_action_views_[i]->UpdateVisibility( |
678 toolbar_model_->input_in_progress() ? NULL : contents, url); | 680 toolbar_model_->input_in_progress() ? NULL : contents, url); |
679 } | 681 } |
680 } | 682 } |
681 | 683 |
682 // If there are no visible page actions, hide the hbox too, so that it does | 684 // If there are no visible page actions, hide the hbox too, so that it does |
683 // not affect the padding in the location bar. | 685 // not affect the padding in the location bar. |
684 gtk_widget_set_visible(page_action_hbox_.get(), | 686 gtk_widget_set_visible(page_action_hbox_.get(), |
(...skipping 741 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1426 //////////////////////////////////////////////////////////////////////////////// | 1428 //////////////////////////////////////////////////////////////////////////////// |
1427 // LocationBarViewGtk::PageActionViewGtk | 1429 // LocationBarViewGtk::PageActionViewGtk |
1428 | 1430 |
1429 LocationBarViewGtk::PageActionViewGtk::PageActionViewGtk( | 1431 LocationBarViewGtk::PageActionViewGtk::PageActionViewGtk( |
1430 LocationBarViewGtk* owner, | 1432 LocationBarViewGtk* owner, |
1431 ExtensionAction* page_action) | 1433 ExtensionAction* page_action) |
1432 : owner_(NULL), | 1434 : owner_(NULL), |
1433 page_action_(page_action), | 1435 page_action_(page_action), |
1434 last_icon_pixbuf_(NULL), | 1436 last_icon_pixbuf_(NULL), |
1435 tracker_(this), | 1437 tracker_(this), |
1438 current_tab_id_(-1), | |
1439 window_(NULL), | |
1440 accel_group_(NULL), | |
1436 preview_enabled_(false) { | 1441 preview_enabled_(false) { |
1437 event_box_.Own(gtk_event_box_new()); | 1442 event_box_.Own(gtk_event_box_new()); |
1438 gtk_widget_set_size_request(event_box_.get(), | 1443 gtk_widget_set_size_request(event_box_.get(), |
1439 Extension::kPageActionIconMaxSize, | 1444 Extension::kPageActionIconMaxSize, |
1440 Extension::kPageActionIconMaxSize); | 1445 Extension::kPageActionIconMaxSize); |
1441 | 1446 |
1442 // Make the event box not visible so it does not paint a background. | 1447 // Make the event box not visible so it does not paint a background. |
1443 gtk_event_box_set_visible_window(GTK_EVENT_BOX(event_box_.get()), FALSE); | 1448 gtk_event_box_set_visible_window(GTK_EVENT_BOX(event_box_.get()), FALSE); |
1444 g_signal_connect(event_box_.get(), "button-press-event", | 1449 g_signal_connect(event_box_.get(), "button-press-event", |
1445 G_CALLBACK(&OnButtonPressedThunk), this); | 1450 G_CALLBACK(&OnButtonPressedThunk), this); |
1446 g_signal_connect_after(event_box_.get(), "expose-event", | 1451 g_signal_connect_after(event_box_.get(), "expose-event", |
1447 G_CALLBACK(OnExposeEventThunk), this); | 1452 G_CALLBACK(OnExposeEventThunk), this); |
1453 g_signal_connect(event_box_.get(), "realize", | |
1454 G_CALLBACK(OnRealizeThunk), this); | |
1448 | 1455 |
1449 image_.Own(gtk_image_new()); | 1456 image_.Own(gtk_image_new()); |
1450 gtk_container_add(GTK_CONTAINER(event_box_.get()), image_.get()); | 1457 gtk_container_add(GTK_CONTAINER(event_box_.get()), image_.get()); |
1451 | 1458 |
1452 const Extension* extension = owner->browser()->profile()-> | 1459 const Extension* extension = owner->browser()->profile()-> |
1453 GetExtensionService()->GetExtensionById(page_action->extension_id(), | 1460 GetExtensionService()->GetExtensionById(page_action->extension_id(), |
1454 false); | 1461 false); |
1455 DCHECK(extension); | 1462 DCHECK(extension); |
1456 | 1463 |
1457 // Load all the icons declared in the manifest. This is the contents of the | 1464 // Load all the icons declared in the manifest. This is the contents of the |
1458 // icons array, plus the default_icon property, if any. | 1465 // icons array, plus the default_icon property, if any. |
1459 std::vector<std::string> icon_paths(*page_action->icon_paths()); | 1466 std::vector<std::string> icon_paths(*page_action->icon_paths()); |
1460 if (!page_action_->default_icon_path().empty()) | 1467 if (!page_action_->default_icon_path().empty()) |
1461 icon_paths.push_back(page_action_->default_icon_path()); | 1468 icon_paths.push_back(page_action_->default_icon_path()); |
1462 | 1469 |
1463 for (std::vector<std::string>::iterator iter = icon_paths.begin(); | 1470 for (std::vector<std::string>::iterator iter = icon_paths.begin(); |
1464 iter != icon_paths.end(); ++iter) { | 1471 iter != icon_paths.end(); ++iter) { |
1465 tracker_.LoadImage(extension, extension->GetResource(*iter), | 1472 tracker_.LoadImage(extension, extension->GetResource(*iter), |
1466 gfx::Size(Extension::kPageActionIconMaxSize, | 1473 gfx::Size(Extension::kPageActionIconMaxSize, |
1467 Extension::kPageActionIconMaxSize), | 1474 Extension::kPageActionIconMaxSize), |
1468 ImageLoadingTracker::DONT_CACHE); | 1475 ImageLoadingTracker::DONT_CACHE); |
1469 } | 1476 } |
1470 | 1477 |
1471 // We set the owner last of all so that we can determine whether we are in | 1478 // We set the owner last of all so that we can determine whether we are in |
1472 // the process of initializing this class or not. | 1479 // the process of initializing this class or not. |
1473 owner_ = owner; | 1480 owner_ = owner; |
1474 } | 1481 } |
1475 | 1482 |
1476 LocationBarViewGtk::PageActionViewGtk::~PageActionViewGtk() { | 1483 LocationBarViewGtk::PageActionViewGtk::~PageActionViewGtk() { |
1484 DisconnectPageActionAccelerator(); | |
1485 | |
1477 image_.Destroy(); | 1486 image_.Destroy(); |
1478 event_box_.Destroy(); | 1487 event_box_.Destroy(); |
1479 for (PixbufMap::iterator iter = pixbufs_.begin(); iter != pixbufs_.end(); | 1488 for (PixbufMap::iterator iter = pixbufs_.begin(); iter != pixbufs_.end(); |
1480 ++iter) { | 1489 ++iter) { |
1481 g_object_unref(iter->second); | 1490 g_object_unref(iter->second); |
1482 } | 1491 } |
1483 if (last_icon_pixbuf_) | 1492 if (last_icon_pixbuf_) |
1484 g_object_unref(last_icon_pixbuf_); | 1493 g_object_unref(last_icon_pixbuf_); |
1485 } | 1494 } |
1486 | 1495 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1583 if (owner_) | 1592 if (owner_) |
1584 owner_->UpdatePageActions(); | 1593 owner_->UpdatePageActions(); |
1585 } | 1594 } |
1586 | 1595 |
1587 void LocationBarViewGtk::PageActionViewGtk::TestActivatePageAction() { | 1596 void LocationBarViewGtk::PageActionViewGtk::TestActivatePageAction() { |
1588 GdkEventButton event = {}; | 1597 GdkEventButton event = {}; |
1589 event.button = 1; | 1598 event.button = 1; |
1590 OnButtonPressed(widget(), &event); | 1599 OnButtonPressed(widget(), &event); |
1591 } | 1600 } |
1592 | 1601 |
1602 void LocationBarViewGtk::PageActionViewGtk::Observe( | |
1603 int type, | |
1604 const content::NotificationSource& source, | |
1605 const content::NotificationDetails& details) { | |
1606 DCHECK(type == chrome::NOTIFICATION_WINDOW_CLOSED); | |
1607 DisconnectPageActionAccelerator(); | |
1608 } | |
1609 | |
1593 void LocationBarViewGtk::PageActionViewGtk::InspectPopup( | 1610 void LocationBarViewGtk::PageActionViewGtk::InspectPopup( |
1594 ExtensionAction* action) { | 1611 ExtensionAction* action) { |
1595 ShowPopup(true); | 1612 ShowPopup(true); |
1596 } | 1613 } |
1597 | 1614 |
1598 bool LocationBarViewGtk::PageActionViewGtk::ShowPopup(bool devtools) { | 1615 bool LocationBarViewGtk::PageActionViewGtk::ShowPopup(bool devtools) { |
1599 if (!page_action_->HasPopup(current_tab_id_)) | 1616 if (!page_action_->HasPopup(current_tab_id_)) |
1600 return false; | 1617 return false; |
1601 | 1618 |
1602 ExtensionPopupGtk::Show( | 1619 ExtensionPopupGtk::Show( |
1603 page_action_->GetPopupUrl(current_tab_id_), | 1620 page_action_->GetPopupUrl(current_tab_id_), |
1604 owner_->browser_, | 1621 owner_->browser_, |
1605 event_box_.get(), | 1622 event_box_.get(), |
1606 devtools); | 1623 devtools); |
1607 return true; | 1624 return true; |
1608 } | 1625 } |
1609 | 1626 |
1627 void LocationBarViewGtk::PageActionViewGtk::ConnectPageActionAccelerator() { | |
1628 const Extension* extension = owner_->browser()->profile()-> | |
1629 GetExtensionService()->GetExtensionById(page_action_->extension_id(), | |
Matt Perry
2012/03/15 19:31:24
Use extensions()->GetByID() instead. We're trying
| |
1630 false); | |
1631 window_ = owner_->browser()->window()->GetNativeHandle(); | |
1632 | |
1633 // Iterate through all the keybindings and see if one is assigned to the | |
1634 // pageAction. | |
1635 const std::vector<Extension::ExtensionKeybinding>& commands = | |
1636 extension->keybindings(); | |
1637 for (size_t i = 0; i < commands.size(); ++i) { | |
1638 if (commands[i].command_name() != | |
1639 extension_manifest_values::kPageActionKeybindingEvent) | |
1640 continue; | |
1641 | |
1642 // Found the browser action shortcut command, register it. | |
1643 keybinding_.reset(new ui::AcceleratorGtk( | |
1644 commands[i].accelerator().key_code(), | |
1645 commands[i].accelerator().IsShiftDown(), | |
1646 commands[i].accelerator().IsCtrlDown(), | |
1647 commands[i].accelerator().IsAltDown())); | |
1648 | |
1649 accel_group_ = gtk_accel_group_new(); | |
1650 gtk_window_add_accel_group(window_, accel_group_); | |
1651 | |
1652 gtk_accel_group_connect( | |
1653 accel_group_, | |
1654 keybinding_.get()->GetGdkKeyCode(), | |
1655 keybinding_.get()->gdk_modifier_type(), | |
Matt Perry
2012/03/15 19:31:24
nit: can skip the .get()
| |
1656 GtkAccelFlags(0), | |
1657 g_cclosure_new(G_CALLBACK(OnGtkAccelerator), this, NULL)); | |
1658 | |
1659 // Since we've added an accelerator, we'll need to unregister it before | |
1660 // the window is closed, so we listen for the window being closed. | |
1661 registrar_.Add(this, | |
1662 chrome::NOTIFICATION_WINDOW_CLOSED, | |
1663 content::Source<GtkWindow>(window_)); | |
1664 break; | |
1665 } | |
1666 } | |
1667 | |
1668 void LocationBarViewGtk::PageActionViewGtk::DisconnectPageActionAccelerator() { | |
1669 if (accel_group_) { | |
1670 gtk_accel_group_disconnect_key( | |
1671 accel_group_, | |
1672 keybinding_.get()->GetGdkKeyCode(), | |
1673 static_cast<GdkModifierType>(keybinding_.get()->modifiers())); | |
1674 gtk_window_remove_accel_group(window_, accel_group_); | |
1675 g_object_unref(accel_group_); | |
1676 accel_group_ = NULL; | |
1677 keybinding_.reset(NULL); | |
1678 } | |
1679 } | |
1680 | |
1610 gboolean LocationBarViewGtk::PageActionViewGtk::OnButtonPressed( | 1681 gboolean LocationBarViewGtk::PageActionViewGtk::OnButtonPressed( |
1611 GtkWidget* sender, | 1682 GtkWidget* sender, |
1612 GdkEventButton* event) { | 1683 GdkEventButton* event) { |
1613 Profile* profile = owner_->browser()->profile(); | 1684 Profile* profile = owner_->browser()->profile(); |
1614 if (event->button != 3) { | 1685 if (event->button != 3) { |
1615 if (!ShowPopup(false)) { | 1686 if (!ShowPopup(false)) { |
1616 ExtensionService* service = profile->GetExtensionService(); | 1687 ExtensionService* service = profile->GetExtensionService(); |
1617 service->browser_event_router()->PageActionExecuted(profile, | 1688 service->browser_event_router()->PageActionExecuted(profile, |
1618 page_action_->extension_id(), page_action_->id(), current_tab_id_, | 1689 page_action_->extension_id(), page_action_->id(), current_tab_id_, |
1619 current_url_.spec(), event->button); | 1690 current_url_.spec(), event->button); |
(...skipping 29 matching lines...) Expand all Loading... | |
1649 std::string badge_text = page_action_->GetBadgeText(tab_id); | 1720 std::string badge_text = page_action_->GetBadgeText(tab_id); |
1650 if (badge_text.empty()) | 1721 if (badge_text.empty()) |
1651 return FALSE; | 1722 return FALSE; |
1652 | 1723 |
1653 gfx::CanvasSkiaPaint canvas(event, false); | 1724 gfx::CanvasSkiaPaint canvas(event, false); |
1654 GtkAllocation allocation; | 1725 GtkAllocation allocation; |
1655 gtk_widget_get_allocation(widget, &allocation); | 1726 gtk_widget_get_allocation(widget, &allocation); |
1656 page_action_->PaintBadge(&canvas, gfx::Rect(allocation), tab_id); | 1727 page_action_->PaintBadge(&canvas, gfx::Rect(allocation), tab_id); |
1657 return FALSE; | 1728 return FALSE; |
1658 } | 1729 } |
1730 | |
1731 void LocationBarViewGtk::PageActionViewGtk::OnRealize(GtkWidget* widget) { | |
1732 ConnectPageActionAccelerator(); | |
1733 } | |
1734 | |
1735 // static | |
1736 gboolean LocationBarViewGtk::PageActionViewGtk::OnGtkAccelerator( | |
1737 GtkAccelGroup* accel_group, | |
1738 GObject* acceleratable, | |
1739 guint keyval, | |
1740 GdkModifierType modifier, | |
1741 void* user_data) { | |
1742 PageActionViewGtk* view = static_cast<PageActionViewGtk*>(user_data); | |
1743 if (!gtk_widget_get_visible(view->widget())) | |
1744 return FALSE; | |
1745 | |
1746 GdkEventButton event = {}; | |
1747 event.button = 1; | |
1748 return view->OnButtonPressed(view->widget(), &event); | |
1749 } | |
OLD | NEW |