| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/repost_form_warning_ui.h" | 5 #include "chrome/browser/ui/webui/tab_modal_dialog_ui.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/string_piece.h" | 12 #include "base/string_piece.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/repost_form_warning_controller.h" | 16 #include "chrome/browser/ui/constrained_window.h" |
| 17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 18 #include "chrome/browser/ui/tab_modal_dialog_delegate.h" |
| 18 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 19 #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/constrained_html_ui.h" |
| 20 #include "chrome/browser/ui/webui/html_dialog_ui.h" | 21 #include "chrome/browser/ui/webui/html_dialog_ui.h" |
| 21 #include "chrome/common/jstemplate_builder.h" | 22 #include "chrome/common/jstemplate_builder.h" |
| 22 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| 23 #include "content/browser/tab_contents/tab_contents.h" | 24 #include "content/browser/tab_contents/tab_contents.h" |
| 24 #include "grit/browser_resources.h" | 25 #include "grit/browser_resources.h" |
| 25 #include "grit/generated_resources.h" | 26 #include "grit/generated_resources.h" |
| 26 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
| 27 #include "ui/base/resource/resource_bundle.h" | 28 #include "ui/base/resource/resource_bundle.h" |
| 28 #include "ui/gfx/size.h" | 29 #include "ui/gfx/size.h" |
| 29 | 30 |
| 30 using std::string; | 31 using std::string; |
| 31 | 32 |
| 32 namespace browser { | 33 namespace browser { |
| 33 | 34 |
| 34 // Declared in browser_dialogs.h so others don't have to depend on our header. | 35 // Declared in browser_dialogs.h so others don't have to depend on our header. |
| 35 void ShowRepostFormWarningDialog(gfx::NativeWindow parent_window, | 36 void ShowTabModalDialog(TabModalDialogDelegate* delegate, |
| 36 TabContents* tab_contents) { | 37 gfx::NativeWindow parent_window, |
| 37 new RepostFormWarningUI(parent_window, tab_contents); | 38 TabContents* tab_contents) { |
| 39 new TabModalDialogUI(delegate, tab_contents); |
| 38 } | 40 } |
| 39 | 41 |
| 40 } // namespace browser | 42 } // namespace browser |
| 41 | 43 |
| 42 class RepostFormWarningSource : public ChromeURLDataManager::DataSource { | 44 class TabModalDialogSource : public ChromeURLDataManager::DataSource { |
| 43 public: | 45 public: |
| 44 RepostFormWarningSource() | 46 TabModalDialogSource() |
| 45 : DataSource(chrome::kChromeUIRepostFormWarningHost, | 47 : DataSource(chrome::kChromeUITabModalDialogHost, |
| 46 MessageLoop::current()) { | 48 MessageLoop::current()) { |
| 47 } | 49 } |
| 48 | 50 |
| 49 virtual void StartDataRequest(const std::string& path, | 51 virtual void StartDataRequest(const std::string& path, |
| 50 bool is_off_the_record, | 52 bool is_off_the_record, |
| 51 int request_id) OVERRIDE { | 53 int request_id) OVERRIDE { |
| 52 DictionaryValue dict; | 54 std::string html( |
| 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( | 55 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 62 IDR_REPOST_FORM_WARNING_HTML); | 56 IDR_TAB_MODAL_DIALOG_HTML).as_string()); |
| 63 string response = jstemplate_builder::GetI18nTemplateHtml(html, &dict); | 57 SendResponse(request_id, base::RefCountedString::TakeString(&html)); |
| 64 SendResponse(request_id, base::RefCountedString::TakeString(&response)); | |
| 65 } | 58 } |
| 66 | 59 |
| 67 virtual string GetMimeType(const std::string& path) const OVERRIDE { | 60 virtual string GetMimeType(const std::string& path) const OVERRIDE { |
| 68 return "text/html"; | 61 return "text/html"; |
| 69 } | 62 } |
| 70 | 63 |
| 71 static void RegisterDataSource(Profile* profile) { | 64 static void RegisterDataSource(Profile* profile) { |
| 72 ChromeURLDataManager* url_manager = profile->GetChromeURLDataManager(); | 65 ChromeURLDataManager* url_manager = profile->GetChromeURLDataManager(); |
| 73 url_manager->AddDataSource(new RepostFormWarningSource()); | 66 url_manager->AddDataSource(new TabModalDialogSource()); |
| 74 } | 67 } |
| 75 | 68 |
| 76 private: | 69 private: |
| 77 virtual ~RepostFormWarningSource() {} | 70 virtual ~TabModalDialogSource() {} |
| 78 | 71 |
| 79 DISALLOW_COPY_AND_ASSIGN(RepostFormWarningSource); | 72 DISALLOW_COPY_AND_ASSIGN(TabModalDialogSource); |
| 80 }; | 73 }; |
| 81 | 74 |
| 82 class RepostFormWarningHtmlDelegate : public HtmlDialogUIDelegate { | 75 class TabModalDialogHtmlDelegate : public HtmlDialogUIDelegate { |
| 83 public: | 76 public: |
| 84 explicit RepostFormWarningHtmlDelegate(RepostFormWarningUI* ui) : ui_(ui) {} | 77 TabModalDialogHtmlDelegate(TabModalDialogUI* ui, |
| 78 TabModalDialogDelegate* dialog_delegate) |
| 79 : ui_(ui), |
| 80 dialog_delegate_(dialog_delegate) {} |
| 85 | 81 |
| 86 virtual ~RepostFormWarningHtmlDelegate() {} | 82 virtual ~TabModalDialogHtmlDelegate() {} |
| 87 | 83 |
| 88 // HtmlDialogUIDelegate implementation. | 84 // HtmlDialogUIDelegate implementation. |
| 89 virtual bool IsDialogModal() const OVERRIDE { | 85 virtual bool IsDialogModal() const OVERRIDE { |
| 90 return true; | 86 return true; |
| 91 } | 87 } |
| 92 | 88 |
| 93 virtual string16 GetDialogTitle() const OVERRIDE { | 89 virtual string16 GetDialogTitle() const OVERRIDE { |
| 94 return l10n_util::GetStringUTF16(IDS_HTTP_POST_WARNING_TITLE); | 90 return dialog_delegate_->GetTitle(); |
| 95 } | 91 } |
| 96 | 92 |
| 97 virtual GURL GetDialogContentURL() const OVERRIDE { | 93 virtual GURL GetDialogContentURL() const OVERRIDE { |
| 98 return GURL(chrome::kChromeUIRepostFormWarningURL); | 94 return GURL(chrome::kChromeUITabModalDialogURL); |
| 99 } | 95 } |
| 100 | 96 |
| 101 virtual void GetWebUIMessageHandlers( | 97 virtual void GetWebUIMessageHandlers( |
| 102 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE {} | 98 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE {} |
| 103 | 99 |
| 104 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE { | 100 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE { |
| 105 size->SetSize(kDialogWidth, kDialogHeight); | 101 size->SetSize(kDialogWidth, kDialogHeight); |
| 106 } | 102 } |
| 107 | 103 |
| 108 virtual std::string GetDialogArgs() const OVERRIDE { | 104 virtual std::string GetDialogArgs() const OVERRIDE { |
| 109 return string(); | 105 DictionaryValue dict; |
| 106 dict.SetString("message", dialog_delegate_->GetMessage()); |
| 107 dict.SetString("accept", dialog_delegate_->GetAcceptButtonTitle()); |
| 108 dict.SetString("cancel", dialog_delegate_->GetCancelButtonTitle()); |
| 109 TabModalDialogSource::SetFontAndTextDirection(&dict); |
| 110 std::string json; |
| 111 base::JSONWriter::Write(&dict, false, &json); |
| 112 return json; |
| 110 } | 113 } |
| 111 | 114 |
| 112 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { | 115 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { |
| 113 bool repost = false; | 116 bool accepted = false; |
| 114 if (!json_retval.empty()) { | 117 if (!json_retval.empty()) { |
| 115 base::JSONReader reader; | 118 base::JSONReader reader; |
| 116 scoped_ptr<Value> value(reader.JsonToValue(json_retval, false, false)); | 119 scoped_ptr<Value> value(reader.JsonToValue(json_retval, false, false)); |
| 117 if (!value.get() || !value->GetAsBoolean(&repost)) | 120 DCHECK(value.get() && value->GetAsBoolean(&accepted)) |
| 118 NOTREACHED() << "Missing or unreadable response from dialog"; | 121 << "Missing or unreadable response from dialog"; |
| 119 } | 122 } |
| 120 | 123 |
| 121 ui_->OnDialogClosed(repost); | 124 ui_->OnDialogClosed(accepted); |
| 122 ui_ = NULL; | |
| 123 delete this; | 125 delete this; |
| 124 } | 126 } |
| 125 | 127 |
| 126 virtual void OnCloseContents(TabContents* source, | 128 virtual void OnCloseContents(TabContents* source, |
| 127 bool* out_close_dialog) OVERRIDE {} | 129 bool* out_close_dialog) OVERRIDE {} |
| 128 | 130 |
| 129 virtual bool ShouldShowDialogTitle() const OVERRIDE { | 131 virtual bool ShouldShowDialogTitle() const OVERRIDE { |
| 130 return true; | 132 return true; |
| 131 } | 133 } |
| 132 | 134 |
| 133 private: | 135 private: |
| 134 static const int kDialogWidth = 400; | 136 static const int kDialogWidth = 400; |
| 135 static const int kDialogHeight = 108; | 137 static const int kDialogHeight = 120; |
| 136 | 138 |
| 137 RepostFormWarningUI* ui_; // not owned | 139 scoped_ptr<TabModalDialogUI> ui_; |
| 140 // Owned by TabModalDialogUI, which we own. |
| 141 TabModalDialogDelegate* dialog_delegate_; |
| 138 | 142 |
| 139 DISALLOW_COPY_AND_ASSIGN(RepostFormWarningHtmlDelegate); | 143 DISALLOW_COPY_AND_ASSIGN(TabModalDialogHtmlDelegate); |
| 140 }; | 144 }; |
| 141 | 145 |
| 142 RepostFormWarningUI::RepostFormWarningUI(gfx::NativeWindow parent_window, | 146 TabModalDialogUI::TabModalDialogUI(TabModalDialogDelegate* delegate, |
| 143 TabContents* tab_contents) | 147 TabContents* tab_contents) |
| 144 : controller_(new RepostFormWarningController(tab_contents)) { | 148 : delegate_(delegate) { |
| 145 TabContentsWrapper* wrapper = | 149 TabContentsWrapper* wrapper = |
| 146 TabContentsWrapper::GetCurrentWrapperForContents(tab_contents); | 150 TabContentsWrapper::GetCurrentWrapperForContents(tab_contents); |
| 147 Profile* profile = wrapper->profile(); | 151 Profile* profile = wrapper->profile(); |
| 148 RepostFormWarningSource::RegisterDataSource(profile); | 152 TabModalDialogSource::RegisterDataSource(profile); |
| 149 RepostFormWarningHtmlDelegate* html_delegate = | 153 TabModalDialogHtmlDelegate* html_delegate = |
| 150 new RepostFormWarningHtmlDelegate(this); | 154 new TabModalDialogHtmlDelegate(this, delegate); |
| 151 ConstrainedHtmlUIDelegate* dialog_delegate = | 155 ConstrainedHtmlUIDelegate* dialog_delegate = |
| 152 ConstrainedHtmlUI::CreateConstrainedHtmlDialog( | 156 ConstrainedHtmlUI::CreateConstrainedHtmlDialog(profile, html_delegate, |
| 153 profile, html_delegate, wrapper); | 157 wrapper); |
| 154 controller_->set_window(dialog_delegate->window()); | 158 delegate_->set_window(dialog_delegate->window()); |
| 155 } | 159 } |
| 156 | 160 |
| 157 RepostFormWarningUI::~RepostFormWarningUI() {} | 161 TabModalDialogUI::~TabModalDialogUI() {} |
| 158 | 162 |
| 159 void RepostFormWarningUI::OnDialogClosed(bool repost) { | 163 void TabModalDialogUI::OnDialogClosed(bool accepted) { |
| 160 controller_->set_window(NULL); | 164 delegate_->set_window(NULL); |
| 161 if (repost) | 165 if (accepted) |
| 162 controller_->Continue(); | 166 delegate_->Accept(); |
| 163 else | 167 else |
| 164 controller_->Cancel(); | 168 delegate_->Cancel(); |
| 165 delete this; | |
| 166 } | 169 } |
| OLD | NEW |