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/webui/constrained_web_dialog_delegate_base.h" 6 7 #include <string> 8 9 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/renderer_preferences_util.h" 11 #include "chrome/browser/ui/webui/chrome_web_contents_handler.h" 12 #include "content/public/browser/render_view_host.h" 13 #include "content/public/browser/web_contents.h" 14 #include "ipc/ipc_message.h" 15 #include "ui/web_dialogs/web_dialog_delegate.h" 16 #include "ui/web_dialogs/web_dialog_ui.h" 17 18 using content::NativeWebKeyboardEvent; 19 using content::WebContents; 20 using ui::WebDialogDelegate; 21 using ui::WebDialogWebContentsDelegate; 22 23 ConstrainedWebDialogDelegateBase::ConstrainedWebDialogDelegateBase( 24 content::BrowserContext* browser_context, 25 WebDialogDelegate* delegate, 26 WebDialogWebContentsDelegate* tab_delegate) 27 : WebDialogWebContentsDelegate(browser_context, 28 new ChromeWebContentsHandler), 29 web_dialog_delegate_(delegate), 30 closed_via_webui_(false), 31 release_contents_on_close_(false) { 32 CHECK(delegate); 33 web_contents_.reset( 34 WebContents::Create(WebContents::CreateParams(browser_context))); 35 if (tab_delegate) { 36 override_tab_delegate_.reset(tab_delegate); 37 web_contents_->SetDelegate(tab_delegate); 38 } else { 39 web_contents_->SetDelegate(this); 40 } 41 renderer_preferences_util::UpdateFromSystemSettings( 42 web_contents_->GetMutableRendererPrefs(), 43 Profile::FromBrowserContext(browser_context)); 44 web_contents_->GetRenderViewHost()->SyncRendererPrefs(); 45 46 // Set |this| as a delegate so the ConstrainedWebDialogUI can retrieve it. 47 ConstrainedWebDialogUI::SetConstrainedDelegate(web_contents_.get(), this); 48 49 web_contents_->GetController().LoadURL(delegate->GetDialogContentURL(), 50 content::Referrer(), 51 content::PAGE_TRANSITION_AUTO_TOPLEVEL, 52 std::string()); 53 } 54 55 ConstrainedWebDialogDelegateBase::~ConstrainedWebDialogDelegateBase() { 56 if (release_contents_on_close_) 57 ignore_result(web_contents_.release()); 58 } 59 60 const WebDialogDelegate* 61 ConstrainedWebDialogDelegateBase::GetWebDialogDelegate() const { 62 return web_dialog_delegate_.get(); 63 } 64 65 WebDialogDelegate* 66 ConstrainedWebDialogDelegateBase::GetWebDialogDelegate() { 67 return web_dialog_delegate_.get(); 68 } 69 70 void ConstrainedWebDialogDelegateBase::OnDialogCloseFromWebUI() { 71 closed_via_webui_ = true; 72 CloseContents(web_contents_.get()); 73 } 74 75 bool ConstrainedWebDialogDelegateBase::closed_via_webui() const { 76 return closed_via_webui_; 77 } 78 79 void ConstrainedWebDialogDelegateBase::ReleaseWebContentsOnDialogClose() { 80 release_contents_on_close_ = true; 81 } 82 83 web_modal::NativeWebContentsModalDialog 84 ConstrainedWebDialogDelegateBase::GetNativeDialog() { 85 NOTREACHED(); 86 return NULL; 87 } 88 89 WebContents* ConstrainedWebDialogDelegateBase::GetWebContents() { 90 return web_contents_.get(); 91 } 92 93 void ConstrainedWebDialogDelegateBase::HandleKeyboardEvent( 94 content::WebContents* source, 95 const NativeWebKeyboardEvent& event) { 96 } 97