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 #if defined(ENABLE_EXTENSIONS)
22 #include "chrome/browser/extensions/tab_helper.h"
25 using content::RenderViewHost
;
26 using content::WebContents
;
27 using content::WebUIMessageHandler
;
31 const char kConstrainedWebDialogDelegateUserDataKey
[] =
32 "ConstrainedWebDialogDelegateUserData";
34 class ConstrainedWebDialogDelegateUserData
35 : public base::SupportsUserData::Data
{
37 explicit ConstrainedWebDialogDelegateUserData(
38 ConstrainedWebDialogDelegate
* delegate
) : delegate_(delegate
) {}
39 ~ConstrainedWebDialogDelegateUserData() override
{}
41 ConstrainedWebDialogDelegate
* delegate() { return delegate_
; }
44 ConstrainedWebDialogDelegate
* delegate_
; // unowned
46 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateUserData
);
51 ConstrainedWebDialogUI::ConstrainedWebDialogUI(content::WebUI
* web_ui
)
52 : WebUIController(web_ui
) {
53 #if defined(ENABLE_EXTENSIONS)
54 extensions::TabHelper::CreateForWebContents(web_ui
->GetWebContents());
58 ConstrainedWebDialogUI::~ConstrainedWebDialogUI() {
61 void ConstrainedWebDialogUI::RenderViewCreated(
62 RenderViewHost
* render_view_host
) {
63 // Add a "dialogClose" callback which matches WebDialogUI behavior.
64 web_ui()->RegisterMessageCallback("dialogClose",
65 base::Bind(&ConstrainedWebDialogUI::OnDialogCloseMessage
,
66 base::Unretained(this)));
68 ConstrainedWebDialogDelegate
* delegate
= GetConstrainedDelegate();
72 ui::WebDialogDelegate
* dialog_delegate
= delegate
->GetWebDialogDelegate();
73 std::vector
<WebUIMessageHandler
*> handlers
;
74 dialog_delegate
->GetWebUIMessageHandlers(&handlers
);
75 render_view_host
->SetWebUIProperty("dialogArguments",
76 dialog_delegate
->GetDialogArgs());
77 for (std::vector
<WebUIMessageHandler
*>::iterator it
= handlers
.begin();
78 it
!= handlers
.end(); ++it
) {
79 web_ui()->AddMessageHandler(*it
);
82 dialog_delegate
->OnDialogShown(web_ui(), render_view_host
);
85 void ConstrainedWebDialogUI::OnDialogCloseMessage(const base::ListValue
* args
) {
86 ConstrainedWebDialogDelegate
* delegate
= GetConstrainedDelegate();
90 std::string json_retval
;
91 if (!args
->empty() && !args
->GetString(0, &json_retval
))
92 NOTREACHED() << "Could not read JSON argument";
93 delegate
->GetWebDialogDelegate()->OnDialogClosed(json_retval
);
94 delegate
->OnDialogCloseFromWebUI();
98 void ConstrainedWebDialogUI::SetConstrainedDelegate(
99 content::WebContents
* web_contents
,
100 ConstrainedWebDialogDelegate
* delegate
) {
101 web_contents
->SetUserData(&kConstrainedWebDialogDelegateUserDataKey
,
102 new ConstrainedWebDialogDelegateUserData(delegate
));
105 ConstrainedWebDialogDelegate
* ConstrainedWebDialogUI::GetConstrainedDelegate() {
106 ConstrainedWebDialogDelegateUserData
* user_data
=
107 static_cast<ConstrainedWebDialogDelegateUserData
*>(
108 web_ui()->GetWebContents()->
109 GetUserData(&kConstrainedWebDialogDelegateUserDataKey
));
111 return user_data
? user_data
->delegate() : NULL
;