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/extensions/extension_popup.h" | 5 #include "chrome/browser/ui/views/extensions/extension_popup.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
9 #include "chrome/browser/devtools/devtools_window.h" | 9 #include "chrome/browser/devtools/devtools_window.h" |
10 #include "chrome/browser/extensions/extension_view_host.h" | 10 #include "chrome/browser/extensions/extension_view_host.h" |
11 #include "chrome/browser/extensions/extension_view_host_factory.h" | |
12 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
14 #include "content/public/browser/devtools_agent_host.h" | 13 #include "content/public/browser/devtools_agent_host.h" |
15 #include "content/public/browser/notification_details.h" | 14 #include "content/public/browser/notification_details.h" |
16 #include "content/public/browser/notification_source.h" | 15 #include "content/public/browser/notification_source.h" |
17 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
18 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
19 #include "ui/gfx/geometry/insets.h" | 18 #include "ui/gfx/geometry/insets.h" |
20 #include "ui/views/layout/fill_layout.h" | 19 #include "ui/views/layout/fill_layout.h" |
21 #include "ui/views/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 inspect_with_devtools_ = show_action == SHOW_AND_INSPECT; | 59 inspect_with_devtools_ = show_action == SHOW_AND_INSPECT; |
61 // Adjust the margin so that contents fit better. | 60 // Adjust the margin so that contents fit better. |
62 const int margin = views::BubbleBorder::GetCornerRadius() / 2; | 61 const int margin = views::BubbleBorder::GetCornerRadius() / 2; |
63 set_margins(gfx::Insets(margin, margin, margin, margin)); | 62 set_margins(gfx::Insets(margin, margin, margin, margin)); |
64 SetLayoutManager(new views::FillLayout()); | 63 SetLayoutManager(new views::FillLayout()); |
65 AddChildView(GetExtensionView(host)); | 64 AddChildView(GetExtensionView(host)); |
66 GetExtensionView(host)->set_container(this); | 65 GetExtensionView(host)->set_container(this); |
67 // ExtensionPopup closes itself on very specific de-activation conditions. | 66 // ExtensionPopup closes itself on very specific de-activation conditions. |
68 set_close_on_deactivate(false); | 67 set_close_on_deactivate(false); |
69 | 68 |
70 // Wait to show the popup until the contained host finishes loading. | |
71 registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, | |
72 content::Source<content::WebContents>(host->host_contents())); | |
73 | 69 |
74 // Listen for the containing view calling window.close(); | 70 // Listen for the containing view calling window.close(); |
75 registrar_.Add( | 71 registrar_.Add( |
76 this, | 72 this, |
77 extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, | 73 extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, |
78 content::Source<content::BrowserContext>(host->browser_context())); | 74 content::Source<content::BrowserContext>(host->browser_context())); |
79 content::DevToolsAgentHost::AddAgentStateCallback(devtools_callback_); | 75 content::DevToolsAgentHost::AddAgentStateCallback(devtools_callback_); |
80 | 76 |
81 GetExtensionView(host)->GetBrowser()->tab_strip_model()->AddObserver(this); | 77 GetExtensionView(host)->GetBrowser()->tab_strip_model()->AddObserver(this); |
| 78 |
| 79 // If the host had somehow finished loading, then we'd miss the notification |
| 80 // and not show. This seems to happen in single-process mode. |
| 81 if (host_->has_loaded_once()) { |
| 82 ShowBubble(); |
| 83 } else { |
| 84 // Wait to show the popup until the contained host finishes loading. |
| 85 registrar_.Add(this, |
| 86 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 87 content::Source<content::WebContents>( |
| 88 host_->host_contents())); |
| 89 } |
82 } | 90 } |
83 | 91 |
84 ExtensionPopup::~ExtensionPopup() { | 92 ExtensionPopup::~ExtensionPopup() { |
85 content::DevToolsAgentHost::RemoveAgentStateCallback(devtools_callback_); | 93 content::DevToolsAgentHost::RemoveAgentStateCallback(devtools_callback_); |
86 | 94 |
87 GetExtensionView( | 95 GetExtensionView( |
88 host_.get())->GetBrowser()->tab_strip_model()->RemoveObserver(this); | 96 host_.get())->GetBrowser()->tab_strip_model()->RemoveObserver(this); |
89 } | 97 } |
90 | 98 |
91 void ExtensionPopup::Observe(int type, | 99 void ExtensionPopup::Observe(int type, |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 // TODO(msw): Find any remaining crashes related to http://crbug.com/327776 | 171 // TODO(msw): Find any remaining crashes related to http://crbug.com/327776 |
164 // No calls are expected if the widget isn't initialized or no longer exists. | 172 // No calls are expected if the widget isn't initialized or no longer exists. |
165 CHECK(widget_initialized_); | 173 CHECK(widget_initialized_); |
166 CHECK(GetWidget()); | 174 CHECK(GetWidget()); |
167 | 175 |
168 if (!inspect_with_devtools_) | 176 if (!inspect_with_devtools_) |
169 GetWidget()->Close(); | 177 GetWidget()->Close(); |
170 } | 178 } |
171 | 179 |
172 // static | 180 // static |
173 ExtensionPopup* ExtensionPopup::ShowPopup(const GURL& url, | 181 ExtensionPopup* ExtensionPopup::ShowPopup( |
174 Browser* browser, | 182 scoped_ptr<extensions::ExtensionViewHost> host, |
175 views::View* anchor_view, | 183 views::View* anchor_view, |
176 views::BubbleBorder::Arrow arrow, | 184 views::BubbleBorder::Arrow arrow, |
177 ShowAction show_action) { | 185 ShowAction show_action) { |
178 extensions::ExtensionViewHost* host = | 186 return ExtensionPopup::Create( |
179 extensions::ExtensionViewHostFactory::CreatePopupHost(url, browser); | 187 host.release(), anchor_view, arrow, show_action); |
180 auto popup = ExtensionPopup::Create(host, anchor_view, arrow, show_action); | |
181 | |
182 // If the host had somehow finished loading, then we'd miss the notification | |
183 // and not show. This seems to happen in single-process mode. | |
184 if (host->has_loaded_once()) | |
185 popup->ShowBubble(); | |
186 | |
187 return popup; | |
188 } | 188 } |
189 | 189 |
190 void ExtensionPopup::ShowBubble() { | 190 void ExtensionPopup::ShowBubble() { |
191 GetWidget()->Show(); | 191 GetWidget()->Show(); |
192 | 192 |
193 // Focus on the host contents when the bubble is first shown. | 193 // Focus on the host contents when the bubble is first shown. |
194 host()->host_contents()->Focus(); | 194 host()->host_contents()->Focus(); |
195 | 195 |
196 if (inspect_with_devtools_) { | 196 if (inspect_with_devtools_) { |
197 DevToolsWindow::OpenDevToolsWindow(host()->host_contents(), | 197 DevToolsWindow::OpenDevToolsWindow(host()->host_contents(), |
198 DevToolsToggleAction::ShowConsole()); | 198 DevToolsToggleAction::ShowConsole()); |
199 } | 199 } |
200 } | 200 } |
OLD | NEW |