constrained_web_dialog_ui.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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
21using content::RenderViewHost;
22using content::WebContents;
23using content::WebUIMessageHandler;
24
25namespace {
26
27const char kConstrainedWebDialogDelegateUserDataKey[] =
28    "ConstrainedWebDialogDelegateUserData";
29
30class ConstrainedWebDialogDelegateUserData
31    : public base::SupportsUserData::Data {
32 public:
33  explicit ConstrainedWebDialogDelegateUserData(
34      ConstrainedWebDialogDelegate* delegate) : delegate_(delegate) {}
35  virtual ~ConstrainedWebDialogDelegateUserData() {}
36
37  ConstrainedWebDialogDelegate* delegate() { return delegate_; }
38
39 private:
40  ConstrainedWebDialogDelegate* delegate_;  // unowned
41
42  DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateUserData);
43};
44
45}  // namespace
46
47ConstrainedWebDialogUI::ConstrainedWebDialogUI(content::WebUI* web_ui)
48    : WebUIController(web_ui) {
49}
50
51ConstrainedWebDialogUI::~ConstrainedWebDialogUI() {
52}
53
54void 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
78void 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
91void ConstrainedWebDialogUI::SetConstrainedDelegate(
92    content::WebContents* web_contents,
93    ConstrainedWebDialogDelegate* delegate) {
94  web_contents->SetUserData(&kConstrainedWebDialogDelegateUserDataKey,
95                            new ConstrainedWebDialogDelegateUserData(delegate));
96}
97
98ConstrainedWebDialogDelegate* 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