Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / web_dialogs / web_dialog_ui.cc
blob8da900efa666afd0e33b74a7bfd02aefd7362b47
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.
5 #include "ui/web_dialogs/web_dialog_ui.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/lazy_instance.h"
10 #include "base/values.h"
11 #include "content/public/browser/render_view_host.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/browser/web_ui.h"
14 #include "content/public/browser/web_ui_message_handler.h"
15 #include "content/public/common/bindings_policy.h"
16 #include "ui/web_dialogs/web_dialog_delegate.h"
18 using content::RenderViewHost;
19 using content::WebUIMessageHandler;
21 namespace ui {
23 namespace {
25 const char kWebDialogDelegateUserDataKey[] = "WebDialogDelegateUserData";
27 class WebDialogDelegateUserData : public base::SupportsUserData::Data {
28 public:
29 explicit WebDialogDelegateUserData(WebDialogDelegate* delegate)
30 : delegate_(delegate) {}
31 ~WebDialogDelegateUserData() override {}
32 WebDialogDelegate* delegate() { return delegate_; }
34 private:
35 WebDialogDelegate* delegate_; // unowned
38 } // namespace
40 WebDialogUI::WebDialogUI(content::WebUI* web_ui)
41 : WebUIController(web_ui) {
44 WebDialogUI::~WebDialogUI() {
45 // Don't unregister our user data. During the teardown of the WebContents,
46 // this will be deleted, but the WebContents will already be destroyed.
48 // This object is owned indirectly by the WebContents. WebUIs can change, so
49 // it's scary if this WebUI is changed out and replaced with something else,
50 // since the user data will still point to the old delegate. But the delegate
51 // is itself the owner of the WebContents for a dialog so will be in scope,
52 // and the HTML dialogs won't swap WebUIs anyway since they don't navigate.
55 void WebDialogUI::CloseDialog(const base::ListValue* args) {
56 OnDialogClosed(args);
59 // static
60 void WebDialogUI::SetDelegate(content::WebContents* web_contents,
61 WebDialogDelegate* delegate) {
62 web_contents->SetUserData(&kWebDialogDelegateUserDataKey,
63 new WebDialogDelegateUserData(delegate));
66 ////////////////////////////////////////////////////////////////////////////////
67 // Private:
69 WebDialogDelegate* WebDialogUI::GetDelegate(
70 content::WebContents* web_contents) {
71 WebDialogDelegateUserData* user_data =
72 static_cast<WebDialogDelegateUserData*>(
73 web_contents->GetUserData(&kWebDialogDelegateUserDataKey));
75 return user_data ? user_data->delegate() : NULL;
79 void WebDialogUI::RenderViewCreated(RenderViewHost* render_view_host) {
80 // Hook up the javascript function calls, also known as chrome.send("foo")
81 // calls in the HTML, to the actual C++ functions.
82 web_ui()->RegisterMessageCallback("dialogClose",
83 base::Bind(&WebDialogUI::OnDialogClosed, base::Unretained(this)));
85 // Pass the arguments to the renderer supplied by the delegate.
86 std::string dialog_args;
87 std::vector<WebUIMessageHandler*> handlers;
88 WebDialogDelegate* delegate = GetDelegate(web_ui()->GetWebContents());
89 if (delegate) {
90 dialog_args = delegate->GetDialogArgs();
91 delegate->GetWebUIMessageHandlers(&handlers);
94 if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI))
95 render_view_host->SetWebUIProperty("dialogArguments", dialog_args);
96 for (std::vector<WebUIMessageHandler*>::iterator it = handlers.begin();
97 it != handlers.end(); ++it) {
98 web_ui()->AddMessageHandler(*it);
101 if (delegate)
102 delegate->OnDialogShown(web_ui(), render_view_host);
105 void WebDialogUI::OnDialogClosed(const base::ListValue* args) {
106 WebDialogDelegate* delegate = GetDelegate(web_ui()->GetWebContents());
107 if (delegate) {
108 std::string json_retval;
109 if (args && !args->empty() && !args->GetString(0, &json_retval))
110 NOTREACHED() << "Could not read JSON argument";
112 delegate->OnDialogCloseFromWebUI(json_retval);
116 } // namespace ui