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.
5 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/lazy_instance.h"
13 #include "base/values.h"
14 #include "content/public/browser/notification_service.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/browser/web_ui.h"
18 #include "ui/web_dialogs/web_dialog_delegate.h"
19 #include "ui/web_dialogs/web_dialog_ui.h"
21 using content::RenderViewHost
;
22 using content::WebContents
;
23 using content::WebUIMessageHandler
;
27 const char kConstrainedWebDialogDelegateUserDataKey
[] =
28 "ConstrainedWebDialogDelegateUserData";
30 class ConstrainedWebDialogDelegateUserData
31 : public base::SupportsUserData::Data
{
33 explicit ConstrainedWebDialogDelegateUserData(
34 ConstrainedWebDialogDelegate
* delegate
) : delegate_(delegate
) {}
35 virtual ~ConstrainedWebDialogDelegateUserData() {}
37 ConstrainedWebDialogDelegate
* delegate() { return delegate_
; }
40 ConstrainedWebDialogDelegate
* delegate_
; // unowned
42 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateUserData
);
47 ConstrainedWebDialogUI::ConstrainedWebDialogUI(content::WebUI
* web_ui
)
48 : WebUIController(web_ui
) {
51 ConstrainedWebDialogUI::~ConstrainedWebDialogUI() {
54 void ConstrainedWebDialogUI::RenderViewCreated(
55 RenderViewHost
* render_view_host
) {
56 ConstrainedWebDialogDelegate
* delegate
= GetConstrainedDelegate();
60 ui::WebDialogDelegate
* dialog_delegate
= delegate
->GetWebDialogDelegate();
61 std::vector
<WebUIMessageHandler
*> handlers
;
62 dialog_delegate
->GetWebUIMessageHandlers(&handlers
);
63 render_view_host
->SetWebUIProperty("dialogArguments",
64 dialog_delegate
->GetDialogArgs());
65 for (std::vector
<WebUIMessageHandler
*>::iterator it
= handlers
.begin();
66 it
!= handlers
.end(); ++it
) {
67 web_ui()->AddMessageHandler(*it
);
70 // Add a "DialogClose" callback which matches WebDialogUI behavior.
71 web_ui()->RegisterMessageCallback("DialogClose",
72 base::Bind(&ConstrainedWebDialogUI::OnDialogCloseMessage
,
73 base::Unretained(this)));
75 dialog_delegate
->OnDialogShown(web_ui(), render_view_host
);
78 void ConstrainedWebDialogUI::OnDialogCloseMessage(const base::ListValue
* args
) {
79 ConstrainedWebDialogDelegate
* delegate
= GetConstrainedDelegate();
83 std::string json_retval
;
84 if (!args
->empty() && !args
->GetString(0, &json_retval
))
85 NOTREACHED() << "Could not read JSON argument";
86 delegate
->GetWebDialogDelegate()->OnDialogClosed(json_retval
);
87 delegate
->OnDialogCloseFromWebUI();
91 void ConstrainedWebDialogUI::SetConstrainedDelegate(
92 content::WebContents
* web_contents
,
93 ConstrainedWebDialogDelegate
* delegate
) {
94 web_contents
->SetUserData(&kConstrainedWebDialogDelegateUserDataKey
,
95 new ConstrainedWebDialogDelegateUserData(delegate
));
98 ConstrainedWebDialogDelegate
* ConstrainedWebDialogUI::GetConstrainedDelegate() {
99 ConstrainedWebDialogDelegateUserData
* user_data
=
100 static_cast<ConstrainedWebDialogDelegateUserData
*>(
101 web_ui()->GetWebContents()->
102 GetUserData(&kConstrainedWebDialogDelegateUserDataKey
));
104 return user_data
? user_data
->delegate() : NULL
;