| OLD | NEW |
| (Empty) |
| 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/web_dialog_ui.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/lazy_instance.h" | |
| 10 #include "base/property_bag.h" | |
| 11 #include "base/values.h" | |
| 12 #include "chrome/browser/ui/webui/web_dialog_delegate.h" | |
| 13 #include "chrome/common/chrome_notification_types.h" | |
| 14 #include "content/public/browser/notification_service.h" | |
| 15 #include "content/public/browser/render_view_host.h" | |
| 16 #include "content/public/browser/web_contents.h" | |
| 17 #include "content/public/browser/web_ui.h" | |
| 18 #include "content/public/browser/web_ui_message_handler.h" | |
| 19 #include "content/public/common/bindings_policy.h" | |
| 20 | |
| 21 using content::RenderViewHost; | |
| 22 using content::WebUIMessageHandler; | |
| 23 | |
| 24 static base::LazyInstance<base::PropertyAccessor<WebDialogDelegate*> > | |
| 25 g_web_dialog_ui_property_accessor = LAZY_INSTANCE_INITIALIZER; | |
| 26 | |
| 27 WebDialogUI::WebDialogUI(content::WebUI* web_ui) | |
| 28 : WebUIController(web_ui) { | |
| 29 } | |
| 30 | |
| 31 WebDialogUI::~WebDialogUI() { | |
| 32 // Don't unregister our property. During the teardown of the WebContents, | |
| 33 // this will be deleted, but the WebContents will already be destroyed. | |
| 34 // | |
| 35 // This object is owned indirectly by the WebContents. WebUIs can change, so | |
| 36 // it's scary if this WebUI is changed out and replaced with something else, | |
| 37 // since the property will still point to the old delegate. But the delegate | |
| 38 // is itself the owner of the WebContents for a dialog so will be in scope, | |
| 39 // and the HTML dialogs won't swap WebUIs anyway since they don't navigate. | |
| 40 } | |
| 41 | |
| 42 void WebDialogUI::CloseDialog(const base::ListValue* args) { | |
| 43 OnDialogClosed(args); | |
| 44 } | |
| 45 | |
| 46 // static | |
| 47 base::PropertyAccessor<WebDialogDelegate*>& WebDialogUI::GetPropertyAccessor() { | |
| 48 return g_web_dialog_ui_property_accessor.Get(); | |
| 49 } | |
| 50 | |
| 51 //////////////////////////////////////////////////////////////////////////////// | |
| 52 // Private: | |
| 53 | |
| 54 void WebDialogUI::RenderViewCreated(RenderViewHost* render_view_host) { | |
| 55 // Hook up the javascript function calls, also known as chrome.send("foo") | |
| 56 // calls in the HTML, to the actual C++ functions. | |
| 57 web_ui()->RegisterMessageCallback("DialogClose", | |
| 58 base::Bind(&WebDialogUI::OnDialogClosed, base::Unretained(this))); | |
| 59 | |
| 60 // Pass the arguments to the renderer supplied by the delegate. | |
| 61 std::string dialog_args; | |
| 62 std::vector<WebUIMessageHandler*> handlers; | |
| 63 WebDialogDelegate** delegate = GetPropertyAccessor().GetProperty( | |
| 64 web_ui()->GetWebContents()->GetPropertyBag()); | |
| 65 if (delegate) { | |
| 66 dialog_args = (*delegate)->GetDialogArgs(); | |
| 67 (*delegate)->GetWebUIMessageHandlers(&handlers); | |
| 68 } | |
| 69 | |
| 70 if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI)) | |
| 71 render_view_host->SetWebUIProperty("dialogArguments", dialog_args); | |
| 72 for (std::vector<WebUIMessageHandler*>::iterator it = handlers.begin(); | |
| 73 it != handlers.end(); ++it) { | |
| 74 web_ui()->AddMessageHandler(*it); | |
| 75 } | |
| 76 | |
| 77 content::NotificationService::current()->Notify( | |
| 78 chrome::NOTIFICATION_WEB_DIALOG_SHOWN, | |
| 79 content::Source<content::WebUI>(web_ui()), | |
| 80 content::Details<RenderViewHost>(render_view_host)); | |
| 81 } | |
| 82 | |
| 83 void WebDialogUI::OnDialogClosed(const ListValue* args) { | |
| 84 WebDialogDelegate** delegate = GetPropertyAccessor().GetProperty( | |
| 85 web_ui()->GetWebContents()->GetPropertyBag()); | |
| 86 if (delegate) { | |
| 87 std::string json_retval; | |
| 88 if (args && !args->empty() && !args->GetString(0, &json_retval)) | |
| 89 NOTREACHED() << "Could not read JSON argument"; | |
| 90 | |
| 91 (*delegate)->OnDialogClosed(json_retval); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 ExternalWebDialogUI::ExternalWebDialogUI(content::WebUI* web_ui) | |
| 96 : WebDialogUI(web_ui) { | |
| 97 // Non-file based UI needs to not have access to the Web UI bindings | |
| 98 // for security reasons. The code hosting the dialog should provide | |
| 99 // dialog specific functionality through other bindings and methods | |
| 100 // that are scoped in duration to the dialogs existence. | |
| 101 web_ui->SetBindings(web_ui->GetBindings() & ~content::BINDINGS_POLICY_WEB_UI); | |
| 102 } | |
| 103 | |
| 104 ExternalWebDialogUI::~ExternalWebDialogUI() { | |
| 105 } | |
| 106 | |
| 107 std::string WebDialogDelegate::GetDialogName() const { | |
| 108 return std::string(); | |
| 109 } | |
| 110 | |
| 111 void WebDialogDelegate::GetMinimumDialogSize(gfx::Size* size) const { | |
| 112 GetDialogSize(size); | |
| 113 } | |
| 114 | |
| 115 bool WebDialogDelegate::HandleContextMenu( | |
| 116 const content::ContextMenuParams& params) { | |
| 117 return false; | |
| 118 } | |
| 119 | |
| 120 bool WebDialogDelegate::HandleOpenURLFromTab( | |
| 121 content::WebContents* source, | |
| 122 const content::OpenURLParams& params, | |
| 123 content::WebContents** out_new_contents) { | |
| 124 return false; | |
| 125 } | |
| 126 | |
| 127 bool WebDialogDelegate::HandleAddNewContents( | |
| 128 content::WebContents* source, | |
| 129 content::WebContents* new_contents, | |
| 130 WindowOpenDisposition disposition, | |
| 131 const gfx::Rect& initial_pos, | |
| 132 bool user_gesture) { | |
| 133 return false; | |
| 134 } | |
| OLD | NEW |