Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: chrome/browser/ui/webui/web_dialog_ui.cc

Issue 10353007: Extract a minimal subset of WebDialogUI/WebDialogDelegate from src/chrome -> src/ui/web_dialogs Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/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 static base::LazyInstance<base::PropertyAccessor<WebDialogDelegate*> >
24 g_web_dialog_ui_property_accessor = LAZY_INSTANCE_INITIALIZER;
25
26 WebDialogUI::WebDialogUI(content::WebUI* web_ui)
27 : WebUIController(web_ui) {
28 }
29
30 WebDialogUI::~WebDialogUI() {
31 // Don't unregister our property. During the teardown of the WebContents,
32 // this will be deleted, but the WebContents will already be destroyed.
33 //
34 // This object is owned indirectly by the WebContents. WebUIs can change, so
35 // it's scary if this WebUI is changed out and replaced with something else,
36 // since the property will still point to the old delegate. But the delegate
37 // is itself the owner of the WebContents for a dialog so will be in scope,
38 // and the HTML dialogs won't swap WebUIs anyway since they don't navigate.
39 }
40
41 void WebDialogUI::CloseDialog(const base::ListValue* args) {
42 OnDialogClosed(args);
43 }
44
45 // static
46 base::PropertyAccessor<WebDialogDelegate*>& WebDialogUI::GetPropertyAccessor() {
47 return g_web_dialog_ui_property_accessor.Get();
48 }
49
50 ////////////////////////////////////////////////////////////////////////////////
51 // Private:
52
53 void WebDialogUI::RenderViewCreated(RenderViewHost* render_view_host) {
54 // Hook up the javascript function calls, also known as chrome.send("foo")
55 // calls in the HTML, to the actual C++ functions.
56 web_ui()->RegisterMessageCallback("DialogClose",
57 base::Bind(&WebDialogUI::OnDialogClosed, base::Unretained(this)));
58
59 // Pass the arguments to the renderer supplied by the delegate.
60 std::string dialog_args;
61 std::vector<WebUIMessageHandler*> handlers;
62 WebDialogDelegate** delegate = GetPropertyAccessor().GetProperty(
63 web_ui()->GetWebContents()->GetPropertyBag());
64 if (delegate) {
65 dialog_args = (*delegate)->GetDialogArgs();
66 (*delegate)->GetWebUIMessageHandlers(&handlers);
67 }
68
69 if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI))
70 render_view_host->SetWebUIProperty("dialogArguments", dialog_args);
71 for (std::vector<WebUIMessageHandler*>::iterator it = handlers.begin();
72 it != handlers.end(); ++it) {
73 web_ui()->AddMessageHandler(*it);
74 }
75
76 content::NotificationService::current()->Notify(
77 chrome::NOTIFICATION_WEB_DIALOG_SHOWN,
78 content::Source<content::WebUI>(web_ui()),
79 content::Details<RenderViewHost>(render_view_host));
80 }
81
82 void WebDialogUI::OnDialogClosed(const ListValue* args) {
83 WebDialogDelegate** delegate = GetPropertyAccessor().GetProperty(
84 web_ui()->GetWebContents()->GetPropertyBag());
85 if (delegate) {
86 std::string json_retval;
87 if (args && !args->empty() && !args->GetString(0, &json_retval))
88 NOTREACHED() << "Could not read JSON argument";
89
90 (*delegate)->OnDialogClosed(json_retval);
91 }
92 }
93
94 ExternalWebDialogUI::ExternalWebDialogUI(content::WebUI* web_ui)
95 : WebDialogUI(web_ui) {
96 // 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
98 // dialog specific functionality through other bindings and methods
99 // that are scoped in duration to the dialogs existence.
100 web_ui->SetBindings(web_ui->GetBindings() & ~content::BINDINGS_POLICY_WEB_UI);
101 }
102
103 ExternalWebDialogUI::~ExternalWebDialogUI() {
104 }
105
106 std::string WebDialogDelegate::GetDialogName() const {
107 return std::string();
108 }
109
110 void WebDialogDelegate::GetMinimumDialogSize(gfx::Size* size) const {
111 GetDialogSize(size);
112 }
113
114 bool WebDialogDelegate::HandleContextMenu(
115 const content::ContextMenuParams& params) {
116 return false;
117 }
118
119 bool WebDialogDelegate::HandleOpenURLFromTab(
120 content::WebContents* source,
121 const content::OpenURLParams& params,
122 content::WebContents** out_new_contents) {
123 return false;
124 }
125
126 bool WebDialogDelegate::HandleAddNewContents(
127 content::WebContents* source,
128 content::WebContents* new_contents,
129 WindowOpenDisposition disposition,
130 const gfx::Rect& initial_pos,
131 bool user_gesture) {
132 return false;
133 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698