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

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

Issue 9224002: Make WebUI objects not derive from WebUI. WebUI objects own the controller. This is the ownership... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: sync to head to clear linux_chromeos browsertest failures Created 8 years, 11 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/browser/ui/webui/html_dialog_ui.h" 5 #include "chrome/browser/ui/webui/html_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" 10 #include "base/property_bag.h"
11 #include "base/values.h" 11 #include "base/values.h"
12 #include "chrome/common/chrome_notification_types.h" 12 #include "chrome/common/chrome_notification_types.h"
13 #include "content/browser/renderer_host/render_view_host.h" 13 #include "content/browser/renderer_host/render_view_host.h"
14 #include "content/browser/webui/web_ui.h"
14 #include "content/public/browser/notification_service.h" 15 #include "content/public/browser/notification_service.h"
15 #include "content/public/browser/web_contents.h" 16 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_ui_message_handler.h" 17 #include "content/public/browser/web_ui_message_handler.h"
17 #include "content/public/common/bindings_policy.h" 18 #include "content/public/common/bindings_policy.h"
18 19
19 using content::WebContents;
20 using content::WebUIMessageHandler; 20 using content::WebUIMessageHandler;
21 21
22 static base::LazyInstance<base::PropertyAccessor<HtmlDialogUIDelegate*> > 22 static base::LazyInstance<base::PropertyAccessor<HtmlDialogUIDelegate*> >
23 g_html_dialog_ui_property_accessor = LAZY_INSTANCE_INITIALIZER; 23 g_html_dialog_ui_property_accessor = LAZY_INSTANCE_INITIALIZER;
24 24
25 HtmlDialogUI::HtmlDialogUI(WebContents* web_contents) 25 HtmlDialogUI::HtmlDialogUI(WebUI* web_ui)
26 : WebUI(web_contents, this) { 26 : WebUIController(web_ui) {
27 } 27 }
28 28
29 HtmlDialogUI::~HtmlDialogUI() { 29 HtmlDialogUI::~HtmlDialogUI() {
30 // Don't unregister our property. During the teardown of the WebContents, 30 // Don't unregister our property. During the teardown of the WebContents,
31 // this will be deleted, but the WebContents will already be destroyed. 31 // this will be deleted, but the WebContents will already be destroyed.
32 // 32 //
33 // This object is owned indirectly by the WebContents. WebUIs can change, so 33 // This object is owned indirectly by the WebContents. WebUIs can change, so
34 // it's scary if this WebUI is changed out and replaced with something else, 34 // it's scary if this WebUI is changed out and replaced with something else,
35 // since the property will still point to the old delegate. But the delegate 35 // since the property will still point to the old delegate. But the delegate
36 // is itself the owner of the WebContents for a dialog so will be in scope, 36 // is itself the owner of the WebContents for a dialog so will be in scope,
37 // and the HTML dialogs won't swap WebUIs anyway since they don't navigate. 37 // and the HTML dialogs won't swap WebUIs anyway since they don't navigate.
38 } 38 }
39 39
40 void HtmlDialogUI::CloseDialog(const base::ListValue* args) { 40 void HtmlDialogUI::CloseDialog(const base::ListValue* args) {
41 OnDialogClosed(args); 41 OnDialogClosed(args);
42 } 42 }
43 43
44 // static 44 // static
45 base::PropertyAccessor<HtmlDialogUIDelegate*>& 45 base::PropertyAccessor<HtmlDialogUIDelegate*>&
46 HtmlDialogUI::GetPropertyAccessor() { 46 HtmlDialogUI::GetPropertyAccessor() {
47 return g_html_dialog_ui_property_accessor.Get(); 47 return g_html_dialog_ui_property_accessor.Get();
48 } 48 }
49 49
50 //////////////////////////////////////////////////////////////////////////////// 50 ////////////////////////////////////////////////////////////////////////////////
51 // Private: 51 // Private:
52 52
53 void HtmlDialogUI::RenderViewCreated(RenderViewHost* render_view_host) { 53 void HtmlDialogUI::RenderViewCreated(RenderViewHost* render_view_host) {
54 // Hook up the javascript function calls, also known as chrome.send("foo") 54 // Hook up the javascript function calls, also known as chrome.send("foo")
55 // calls in the HTML, to the actual C++ functions. 55 // calls in the HTML, to the actual C++ functions.
56 RegisterMessageCallback("DialogClose", 56 web_ui()->RegisterMessageCallback("DialogClose",
57 base::Bind(&HtmlDialogUI::OnDialogClosed, base::Unretained(this))); 57 base::Bind(&HtmlDialogUI::OnDialogClosed, base::Unretained(this)));
58 58
59 // Pass the arguments to the renderer supplied by the delegate. 59 // Pass the arguments to the renderer supplied by the delegate.
60 std::string dialog_args; 60 std::string dialog_args;
61 std::vector<WebUIMessageHandler*> handlers; 61 std::vector<WebUIMessageHandler*> handlers;
62 HtmlDialogUIDelegate** delegate = GetPropertyAccessor().GetProperty( 62 HtmlDialogUIDelegate** delegate = GetPropertyAccessor().GetProperty(
63 web_contents()->GetPropertyBag()); 63 web_ui()->web_contents()->GetPropertyBag());
64 if (delegate) { 64 if (delegate) {
65 dialog_args = (*delegate)->GetDialogArgs(); 65 dialog_args = (*delegate)->GetDialogArgs();
66 (*delegate)->GetWebUIMessageHandlers(&handlers); 66 (*delegate)->GetWebUIMessageHandlers(&handlers);
67 } 67 }
68 68
69 if (0 != (bindings_ & content::BINDINGS_POLICY_WEB_UI)) 69 if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI))
70 render_view_host->SetWebUIProperty("dialogArguments", dialog_args); 70 render_view_host->SetWebUIProperty("dialogArguments", dialog_args);
71 for (std::vector<WebUIMessageHandler*>::iterator it = handlers.begin(); 71 for (std::vector<WebUIMessageHandler*>::iterator it = handlers.begin();
72 it != handlers.end(); ++it) { 72 it != handlers.end(); ++it) {
73 AddMessageHandler(*it); 73 web_ui()->AddMessageHandler(*it);
74 } 74 }
75 75
76 content::NotificationService::current()->Notify( 76 content::NotificationService::current()->Notify(
77 chrome::NOTIFICATION_HTML_DIALOG_SHOWN, 77 chrome::NOTIFICATION_HTML_DIALOG_SHOWN,
78 content::Source<WebUI>(this), 78 content::Source<WebUI>(web_ui()),
79 content::Details<RenderViewHost>(render_view_host)); 79 content::Details<RenderViewHost>(render_view_host));
80 } 80 }
81 81
82 void HtmlDialogUI::OnDialogClosed(const ListValue* args) { 82 void HtmlDialogUI::OnDialogClosed(const ListValue* args) {
83 HtmlDialogUIDelegate** delegate = GetPropertyAccessor().GetProperty( 83 HtmlDialogUIDelegate** delegate = GetPropertyAccessor().GetProperty(
84 web_contents()->GetPropertyBag()); 84 web_ui()->web_contents()->GetPropertyBag());
85 if (delegate) { 85 if (delegate) {
86 std::string json_retval; 86 std::string json_retval;
87 if (args && !args->empty() && !args->GetString(0, &json_retval)) 87 if (args && !args->empty() && !args->GetString(0, &json_retval))
88 NOTREACHED() << "Could not read JSON argument"; 88 NOTREACHED() << "Could not read JSON argument";
89 89
90 (*delegate)->OnDialogClosed(json_retval); 90 (*delegate)->OnDialogClosed(json_retval);
91 } 91 }
92 } 92 }
93 93
94 ExternalHtmlDialogUI::ExternalHtmlDialogUI(WebContents* web_contents) 94 ExternalHtmlDialogUI::ExternalHtmlDialogUI(WebUI* web_ui)
95 : HtmlDialogUI(web_contents) { 95 : HtmlDialogUI(web_ui) {
96 // Non-file based UI needs to not have access to the Web UI bindings 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 97 // for security reasons. The code hosting the dialog should provide
98 // dialog specific functionality through other bindings and methods 98 // dialog specific functionality through other bindings and methods
99 // that are scoped in duration to the dialogs existence. 99 // that are scoped in duration to the dialogs existence.
100 bindings_ &= ~content::BINDINGS_POLICY_WEB_UI; 100 web_ui->SetBindings(web_ui->GetBindings() & ~content::BINDINGS_POLICY_WEB_UI);
101 } 101 }
102 102
103 ExternalHtmlDialogUI::~ExternalHtmlDialogUI() { 103 ExternalHtmlDialogUI::~ExternalHtmlDialogUI() {
104 } 104 }
105 105
106 bool HtmlDialogUIDelegate::HandleContextMenu(const ContextMenuParams& params) { 106 bool HtmlDialogUIDelegate::HandleContextMenu(const ContextMenuParams& params) {
107 return false; 107 return false;
108 } 108 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698