| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/jsmessage_box_handler.h" | 5 #include "chrome/browser/jsmessage_box_handler.h" |
| 6 | 6 |
| 7 #include "app/gfx/text_elider.h" | |
| 8 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 9 #include "app/message_box_flags.h" | 8 #include "app/message_box_flags.h" |
| 10 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 11 #include "chrome/browser/app_modal_dialog_queue.h" | 10 #include "chrome/browser/app_modal_dialog_queue.h" |
| 12 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/profile.h" | |
| 14 #include "chrome/browser/tab_contents/tab_contents.h" | 12 #include "chrome/browser/tab_contents/tab_contents.h" |
| 15 #include "chrome/common/pref_names.h" | |
| 16 #include "chrome/common/pref_service.h" | |
| 17 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 18 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
| 19 | 15 |
| 20 namespace { | 16 namespace { |
| 21 | 17 |
| 22 const size_t kMaxReasonableTextLength = 2048; | 18 const size_t kMaxReasonableTextLength = 2048; |
| 23 | 19 |
| 24 // In some platforms, the underlying processing of humongous strings takes too | 20 // In some platforms, the underlying processing of humongous strings takes too |
| 25 // long and thus make the UI thread unresponsive. | 21 // long and thus make the UI thread unresponsive. |
| 26 std::wstring MakeTextSafe(const std::wstring& text) { | 22 std::wstring MakeTextSafe(const std::wstring& text) { |
| 27 if (text.size() > kMaxReasonableTextLength) | 23 if (text.size() > kMaxReasonableTextLength) |
| 28 return text.substr(0, kMaxReasonableTextLength) + L"\x2026"; | 24 return text.substr(0, kMaxReasonableTextLength) + L"\x2026"; |
| 29 return text; | 25 return text; |
| 30 } | 26 } |
| 31 | 27 |
| 32 std::wstring GetWindowTitle(TabContents* tab_contents, const GURL& frame_url, | |
| 33 int dialog_flags) { | |
| 34 bool is_alert = (dialog_flags == MessageBoxFlags::kIsJavascriptAlert); | |
| 35 if (!frame_url.has_host()) | |
| 36 return l10n_util::GetString( | |
| 37 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE | |
| 38 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE); | |
| 39 | |
| 40 // We really only want the scheme, hostname, and port. | |
| 41 GURL::Replacements replacements; | |
| 42 replacements.ClearUsername(); | |
| 43 replacements.ClearPassword(); | |
| 44 replacements.ClearPath(); | |
| 45 replacements.ClearQuery(); | |
| 46 replacements.ClearRef(); | |
| 47 GURL clean_url = frame_url.ReplaceComponents(replacements); | |
| 48 | |
| 49 // TODO(brettw) it should be easier than this to do the correct language | |
| 50 // handling without getting the accept language from the profile. | |
| 51 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0, | |
| 52 tab_contents->profile()->GetPrefs()->GetString(prefs::kAcceptLanguages)); | |
| 53 // Force URL to have LTR directionality. | |
| 54 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) | |
| 55 l10n_util::WrapStringWithLTRFormatting(&base_address); | |
| 56 return l10n_util::GetStringF( | |
| 57 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE, | |
| 58 base_address); | |
| 59 } | |
| 60 | |
| 61 } // namespace | 28 } // namespace |
| 62 | 29 |
| 63 void RunJavascriptMessageBox(TabContents* tab_contents, | 30 void RunJavascriptMessageBox(JavaScriptMessageBoxClient* client, |
| 64 const GURL& frame_url, | 31 const GURL& frame_url, |
| 65 int dialog_flags, | 32 int dialog_flags, |
| 66 const std::wstring& message_text, | 33 const std::wstring& message_text, |
| 67 const std::wstring& default_prompt_text, | 34 const std::wstring& default_prompt_text, |
| 68 bool display_suppress_checkbox, | 35 bool display_suppress_checkbox, |
| 69 IPC::Message* reply_msg) { | 36 IPC::Message* reply_msg) { |
| 70 std::wstring title = GetWindowTitle(tab_contents, frame_url, dialog_flags); | 37 std::wstring title = client->GetMessageBoxTitle(frame_url, |
| 38 (dialog_flags == MessageBoxFlags::kIsJavascriptAlert)); |
| 71 Singleton<AppModalDialogQueue>()->AddDialog( | 39 Singleton<AppModalDialogQueue>()->AddDialog( |
| 72 new AppModalDialog(tab_contents, title, | 40 new AppModalDialog(client, title, |
| 73 dialog_flags, MakeTextSafe(message_text), | 41 dialog_flags, MakeTextSafe(message_text), |
| 74 default_prompt_text, display_suppress_checkbox, | 42 default_prompt_text, display_suppress_checkbox, |
| 75 false, reply_msg)); | 43 false, reply_msg)); |
| 76 } | 44 } |
| 77 | 45 |
| 78 void RunBeforeUnloadDialog(TabContents* tab_contents, | 46 void RunBeforeUnloadDialog(TabContents* tab_contents, |
| 79 const std::wstring& message_text, | 47 const std::wstring& message_text, |
| 80 IPC::Message* reply_msg) { | 48 IPC::Message* reply_msg) { |
| 81 std::wstring full_message = | 49 std::wstring full_message = |
| 82 message_text + L"\n\n" + | 50 message_text + L"\n\n" + |
| 83 l10n_util::GetString(IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); | 51 l10n_util::GetString(IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); |
| 84 Singleton<AppModalDialogQueue>()->AddDialog(new AppModalDialog( | 52 Singleton<AppModalDialogQueue>()->AddDialog(new AppModalDialog( |
| 85 tab_contents, l10n_util::GetString(IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE), | 53 tab_contents, l10n_util::GetString(IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE), |
| 86 MessageBoxFlags::kIsJavascriptConfirm, MakeTextSafe(message_text), | 54 MessageBoxFlags::kIsJavascriptConfirm, MakeTextSafe(message_text), |
| 87 std::wstring(), false, true, reply_msg)); | 55 std::wstring(), false, true, reply_msg)); |
| 88 } | 56 } |
| OLD | NEW |