blob: eca0b18c4f6b3887200092492252b679f65c602d [file] [log] [blame]
[email protected]daa9e382012-01-06 00:30:341// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]7b5dc002010-11-16 23:08:105#include "chrome/browser/ui/browser.h"
[email protected]c61db1d2009-02-06 03:39:186
[email protected]aca02cf2010-05-03 18:56:017#if defined(OS_WIN)
[email protected]541434c2011-06-25 01:38:598#include <windows.h>
[email protected]dcd57762011-06-25 12:18:519#include <shellapi.h>
[email protected]aca02cf2010-05-03 18:56:0110#endif // OS_WIN
11
[email protected]5dcbc02f2010-01-26 22:32:0612#include <algorithm>
13#include <string>
14
[email protected]b02d0382009-11-30 21:19:5015#include "base/base_paths.h"
[email protected]24f1477532011-11-22 22:55:3616#include "base/bind.h"
[email protected]8a53ee042009-01-21 16:41:3317#include "base/command_line.h"
[email protected]8a53ee042009-01-21 16:41:3318#include "base/logging.h"
[email protected]49098f702011-10-13 03:47:1819#include "base/metrics/field_trial.h"
[email protected]835d7c82010-10-14 04:38:3820#include "base/metrics/histogram.h"
[email protected]b02d0382009-11-30 21:19:5021#include "base/path_service.h"
[email protected]0af8f132011-07-16 01:37:0222#include "base/string_number_conversions.h"
[email protected]b6a4ac2b2011-10-17 20:05:4823#include "base/string_util.h"
[email protected]5a17d492011-10-24 19:37:0324#include "base/stringprintf.h"
[email protected]34b99632011-01-01 01:01:0625#include "base/threading/thread.h"
26#include "base/threading/thread_restrictions.h"
[email protected]7f070d42011-03-09 20:25:3227#include "base/time.h"
[email protected]6524385ef2010-08-18 06:34:1328#include "base/utf_string_conversions.h"
[email protected]1a3aba82010-11-08 23:52:5429#include "chrome/app/chrome_command_ids.h"
[email protected]ea9edcb02011-09-23 22:05:0430#include "chrome/browser/autofill/personal_data_manager_factory.h"
[email protected]a07676b22011-06-17 16:36:5331#include "chrome/browser/background/background_contents_service.h"
[email protected]4fdf6742012-01-10 20:14:3632#include "chrome/browser/background/background_contents_service_factory.h"
[email protected]a9afddb2009-02-12 17:49:4233#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]b3ac5c82009-10-08 20:56:5434#include "chrome/browser/bookmarks/bookmark_utils.h"
[email protected]5c238752009-06-13 10:29:0735#include "chrome/browser/browser_process.h"
[email protected]c61db1d2009-02-06 03:39:1836#include "chrome/browser/browser_shutdown.h"
[email protected]40d59ce52009-03-06 23:20:1437#include "chrome/browser/character_encoding.h"
[email protected]0f083402011-11-22 02:59:0138#include "chrome/browser/chrome_page_zoom.h"
[email protected]55c87fa2011-10-15 07:28:2839#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]7d189022011-08-25 22:54:2040#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
41#include "chrome/browser/custom_handlers/register_protocol_handler_infobar_delegate.h"
[email protected]dce502762011-07-20 08:53:4942#include "chrome/browser/debugger/devtools_toggle_action.h"
43#include "chrome/browser/debugger/devtools_window.h"
[email protected]7fb4bbb2012-05-27 18:06:2244#include "chrome/browser/download/download_crx_util.h"
[email protected]59560e0b2009-06-04 03:30:2245#include "chrome/browser/download/download_item_model.h"
[email protected]9bb54ee2011-10-12 17:43:3546#include "chrome/browser/download/download_service.h"
47#include "chrome/browser/download/download_service_factory.h"
[email protected]a1bc3b82012-04-19 19:32:2048#include "chrome/browser/download/download_shelf.h"
[email protected]59560e0b2009-06-04 03:30:2249#include "chrome/browser/download/download_started_animation.h"
[email protected]a53209b2012-01-20 16:48:1650#include "chrome/browser/download/download_util.h"
[email protected]9c1662b2012-03-06 15:44:3351#include "chrome/browser/extensions/browser_extension_window_controller.h"
[email protected]bb461532010-11-26 21:50:2352#include "chrome/browser/extensions/crx_installer.h"
[email protected]49098f702011-10-13 03:47:1853#include "chrome/browser/extensions/default_apps_trial.h"
[email protected]10abd192010-09-30 02:03:4954#include "chrome/browser/extensions/extension_prefs.h"
[email protected]eaa7dd182010-12-14 11:09:0055#include "chrome/browser/extensions/extension_service.h"
[email protected]36fb2c7c2011-04-04 15:49:0856#include "chrome/browser/extensions/extension_tab_helper.h"
[email protected]b375c5d2011-05-03 21:15:0457#include "chrome/browser/favicon/favicon_tab_helper.h"
[email protected]3a29a6e2011-08-24 18:26:2158#include "chrome/browser/file_select_helper.h"
[email protected]820735792010-07-29 23:40:0159#include "chrome/browser/first_run/first_run.h"
[email protected]f7578f52010-08-30 22:22:4960#include "chrome/browser/google/google_url_tracker.h"
[email protected]7e204122011-09-01 18:56:2161#include "chrome/browser/infobars/infobar_tab_helper.h"
[email protected]6b723f82010-10-05 20:14:2762#include "chrome/browser/instant/instant_controller.h"
[email protected]5aab5e22010-12-08 22:13:2963#include "chrome/browser/instant/instant_unload_handler.h"
[email protected]7d189022011-08-25 22:54:2064#include "chrome/browser/intents/register_intent_handler_infobar_delegate.h"
[email protected]8f0bbd62012-02-22 03:37:1865#include "chrome/browser/intents/web_intents_util.h"
[email protected]2e6389f2012-05-18 19:41:2566#include "chrome/browser/lifetime/application_lifetime.h"
[email protected]4afde5c2012-06-25 17:00:5367#include "chrome/browser/media/media_stream_devices_controller.h"
[email protected]a239c3f2009-02-17 22:13:1968#include "chrome/browser/net/url_fixer_upper.h"
[email protected]2c4fb7b2011-04-02 06:33:2969#include "chrome/browser/notifications/notification_ui_manager.h"
[email protected]14a000d2010-04-29 21:44:2470#include "chrome/browser/platform_util.h"
[email protected]4e94ab32011-08-05 05:28:2771#include "chrome/browser/prefs/incognito_mode_prefs.h"
[email protected]37858e52010-08-26 00:22:0272#include "chrome/browser/prefs/pref_service.h"
[email protected]02337592010-09-27 18:38:2573#include "chrome/browser/printing/cloud_print/cloud_print_setup_flow.h"
[email protected]0996e9b2011-08-26 17:59:0174#include "chrome/browser/printing/print_preview_tab_controller.h"
[email protected]8ecad5e2010-12-02 21:18:3375#include "chrome/browser/profiles/profile.h"
[email protected]5e919242012-02-13 23:59:3576#include "chrome/browser/profiles/profile_destroyer.h"
[email protected]5a17d492011-10-24 19:37:0377#include "chrome/browser/profiles/profile_manager.h"
[email protected]a7be6982011-12-12 21:53:5778#include "chrome/browser/profiles/profile_metrics.h"
[email protected]67baffc82011-12-19 18:03:0779#include "chrome/browser/repost_form_warning_controller.h"
[email protected]81898992011-06-14 22:15:0080#include "chrome/browser/sessions/restore_tab_helper.h"
[email protected]85e921fb82009-02-11 23:19:4481#include "chrome/browser/sessions/session_service.h"
[email protected]92371eb2011-04-28 11:50:1582#include "chrome/browser/sessions/session_service_factory.h"
[email protected]c61db1d2009-02-06 03:39:1883#include "chrome/browser/sessions/session_types.h"
[email protected]92926d92010-09-02 18:35:0684#include "chrome/browser/sessions/tab_restore_service.h"
[email protected]92371eb2011-04-28 11:50:1585#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]066629f2009-10-17 00:28:1386#include "chrome/browser/sync/profile_sync_service.h"
[email protected]5e61afb92012-01-27 20:09:1387#include "chrome/browser/sync/profile_sync_service_factory.h"
[email protected]40f047972009-11-25 03:54:4088#include "chrome/browser/sync/sync_ui_util.h"
[email protected]2c4fb7b2011-04-02 06:33:2989#include "chrome/browser/tab_contents/background_contents.h"
[email protected]4fdf6742012-01-10 20:14:3690#include "chrome/browser/tab_contents/retargeting_details.h"
[email protected]7cceebac2011-03-03 00:32:2191#include "chrome/browser/tab_contents/simple_alert_infobar_delegate.h"
[email protected]1ab4ddf2011-07-21 04:48:0492#include "chrome/browser/tab_contents/tab_util.h"
[email protected]ffa6f592011-06-24 22:03:5793#include "chrome/browser/themes/theme_service.h"
94#include "chrome/browser/themes/theme_service_factory.h"
[email protected]82194b5fb2012-05-22 23:53:0795#include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_creator.h"
[email protected]e7cfdbd2011-04-22 14:41:3796#include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h"
[email protected]35699d572011-05-11 19:46:1497#include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
[email protected]5d98294912012-06-27 22:57:4098#include "chrome/browser/ui/browser_command_controller.h"
[email protected]a37d4b02012-06-25 21:56:1099#include "chrome/browser/ui/browser_commands.h"
[email protected]ef557022012-03-16 10:05:33100#include "chrome/browser/ui/browser_content_setting_bubble_model_delegate.h"
[email protected]55452902011-06-01 21:57:47101#include "chrome/browser/ui/browser_dialogs.h"
[email protected]d8748142012-05-16 21:13:43102#include "chrome/browser/ui/browser_finder.h"
[email protected]6768ac02011-04-06 17:41:04103#include "chrome/browser/ui/browser_list.h"
[email protected]fcca741b2011-06-17 22:46:37104#include "chrome/browser/ui/browser_navigator.h"
[email protected]fb7da4132011-03-15 15:51:00105#include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
[email protected]52877dbc62012-06-29 22:22:03106#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]ef557022012-03-16 10:05:33107#include "chrome/browser/ui/browser_toolbar_model_delegate.h"
[email protected]2cd4fde2012-06-26 03:10:26108#include "chrome/browser/ui/browser_ui_prefs.h"
[email protected]00070c732011-04-09 15:31:33109#include "chrome/browser/ui/browser_window.h"
[email protected]5d9cace72012-06-21 16:07:12110#include "chrome/browser/ui/chrome_pages.h"
[email protected]31145092011-09-30 01:49:44111#include "chrome/browser/ui/constrained_window_tab_helper.h"
[email protected]a44657202012-01-09 05:48:31112#include "chrome/browser/ui/extensions/shell_window.h"
[email protected]45300ad42010-12-02 15:51:14113#include "chrome/browser/ui/find_bar/find_bar.h"
114#include "chrome/browser/ui/find_bar/find_bar_controller.h"
[email protected]c90c6ca2011-02-16 20:11:38115#include "chrome/browser/ui/find_bar/find_tab_helper.h"
[email protected]9d5c059a82012-06-29 20:13:07116#include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
[email protected]1ba55cf2012-06-29 19:11:39117#include "chrome/browser/ui/global_error/global_error.h"
118#include "chrome/browser/ui/global_error/global_error_service.h"
119#include "chrome/browser/ui/global_error/global_error_service_factory.h"
[email protected]8be45842012-04-13 19:49:29120#include "chrome/browser/ui/hung_plugin_tab_helper.h"
[email protected]943b8612011-08-31 21:07:13121#include "chrome/browser/ui/intents/web_intent_picker_controller.h"
[email protected]f13b4202012-06-12 23:53:23122#include "chrome/browser/ui/media_stream_infobar_delegate.h"
[email protected]6a3ec2312010-12-02 19:30:19123#include "chrome/browser/ui/omnibox/location_bar.h"
[email protected]fe90ca8e2011-04-08 20:40:43124#include "chrome/browser/ui/panels/panel.h"
125#include "chrome/browser/ui/panels/panel_manager.h"
[email protected]e3a5d5b2012-06-22 17:56:46126#include "chrome/browser/ui/search/search.h"
127#include "chrome/browser/ui/search/search_delegate.h"
128#include "chrome/browser/ui/search/search_model.h"
[email protected]9219d262011-02-15 21:05:12129#include "chrome/browser/ui/search_engines/search_engine_tab_helper.h"
[email protected]5d9cace72012-06-21 16:07:12130#include "chrome/browser/ui/singleton_tabs.h"
[email protected]6a3ec2312010-12-02 19:30:19131#include "chrome/browser/ui/status_bubble.h"
[email protected]3da95a102011-11-30 21:47:45132#include "chrome/browser/ui/sync/browser_synced_window_delegate.h"
[email protected]51d83bdf2011-12-09 16:09:29133#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
[email protected]3d61dd02012-06-07 00:05:53134#include "chrome/browser/ui/tab_contents/tab_contents.h"
[email protected]9a800652010-12-02 17:08:44135#include "chrome/browser/ui/tabs/dock_info.h"
[email protected]6a3ec2312010-12-02 19:30:19136#include "chrome/browser/ui/tabs/tab_menu_model.h"
[email protected]b56e2e32012-05-11 21:18:04137#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]f847e6082011-03-24 00:08:26138#include "chrome/browser/ui/web_applications/web_app_ui.h"
[email protected]daa9e382012-01-06 00:30:34139#include "chrome/browser/ui/webui/feedback_ui.h"
[email protected]a9acdff2012-03-08 04:04:52140#include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
[email protected]65549182012-02-25 00:45:40141#include "chrome/browser/ui/webui/signin/login_ui_service.h"
142#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
[email protected]03639422012-06-28 19:03:37143#include "chrome/browser/ui/window_sizer/window_sizer.h"
[email protected]b1b73942010-05-26 20:11:54144#include "chrome/browser/upgrade_detector.h"
[email protected]86b54012009-11-19 09:18:50145#include "chrome/browser/web_applications/web_app.h"
[email protected]d938aed92009-01-22 19:49:33146#include "chrome/common/chrome_constants.h"
[email protected]432115822011-07-10 15:52:27147#include "chrome/common/chrome_notification_types.h"
[email protected]d938aed92009-01-22 19:49:33148#include "chrome/common/chrome_switches.h"
[email protected]7d189022011-08-25 22:54:20149#include "chrome/common/custom_handlers/protocol_handler.h"
[email protected]5b1a0e22009-05-26 19:00:58150#include "chrome/common/extensions/extension.h"
[email protected]46fd1ea42011-02-16 15:59:33151#include "chrome/common/extensions/extension_constants.h"
[email protected]8a53ee042009-01-21 16:41:33152#include "chrome/common/pref_names.h"
[email protected]7664ab32011-02-01 23:35:25153#include "chrome/common/profiling.h"
[email protected]b689fce72009-03-17 22:45:34154#include "chrome/common/url_constants.h"
[email protected]bb461532010-11-26 21:50:23155#include "chrome/common/web_apps.h"
[email protected]da8543762012-03-20 08:52:20156#include "content/public/browser/color_chooser.h"
[email protected]0e12d7d2011-12-01 16:21:44157#include "content/public/browser/devtools_manager.h"
[email protected]e582fdd2011-12-20 16:48:17158#include "content/public/browser/download_item.h"
159#include "content/public/browser/download_manager.h"
[email protected]cadaec52012-02-08 21:53:13160#include "content/public/browser/interstitial_page.h"
[email protected]d9083482012-01-06 00:38:46161#include "content/public/browser/invalidate_type.h"
[email protected]cdcb1dee2012-01-04 00:46:20162#include "content/public/browser/navigation_controller.h"
[email protected]022af742011-12-28 18:37:25163#include "content/public/browser/navigation_entry.h"
[email protected]7f6f44c2011-12-14 13:23:38164#include "content/public/browser/notification_details.h"
165#include "content/public/browser/notification_service.h"
[email protected]3a5180ae2011-12-21 02:39:38166#include "content/public/browser/plugin_service.h"
[email protected]9c1662b2012-03-06 15:44:33167#include "content/public/browser/render_process_host.h"
168#include "content/public/browser/render_view_host.h"
[email protected]b6583592012-01-25 19:52:33169#include "content/public/browser/site_instance.h"
[email protected]7f6f44c2011-12-14 13:23:38170#include "content/public/browser/user_metrics.h"
[email protected]d9083482012-01-06 00:38:46171#include "content/public/browser/web_contents.h"
[email protected]8643e6d2012-01-18 20:26:10172#include "content/public/browser/web_contents_view.h"
[email protected]0d9989d2011-12-21 20:26:00173#include "content/public/browser/web_intents_dispatcher.h"
[email protected]4573fbd2011-10-31 20:25:18174#include "content/public/common/content_restriction.h"
[email protected]c08950d22011-10-13 22:20:29175#include "content/public/common/content_switches.h"
[email protected]0f083402011-11-22 02:59:01176#include "content/public/common/page_zoom.h"
[email protected]cadaec52012-02-08 21:53:13177#include "content/public/common/renderer_preferences.h"
[email protected]a3e18c42009-03-04 23:36:05178#include "grit/chromium_strings.h"
179#include "grit/generated_resources.h"
180#include "grit/locale_settings.h"
[email protected]d952a052011-09-06 18:42:45181#include "grit/theme_resources_standard.h"
[email protected]8a53ee042009-01-21 16:41:33182#include "net/base/net_util.h"
183#include "net/base/registry_controlled_domain.h"
[email protected]4d7c4ef2012-03-16 01:47:12184#include "net/cookies/cookie_monster.h"
[email protected]8a53ee042009-01-21 16:41:33185#include "net/url_request/url_request_context.h"
[email protected]f67678062011-01-07 17:33:39186#include "ui/base/animation/animation.h"
[email protected]c051a1b2011-01-21 23:30:17187#include "ui/base/l10n/l10n_util.h"
[email protected]08397d52011-02-05 01:53:38188#include "ui/gfx/point.h"
[email protected]ce9751942011-09-21 01:57:24189#include "webkit/glue/web_intent_data.h"
[email protected]3a3b75a2012-06-01 08:38:36190#include "webkit/glue/web_intent_service_data.h"
[email protected]4a19be92011-09-22 14:25:02191#include "webkit/glue/webkit_glue.h"
[email protected]d938aed92009-01-22 19:49:33192#include "webkit/glue/window_open_disposition.h"
[email protected]3a5180ae2011-12-21 02:39:38193#include "webkit/plugins/webplugininfo.h"
[email protected]8a53ee042009-01-21 16:41:33194
195#if defined(OS_WIN)
[email protected]77cbe50e2012-06-14 02:44:38196#include "base/win/metro.h"
[email protected]36d5e5592010-11-15 20:45:59197#include "chrome/browser/autofill/autofill_ie_toolbar_import_win.h"
[email protected]12f520c2010-01-06 18:11:15198#include "chrome/browser/shell_integration.h"
[email protected]b17d41c2011-02-17 22:09:59199#include "chrome/browser/ssl/ssl_error_info.h"
[email protected]a5d1e1e2010-09-23 19:34:12200#include "chrome/browser/task_manager/task_manager.h"
[email protected]6a3ec2312010-12-02 19:30:19201#include "chrome/browser/ui/view_ids.h"
[email protected]dcd57762011-06-25 12:18:51202#include "ui/base/win/shell.h"
[email protected]8a53ee042009-01-21 16:41:33203#endif // OS_WIN
[email protected]e1acf6f2008-10-27 20:43:33204
[email protected]b7969202010-05-14 21:29:26205#if defined(OS_CHROMEOS)
[email protected]b7813a22012-04-04 18:41:02206#include "chrome/browser/chromeos/gdata/gdata_util.h"
[email protected]eab8c0f2011-11-18 22:33:50207#endif
208
[email protected]dc04be7c2012-03-15 23:57:49209#if defined(USE_ASH)
[email protected]bafda132012-02-16 19:32:51210#include "ash/ash_switches.h"
[email protected]c2f0a892012-01-24 22:19:21211#endif
212
[email protected]8a53ee042009-01-21 16:41:33213using base::TimeDelta;
[email protected]b87ee522012-05-18 15:16:54214using content::NativeWebKeyboardEvent;
[email protected]c5eed492012-01-04 17:07:50215using content::NavigationController;
[email protected]10f417c52011-12-28 21:04:23216using content::NavigationEntry;
[email protected]e5d549d2011-12-28 01:29:20217using content::OpenURLParams;
[email protected]3a5180ae2011-12-21 02:39:38218using content::PluginService;
[email protected]e5d549d2011-12-28 01:29:20219using content::Referrer;
[email protected]b6583592012-01-25 19:52:33220using content::SiteInstance;
[email protected]7f6f44c2011-12-14 13:23:38221using content::UserMetricsAction;
[email protected]ea049a02011-12-25 21:37:09222using content::WebContents;
[email protected]1c321ee52012-05-21 03:02:34223using extensions::Extension;
[email protected]20c07f8e2012-05-31 08:43:14224using ui::WebDialogDelegate;
initial.commit09911bf2008-07-26 23:55:29225
[email protected]36b6dcb2008-11-12 01:19:57226///////////////////////////////////////////////////////////////////////////////
initial.commit09911bf2008-07-26 23:55:29227
[email protected]505323e22009-01-24 02:47:58228namespace {
229
[email protected]91a79232010-11-10 18:15:08230// The URL to be loaded to display the "Report a broken page" form.
231const char kBrokenPageUrl[] =
[email protected]95fc6f92011-04-13 00:37:20232 "https://www.google.com/support/chrome/bin/request.py?contact_type="
[email protected]91a79232010-11-10 18:15:08233 "broken_website&format=inproduct&p.page_title=$1&p.page_url=$2";
234
235// The URL for the privacy dashboard.
236const char kPrivacyDashboardUrl[] = "https://www.google.com/dashboard";
237
238// How long we wait before updating the browser chrome while loading a page.
239const int kUIUpdateCoalescingTimeMS = 200;
240
[email protected]0da58292012-03-22 20:37:21241bool AllowPanels(const std::string& app_name) {
[email protected]cae97652012-04-20 03:12:12242 return PanelManager::ShouldUsePanels(
243 web_app::GetExtensionIdFromApplicationName(app_name));
[email protected]0da58292012-03-22 20:37:21244}
245
[email protected]505323e22009-01-24 02:47:58246} // namespace
247
[email protected]b35b26b32011-05-05 20:35:14248////////////////////////////////////////////////////////////////////////////////
249// Browser, CreateParams:
250
[email protected]da22aa62012-04-04 18:54:35251Browser::CreateParams::CreateParams()
252 : type(TYPE_TABBED),
253 profile(NULL),
254 initial_show_state(ui::SHOW_STATE_DEFAULT),
255 is_session_restore(false) {
256}
257
[email protected]b35b26b32011-05-05 20:35:14258Browser::CreateParams::CreateParams(Type type, Profile* profile)
259 : type(type),
[email protected]a636d8e52012-02-28 15:40:41260 profile(profile),
[email protected]0da58292012-03-22 20:37:21261 app_type(APP_TYPE_HOST),
[email protected]a636d8e52012-02-28 15:40:41262 initial_show_state(ui::SHOW_STATE_DEFAULT),
263 is_session_restore(false) {
[email protected]b35b26b32011-05-05 20:35:14264}
265
[email protected]da22aa62012-04-04 18:54:35266// static
267Browser::CreateParams Browser::CreateParams::CreateForApp(
268 Type type,
269 const std::string& app_name,
270 const gfx::Rect& window_bounds,
271 Profile* profile) {
272 DCHECK(type != TYPE_TABBED);
273 DCHECK(!app_name.empty());
274
275 if (type == TYPE_PANEL && !AllowPanels(app_name))
276 type = TYPE_POPUP;
277
278 CreateParams params(type, profile);
279 params.app_name = app_name;
280 params.app_type = APP_TYPE_CHILD;
281 params.initial_bounds = window_bounds;
282
283 return params;
284}
285
286// static
287Browser::CreateParams Browser::CreateParams::CreateForDevTools(
288 Profile* profile) {
289 CreateParams params(TYPE_POPUP, profile);
290 params.app_name = DevToolsWindow::kDevToolsApp;
291 return params;
292}
293
[email protected]36b6dcb2008-11-12 01:19:57294///////////////////////////////////////////////////////////////////////////////
295// Browser, Constructors, Creation, Showing:
initial.commit09911bf2008-07-26 23:55:29296
[email protected]299dabd2008-11-19 02:27:16297Browser::Browser(Type type, Profile* profile)
[email protected]15952e462008-11-14 00:29:05298 : type_(type),
299 profile_(profile),
[email protected]f3e99e32008-07-30 04:48:39300 window_(NULL),
[email protected]ecc523f62010-09-28 22:45:40301 ALLOW_THIS_IN_INITIALIZER_LIST(
[email protected]2fc15ae2012-05-06 00:01:37302 tab_strip_model_(new TabStripModel(this, profile))),
[email protected]0da58292012-03-22 20:37:21303 app_type_(APP_TYPE_HOST),
[email protected]15952e462008-11-14 00:29:05304 chrome_updater_factory_(this),
305 is_attempting_to_close_browser_(false),
[email protected]69444cc2009-04-09 20:40:06306 cancel_download_confirmation_state_(NOT_PROMPTED),
[email protected]1f0b50b2012-06-22 20:37:16307 initial_show_state_(ui::SHOW_STATE_DEFAULT),
[email protected]8bc061f2011-08-31 22:46:23308 is_session_restore_(false),
[email protected]24f1477532011-11-22 22:55:36309 weak_factory_(this),
[email protected]fb7da4132011-03-15 15:51:00310 pending_web_app_action_(NONE),
311 ALLOW_THIS_IN_INITIALIZER_LIST(
[email protected]ef557022012-03-16 10:05:33312 content_setting_bubble_model_delegate_(
313 new BrowserContentSettingBubbleModelDelegate(this))),
314 ALLOW_THIS_IN_INITIALIZER_LIST(
315 toolbar_model_delegate_(
316 new BrowserToolbarModelDelegate(this))),
317 ALLOW_THIS_IN_INITIALIZER_LIST(
[email protected]fb7da4132011-03-15 15:51:00318 tab_restore_service_delegate_(
[email protected]09b29342011-06-24 19:18:48319 new BrowserTabRestoreServiceDelegate(this))),
[email protected]afefa74e2011-07-26 05:04:23320 ALLOW_THIS_IN_INITIALIZER_LIST(
321 synced_window_delegate_(
322 new BrowserSyncedWindowDelegate(this))),
[email protected]8a5e0ca2011-08-25 06:30:47323 bookmark_bar_state_(BookmarkBar::HIDDEN),
[email protected]bd52a982012-06-26 23:34:29324 device_attached_intent_source_(this, this),
[email protected]5d98294912012-06-27 22:57:40325 ALLOW_THIS_IN_INITIALIZER_LIST(
326 command_controller_(new chrome::BrowserCommandController(this))),
[email protected]2f516c792011-09-19 22:22:09327 window_has_shown_(false) {
[email protected]2fc15ae2012-05-06 00:01:37328 tab_strip_model_->AddObserver(this);
329
[email protected]ef557022012-03-16 10:05:33330 toolbar_model_.reset(new ToolbarModel(toolbar_model_delegate_.get()));
[email protected]e3a5d5b2012-06-22 17:56:46331 search_model_.reset(new chrome::search::SearchModel(NULL));
332 search_delegate_.reset(
333 new chrome::search::SearchDelegate(search_model_.get()));
[email protected]ef557022012-03-16 10:05:33334
[email protected]432115822011-07-10 15:52:27335 registrar_.Add(this, content::NOTIFICATION_SSL_VISIBLE_STATE_CHANGED,
[email protected]ad50def52011-10-19 23:17:07336 content::NotificationService::AllSources());
[email protected]432115822011-07-10 15:52:27337 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
[email protected]a93089b12011-11-22 20:47:38338 content::Source<Profile>(profile_->GetOriginalProfile()));
[email protected]432115822011-07-10 15:52:27339 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]a93089b12011-11-22 20:47:38340 content::Source<Profile>(profile_->GetOriginalProfile()));
[email protected]432115822011-07-10 15:52:27341 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]a93089b12011-11-22 20:47:38342 content::Source<Profile>(profile_->GetOriginalProfile()));
[email protected]432115822011-07-10 15:52:27343 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07344 content::NotificationService::AllSources());
[email protected]6b40bb582012-03-15 20:50:38345#if defined(ENABLE_THEMES)
[email protected]ffa6f592011-06-24 22:03:57346 registrar_.Add(
[email protected]432115822011-07-10 15:52:27347 this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
[email protected]6c2381d2011-10-19 02:52:53348 content::Source<ThemeService>(
349 ThemeServiceFactory::GetForProfile(profile_)));
[email protected]6b40bb582012-03-15 20:50:38350#endif
[email protected]ea049a02011-12-25 21:37:09351 registrar_.Add(this, chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
[email protected]ad50def52011-10-19 23:17:07352 content::NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29353
[email protected]97b46382011-05-02 13:23:04354 profile_pref_registrar_.Init(profile_->GetPrefs());
355 profile_pref_registrar_.Add(prefs::kDevToolsDisabled, this);
[email protected]3710d0002011-10-11 00:35:00356 profile_pref_registrar_.Add(prefs::kShowBookmarkBar, this);
[email protected]6c6b02d2011-09-02 03:36:47357 profile_pref_registrar_.Add(prefs::kHomePage, this);
[email protected]97b46382011-05-02 13:23:04358 profile_pref_registrar_.Add(prefs::kInstantEnabled, this);
[email protected]9e823662010-10-13 23:36:00359
initial.commit09911bf2008-07-26 23:55:29360 BrowserList::AddBrowser(this);
361
[email protected]97b46382011-05-02 13:23:04362 // NOTE: These prefs all need to be explicitly destroyed in the destructor
363 // or you'll get a nasty surprise when you run the incognito tests.
initial.commit09911bf2008-07-26 23:55:29364 encoding_auto_detect_.Init(prefs::kWebKitUsesUniversalDetector,
365 profile_->GetPrefs(), NULL);
[email protected]281918912010-05-27 22:05:13366
[email protected]2031cf9d2010-10-12 05:05:37367 CreateInstantIfNecessary();
[email protected]07d490bc2011-03-07 17:05:26368
[email protected]09b29342011-06-24 19:18:48369 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_INIT);
[email protected]a7be6982011-12-12 21:53:57370
371 FilePath profile_path = profile->GetPath();
372 ProfileMetrics::LogProfileLaunch(profile_path);
initial.commit09911bf2008-07-26 23:55:29373}
374
375Browser::~Browser() {
[email protected]2fc15ae2012-05-06 00:01:37376 // The tab strip should not have any tabs at this point.
377 if (!browser_shutdown::ShuttingDownWithoutClosingBrowsers())
378 DCHECK(tab_strip_model_->empty());
379 tab_strip_model_->RemoveObserver(this);
380
initial.commit09911bf2008-07-26 23:55:29381 BrowserList::RemoveBrowser(this);
382
[email protected]bdc6ccd12012-03-20 22:06:13383 SessionService* session_service =
384 SessionServiceFactory::GetForProfile(profile_);
385 if (session_service)
386 session_service->WindowClosed(session_id_);
387
388 TabRestoreService* tab_restore_service =
389 TabRestoreServiceFactory::GetForProfile(profile());
390 if (tab_restore_service)
391 tab_restore_service->BrowserClosed(tab_restore_service_delegate());
392
[email protected]7be64502011-05-03 17:51:47393#if !defined(OS_MACOSX)
[email protected]d8748142012-05-16 21:13:43394 if (!browser::GetBrowserCount(profile_)) {
initial.commit09911bf2008-07-26 23:55:29395 // We're the last browser window with this profile. We need to nuke the
396 // TabRestoreService, which will start the shutdown of the
397 // NavigationControllers and allow for proper shutdown. If we don't do this
398 // chrome won't shutdown cleanly, and may end up crashing when some
399 // thread tries to use the IO thread (or another thread) that is no longer
400 // valid.
[email protected]fbc947b2009-06-19 13:28:24401 // This isn't a valid assumption for Mac OS, as it stays running after
402 // the last browser has closed. The Mac equivalent is in its app
403 // controller.
[email protected]92371eb2011-04-28 11:50:15404 TabRestoreServiceFactory::ResetForProfile(profile_);
initial.commit09911bf2008-07-26 23:55:29405 }
[email protected]fbc947b2009-06-19 13:28:24406#endif
initial.commit09911bf2008-07-26 23:55:29407
[email protected]97b46382011-05-02 13:23:04408 profile_pref_registrar_.RemoveAll();
[email protected]97b46382011-05-02 13:23:04409
[email protected]9a8c4022011-01-25 14:25:33410 encoding_auto_detect_.Destroy();
[email protected]9a8c4022011-01-25 14:25:33411
[email protected]5d98294912012-06-27 22:57:40412 command_controller_.reset();
413
initial.commit09911bf2008-07-26 23:55:29414 if (profile_->IsOffTheRecord() &&
[email protected]474e5db2011-08-12 13:02:23415 !BrowserList::IsOffTheRecordSessionActiveForProfile(profile_)) {
[email protected]5e919242012-02-13 23:59:35416 // An incognito profile is no longer needed, this indirectly frees
417 // its cache and cookies once it gets destroyed at the appropriate time.
[email protected]4dffabe2012-05-19 14:37:06418 ProfileDestroyer::DestroyProfileWhenAppropriate(profile_);
initial.commit09911bf2008-07-26 23:55:29419 }
420
421 // There may be pending file dialogs, we need to tell them that we've gone
422 // away so they don't try and call back to us.
423 if (select_file_dialog_.get())
424 select_file_dialog_->ListenerDestroyed();
425}
426
[email protected]15952e462008-11-14 00:29:05427// static
428Browser* Browser::Create(Profile* profile) {
[email protected]b35b26b32011-05-05 20:35:14429 Browser* browser = new Browser(TYPE_TABBED, profile);
430 browser->InitBrowserWindow();
431 return browser;
432}
433
434// static
435Browser* Browser::CreateWithParams(const CreateParams& params) {
[email protected]a636d8e52012-02-28 15:40:41436 if (!params.app_name.empty())
[email protected]2cd4fde2012-06-26 03:10:26437 chrome::RegisterAppPrefs(params.app_name, params.profile);
[email protected]a636d8e52012-02-28 15:40:41438
[email protected]b35b26b32011-05-05 20:35:14439 Browser* browser = new Browser(params.type, params.profile);
440 browser->app_name_ = params.app_name;
[email protected]0da58292012-03-22 20:37:21441 browser->app_type_ = params.app_type;
[email protected]81b2b0ac2011-11-23 01:54:48442 browser->set_override_bounds(params.initial_bounds);
[email protected]1f0b50b2012-06-22 20:37:16443 browser->set_initial_show_state(params.initial_show_state);
[email protected]a636d8e52012-02-28 15:40:41444 browser->set_is_session_restore(params.is_session_restore);
445
[email protected]9800de5e2011-03-11 18:19:49446 browser->InitBrowserWindow();
[email protected]15952e462008-11-14 00:29:05447 return browser;
448}
[email protected]6104acf2008-11-11 22:27:34449
[email protected]9800de5e2011-03-11 18:19:49450void Browser::InitBrowserWindow() {
[email protected]15952e462008-11-14 00:29:05451 DCHECK(!window_);
[email protected]9c45b7182009-08-04 16:44:43452
[email protected]9800de5e2011-03-11 18:19:49453 window_ = CreateBrowserWindow();
[email protected]01a5a762011-11-21 05:40:55454 fullscreen_controller_ = new FullscreenController(window_, profile_, this);
[email protected]15952e462008-11-14 00:29:05455
[email protected]5e37c452011-09-06 19:31:39456#if defined(OS_WIN) && !defined(USE_AURA)
[email protected]6a34951a2012-01-17 18:10:40457 // Set the app user model id for this application to that of the application
458 // name. See http://crbug.com/7028.
459 ui::win::SetAppIdForWindow(
460 is_app() && !is_type_panel() ?
[email protected]a0448002012-06-19 04:32:10461 ShellIntegration::GetAppModelIdForProfile(UTF8ToWide(app_name_),
462 profile_->GetPath()) :
463 ShellIntegration::GetChromiumModelIdForProfile(profile_->GetPath()),
[email protected]90556dd2012-06-07 20:26:18464 window()->GetNativeWindow());
[email protected]45446a52010-11-04 17:41:00465
[email protected]6a34951a2012-01-17 18:10:40466 if (is_type_panel()) {
467 ui::win::SetAppIconForWindow(ShellIntegration::GetChromiumIconPath(),
[email protected]90556dd2012-06-07 20:26:18468 window()->GetNativeWindow());
[email protected]45446a52010-11-04 17:41:00469 }
[email protected]80cf3562009-11-17 08:17:24470#endif
471
[email protected]41d9faf2012-02-28 23:46:02472 // Create the extension window controller before sending notifications.
473 extension_window_controller_.reset(
474 new BrowserExtensionWindowController(this));
475
[email protected]ad50def52011-10-19 23:17:07476 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27477 chrome::NOTIFICATION_BROWSER_WINDOW_READY,
[email protected]6c2381d2011-10-19 02:52:53478 content::Source<Browser>(this),
[email protected]ad50def52011-10-19 23:17:07479 content::NotificationService::NoDetails());
[email protected]14d8c66e2009-09-22 00:32:52480
[email protected]15952e462008-11-14 00:29:05481 PrefService* local_state = g_browser_process->local_state();
[email protected]2f516c792011-09-19 22:22:09482 if (local_state && local_state->FindPreference(
[email protected]663bd9e2011-03-21 01:07:01483 prefs::kAutofillPersonalDataManagerFirstRun) &&
484 local_state->GetBoolean(prefs::kAutofillPersonalDataManagerFirstRun)) {
[email protected]f69c8852010-10-12 02:36:46485 // Notify PDM that this is a first run.
486#if defined(OS_WIN)
[email protected]ea9edcb02011-09-23 22:05:04487 ImportAutofillDataWin(PersonalDataManagerFactory::GetForProfile(profile_));
[email protected]f69c8852010-10-12 02:36:46488#endif // defined(OS_WIN)
489 // Reset the preference so we don't call it again for subsequent windows.
[email protected]663bd9e2011-03-21 01:07:01490 local_state->ClearPref(prefs::kAutofillPersonalDataManagerFirstRun);
[email protected]f69c8852010-10-12 02:36:46491 }
initial.commit09911bf2008-07-26 23:55:29492}
493
[email protected]bf8708692011-12-20 21:24:28494void Browser::SetWindowForTesting(BrowserWindow* window) {
495 DCHECK(!window_);
496 window_ = window;
497 fullscreen_controller_ = new FullscreenController(window_, profile_, this);
498}
499
[email protected]36b6dcb2008-11-12 01:19:57500///////////////////////////////////////////////////////////////////////////////
[email protected]5c238752009-06-13 10:29:07501// Getters & Setters
502
[email protected]b77cb302009-10-29 04:09:17503FindBarController* Browser::GetFindBarController() {
504 if (!find_bar_controller_.get()) {
[email protected]632983f2011-08-08 22:51:24505 FindBar* find_bar = window_->CreateFindBar();
[email protected]b77cb302009-10-29 04:09:17506 find_bar_controller_.reset(new FindBarController(find_bar));
507 find_bar->SetFindBarController(find_bar_controller_.get());
[email protected]52877dbc62012-06-29 22:22:03508 find_bar_controller_->ChangeTabContents(chrome::GetActiveTabContents(this));
[email protected]b77cb302009-10-29 04:09:17509 find_bar_controller_->find_bar()->MoveWindowIfNecessary(gfx::Rect(), true);
510 }
511 return find_bar_controller_.get();
512}
513
[email protected]24db8a072009-10-29 20:35:37514bool Browser::HasFindBarController() const {
515 return find_bar_controller_.get() != NULL;
516}
517
[email protected]b35b26b32011-05-05 20:35:14518bool Browser::is_app() const {
519 return !app_name_.empty();
520}
521
522bool Browser::is_devtools() const {
523 return app_name_ == DevToolsWindow::kDevToolsApp;
524}
525
[email protected]5c238752009-06-13 10:29:07526///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:57527// Browser, State Storage and Retrieval for UI:
528
[email protected]36b6dcb2008-11-12 01:19:57529SkBitmap Browser::GetCurrentPageIcon() const {
[email protected]52877dbc62012-06-29 22:22:03530 TabContents* contents = chrome::GetActiveTabContents(this);
[email protected]ce5348a82008-12-18 18:36:23531 // |contents| can be NULL since GetCurrentPageIcon() is called by the window
532 // during the window's creation (before tabs have been added).
[email protected]b375c5d2011-05-03 21:15:04533 return contents ? contents->favicon_tab_helper()->GetFavicon() : SkBitmap();
[email protected]36b6dcb2008-11-12 01:19:57534}
535
[email protected]731f8a42009-07-20 22:07:32536string16 Browser::GetWindowTitleForCurrentTab() const {
[email protected]52877dbc62012-06-29 22:22:03537 WebContents* contents = chrome::GetActiveWebContents(this);
[email protected]dda5dfa32009-07-10 00:33:29538 string16 title;
[email protected]c7c42332008-11-15 01:10:54539
[email protected]731f8a42009-07-20 22:07:32540 // |contents| can be NULL because GetWindowTitleForCurrentTab is called by the
541 // window during the window's creation (before tabs have been added).
[email protected]36b6dcb2008-11-12 01:19:57542 if (contents) {
[email protected]dda5dfa32009-07-10 00:33:29543 title = contents->GetTitle();
[email protected]36b6dcb2008-11-12 01:19:57544 FormatTitleForDisplay(&title);
545 }
546 if (title.empty())
[email protected]51d83bdf2011-12-09 16:09:29547 title = CoreTabHelper::GetDefaultTitle();
[email protected]36b6dcb2008-11-12 01:19:57548
[email protected]cae97652012-04-20 03:12:12549#if defined(OS_MACOSX) || defined(USE_ASH)
550 // On Mac or Ash, we don't want to suffix the page title with
[email protected]22a3daa2009-11-09 22:03:33551 // the application name.
[email protected]d904bc82009-07-07 00:41:04552 return title;
[email protected]7be64502011-05-03 17:51:47553#else
[email protected]edb5e6a2009-04-08 23:46:17554 int string_id = IDS_BROWSER_WINDOW_TITLE_FORMAT;
[email protected]dff529732010-04-01 23:50:55555 // Don't append the app name to window titles on app frames and app popups
[email protected]b35b26b32011-05-05 20:35:14556 if (is_app())
[email protected]edb5e6a2009-04-08 23:46:17557 string_id = IDS_BROWSER_WINDOW_TITLE_FORMAT_NO_LOGO;
[email protected]dda5dfa32009-07-10 00:33:29558 return l10n_util::GetStringFUTF16(string_id, title);
[email protected]57e68dba2009-05-15 21:30:50559#endif
[email protected]36b6dcb2008-11-12 01:19:57560}
561
562// static
[email protected]dda5dfa32009-07-10 00:33:29563void Browser::FormatTitleForDisplay(string16* title) {
[email protected]36b6dcb2008-11-12 01:19:57564 size_t current_index = 0;
565 size_t match_index;
[email protected]57ecc4b2010-08-11 03:02:51566 while ((match_index = title->find(L'\n', current_index)) != string16::npos) {
[email protected]810a52ef2010-01-08 01:22:15567 title->replace(match_index, 1, string16());
[email protected]36b6dcb2008-11-12 01:19:57568 current_index = match_index;
569 }
570}
571
572///////////////////////////////////////////////////////////////////////////////
573// Browser, OnBeforeUnload handling:
574
575bool Browser::ShouldCloseWindow() {
[email protected]69444cc2009-04-09 20:40:06576 if (!CanCloseWithInProgressDownloads())
577 return false;
578
579 if (HasCompletedUnloadProcessing())
[email protected]22bf2c12012-06-23 08:35:55580 return true;
[email protected]69444cc2009-04-09 20:40:06581
[email protected]36b6dcb2008-11-12 01:19:57582 is_attempting_to_close_browser_ = true;
583
[email protected]460a2942010-12-15 19:43:15584 if (!TabsNeedBeforeUnloadFired())
[email protected]22bf2c12012-06-23 08:35:55585 return true;
[email protected]36b6dcb2008-11-12 01:19:57586
587 ProcessPendingTabs();
588 return false;
589}
590
591void Browser::OnWindowClosing() {
592 if (!ShouldCloseWindow())
593 return;
594
[email protected]911f8152010-03-18 16:46:40595 bool exiting = false;
596
[email protected]c984d9f2010-07-20 20:52:20597 // Application should shutdown on last window close if the user is explicitly
598 // trying to quit, or if there is nothing keeping the browser alive (such as
599 // AppController on the Mac, or BackgroundContentsService for background
600 // pages).
601 bool should_quit_if_last_browser =
[email protected]2e6389f2012-05-18 19:41:25602 browser_shutdown::IsTryingToQuit() || !browser::WillKeepAlive();
[email protected]7dc8c6b2010-04-09 17:02:50603
604 if (should_quit_if_last_browser && BrowserList::size() == 1) {
[email protected]36b6dcb2008-11-12 01:19:57605 browser_shutdown::OnShutdownStarting(browser_shutdown::WINDOW_CLOSE);
[email protected]911f8152010-03-18 16:46:40606 exiting = true;
607 }
[email protected]36b6dcb2008-11-12 01:19:57608
[email protected]92371eb2011-04-28 11:50:15609 // Don't use GetForProfileIfExisting here, we want to force creation of the
[email protected]36b6dcb2008-11-12 01:19:57610 // session service so that user can restore what was open.
[email protected]92371eb2011-04-28 11:50:15611 SessionService* session_service =
612 SessionServiceFactory::GetForProfile(profile());
[email protected]36b6dcb2008-11-12 01:19:57613 if (session_service)
614 session_service->WindowClosing(session_id());
615
[email protected]92371eb2011-04-28 11:50:15616 TabRestoreService* tab_restore_service =
617 TabRestoreServiceFactory::GetForProfile(profile());
[email protected]a636d8e52012-02-28 15:40:41618
619#if defined(USE_AURA)
620 if (tab_restore_service && is_app())
621 tab_restore_service->BrowserClosing(tab_restore_service_delegate());
622#endif
623
[email protected]b35b26b32011-05-05 20:35:14624 if (tab_restore_service && is_type_tabbed() && tab_count())
[email protected]fb7da4132011-03-15 15:51:00625 tab_restore_service->BrowserClosing(tab_restore_service_delegate());
[email protected]d8375fd2008-11-25 22:45:39626
[email protected]911f8152010-03-18 16:46:40627 // TODO(sky): convert session/tab restore to use notification.
[email protected]ad50def52011-10-19 23:17:07628 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27629 chrome::NOTIFICATION_BROWSER_CLOSING,
[email protected]6c2381d2011-10-19 02:52:53630 content::Source<Browser>(this),
631 content::Details<bool>(&exiting));
[email protected]911f8152010-03-18 16:46:40632
[email protected]52877dbc62012-06-29 22:22:03633 chrome::CloseAllTabs(this);
[email protected]36b6dcb2008-11-12 01:19:57634}
635
[email protected]28788c7d2011-05-20 23:03:45636void Browser::OnWindowActivated() {
637 // On some platforms we want to automatically reload tabs that are
638 // killed when the user selects them.
[email protected]52877dbc62012-06-29 22:22:03639 WebContents* contents = chrome::GetActiveWebContents(this);
[email protected]be1f56ab2011-12-22 06:55:31640 if (contents && contents->GetCrashedStatus() ==
[email protected]28788c7d2011-05-20 23:03:45641 base::TERMINATION_STATUS_PROCESS_WAS_KILLED) {
642 if (CommandLine::ForCurrentProcess()->HasSwitch(
643 switches::kReloadKilledTabs)) {
[email protected]a37d4b02012-06-25 21:56:10644 chrome::Reload(this, CURRENT_TAB);
[email protected]28788c7d2011-05-20 23:03:45645 }
646 }
647}
648
[email protected]69444cc2009-04-09 20:40:06649////////////////////////////////////////////////////////////////////////////////
650// In-progress download termination handling:
651
652void Browser::InProgressDownloadResponse(bool cancel_downloads) {
653 if (cancel_downloads) {
654 cancel_download_confirmation_state_ = RESPONSE_RECEIVED;
[email protected]a37d4b02012-06-25 21:56:10655 chrome::CloseWindow(this);
[email protected]69444cc2009-04-09 20:40:06656 return;
657 }
658
659 // Sets the confirmation state to NOT_PROMPTED so that if the user tries to
660 // close again we'll show the warning again.
661 cancel_download_confirmation_state_ = NOT_PROMPTED;
662
663 // Show the download page so the user can figure-out what downloads are still
664 // in-progress.
[email protected]5d9cace72012-06-21 16:07:12665 chrome::ShowDownloads(this);
[email protected]69444cc2009-04-09 20:40:06666}
667
[email protected]5a17d492011-10-24 19:37:03668Browser::DownloadClosePreventionType Browser::OkToCloseWithInProgressDownloads(
669 int* num_downloads_blocking) const {
670 DCHECK(num_downloads_blocking);
671 *num_downloads_blocking = 0;
672
673 if (is_attempting_to_close_browser_)
674 return DOWNLOAD_CLOSE_OK;
675
676 // If we're not running a full browser process with a profile manager
677 // (testing), it's ok to close the browser.
678 if (!g_browser_process->profile_manager())
679 return DOWNLOAD_CLOSE_OK;
680
681 int total_download_count = DownloadService::DownloadCountAllProfiles();
682 if (total_download_count == 0)
683 return DOWNLOAD_CLOSE_OK; // No downloads; can definitely close.
684
685 // Figure out how many windows are open total, and associated with this
686 // profile, that are relevant for the ok-to-close decision.
687 int profile_window_count = 0;
688 int total_window_count = 0;
689 for (BrowserList::const_iterator iter = BrowserList::begin();
690 iter != BrowserList::end(); ++iter) {
691 // Don't count this browser window or any other in the process of closing.
692 Browser* const browser = *iter;
693 // Check is_attempting_to_close_browser_ as window closing may be
694 // delayed, and windows that are in the process of closing don't
695 // count against our totals.
696 if (browser == this || browser->is_attempting_to_close_browser_)
697 continue;
698
699 if ((*iter)->profile() == profile())
700 profile_window_count++;
701 total_window_count++;
702 }
703
704 // If there aren't any other windows, we're at browser shutdown,
705 // which would cancel all current downloads.
706 if (total_window_count == 0) {
707 *num_downloads_blocking = total_download_count;
708 return DOWNLOAD_CLOSE_BROWSER_SHUTDOWN;
709 }
710
711 // If there aren't any other windows on our profile, and we're an incognito
712 // profile, and there are downloads associated with that profile,
713 // those downloads would be cancelled by our window (-> profile) close.
714 DownloadService* download_service =
715 DownloadServiceFactory::GetForProfile(profile());
716 if (profile_window_count == 0 && download_service->DownloadCount() > 0 &&
717 profile()->IsOffTheRecord()) {
718 *num_downloads_blocking = download_service->DownloadCount();
719 return DOWNLOAD_CLOSE_LAST_WINDOW_IN_INCOGNITO_PROFILE;
720 }
721
722 // Those are the only conditions under which we will block shutdown.
723 return DOWNLOAD_CLOSE_OK;
724}
725
[email protected]69444cc2009-04-09 20:40:06726////////////////////////////////////////////////////////////////////////////////
[email protected]c848d3d92010-09-16 21:57:45727// Browser, TabStripModel pass-thrus:
728
729int Browser::tab_count() const {
[email protected]2fc15ae2012-05-06 00:01:37730 return tab_strip_model_->count();
[email protected]c848d3d92010-09-16 21:57:45731}
732
[email protected]1ea49d52011-04-12 17:44:44733int Browser::active_index() const {
[email protected]2fc15ae2012-05-06 00:01:37734 return tab_strip_model_->active_index();
[email protected]c848d3d92010-09-16 21:57:45735}
736
[email protected]c848d3d92010-09-16 21:57:45737////////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:57738// Browser, Tab adding/showing functions:
739
[email protected]227393e2011-06-29 21:28:39740void Browser::WindowFullscreenStateChanged() {
[email protected]01a5a762011-11-21 05:40:55741 fullscreen_controller_->WindowFullscreenStateChanged();
[email protected]5d98294912012-06-27 22:57:40742 command_controller_->FullscreenStateChanged();
[email protected]9adb8b12011-12-09 06:49:13743 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TOGGLE_FULLSCREEN);
[email protected]ae2622c2009-07-30 23:47:58744}
745
[email protected]36b6dcb2008-11-12 01:19:57746///////////////////////////////////////////////////////////////////////////////
747// Browser, Assorted browser commands:
748
[email protected]184717d52012-02-22 04:46:16749void Browser::ToggleFullscreenMode() {
750 fullscreen_controller_->ToggleFullscreenMode();
751}
752
[email protected]41d9faf2012-02-28 23:46:02753void Browser::ToggleFullscreenModeWithExtension(const GURL& extension_url) {
754 fullscreen_controller_->ToggleFullscreenModeWithExtension(extension_url);
[email protected]9282cea2009-02-18 18:49:00755}
756
[email protected]d3766932011-08-04 22:18:23757#if defined(OS_MACOSX)
[email protected]184717d52012-02-22 04:46:16758void Browser::TogglePresentationMode() {
759 fullscreen_controller_->TogglePresentationMode();
[email protected]d3766932011-08-04 22:18:23760}
761#endif
762
[email protected]a3e18c42009-03-04 23:36:05763bool Browser::SupportsWindowFeature(WindowFeature feature) const {
[email protected]018cf362010-05-05 22:43:22764 return SupportsWindowFeatureImpl(feature, true);
765}
[email protected]f5bf8ccf2010-02-05 18:19:25766
[email protected]018cf362010-05-05 22:43:22767bool Browser::CanSupportWindowFeature(WindowFeature feature) const {
768 return SupportsWindowFeatureImpl(feature, false);
[email protected]a3e18c42009-03-04 23:36:05769}
770
[email protected]fbd77592008-11-12 20:50:27771void Browser::ToggleEncodingAutoDetect() {
[email protected]7f6f44c2011-12-14 13:23:38772 content::RecordAction(UserMetricsAction("AutoDetectChange"));
[email protected]fbd77592008-11-12 20:50:27773 encoding_auto_detect_.SetValue(!encoding_auto_detect_.GetValue());
[email protected]a697f4c2009-09-14 22:30:18774 // If "auto detect" is turned on, then any current override encoding
775 // is cleared. This also implicitly performs a reload.
776 // OTOH, if "auto detect" is turned off, we don't change the currently
777 // active encoding.
778 if (encoding_auto_detect_.GetValue()) {
[email protected]52877dbc62012-06-29 22:22:03779 WebContents* contents = chrome::GetActiveWebContents(this);
[email protected]a697f4c2009-09-14 22:30:18780 if (contents)
[email protected]b2fe07d12010-02-09 14:38:08781 contents->ResetOverrideEncoding();
[email protected]a697f4c2009-09-14 22:30:18782 }
[email protected]fbd77592008-11-12 20:50:27783}
784
785void Browser::OverrideEncoding(int encoding_id) {
[email protected]7f6f44c2011-12-14 13:23:38786 content::RecordAction(UserMetricsAction("OverrideEncoding"));
[email protected]41fc0322009-09-04 22:23:40787 const std::string selected_encoding =
[email protected]fbd77592008-11-12 20:50:27788 CharacterEncoding::GetCanonicalEncodingNameByCommandId(encoding_id);
[email protected]52877dbc62012-06-29 22:22:03789 WebContents* contents = chrome::GetActiveWebContents(this);
[email protected]57c6a652009-05-04 07:58:34790 if (!selected_encoding.empty() && contents)
[email protected]b2fe07d12010-02-09 14:38:08791 contents->SetOverrideEncoding(selected_encoding);
[email protected]fbd77592008-11-12 20:50:27792 // Update the list of recently selected encodings.
[email protected]41fc0322009-09-04 22:23:40793 std::string new_selected_encoding_list;
[email protected]ddd231e2010-06-29 20:35:19794 if (CharacterEncoding::UpdateRecentlySelectedEncoding(
795 profile_->GetPrefs()->GetString(prefs::kRecentlySelectedEncoding),
[email protected]41fc0322009-09-04 22:23:40796 encoding_id,
797 &new_selected_encoding_list)) {
[email protected]fbd77592008-11-12 20:50:27798 profile_->GetPrefs()->SetString(prefs::kRecentlySelectedEncoding,
[email protected]ddd231e2010-06-29 20:35:19799 new_selected_encoding_list);
[email protected]fbd77592008-11-12 20:50:27800 }
[email protected]36b6dcb2008-11-12 01:19:57801}
802
[email protected]84214982008-12-10 18:49:10803void Browser::OpenFile() {
[email protected]7f6f44c2011-12-14 13:23:38804 content::RecordAction(UserMetricsAction("OpenFile"));
[email protected]91254572012-06-29 19:00:46805 if (!select_file_dialog_.get())
806 select_file_dialog_ = SelectFileDialog::Create(this);
[email protected]84214982008-12-10 18:49:10807
[email protected]35896a32010-06-09 08:42:51808 const FilePath directory = profile_->last_selected_directory();
809
[email protected]84214982008-12-10 18:49:10810 // TODO(beng): figure out how to juggle this.
[email protected]90556dd2012-06-07 20:26:18811 gfx::NativeWindow parent_window = window_->GetNativeWindow();
[email protected]84214982008-12-10 18:49:10812 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_OPEN_FILE,
[email protected]35896a32010-06-09 08:42:51813 string16(), directory,
[email protected]b949f1112009-04-12 20:03:08814 NULL, 0, FILE_PATH_LITERAL(""),
[email protected]52877dbc62012-06-29 22:22:03815 chrome::GetActiveWebContents(this),
[email protected]d56bcd22009-03-16 19:51:56816 parent_window, NULL);
[email protected]84214982008-12-10 18:49:10817}
818
819void Browser::OpenCreateShortcutsDialog() {
[email protected]7f6f44c2011-12-14 13:23:38820 content::RecordAction(UserMetricsAction("CreateShortcut"));
[email protected]7be64502011-05-03 17:51:47821#if !defined(OS_MACOSX)
[email protected]52877dbc62012-06-29 22:22:03822 TabContents* current_tab = chrome::GetActiveTabContents(this);
[email protected]3c9e1872010-11-18 16:17:49823 DCHECK(current_tab &&
[email protected]d9083482012-01-06 00:38:46824 web_app::IsValidUrl(current_tab->web_contents()->GetURL())) <<
[email protected]3c9e1872010-11-18 16:17:49825 "Menu item should be disabled.";
[email protected]ed5431872009-11-17 08:39:51826
[email protected]10f417c52011-12-28 21:04:23827 NavigationEntry* entry =
[email protected]d9083482012-01-06 00:38:46828 current_tab->web_contents()->GetController().GetLastCommittedEntry();
[email protected]ed5431872009-11-17 08:39:51829 if (!entry)
830 return;
831
[email protected]eabfdae92009-12-11 06:13:51832 // RVH's GetApplicationInfo should not be called before it returns.
833 DCHECK(pending_web_app_action_ == NONE);
834 pending_web_app_action_ = CREATE_SHORTCUT;
835
[email protected]a2cf65eb2010-01-18 08:20:38836 // Start fetching web app info for CreateApplicationShortcut dialog and show
837 // the dialog when the data is available in OnDidGetApplicationInfo.
[email protected]36fc0392011-12-25 03:59:51838 current_tab->extension_tab_helper()->GetApplicationInfo(entry->GetPageID());
[email protected]98f6e022009-06-05 22:49:53839#else
840 NOTIMPLEMENTED();
841#endif
[email protected]36b6dcb2008-11-12 01:19:57842}
843
[email protected]92655362012-04-20 21:33:17844void Browser::UpdateDownloadShelfVisibility(bool visible) {
845 if (GetStatusBubble())
846 GetStatusBubble()->UpdateDownloadShelfVisibility(visible);
847}
848
[email protected]a37d4b02012-06-25 21:56:10849bool Browser::OpenInstant(WindowOpenDisposition disposition) {
850 if (!instant() || !instant()->PrepareForCommit() ||
851 disposition == NEW_BACKGROUND_TAB) {
852 // NEW_BACKGROUND_TAB results in leaving the omnibox open, so we don't
853 // attempt to use the instant preview.
854 return false;
855 }
856
857 if (disposition == CURRENT_TAB) {
858 content::NotificationService::current()->Notify(
859 chrome::NOTIFICATION_INSTANT_COMMITTED,
860 content::Source<TabContents>(instant()->CommitCurrentPreview(
861 INSTANT_COMMIT_PRESSED_ENTER)),
862 content::NotificationService::NoDetails());
863 return true;
864 }
865 if (disposition == NEW_FOREGROUND_TAB) {
866 TabContents* preview_contents = instant()->ReleasePreviewContents(
867 INSTANT_COMMIT_PRESSED_ENTER, NULL);
868 // HideInstant is invoked after release so that InstantController is not
869 // active when HideInstant asks it for its state.
870 HideInstant();
871 preview_contents->web_contents()->GetController().PruneAllButActive();
872 tab_strip_model_->AddTabContents(
873 preview_contents,
874 -1,
875 instant()->last_transition_type(),
876 TabStripModel::ADD_ACTIVE);
877 instant()->CompleteRelease(preview_contents);
878 content::NotificationService::current()->Notify(
879 chrome::NOTIFICATION_INSTANT_COMMITTED,
880 content::Source<TabContents>(preview_contents),
881 content::NotificationService::NoDetails());
882 return true;
883 }
884 // The omnibox currently doesn't use other dispositions, so we don't attempt
885 // to handle them. If you hit this NOTREACHED file a bug and I'll (sky) add
886 // support for the new disposition.
887 NOTREACHED();
888 return false;
889}
890
[email protected]36b6dcb2008-11-12 01:19:57891///////////////////////////////////////////////////////////////////////////////
892
893// static
[email protected]d9083482012-01-06 00:38:46894bool Browser::RunUnloadEventsHelper(WebContents* contents) {
[email protected]075969d2012-04-17 20:05:55895 // If the WebContents is not connected yet, then there's no unload
896 // handler we can fire even if the WebContents has an unload listener.
[email protected]ecc523f62010-09-28 22:45:40897 // One case where we hit this is in a tab that has an infinite loop
898 // before load.
[email protected]5aab5e22010-12-08 22:13:29899 if (contents->NeedToFireBeforeUnload()) {
[email protected]ecc523f62010-09-28 22:45:40900 // If the page has unload listeners, then we tell the renderer to fire
901 // them. Once they have fired, we'll get a message back saying whether
902 // to proceed closing the page or not, which sends us back to this method
[email protected]5aab5e22010-12-08 22:13:29903 // with the NeedToFireBeforeUnload bit cleared.
[email protected]151a63d2011-12-20 22:32:52904 contents->GetRenderViewHost()->FirePageBeforeUnload(false);
[email protected]ecc523f62010-09-28 22:45:40905 return true;
906 }
907 return false;
908}
909
910// static
[email protected]1d3e9172012-06-12 02:59:13911void Browser::JSOutOfMemoryHelper(WebContents* web_contents) {
912 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
[email protected]3d61dd02012-06-07 00:05:53913 if (!tab_contents)
[email protected]2a6bc3e2011-12-28 23:51:33914 return;
915
[email protected]3d61dd02012-06-07 00:05:53916 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper();
[email protected]2a6bc3e2011-12-28 23:51:33917 infobar_helper->AddInfoBar(new SimpleAlertInfoBarDelegate(
918 infobar_helper,
919 NULL,
920 l10n_util::GetStringUTF16(IDS_JS_OUT_OF_MEMORY_PROMPT),
921 true));
[email protected]7d189022011-08-25 22:54:20922}
923
924// static
[email protected]1d3e9172012-06-12 02:59:13925void Browser::RegisterProtocolHandlerHelper(WebContents* web_contents,
[email protected]7d189022011-08-25 22:54:20926 const std::string& protocol,
927 const GURL& url,
[email protected]3a3b75a2012-06-01 08:38:36928 const string16& title,
[email protected]9b4d241c92012-06-29 19:48:31929 bool user_gesture) {
[email protected]1d3e9172012-06-12 02:59:13930 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
[email protected]3d61dd02012-06-07 00:05:53931 if (!tab_contents || tab_contents->profile()->IsOffTheRecord())
[email protected]7d189022011-08-25 22:54:20932 return;
933
[email protected]7d189022011-08-25 22:54:20934 ProtocolHandler handler =
935 ProtocolHandler::CreateProtocolHandler(protocol, url, title);
936
937 ProtocolHandlerRegistry* registry =
[email protected]3d61dd02012-06-07 00:05:53938 tab_contents->profile()->GetProtocolHandlerRegistry();
[email protected]7d189022011-08-25 22:54:20939
[email protected]a50af292011-10-25 03:07:40940 if (!registry->SilentlyHandleRegisterHandlerRequest(handler)) {
[email protected]7f6f44c2011-12-14 13:23:38941 content::RecordAction(
[email protected]7d189022011-08-25 22:54:20942 UserMetricsAction("RegisterProtocolHandler.InfoBar_Shown"));
[email protected]3d61dd02012-06-07 00:05:53943 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper();
[email protected]d3ddb6d2012-01-06 02:51:17944
945 RegisterProtocolHandlerInfoBarDelegate* rph_delegate =
[email protected]95a33ed62011-09-30 15:07:08946 new RegisterProtocolHandlerInfoBarDelegate(infobar_helper,
947 registry,
[email protected]d3ddb6d2012-01-06 02:51:17948 handler);
949
950 for (size_t i = 0; i < infobar_helper->infobar_count(); i++) {
951 InfoBarDelegate* delegate = infobar_helper->GetInfoBarDelegateAt(i);
952 RegisterProtocolHandlerInfoBarDelegate* cast_delegate =
953 delegate->AsRegisterProtocolHandlerInfoBarDelegate();
954 if (cast_delegate != NULL && cast_delegate->IsReplacedBy(rph_delegate)) {
955 infobar_helper->ReplaceInfoBar(cast_delegate, rph_delegate);
956 rph_delegate = NULL;
957 break;
958 }
959 }
960
961 if (rph_delegate != NULL)
962 infobar_helper->AddInfoBar(rph_delegate);
[email protected]7d189022011-08-25 22:54:20963 }
964}
965
966// static
[email protected]1d3e9172012-06-12 02:59:13967void Browser::FindReplyHelper(WebContents* web_contents,
[email protected]b888919c2011-09-02 00:32:16968 int request_id,
969 int number_of_matches,
970 const gfx::Rect& selection_rect,
971 int active_match_ordinal,
972 bool final_update) {
[email protected]1d3e9172012-06-12 02:59:13973 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
[email protected]3d61dd02012-06-07 00:05:53974 if (!tab_contents || !tab_contents->find_tab_helper())
[email protected]b888919c2011-09-02 00:32:16975 return;
976
[email protected]3d61dd02012-06-07 00:05:53977 tab_contents->find_tab_helper()->HandleFindReply(request_id,
978 number_of_matches,
979 selection_rect,
980 active_match_ordinal,
981 final_update);
[email protected]b888919c2011-09-02 00:32:16982}
983
[email protected]3d61dd02012-06-07 00:05:53984void Browser::UpdateUIForNavigationInTab(TabContents* contents,
[email protected]ef557022012-03-16 10:05:33985 content::PageTransition transition,
986 bool user_initiated) {
[email protected]2fc15ae2012-05-06 00:01:37987 tab_strip_model_->TabNavigating(contents, transition);
[email protected]ef557022012-03-16 10:05:33988
[email protected]52877dbc62012-06-29 22:22:03989 bool contents_is_selected = contents == chrome::GetActiveTabContents(this);
[email protected]ef557022012-03-16 10:05:33990 if (user_initiated && contents_is_selected && window()->GetLocationBar()) {
991 // Forcibly reset the location bar if the url is going to change in the
992 // current tab, since otherwise it won't discard any ongoing user edits,
993 // since it doesn't realize this is a user-initiated action.
994 window()->GetLocationBar()->Revert();
995 }
996
997 if (GetStatusBubble())
998 GetStatusBubble()->Hide();
999
1000 // Update the location bar. This is synchronous. We specifically don't
1001 // update the load state since the load hasn't started yet and updating it
1002 // will put it out of sync with the actual state like whether we're
1003 // displaying a favicon, which controls the throbber. If we updated it here,
1004 // the throbber will show the default favicon for a split second when
1005 // navigating away from the new tab page.
1006 ScheduleUIUpdate(contents->web_contents(), content::INVALIDATE_TYPE_URL);
1007
[email protected]e1f288d2012-05-23 19:36:101008 if (contents_is_selected)
[email protected]ef557022012-03-16 10:05:331009 contents->web_contents()->Focus();
1010}
1011
1012///////////////////////////////////////////////////////////////////////////////
1013// Browser, PageNavigator implementation:
1014
1015WebContents* Browser::OpenURL(const OpenURLParams& params) {
1016 return OpenURLFromTab(NULL, params);
1017}
1018
[email protected]485fba42009-03-24 23:27:291019///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:571020// Browser, TabStripModelDelegate implementation:
1021
[email protected]3d61dd02012-06-07 00:05:531022TabContents* Browser::AddBlankTab(bool foreground) {
[email protected]22735af62009-04-07 21:09:581023 return AddBlankTabAt(-1, foreground);
1024}
1025
[email protected]3d61dd02012-06-07 00:05:531026TabContents* Browser::AddBlankTabAt(int index, bool foreground) {
[email protected]3f8eb7f2009-10-02 23:58:051027 // Time new tab page creation time. We keep track of the timing data in
[email protected]075969d2012-04-17 20:05:551028 // WebContents, but we want to include the time it takes to create the
1029 // WebContents object too.
[email protected]3f8eb7f2009-10-02 23:58:051030 base::TimeTicks new_tab_start_time = base::TimeTicks::Now();
[email protected]616381f02010-11-02 15:15:331031 browser::NavigateParams params(this, GURL(chrome::kChromeUINewTabURL),
[email protected]2905f742011-10-13 03:51:581032 content::PAGE_TRANSITION_TYPED);
[email protected]616381f02010-11-02 15:15:331033 params.disposition = foreground ? NEW_FOREGROUND_TAB : NEW_BACKGROUND_TAB;
1034 params.tabstrip_index = index;
1035 browser::Navigate(&params);
[email protected]d9083482012-01-06 00:38:461036 params.target_contents->web_contents()->SetNewTabStartTime(
[email protected]3c9e1872010-11-18 16:17:491037 new_tab_start_time);
[email protected]616381f02010-11-02 15:15:331038 return params.target_contents;
[email protected]15952e462008-11-14 00:29:051039}
1040
[email protected]3c9e1872010-11-18 16:17:491041Browser* Browser::CreateNewStripWithContents(
[email protected]3d61dd02012-06-07 00:05:531042 TabContents* detached_contents,
[email protected]3c9e1872010-11-18 16:17:491043 const gfx::Rect& window_bounds,
1044 const DockInfo& dock_info,
1045 bool maximize) {
[email protected]018cf362010-05-05 22:43:221046 DCHECK(CanSupportWindowFeature(FEATURE_TABSTRIP));
[email protected]adf650f2008-12-09 16:10:061047
[email protected]5e495462008-11-20 23:07:411048 gfx::Rect new_window_bounds = window_bounds;
[email protected]5e495462008-11-20 23:07:411049 if (dock_info.GetNewWindowBounds(&new_window_bounds, &maximize))
1050 dock_info.AdjustOtherWindowBounds();
1051
[email protected]36b6dcb2008-11-12 01:19:571052 // Create an empty new browser window the same size as the old one.
[email protected]b35b26b32011-05-05 20:35:141053 Browser* browser = new Browser(TYPE_TABBED, profile_);
[email protected]5e495462008-11-20 23:07:411054 browser->set_override_bounds(new_window_bounds);
[email protected]1f0b50b2012-06-22 20:37:161055 browser->set_initial_show_state(
[email protected]400eaf82011-08-22 15:47:391056 maximize ? ui::SHOW_STATE_MAXIMIZED : ui::SHOW_STATE_NORMAL);
[email protected]9800de5e2011-03-11 18:19:491057 browser->InitBrowserWindow();
[email protected]c0849252012-05-12 13:51:271058 browser->tab_strip_model()->AppendTabContents(detached_contents, true);
[email protected]159f7762008-12-19 14:58:271059 // Make sure the loading state is updated correctly, otherwise the throbber
1060 // won't start if the page is loading.
[email protected]2a6bc3e2011-12-28 23:51:331061 browser->LoadingStateChanged(detached_contents->web_contents());
[email protected]3d1104f2009-03-26 15:30:281062 return browser;
[email protected]36b6dcb2008-11-12 01:19:571063}
1064
1065int Browser::GetDragActions() const {
[email protected]ecc523f62010-09-28 22:45:401066 return TabStripModelDelegate::TAB_TEAROFF_ACTION | (tab_count() > 1 ?
1067 TabStripModelDelegate::TAB_MOVE_ACTION : 0);
[email protected]36b6dcb2008-11-12 01:19:571068}
1069
[email protected]3d61dd02012-06-07 00:05:531070TabContents* Browser::CreateTabContentsForURL(
[email protected]3ebe558b2011-12-06 08:05:081071 const GURL& url, const content::Referrer& referrer, Profile* profile,
[email protected]2905f742011-10-13 03:51:581072 content::PageTransition transition, bool defer_load,
[email protected]36b6dcb2008-11-12 01:19:571073 SiteInstance* instance) const {
[email protected]52877dbc62012-06-29 22:22:031074 TabContents* contents = chrome::TabContentsFactory(profile, instance,
1075 MSG_ROUTING_NONE, chrome::GetActiveWebContents(this), NULL);
[email protected]36b6dcb2008-11-12 01:19:571076 if (!defer_load) {
1077 // Load the initial URL before adding the new tab contents to the tab strip
1078 // so that the tab contents has navigation state.
[email protected]d9083482012-01-06 00:38:461079 contents->web_contents()->GetController().LoadURL(
[email protected]4a63e242011-12-12 15:23:081080 url, referrer, transition, std::string());
[email protected]36b6dcb2008-11-12 01:19:571081 }
1082
1083 return contents;
1084}
1085
1086bool Browser::CanDuplicateContentsAt(int index) {
[email protected]52877dbc62012-06-29 22:22:031087 NavigationController& nc =
1088 chrome::GetWebContentsAt(this, index)->GetController();
[email protected]fbc5e5f92012-01-02 06:08:321089 return nc.GetWebContents() && nc.GetLastCommittedEntry();
[email protected]36b6dcb2008-11-12 01:19:571090}
1091
1092void Browser::DuplicateContentsAt(int index) {
[email protected]52877dbc62012-06-29 22:22:031093 TabContents* contents = chrome::GetTabContentsAt(this, index);
[email protected]94ce7252010-12-20 21:46:331094 CHECK(contents);
[email protected]3d61dd02012-06-07 00:05:531095 TabContents* contents_dupe = contents->Clone();
[email protected]47b309f2010-12-28 17:35:561096
1097 bool pinned = false;
1098 if (CanSupportWindowFeature(FEATURE_TABSTRIP)) {
1099 // If this is a tabbed browser, just create a duplicate tab inside the same
1100 // window next to the tab being duplicated.
[email protected]2fc15ae2012-05-06 00:01:371101 int index = tab_strip_model_->GetIndexOfTabContents(contents);
[email protected]52877dbc62012-06-29 22:22:031102 pinned = tab_strip_model_->IsTabPinned(index);
[email protected]eaca0ad12011-04-18 15:53:411103 int add_types = TabStripModel::ADD_ACTIVE |
[email protected]47b309f2010-12-28 17:35:561104 TabStripModel::ADD_INHERIT_GROUP |
1105 (pinned ? TabStripModel::ADD_PINNED : 0);
[email protected]2fc15ae2012-05-06 00:01:371106 tab_strip_model_->InsertTabContentsAt(index + 1, contents_dupe, add_types);
[email protected]47b309f2010-12-28 17:35:561107 } else {
1108 Browser* browser = NULL;
[email protected]b35b26b32011-05-05 20:35:141109 if (is_app()) {
1110 CHECK(!is_type_popup());
1111 CHECK(!is_type_panel());
[email protected]da22aa62012-04-04 18:54:351112 browser = Browser::CreateWithParams(
1113 Browser::CreateParams::CreateForApp(
1114 TYPE_POPUP, app_name_, gfx::Rect(),profile_));
[email protected]b35b26b32011-05-05 20:35:141115 } else if (is_type_popup()) {
[email protected]da22aa62012-04-04 18:54:351116 browser = Browser::CreateWithParams(
1117 Browser::CreateParams(TYPE_POPUP, profile_));
[email protected]47b309f2010-12-28 17:35:561118 }
1119
1120 // Preserve the size of the original window. The new window has already
1121 // been given an offset by the OS, so we shouldn't copy the old bounds.
1122 BrowserWindow* new_window = browser->window();
1123 new_window->SetBounds(gfx::Rect(new_window->GetRestoredBounds().origin(),
1124 window()->GetRestoredBounds().size()));
1125
[email protected]4e595682011-02-09 17:07:021126 // We need to show the browser now. Otherwise ContainerWin assumes the
[email protected]075969d2012-04-17 20:05:551127 // WebContents is invisible and won't size it.
[email protected]47b309f2010-12-28 17:35:561128 browser->window()->Show();
1129
1130 // The page transition below is only for the purpose of inserting the tab.
[email protected]52877dbc62012-06-29 22:22:031131 chrome::AddTab(browser, contents_dupe, content::PAGE_TRANSITION_LINK);
[email protected]47b309f2010-12-28 17:35:561132 }
1133
[email protected]92371eb2011-04-28 11:50:151134 SessionService* session_service =
1135 SessionServiceFactory::GetForProfileIfExisting(profile_);
1136 if (session_service)
[email protected]81898992011-06-14 22:15:001137 session_service->TabRestored(contents_dupe, pinned);
[email protected]36b6dcb2008-11-12 01:19:571138}
1139
[email protected]36b6dcb2008-11-12 01:19:571140void Browser::CloseFrameAfterDragSession() {
[email protected]7be64502011-05-03 17:51:471141#if !defined(OS_MACOSX)
[email protected]36b6dcb2008-11-12 01:19:571142 // This is scheduled to run after we return to the message loop because
1143 // otherwise the frame will think the drag session is still active and ignore
1144 // the request.
[email protected]d6a3c772009-01-27 19:41:201145 // TODO(port): figure out what is required here in a cross-platform world
[email protected]0586b0e2010-02-12 21:38:371146 MessageLoop::current()->PostTask(
[email protected]24f1477532011-11-22 22:55:361147 FROM_HERE, base::Bind(&Browser::CloseFrame, weak_factory_.GetWeakPtr()));
[email protected]d6a3c772009-01-27 19:41:201148#endif
initial.commit09911bf2008-07-26 23:55:291149}
1150
[email protected]3d61dd02012-06-07 00:05:531151void Browser::CreateHistoricalTab(TabContents* contents) {
[email protected]505323e22009-01-24 02:47:581152 // We don't create historical tabs for incognito windows or windows without
1153 // profiles.
[email protected]92371eb2011-04-28 11:50:151154 if (!profile() || profile()->IsOffTheRecord())
[email protected]505323e22009-01-24 02:47:581155 return;
[email protected]92371eb2011-04-28 11:50:151156
[email protected]0d4ef7f2011-05-12 22:38:571157 // We don't create historical tabs for print preview tabs.
[email protected]d9083482012-01-06 00:38:461158 if (contents->web_contents()->GetURL() == GURL(chrome::kChromeUIPrintURL))
[email protected]0d4ef7f2011-05-12 22:38:571159 return;
1160
[email protected]92371eb2011-04-28 11:50:151161 TabRestoreService* service =
1162 TabRestoreServiceFactory::GetForProfile(profile());
[email protected]505323e22009-01-24 02:47:581163
[email protected]fb5ff422010-05-03 20:46:101164 // We only create historical tab entries for tabbed browser windows.
[email protected]92371eb2011-04-28 11:50:151165 if (service && CanSupportWindowFeature(FEATURE_TABSTRIP)) {
[email protected]c08337f2012-06-19 20:34:471166 service->CreateHistoricalTab(contents->web_contents(),
[email protected]2fc15ae2012-05-06 00:01:371167 tab_strip_model_->GetIndexOfTabContents(contents));
[email protected]505323e22009-01-24 02:47:581168 }
1169}
1170
[email protected]3d61dd02012-06-07 00:05:531171bool Browser::RunUnloadListenerBeforeClosing(TabContents* contents) {
[email protected]d9083482012-01-06 00:38:461172 return Browser::RunUnloadEventsHelper(contents->web_contents());
[email protected]505323e22009-01-24 02:47:581173}
1174
[email protected]a206b442009-10-08 23:20:201175bool Browser::CanBookmarkAllTabs() const {
[email protected]5d98294912012-06-27 22:57:401176 return chrome::CanBookmarkAllTabs(this);
[email protected]a206b442009-10-08 23:20:201177}
1178
[email protected]b3ac5c82009-10-08 20:56:541179void Browser::BookmarkAllTabs() {
[email protected]5d98294912012-06-27 22:57:401180 chrome::BookmarkAllTabs(this);
[email protected]b3ac5c82009-10-08 20:56:541181}
1182
[email protected]92655362012-04-20 21:33:171183bool Browser::CanRestoreTab() {
[email protected]5d98294912012-06-27 22:57:401184 return chrome::CanRestoreTab(this);
[email protected]92655362012-04-20 21:33:171185}
1186
1187void Browser::RestoreTab() {
[email protected]5d98294912012-06-27 22:57:401188 chrome::RestoreTab(this);
[email protected]92655362012-04-20 21:33:171189}
1190
[email protected]2d46c842008-11-14 19:24:311191///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:571192// Browser, TabStripModelObserver implementation:
1193
[email protected]3d61dd02012-06-07 00:05:531194void Browser::TabInsertedAt(TabContents* contents,
[email protected]36b6dcb2008-11-12 01:19:571195 int index,
1196 bool foreground) {
[email protected]9219d262011-02-15 21:05:121197 SetAsDelegate(contents, this);
[email protected]81898992011-06-14 22:15:001198 contents->restore_tab_helper()->SetWindowID(session_id());
[email protected]e524ff92011-03-30 13:26:581199
[email protected]c37e3b62010-01-22 00:15:171200 SyncHistoryWithTabs(index);
[email protected]36b6dcb2008-11-12 01:19:571201
[email protected]159f7762008-12-19 14:58:271202 // Make sure the loading state is updated correctly, otherwise the throbber
1203 // won't start if the page is loading.
[email protected]2a6bc3e2011-12-28 23:51:331204 LoadingStateChanged(contents->web_contents());
[email protected]159f7762008-12-19 14:58:271205
[email protected]36b6dcb2008-11-12 01:19:571206 // If the tab crashes in the beforeunload or unload handler, it won't be
1207 // able to ack. But we know we can close it.
[email protected]fbc5e5f92012-01-02 06:08:321208 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
1209 content::Source<WebContents>(contents->web_contents()));
[email protected]93f230e02011-06-01 14:40:001210
[email protected]432115822011-07-10 15:52:271211 registrar_.Add(this, content::NOTIFICATION_INTERSTITIAL_ATTACHED,
[email protected]fbc5e5f92012-01-02 06:08:321212 content::Source<WebContents>(contents->web_contents()));
[email protected]e397a4472011-12-21 21:47:501213
1214 registrar_.Add(this, content::NOTIFICATION_INTERSTITIAL_DETACHED,
[email protected]fbc5e5f92012-01-02 06:08:321215 content::Source<WebContents>(contents->web_contents()));
[email protected]36b6dcb2008-11-12 01:19:571216}
1217
[email protected]f2cc7382010-10-15 18:03:051218void Browser::TabClosingAt(TabStripModel* tab_strip_model,
[email protected]3d61dd02012-06-07 00:05:531219 TabContents* contents,
[email protected]f2cc7382010-10-15 18:03:051220 int index) {
[email protected]d9083482012-01-06 00:38:461221 fullscreen_controller_->OnTabClosing(contents->web_contents());
[email protected]ad50def52011-10-19 23:17:071222 content::NotificationService::current()->Notify(
[email protected]884033e2012-04-16 19:38:421223 chrome::NOTIFICATION_TAB_CLOSING,
[email protected]c5eed492012-01-04 17:07:501224 content::Source<NavigationController>(
[email protected]cca0f1e2012-01-03 18:27:461225 &contents->web_contents()->GetController()),
[email protected]ad50def52011-10-19 23:17:071226 content::NotificationService::NoDetails());
[email protected]36b6dcb2008-11-12 01:19:571227
[email protected]075969d2012-04-17 20:05:551228 // Sever the WebContents' connection back to us.
[email protected]9219d262011-02-15 21:05:121229 SetAsDelegate(contents, NULL);
[email protected]36b6dcb2008-11-12 01:19:571230}
1231
[email protected]3d61dd02012-06-07 00:05:531232void Browser::TabDetachedAt(TabContents* contents, int index) {
[email protected]c37e3b62010-01-22 00:15:171233 TabDetachedAtImpl(contents, index, DETACH_TYPE_DETACH);
[email protected]36b6dcb2008-11-12 01:19:571234}
1235
[email protected]3d61dd02012-06-07 00:05:531236void Browser::TabDeactivated(TabContents* contents) {
[email protected]01a5a762011-11-21 05:40:551237 fullscreen_controller_->OnTabDeactivated(contents);
[email protected]e3a5d5b2012-06-22 17:56:461238 search_delegate_->OnTabDeactivated(contents);
1239
[email protected]6b723f82010-10-05 20:14:271240 if (instant())
[email protected]3e481282011-10-15 15:39:501241 instant()->Hide();
[email protected]03bb953d2010-09-14 21:38:301242
[email protected]85ff2c42009-05-29 21:22:031243 // Save what the user's currently typing, so it can be restored when we
1244 // switch back to this tab.
[email protected]83a2610a2012-01-05 01:00:271245 window_->GetLocationBar()->SaveStateToContents(contents->web_contents());
[email protected]85ff2c42009-05-29 21:22:031246}
1247
[email protected]3d61dd02012-06-07 00:05:531248void Browser::ActiveTabChanged(TabContents* old_contents,
1249 TabContents* new_contents,
[email protected]3aca95d42011-05-24 22:06:541250 int index,
1251 bool user_gesture) {
[email protected]a8f024392011-01-13 21:50:161252 // On some platforms we want to automatically reload tabs that are
1253 // killed when the user selects them.
[email protected]9d7518f2012-04-17 02:15:201254 bool did_reload = false;
[email protected]d9083482012-01-06 00:38:461255 if (user_gesture && new_contents->web_contents()->GetCrashedStatus() ==
[email protected]a8f024392011-01-13 21:50:161256 base::TERMINATION_STATUS_PROCESS_WAS_KILLED) {
1257 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
[email protected]f8a35772011-02-02 23:17:561258 if (parsed_command_line.HasSwitch(switches::kReloadKilledTabs)) {
[email protected]734d26692012-03-26 18:00:251259 LOG(WARNING) << "Reloading killed tab at " << index;
[email protected]d6d4dae2012-04-18 17:47:321260 static int reload_count = 0;
[email protected]24c463a2012-04-20 22:33:501261 UMA_HISTOGRAM_CUSTOM_COUNTS(
1262 "Tabs.SadTab.ReloadCount", ++reload_count, 1, 1000, 50);
[email protected]a37d4b02012-06-25 21:56:101263 chrome::Reload(this, CURRENT_TAB);
[email protected]9d7518f2012-04-17 02:15:201264 did_reload = true;
[email protected]f8a35772011-02-02 23:17:561265 }
[email protected]a8f024392011-01-13 21:50:161266 }
1267
[email protected]002efb042011-10-07 17:30:241268 // Discarded tabs always get reloaded.
[email protected]52877dbc62012-06-29 22:22:031269 if (!did_reload && tab_strip_model_->IsTabDiscarded(index)) {
[email protected]d6d4dae2012-04-18 17:47:321270 LOG(WARNING) << "Reloading discarded tab at " << index;
1271 static int reload_count = 0;
[email protected]24c463a2012-04-20 22:33:501272 UMA_HISTOGRAM_CUSTOM_COUNTS(
1273 "Tabs.Discard.ReloadCount", ++reload_count, 1, 1000, 50);
[email protected]a37d4b02012-06-25 21:56:101274 chrome::Reload(this, CURRENT_TAB);
[email protected]002efb042011-10-07 17:30:241275 }
1276
[email protected]36b6dcb2008-11-12 01:19:571277 // If we have any update pending, do it now.
[email protected]24f1477532011-11-22 22:55:361278 if (chrome_updater_factory_.HasWeakPtrs() && old_contents)
[email protected]36b6dcb2008-11-12 01:19:571279 ProcessPendingUIUpdates();
1280
[email protected]36b6dcb2008-11-12 01:19:571281 // Propagate the profile to the location bar.
1282 UpdateToolbar(true);
1283
[email protected]e3a5d5b2012-06-22 17:56:461284 // Propagate tab state to toolbar, tab-strip, etc.
1285 UpdateSearchState(new_contents);
1286
[email protected]943d8122010-06-11 02:13:451287 // Update reload/stop state.
[email protected]5d98294912012-06-27 22:57:401288 command_controller_->LoadingStateChanged(
1289 new_contents->web_contents()->IsLoading(), true);
[email protected]36b6dcb2008-11-12 01:19:571290
[email protected]84214982008-12-10 18:49:101291 // Update commands to reflect current state.
[email protected]5d98294912012-06-27 22:57:401292 command_controller_->TabStateChanged();
[email protected]36b6dcb2008-11-12 01:19:571293
1294 // Reset the status bubble.
[email protected]be3877f2009-01-14 15:51:101295 StatusBubble* status_bubble = GetStatusBubble();
1296 if (status_bubble) {
1297 status_bubble->Hide();
[email protected]36b6dcb2008-11-12 01:19:571298
[email protected]be3877f2009-01-14 15:51:101299 // Show the loading state (if any).
[email protected]51d83bdf2011-12-09 16:09:291300 status_bubble->SetStatus(
[email protected]52877dbc62012-06-29 22:22:031301 chrome::GetActiveTabContents(this)->core_tab_helper()->GetStatusText());
[email protected]be3877f2009-01-14 15:51:101302 }
[email protected]36b6dcb2008-11-12 01:19:571303
[email protected]24db8a072009-10-29 20:35:371304 if (HasFindBarController()) {
[email protected]9c318862011-02-01 22:27:241305 find_bar_controller_->ChangeTabContents(new_contents);
[email protected]b77cb302009-10-29 04:09:171306 find_bar_controller_->find_bar()->MoveWindowIfNecessary(gfx::Rect(), true);
[email protected]4801ecc2009-04-05 04:52:581307 }
1308
[email protected]36b6dcb2008-11-12 01:19:571309 // Update sessions. Don't force creation of sessions. If sessions doesn't
1310 // exist, the change will be picked up by sessions when created.
[email protected]92371eb2011-04-28 11:50:151311 SessionService* session_service =
1312 SessionServiceFactory::GetForProfileIfExisting(profile_);
[email protected]2fc15ae2012-05-06 00:01:371313 if (session_service && !tab_strip_model_->closing_all()) {
[email protected]41f022e2012-01-31 05:56:401314 session_service->SetSelectedTabInWindow(session_id(), active_index());
[email protected]36b6dcb2008-11-12 01:19:571315 }
[email protected]09b29342011-06-24 19:18:481316
1317 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_SWITCH);
[email protected]36b6dcb2008-11-12 01:19:571318}
1319
[email protected]3d61dd02012-06-07 00:05:531320void Browser::TabMoved(TabContents* contents,
[email protected]36b6dcb2008-11-12 01:19:571321 int from_index,
[email protected]1a242c32010-02-02 21:20:541322 int to_index) {
[email protected]36b6dcb2008-11-12 01:19:571323 DCHECK(from_index >= 0 && to_index >= 0);
1324 // Notify the history service.
1325 SyncHistoryWithTabs(std::min(from_index, to_index));
1326}
1327
[email protected]da25dd52011-01-11 19:06:301328void Browser::TabReplacedAt(TabStripModel* tab_strip_model,
[email protected]3d61dd02012-06-07 00:05:531329 TabContents* old_contents,
1330 TabContents* new_contents,
[email protected]8a34e6602010-10-02 17:29:431331 int index) {
[email protected]c37e3b62010-01-22 00:15:171332 TabDetachedAtImpl(old_contents, index, DETACH_TYPE_REPLACE);
[email protected]41f022e2012-01-31 05:56:401333 TabInsertedAt(new_contents, index, (index == active_index()));
[email protected]c37e3b62010-01-22 00:15:171334
[email protected]a26023822011-12-29 00:23:551335 int entry_count =
[email protected]d9083482012-01-06 00:38:461336 new_contents->web_contents()->GetController().GetEntryCount();
[email protected]c37e3b62010-01-22 00:15:171337 if (entry_count > 0) {
1338 // Send out notification so that observers are updated appropriately.
[email protected]d9083482012-01-06 00:38:461339 new_contents->web_contents()->GetController().NotifyEntryChanged(
1340 new_contents->web_contents()->GetController().GetEntryAtIndex(
[email protected]4a63e242011-12-12 15:23:081341 entry_count - 1),
[email protected]c37e3b62010-01-22 00:15:171342 entry_count - 1);
1343 }
[email protected]47e020a2010-10-15 14:43:371344
[email protected]92371eb2011-04-28 11:50:151345 SessionService* session_service =
1346 SessionServiceFactory::GetForProfile(profile());
[email protected]47e020a2010-10-15 14:43:371347 if (session_service) {
1348 // The new_contents may end up with a different navigation stack. Force
1349 // the session service to update itself.
[email protected]52877dbc62012-06-29 22:22:031350 session_service->TabRestored(new_contents,
1351 tab_strip_model_->IsTabPinned(index));
[email protected]47e020a2010-10-15 14:43:371352 }
[email protected]2914600d2011-01-31 23:25:141353
[email protected]a0262432012-04-13 15:48:091354 content::DevToolsManager::GetInstance()->ContentsReplaced(
[email protected]d9083482012-01-06 00:38:461355 old_contents->web_contents(), new_contents->web_contents());
[email protected]c37e3b62010-01-22 00:15:171356}
1357
[email protected]3d61dd02012-06-07 00:05:531358void Browser::TabPinnedStateChanged(TabContents* contents, int index) {
[email protected]92371eb2011-04-28 11:50:151359 SessionService* session_service =
1360 SessionServiceFactory::GetForProfileIfExisting(profile());
[email protected]5c0e6482009-07-14 20:20:091361 if (session_service) {
1362 session_service->SetPinnedState(
1363 session_id(),
[email protected]52877dbc62012-06-29 22:22:031364 chrome::GetTabContentsAt(this, index)->restore_tab_helper()->
1365 session_id(),
1366 tab_strip_model_->IsTabPinned(index));
[email protected]5c0e6482009-07-14 20:20:091367 }
1368}
1369
[email protected]36b6dcb2008-11-12 01:19:571370void Browser::TabStripEmpty() {
1371 // Close the frame after we return to the message loop (not immediately,
1372 // otherwise it will destroy this object before the stack has a chance to
1373 // cleanly unwind.)
1374 // Note: This will be called several times if TabStripEmpty is called several
1375 // times. This is because it does not close the window if tabs are
1376 // still present.
[email protected]0586b0e2010-02-12 21:38:371377 MessageLoop::current()->PostTask(
[email protected]24f1477532011-11-22 22:55:361378 FROM_HERE, base::Bind(&Browser::CloseFrame, weak_factory_.GetWeakPtr()));
[email protected]49d864f2012-04-09 21:14:391379 // Set is_attempting_to_close_browser_ here, so that extensions, etc, do not
1380 // attempt to add tabs to the browser before it closes.
1381 is_attempting_to_close_browser_ = true;
[email protected]36b6dcb2008-11-12 01:19:571382}
1383
[email protected]fc761142012-06-08 04:49:201384bool Browser::PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
1385 bool* is_keyboard_shortcut) {
1386 // Escape exits tabbed fullscreen mode.
1387 // TODO(koz): Write a test for this http://crbug.com/100441.
1388 if (event.windowsKeyCode == 27 &&
1389 fullscreen_controller_->HandleUserPressedEscape()) {
1390 return true;
1391 }
1392 return window()->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
1393}
1394
1395void Browser::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
1396 window()->HandleKeyboardEvent(event);
1397}
1398
[email protected]92655362012-04-20 21:33:171399void Browser::OnAcceptFullscreenPermission(
1400 const GURL& url,
1401 FullscreenExitBubbleType bubble_type) {
1402 fullscreen_controller_->OnAcceptFullscreenPermission(url, bubble_type);
1403}
1404
1405void Browser::OnDenyFullscreenPermission(FullscreenExitBubbleType bubble_type) {
1406 fullscreen_controller_->OnDenyFullscreenPermission(bubble_type);
1407}
1408
1409bool Browser::TabsNeedBeforeUnloadFired() {
1410 if (tabs_needing_before_unload_fired_.empty()) {
1411 for (int i = 0; i < tab_count(); ++i) {
[email protected]52877dbc62012-06-29 22:22:031412 WebContents* contents = chrome::GetTabContentsAt(this, i)->web_contents();
[email protected]92655362012-04-20 21:33:171413 if (contents->NeedToFireBeforeUnload())
1414 tabs_needing_before_unload_fired_.insert(contents);
1415 }
1416 }
1417 return !tabs_needing_before_unload_fired_.empty();
1418}
1419
1420bool Browser::IsFullscreenForTabOrPending() const {
1421 return fullscreen_controller_->IsFullscreenForTabOrPending();
1422}
1423
[email protected]d013f142012-04-27 17:31:141424bool Browser::IsMouseLocked() const {
1425 return fullscreen_controller_->IsMouseLocked();
[email protected]92655362012-04-20 21:33:171426}
1427
1428void Browser::OnWindowDidShow() {
1429 if (window_has_shown_)
1430 return;
1431 window_has_shown_ = true;
1432
1433 // Nothing to do for non-tabbed windows.
1434 if (!is_type_tabbed())
1435 return;
1436
1437 // Show any pending global error bubble.
1438 GlobalErrorService* service =
1439 GlobalErrorServiceFactory::GetForProfile(profile());
1440 GlobalError* error = service->GetFirstGlobalErrorWithBubbleView();
1441 if (error)
1442 error->ShowBubbleView(this);
1443}
1444
1445void Browser::ShowFirstRunBubble() {
1446 window()->GetLocationBar()->ShowFirstRunBubble();
1447}
1448
1449///////////////////////////////////////////////////////////////////////////////
1450// Browser, protected:
1451
1452BrowserWindow* Browser::CreateBrowserWindow() {
1453 bool create_panel = false;
1454#if defined(USE_ASH)
1455 if (CommandLine::ForCurrentProcess()->HasSwitch(
1456 ash::switches::kAuraPanelManager))
1457 create_panel = is_type_panel();
1458#elif !defined(OS_CHROMEOS)
1459 create_panel = is_type_panel();
1460#endif
1461 if (create_panel)
[email protected]4d617832012-05-22 19:10:261462 return PanelManager::GetInstance()->CreatePanel(this)->browser_window();
[email protected]92655362012-04-20 21:33:171463
1464 return BrowserWindow::CreateBrowserWindow(this);
1465}
1466
[email protected]36b6dcb2008-11-12 01:19:571467///////////////////////////////////////////////////////////////////////////////
[email protected]674bc592011-12-20 23:00:421468// Browser, content::WebContentsDelegate implementation:
initial.commit09911bf2008-07-26 23:55:291469
[email protected]e5d549d2011-12-28 01:29:201470WebContents* Browser::OpenURLFromTab(WebContents* source,
[email protected]00c37fc2011-08-02 00:22:501471 const OpenURLParams& params) {
1472 browser::NavigateParams nav_params(this, params.url, params.transition);
[email protected]52877dbc62012-06-29 22:22:031473 nav_params.source_contents = chrome::GetTabContentsAt(this,
[email protected]e52d0a42012-06-08 22:44:161474 tab_strip_model_->GetIndexOfWebContents(source));
[email protected]cb8fb01a2011-12-05 21:01:141475 nav_params.referrer = params.referrer;
[email protected]00c37fc2011-08-02 00:22:501476 nav_params.disposition = params.disposition;
1477 nav_params.tabstrip_add_types = TabStripModel::ADD_NONE;
[email protected]e35ccd552012-05-23 16:22:471478 nav_params.window_action = browser::NavigateParams::SHOW_WINDOW;
[email protected]00c37fc2011-08-02 00:22:501479 nav_params.user_gesture = true;
1480 nav_params.override_encoding = params.override_encoding;
[email protected]e47ae9472011-10-13 19:48:341481 nav_params.is_renderer_initiated = params.is_renderer_initiated;
[email protected]4ad5d77d2011-12-03 02:00:481482 nav_params.transferred_global_request_id =
1483 params.transferred_global_request_id;
[email protected]00c37fc2011-08-02 00:22:501484 browser::Navigate(&nav_params);
1485
1486 return nav_params.target_contents ?
[email protected]d9083482012-01-06 00:38:461487 nav_params.target_contents->web_contents() : NULL;
initial.commit09911bf2008-07-26 23:55:291488}
1489
[email protected]2a6bc3e2011-12-28 23:51:331490void Browser::NavigationStateChanged(const WebContents* source,
initial.commit09911bf2008-07-26 23:55:291491 unsigned changed_flags) {
initial.commit09911bf2008-07-26 23:55:291492 // Only update the UI when something visible has changed.
[email protected]e83f1682008-09-07 23:57:401493 if (changed_flags)
initial.commit09911bf2008-07-26 23:55:291494 ScheduleUIUpdate(source, changed_flags);
1495
[email protected]83c2e232011-10-07 21:36:461496 // We can synchronously update commands since they will only change once per
1497 // navigation, so we don't have to worry about flickering. We do, however,
1498 // need to update the command state early on load to always present usable
1499 // actions in the face of slow-to-commit pages.
[email protected]d9083482012-01-06 00:38:461500 if (changed_flags & (content::INVALIDATE_TYPE_URL |
1501 content::INVALIDATE_TYPE_LOAD))
[email protected]5d98294912012-06-27 22:57:401502 command_controller_->TabStateChanged();
initial.commit09911bf2008-07-26 23:55:291503}
1504
[email protected]2a6bc3e2011-12-28 23:51:331505void Browser::AddNewContents(WebContents* source,
1506 WebContents* new_contents,
initial.commit09911bf2008-07-26 23:55:291507 WindowOpenDisposition disposition,
1508 const gfx::Rect& initial_pos,
1509 bool user_gesture) {
[email protected]52877dbc62012-06-29 22:22:031510 chrome::AddWebContents(this, source, new_contents, disposition, initial_pos,
1511 user_gesture);
initial.commit09911bf2008-07-26 23:55:291512}
1513
[email protected]2a6bc3e2011-12-28 23:51:331514void Browser::ActivateContents(WebContents* contents) {
[email protected]52877dbc62012-06-29 22:22:031515 chrome::ActivateTabAt(this, tab_strip_model_->GetIndexOfWebContents(contents),
1516 false);
[email protected]f3e99e32008-07-30 04:48:391517 window_->Activate();
initial.commit09911bf2008-07-26 23:55:291518}
1519
[email protected]2a6bc3e2011-12-28 23:51:331520void Browser::DeactivateContents(WebContents* contents) {
[email protected]ea42e7782010-08-23 23:58:121521 window_->Deactivate();
1522}
1523
[email protected]2a6bc3e2011-12-28 23:51:331524void Browser::LoadingStateChanged(WebContents* source) {
[email protected]2fc15ae2012-05-06 00:01:371525 window_->UpdateLoadingAnimations(tab_strip_model_->TabsAreLoading());
[email protected]f3e99e32008-07-30 04:48:391526 window_->UpdateTitleBar();
initial.commit09911bf2008-07-26 23:55:291527
[email protected]52877dbc62012-06-29 22:22:031528 WebContents* selected_contents = chrome::GetActiveWebContents(this);
[email protected]3c9e1872010-11-18 16:17:491529 if (source == selected_contents) {
[email protected]f5d978c2011-07-21 14:43:511530 bool is_loading = source->IsLoading();
[email protected]5d98294912012-06-27 22:57:401531 command_controller_->LoadingStateChanged(is_loading, false);
[email protected]43d259d2010-09-03 21:37:461532 if (GetStatusBubble()) {
[email protected]0dd3a0ab2011-02-18 08:17:441533 GetStatusBubble()->SetStatus(
[email protected]52877dbc62012-06-29 22:22:031534 chrome::GetActiveTabContents(this)->core_tab_helper()->
1535 GetStatusText());
[email protected]43d259d2010-09-03 21:37:461536 }
[email protected]eabfdae92009-12-11 06:13:511537
[email protected]f5d978c2011-07-21 14:43:511538 if (!is_loading && pending_web_app_action_ == UPDATE_SHORTCUT) {
[email protected]867dff82010-04-23 21:30:331539 // Schedule a shortcut update when web application info is available if
1540 // last committed entry is not NULL. Last committed entry could be NULL
1541 // when an interstitial page is injected (e.g. bad https certificate,
1542 // malware site etc). When this happens, we abort the shortcut update.
[email protected]10f417c52011-12-28 21:04:231543 NavigationEntry* entry = source->GetController().GetLastCommittedEntry();
[email protected]eabfdae92009-12-11 06:13:511544 if (entry) {
[email protected]1d3e9172012-06-12 02:59:131545 TabContents::FromWebContents(source)->
[email protected]36fc0392011-12-25 03:59:511546 extension_tab_helper()->GetApplicationInfo(entry->GetPageID());
[email protected]eabfdae92009-12-11 06:13:511547 } else {
1548 pending_web_app_action_ = NONE;
[email protected]eabfdae92009-12-11 06:13:511549 }
1550 }
initial.commit09911bf2008-07-26 23:55:291551 }
1552}
1553
[email protected]a81343d232011-12-27 07:39:201554void Browser::CloseContents(WebContents* source) {
[email protected]04b4a6c2008-08-02 00:44:471555 if (is_attempting_to_close_browser_) {
1556 // If we're trying to close the browser, just clear the state related to
[email protected]adf650f2008-12-09 16:10:061557 // waiting for unload to fire. Don't actually try to close the tab as it
[email protected]04b4a6c2008-08-02 00:44:471558 // will go down the slow shutdown path instead of the fast path of killing
1559 // all the renderer processes.
[email protected]08f0d612011-01-13 23:58:211560 ClearUnloadState(source, true);
[email protected]04b4a6c2008-08-02 00:44:471561 return;
1562 }
1563
[email protected]52877dbc62012-06-29 22:22:031564 chrome::CloseWebContents(this, source);
initial.commit09911bf2008-07-26 23:55:291565}
1566
[email protected]2a6bc3e2011-12-28 23:51:331567void Browser::MoveContents(WebContents* source, const gfx::Rect& pos) {
[email protected]b35b26b32011-05-05 20:35:141568 if (!IsPopupOrPanel(source)) {
initial.commit09911bf2008-07-26 23:55:291569 NOTREACHED() << "moving invalid browser type";
1570 return;
1571 }
[email protected]e561e202010-03-24 17:57:121572 window_->SetBounds(pos);
initial.commit09911bf2008-07-26 23:55:291573}
1574
[email protected]2a6bc3e2011-12-28 23:51:331575bool Browser::IsPopupOrPanel(const WebContents* source) const {
initial.commit09911bf2008-07-26 23:55:291576 // A non-tabbed BROWSER is an unconstrained popup.
[email protected]b35b26b32011-05-05 20:35:141577 return is_type_popup() || is_type_panel();
initial.commit09911bf2008-07-26 23:55:291578}
1579
[email protected]2a6bc3e2011-12-28 23:51:331580void Browser::UpdateTargetURL(WebContents* source, int32 page_id,
[email protected]d952a052011-09-06 18:42:451581 const GURL& url) {
[email protected]be3877f2009-01-14 15:51:101582 if (!GetStatusBubble())
1583 return;
1584
[email protected]52877dbc62012-06-29 22:22:031585 if (source == chrome::GetActiveWebContents(this)) {
[email protected]36b6dcb2008-11-12 01:19:571586 PrefService* prefs = profile_->GetPrefs();
[email protected]65233052011-08-22 19:02:481587 GetStatusBubble()->SetURL(url, prefs->GetString(prefs::kAcceptLanguages));
[email protected]36b6dcb2008-11-12 01:19:571588 }
1589}
1590
[email protected]92655362012-04-20 21:33:171591void Browser::ContentsMouseEvent(
1592 WebContents* source, const gfx::Point& location, bool motion) {
1593 if (!GetStatusBubble())
1594 return;
1595
[email protected]52877dbc62012-06-29 22:22:031596 if (source == chrome::GetActiveWebContents(this)) {
[email protected]92655362012-04-20 21:33:171597 GetStatusBubble()->MouseMoved(location, !motion);
1598 if (!motion)
1599 GetStatusBubble()->SetURL(GURL(), std::string());
1600 }
[email protected]3a6a3b62009-05-27 21:36:201601}
1602
[email protected]36b6dcb2008-11-12 01:19:571603void Browser::ContentsZoomChange(bool zoom_in) {
[email protected]5d98294912012-06-27 22:57:401604 chrome::ExecuteCommand(this, zoom_in ? IDC_ZOOM_PLUS : IDC_ZOOM_MINUS);
[email protected]36b6dcb2008-11-12 01:19:571605}
1606
[email protected]2a6bc3e2011-12-28 23:51:331607void Browser::WebContentsFocused(WebContents* contents) {
1608 window_->WebContentsFocused(contents);
[email protected]7e383692009-06-12 19:14:541609}
1610
[email protected]130efb02009-09-18 18:54:351611bool Browser::TakeFocus(bool reverse) {
[email protected]ad50def52011-10-19 23:17:071612 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271613 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:531614 content::Source<Browser>(this),
[email protected]ad50def52011-10-19 23:17:071615 content::NotificationService::NoDetails());
[email protected]130efb02009-09-18 18:54:351616 return false;
1617}
1618
[email protected]36b6dcb2008-11-12 01:19:571619bool Browser::IsApplication() const {
[email protected]b35b26b32011-05-05 20:35:141620 return is_app();
[email protected]36b6dcb2008-11-12 01:19:571621}
1622
[email protected]b7a756d42012-01-23 18:08:171623gfx::Rect Browser::GetRootWindowResizerRect() const {
1624 return window_->GetRootWindowResizerRect();
1625}
1626
[email protected]1d3e9172012-06-12 02:59:131627void Browser::BeforeUnloadFired(WebContents* web_contents,
[email protected]36b6dcb2008-11-12 01:19:571628 bool proceed,
1629 bool* proceed_to_fire_unload) {
1630 if (!is_attempting_to_close_browser_) {
1631 *proceed_to_fire_unload = proceed;
[email protected]0cce15f2010-05-21 16:58:391632 if (!proceed)
[email protected]1d3e9172012-06-12 02:59:131633 web_contents->SetClosedByUserGesture(false);
[email protected]36b6dcb2008-11-12 01:19:571634 return;
1635 }
1636
1637 if (!proceed) {
1638 CancelWindowClose();
1639 *proceed_to_fire_unload = false;
[email protected]1d3e9172012-06-12 02:59:131640 web_contents->SetClosedByUserGesture(false);
[email protected]36b6dcb2008-11-12 01:19:571641 return;
1642 }
1643
[email protected]1d3e9172012-06-12 02:59:131644 if (RemoveFromSet(&tabs_needing_before_unload_fired_, web_contents)) {
[email protected]2d46c842008-11-14 19:24:311645 // Now that beforeunload has fired, put the tab on the queue to fire
1646 // unload.
[email protected]1d3e9172012-06-12 02:59:131647 tabs_needing_unload_fired_.insert(web_contents);
[email protected]36b6dcb2008-11-12 01:19:571648 ProcessPendingTabs();
[email protected]adf650f2008-12-09 16:10:061649 // We want to handle firing the unload event ourselves since we want to
[email protected]36b6dcb2008-11-12 01:19:571650 // fire all the beforeunload events before attempting to fire the unload
1651 // events should the user cancel closing the browser.
1652 *proceed_to_fire_unload = false;
1653 return;
1654 }
1655
1656 *proceed_to_fire_unload = true;
1657}
1658
[email protected]a26dc362010-04-23 01:48:581659void Browser::SetFocusToLocationBar(bool select_all) {
[email protected]1a380622009-02-17 17:33:131660 // Two differences between this and FocusLocationBar():
1661 // (1) This doesn't get recorded in user metrics, since it's called
1662 // internally.
1663 // (2) This checks whether the location bar can be focused, and if not, clears
1664 // the focus. FocusLocationBar() is only reached when the location bar is
1665 // focusable, but this may be reached at other times, e.g. while in
1666 // fullscreen mode, where we need to leave focus in a consistent state.
[email protected]a26dc362010-04-23 01:48:581667 window_->SetFocusToLocationBar(select_all);
[email protected]4bcefee2009-01-29 15:57:571668}
1669
[email protected]7d5925a2009-03-03 02:59:121670void Browser::RenderWidgetShowing() {
1671 window_->DisableInactiveFrame();
1672}
[email protected]a239c3f2009-02-17 22:13:191673
[email protected]0a2aeb82009-05-15 21:52:481674int Browser::GetExtraRenderViewHeight() const {
1675 return window_->GetExtraRenderViewHeight();
1676}
1677
[email protected]2a6bc3e2011-12-28 23:51:331678void Browser::OnStartDownload(WebContents* source,
[email protected]e582fdd2011-12-20 16:48:171679 content::DownloadItem* download) {
[email protected]1d3e9172012-06-12 02:59:131680 TabContents* tab_contents = TabContents::FromWebContents(source);
[email protected]2ae41f9d2012-06-11 20:13:381681 TabContents* constrained = GetConstrainingTabContents(tab_contents);
[email protected]3d61dd02012-06-07 00:05:531682 if (constrained != tab_contents) {
[email protected]686493142011-07-15 21:47:221683 // Download in a constrained popup is shown in the tab that opened it.
[email protected]d9083482012-01-06 00:38:461684 WebContents* constrained_tab = constrained->web_contents();
[email protected]6934a702011-12-20 00:04:511685 constrained_tab->GetDelegate()->OnStartDownload(constrained_tab, download);
[email protected]686493142011-07-15 21:47:221686 return;
1687 }
1688
1689 if (!window())
1690 return;
1691
[email protected]27c14f02012-06-22 17:29:581692 // GetDownloadShelf creates the download shelf if it was not yet created.
1693 DownloadShelf* shelf = window()->GetDownloadShelf();
1694 shelf->AddDownload(new DownloadItemModel(download));
1695 // Don't show the animation for "Save file" downloads.
1696 // For non-theme extensions, we don't show the download animation.
1697 // Show animation in same window as the download shelf. Download shelf
1698 // may not be in the same window that initiated the download, e.g.
1699 // Panels.
1700 // Don't show the animation if the selected tab is not visible (i.e. the
1701 // window is minimized, we're in a unit test, etc.).
[email protected]52877dbc62012-06-29 22:22:031702 WebContents* shelf_tab = chrome::GetActiveWebContents(shelf->browser());
[email protected]27c14f02012-06-22 17:29:581703 if ((download->GetTotalBytes() > 0) &&
1704 !download_crx_util::IsExtensionDownload(*download) &&
1705 platform_util::IsVisible(shelf_tab->GetNativeView()) &&
1706 ui::Animation::ShouldRenderRichAnimation()) {
1707 DownloadStartedAnimation::Show(shelf_tab);
[email protected]a0e4b072011-08-17 01:47:071708 }
[email protected]686493142011-07-15 21:47:221709
1710 // If the download occurs in a new tab, close it.
[email protected]f5fa20e2011-12-21 22:35:561711 if (source->GetController().IsInitialNavigation() && tab_count() > 1)
[email protected]686493142011-07-15 21:47:221712 CloseContents(source);
1713}
1714
[email protected]2a6bc3e2011-12-28 23:51:331715void Browser::ViewSourceForTab(WebContents* source, const GURL& page_url) {
[email protected]1788e772010-12-15 16:40:501716 DCHECK(source);
[email protected]52877dbc62012-06-29 22:22:031717 TabContents* tab_contents = chrome::GetTabContentsAt(this,
[email protected]e52d0a42012-06-08 22:44:161718 tab_strip_model_->GetIndexOfWebContents(source));
[email protected]2cd4fde2012-06-26 03:10:261719 chrome::ViewSource(this, tab_contents);
[email protected]77d8d622010-12-15 10:30:121720}
1721
[email protected]2a6bc3e2011-12-28 23:51:331722void Browser::ViewSourceForFrame(WebContents* source,
[email protected]932b7a12011-03-09 12:50:271723 const GURL& frame_url,
1724 const std::string& frame_content_state) {
1725 DCHECK(source);
[email protected]52877dbc62012-06-29 22:22:031726 TabContents* tab_contents = chrome::GetTabContentsAt(this,
[email protected]e52d0a42012-06-08 22:44:161727 tab_strip_model_->GetIndexOfWebContents(source));
[email protected]2cd4fde2012-06-26 03:10:261728 chrome::ViewSource(this, tab_contents, frame_url, frame_content_state);
[email protected]932b7a12011-03-09 12:50:271729}
1730
[email protected]4fdf6742012-01-10 20:14:361731void Browser::ShowRepostFormWarningDialog(WebContents* source) {
[email protected]67baffc82011-12-19 18:03:071732 browser::ShowTabModalConfirmDialog(
[email protected]2a6bc3e2011-12-28 23:51:331733 new RepostFormWarningController(source),
[email protected]1d3e9172012-06-12 02:59:131734 TabContents::FromWebContents(source));
[email protected]14f3408a2009-08-31 20:53:531735}
1736
[email protected]ec0b6c42010-08-26 03:16:581737bool Browser::ShouldAddNavigationToHistory(
1738 const history::HistoryAddPageArgs& add_page_args,
[email protected]610cbb82011-10-18 16:35:081739 content::NavigationType navigation_type) {
[email protected]2e29d922009-10-22 22:13:351740 // Don't update history if running as app.
1741 return !IsApplication();
1742}
1743
[email protected]4fdf6742012-01-10 20:14:361744bool Browser::ShouldCreateWebContents(
1745 WebContents* web_contents,
1746 int route_id,
1747 WindowContainerType window_container_type,
[email protected]03b6d552012-03-29 04:03:011748 const string16& frame_name,
1749 const GURL& target_url) {
[email protected]4fdf6742012-01-10 20:14:361750 if (window_container_type == WINDOW_CONTAINER_TYPE_BACKGROUND) {
1751 // If a BackgroundContents is created, suppress the normal WebContents.
1752 return !MaybeCreateBackgroundContents(
[email protected]03b6d552012-03-29 04:03:011753 route_id, web_contents, frame_name, target_url);
[email protected]4fdf6742012-01-10 20:14:361754 }
1755
1756 return true;
1757}
1758
1759void Browser::WebContentsCreated(WebContents* source_contents,
1760 int64 source_frame_id,
1761 const GURL& target_url,
1762 WebContents* new_contents) {
[email protected]3d61dd02012-06-07 00:05:531763 // Create a TabContents now, so all observers are in place, as the network
1764 // requests for its initial navigation will start immediately. The WebContents
1765 // will later be inserted into this browser using Browser::Navigate via
1766 // AddNewContents. The latter will retrieve the newly created TabContents from
1767 // WebContents object.
1768 new TabContents(new_contents);
[email protected]4fdf6742012-01-10 20:14:361769
1770 // Notify.
1771 RetargetingDetails details;
1772 details.source_web_contents = source_contents;
1773 details.source_frame_id = source_frame_id;
1774 details.target_url = target_url;
1775 details.target_web_contents = new_contents;
1776 details.not_yet_in_tabstrip = true;
1777 content::NotificationService::current()->Notify(
1778 chrome::NOTIFICATION_RETARGETING,
1779 content::Source<Profile>(profile_),
1780 content::Details<RetargetingDetails>(&details));
[email protected]80c7b802011-10-28 19:59:081781}
1782
[email protected]2a6bc3e2011-12-28 23:51:331783void Browser::ContentRestrictionsChanged(WebContents* source) {
[email protected]5d98294912012-06-27 22:57:401784 command_controller_->ContentRestrictionsChanged();
[email protected]9e823662010-10-13 23:36:001785}
1786
[email protected]2a6bc3e2011-12-28 23:51:331787void Browser::RendererUnresponsive(WebContents* source) {
[email protected]c19d9542011-11-15 22:26:421788 // Ignore hangs if print preview is open.
[email protected]1d3e9172012-06-12 02:59:131789 TabContents* tab_contents = TabContents::FromWebContents(source);
[email protected]3d61dd02012-06-07 00:05:531790 if (tab_contents) {
[email protected]c19d9542011-11-15 22:26:421791 printing::PrintPreviewTabController* controller =
1792 printing::PrintPreviewTabController::GetInstance();
1793 if (controller) {
[email protected]3d61dd02012-06-07 00:05:531794 TabContents* preview_tab =
1795 controller->GetPrintPreviewForTab(tab_contents);
1796 if (preview_tab && preview_tab != tab_contents) {
[email protected]c19d9542011-11-15 22:26:421797 return;
1798 }
1799 }
1800 }
1801
[email protected]55452902011-06-01 21:57:471802 browser::ShowHungRendererDialog(source);
1803}
1804
[email protected]2a6bc3e2011-12-28 23:51:331805void Browser::RendererResponsive(WebContents* source) {
[email protected]55452902011-06-01 21:57:471806 browser::HideHungRendererDialog(source);
1807}
1808
[email protected]2a6bc3e2011-12-28 23:51:331809void Browser::WorkerCrashed(WebContents* source) {
[email protected]1d3e9172012-06-12 02:59:131810 TabContents* tab_contents = TabContents::FromWebContents(source);
[email protected]3d61dd02012-06-07 00:05:531811 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper();
[email protected]95a33ed62011-09-30 15:07:081812 infobar_helper->AddInfoBar(new SimpleAlertInfoBarDelegate(
1813 infobar_helper,
1814 NULL,
1815 l10n_util::GetStringUTF16(IDS_WEBWORKER_CRASHED_PROMPT),
[email protected]55452902011-06-01 21:57:471816 true));
[email protected]0dd3a0ab2011-02-18 08:17:441817}
1818
[email protected]1d3e9172012-06-12 02:59:131819void Browser::DidNavigateMainFramePostCommit(WebContents* web_contents) {
[email protected]52877dbc62012-06-29 22:22:031820 if (web_contents == chrome::GetActiveWebContents(this))
[email protected]09b29342011-06-24 19:18:481821 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]93f230e02011-06-01 14:40:001822}
1823
[email protected]1d3e9172012-06-12 02:59:131824void Browser::DidNavigateToPendingEntry(WebContents* web_contents) {
[email protected]52877dbc62012-06-29 22:22:031825 if (web_contents == chrome::GetActiveWebContents(this))
[email protected]09b29342011-06-24 19:18:481826 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]93f230e02011-06-01 14:40:001827}
1828
[email protected]3ab9cb82011-06-03 18:02:071829content::JavaScriptDialogCreator* Browser::GetJavaScriptDialogCreator() {
1830 return GetJavaScriptDialogCreatorInstance();
1831}
1832
[email protected]1d3e9172012-06-12 02:59:131833content::ColorChooser* Browser::OpenColorChooser(WebContents* web_contents,
[email protected]da8543762012-03-20 08:52:201834 int color_chooser_id,
[email protected]55578b0a2012-04-18 14:31:321835 SkColor color) {
[email protected]da8543762012-03-20 08:52:201836#if defined(OS_WIN)
1837 // On Windows, only create a color chooser if one doesn't exist, because we
1838 // can't close the old color chooser dialog.
1839 if (!color_chooser_.get())
[email protected]1d3e9172012-06-12 02:59:131840 color_chooser_.reset(content::ColorChooser::Create(color_chooser_id,
1841 web_contents,
[email protected]da8543762012-03-20 08:52:201842 color));
1843#else
1844 if (color_chooser_.get())
1845 color_chooser_->End();
[email protected]1d3e9172012-06-12 02:59:131846 color_chooser_.reset(content::ColorChooser::Create(color_chooser_id,
1847 web_contents,
[email protected]da8543762012-03-20 08:52:201848 color));
1849#endif
1850 return color_chooser_.get();
1851}
1852
1853void Browser::DidEndColorChooser() {
1854 color_chooser_.reset();
1855}
1856
[email protected]1d3e9172012-06-12 02:59:131857void Browser::RunFileChooser(WebContents* web_contents,
[email protected]8caadeb2011-11-22 02:45:231858 const content::FileChooserParams& params) {
[email protected]1d3e9172012-06-12 02:59:131859 FileSelectHelper::RunFileChooser(web_contents, params);
[email protected]3a29a6e2011-08-24 18:26:211860}
1861
[email protected]1d3e9172012-06-12 02:59:131862void Browser::EnumerateDirectory(WebContents* web_contents,
[email protected]33f8ad52012-05-22 18:10:131863 int request_id,
[email protected]3a29a6e2011-08-24 18:26:211864 const FilePath& path) {
[email protected]1d3e9172012-06-12 02:59:131865 FileSelectHelper::EnumerateDirectory(web_contents, request_id, path);
[email protected]3a29a6e2011-08-24 18:26:211866}
1867
[email protected]1d3e9172012-06-12 02:59:131868void Browser::ToggleFullscreenModeForTab(WebContents* web_contents,
[email protected]01a5a762011-11-21 05:40:551869 bool enter_fullscreen) {
[email protected]1d3e9172012-06-12 02:59:131870 fullscreen_controller_->ToggleFullscreenModeForTab(web_contents,
1871 enter_fullscreen);
[email protected]8a5e0ca2011-08-25 06:30:471872}
1873
[email protected]1d3e9172012-06-12 02:59:131874bool Browser::IsFullscreenForTabOrPending(
1875 const WebContents* web_contents) const {
1876 return fullscreen_controller_->IsFullscreenForTabOrPending(web_contents);
[email protected]5d5f7af2011-10-01 01:38:121877}
1878
[email protected]1d3e9172012-06-12 02:59:131879void Browser::JSOutOfMemory(WebContents* web_contents) {
1880 JSOutOfMemoryHelper(web_contents);
[email protected]7d189022011-08-25 22:54:201881}
1882
[email protected]1d3e9172012-06-12 02:59:131883void Browser::RegisterProtocolHandler(WebContents* web_contents,
[email protected]7d189022011-08-25 22:54:201884 const std::string& protocol,
1885 const GURL& url,
[email protected]3a3b75a2012-06-01 08:38:361886 const string16& title,
1887 bool user_gesture) {
[email protected]1d3e9172012-06-12 02:59:131888 RegisterProtocolHandlerHelper(
[email protected]9b4d241c92012-06-29 19:48:311889 web_contents, protocol, url, title, user_gesture);
[email protected]7d189022011-08-25 22:54:201890}
1891
[email protected]3a3b75a2012-06-01 08:38:361892void Browser::RegisterIntentHandler(
[email protected]1d3e9172012-06-12 02:59:131893 WebContents* web_contents,
[email protected]3a3b75a2012-06-01 08:38:361894 const webkit_glue::WebIntentServiceData& data,
1895 bool user_gesture) {
[email protected]1d3e9172012-06-12 02:59:131896 RegisterIntentHandlerHelper(web_contents, data, user_gesture);
[email protected]7d189022011-08-25 22:54:201897}
1898
[email protected]0d9989d2011-12-21 20:26:001899void Browser::WebIntentDispatch(
[email protected]1d3e9172012-06-12 02:59:131900 WebContents* web_contents,
1901 content::WebIntentsDispatcher* intents_dispatcher) {
[email protected]d9436152012-05-24 03:24:461902 if (!web_intents::IsWebIntentsEnabledForProfile(profile_))
[email protected]828e61af2011-09-14 19:45:061903 return;
1904
[email protected]86238d22012-05-22 20:00:111905 UMA_HISTOGRAM_COUNTS("WebIntents.Dispatch", 1);
1906
[email protected]bd52a982012-06-26 23:34:291907 if (!web_contents) {
1908 // Intent is system-caused and the picker will show over the currently
1909 // active web contents.
[email protected]52877dbc62012-06-29 22:22:031910 web_contents = chrome::GetActiveWebContents(this);
[email protected]bd52a982012-06-26 23:34:291911 }
[email protected]1d3e9172012-06-12 02:59:131912 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
[email protected]3d61dd02012-06-07 00:05:531913 tab_contents->web_intent_picker_controller()->SetIntentsDispatcher(
1914 intents_dispatcher);
1915 tab_contents->web_intent_picker_controller()->ShowDialog(
[email protected]0d9989d2011-12-21 20:26:001916 intents_dispatcher->GetIntent().action,
1917 intents_dispatcher->GetIntent().type);
[email protected]b888919c2011-09-02 00:32:161918}
1919
[email protected]2a6bc3e2011-12-28 23:51:331920void Browser::UpdatePreferredSize(WebContents* source,
[email protected]0548c5352011-09-07 00:33:331921 const gfx::Size& pref_size) {
1922 window_->UpdatePreferredSize(source, pref_size);
1923}
1924
[email protected]61e2b3cc2012-03-02 16:13:341925void Browser::ResizeDueToAutoResize(WebContents* source,
1926 const gfx::Size& new_size) {
1927 window_->ResizeDueToAutoResize(source, new_size);
1928}
1929
[email protected]1d3e9172012-06-12 02:59:131930void Browser::FindReply(WebContents* web_contents,
[email protected]92655362012-04-20 21:33:171931 int request_id,
1932 int number_of_matches,
1933 const gfx::Rect& selection_rect,
1934 int active_match_ordinal,
1935 bool final_update) {
[email protected]1d3e9172012-06-12 02:59:131936 FindReplyHelper(web_contents, request_id, number_of_matches, selection_rect,
[email protected]92655362012-04-20 21:33:171937 active_match_ordinal, final_update);
1938}
1939
[email protected]1d3e9172012-06-12 02:59:131940void Browser::RequestToLockMouse(WebContents* web_contents,
[email protected]a9c81f02012-06-01 00:15:441941 bool user_gesture,
1942 bool last_unlocked_by_target) {
[email protected]1d3e9172012-06-12 02:59:131943 fullscreen_controller_->RequestToLockMouse(web_contents,
[email protected]a9c81f02012-06-01 00:15:441944 user_gesture,
1945 last_unlocked_by_target);
[email protected]55c87fa2011-10-15 07:28:281946}
1947
[email protected]5ef8e372011-10-18 04:25:551948void Browser::LostMouseLock() {
[email protected]01a5a762011-11-21 05:40:551949 fullscreen_controller_->LostMouseLock();
[email protected]5ef8e372011-10-18 04:25:551950}
1951
[email protected]f13b4202012-06-12 23:53:231952void Browser::RequestMediaAccessPermission(
1953 content::WebContents* web_contents,
1954 const content::MediaStreamRequest* request,
1955 const content::MediaResponseCallback& callback) {
1956 TabContents* tab = TabContents::FromWebContents(web_contents);
1957 DCHECK(tab);
1958
[email protected]4afde5c2012-06-25 17:00:531959 scoped_ptr<MediaStreamDevicesController>
1960 controller(new MediaStreamDevicesController(tab->profile(),
1961 request,
1962 callback));
1963 if (!controller->DismissInfoBarAndTakeActionOnSettings()) {
1964 InfoBarTabHelper* infobar_helper = tab->infobar_tab_helper();
1965 InfoBarDelegate* old_infobar = NULL;
1966 for (size_t i = 0; i < infobar_helper->infobar_count(); ++i) {
1967 old_infobar = infobar_helper->GetInfoBarDelegateAt(i)->
1968 AsMediaStreamInfoBarDelegate();
1969 if (old_infobar)
1970 break;
1971 }
[email protected]f13b4202012-06-12 23:53:231972
[email protected]4afde5c2012-06-25 17:00:531973 InfoBarDelegate* infobar =
1974 new MediaStreamInfoBarDelegate(infobar_helper, controller.release());
1975 if (old_infobar)
1976 infobar_helper->ReplaceInfoBar(old_infobar, infobar);
1977 else
1978 infobar_helper->AddInfoBar(infobar);
1979 }
[email protected]f13b4202012-06-12 23:53:231980}
1981
[email protected]36b6dcb2008-11-12 01:19:571982///////////////////////////////////////////////////////////////////////////////
[email protected]51d83bdf2011-12-09 16:09:291983// Browser, CoreTabHelperDelegate implementation:
[email protected]a0366a52011-02-04 20:04:211984
[email protected]3d61dd02012-06-07 00:05:531985void Browser::SwapTabContents(TabContents* old_tab_contents,
1986 TabContents* new_tab_contents) {
[email protected]2fc15ae2012-05-06 00:01:371987 int index = tab_strip_model_->GetIndexOfTabContents(old_tab_contents);
[email protected]92655362012-04-20 21:33:171988 DCHECK_NE(TabStripModel::kNoTab, index);
[email protected]2fc15ae2012-05-06 00:01:371989 tab_strip_model_->ReplaceTabContentsAt(index, new_tab_contents);
[email protected]92655362012-04-20 21:33:171990}
1991
[email protected]e1461952012-06-14 21:05:351992bool Browser::CanReloadContents(TabContents* source) const {
[email protected]5d98294912012-06-27 22:57:401993 return chrome::CanReload(this);
[email protected]e1461952012-06-14 21:05:351994}
1995
1996bool Browser::CanSaveContents(TabContents* source) const {
[email protected]5d98294912012-06-27 22:57:401997 return chrome::CanSavePage(this);
[email protected]e1461952012-06-14 21:05:351998}
1999
[email protected]92655362012-04-20 21:33:172000///////////////////////////////////////////////////////////////////////////////
2001// Browser, SearchEngineTabHelperDelegate implementation:
2002
[email protected]3613347d2012-04-27 20:27:372003void Browser::ConfirmAddSearchProvider(TemplateURL* template_url,
[email protected]92655362012-04-20 21:33:172004 Profile* profile) {
2005 window()->ConfirmAddSearchProvider(template_url, profile);
2006}
2007
2008///////////////////////////////////////////////////////////////////////////////
2009// Browser, ConstrainedWindowTabHelperDelegate implementation:
2010
[email protected]3d61dd02012-06-07 00:05:532011void Browser::SetTabContentBlocked(TabContents* tab_contents, bool blocked) {
2012 int index = tab_strip_model_->GetIndexOfTabContents(tab_contents);
[email protected]92655362012-04-20 21:33:172013 if (index == TabStripModel::kNoTab) {
2014 NOTREACHED();
2015 return;
2016 }
[email protected]2fc15ae2012-05-06 00:01:372017 tab_strip_model_->SetTabBlocked(index, blocked);
[email protected]5d98294912012-06-27 22:57:402018 command_controller_->PrintingStateChanged();
[email protected]52877dbc62012-06-29 22:22:032019 if (!blocked && chrome::GetActiveTabContents(this) == tab_contents)
[email protected]3d61dd02012-06-07 00:05:532020 tab_contents->web_contents()->Focus();
[email protected]92655362012-04-20 21:33:172021}
2022
2023///////////////////////////////////////////////////////////////////////////////
2024// Browser, BlockedContentTabHelperDelegate implementation:
2025
[email protected]2ae41f9d2012-06-11 20:13:382026TabContents* Browser::GetConstrainingTabContents(TabContents* source) {
[email protected]92655362012-04-20 21:33:172027 return source;
2028}
2029
2030///////////////////////////////////////////////////////////////////////////////
2031// Browser, BookmarkTabHelperDelegate implementation:
2032
[email protected]3d61dd02012-06-07 00:05:532033void Browser::URLStarredChanged(TabContents* source, bool starred) {
[email protected]52877dbc62012-06-29 22:22:032034 if (source == chrome::GetActiveTabContents(this))
[email protected]92655362012-04-20 21:33:172035 window_->SetStarredState(starred);
2036}
2037
2038///////////////////////////////////////////////////////////////////////////////
[email protected]6f80e932012-06-04 19:00:072039// Browser, ZoomObserver implementation:
2040
[email protected]3d61dd02012-06-07 00:05:532041void Browser::OnZoomIconChanged(TabContents* source,
[email protected]6f80e932012-06-04 19:00:072042 ZoomController::ZoomIconState state) {
[email protected]52877dbc62012-06-29 22:22:032043 if (source == chrome::GetActiveTabContents(this))
[email protected]6f80e932012-06-04 19:00:072044 window_->SetZoomIconState(state);
2045}
2046
[email protected]3d61dd02012-06-07 00:05:532047void Browser::OnZoomChanged(TabContents* source,
[email protected]6f80e932012-06-04 19:00:072048 int zoom_percent,
2049 bool can_show_bubble) {
[email protected]52877dbc62012-06-29 22:22:032050 if (source == chrome::GetActiveTabContents(this)) {
[email protected]6f80e932012-06-04 19:00:072051 window_->SetZoomIconTooltipPercent(zoom_percent);
2052
2053 // Only show the zoom bubble for zoom changes in the active window.
2054 if (can_show_bubble && window_->IsActive())
2055 window_->ShowZoomBubble(zoom_percent);
2056 }
2057}
2058
2059///////////////////////////////////////////////////////////////////////////////
[email protected]92655362012-04-20 21:33:172060// Browser, ExtensionTabHelperDelegate implementation:
2061
[email protected]3d61dd02012-06-07 00:05:532062void Browser::OnDidGetApplicationInfo(TabContents* source,
[email protected]553602e12011-04-05 17:01:182063 int32 page_id) {
[email protected]52877dbc62012-06-29 22:22:032064 if (chrome::GetActiveTabContents(this) != source)
[email protected]553602e12011-04-05 17:01:182065 return;
2066
[email protected]10f417c52011-12-28 21:04:232067 NavigationEntry* entry =
[email protected]d9083482012-01-06 00:38:462068 source->web_contents()->GetController().GetLastCommittedEntry();
[email protected]36fc0392011-12-25 03:59:512069 if (!entry || (entry->GetPageID() != page_id))
[email protected]553602e12011-04-05 17:01:182070 return;
2071
2072 switch (pending_web_app_action_) {
2073 case CREATE_SHORTCUT: {
2074 window()->ShowCreateWebAppShortcutsDialog(source);
2075 break;
2076 }
2077 case UPDATE_SHORTCUT: {
2078 web_app::UpdateShortcutForTabContents(source);
2079 break;
2080 }
2081 default:
2082 NOTREACHED();
2083 break;
2084 }
2085
2086 pending_web_app_action_ = NONE;
2087}
2088
[email protected]3d61dd02012-06-07 00:05:532089void Browser::OnInstallApplication(TabContents* source,
[email protected]553602e12011-04-05 17:01:182090 const WebApplicationInfo& web_app) {
[email protected]14908b72011-04-20 06:54:362091 ExtensionService* extension_service = profile()->GetExtensionService();
2092 if (!extension_service)
[email protected]553602e12011-04-05 17:01:182093 return;
2094
[email protected]d8c8f25f2011-11-02 18:18:012095 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(
2096 extension_service,
[email protected]14908b72011-04-20 06:54:362097 extension_service->show_extensions_prompts() ?
[email protected]32fc4ff72012-06-15 21:50:012098 new ExtensionInstallPrompt(this) : NULL));
[email protected]553602e12011-04-05 17:01:182099 installer->InstallWebApp(web_app);
2100}
2101
[email protected]c90c6ca2011-02-16 20:11:382102///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:572103// Browser, SelectFileDialog::Listener implementation:
2104
[email protected]561abe62009-04-06 18:08:342105void Browser::FileSelected(const FilePath& path, int index, void* params) {
[email protected]35896a32010-06-09 08:42:512106 profile_->set_last_selected_directory(path.DirName());
[email protected]72cbd322009-04-07 10:17:122107 GURL file_url = net::FilePathToFileURL(path);
[email protected]b7813a22012-04-04 18:41:022108
2109#if defined(OS_CHROMEOS)
2110 gdata::util::ModifyGDataFileResourceUrl(profile_, path, &file_url);
2111#endif
2112
[email protected]e5d549d2011-12-28 01:29:202113 if (file_url.is_empty())
2114 return;
2115
2116 OpenURL(OpenURLParams(
2117 file_url, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
2118 false));
[email protected]36b6dcb2008-11-12 01:19:572119}
2120
2121///////////////////////////////////////////////////////////////////////////////
[email protected]6c2381d2011-10-19 02:52:532122// Browser, content::NotificationObserver implementation:
[email protected]36b6dcb2008-11-12 01:19:572123
[email protected]432115822011-07-10 15:52:272124void Browser::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532125 const content::NotificationSource& source,
2126 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272127 switch (type) {
[email protected]fbc5e5f92012-01-02 06:08:322128 case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED:
[email protected]e83f1682008-09-07 23:57:402129 if (is_attempting_to_close_browser_) {
[email protected]08f0d612011-01-13 23:58:212130 // Pass in false so that we delay processing. We need to delay the
2131 // processing as it may close the tab, which is currently on the call
2132 // stack above us.
[email protected]fbc5e5f92012-01-02 06:08:322133 ClearUnloadState(content::Source<WebContents>(source).ptr(), false);
[email protected]e83f1682008-09-07 23:57:402134 }
2135 break;
2136
[email protected]432115822011-07-10 15:52:272137 case content::NOTIFICATION_SSL_VISIBLE_STATE_CHANGED:
[email protected]e83f1682008-09-07 23:57:402138 // When the current tab's SSL state changes, we need to update the URL
[email protected]90e8d062008-09-08 02:26:322139 // bar to reflect the new state. Note that it's possible for the selected
2140 // tab contents to be NULL. This is because we listen for all sources
2141 // (NavigationControllers) for convenience, so the notification could
2142 // actually be for a different window while we're doing asynchronous
2143 // closing of this one.
[email protected]52877dbc62012-06-29 22:22:032144 if (chrome::GetActiveWebContents(this) &&
2145 &chrome::GetActiveWebContents(this)->GetController() ==
[email protected]c5eed492012-01-04 17:07:502146 content::Source<NavigationController>(source).ptr())
[email protected]36b6dcb2008-11-12 01:19:572147 UpdateToolbar(false);
[email protected]e83f1682008-09-07 23:57:402148 break;
2149
[email protected]432115822011-07-10 15:52:272150 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
[email protected]ce696a452011-05-31 17:47:362151 if (window()->GetLocationBar())
2152 window()->GetLocationBar()->UpdatePageActions();
[email protected]57f71b92009-09-11 19:31:382153
[email protected]b3f7fe22011-11-11 19:27:562154 // Close any tabs from the unloaded extension, unless it's terminated,
2155 // in which case let the sad tabs remain.
[email protected]1c321ee52012-05-21 03:02:342156 if (content::Details<extensions::UnloadedExtensionInfo>(
2157 details)->reason != extension_misc::UNLOAD_REASON_TERMINATE) {
[email protected]b3f7fe22011-11-11 19:27:562158 const Extension* extension =
[email protected]1c321ee52012-05-21 03:02:342159 content::Details<extensions::UnloadedExtensionInfo>(
2160 details)->extension;
[email protected]2fc15ae2012-05-06 00:01:372161 for (int i = tab_strip_model_->count() - 1; i >= 0; --i) {
[email protected]52877dbc62012-06-29 22:22:032162 WebContents* tc = chrome::GetTabContentsAt(this, i)->web_contents();
[email protected]b3f7fe22011-11-11 19:27:562163 bool close_tab_contents =
2164 tc->GetURL().SchemeIs(chrome::kExtensionScheme) &&
2165 tc->GetURL().host() == extension->id();
2166 // We want to close all panels originated by the unloaded extension.
2167 close_tab_contents = close_tab_contents ||
2168 (type_ == TYPE_PANEL &&
2169 (web_app::GetExtensionIdFromApplicationName(app_name_) ==
[email protected]f9e82d92011-10-29 00:50:452170 extension->id()));
[email protected]52877dbc62012-06-29 22:22:032171 if (close_tab_contents)
2172 chrome::CloseWebContents(this, tc);
[email protected]894bb502009-05-21 22:39:572173 }
2174 }
2175 break;
2176 }
2177
[email protected]432115822011-07-10 15:52:272178 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532179 Profile* profile = content::Source<Profile>(source).ptr();
[email protected]fe8944f2011-08-30 17:47:322180 if (profile_->IsSameProfile(profile) && window()->GetLocationBar())
[email protected]ce696a452011-05-31 17:47:362181 window()->GetLocationBar()->InvalidatePageActions();
[email protected]371ed7a2009-08-25 15:22:462182 break;
2183 }
2184
[email protected]432115822011-07-10 15:52:272185 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED:
2186 case chrome::NOTIFICATION_EXTENSION_LOADED:
[email protected]1797f4a2011-08-30 18:00:282187 // During window creation on Windows we may end up calling into
2188 // SHAppBarMessage, which internally spawns a nested message loop. This
2189 // makes it possible for us to end up here before window creation has
2190 // completed,at which point window_ is NULL. See 94752 for details.
2191 if (window() && window()->GetLocationBar())
[email protected]ce696a452011-05-31 17:47:362192 window()->GetLocationBar()->UpdatePageActions();
[email protected]0dfe05c2011-02-23 23:03:362193 break;
[email protected]ad0c2e1b2010-01-30 00:00:102194
[email protected]6b40bb582012-03-15 20:50:382195#if defined(ENABLE_THEMES)
[email protected]432115822011-07-10 15:52:272196 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED:
[email protected]e001d412009-06-26 20:53:252197 window()->UserChangedTheme();
2198 break;
[email protected]6b40bb582012-03-15 20:50:382199#endif
[email protected]e001d412009-06-26 20:53:252200
[email protected]432115822011-07-10 15:52:272201 case chrome::NOTIFICATION_PREF_CHANGED: {
[email protected]6c2381d2011-10-19 02:52:532202 const std::string& pref_name =
2203 *content::Details<std::string>(details).ptr();
[email protected]5d98294912012-06-27 22:57:402204 if (pref_name == prefs::kInstantEnabled) {
[email protected]31359362012-06-11 23:27:102205 if (browser_shutdown::ShuttingDownWithoutClosingBrowsers() ||
2206 !InstantController::IsEnabled(profile())) {
[email protected]73de26a2010-10-17 03:23:252207 if (instant()) {
2208 instant()->DestroyPreviewContents();
[email protected]5aab5e22010-12-08 22:13:292209 instant_.reset();
2210 instant_unload_handler_.reset();
[email protected]73de26a2010-10-17 03:23:252211 }
2212 } else {
2213 CreateInstantIfNecessary();
2214 }
[email protected]543432ee2010-10-22 16:32:132215 } else if (pref_name == prefs::kDevToolsDisabled) {
[email protected]97b46382011-05-02 13:23:042216 if (profile_->GetPrefs()->GetBoolean(prefs::kDevToolsDisabled))
[email protected]0e12d7d2011-12-01 16:21:442217 content::DevToolsManager::GetInstance()->CloseAllClientHosts();
[email protected]3710d0002011-10-11 00:35:002218 } else if (pref_name == prefs::kShowBookmarkBar) {
[email protected]3710d0002011-10-11 00:35:002219 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_PREF_CHANGE);
[email protected]6c6b02d2011-09-02 03:36:472220 } else if (pref_name == prefs::kHomePage) {
[email protected]6c2381d2011-10-19 02:52:532221 PrefService* pref_service = content::Source<PrefService>(source).ptr();
[email protected]6c6b02d2011-09-02 03:36:472222 MarkHomePageAsChanged(pref_service);
[email protected]2a7e7c152010-10-01 20:12:092223 } else {
[email protected]b9c505b2010-04-30 15:05:102224 NOTREACHED();
[email protected]2a7e7c152010-10-01 20:12:092225 }
[email protected]b9c505b2010-04-30 15:05:102226 break;
2227 }
2228
[email protected]ea049a02011-12-25 21:37:092229 case chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED: {
2230 WebContents* web_contents = content::Source<WebContents>(source).ptr();
[email protected]52877dbc62012-06-29 22:22:032231 if (web_contents == chrome::GetActiveWebContents(this)) {
[email protected]9a9a9e22011-05-13 22:20:352232 LocationBar* location_bar = window()->GetLocationBar();
2233 if (location_bar)
2234 location_bar->UpdateContentSettingsIcons();
2235 }
2236 break;
2237 }
2238
[email protected]432115822011-07-10 15:52:272239 case content::NOTIFICATION_INTERSTITIAL_ATTACHED:
[email protected]09b29342011-06-24 19:18:482240 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]e397a4472011-12-21 21:47:502241 break;
2242
2243 case content::NOTIFICATION_INTERSTITIAL_DETACHED:
2244 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]09b29342011-06-24 19:18:482245 break;
2246
[email protected]e83f1682008-09-07 23:57:402247 default:
2248 NOTREACHED() << "Got a notification we didn't register for.";
initial.commit09911bf2008-07-26 23:55:292249 }
2250}
2251
[email protected]d3b98c82010-07-14 07:45:592252///////////////////////////////////////////////////////////////////////////////
[email protected]37c917b2012-06-28 15:53:592253// Browser, InstantControllerDelegate implementation:
[email protected]03bb953d2010-09-14 21:38:302254
[email protected]3d61dd02012-06-07 00:05:532255void Browser::ShowInstant(TabContents* preview_contents) {
[email protected]e3690ed2011-03-25 20:25:142256 window_->ShowInstant(preview_contents);
[email protected]42f027fb2011-10-27 16:37:482257
[email protected]3e324142012-06-25 18:26:332258 // TODO(beng): investigate if we can avoid this and instead rely on the
2259 // visibility of the WebContentsView
[email protected]52877dbc62012-06-29 22:22:032260 chrome::GetActiveWebContents(this)->WasHidden();
[email protected]3e324142012-06-25 18:26:332261 preview_contents->web_contents()->WasRestored();
[email protected]03bb953d2010-09-14 21:38:302262}
2263
[email protected]6b723f82010-10-05 20:14:272264void Browser::HideInstant() {
[email protected]3e481282011-10-15 15:39:502265 window_->HideInstant();
[email protected]52877dbc62012-06-29 22:22:032266 if (chrome::GetActiveWebContents(this))
2267 chrome::GetActiveWebContents(this)->WasRestored();
[email protected]42f027fb2011-10-27 16:37:482268 if (instant_->GetPreviewContents())
[email protected]3e324142012-06-25 18:26:332269 instant_->GetPreviewContents()->web_contents()->WasHidden();
[email protected]03bb953d2010-09-14 21:38:302270}
2271
[email protected]3d61dd02012-06-07 00:05:532272void Browser::CommitInstant(TabContents* preview_contents) {
[email protected]52877dbc62012-06-29 22:22:032273 TabContents* tab_contents = chrome::GetActiveTabContents(this);
[email protected]2fc15ae2012-05-06 00:01:372274 int index = tab_strip_model_->GetIndexOfTabContents(tab_contents);
[email protected]3c9e1872010-11-18 16:17:492275 DCHECK_NE(TabStripModel::kNoTab, index);
[email protected]03bb953d2010-09-14 21:38:302276 // TabStripModel takes ownership of preview_contents.
[email protected]2fc15ae2012-05-06 00:01:372277 tab_strip_model_->ReplaceTabContentsAt(index, preview_contents);
[email protected]43032342011-03-21 14:10:312278 // InstantUnloadHandler takes ownership of tab_contents.
2279 instant_unload_handler_->RunUnloadListenersOrDestroy(tab_contents, index);
[email protected]46fd1ea42011-02-16 15:59:332280
[email protected]d9083482012-01-06 00:38:462281 GURL url = preview_contents->web_contents()->GetURL();
[email protected]6e37fe32011-03-02 09:06:522282 DCHECK(profile_->GetExtensionService());
2283 if (profile_->GetExtensionService()->IsInstalledApp(url)) {
[email protected]a9acdff2012-03-08 04:04:522284 AppLauncherHandler::RecordAppLaunchType(
2285 extension_misc::APP_LAUNCH_OMNIBOX_INSTANT);
[email protected]46fd1ea42011-02-16 15:59:332286 }
[email protected]03bb953d2010-09-14 21:38:302287}
2288
[email protected]33b8b8e2011-03-15 14:51:552289void Browser::SetSuggestedText(const string16& text,
2290 InstantCompleteBehavior behavior) {
[email protected]ce696a452011-05-31 17:47:362291 if (window()->GetLocationBar())
2292 window()->GetLocationBar()->SetSuggestedText(text, behavior);
[email protected]03bb953d2010-09-14 21:38:302293}
2294
[email protected]6b723f82010-10-05 20:14:272295gfx::Rect Browser::GetInstantBounds() {
2296 return window()->GetInstantBounds();
[email protected]46fe8e92010-09-22 03:32:472297}
2298
[email protected]09c69432012-03-16 16:23:282299void Browser::InstantPreviewFocused() {
2300 // NOTE: This is only invoked on aura.
2301 window_->WebContentsFocused(instant_->GetPreviewContents()->web_contents());
2302}
2303
[email protected]3d61dd02012-06-07 00:05:532304TabContents* Browser::GetInstantHostTabContents() const {
[email protected]52877dbc62012-06-29 22:22:032305 return chrome::GetActiveTabContents(this);
[email protected]05cf2fa2012-05-29 20:36:062306}
2307
[email protected]9800de5e2011-03-11 18:19:492308///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:572309// Browser, Command and state updating (private):
2310
[email protected]6c6b02d2011-09-02 03:36:472311void Browser::MarkHomePageAsChanged(PrefService* pref_service) {
2312 pref_service->SetBoolean(prefs::kHomePageChanged, true);
[email protected]6c6b02d2011-09-02 03:36:472313}
2314
[email protected]36b6dcb2008-11-12 01:19:572315///////////////////////////////////////////////////////////////////////////////
2316// Browser, UI update coalescing and handling (private):
2317
2318void Browser::UpdateToolbar(bool should_restore_state) {
[email protected]52877dbc62012-06-29 22:22:032319 window_->UpdateToolbar(chrome::GetActiveTabContents(this),
2320 should_restore_state);
initial.commit09911bf2008-07-26 23:55:292321}
2322
[email protected]e3a5d5b2012-06-22 17:56:462323void Browser::UpdateSearchState(TabContents* contents) {
2324 if (chrome::search::IsInstantExtendedAPIEnabled(profile_))
2325 search_delegate_->OnTabActivated(contents);
2326}
2327
[email protected]2a6bc3e2011-12-28 23:51:332328void Browser::ScheduleUIUpdate(const WebContents* source,
[email protected]36b6dcb2008-11-12 01:19:572329 unsigned changed_flags) {
[email protected]a5231662009-09-26 00:56:002330 if (!source)
2331 return;
2332
[email protected]2b4355c2009-04-04 17:55:462333 // Do some synchronous updates.
[email protected]d9083482012-01-06 00:38:462334 if (changed_flags & content::INVALIDATE_TYPE_URL &&
[email protected]52877dbc62012-06-29 22:22:032335 source == chrome::GetActiveWebContents(this)) {
[email protected]36b6dcb2008-11-12 01:19:572336 // Only update the URL for the current tab. Note that we do not update
2337 // the navigation commands since those would have already been updated
2338 // synchronously by NavigationStateChanged.
2339 UpdateToolbar(false);
[email protected]d9083482012-01-06 00:38:462340 changed_flags &= ~content::INVALIDATE_TYPE_URL;
[email protected]36b6dcb2008-11-12 01:19:572341 }
[email protected]d9083482012-01-06 00:38:462342 if (changed_flags & content::INVALIDATE_TYPE_LOAD) {
[email protected]6ebdc9b2010-09-27 16:55:572343 // Update the loading state synchronously. This is so the throbber will
2344 // immediately start/stop, which gives a more snappy feel. We want to do
[email protected]a5231662009-09-26 00:56:002345 // this for any tab so they start & stop quickly.
[email protected]2fc15ae2012-05-06 00:01:372346 tab_strip_model_->UpdateTabContentsStateAt(
[email protected]52877dbc62012-06-29 22:22:032347 chrome::GetIndexOfTab(this, source),
[email protected]6ebdc9b2010-09-27 16:55:572348 TabStripModelObserver::LOADING_ONLY);
[email protected]d9083482012-01-06 00:38:462349 // The status bubble needs to be updated during INVALIDATE_TYPE_LOAD too,
2350 // but we do that asynchronously by not stripping INVALIDATE_TYPE_LOAD from
[email protected]6ebdc9b2010-09-27 16:55:572351 // changed_flags.
[email protected]8030f012009-09-25 18:09:372352 }
2353
[email protected]d9083482012-01-06 00:38:462354 if (changed_flags & content::INVALIDATE_TYPE_TITLE && !source->IsLoading()) {
[email protected]f1cd5e82009-10-23 17:19:032355 // To correctly calculate whether the title changed while not loading
2356 // we need to process the update synchronously. This state only matters for
2357 // the TabStripModel, so we notify the TabStripModel now and notify others
2358 // asynchronously.
[email protected]2fc15ae2012-05-06 00:01:372359 tab_strip_model_->UpdateTabContentsStateAt(
[email protected]52877dbc62012-06-29 22:22:032360 chrome::GetIndexOfTab(this, source),
[email protected]f1cd5e82009-10-23 17:19:032361 TabStripModelObserver::TITLE_NOT_LOADING);
2362 }
2363
[email protected]2b4355c2009-04-04 17:55:462364 // If the only updates were synchronously handled above, we're done.
[email protected]8030f012009-09-25 18:09:372365 if (changed_flags == 0)
[email protected]2b4355c2009-04-04 17:55:462366 return;
[email protected]36b6dcb2008-11-12 01:19:572367
2368 // Save the dirty bits.
[email protected]85354982009-09-25 19:58:402369 scheduled_updates_[source] |= changed_flags;
[email protected]36b6dcb2008-11-12 01:19:572370
[email protected]24f1477532011-11-22 22:55:362371 if (!chrome_updater_factory_.HasWeakPtrs()) {
[email protected]36b6dcb2008-11-12 01:19:572372 // No task currently scheduled, start another.
[email protected]0586b0e2010-02-12 21:38:372373 MessageLoop::current()->PostDelayedTask(
2374 FROM_HERE,
[email protected]24f1477532011-11-22 22:55:362375 base::Bind(&Browser::ProcessPendingUIUpdates,
2376 chrome_updater_factory_.GetWeakPtr()),
[email protected]11d6ab1a2012-01-12 21:37:262377 base::TimeDelta::FromMilliseconds(kUIUpdateCoalescingTimeMS));
[email protected]36b6dcb2008-11-12 01:19:572378 }
2379}
2380
2381void Browser::ProcessPendingUIUpdates() {
2382#ifndef NDEBUG
2383 // Validate that all tabs we have pending updates for exist. This is scary
2384 // because the pending list must be kept in sync with any detached or
[email protected]a5231662009-09-26 00:56:002385 // deleted tabs.
[email protected]85354982009-09-25 19:58:402386 for (UpdateMap::const_iterator i = scheduled_updates_.begin();
2387 i != scheduled_updates_.end(); ++i) {
[email protected]36b6dcb2008-11-12 01:19:572388 bool found = false;
2389 for (int tab = 0; tab < tab_count(); tab++) {
[email protected]52877dbc62012-06-29 22:22:032390 if (chrome::GetWebContentsAt(this, tab) == i->first) {
[email protected]36b6dcb2008-11-12 01:19:572391 found = true;
2392 break;
2393 }
2394 }
2395 DCHECK(found);
2396 }
2397#endif
2398
[email protected]24f1477532011-11-22 22:55:362399 chrome_updater_factory_.InvalidateWeakPtrs();
[email protected]36b6dcb2008-11-12 01:19:572400
[email protected]85354982009-09-25 19:58:402401 for (UpdateMap::const_iterator i = scheduled_updates_.begin();
2402 i != scheduled_updates_.end(); ++i) {
[email protected]36b6dcb2008-11-12 01:19:572403 // Do not dereference |contents|, it may be out-of-date!
[email protected]2a6bc3e2011-12-28 23:51:332404 const WebContents* contents = i->first;
[email protected]85354982009-09-25 19:58:402405 unsigned flags = i->second;
[email protected]36b6dcb2008-11-12 01:19:572406
[email protected]52877dbc62012-06-29 22:22:032407 if (contents == chrome::GetActiveWebContents(this)) {
[email protected]a5231662009-09-26 00:56:002408 // Updates that only matter when the tab is selected go here.
[email protected]f7f3a5f2009-05-01 22:02:342409
[email protected]d9083482012-01-06 00:38:462410 if (flags & content::INVALIDATE_TYPE_PAGE_ACTIONS) {
[email protected]9800de5e2011-03-11 18:19:492411 LocationBar* location_bar = window()->GetLocationBar();
2412 if (location_bar)
2413 location_bar->UpdatePageActions();
2414 }
[email protected]6ebdc9b2010-09-27 16:55:572415 // Updating the URL happens synchronously in ScheduleUIUpdate.
[email protected]d9083482012-01-06 00:38:462416 if (flags & content::INVALIDATE_TYPE_LOAD && GetStatusBubble()) {
[email protected]52877dbc62012-06-29 22:22:032417 GetStatusBubble()->SetStatus(chrome::GetActiveTabContents(this)->
2418 core_tab_helper()->GetStatusText());
[email protected]0dd3a0ab2011-02-18 08:17:442419 }
[email protected]a5231662009-09-26 00:56:002420
[email protected]d9083482012-01-06 00:38:462421 if (flags & (content::INVALIDATE_TYPE_TAB |
2422 content::INVALIDATE_TYPE_TITLE)) {
[email protected]a5231662009-09-26 00:56:002423 window_->UpdateTitleBar();
2424 }
[email protected]4d34e2e2009-05-26 22:55:282425 }
[email protected]36b6dcb2008-11-12 01:19:572426
[email protected]a5231662009-09-26 00:56:002427 // Updates that don't depend upon the selected state go here.
[email protected]d9083482012-01-06 00:38:462428 if (flags &
2429 (content::INVALIDATE_TYPE_TAB | content::INVALIDATE_TYPE_TITLE)) {
[email protected]2fc15ae2012-05-06 00:01:372430 tab_strip_model_->UpdateTabContentsStateAt(
[email protected]e52d0a42012-06-08 22:44:162431 tab_strip_model_->GetIndexOfWebContents(contents),
[email protected]f1cd5e82009-10-23 17:19:032432 TabStripModelObserver::ALL);
[email protected]36b6dcb2008-11-12 01:19:572433 }
2434
2435 // We don't need to process INVALIDATE_STATE, since that's not visible.
2436 }
2437
2438 scheduled_updates_.clear();
2439}
2440
[email protected]d9083482012-01-06 00:38:462441void Browser::RemoveScheduledUpdatesFor(WebContents* contents) {
[email protected]36b6dcb2008-11-12 01:19:572442 if (!contents)
2443 return;
2444
[email protected]85354982009-09-25 19:58:402445 UpdateMap::iterator i = scheduled_updates_.find(contents);
2446 if (i != scheduled_updates_.end())
2447 scheduled_updates_.erase(i);
initial.commit09911bf2008-07-26 23:55:292448}
2449
[email protected]d938aed92009-01-22 19:49:332450
[email protected]36b6dcb2008-11-12 01:19:572451///////////////////////////////////////////////////////////////////////////////
2452// Browser, Getters for UI (private):
initial.commit09911bf2008-07-26 23:55:292453
[email protected]36b6dcb2008-11-12 01:19:572454StatusBubble* Browser::GetStatusBubble() {
[email protected]34930432009-11-09 00:12:092455 // In kiosk mode, we want to always hide the status bubble.
2456 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kKioskMode))
2457 return NULL;
[email protected]9b032bf2009-07-21 17:34:232458 return window_ ? window_->GetStatusBubble() : NULL;
initial.commit09911bf2008-07-26 23:55:292459}
2460
[email protected]36b6dcb2008-11-12 01:19:572461///////////////////////////////////////////////////////////////////////////////
2462// Browser, Session restore functions (private):
initial.commit09911bf2008-07-26 23:55:292463
2464void Browser::SyncHistoryWithTabs(int index) {
[email protected]92371eb2011-04-28 11:50:152465 SessionService* session_service =
2466 SessionServiceFactory::GetForProfileIfExisting(profile());
initial.commit09911bf2008-07-26 23:55:292467 if (session_service) {
2468 for (int i = index; i < tab_count(); ++i) {
[email protected]52877dbc62012-06-29 22:22:032469 TabContents* tab = chrome::GetTabContentsAt(this, i);
[email protected]81898992011-06-14 22:15:002470 if (tab) {
initial.commit09911bf2008-07-26 23:55:292471 session_service->SetTabIndexInWindow(
[email protected]81898992011-06-14 22:15:002472 session_id(), tab->restore_tab_helper()->session_id(), i);
[email protected]ecc523f62010-09-28 22:45:402473 session_service->SetPinnedState(
2474 session_id(),
[email protected]81898992011-06-14 22:15:002475 tab->restore_tab_helper()->session_id(),
[email protected]52877dbc62012-06-29 22:22:032476 tab_strip_model_->IsTabPinned(i));
initial.commit09911bf2008-07-26 23:55:292477 }
2478 }
2479 }
2480}
2481
[email protected]36b6dcb2008-11-12 01:19:572482///////////////////////////////////////////////////////////////////////////////
2483// Browser, OnBeforeUnload handling (private):
initial.commit09911bf2008-07-26 23:55:292484
[email protected]04b4a6c2008-08-02 00:44:472485void Browser::ProcessPendingTabs() {
[email protected]08f0d612011-01-13 23:58:212486 if (!is_attempting_to_close_browser_) {
2487 // Because we might invoke this after a delay it's possible for the value of
2488 // is_attempting_to_close_browser_ to have changed since we scheduled the
2489 // task.
2490 return;
2491 }
initial.commit09911bf2008-07-26 23:55:292492
[email protected]04b4a6c2008-08-02 00:44:472493 if (HasCompletedUnloadProcessing()) {
2494 // We've finished all the unload events and can proceed to close the
2495 // browser.
2496 OnWindowClosing();
initial.commit09911bf2008-07-26 23:55:292497 return;
2498 }
2499
[email protected]04b4a6c2008-08-02 00:44:472500 // Process beforeunload tabs first. When that queue is empty, process
2501 // unload tabs.
[email protected]04b4a6c2008-08-02 00:44:472502 if (!tabs_needing_before_unload_fired_.empty()) {
[email protected]1d3e9172012-06-12 02:59:132503 WebContents* web_contents = *(tabs_needing_before_unload_fired_.begin());
[email protected]22484622009-06-10 21:17:242504 // Null check render_view_host here as this gets called on a PostTask and
2505 // the tab's render_view_host may have been nulled out.
[email protected]1d3e9172012-06-12 02:59:132506 if (web_contents->GetRenderViewHost()) {
2507 web_contents->GetRenderViewHost()->FirePageBeforeUnload(false);
[email protected]22484622009-06-10 21:17:242508 } else {
[email protected]1d3e9172012-06-12 02:59:132509 ClearUnloadState(web_contents, true);
[email protected]22484622009-06-10 21:17:242510 }
[email protected]04b4a6c2008-08-02 00:44:472511 } else if (!tabs_needing_unload_fired_.empty()) {
initial.commit09911bf2008-07-26 23:55:292512 // We've finished firing all beforeunload events and can proceed with unload
2513 // events.
2514 // TODO(ojan): We should add a call to browser_shutdown::OnShutdownStarting
2515 // somewhere around here so that we have accurate measurements of shutdown
2516 // time.
[email protected]04b4a6c2008-08-02 00:44:472517 // TODO(ojan): We can probably fire all the unload events in parallel and
2518 // get a perf benefit from that in the cases where the tab hangs in it's
2519 // unload handler or takes a long time to page in.
[email protected]1d3e9172012-06-12 02:59:132520 WebContents* web_contents = *(tabs_needing_unload_fired_.begin());
[email protected]22484622009-06-10 21:17:242521 // Null check render_view_host here as this gets called on a PostTask and
2522 // the tab's render_view_host may have been nulled out.
[email protected]1d3e9172012-06-12 02:59:132523 if (web_contents->GetRenderViewHost()) {
2524 web_contents->GetRenderViewHost()->ClosePage();
[email protected]22484622009-06-10 21:17:242525 } else {
[email protected]1d3e9172012-06-12 02:59:132526 ClearUnloadState(web_contents, true);
[email protected]22484622009-06-10 21:17:242527 }
initial.commit09911bf2008-07-26 23:55:292528 } else {
[email protected]04b4a6c2008-08-02 00:44:472529 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:292530 }
2531}
2532
[email protected]d043c2cc2009-03-25 18:30:452533bool Browser::HasCompletedUnloadProcessing() const {
[email protected]04b4a6c2008-08-02 00:44:472534 return is_attempting_to_close_browser_ &&
2535 tabs_needing_before_unload_fired_.empty() &&
2536 tabs_needing_unload_fired_.empty();
2537}
2538
2539void Browser::CancelWindowClose() {
[email protected]22bf2c12012-06-23 08:35:552540 // Closing of window can be canceled from a beforeunload handler.
[email protected]04b4a6c2008-08-02 00:44:472541 DCHECK(is_attempting_to_close_browser_);
[email protected]8f673f3a2008-08-05 22:34:282542 tabs_needing_before_unload_fired_.clear();
2543 tabs_needing_unload_fired_.clear();
[email protected]04b4a6c2008-08-02 00:44:472544 is_attempting_to_close_browser_ = false;
[email protected]0b4d3382010-07-14 16:13:042545
[email protected]9cc821652012-04-18 18:18:102546 content::NotificationService::current()->Notify(
2547 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED,
2548 content::Source<Browser>(this),
2549 content::NotificationService::NoDetails());
[email protected]04b4a6c2008-08-02 00:44:472550}
2551
[email protected]1d3e9172012-06-12 02:59:132552bool Browser::RemoveFromSet(UnloadListenerSet* set, WebContents* web_contents) {
[email protected]04b4a6c2008-08-02 00:44:472553 DCHECK(is_attempting_to_close_browser_);
2554
[email protected]1d3e9172012-06-12 02:59:132555 UnloadListenerSet::iterator iter =
2556 std::find(set->begin(), set->end(), web_contents);
[email protected]06b42f032008-12-03 18:43:052557 if (iter != set->end()) {
2558 set->erase(iter);
2559 return true;
[email protected]04b4a6c2008-08-02 00:44:472560 }
2561 return false;
2562}
initial.commit09911bf2008-07-26 23:55:292563
[email protected]1d3e9172012-06-12 02:59:132564void Browser::ClearUnloadState(WebContents* web_contents, bool process_now) {
[email protected]0b4d3382010-07-14 16:13:042565 if (is_attempting_to_close_browser_) {
[email protected]1d3e9172012-06-12 02:59:132566 RemoveFromSet(&tabs_needing_before_unload_fired_, web_contents);
2567 RemoveFromSet(&tabs_needing_unload_fired_, web_contents);
[email protected]08f0d612011-01-13 23:58:212568 if (process_now) {
2569 ProcessPendingTabs();
2570 } else {
2571 MessageLoop::current()->PostTask(
2572 FROM_HERE,
[email protected]24f1477532011-11-22 22:55:362573 base::Bind(&Browser::ProcessPendingTabs, weak_factory_.GetWeakPtr()));
[email protected]08f0d612011-01-13 23:58:212574 }
[email protected]0b4d3382010-07-14 16:13:042575 }
initial.commit09911bf2008-07-26 23:55:292576}
2577
[email protected]36b6dcb2008-11-12 01:19:572578///////////////////////////////////////////////////////////////////////////////
[email protected]69444cc2009-04-09 20:40:062579// Browser, In-progress download termination handling (private):
2580
[email protected]b6a4ac2b2011-10-17 20:05:482581bool Browser::CanCloseWithInProgressDownloads() {
[email protected]5a17d492011-10-24 19:37:032582 // If we've prompted, we need to hear from the user before we
2583 // can close.
2584 if (cancel_download_confirmation_state_ != NOT_PROMPTED)
2585 return cancel_download_confirmation_state_ != WAITING_FOR_RESPONSE;
2586
2587 int num_downloads_blocking;
2588 if (DOWNLOAD_CLOSE_OK ==
2589 OkToCloseWithInProgressDownloads(&num_downloads_blocking))
[email protected]446295a2010-04-19 23:43:002590 return true;
2591
[email protected]5a17d492011-10-24 19:37:032592 // Closing this window will kill some downloads; prompt to make sure
2593 // that's ok.
[email protected]69444cc2009-04-09 20:40:062594 cancel_download_confirmation_state_ = WAITING_FOR_RESPONSE;
2595 window_->ConfirmBrowserCloseWithPendingDownloads();
2596
2597 // Return false so the browser does not close. We'll close if the user
2598 // confirms in the dialog.
2599 return false;
2600}
2601
2602///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:572603// Browser, Assorted utility functions (private):
initial.commit09911bf2008-07-26 23:55:292604
[email protected]3d61dd02012-06-07 00:05:532605void Browser::SetAsDelegate(TabContents* tab, Browser* delegate) {
[email protected]075969d2012-04-17 20:05:552606 // WebContents...
[email protected]d9083482012-01-06 00:38:462607 tab->web_contents()->SetDelegate(delegate);
[email protected]e7cfdbd2011-04-22 14:41:372608
2609 // ...and all the helpers.
2610 tab->blocked_content_tab_helper()->set_delegate(delegate);
[email protected]35699d572011-05-11 19:46:142611 tab->bookmark_tab_helper()->set_delegate(delegate);
[email protected]6f80e932012-06-04 19:00:072612 tab->zoom_controller()->set_observer(delegate);
[email protected]31145092011-09-30 01:49:442613 tab->constrained_window_tab_helper()->set_delegate(delegate);
[email protected]51d83bdf2011-12-09 16:09:292614 tab->core_tab_helper()->set_delegate(delegate);
[email protected]1739e57d2011-11-30 21:18:252615 tab->extension_tab_helper()->set_delegate(delegate);
[email protected]9219d262011-02-15 21:05:122616 tab->search_engine_tab_helper()->set_delegate(delegate);
2617}
2618
[email protected]36b6dcb2008-11-12 01:19:572619void Browser::CloseFrame() {
2620 window_->Close();
initial.commit09911bf2008-07-26 23:55:292621}
2622
[email protected]3d61dd02012-06-07 00:05:532623void Browser::TabDetachedAtImpl(TabContents* contents, int index,
[email protected]c37e3b62010-01-22 00:15:172624 DetachType type) {
2625 if (type == DETACH_TYPE_DETACH) {
[email protected]56580852010-11-17 16:09:152626 // Save the current location bar state, but only if the tab being detached
2627 // is the selected tab. Because saving state can conditionally revert the
2628 // location bar, saving the current tab's location bar state to a
2629 // non-selected tab can corrupt both tabs.
[email protected]52877dbc62012-06-29 22:22:032630 if (contents == chrome::GetActiveTabContents(this)) {
[email protected]9800de5e2011-03-11 18:19:492631 LocationBar* location_bar = window()->GetLocationBar();
2632 if (location_bar)
[email protected]83a2610a2012-01-05 01:00:272633 location_bar->SaveStateToContents(contents->web_contents());
[email protected]9800de5e2011-03-11 18:19:492634 }
[email protected]c37e3b62010-01-22 00:15:172635
[email protected]2fc15ae2012-05-06 00:01:372636 if (!tab_strip_model_->closing_all())
[email protected]c37e3b62010-01-22 00:15:172637 SyncHistoryWithTabs(0);
2638 }
2639
[email protected]9219d262011-02-15 21:05:122640 SetAsDelegate(contents, NULL);
[email protected]d9083482012-01-06 00:38:462641 RemoveScheduledUpdatesFor(contents->web_contents());
[email protected]c37e3b62010-01-22 00:15:172642
[email protected]41f022e2012-01-31 05:56:402643 if (find_bar_controller_.get() && index == active_index()) {
[email protected]c37e3b62010-01-22 00:15:172644 find_bar_controller_->ChangeTabContents(NULL);
[email protected]ecc523f62010-09-28 22:45:402645 }
[email protected]c37e3b62010-01-22 00:15:172646
[email protected]08f0d612011-01-13 23:58:212647 if (is_attempting_to_close_browser_) {
2648 // If this is the last tab with unload handlers, then ProcessPendingTabs
2649 // would call back into the TabStripModel (which is invoking this method on
2650 // us). Avoid that by passing in false so that the call to
2651 // ProcessPendingTabs is delayed.
[email protected]d9083482012-01-06 00:38:462652 ClearUnloadState(contents->web_contents(), false);
[email protected]08f0d612011-01-13 23:58:212653 }
2654
[email protected]e3a5d5b2012-06-22 17:56:462655 // Stop observing search model changes for this tab.
2656 search_delegate_->OnTabDetached(contents);
2657
[email protected]432115822011-07-10 15:52:272658 registrar_.Remove(this, content::NOTIFICATION_INTERSTITIAL_ATTACHED,
[email protected]fbc5e5f92012-01-02 06:08:322659 content::Source<WebContents>(contents->web_contents()));
[email protected]e397a4472011-12-21 21:47:502660 registrar_.Remove(this, content::NOTIFICATION_INTERSTITIAL_DETACHED,
[email protected]4b19ea52012-01-02 20:15:252661 content::Source<WebContents>(contents->web_contents()));
[email protected]fbc5e5f92012-01-02 06:08:322662 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
2663 content::Source<WebContents>(contents->web_contents()));
[email protected]c37e3b62010-01-22 00:15:172664}
2665
[email protected]92655362012-04-20 21:33:172666bool Browser::SupportsWindowFeatureImpl(WindowFeature feature,
2667 bool check_fullscreen) const {
2668 // On Mac, fullscreen mode has most normal things (in a slide-down panel). On
2669 // other platforms, we hide some controls when in fullscreen mode.
2670 bool hide_ui_for_fullscreen = false;
2671#if !defined(OS_MACOSX)
2672 hide_ui_for_fullscreen = check_fullscreen && window_ &&
2673 window_->IsFullscreen();
2674#endif
2675
2676 unsigned int features = FEATURE_INFOBAR | FEATURE_DOWNLOADSHELF;
2677
2678 if (is_type_tabbed())
2679 features |= FEATURE_BOOKMARKBAR;
2680
2681 if (!hide_ui_for_fullscreen) {
2682 if (!is_type_tabbed())
2683 features |= FEATURE_TITLEBAR;
2684
2685 if (is_type_tabbed())
2686 features |= FEATURE_TABSTRIP;
2687
2688 if (is_type_tabbed())
2689 features |= FEATURE_TOOLBAR;
2690
2691 if (!is_app())
2692 features |= FEATURE_LOCATIONBAR;
2693 }
2694 return !!(features & feature);
2695}
2696
[email protected]2031cf9d2010-10-12 05:05:372697void Browser::CreateInstantIfNecessary() {
[email protected]b35b26b32011-05-05 20:35:142698 if (is_type_tabbed() && InstantController::IsEnabled(profile()) &&
[email protected]2031cf9d2010-10-12 05:05:372699 !profile()->IsOffTheRecord()) {
[email protected]05cf2fa2012-05-29 20:36:062700 instant_.reset(new InstantController(this, InstantController::INSTANT));
[email protected]5aab5e22010-12-08 22:13:292701 instant_unload_handler_.reset(new InstantUnloadHandler(this));
[email protected]2031cf9d2010-10-12 05:05:372702 }
2703}
[email protected]77d8d622010-12-15 10:30:122704
[email protected]09b29342011-06-24 19:18:482705void Browser::UpdateBookmarkBarState(BookmarkBarStateChangeReason reason) {
2706 BookmarkBar::State state;
[email protected]227393e2011-06-29 21:28:392707 // The bookmark bar is hidden in fullscreen mode, unless on the new tab page.
[email protected]3710d0002011-10-11 00:35:002708 if (browser_defaults::bookmarks_enabled &&
2709 profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar) &&
[email protected]227393e2011-06-29 21:28:392710 (!window_ || !window_->IsFullscreen())) {
[email protected]09b29342011-06-24 19:18:482711 state = BookmarkBar::SHOW;
2712 } else {
[email protected]52877dbc62012-06-29 22:22:032713 TabContents* tab = chrome::GetActiveTabContents(this);
[email protected]09b29342011-06-24 19:18:482714 if (tab && tab->bookmark_tab_helper()->ShouldShowBookmarkBar())
2715 state = BookmarkBar::DETACHED;
2716 else
2717 state = BookmarkBar::HIDDEN;
2718 }
[email protected]2d4e43a02012-06-26 00:11:552719
2720 // Only allow the bookmark bar to be shown in default mode.
2721 if (!search_model_->mode().is_default())
2722 state = BookmarkBar::HIDDEN;
2723
[email protected]09b29342011-06-24 19:18:482724 if (state == bookmark_bar_state_)
2725 return;
2726
2727 bookmark_bar_state_ = state;
2728
2729 if (!window_)
2730 return; // This is called from the constructor when window_ is NULL.
2731
2732 if (reason == BOOKMARK_BAR_STATE_CHANGE_TAB_SWITCH) {
2733 // Don't notify BrowserWindow on a tab switch as at the time this is invoked
2734 // BrowserWindow hasn't yet switched tabs. The BrowserWindow implementations
2735 // end up querying state once they process the tab switch.
2736 return;
2737 }
2738
2739 BookmarkBar::AnimateChangeType animate_type =
2740 (reason == BOOKMARK_BAR_STATE_CHANGE_PREF_CHANGE) ?
2741 BookmarkBar::ANIMATE_STATE_CHANGE :
2742 BookmarkBar::DONT_ANIMATE_STATE_CHANGE;
2743 window_->BookmarkBarStateChanged(animate_type);
2744}
[email protected]2e2cacc2011-07-12 21:54:262745
[email protected]92655362012-04-20 21:33:172746bool Browser::MaybeCreateBackgroundContents(int route_id,
2747 WebContents* opener_web_contents,
2748 const string16& frame_name,
2749 const GURL& target_url) {
2750 GURL opener_url = opener_web_contents->GetURL();
2751 ExtensionService* extensions_service = profile_->GetExtensionService();
2752
2753 if (!opener_url.is_valid() ||
2754 frame_name.empty() ||
2755 !extensions_service ||
2756 !extensions_service->is_ready())
2757 return false;
2758
2759 // Only hosted apps have web extents, so this ensures that only hosted apps
2760 // can create BackgroundContents. We don't have to check for background
2761 // permission as that is checked in RenderMessageFilter when the CreateWindow
2762 // message is processed.
2763 const Extension* extension =
2764 extensions_service->extensions()->GetHostedAppByURL(
2765 ExtensionURLInfo(opener_url));
2766 if (!extension)
2767 return false;
2768
2769 // No BackgroundContents allowed if BackgroundContentsService doesn't exist.
2770 BackgroundContentsService* service =
2771 BackgroundContentsServiceFactory::GetForProfile(profile_);
2772 if (!service)
2773 return false;
2774
2775 // Ensure that we're trying to open this from the extension's process.
2776 SiteInstance* opener_site_instance = opener_web_contents->GetSiteInstance();
2777 extensions::ProcessMap* process_map = extensions_service->process_map();
2778 if (!opener_site_instance->GetProcess() ||
2779 !process_map->Contains(
2780 extension->id(), opener_site_instance->GetProcess()->GetID())) {
2781 return false;
2782 }
2783
2784 // Only allow a single background contents per app.
2785 bool allow_js_access = extension->allow_background_js_access();
2786 BackgroundContents* existing =
2787 service->GetAppBackgroundContents(ASCIIToUTF16(extension->id()));
2788 if (existing) {
2789 // For non-scriptable background contents, ignore the request altogether,
2790 // (returning true, so that a regular WebContents isn't created either).
2791 if (!allow_js_access)
2792 return true;
2793 // For scriptable background pages, if one already exists, close it (even
2794 // if it was specified in the manifest).
2795 DLOG(INFO) << "Closing existing BackgroundContents for " << opener_url;
2796 delete existing;
2797 }
2798
2799 // If script access is not allowed, create the the background contents in a
2800 // new SiteInstance, so that a separate process is used.
2801 scoped_refptr<content::SiteInstance> site_instance =
2802 allow_js_access ?
2803 opener_site_instance :
2804 content::SiteInstance::Create(opener_web_contents->GetBrowserContext());
2805
2806 // Passed all the checks, so this should be created as a BackgroundContents.
2807 BackgroundContents* contents = service->CreateBackgroundContents(
2808 site_instance,
2809 route_id,
2810 profile_,
2811 frame_name,
2812 ASCIIToUTF16(extension->id()));
2813
2814 // When a separate process is used, the original renderer cannot access the
2815 // new window later, thus we need to navigate the window now.
2816 if (contents && !allow_js_access) {
2817 contents->web_contents()->GetController().LoadURL(
2818 target_url,
2819 content::Referrer(),
2820 content::PAGE_TRANSITION_LINK,
2821 std::string()); // No extra headers.
2822 }
2823
2824 return contents != NULL;
[email protected]afcb43562011-09-02 23:33:382825}