| 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_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/common/chrome_notification_types.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 |
| 20 using content::RenderViewHost; |
| 21 using content::WebUIMessageHandler; |
| 22 |
| 23 namespace views { |
| 24 |
| 25 static base::LazyInstance<base::PropertyAccessor<WebDialogDelegate*> > |
| 26 g_web_dialog_ui_property_accessor = LAZY_INSTANCE_INITIALIZER; |
| 27 |
| 28 WebDialogUI::WebDialogUI(content::WebUI* web_ui) |
| 29 : WebUIController(web_ui) { |
| 30 } |
| 31 |
| 32 WebDialogUI::~WebDialogUI() { |
| 33 // Don't unregister our property. During the teardown of the WebContents, |
| 34 // this will be deleted, but the WebContents will already be destroyed. |
| 35 // |
| 36 // 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, |
| 38 // since the property 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, |
| 40 // and the HTML dialogs won't swap WebUIs anyway since they don't navigate. |
| 41 } |
| 42 |
| 43 void WebDialogUI::CloseDialog(const base::ListValue* args) { |
| 44 OnDialogClosed(args); |
| 45 } |
| 46 |
| 47 // static |
| 48 base::PropertyAccessor<WebDialogDelegate*>& WebDialogUI::GetPropertyAccessor() { |
| 49 return g_web_dialog_ui_property_accessor.Get(); |
| 50 } |
| 51 |
| 52 //////////////////////////////////////////////////////////////////////////////// |
| 53 // Private: |
| 54 |
| 55 void WebDialogUI::RenderViewCreated(RenderViewHost* render_view_host) { |
| 56 // Hook up the javascript function calls, also known as chrome.send("foo") |
| 57 // calls in the HTML, to the actual C++ functions. |
| 58 web_ui()->RegisterMessageCallback("DialogClose", |
| 59 base::Bind(&WebDialogUI::OnDialogClosed, base::Unretained(this))); |
| 60 |
| 61 // Pass the arguments to the renderer supplied by the delegate. |
| 62 std::string dialog_args; |
| 63 std::vector<WebUIMessageHandler*> handlers; |
| 64 WebDialogDelegate** delegate = GetPropertyAccessor().GetProperty( |
| 65 web_ui()->GetWebContents()->GetPropertyBag()); |
| 66 if (delegate) { |
| 67 dialog_args = (*delegate)->GetDialogArgs(); |
| 68 (*delegate)->GetWebUIMessageHandlers(&handlers); |
| 69 } |
| 70 |
| 71 if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI)) |
| 72 render_view_host->SetWebUIProperty("dialogArguments", dialog_args); |
| 73 for (std::vector<WebUIMessageHandler*>::iterator it = handlers.begin(); |
| 74 it != handlers.end(); ++it) { |
| 75 web_ui()->AddMessageHandler(*it); |
| 76 } |
| 77 |
| 78 content::NotificationService::current()->Notify( |
| 79 chrome::NOTIFICATION_WEB_DIALOG_SHOWN, |
| 80 content::Source<content::WebUI>(web_ui()), |
| 81 content::Details<RenderViewHost>(render_view_host)); |
| 82 } |
| 83 |
| 84 void WebDialogUI::OnDialogClosed(const ListValue* args) { |
| 85 WebDialogDelegate** delegate = GetPropertyAccessor().GetProperty( |
| 86 web_ui()->GetWebContents()->GetPropertyBag()); |
| 87 if (delegate) { |
| 88 std::string json_retval; |
| 89 if (args && !args->empty() && !args->GetString(0, &json_retval)) |
| 90 NOTREACHED() << "Could not read JSON argument"; |
| 91 |
| 92 (*delegate)->OnDialogClosed(json_retval); |
| 93 } |
| 94 } |
| 95 |
| 96 std::string WebDialogDelegate::GetDialogName() const { |
| 97 return std::string(); |
| 98 } |
| 99 |
| 100 void WebDialogDelegate::GetMinimumDialogSize(gfx::Size* size) const { |
| 101 GetDialogSize(size); |
| 102 } |
| 103 |
| 104 bool WebDialogDelegate::HandleContextMenu( |
| 105 const content::ContextMenuParams& params) { |
| 106 return false; |
| 107 } |
| 108 |
| 109 bool WebDialogDelegate::HandleOpenURLFromTab( |
| 110 content::WebContents* source, |
| 111 const content::OpenURLParams& params, |
| 112 content::WebContents** out_new_contents) { |
| 113 return false; |
| 114 } |
| 115 |
| 116 bool WebDialogDelegate::HandleAddNewContents( |
| 117 content::WebContents* source, |
| 118 content::WebContents* new_contents, |
| 119 WindowOpenDisposition disposition, |
| 120 const gfx::Rect& initial_pos, |
| 121 bool user_gesture) { |
| 122 return false; |
| 123 } |
| 124 } //namespace views |
| OLD | NEW |