| 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" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 return popup; | 52 return popup; |
| 53 } | 53 } |
| 54 #endif | 54 #endif |
| 55 | 55 |
| 56 ExtensionPopup::ExtensionPopup(extensions::ExtensionViewHost* host, | 56 ExtensionPopup::ExtensionPopup(extensions::ExtensionViewHost* host, |
| 57 views::View* anchor_view, | 57 views::View* anchor_view, |
| 58 views::BubbleBorder::Arrow arrow, | 58 views::BubbleBorder::Arrow arrow, |
| 59 ShowAction show_action) | 59 ShowAction show_action) |
| 60 : BubbleDialogDelegateView(anchor_view, arrow), | 60 : BubbleDialogDelegateView(anchor_view, arrow), |
| 61 host_(host), | 61 host_(host), |
| 62 devtools_callback_(base::Bind( | |
| 63 &ExtensionPopup::OnDevToolsStateChanged, base::Unretained(this))), | |
| 64 widget_initialized_(false) { | 62 widget_initialized_(false) { |
| 65 inspect_with_devtools_ = show_action == SHOW_AND_INSPECT; | 63 inspect_with_devtools_ = show_action == SHOW_AND_INSPECT; |
| 66 set_margins(gfx::Insets(kBubbleMargin)); | 64 set_margins(gfx::Insets(kBubbleMargin)); |
| 67 SetLayoutManager(new views::FillLayout()); | 65 SetLayoutManager(new views::FillLayout()); |
| 68 AddChildView(GetExtensionView(host)); | 66 AddChildView(GetExtensionView(host)); |
| 69 GetExtensionView(host)->set_container(this); | 67 GetExtensionView(host)->set_container(this); |
| 70 // ExtensionPopup closes itself on very specific de-activation conditions. | 68 // ExtensionPopup closes itself on very specific de-activation conditions. |
| 71 set_close_on_deactivate(false); | 69 set_close_on_deactivate(false); |
| 72 | 70 |
| 73 // Listen for the containing view calling window.close(); | 71 // Listen for the containing view calling window.close(); |
| 74 registrar_.Add( | 72 registrar_.Add( |
| 75 this, | 73 this, |
| 76 extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, | 74 extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, |
| 77 content::Source<content::BrowserContext>(host->browser_context())); | 75 content::Source<content::BrowserContext>(host->browser_context())); |
| 78 content::DevToolsAgentHost::AddAgentStateCallback(devtools_callback_); | 76 content::DevToolsAgentHost::AddObserver(this); |
| 79 | 77 |
| 80 GetExtensionView(host)->GetBrowser()->tab_strip_model()->AddObserver(this); | 78 GetExtensionView(host)->GetBrowser()->tab_strip_model()->AddObserver(this); |
| 81 | 79 |
| 82 // If the host had somehow finished loading, then we'd miss the notification | 80 // If the host had somehow finished loading, then we'd miss the notification |
| 83 // and not show. This seems to happen in single-process mode. | 81 // and not show. This seems to happen in single-process mode. |
| 84 if (host_->has_loaded_once()) { | 82 if (host_->has_loaded_once()) { |
| 85 ShowBubble(); | 83 ShowBubble(); |
| 86 } else { | 84 } else { |
| 87 // Wait to show the popup until the contained host finishes loading. | 85 // Wait to show the popup until the contained host finishes loading. |
| 88 registrar_.Add(this, | 86 registrar_.Add(this, |
| 89 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, | 87 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 90 content::Source<content::WebContents>( | 88 content::Source<content::WebContents>( |
| 91 host_->host_contents())); | 89 host_->host_contents())); |
| 92 } | 90 } |
| 93 } | 91 } |
| 94 | 92 |
| 95 ExtensionPopup::~ExtensionPopup() { | 93 ExtensionPopup::~ExtensionPopup() { |
| 96 content::DevToolsAgentHost::RemoveAgentStateCallback(devtools_callback_); | 94 content::DevToolsAgentHost::RemoveObserver(this); |
| 97 | 95 |
| 98 GetExtensionView( | 96 GetExtensionView( |
| 99 host_.get())->GetBrowser()->tab_strip_model()->RemoveObserver(this); | 97 host_.get())->GetBrowser()->tab_strip_model()->RemoveObserver(this); |
| 100 } | 98 } |
| 101 | 99 |
| 102 int ExtensionPopup::GetDialogButtons() const { | 100 int ExtensionPopup::GetDialogButtons() const { |
| 103 return ui::DIALOG_BUTTON_NONE; | 101 return ui::DIALOG_BUTTON_NONE; |
| 104 } | 102 } |
| 105 | 103 |
| 106 void ExtensionPopup::Observe(int type, | 104 void ExtensionPopup::Observe(int type, |
| 107 const content::NotificationSource& source, | 105 const content::NotificationSource& source, |
| 108 const content::NotificationDetails& details) { | 106 const content::NotificationDetails& details) { |
| 109 switch (type) { | 107 switch (type) { |
| 110 case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME: | 108 case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME: |
| 111 DCHECK_EQ(host()->host_contents(), | 109 DCHECK_EQ(host()->host_contents(), |
| 112 content::Source<content::WebContents>(source).ptr()); | 110 content::Source<content::WebContents>(source).ptr()); |
| 113 // Show when the content finishes loading and its width is computed. | 111 // Show when the content finishes loading and its width is computed. |
| 114 ShowBubble(); | 112 ShowBubble(); |
| 115 break; | 113 break; |
| 116 case extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: | 114 case extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: |
| 117 // If we aren't the host of the popup, then disregard the notification. | 115 // If we aren't the host of the popup, then disregard the notification. |
| 118 if (content::Details<extensions::ExtensionHost>(host()) == details) | 116 if (content::Details<extensions::ExtensionHost>(host()) == details) |
| 119 GetWidget()->Close(); | 117 GetWidget()->Close(); |
| 120 break; | 118 break; |
| 121 default: | 119 default: |
| 122 NOTREACHED() << "Received unexpected notification"; | 120 NOTREACHED() << "Received unexpected notification"; |
| 123 } | 121 } |
| 124 } | 122 } |
| 125 | 123 |
| 126 void ExtensionPopup::OnDevToolsStateChanged( | 124 void ExtensionPopup::DevToolsAgentHostAttached( |
| 127 content::DevToolsAgentHost* agent_host, | 125 content::DevToolsAgentHost* agent_host) { |
| 128 bool attached) { | |
| 129 // First check that the devtools are being opened on this popup. | 126 // First check that the devtools are being opened on this popup. |
| 130 if (host()->host_contents() != agent_host->GetWebContents()) | 127 if (host()->host_contents() != agent_host->GetWebContents()) |
| 131 return; | 128 return; |
| 129 // Set inspect_with_devtools_ so the popup will be kept open while |
| 130 // the devtools are open. |
| 131 inspect_with_devtools_ = true; |
| 132 } |
| 132 | 133 |
| 133 if (attached) { | 134 void ExtensionPopup::DevToolsAgentHostDetached( |
| 134 // Set inspect_with_devtools_ so the popup will be kept open while | 135 content::DevToolsAgentHost* agent_host) { |
| 135 // the devtools are open. | 136 if (host()->host_contents() != agent_host->GetWebContents()) |
| 136 inspect_with_devtools_ = true; | 137 return; |
| 137 } else { | 138 // Widget::Close posts a task, which should give the devtools window a |
| 138 // Widget::Close posts a task, which should give the devtools window a | 139 // chance to finish detaching from the inspected RenderViewHost. |
| 139 // chance to finish detaching from the inspected RenderViewHost. | 140 GetWidget()->Close(); |
| 140 GetWidget()->Close(); | |
| 141 } | |
| 142 } | 141 } |
| 143 | 142 |
| 144 void ExtensionPopup::OnExtensionSizeChanged(ExtensionViewViews* view) { | 143 void ExtensionPopup::OnExtensionSizeChanged(ExtensionViewViews* view) { |
| 145 SizeToContents(); | 144 SizeToContents(); |
| 146 } | 145 } |
| 147 | 146 |
| 148 gfx::Size ExtensionPopup::GetPreferredSize() const { | 147 gfx::Size ExtensionPopup::GetPreferredSize() const { |
| 149 // Constrain the size to popup min/max. | 148 // Constrain the size to popup min/max. |
| 150 gfx::Size sz = views::View::GetPreferredSize(); | 149 gfx::Size sz = views::View::GetPreferredSize(); |
| 151 sz.set_width(std::max(kMinWidth, std::min(kMaxWidth, sz.width()))); | 150 sz.set_width(std::max(kMinWidth, std::min(kMaxWidth, sz.width()))); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 GetWidget()->Show(); | 197 GetWidget()->Show(); |
| 199 | 198 |
| 200 // Focus on the host contents when the bubble is first shown. | 199 // Focus on the host contents when the bubble is first shown. |
| 201 host()->host_contents()->Focus(); | 200 host()->host_contents()->Focus(); |
| 202 | 201 |
| 203 if (inspect_with_devtools_) { | 202 if (inspect_with_devtools_) { |
| 204 DevToolsWindow::OpenDevToolsWindow(host()->host_contents(), | 203 DevToolsWindow::OpenDevToolsWindow(host()->host_contents(), |
| 205 DevToolsToggleAction::ShowConsole()); | 204 DevToolsToggleAction::ShowConsole()); |
| 206 } | 205 } |
| 207 } | 206 } |
| OLD | NEW |