summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/tab_modal_confirm_dialog_webui.cc
blob: 4ebf3287a5a76958070aecbafefd3fbfdeadda16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/tab_modal_confirm_dialog_webui.h"

#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/string_piece.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/constrained_window.h"
#include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h"
#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
#include "chrome/browser/ui/webui/chrome_web_ui_data_source.h"
#include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
#include "chrome/common/jstemplate_builder.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/web_contents.h"
#include "grit/browser_resources.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/size.h"

using content::WebContents;
using content::WebUIMessageHandler;

// static
TabModalConfirmDialog* TabModalConfirmDialog::Create(
    TabModalConfirmDialogDelegate* delegate,
    content::WebContents* web_contents) {
  return new TabModalConfirmDialogWebUI(delegate, web_contents);
}

const int kDialogWidth = 400;
const int kDialogHeight = 120;

TabModalConfirmDialogWebUI::TabModalConfirmDialogWebUI(
    TabModalConfirmDialogDelegate* delegate,
    WebContents* web_contents)
    : delegate_(delegate) {
  Profile* profile =
      Profile::FromBrowserContext(web_contents->GetBrowserContext());
  ChromeWebUIDataSource* data_source =
      new ChromeWebUIDataSource(chrome::kChromeUITabModalConfirmDialogHost);
  data_source->set_default_resource(IDR_TAB_MODAL_CONFIRM_DIALOG_HTML);
  ChromeURLDataManager::AddDataSource(profile, data_source);

  constrained_web_dialog_delegate_ =
      CreateConstrainedWebDialog(profile, this, NULL, web_contents);
  delegate_->set_window(constrained_web_dialog_delegate_->GetWindow());
}

ui::ModalType TabModalConfirmDialogWebUI::GetDialogModalType() const {
  return ui::MODAL_TYPE_WINDOW;
}

string16 TabModalConfirmDialogWebUI::GetDialogTitle() const {
  return delegate_->GetTitle();
}

GURL TabModalConfirmDialogWebUI::GetDialogContentURL() const {
  return GURL(chrome::kChromeUITabModalConfirmDialogURL);
}

void TabModalConfirmDialogWebUI::GetWebUIMessageHandlers(
    std::vector<WebUIMessageHandler*>* handlers) const {}

void TabModalConfirmDialogWebUI::GetDialogSize(gfx::Size* size) const {
  size->SetSize(kDialogWidth, kDialogHeight);
}

std::string TabModalConfirmDialogWebUI::GetDialogArgs() const {
  DictionaryValue dict;
  dict.SetString("message", delegate_->GetMessage());
  dict.SetString("accept", delegate_->GetAcceptButtonTitle());
  dict.SetString("cancel", delegate_->GetCancelButtonTitle());
  ChromeWebUIDataSource::SetFontAndTextDirection(&dict);
  std::string json;
  base::JSONWriter::Write(&dict, &json);
  return json;
}

void TabModalConfirmDialogWebUI::OnDialogClosed(
    const std::string& json_retval) {
  bool accepted = false;
  if (!json_retval.empty()) {
    scoped_ptr<Value> value(base::JSONReader::Read(json_retval));
    if (!value.get() || !value->GetAsBoolean(&accepted))
      NOTREACHED() << "Missing or unreadable response from dialog";
  }

  delegate_->set_window(NULL);
  if (accepted)
    delegate_->Accept();
  else
    delegate_->Cancel();
}

void TabModalConfirmDialogWebUI::OnCloseContents(WebContents* source,
                                                 bool* out_close_dialog) {}

bool TabModalConfirmDialogWebUI::ShouldShowDialogTitle() const {
  return true;
}

TabModalConfirmDialogWebUI::~TabModalConfirmDialogWebUI() {}

void TabModalConfirmDialogWebUI::AcceptTabModalDialog() {
}

void TabModalConfirmDialogWebUI::CancelTabModalDialog() {
}