OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/gtk/extension_popup_gtk.h" | 5 #include "chrome/browser/gtk/extension_popup_gtk.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/message_loop.h" |
10 #include "chrome/browser/browser.h" | 11 #include "chrome/browser/browser.h" |
11 #include "chrome/browser/browser_window.h" | 12 #include "chrome/browser/browser_window.h" |
| 13 #include "chrome/browser/debugger/devtools_manager.h" |
12 #include "chrome/browser/profile.h" | 14 #include "chrome/browser/profile.h" |
13 #include "chrome/browser/extensions/extension_host.h" | 15 #include "chrome/browser/extensions/extension_host.h" |
14 #include "chrome/browser/extensions/extension_process_manager.h" | 16 #include "chrome/browser/extensions/extension_process_manager.h" |
15 #include "chrome/browser/gtk/gtk_theme_provider.h" | 17 #include "chrome/browser/gtk/gtk_theme_provider.h" |
16 #include "chrome/common/notification_service.h" | 18 #include "chrome/common/notification_service.h" |
17 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
18 | 20 |
19 ExtensionPopupGtk* ExtensionPopupGtk::current_extension_popup_ = NULL; | 21 ExtensionPopupGtk* ExtensionPopupGtk::current_extension_popup_ = NULL; |
20 | 22 |
21 ExtensionPopupGtk::ExtensionPopupGtk(Browser* browser, | 23 ExtensionPopupGtk::ExtensionPopupGtk(Browser* browser, |
22 ExtensionHost* host, | 24 ExtensionHost* host, |
23 const gfx::Rect& relative_to) | 25 const gfx::Rect& relative_to, |
| 26 bool inspect) |
24 : browser_(browser), | 27 : browser_(browser), |
25 bubble_(NULL), | 28 bubble_(NULL), |
26 host_(host), | 29 host_(host), |
27 relative_to_(relative_to) { | 30 relative_to_(relative_to), |
| 31 being_inspected_(inspect), |
| 32 method_factory_(this) { |
28 // If the host had somehow finished loading, then we'd miss the notification | 33 // If the host had somehow finished loading, then we'd miss the notification |
29 // and not show. This seems to happen in single-process mode. | 34 // and not show. This seems to happen in single-process mode. |
30 if (host->did_stop_loading()) { | 35 if (host->did_stop_loading()) { |
31 ShowPopup(); | 36 ShowPopup(); |
32 } else { | 37 } else { |
33 registrar_.Add(this, NotificationType::EXTENSION_HOST_DID_STOP_LOADING, | 38 registrar_.Add(this, NotificationType::EXTENSION_HOST_DID_STOP_LOADING, |
34 Source<Profile>(host->profile())); | 39 Source<Profile>(host->profile())); |
35 } | 40 } |
36 | 41 |
37 registrar_.Add(this, NotificationType::EXTENSION_HOST_VIEW_SHOULD_CLOSE, | 42 registrar_.Add(this, NotificationType::EXTENSION_HOST_VIEW_SHOULD_CLOSE, |
38 Source<Profile>(host->profile())); | 43 Source<Profile>(host->profile())); |
39 } | 44 } |
40 | 45 |
41 ExtensionPopupGtk::~ExtensionPopupGtk() { | 46 ExtensionPopupGtk::~ExtensionPopupGtk() { |
42 } | 47 } |
43 | 48 |
44 void ExtensionPopupGtk::Observe(NotificationType type, | 49 void ExtensionPopupGtk::Observe(NotificationType type, |
45 const NotificationSource& source, | 50 const NotificationSource& source, |
46 const NotificationDetails& details) { | 51 const NotificationDetails& details) { |
47 if (Details<ExtensionHost>(host_.get()) != details) | 52 switch (type.value) { |
48 return; | 53 case NotificationType::EXTENSION_HOST_DID_STOP_LOADING: |
| 54 if (Details<ExtensionHost>(host_.get()) == details) |
| 55 ShowPopup(); |
| 56 break; |
| 57 case NotificationType::EXTENSION_HOST_VIEW_SHOULD_CLOSE: |
| 58 if (Details<ExtensionHost>(host_.get()) == details) |
| 59 DestroyPopup(); |
| 60 break; |
| 61 case NotificationType::DEVTOOLS_WINDOW_CLOSING: |
| 62 // Make sure its the devtools window that inspecting our popup. |
| 63 if (Details<RenderViewHost>(host_->render_view_host()) != details) |
| 64 break; |
49 | 65 |
50 if (type == NotificationType::EXTENSION_HOST_DID_STOP_LOADING) { | 66 // If the devtools window is closing, we post a task to ourselves to |
51 ShowPopup(); | 67 // close the popup. This gives the devtools window a chance to finish |
52 } else if (type == NotificationType::EXTENSION_HOST_VIEW_SHOULD_CLOSE) { | 68 // detaching from the inspected RenderViewHost. |
53 DestroyPopup(); | 69 MessageLoop::current()->PostTask(FROM_HERE, |
54 } else { | 70 method_factory_.NewRunnableMethod(&ExtensionPopupGtk::DestroyPopup)); |
55 NOTREACHED() << "Received unexpected notification"; | 71 break; |
| 72 default: |
| 73 NOTREACHED() << "Received unexpected notification"; |
56 } | 74 } |
57 } | 75 } |
58 | 76 |
59 void ExtensionPopupGtk::ShowPopup() { | 77 void ExtensionPopupGtk::ShowPopup() { |
60 if (bubble_) { | 78 if (bubble_) { |
61 NOTREACHED(); | 79 NOTREACHED(); |
62 return; | 80 return; |
63 } | 81 } |
64 | 82 |
| 83 if (being_inspected_) { |
| 84 DevToolsManager::GetInstance()->OpenDevToolsWindow( |
| 85 host_->render_view_host()); |
| 86 // Listen for the the devtools window closing. |
| 87 registrar_.Add(this, NotificationType::DEVTOOLS_WINDOW_CLOSING, |
| 88 Source<Profile>(host_->profile())); |
| 89 } |
| 90 |
65 // Only one instance should be showing at a time. Get rid of the old one, if | 91 // Only one instance should be showing at a time. Get rid of the old one, if |
66 // any. Typically, |current_extension_popup_| will be NULL, but it can be | 92 // any. Typically, |current_extension_popup_| will be NULL, but it can be |
67 // non-NULL if a browser action button is clicked while another extension | 93 // non-NULL if a browser action button is clicked while another extension |
68 // popup's extension host is still loading. | 94 // popup's extension host is still loading. |
69 if (current_extension_popup_) | 95 if (current_extension_popup_) |
70 current_extension_popup_->DestroyPopup(); | 96 current_extension_popup_->DestroyPopup(); |
71 current_extension_popup_ = this; | 97 current_extension_popup_ = this; |
72 | 98 |
73 // We'll be in the upper-right corner of the window for LTR languages, so we | 99 // We'll be in the upper-right corner of the window for LTR languages, so we |
74 // want to put the arrow at the upper-right corner of the bubble to match the | 100 // want to put the arrow at the upper-right corner of the bubble to match the |
75 // page and app menus. | 101 // page and app menus. |
76 InfoBubbleGtk::ArrowLocationGtk arrow_location = | 102 InfoBubbleGtk::ArrowLocationGtk arrow_location = |
77 !base::i18n::IsRTL() ? | 103 !base::i18n::IsRTL() ? |
78 InfoBubbleGtk::ARROW_LOCATION_TOP_RIGHT : | 104 InfoBubbleGtk::ARROW_LOCATION_TOP_RIGHT : |
79 InfoBubbleGtk::ARROW_LOCATION_TOP_LEFT; | 105 InfoBubbleGtk::ARROW_LOCATION_TOP_LEFT; |
80 bubble_ = InfoBubbleGtk::Show(browser_->window()->GetNativeHandle(), | 106 bubble_ = InfoBubbleGtk::Show(browser_->window()->GetNativeHandle(), |
81 relative_to_, | 107 relative_to_, |
82 host_->view()->native_view(), | 108 host_->view()->native_view(), |
83 arrow_location, | 109 arrow_location, |
84 false, | 110 false, // match_system_theme |
| 111 !being_inspected_, // grab_input |
85 GtkThemeProvider::GetFrom(browser_->profile()), | 112 GtkThemeProvider::GetFrom(browser_->profile()), |
86 this); | 113 this); |
87 } | 114 } |
88 | 115 |
89 bool ExtensionPopupGtk::DestroyPopup() { | 116 bool ExtensionPopupGtk::DestroyPopup() { |
90 if (!bubble_) { | 117 if (!bubble_) { |
91 NOTREACHED(); | 118 NOTREACHED(); |
92 return false; | 119 return false; |
93 } | 120 } |
94 | 121 |
95 bubble_->Close(); | 122 bubble_->Close(); |
96 return true; | 123 return true; |
97 } | 124 } |
98 | 125 |
99 void ExtensionPopupGtk::InfoBubbleClosing(InfoBubbleGtk* bubble, | 126 void ExtensionPopupGtk::InfoBubbleClosing(InfoBubbleGtk* bubble, |
100 bool closed_by_escape) { | 127 bool closed_by_escape) { |
101 current_extension_popup_ = NULL; | 128 current_extension_popup_ = NULL; |
102 delete this; | 129 delete this; |
103 } | 130 } |
104 | 131 |
105 // static | 132 // static |
106 void ExtensionPopupGtk::Show(const GURL& url, Browser* browser, | 133 void ExtensionPopupGtk::Show(const GURL& url, Browser* browser, |
107 const gfx::Rect& relative_to) { | 134 const gfx::Rect& relative_to, bool inspect) { |
108 ExtensionProcessManager* manager = | 135 ExtensionProcessManager* manager = |
109 browser->profile()->GetExtensionProcessManager(); | 136 browser->profile()->GetExtensionProcessManager(); |
110 DCHECK(manager); | 137 DCHECK(manager); |
111 if (!manager) | 138 if (!manager) |
112 return; | 139 return; |
113 | 140 |
114 ExtensionHost* host = manager->CreatePopup(url, browser); | 141 ExtensionHost* host = manager->CreatePopup(url, browser); |
115 // This object will delete itself when the info bubble is closed. | 142 // This object will delete itself when the info bubble is closed. |
116 new ExtensionPopupGtk(browser, host, relative_to); | 143 new ExtensionPopupGtk(browser, host, relative_to, inspect); |
117 } | 144 } |
118 | 145 |
119 gfx::Rect ExtensionPopupGtk::GetViewBounds() { | 146 gfx::Rect ExtensionPopupGtk::GetViewBounds() { |
120 return gfx::Rect(host_->view()->native_view()->allocation); | 147 return gfx::Rect(host_->view()->native_view()->allocation); |
121 } | 148 } |
OLD | NEW |