1 // Copyright (c) 2010 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/app_modal_dialogs/message_box_handler.h" 6 7 #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" 11 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" 13 #include "chrome/browser/ui/app_modal_dialogs/js_modal_dialog.h" 14 #include "chrome/common/pref_names.h" 15 #include "chrome/common/url_constants.h" 16 #include "content/browser/tab_contents/tab_contents.h" 17 #include "googleurl/src/gurl.h" 18 #include "grit/generated_resources.h" 19 #include "grit/chromium_strings.h" 20 #include "ui/base/l10n/l10n_util.h" 21 #include "ui/base/message_box_flags.h" 22 #include "ui/base/text/text_elider.h" 23 #include "ui/gfx/font.h" 24 25 static std::wstring GetTitle(Profile* profile, 26 bool is_alert, 27 const GURL& frame_url) { 28 ExtensionService* extensions_service = profile->GetExtensionService(); 29 if (extensions_service) { 30 const Extension* extension = 31 extensions_service->GetExtensionByURL(frame_url); 32 if (!extension) 33 extension = extensions_service->GetExtensionByWebExtent(frame_url); 34 35 if (extension && (extension->location() == Extension::COMPONENT)) { 36 return UTF16ToWideHack(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)); 37 } else if (extension && !extension->name().empty()) { 38 return UTF8ToWide(extension->name()); 39 } 40 } 41 if (!frame_url.has_host()) { 42 return UTF16ToWideHack(l10n_util::GetStringUTF16( 43 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE 44 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE)); 45 } 46 47 // TODO(brettw) it should be easier than this to do the correct language 48 // handling without getting the accept language from the profile. 49 string16 base_address = ui::ElideUrl(frame_url.GetOrigin(), 50 gfx::Font(), 0, profile->GetPrefs()->GetString(prefs::kAcceptLanguages)); 51 52 // Force URL to have LTR directionality. 53 base_address = base::i18n::GetDisplayStringInLTRDirectionality( 54 base_address); 55 56 return UTF16ToWide(l10n_util::GetStringFUTF16( 57 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : 58 IDS_JAVASCRIPT_MESSAGEBOX_TITLE, 59 base_address)); 60 } 61 62 void RunJavascriptMessageBox(Profile* profile, 63 JavaScriptAppModalDialogDelegate* delegate, 64 const GURL& frame_url, 65 int dialog_flags, 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 } 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 } 93