extension_dialog.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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 3// found in the LICENSE file. 4 5#include "chrome/browser/ui/views/extensions/extension_dialog.h" 6 7#include "chrome/browser/chrome_notification_types.h" 8#include "chrome/browser/extensions/extension_host.h" 9#include "chrome/browser/extensions/extension_process_manager.h" 10#include "chrome/browser/extensions/extension_system.h" 11#include "chrome/browser/profiles/profile.h" 12#include "chrome/browser/ui/views/constrained_window_views.h" 13#include "chrome/browser/ui/views/extensions/extension_dialog_observer.h" 14#include "content/public/browser/notification_details.h" 15#include "content/public/browser/notification_source.h" 16#include "content/public/browser/render_view_host.h" 17#include "content/public/browser/render_widget_host_view.h" 18#include "content/public/browser/web_contents.h" 19#include "content/public/browser/web_contents_view.h" 20#include "ui/base/base_window.h" 21#include "ui/gfx/screen.h" 22#include "ui/views/background.h" 23#include "ui/views/widget/widget.h" 24#include "url/gurl.h" 25 26using content::WebContents; 27 28ExtensionDialog::ExtensionDialog(extensions::ExtensionHost* host, 29 ExtensionDialogObserver* observer) 30 : extension_host_(host), 31 observer_(observer) { 32 AddRef(); // Balanced in DeleteDelegate(); 33 34 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, 35 content::Source<Profile>(host->profile())); 36 // Listen for the containing view calling window.close(); 37 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, 38 content::Source<Profile>(host->profile())); 39 // Listen for a crash or other termination of the extension process. 40 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED, 41 content::Source<Profile>(host->profile())); 42} 43 44ExtensionDialog::~ExtensionDialog() { 45} 46 47// static 48ExtensionDialog* ExtensionDialog::Show( 49 const GURL& url, 50 ui::BaseWindow* base_window, 51 Profile* profile, 52 WebContents* web_contents, 53 int width, 54 int height, 55 int min_width, 56 int min_height, 57 const string16& title, 58 ExtensionDialogObserver* observer) { 59 extensions::ExtensionHost* host = CreateExtensionHost(url, profile); 60 if (!host) 61 return NULL; 62 // Preferred size must be set before views::Widget::CreateWindowWithParent 63 // is called because CreateWindowWithParent refers the result of CanResize(). 64 host->view()->SetPreferredSize(gfx::Size(min_width, min_height)); 65 host->SetAssociatedWebContents(web_contents); 66 67 CHECK(base_window); 68 ExtensionDialog* dialog = new ExtensionDialog(host, observer); 69 dialog->set_title(title); 70 dialog->InitWindow(base_window, width, height); 71 72 // Show a white background while the extension loads. This is prettier than 73 // flashing a black unfilled window frame. 74 host->view()->set_background( 75 views::Background::CreateSolidBackground(0xFF, 0xFF, 0xFF)); 76 host->view()->SetVisible(true); 77 78 // Ensure the DOM JavaScript can respond immediately to keyboard shortcuts. 79 host->host_contents()->GetView()->Focus(); 80 return dialog; 81} 82 83// static 84extensions::ExtensionHost* ExtensionDialog::CreateExtensionHost( 85 const GURL& url, 86 Profile* profile) { 87 DCHECK(profile); 88 ExtensionProcessManager* manager = 89 extensions::ExtensionSystem::Get(profile)->process_manager(); 90 91 DCHECK(manager); 92 if (!manager) 93 return NULL; 94 return manager->CreateDialogHost(url); 95} 96 97void ExtensionDialog::InitWindow(ui::BaseWindow* base_window, 98 int width, 99 int height) { 100 gfx::NativeWindow parent = base_window->GetNativeWindow(); 101 views::Widget* window = CreateBrowserModalDialogViews(this, parent); 102 103 // Center the window over the browser. 104 gfx::Point center = base_window->GetBounds().CenterPoint(); 105 int x = center.x() - width / 2; 106 int y = center.y() - height / 2; 107 // Ensure the top left and top right of the window are on screen, with 108 // priority given to the top left. 109 gfx::Rect screen_rect = gfx::Screen::GetScreenFor(parent)-> 110 GetDisplayNearestPoint(center).bounds(); 111 gfx::Rect bounds_rect = gfx::Rect(x, y, width, height); 112 bounds_rect.AdjustToFit(screen_rect); 113 window->SetBounds(bounds_rect); 114 115 window->Show(); 116 // TODO(jamescook): Remove redundant call to Activate()? 117 window->Activate(); 118} 119 120void ExtensionDialog::ObserverDestroyed() { 121 observer_ = NULL; 122} 123 124void ExtensionDialog::MaybeFocusRenderView() { 125 views::FocusManager* focus_manager = GetWidget()->GetFocusManager(); 126 DCHECK(focus_manager != NULL); 127 128 // Already there's a focused view, so no need to switch the focus. 129 if (focus_manager->GetFocusedView()) 130 return; 131 132 content::RenderWidgetHostView* view = host()->render_view_host()->GetView(); 133 if (!view) 134 return; 135 136 view->Focus(); 137} 138 139///////////////////////////////////////////////////////////////////////////// 140// views::DialogDelegate overrides. 141 142int ExtensionDialog::GetDialogButtons() const { 143 // The only user, SelectFileDialogExtension, provides its own buttons. 144 return ui::DIALOG_BUTTON_NONE; 145} 146 147bool ExtensionDialog::CanResize() const { 148 // Can resize only if minimum contents size set. 149 return extension_host_->view()->GetPreferredSize() != gfx::Size(); 150} 151 152void ExtensionDialog::SetMinimumContentsSize(int width, int height) { 153 extension_host_->view()->SetPreferredSize(gfx::Size(width, height)); 154} 155 156ui::ModalType ExtensionDialog::GetModalType() const { 157 return ui::MODAL_TYPE_WINDOW; 158} 159 160bool ExtensionDialog::ShouldShowWindowTitle() const { 161 return !window_title_.empty(); 162} 163 164string16 ExtensionDialog::GetWindowTitle() const { 165 return window_title_; 166} 167 168void ExtensionDialog::WindowClosing() { 169 if (observer_) 170 observer_->ExtensionDialogClosing(this); 171} 172 173void ExtensionDialog::DeleteDelegate() { 174 // The window has finished closing. Allow ourself to be deleted. 175 Release(); 176} 177 178views::Widget* ExtensionDialog::GetWidget() { 179 return extension_host_->view()->GetWidget(); 180} 181 182const views::Widget* ExtensionDialog::GetWidget() const { 183 return extension_host_->view()->GetWidget(); 184} 185 186views::View* ExtensionDialog::GetContentsView() { 187 return extension_host_->view(); 188} 189 190bool ExtensionDialog::UseNewStyleForThisDialog() const { 191 return false; 192} 193 194///////////////////////////////////////////////////////////////////////////// 195// content::NotificationObserver overrides. 196 197void ExtensionDialog::Observe(int type, 198 const content::NotificationSource& source, 199 const content::NotificationDetails& details) { 200 switch (type) { 201 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: 202 // Avoid potential overdraw by removing the temporary background after 203 // the extension finishes loading. 204 extension_host_->view()->set_background(NULL); 205 // The render view is created during the LoadURL(), so we should 206 // set the focus to the view if nobody else takes the focus. 207 if (content::Details<extensions::ExtensionHost>(host()) == details) 208 MaybeFocusRenderView(); 209 break; 210 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: 211 // If we aren't the host of the popup, then disregard the notification. 212 if (content::Details<extensions::ExtensionHost>(host()) != details) 213 return; 214 GetWidget()->Close(); 215 break; 216 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: 217 if (content::Details<extensions::ExtensionHost>(host()) != details) 218 return; 219 if (observer_) 220 observer_->ExtensionTerminated(this); 221 break; 222 default: 223 NOTREACHED() << L"Received unexpected notification"; 224 break; 225 } 226} 227