• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_web_dialog_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/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"
20 
21 using content::RenderViewHost;
22 using content::WebContents;
23 using content::WebUIMessageHandler;
24 
25 namespace {
26 
27 const char kConstrainedWebDialogDelegateUserDataKey[] =
28     "ConstrainedWebDialogDelegateUserData";
29 
30 class ConstrainedWebDialogDelegateUserData
31     : public base::SupportsUserData::Data {
32  public:
ConstrainedWebDialogDelegateUserData(ConstrainedWebDialogDelegate * delegate)33   explicit ConstrainedWebDialogDelegateUserData(
34       ConstrainedWebDialogDelegate* delegate) : delegate_(delegate) {}
~ConstrainedWebDialogDelegateUserData()35   virtual ~ConstrainedWebDialogDelegateUserData() {}
36 
delegate()37   ConstrainedWebDialogDelegate* delegate() { return delegate_; }
38 
39  private:
40   ConstrainedWebDialogDelegate* delegate_;  // unowned
41 
42   DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateUserData);
43 };
44 
45 }  // namespace
46 
ConstrainedWebDialogUI(content::WebUI * web_ui)47 ConstrainedWebDialogUI::ConstrainedWebDialogUI(content::WebUI* web_ui)
48     : WebUIController(web_ui) {
49 }
50 
~ConstrainedWebDialogUI()51 ConstrainedWebDialogUI::~ConstrainedWebDialogUI() {
52 }
53 
RenderViewCreated(RenderViewHost * render_view_host)54 void ConstrainedWebDialogUI::RenderViewCreated(
55     RenderViewHost* render_view_host) {
56   ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate();
57   if (!delegate)
58     return;
59 
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);
68   }
69 
70   // Add a "dialogClose" callback which matches WebDialogUI behavior.
71   web_ui()->RegisterMessageCallback("dialogClose",
72       base::Bind(&ConstrainedWebDialogUI::OnDialogCloseMessage,
73                  base::Unretained(this)));
74 
75   dialog_delegate->OnDialogShown(web_ui(), render_view_host);
76 }
77 
OnDialogCloseMessage(const base::ListValue * args)78 void ConstrainedWebDialogUI::OnDialogCloseMessage(const base::ListValue* args) {
79   ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate();
80   if (!delegate)
81     return;
82 
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();
88 }
89 
90 // static
SetConstrainedDelegate(content::WebContents * web_contents,ConstrainedWebDialogDelegate * delegate)91 void ConstrainedWebDialogUI::SetConstrainedDelegate(
92     content::WebContents* web_contents,
93     ConstrainedWebDialogDelegate* delegate) {
94   web_contents->SetUserData(&kConstrainedWebDialogDelegateUserDataKey,
95                             new ConstrainedWebDialogDelegateUserData(delegate));
96 }
97 
GetConstrainedDelegate()98 ConstrainedWebDialogDelegate* ConstrainedWebDialogUI::GetConstrainedDelegate() {
99   ConstrainedWebDialogDelegateUserData* user_data =
100       static_cast<ConstrainedWebDialogDelegateUserData*>(
101           web_ui()->GetWebContents()->
102               GetUserData(&kConstrainedWebDialogDelegateUserDataKey));
103 
104   return user_data ? user_data->delegate() : NULL;
105 }
106