Chromium Code Reviews| 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("resend", | |
| 52 l10n_util::GetStringUTF16(IDS_HTTP_POST_WARNING_RESEND)); | |
| 53 dict.SetString("cancel", l10n_util::GetStringUTF16(IDS_CANCEL)); | |
| 54 | |
| 55 SetFontAndTextDirection(&dict); | |
| 56 base::StringPiece html = | |
| 57 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 58 IDR_REPOST_FORM_WARNING_HTML); | |
| 59 string response = jstemplate_builder::GetI18nTemplateHtml(html, &dict); | |
| 60 SendResponse(request_id, base::RefCountedString::TakeString(&response)); | |
| 61 } | |
| 62 | |
| 63 virtual string GetMimeType(const std::string& path) const OVERRIDE { | |
| 64 return "text/html"; | |
| 65 } | |
| 66 | |
| 67 static void RegisterDataSource(Profile* profile) { | |
| 68 ChromeURLDataManager* url_manager = profile->GetChromeURLDataManager(); | |
| 69 url_manager->AddDataSource(new RepostFormWarningSource()); | |
| 70 } | |
| 71 | |
| 72 private: | |
| 73 virtual ~RepostFormWarningSource() {} | |
| 74 | |
| 75 DISALLOW_COPY_AND_ASSIGN(RepostFormWarningSource); | |
| 76 }; | |
| 77 | |
| 78 class RepostFormWarningHtmlDelegate : public HtmlDialogUIDelegate { | |
| 79 public: | |
| 80 explicit RepostFormWarningHtmlDelegate(RepostFormWarningUI* ui) : ui_(ui) {} | |
| 81 | |
| 82 virtual ~RepostFormWarningHtmlDelegate() {} | |
| 83 | |
| 84 // HtmlDialogUIDelegate implementation. | |
| 85 virtual bool IsDialogModal() const OVERRIDE { | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 89 virtual string16 GetDialogTitle() const OVERRIDE { | |
| 90 return l10n_util::GetStringUTF16(IDS_HTTP_POST_WARNING_TITLE); | |
| 91 } | |
| 92 | |
| 93 virtual GURL GetDialogContentURL() const OVERRIDE { | |
| 94 return GURL(chrome::kChromeUIRepostFormWarningURL); | |
| 95 } | |
| 96 | |
| 97 virtual void GetWebUIMessageHandlers( | |
| 98 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE {} | |
| 99 | |
| 100 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE { | |
| 101 size->SetSize(kDialogWidth, kDialogHeight); | |
| 102 } | |
| 103 | |
| 104 virtual std::string GetDialogArgs() const OVERRIDE { | |
| 105 return UTF16ToUTF8(l10n_util::GetStringUTF16(IDS_HTTP_POST_WARNING)); | |
|
arv (Not doing code reviews)
2011/09/07 20:58:20
It seems a bit strange to use dialog args here sin
Daniel Erat
2011/09/08 01:46:56
Done.
| |
| 106 } | |
| 107 | |
| 108 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { | |
| 109 bool repost = false; | |
| 110 if (!json_retval.empty()) { | |
| 111 scoped_ptr<Value> value(base::JSONReader::Read(json_retval, false)); | |
| 112 ListValue* list; | |
| 113 if (!value.get() || !value->GetAsList(&list) || !list || | |
| 114 !list->GetBoolean(0, &repost)) | |
| 115 NOTREACHED() << "Missing or unreadable response from dialog"; | |
| 116 } | |
| 117 | |
| 118 ui_->OnDialogClosed(repost); | |
| 119 ui_ = NULL; | |
| 120 delete this; | |
| 121 } | |
| 122 | |
| 123 virtual void OnCloseContents(TabContents* source, | |
| 124 bool* out_close_dialog) OVERRIDE {} | |
| 125 | |
| 126 virtual bool ShouldShowDialogTitle() const OVERRIDE { | |
| 127 return true; | |
| 128 } | |
| 129 | |
| 130 private: | |
| 131 static const int kDialogWidth = 400; | |
| 132 static const int kDialogHeight = 108; | |
| 133 | |
| 134 RepostFormWarningUI* ui_; // not owned | |
| 135 | |
| 136 DISALLOW_COPY_AND_ASSIGN(RepostFormWarningHtmlDelegate); | |
| 137 }; | |
| 138 | |
| 139 RepostFormWarningUI::RepostFormWarningUI(gfx::NativeWindow parent_window, | |
| 140 TabContents* tab_contents) | |
| 141 : controller_(new RepostFormWarningController(tab_contents)) { | |
| 142 Profile* profile = | |
| 143 Profile::FromBrowserContext(tab_contents->browser_context()); | |
| 144 RepostFormWarningSource::RegisterDataSource(profile); | |
| 145 RepostFormWarningHtmlDelegate* delegate = | |
| 146 new RepostFormWarningHtmlDelegate(this); | |
| 147 ConstrainedHtmlUI::CreateConstrainedHtmlDialog( | |
| 148 profile, delegate, tab_contents); | |
| 149 } | |
| 150 | |
| 151 RepostFormWarningUI::~RepostFormWarningUI() {} | |
| 152 | |
| 153 void RepostFormWarningUI::OnDialogClosed(bool repost) { | |
| 154 if (repost) | |
| 155 controller_->Continue(); | |
| 156 else | |
| 157 controller_->Cancel(); | |
| 158 delete this; | |
| 159 } | |
| OLD | NEW |