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