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