Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 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/app_modal_dialogs/message_box_handler.h" | 5 #include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h" |
| 6 | 6 |
| 7 #include <map> | |
| 8 | |
| 9 #include "base/compiler_specific.h" | |
| 10 #include "base/memory/singleton.h" | |
| 11 #include "base/time.h" | |
| 12 #include "base/utf_string_conversions.h" | |
| 7 #include "base/i18n/rtl.h" | 13 #include "base/i18n/rtl.h" |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "build/build_config.h" | |
| 10 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" | 16 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" |
| 13 #include "chrome/browser/ui/app_modal_dialogs/js_modal_dialog.h" | 17 #include "chrome/browser/ui/app_modal_dialogs/js_modal_dialog.h" |
| 18 #include "chrome/common/chrome_constants.h" | |
| 14 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/common/url_constants.h" | 20 #include "content/browser/javascript_dialogs.h" |
| 16 #include "content/browser/tab_contents/tab_contents.h" | |
| 17 #include "googleurl/src/gurl.h" | |
| 18 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
| 19 #include "grit/chromium_strings.h" | 22 #include "grit/chromium_strings.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
| 21 #include "ui/base/message_box_flags.h" | 24 #include "ui/base/message_box_flags.h" |
| 22 #include "ui/base/text/text_elider.h" | 25 #include "ui/base/text/text_elider.h" |
| 23 #include "ui/gfx/font.h" | 26 #include "ui/gfx/font.h" |
| 24 | 27 |
| 25 static std::wstring GetTitle(Profile* profile, | 28 class JavaScriptDialogCreatorImpl : public content::JavaScriptDialogCreator { |
|
jam
2011/06/03 00:21:45
nit: ChromeJavaScriptDialogCreator for consistency
| |
| 26 bool is_alert, | 29 public: |
| 27 const GURL& frame_url) { | 30 static JavaScriptDialogCreatorImpl* GetInstance(); |
|
jam
2011/06/03 00:21:45
nit: if you're going to have the class in the cc f
| |
| 31 | |
| 32 virtual void RunJavaScriptDialog(content::JavaScriptDialogDelegate* delegate, | |
| 33 const GURL& frame_url, | |
| 34 int dialog_flags, | |
| 35 const string16& message_text, | |
| 36 const string16& default_prompt_text, | |
| 37 IPC::Message* reply_message, | |
| 38 bool* did_suppress_message, | |
| 39 Profile* profile) OVERRIDE; | |
| 40 | |
| 41 virtual void RunBeforeUnloadDialog( | |
| 42 content::JavaScriptDialogDelegate* delegate, | |
| 43 const string16& message_text, | |
| 44 IPC::Message* reply_message) OVERRIDE; | |
| 45 | |
| 46 virtual void ResetJavaScriptState( | |
| 47 content::JavaScriptDialogDelegate* delegate) OVERRIDE; | |
| 48 | |
| 49 private: | |
| 50 explicit JavaScriptDialogCreatorImpl(); | |
| 51 virtual ~JavaScriptDialogCreatorImpl(); | |
| 52 | |
| 53 friend struct DefaultSingletonTraits<JavaScriptDialogCreatorImpl>; | |
| 54 | |
| 55 string16 GetTitle(Profile* profile, | |
| 56 bool is_alert, | |
| 57 const GURL& frame_url); | |
| 58 | |
| 59 // Mapping between the JavaScriptDialogDelegates and their extra data. The key | |
| 60 // is a void* because the pointer is just a cookie and is never dereferenced. | |
| 61 typedef std::map<void*, ChromeJavaScriptDialogExtraData> | |
| 62 JavaScriptDialogExtraDataMap; | |
| 63 JavaScriptDialogExtraDataMap javascript_dialog_extra_data_; | |
| 64 }; | |
| 65 | |
| 66 //------------------------------------------------------------------------------ | |
| 67 | |
| 68 JavaScriptDialogCreatorImpl::JavaScriptDialogCreatorImpl() { | |
| 69 } | |
| 70 | |
| 71 JavaScriptDialogCreatorImpl::~JavaScriptDialogCreatorImpl() { | |
| 72 } | |
| 73 | |
| 74 /* static */ | |
| 75 JavaScriptDialogCreatorImpl* JavaScriptDialogCreatorImpl::GetInstance() { | |
| 76 return Singleton<JavaScriptDialogCreatorImpl>::get(); | |
| 77 } | |
| 78 | |
| 79 void JavaScriptDialogCreatorImpl::RunJavaScriptDialog( | |
| 80 content::JavaScriptDialogDelegate* delegate, | |
| 81 const GURL& frame_url, | |
| 82 int dialog_flags, | |
| 83 const string16& message_text, | |
| 84 const string16& default_prompt_text, | |
| 85 IPC::Message* reply_message, | |
| 86 bool* did_suppress_message, | |
| 87 Profile* profile) { | |
| 88 *did_suppress_message = false; | |
| 89 | |
| 90 ChromeJavaScriptDialogExtraData* extra_data = | |
| 91 &javascript_dialog_extra_data_[delegate]; | |
| 92 | |
| 93 if (extra_data->suppress_javascript_messages_) { | |
| 94 *did_suppress_message = true; | |
| 95 return; | |
| 96 } | |
| 97 | |
| 98 base::TimeDelta time_since_last_message = base::TimeTicks::Now() - | |
| 99 extra_data->last_javascript_message_dismissal_; | |
| 100 bool display_suppress_checkbox = false; | |
| 101 // Show a checkbox offering to suppress further messages if this message is | |
| 102 // being displayed within kJavascriptMessageExpectedDelay of the last one. | |
| 103 if (time_since_last_message < | |
| 104 base::TimeDelta::FromMilliseconds( | |
| 105 chrome::kJavascriptMessageExpectedDelay)) { | |
| 106 display_suppress_checkbox = true; | |
| 107 } | |
| 108 | |
| 109 bool is_alert = dialog_flags == ui::MessageBoxFlags::kIsJavascriptAlert; | |
| 110 string16 title = GetTitle(profile, is_alert, frame_url); | |
| 111 | |
| 112 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | |
| 113 delegate, | |
| 114 extra_data, | |
| 115 title, | |
| 116 dialog_flags, | |
| 117 message_text, | |
| 118 default_prompt_text, | |
| 119 display_suppress_checkbox, | |
| 120 false, // is_before_unload_dialog | |
| 121 reply_message)); | |
| 122 } | |
| 123 | |
| 124 void JavaScriptDialogCreatorImpl::RunBeforeUnloadDialog( | |
| 125 content::JavaScriptDialogDelegate* delegate, | |
| 126 const string16& message_text, | |
| 127 IPC::Message* reply_message) { | |
| 128 ChromeJavaScriptDialogExtraData* extra_data = | |
| 129 &javascript_dialog_extra_data_[delegate]; | |
| 130 | |
| 131 string16 full_message = message_text + ASCIIToUTF16("\n\n") + | |
| 132 l10n_util::GetStringUTF16(IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); | |
| 133 | |
| 134 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | |
| 135 delegate, | |
| 136 extra_data, | |
| 137 l10n_util::GetStringUTF16(IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE), | |
| 138 ui::MessageBoxFlags::kIsJavascriptConfirm, | |
| 139 full_message, | |
| 140 string16(), // default_prompt_text | |
| 141 false, // display_suppress_checkbox | |
| 142 true, // is_before_unload_dialog | |
| 143 reply_message)); | |
| 144 } | |
| 145 | |
| 146 void JavaScriptDialogCreatorImpl::ResetJavaScriptState( | |
| 147 content::JavaScriptDialogDelegate* delegate) { | |
| 148 javascript_dialog_extra_data_.erase(delegate); | |
| 149 } | |
| 150 | |
| 151 string16 JavaScriptDialogCreatorImpl::GetTitle(Profile* profile, | |
| 152 bool is_alert, | |
| 153 const GURL& frame_url) { | |
| 28 ExtensionService* extensions_service = profile->GetExtensionService(); | 154 ExtensionService* extensions_service = profile->GetExtensionService(); |
| 29 if (extensions_service) { | 155 if (extensions_service) { |
| 30 const Extension* extension = | 156 const Extension* extension = |
| 31 extensions_service->GetExtensionByURL(frame_url); | 157 extensions_service->GetExtensionByURL(frame_url); |
| 32 if (!extension) | 158 if (!extension) |
| 33 extension = extensions_service->GetExtensionByWebExtent(frame_url); | 159 extension = extensions_service->GetExtensionByWebExtent(frame_url); |
| 34 | 160 |
| 35 if (extension && (extension->location() == Extension::COMPONENT)) { | 161 if (extension && (extension->location() == Extension::COMPONENT)) { |
| 36 return UTF16ToWideHack(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)); | 162 return l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
| 37 } else if (extension && !extension->name().empty()) { | 163 } else if (extension && !extension->name().empty()) { |
| 38 return UTF8ToWide(extension->name()); | 164 return UTF8ToUTF16(extension->name()); |
| 39 } | 165 } |
| 40 } | 166 } |
| 41 if (!frame_url.has_host()) { | 167 if (!frame_url.has_host()) { |
| 42 return UTF16ToWideHack(l10n_util::GetStringUTF16( | 168 return l10n_util::GetStringUTF16( |
| 43 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE | 169 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE |
| 44 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE)); | 170 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE); |
| 45 } | 171 } |
| 46 | 172 |
| 47 // TODO(brettw) it should be easier than this to do the correct language | 173 // TODO(brettw) it should be easier than this to do the correct language |
| 48 // handling without getting the accept language from the profile. | 174 // handling without getting the accept language from the profile. |
| 49 string16 base_address = ui::ElideUrl(frame_url.GetOrigin(), | 175 string16 base_address = ui::ElideUrl(frame_url.GetOrigin(), |
| 50 gfx::Font(), 0, profile->GetPrefs()->GetString(prefs::kAcceptLanguages)); | 176 gfx::Font(), 0, profile->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
| 51 | 177 |
| 52 // Force URL to have LTR directionality. | 178 // Force URL to have LTR directionality. |
| 53 base_address = base::i18n::GetDisplayStringInLTRDirectionality( | 179 base_address = base::i18n::GetDisplayStringInLTRDirectionality( |
| 54 base_address); | 180 base_address); |
| 55 | 181 |
| 56 return UTF16ToWide(l10n_util::GetStringFUTF16( | 182 return l10n_util::GetStringFUTF16( |
| 57 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : | 183 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : |
| 58 IDS_JAVASCRIPT_MESSAGEBOX_TITLE, | 184 IDS_JAVASCRIPT_MESSAGEBOX_TITLE, |
| 59 base_address)); | 185 base_address); |
| 60 } | 186 } |
| 61 | 187 |
| 62 void RunJavascriptMessageBox(Profile* profile, | 188 //------------------------------------------------------------------------------ |
| 63 JavaScriptAppModalDialogDelegate* delegate, | 189 |
| 64 const GURL& frame_url, | 190 content::JavaScriptDialogCreator* GetJavaScriptDialogCreatorInstance() { |
| 65 int dialog_flags, | 191 return JavaScriptDialogCreatorImpl::GetInstance(); |
| 66 const std::wstring& message_text, | |
| 67 const std::wstring& default_prompt_text, | |
| 68 bool display_suppress_checkbox, | |
| 69 IPC::Message* reply_msg) { | |
| 70 bool is_alert = dialog_flags == ui::MessageBoxFlags::kIsJavascriptAlert; | |
| 71 std::wstring title = GetTitle(profile, is_alert, frame_url); | |
| 72 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | |
| 73 delegate, title, dialog_flags, message_text, default_prompt_text, | |
| 74 display_suppress_checkbox, false, reply_msg)); | |
| 75 } | 192 } |
| 76 | |
| 77 void RunBeforeUnloadDialog(TabContents* tab_contents, | |
| 78 const std::wstring& message_text, | |
| 79 IPC::Message* reply_msg) { | |
| 80 std::wstring full_message = message_text + L"\n\n" + UTF16ToWideHack( | |
| 81 l10n_util::GetStringUTF16(IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER)); | |
| 82 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | |
| 83 tab_contents, | |
| 84 UTF16ToWideHack( | |
| 85 l10n_util::GetStringUTF16(IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE)), | |
| 86 ui::MessageBoxFlags::kIsJavascriptConfirm, | |
| 87 message_text, | |
| 88 std::wstring(), | |
| 89 false, | |
| 90 true, | |
| 91 reply_msg)); | |
| 92 } | |
| OLD | NEW |