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/repost_form_warning_ui.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "base/json/json_reader.h" |
| 11 #include "base/string_piece.h" |
| 12 #include "base/utf_string_conversions.h" |
| 13 #include "base/values.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/repost_form_warning_controller.h" |
| 16 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
| 17 #include "chrome/browser/ui/webui/constrained_html_ui.h" |
| 18 #include "chrome/browser/ui/webui/html_dialog_ui.h" |
| 19 #include "chrome/common/jstemplate_builder.h" |
| 20 #include "chrome/common/url_constants.h" |
| 21 #include "content/browser/tab_contents/tab_contents.h" |
| 22 #include "grit/browser_resources.h" |
| 23 #include "grit/generated_resources.h" |
| 24 #include "ui/base/l10n/l10n_util.h" |
| 25 #include "ui/base/resource/resource_bundle.h" |
| 26 #include "ui/gfx/size.h" |
| 27 |
| 28 using std::string; |
| 29 |
| 30 namespace browser { |
| 31 |
| 32 // Declared in browser_dialogs.h so others don't have to depend on our header. |
| 33 void ShowRepostFormWarningDialog(gfx::NativeWindow parent_window, |
| 34 TabContents* tab_contents) { |
| 35 new RepostFormWarningUI(parent_window, tab_contents); |
| 36 } |
| 37 |
| 38 } // namespace browser |
| 39 |
| 40 class RepostFormWarningSource : public ChromeURLDataManager::DataSource { |
| 41 public: |
| 42 RepostFormWarningSource() |
| 43 : DataSource(chrome::kChromeUIRepostFormWarningHost, |
| 44 MessageLoop::current()) { |
| 45 } |
| 46 |
| 47 virtual void StartDataRequest(const std::string& path, |
| 48 bool is_off_the_record, |
| 49 int request_id) OVERRIDE { |
| 50 DictionaryValue dict; |
| 51 dict.SetString("explanation", |
| 52 l10n_util::GetStringUTF16(IDS_HTTP_POST_WARNING)); |
| 53 dict.SetString("resend", |
| 54 l10n_util::GetStringUTF16(IDS_HTTP_POST_WARNING_RESEND)); |
| 55 dict.SetString("cancel", l10n_util::GetStringUTF16(IDS_CANCEL)); |
| 56 |
| 57 SetFontAndTextDirection(&dict); |
| 58 base::StringPiece html = |
| 59 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 60 IDR_REPOST_FORM_WARNING_HTML); |
| 61 string response = jstemplate_builder::GetI18nTemplateHtml(html, &dict); |
| 62 SendResponse(request_id, base::RefCountedString::TakeString(&response)); |
| 63 } |
| 64 |
| 65 virtual string GetMimeType(const std::string& path) const OVERRIDE { |
| 66 return "text/html"; |
| 67 } |
| 68 |
| 69 static void RegisterDataSource(Profile* profile) { |
| 70 ChromeURLDataManager* url_manager = profile->GetChromeURLDataManager(); |
| 71 url_manager->AddDataSource(new RepostFormWarningSource()); |
| 72 } |
| 73 |
| 74 private: |
| 75 virtual ~RepostFormWarningSource() {} |
| 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(RepostFormWarningSource); |
| 78 }; |
| 79 |
| 80 class RepostFormWarningHtmlDelegate : public HtmlDialogUIDelegate { |
| 81 public: |
| 82 explicit RepostFormWarningHtmlDelegate(RepostFormWarningUI* ui) : ui_(ui) {} |
| 83 |
| 84 virtual ~RepostFormWarningHtmlDelegate() {} |
| 85 |
| 86 // HtmlDialogUIDelegate implementation. |
| 87 virtual bool IsDialogModal() const OVERRIDE { |
| 88 return true; |
| 89 } |
| 90 |
| 91 virtual string16 GetDialogTitle() const OVERRIDE { |
| 92 return l10n_util::GetStringUTF16(IDS_HTTP_POST_WARNING_TITLE); |
| 93 } |
| 94 |
| 95 virtual GURL GetDialogContentURL() const OVERRIDE { |
| 96 return GURL(chrome::kChromeUIRepostFormWarningURL); |
| 97 } |
| 98 |
| 99 virtual void GetWebUIMessageHandlers( |
| 100 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE {} |
| 101 |
| 102 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE { |
| 103 size->SetSize(kDialogWidth, kDialogHeight); |
| 104 } |
| 105 |
| 106 virtual std::string GetDialogArgs() const OVERRIDE { |
| 107 return string(); |
| 108 } |
| 109 |
| 110 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { |
| 111 bool repost = false; |
| 112 if (!json_retval.empty()) { |
| 113 base::JSONReader reader; |
| 114 scoped_ptr<Value> value(reader.JsonToValue(json_retval, false, false)); |
| 115 if (!value.get() || !value->GetAsBoolean(&repost)) |
| 116 NOTREACHED() << "Missing or unreadable response from dialog"; |
| 117 } |
| 118 |
| 119 ui_->OnDialogClosed(repost); |
| 120 ui_ = NULL; |
| 121 delete this; |
| 122 } |
| 123 |
| 124 virtual void OnCloseContents(TabContents* source, |
| 125 bool* out_close_dialog) OVERRIDE {} |
| 126 |
| 127 virtual bool ShouldShowDialogTitle() const OVERRIDE { |
| 128 return true; |
| 129 } |
| 130 |
| 131 private: |
| 132 static const int kDialogWidth = 400; |
| 133 static const int kDialogHeight = 108; |
| 134 |
| 135 RepostFormWarningUI* ui_; // not owned |
| 136 |
| 137 DISALLOW_COPY_AND_ASSIGN(RepostFormWarningHtmlDelegate); |
| 138 }; |
| 139 |
| 140 RepostFormWarningUI::RepostFormWarningUI(gfx::NativeWindow parent_window, |
| 141 TabContents* tab_contents) |
| 142 : controller_(new RepostFormWarningController(tab_contents)) { |
| 143 Profile* profile = |
| 144 Profile::FromBrowserContext(tab_contents->browser_context()); |
| 145 RepostFormWarningSource::RegisterDataSource(profile); |
| 146 RepostFormWarningHtmlDelegate* delegate = |
| 147 new RepostFormWarningHtmlDelegate(this); |
| 148 ConstrainedHtmlUI::CreateConstrainedHtmlDialog( |
| 149 profile, delegate, tab_contents); |
| 150 } |
| 151 |
| 152 RepostFormWarningUI::~RepostFormWarningUI() {} |
| 153 |
| 154 void RepostFormWarningUI::OnDialogClosed(bool repost) { |
| 155 if (repost) |
| 156 controller_->Continue(); |
| 157 else |
| 158 controller_->Cancel(); |
| 159 delete this; |
| 160 } |
OLD | NEW |