Chromium Code Reviews| Index: chrome/browser/ui/app_modal_dialogs/message_box_handler.cc |
| diff --git a/chrome/browser/ui/app_modal_dialogs/message_box_handler.cc b/chrome/browser/ui/app_modal_dialogs/message_box_handler.cc |
| index 0de1b9affe486e3553e362b774908d0b68a51f4f..c4ae7b5078c5e31c400ebffd4d59bd665d6bc5f1 100644 |
| --- a/chrome/browser/ui/app_modal_dialogs/message_box_handler.cc |
| +++ b/chrome/browser/ui/app_modal_dialogs/message_box_handler.cc |
| @@ -1,20 +1,23 @@ |
| -// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| #include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h" |
| -#include "base/i18n/rtl.h" |
| +#include <map> |
| + |
| +#include "base/compiler_specific.h" |
| +#include "base/memory/singleton.h" |
| +#include "base/time.h" |
| #include "base/utf_string_conversions.h" |
| -#include "build/build_config.h" |
| +#include "base/i18n/rtl.h" |
| #include "chrome/browser/extensions/extension_service.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" |
| #include "chrome/browser/ui/app_modal_dialogs/js_modal_dialog.h" |
| +#include "chrome/common/chrome_constants.h" |
| #include "chrome/common/pref_names.h" |
| -#include "chrome/common/url_constants.h" |
| -#include "content/browser/tab_contents/tab_contents.h" |
| -#include "googleurl/src/gurl.h" |
| +#include "content/browser/javascript_dialogs.h" |
| #include "grit/generated_resources.h" |
| #include "grit/chromium_strings.h" |
| #include "ui/base/l10n/l10n_util.h" |
| @@ -22,9 +25,132 @@ |
| #include "ui/base/text/text_elider.h" |
| #include "ui/gfx/font.h" |
| -static std::wstring GetTitle(Profile* profile, |
| - bool is_alert, |
| - const GURL& frame_url) { |
| +class JavaScriptDialogCreatorImpl : public content::JavaScriptDialogCreator { |
|
jam
2011/06/03 00:21:45
nit: ChromeJavaScriptDialogCreator for consistency
|
| + public: |
| + static JavaScriptDialogCreatorImpl* GetInstance(); |
|
jam
2011/06/03 00:21:45
nit: if you're going to have the class in the cc f
|
| + |
| + virtual void RunJavaScriptDialog(content::JavaScriptDialogDelegate* delegate, |
| + const GURL& frame_url, |
| + int dialog_flags, |
| + const string16& message_text, |
| + const string16& default_prompt_text, |
| + IPC::Message* reply_message, |
| + bool* did_suppress_message, |
| + Profile* profile) OVERRIDE; |
| + |
| + virtual void RunBeforeUnloadDialog( |
| + content::JavaScriptDialogDelegate* delegate, |
| + const string16& message_text, |
| + IPC::Message* reply_message) OVERRIDE; |
| + |
| + virtual void ResetJavaScriptState( |
| + content::JavaScriptDialogDelegate* delegate) OVERRIDE; |
| + |
| + private: |
| + explicit JavaScriptDialogCreatorImpl(); |
| + virtual ~JavaScriptDialogCreatorImpl(); |
| + |
| + friend struct DefaultSingletonTraits<JavaScriptDialogCreatorImpl>; |
| + |
| + string16 GetTitle(Profile* profile, |
| + bool is_alert, |
| + const GURL& frame_url); |
| + |
| + // Mapping between the JavaScriptDialogDelegates and their extra data. The key |
| + // is a void* because the pointer is just a cookie and is never dereferenced. |
| + typedef std::map<void*, ChromeJavaScriptDialogExtraData> |
| + JavaScriptDialogExtraDataMap; |
| + JavaScriptDialogExtraDataMap javascript_dialog_extra_data_; |
| +}; |
| + |
| +//------------------------------------------------------------------------------ |
| + |
| +JavaScriptDialogCreatorImpl::JavaScriptDialogCreatorImpl() { |
| +} |
| + |
| +JavaScriptDialogCreatorImpl::~JavaScriptDialogCreatorImpl() { |
| +} |
| + |
| +/* static */ |
| +JavaScriptDialogCreatorImpl* JavaScriptDialogCreatorImpl::GetInstance() { |
| + return Singleton<JavaScriptDialogCreatorImpl>::get(); |
| +} |
| + |
| +void JavaScriptDialogCreatorImpl::RunJavaScriptDialog( |
| + content::JavaScriptDialogDelegate* delegate, |
| + const GURL& frame_url, |
| + int dialog_flags, |
| + const string16& message_text, |
| + const string16& default_prompt_text, |
| + IPC::Message* reply_message, |
| + bool* did_suppress_message, |
| + Profile* profile) { |
| + *did_suppress_message = false; |
| + |
| + ChromeJavaScriptDialogExtraData* extra_data = |
| + &javascript_dialog_extra_data_[delegate]; |
| + |
| + if (extra_data->suppress_javascript_messages_) { |
| + *did_suppress_message = true; |
| + return; |
| + } |
| + |
| + base::TimeDelta time_since_last_message = base::TimeTicks::Now() - |
| + extra_data->last_javascript_message_dismissal_; |
| + bool display_suppress_checkbox = false; |
| + // Show a checkbox offering to suppress further messages if this message is |
| + // being displayed within kJavascriptMessageExpectedDelay of the last one. |
| + if (time_since_last_message < |
| + base::TimeDelta::FromMilliseconds( |
| + chrome::kJavascriptMessageExpectedDelay)) { |
| + display_suppress_checkbox = true; |
| + } |
| + |
| + bool is_alert = dialog_flags == ui::MessageBoxFlags::kIsJavascriptAlert; |
| + string16 title = GetTitle(profile, is_alert, frame_url); |
| + |
| + AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( |
| + delegate, |
| + extra_data, |
| + title, |
| + dialog_flags, |
| + message_text, |
| + default_prompt_text, |
| + display_suppress_checkbox, |
| + false, // is_before_unload_dialog |
| + reply_message)); |
| +} |
| + |
| +void JavaScriptDialogCreatorImpl::RunBeforeUnloadDialog( |
| + content::JavaScriptDialogDelegate* delegate, |
| + const string16& message_text, |
| + IPC::Message* reply_message) { |
| + ChromeJavaScriptDialogExtraData* extra_data = |
| + &javascript_dialog_extra_data_[delegate]; |
| + |
| + string16 full_message = message_text + ASCIIToUTF16("\n\n") + |
| + l10n_util::GetStringUTF16(IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); |
| + |
| + AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( |
| + delegate, |
| + extra_data, |
| + l10n_util::GetStringUTF16(IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE), |
| + ui::MessageBoxFlags::kIsJavascriptConfirm, |
| + full_message, |
| + string16(), // default_prompt_text |
| + false, // display_suppress_checkbox |
| + true, // is_before_unload_dialog |
| + reply_message)); |
| +} |
| + |
| +void JavaScriptDialogCreatorImpl::ResetJavaScriptState( |
| + content::JavaScriptDialogDelegate* delegate) { |
| + javascript_dialog_extra_data_.erase(delegate); |
| +} |
| + |
| +string16 JavaScriptDialogCreatorImpl::GetTitle(Profile* profile, |
| + bool is_alert, |
| + const GURL& frame_url) { |
| ExtensionService* extensions_service = profile->GetExtensionService(); |
| if (extensions_service) { |
| const Extension* extension = |
| @@ -33,15 +159,15 @@ static std::wstring GetTitle(Profile* profile, |
| extension = extensions_service->GetExtensionByWebExtent(frame_url); |
| if (extension && (extension->location() == Extension::COMPONENT)) { |
| - return UTF16ToWideHack(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)); |
| + return l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
| } else if (extension && !extension->name().empty()) { |
| - return UTF8ToWide(extension->name()); |
| + return UTF8ToUTF16(extension->name()); |
| } |
| } |
| if (!frame_url.has_host()) { |
| - return UTF16ToWideHack(l10n_util::GetStringUTF16( |
| + return l10n_util::GetStringUTF16( |
| is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE |
| - : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE)); |
| + : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE); |
| } |
| // TODO(brettw) it should be easier than this to do the correct language |
| @@ -53,40 +179,14 @@ static std::wstring GetTitle(Profile* profile, |
| base_address = base::i18n::GetDisplayStringInLTRDirectionality( |
| base_address); |
| - return UTF16ToWide(l10n_util::GetStringFUTF16( |
| + return l10n_util::GetStringFUTF16( |
| is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : |
| IDS_JAVASCRIPT_MESSAGEBOX_TITLE, |
| - base_address)); |
| + base_address); |
| } |
| -void RunJavascriptMessageBox(Profile* profile, |
| - JavaScriptAppModalDialogDelegate* delegate, |
| - const GURL& frame_url, |
| - int dialog_flags, |
| - const std::wstring& message_text, |
| - const std::wstring& default_prompt_text, |
| - bool display_suppress_checkbox, |
| - IPC::Message* reply_msg) { |
| - bool is_alert = dialog_flags == ui::MessageBoxFlags::kIsJavascriptAlert; |
| - std::wstring title = GetTitle(profile, is_alert, frame_url); |
| - AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( |
| - delegate, title, dialog_flags, message_text, default_prompt_text, |
| - display_suppress_checkbox, false, reply_msg)); |
| -} |
| +//------------------------------------------------------------------------------ |
| -void RunBeforeUnloadDialog(TabContents* tab_contents, |
| - const std::wstring& message_text, |
| - IPC::Message* reply_msg) { |
| - std::wstring full_message = message_text + L"\n\n" + UTF16ToWideHack( |
| - l10n_util::GetStringUTF16(IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER)); |
| - AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( |
| - tab_contents, |
| - UTF16ToWideHack( |
| - l10n_util::GetStringUTF16(IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE)), |
| - ui::MessageBoxFlags::kIsJavascriptConfirm, |
| - message_text, |
| - std::wstring(), |
| - false, |
| - true, |
| - reply_msg)); |
| +content::JavaScriptDialogCreator* GetJavaScriptDialogCreatorInstance() { |
| + return JavaScriptDialogCreatorImpl::GetInstance(); |
| } |