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