| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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/tab_modal_confirm_dialog_webui.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "base/json/json_reader.h" |
| 11 #include "base/json/json_writer.h" |
| 12 #include "base/string_piece.h" |
| 13 #include "base/utf_string_conversions.h" |
| 14 #include "base/values.h" |
| 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/ui/browser_dialogs.h" |
| 17 #include "chrome/browser/ui/constrained_window.h" |
| 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 19 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" |
| 20 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| 21 #include "chrome/browser/ui/webui/constrained_html_ui.h" |
| 22 #include "chrome/browser/ui/webui/html_dialog_ui.h" |
| 23 #include "chrome/common/jstemplate_builder.h" |
| 24 #include "chrome/common/url_constants.h" |
| 25 #include "content/browser/tab_contents/tab_contents.h" |
| 26 #include "grit/browser_resources.h" |
| 27 #include "grit/generated_resources.h" |
| 28 #include "ui/base/l10n/l10n_util.h" |
| 29 #include "ui/base/resource/resource_bundle.h" |
| 30 #include "ui/gfx/size.h" |
| 31 |
| 32 namespace browser { |
| 33 |
| 34 // Declared in browser_dialogs.h so others don't have to depend on our header. |
| 35 void ShowTabModalConfirmDialog(TabModalConfirmDialogDelegate* delegate, |
| 36 TabContentsWrapper* wrapper) { |
| 37 new TabModalConfirmDialogUI(delegate, wrapper); |
| 38 } |
| 39 |
| 40 } // namespace browser |
| 41 |
| 42 class TabModalConfirmDialogHtmlDelegate : public HtmlDialogUIDelegate { |
| 43 public: |
| 44 TabModalConfirmDialogHtmlDelegate( |
| 45 TabModalConfirmDialogUI* ui, |
| 46 TabModalConfirmDialogDelegate* dialog_delegate) |
| 47 : ui_(ui), |
| 48 dialog_delegate_(dialog_delegate) {} |
| 49 |
| 50 virtual ~TabModalConfirmDialogHtmlDelegate() {} |
| 51 |
| 52 // HtmlDialogUIDelegate implementation. |
| 53 virtual bool IsDialogModal() const OVERRIDE { |
| 54 return true; |
| 55 } |
| 56 |
| 57 virtual string16 GetDialogTitle() const OVERRIDE { |
| 58 return dialog_delegate_->GetTitle(); |
| 59 } |
| 60 |
| 61 virtual GURL GetDialogContentURL() const OVERRIDE { |
| 62 return GURL(chrome::kChromeUITabModalConfirmDialogURL); |
| 63 } |
| 64 |
| 65 virtual void GetWebUIMessageHandlers( |
| 66 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE {} |
| 67 |
| 68 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE { |
| 69 size->SetSize(kDialogWidth, kDialogHeight); |
| 70 } |
| 71 |
| 72 virtual std::string GetDialogArgs() const OVERRIDE { |
| 73 DictionaryValue dict; |
| 74 dict.SetString("message", dialog_delegate_->GetMessage()); |
| 75 dict.SetString("accept", dialog_delegate_->GetAcceptButtonTitle()); |
| 76 dict.SetString("cancel", dialog_delegate_->GetCancelButtonTitle()); |
| 77 ChromeWebUIDataSource::SetFontAndTextDirection(&dict); |
| 78 std::string json; |
| 79 base::JSONWriter::Write(&dict, false, &json); |
| 80 return json; |
| 81 } |
| 82 |
| 83 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { |
| 84 bool accepted = false; |
| 85 if (!json_retval.empty()) { |
| 86 base::JSONReader reader; |
| 87 scoped_ptr<Value> value(reader.JsonToValue(json_retval, false, false)); |
| 88 DCHECK(value.get() && value->GetAsBoolean(&accepted)) |
| 89 << "Missing or unreadable response from dialog"; |
| 90 } |
| 91 |
| 92 ui_->OnDialogClosed(accepted); |
| 93 delete this; |
| 94 } |
| 95 |
| 96 virtual void OnCloseContents(TabContents* source, |
| 97 bool* out_close_dialog) OVERRIDE {} |
| 98 |
| 99 virtual bool ShouldShowDialogTitle() const OVERRIDE { |
| 100 return true; |
| 101 } |
| 102 |
| 103 private: |
| 104 static const int kDialogWidth = 400; |
| 105 static const int kDialogHeight = 120; |
| 106 |
| 107 scoped_ptr<TabModalConfirmDialogUI> ui_; |
| 108 // Owned by TabModalConfirmDialogUI, which we own. |
| 109 TabModalConfirmDialogDelegate* dialog_delegate_; |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(TabModalConfirmDialogHtmlDelegate); |
| 112 }; |
| 113 |
| 114 TabModalConfirmDialogUI::TabModalConfirmDialogUI( |
| 115 TabModalConfirmDialogDelegate* delegate, |
| 116 TabContentsWrapper* wrapper) |
| 117 : delegate_(delegate) { |
| 118 Profile* profile = wrapper->profile(); |
| 119 ChromeWebUIDataSource* data_source = |
| 120 new ChromeWebUIDataSource(chrome::kChromeUITabModalConfirmDialogHost); |
| 121 data_source->set_default_resource(IDR_TAB_MODAL_CONFIRM_DIALOG_HTML); |
| 122 profile->GetChromeURLDataManager()->AddDataSource(data_source); |
| 123 |
| 124 TabModalConfirmDialogHtmlDelegate* html_delegate = |
| 125 new TabModalConfirmDialogHtmlDelegate(this, delegate); |
| 126 ConstrainedHtmlUIDelegate* dialog_delegate = |
| 127 ConstrainedHtmlUI::CreateConstrainedHtmlDialog(profile, html_delegate, |
| 128 wrapper); |
| 129 delegate_->set_window(dialog_delegate->window()); |
| 130 } |
| 131 |
| 132 TabModalConfirmDialogUI::~TabModalConfirmDialogUI() {} |
| 133 |
| 134 void TabModalConfirmDialogUI::OnDialogClosed(bool accepted) { |
| 135 delegate_->set_window(NULL); |
| 136 if (accepted) |
| 137 delegate_->Accept(); |
| 138 else |
| 139 delegate_->Cancel(); |
| 140 } |
| OLD | NEW |