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 "chrome/browser/ui/webui/constrained_html_ui.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/bind_helpers.h" | |
12 #include "base/lazy_instance.h" | |
13 #include "base/property_bag.h" | |
14 #include "base/values.h" | |
15 #include "chrome/browser/ui/webui/html_dialog_ui.h" | |
16 #include "chrome/common/chrome_notification_types.h" | |
17 #include "content/public/browser/notification_service.h" | |
18 #include "content/public/browser/render_view_host.h" | |
19 #include "content/public/browser/web_contents.h" | |
20 #include "content/public/browser/web_ui.h" | |
21 | |
22 using content::RenderViewHost; | |
23 using content::WebContents; | |
24 using content::WebUIMessageHandler; | |
25 | |
26 static base::LazyInstance<base::PropertyAccessor<ConstrainedHtmlUIDelegate*> > | |
27 g_constrained_html_ui_property_accessor = LAZY_INSTANCE_INITIALIZER; | |
28 | |
29 ConstrainedHtmlUI::ConstrainedHtmlUI(content::WebUI* web_ui) | |
30 : WebUIController(web_ui) { | |
31 } | |
32 | |
33 ConstrainedHtmlUI::~ConstrainedHtmlUI() { | |
34 } | |
35 | |
36 void ConstrainedHtmlUI::RenderViewCreated(RenderViewHost* render_view_host) { | |
37 ConstrainedHtmlUIDelegate* delegate = GetConstrainedDelegate(); | |
38 if (!delegate) | |
39 return; | |
40 | |
41 HtmlDialogUIDelegate* dialog_delegate = delegate->GetHtmlDialogUIDelegate(); | |
42 std::vector<WebUIMessageHandler*> handlers; | |
43 dialog_delegate->GetWebUIMessageHandlers(&handlers); | |
44 render_view_host->SetWebUIProperty("dialogArguments", | |
45 dialog_delegate->GetDialogArgs()); | |
46 for (std::vector<WebUIMessageHandler*>::iterator it = handlers.begin(); | |
47 it != handlers.end(); ++it) { | |
48 web_ui()->AddMessageHandler(*it); | |
49 } | |
50 | |
51 // Add a "DialogClose" callback which matches HTMLDialogUI behavior. | |
52 web_ui()->RegisterMessageCallback("DialogClose", | |
53 base::Bind(&ConstrainedHtmlUI::OnDialogCloseMessage, | |
54 base::Unretained(this))); | |
55 | |
56 content::NotificationService::current()->Notify( | |
57 chrome::NOTIFICATION_HTML_DIALOG_SHOWN, | |
58 content::Source<content::WebUI>(web_ui()), | |
59 content::Details<RenderViewHost>(render_view_host)); | |
60 } | |
61 | |
62 void ConstrainedHtmlUI::OnDialogCloseMessage(const ListValue* args) { | |
63 ConstrainedHtmlUIDelegate* delegate = GetConstrainedDelegate(); | |
64 if (!delegate) | |
65 return; | |
66 | |
67 std::string json_retval; | |
68 if (!args->empty() && !args->GetString(0, &json_retval)) | |
69 NOTREACHED() << "Could not read JSON argument"; | |
70 delegate->GetHtmlDialogUIDelegate()->OnDialogClosed(json_retval); | |
71 delegate->OnDialogCloseFromWebUI(); | |
72 } | |
73 | |
74 ConstrainedHtmlUIDelegate* ConstrainedHtmlUI::GetConstrainedDelegate() { | |
75 ConstrainedHtmlUIDelegate** property = GetPropertyAccessor().GetProperty( | |
76 web_ui()->GetWebContents()->GetPropertyBag()); | |
77 return property ? *property : NULL; | |
78 } | |
79 | |
80 // static | |
81 base::PropertyAccessor<ConstrainedHtmlUIDelegate*>& | |
82 ConstrainedHtmlUI::GetPropertyAccessor() { | |
83 return g_constrained_html_ui_property_accessor.Get(); | |
84 } | |
OLD | NEW |