message_box_handler.cc revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
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 "app/l10n_util.h" 8#include "app/message_box_flags.h" 9#include "app/text_elider.h" 10#include "base/i18n/rtl.h" 11#include "base/utf_string_conversions.h" 12#include "build/build_config.h" 13#include "chrome/browser/extensions/extension_service.h" 14#include "chrome/browser/profiles/profile.h" 15#include "chrome/browser/tab_contents/tab_contents.h" 16#include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" 17#include "chrome/browser/ui/app_modal_dialogs/js_modal_dialog.h" 18#include "chrome/common/pref_names.h" 19#include "chrome/common/url_constants.h" 20#include "gfx/font.h" 21#include "googleurl/src/gurl.h" 22#include "grit/generated_resources.h" 23#include "grit/chromium_strings.h" 24 25static 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 l10n_util::GetString(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 l10n_util::GetString( 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 = gfx::ElideUrl(frame_url.GetOrigin(), 50 gfx::Font(), 0, 51 UTF8ToWide(profile->GetPrefs()->GetString(prefs::kAcceptLanguages))); 52 53 // Force URL to have LTR directionality. 54 base_address = base::i18n::GetDisplayStringInLTRDirectionality( 55 base_address); 56 57 return UTF16ToWide(l10n_util::GetStringFUTF16( 58 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : 59 IDS_JAVASCRIPT_MESSAGEBOX_TITLE, 60 base_address)); 61} 62 63void RunJavascriptMessageBox(Profile* profile, 64 JavaScriptAppModalDialogDelegate* delegate, 65 const GURL& frame_url, 66 int dialog_flags, 67 const std::wstring& message_text, 68 const std::wstring& default_prompt_text, 69 bool display_suppress_checkbox, 70 IPC::Message* reply_msg) { 71 bool is_alert = dialog_flags == MessageBoxFlags::kIsJavascriptAlert; 72 std::wstring title = GetTitle(profile, is_alert, frame_url); 73 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( 74 delegate, title, dialog_flags, message_text, default_prompt_text, 75 display_suppress_checkbox, false, reply_msg)); 76} 77 78void RunBeforeUnloadDialog(TabContents* tab_contents, 79 const std::wstring& message_text, 80 IPC::Message* reply_msg) { 81 std::wstring full_message = 82 message_text + L"\n\n" + 83 l10n_util::GetString(IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); 84 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( 85 tab_contents, l10n_util::GetString(IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE), 86 MessageBoxFlags::kIsJavascriptConfirm, message_text, std::wstring(), 87 false, true, reply_msg)); 88} 89