blob: 511b99e33601cd3bb2699ebf6ff16578fc18c192 [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]b5febf72012-07-29 18:10:0922#include "base/process_info.h"
[email protected]0af8f132011-07-16 01:37:0223#include "base/string_number_conversions.h"
[email protected]b6a4ac2b2011-10-17 20:05:4824#include "base/string_util.h"
[email protected]5a17d492011-10-24 19:37:0325#include "base/stringprintf.h"
[email protected]34b99632011-01-01 01:01:0626#include "base/threading/thread.h"
27#include "base/threading/thread_restrictions.h"
[email protected]7f070d42011-03-09 20:25:3228#include "base/time.h"
[email protected]6524385ef2010-08-18 06:34:1329#include "base/utf_string_conversions.h"
[email protected]1a3aba82010-11-08 23:52:5430#include "chrome/app/chrome_command_ids.h"
[email protected]77a91c72012-08-13 16:19:3431#include "chrome/browser/api/infobars/simple_alert_infobar_delegate.h"
[email protected]ea9edcb02011-09-23 22:05:0432#include "chrome/browser/autofill/personal_data_manager_factory.h"
[email protected]a07676b22011-06-17 16:36:5333#include "chrome/browser/background/background_contents_service.h"
[email protected]4fdf6742012-01-10 20:14:3634#include "chrome/browser/background/background_contents_service_factory.h"
[email protected]a9afddb2009-02-12 17:49:4235#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]b3ac5c82009-10-08 20:56:5436#include "chrome/browser/bookmarks/bookmark_utils.h"
[email protected]5c238752009-06-13 10:29:0737#include "chrome/browser/browser_process.h"
[email protected]c61db1d2009-02-06 03:39:1838#include "chrome/browser/browser_shutdown.h"
[email protected]40d59ce52009-03-06 23:20:1439#include "chrome/browser/character_encoding.h"
[email protected]0f083402011-11-22 02:59:0140#include "chrome/browser/chrome_page_zoom.h"
[email protected]55c87fa2011-10-15 07:28:2841#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]7ae9c322012-07-17 23:46:4442#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]7d189022011-08-25 22:54:2043#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
44#include "chrome/browser/custom_handlers/register_protocol_handler_infobar_delegate.h"
[email protected]dce502762011-07-20 08:53:4945#include "chrome/browser/debugger/devtools_toggle_action.h"
46#include "chrome/browser/debugger/devtools_window.h"
[email protected]7fb4bbb2012-05-27 18:06:2247#include "chrome/browser/download/download_crx_util.h"
[email protected]59560e0b2009-06-04 03:30:2248#include "chrome/browser/download/download_item_model.h"
[email protected]9bb54ee2011-10-12 17:43:3549#include "chrome/browser/download/download_service.h"
50#include "chrome/browser/download/download_service_factory.h"
[email protected]a1bc3b82012-04-19 19:32:2051#include "chrome/browser/download/download_shelf.h"
[email protected]59560e0b2009-06-04 03:30:2252#include "chrome/browser/download/download_started_animation.h"
[email protected]a53209b2012-01-20 16:48:1653#include "chrome/browser/download/download_util.h"
[email protected]9c1662b2012-03-06 15:44:3354#include "chrome/browser/extensions/browser_extension_window_controller.h"
[email protected]49098f702011-10-13 03:47:1855#include "chrome/browser/extensions/default_apps_trial.h"
[email protected]10abd192010-09-30 02:03:4956#include "chrome/browser/extensions/extension_prefs.h"
[email protected]eaa7dd182010-12-14 11:09:0057#include "chrome/browser/extensions/extension_service.h"
[email protected]a6394ae2012-07-16 20:58:4358#include "chrome/browser/extensions/tab_helper.h"
[email protected]b375c5d2011-05-03 21:15:0459#include "chrome/browser/favicon/favicon_tab_helper.h"
[email protected]3a29a6e2011-08-24 18:26:2160#include "chrome/browser/file_select_helper.h"
[email protected]820735792010-07-29 23:40:0161#include "chrome/browser/first_run/first_run.h"
[email protected]f7578f52010-08-30 22:22:4962#include "chrome/browser/google/google_url_tracker.h"
[email protected]7e204122011-09-01 18:56:2163#include "chrome/browser/infobars/infobar_tab_helper.h"
[email protected]65c81142012-07-31 19:44:4364#include "chrome/browser/intents/device_attached_intent_source.h"
[email protected]7d189022011-08-25 22:54:2065#include "chrome/browser/intents/register_intent_handler_infobar_delegate.h"
[email protected]59d73ec2012-09-01 02:24:2766#include "chrome/browser/intents/web_intents_reporting.h"
[email protected]8f0bbd62012-02-22 03:37:1867#include "chrome/browser/intents/web_intents_util.h"
[email protected]2e6389f2012-05-18 19:41:2568#include "chrome/browser/lifetime/application_lifetime.h"
[email protected]4afde5c2012-06-25 17:00:5369#include "chrome/browser/media/media_stream_devices_controller.h"
[email protected]a239c3f2009-02-17 22:13:1970#include "chrome/browser/net/url_fixer_upper.h"
[email protected]2c4fb7b2011-04-02 06:33:2971#include "chrome/browser/notifications/notification_ui_manager.h"
[email protected]14a000d2010-04-29 21:44:2472#include "chrome/browser/platform_util.h"
[email protected]4e94ab32011-08-05 05:28:2773#include "chrome/browser/prefs/incognito_mode_prefs.h"
[email protected]37858e52010-08-26 00:22:0274#include "chrome/browser/prefs/pref_service.h"
[email protected]02337592010-09-27 18:38:2575#include "chrome/browser/printing/cloud_print/cloud_print_setup_flow.h"
[email protected]0996e9b2011-08-26 17:59:0176#include "chrome/browser/printing/print_preview_tab_controller.h"
[email protected]8ecad5e2010-12-02 21:18:3377#include "chrome/browser/profiles/profile.h"
[email protected]5e919242012-02-13 23:59:3578#include "chrome/browser/profiles/profile_destroyer.h"
[email protected]5a17d492011-10-24 19:37:0379#include "chrome/browser/profiles/profile_manager.h"
[email protected]a7be6982011-12-12 21:53:5780#include "chrome/browser/profiles/profile_metrics.h"
[email protected]67baffc82011-12-19 18:03:0781#include "chrome/browser/repost_form_warning_controller.h"
[email protected]85e921fb82009-02-11 23:19:4482#include "chrome/browser/sessions/session_service.h"
[email protected]92371eb2011-04-28 11:50:1583#include "chrome/browser/sessions/session_service_factory.h"
[email protected]bde40b822012-08-30 17:28:5684#include "chrome/browser/sessions/session_tab_helper.h"
[email protected]c61db1d2009-02-06 03:39:1885#include "chrome/browser/sessions/session_types.h"
[email protected]92926d92010-09-02 18:35:0686#include "chrome/browser/sessions/tab_restore_service.h"
[email protected]92371eb2011-04-28 11:50:1587#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]066629f2009-10-17 00:28:1388#include "chrome/browser/sync/profile_sync_service.h"
[email protected]5e61afb92012-01-27 20:09:1389#include "chrome/browser/sync/profile_sync_service_factory.h"
[email protected]40f047972009-11-25 03:54:4090#include "chrome/browser/sync/sync_ui_util.h"
[email protected]2c4fb7b2011-04-02 06:33:2991#include "chrome/browser/tab_contents/background_contents.h"
[email protected]4fdf6742012-01-10 20:14:3692#include "chrome/browser/tab_contents/retargeting_details.h"
[email protected]1ab4ddf2011-07-21 04:48:0493#include "chrome/browser/tab_contents/tab_util.h"
[email protected]ffa6f592011-06-24 22:03:5794#include "chrome/browser/themes/theme_service.h"
95#include "chrome/browser/themes/theme_service_factory.h"
[email protected]82194b5fb2012-05-22 23:53:0796#include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_creator.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]7acfaf92012-07-11 15:51:59103#include "chrome/browser/ui/browser_instant_controller.h"
[email protected]6768ac02011-04-06 17:41:04104#include "chrome/browser/ui/browser_list.h"
[email protected]fcca741b2011-06-17 22:46:37105#include "chrome/browser/ui/browser_navigator.h"
[email protected]fb7da4132011-03-15 15:51:00106#include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
[email protected]855370052012-07-10 19:30:32107#include "chrome/browser/ui/browser_tab_strip_model_delegate.h"
[email protected]52877dbc62012-06-29 22:22:03108#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]ef557022012-03-16 10:05:33109#include "chrome/browser/ui/browser_toolbar_model_delegate.h"
[email protected]2cd4fde2012-06-26 03:10:26110#include "chrome/browser/ui/browser_ui_prefs.h"
[email protected]00070c732011-04-09 15:31:33111#include "chrome/browser/ui/browser_window.h"
[email protected]5d9cace72012-06-21 16:07:12112#include "chrome/browser/ui/chrome_pages.h"
[email protected]6e1fcd12012-07-02 17:14:20113#include "chrome/browser/ui/chrome_select_file_policy.h"
[email protected]a44657202012-01-09 05:48:31114#include "chrome/browser/ui/extensions/shell_window.h"
[email protected]45300ad42010-12-02 15:51:14115#include "chrome/browser/ui/find_bar/find_bar.h"
116#include "chrome/browser/ui/find_bar/find_bar_controller.h"
[email protected]c90c6ca2011-02-16 20:11:38117#include "chrome/browser/ui/find_bar/find_tab_helper.h"
[email protected]9d5c059a82012-06-29 20:13:07118#include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
[email protected]1ba55cf2012-06-29 19:11:39119#include "chrome/browser/ui/global_error/global_error.h"
120#include "chrome/browser/ui/global_error/global_error_service.h"
121#include "chrome/browser/ui/global_error/global_error_service_factory.h"
[email protected]943b8612011-08-31 21:07:13122#include "chrome/browser/ui/intents/web_intent_picker_controller.h"
[email protected]f13b4202012-06-12 23:53:23123#include "chrome/browser/ui/media_stream_infobar_delegate.h"
[email protected]6a3ec2312010-12-02 19:30:19124#include "chrome/browser/ui/omnibox/location_bar.h"
[email protected]e3a5d5b2012-06-22 17:56:46125#include "chrome/browser/ui/search/search.h"
126#include "chrome/browser/ui/search/search_delegate.h"
127#include "chrome/browser/ui/search/search_model.h"
[email protected]c3166302012-09-25 21:27:08128#include "chrome/browser/ui/search/search_types.h"
[email protected]5d9cace72012-06-21 16:07:12129#include "chrome/browser/ui/singleton_tabs.h"
[email protected]6a3ec2312010-12-02 19:30:19130#include "chrome/browser/ui/status_bubble.h"
[email protected]3da95a102011-11-30 21:47:45131#include "chrome/browser/ui/sync/browser_synced_window_delegate.h"
[email protected]51d83bdf2011-12-09 16:09:29132#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
[email protected]3d61dd02012-06-07 00:05:53133#include "chrome/browser/ui/tab_contents/tab_contents.h"
[email protected]51296c62012-09-26 13:02:44134#include "chrome/browser/ui/tab_modal_confirm_dialog.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]f9b6c092012-07-09 19:32:28138#include "chrome/browser/ui/unload_controller.h"
[email protected]f847e6082011-03-24 00:08:26139#include "chrome/browser/ui/web_applications/web_app_ui.h"
[email protected]65549182012-02-25 00:45:40140#include "chrome/browser/ui/webui/signin/login_ui_service.h"
141#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
[email protected]03639422012-06-28 19:03:37142#include "chrome/browser/ui/window_sizer/window_sizer.h"
[email protected]b1b73942010-05-26 20:11:54143#include "chrome/browser/upgrade_detector.h"
[email protected]86b54012009-11-19 09:18:50144#include "chrome/browser/web_applications/web_app.h"
[email protected]d938aed92009-01-22 19:49:33145#include "chrome/common/chrome_constants.h"
[email protected]432115822011-07-10 15:52:27146#include "chrome/common/chrome_notification_types.h"
[email protected]d938aed92009-01-22 19:49:33147#include "chrome/common/chrome_switches.h"
[email protected]7d189022011-08-25 22:54:20148#include "chrome/common/custom_handlers/protocol_handler.h"
[email protected]5b1a0e22009-05-26 19:00:58149#include "chrome/common/extensions/extension.h"
[email protected]46fd1ea42011-02-16 15:59:33150#include "chrome/common/extensions/extension_constants.h"
[email protected]8a53ee042009-01-21 16:41:33151#include "chrome/common/pref_names.h"
[email protected]7664ab32011-02-01 23:35:25152#include "chrome/common/profiling.h"
[email protected]b5febf72012-07-29 18:10:09153#include "chrome/common/startup_metric_utils.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]2a2813352012-07-11 22:20:23181#include "grit/theme_resources.h"
[email protected]8a53ee042009-01-21 16:41:33182#include "net/base/net_util.h"
[email protected]be28b5f42012-07-20 11:31:25183#include "net/base/registry_controlled_domains/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]53f04c82012-07-26 02:31:09187#include "ui/base/dialogs/selected_file_info.h"
[email protected]c051a1b2011-01-21 23:30:17188#include "ui/base/l10n/l10n_util.h"
[email protected]08397d52011-02-05 01:53:38189#include "ui/gfx/point.h"
[email protected]ce9751942011-09-21 01:57:24190#include "webkit/glue/web_intent_data.h"
[email protected]3a3b75a2012-06-01 08:38:36191#include "webkit/glue/web_intent_service_data.h"
[email protected]4a19be92011-09-22 14:25:02192#include "webkit/glue/webkit_glue.h"
[email protected]d938aed92009-01-22 19:49:33193#include "webkit/glue/window_open_disposition.h"
[email protected]3a5180ae2011-12-21 02:39:38194#include "webkit/plugins/webplugininfo.h"
[email protected]8a53ee042009-01-21 16:41:33195
196#if defined(OS_WIN)
[email protected]77cbe50e2012-06-14 02:44:38197#include "base/win/metro.h"
[email protected]36d5e5592010-11-15 20:45:59198#include "chrome/browser/autofill/autofill_ie_toolbar_import_win.h"
[email protected]12f520c2010-01-06 18:11:15199#include "chrome/browser/shell_integration.h"
[email protected]b17d41c2011-02-17 22:09:59200#include "chrome/browser/ssl/ssl_error_info.h"
[email protected]a5d1e1e2010-09-23 19:34:12201#include "chrome/browser/task_manager/task_manager.h"
[email protected]6a3ec2312010-12-02 19:30:19202#include "chrome/browser/ui/view_ids.h"
[email protected]dcd57762011-06-25 12:18:51203#include "ui/base/win/shell.h"
[email protected]8a53ee042009-01-21 16:41:33204#endif // OS_WIN
[email protected]e1acf6f2008-10-27 20:43:33205
[email protected]b7969202010-05-14 21:29:26206#if defined(OS_CHROMEOS)
[email protected]361c2012012-09-05 16:31:13207#include "chrome/browser/chromeos/gdata/drive_file_system_util.h"
[email protected]eab8c0f2011-11-18 22:33:50208#endif
209
[email protected]dc04be7c2012-03-15 23:57:49210#if defined(USE_ASH)
[email protected]bafda132012-02-16 19:32:51211#include "ash/ash_switches.h"
[email protected]c2f0a892012-01-24 22:19:21212#endif
213
[email protected]8a53ee042009-01-21 16:41:33214using base::TimeDelta;
[email protected]b87ee522012-05-18 15:16:54215using content::NativeWebKeyboardEvent;
[email protected]c5eed492012-01-04 17:07:50216using content::NavigationController;
[email protected]10f417c52011-12-28 21:04:23217using content::NavigationEntry;
[email protected]e5d549d2011-12-28 01:29:20218using content::OpenURLParams;
[email protected]3a5180ae2011-12-21 02:39:38219using content::PluginService;
[email protected]e5d549d2011-12-28 01:29:20220using content::Referrer;
[email protected]b6583592012-01-25 19:52:33221using content::SiteInstance;
[email protected]7f6f44c2011-12-14 13:23:38222using content::UserMetricsAction;
[email protected]ea049a02011-12-25 21:37:09223using content::WebContents;
[email protected]1c321ee52012-05-21 03:02:34224using extensions::Extension;
[email protected]20c07f8e2012-05-31 08:43:14225using ui::WebDialogDelegate;
initial.commit09911bf2008-07-26 23:55:29226
[email protected]36b6dcb2008-11-12 01:19:57227///////////////////////////////////////////////////////////////////////////////
initial.commit09911bf2008-07-26 23:55:29228
[email protected]505323e22009-01-24 02:47:58229namespace {
230
[email protected]91a79232010-11-10 18:15:08231// The URL to be loaded to display the "Report a broken page" form.
232const char kBrokenPageUrl[] =
[email protected]95fc6f92011-04-13 00:37:20233 "https://www.google.com/support/chrome/bin/request.py?contact_type="
[email protected]91a79232010-11-10 18:15:08234 "broken_website&format=inproduct&p.page_title=$1&p.page_url=$2";
235
[email protected]91a79232010-11-10 18:15:08236// How long we wait before updating the browser chrome while loading a page.
237const int kUIUpdateCoalescingTimeMS = 200;
238
[email protected]a4fe67012012-07-25 20:14:29239BrowserWindow* CreateBrowserWindow(Browser* browser) {
[email protected]a4fe67012012-07-25 20:14:29240 return BrowserWindow::CreateBrowserWindow(browser);
241}
242
[email protected]56073dc2012-08-10 16:59:06243#if defined(OS_CHROMEOS)
244chrome::HostDesktopType kDefaultHostDesktopType = chrome::HOST_DESKTOP_TYPE_ASH;
245#else
246chrome::HostDesktopType kDefaultHostDesktopType =
247 chrome::HOST_DESKTOP_TYPE_NATIVE;
248#endif
249
250
[email protected]505323e22009-01-24 02:47:58251} // namespace
252
[email protected]b35b26b32011-05-05 20:35:14253////////////////////////////////////////////////////////////////////////////////
254// Browser, CreateParams:
255
[email protected]da22aa62012-04-04 18:54:35256Browser::CreateParams::CreateParams()
257 : type(TYPE_TABBED),
258 profile(NULL),
[email protected]56073dc2012-08-10 16:59:06259 host_desktop_type(kDefaultHostDesktopType),
[email protected]a4fe67012012-07-25 20:14:29260 app_type(APP_TYPE_HOST),
[email protected]da22aa62012-04-04 18:54:35261 initial_show_state(ui::SHOW_STATE_DEFAULT),
[email protected]a4fe67012012-07-25 20:14:29262 is_session_restore(false),
263 window(NULL) {
264}
265
266Browser::CreateParams::CreateParams(Profile* profile)
[email protected]56073dc2012-08-10 16:59:06267 : type(TYPE_TABBED),
268 profile(profile),
269 host_desktop_type(kDefaultHostDesktopType),
270 app_type(APP_TYPE_HOST),
271 initial_show_state(ui::SHOW_STATE_DEFAULT),
272 is_session_restore(false),
273 window(NULL) {
[email protected]da22aa62012-04-04 18:54:35274}
275
[email protected]b35b26b32011-05-05 20:35:14276Browser::CreateParams::CreateParams(Type type, Profile* profile)
277 : type(type),
[email protected]a636d8e52012-02-28 15:40:41278 profile(profile),
[email protected]56073dc2012-08-10 16:59:06279 host_desktop_type(kDefaultHostDesktopType),
280 app_type(APP_TYPE_HOST),
281 initial_show_state(ui::SHOW_STATE_DEFAULT),
282 is_session_restore(false),
283 window(NULL) {
284}
285
286Browser::CreateParams::CreateParams(Type type,
287 Profile* profile,
288 chrome::HostDesktopType host_desktop_type)
289 : type(type),
290 profile(profile),
291 host_desktop_type(host_desktop_type),
[email protected]0da58292012-03-22 20:37:21292 app_type(APP_TYPE_HOST),
[email protected]a636d8e52012-02-28 15:40:41293 initial_show_state(ui::SHOW_STATE_DEFAULT),
[email protected]a4fe67012012-07-25 20:14:29294 is_session_restore(false),
295 window(NULL) {
[email protected]b35b26b32011-05-05 20:35:14296}
297
[email protected]da22aa62012-04-04 18:54:35298// static
299Browser::CreateParams Browser::CreateParams::CreateForApp(
300 Type type,
301 const std::string& app_name,
302 const gfx::Rect& window_bounds,
303 Profile* profile) {
304 DCHECK(type != TYPE_TABBED);
305 DCHECK(!app_name.empty());
306
[email protected]da22aa62012-04-04 18:54:35307 CreateParams params(type, profile);
308 params.app_name = app_name;
309 params.app_type = APP_TYPE_CHILD;
310 params.initial_bounds = window_bounds;
311
312 return params;
313}
314
315// static
316Browser::CreateParams Browser::CreateParams::CreateForDevTools(
317 Profile* profile) {
318 CreateParams params(TYPE_POPUP, profile);
319 params.app_name = DevToolsWindow::kDevToolsApp;
320 return params;
321}
322
[email protected]36b6dcb2008-11-12 01:19:57323///////////////////////////////////////////////////////////////////////////////
324// Browser, Constructors, Creation, Showing:
initial.commit09911bf2008-07-26 23:55:29325
[email protected]a4fe67012012-07-25 20:14:29326Browser::Browser(const CreateParams& params)
327 : type_(params.type),
328 profile_(params.profile),
[email protected]f3e99e32008-07-30 04:48:39329 window_(NULL),
[email protected]ecc523f62010-09-28 22:45:40330 ALLOW_THIS_IN_INITIALIZER_LIST(
[email protected]855370052012-07-10 19:30:32331 tab_strip_model_delegate_(
332 new chrome::BrowserTabStripModelDelegate(this))),
333 ALLOW_THIS_IN_INITIALIZER_LIST(
334 tab_strip_model_(new TabStripModel(tab_strip_model_delegate_.get(),
[email protected]a4fe67012012-07-25 20:14:29335 params.profile))),
336 app_name_(params.app_name),
337 app_type_(params.app_type),
[email protected]15952e462008-11-14 00:29:05338 chrome_updater_factory_(this),
[email protected]69444cc2009-04-09 20:40:06339 cancel_download_confirmation_state_(NOT_PROMPTED),
[email protected]a4fe67012012-07-25 20:14:29340 override_bounds_(params.initial_bounds),
341 initial_show_state_(params.initial_show_state),
342 is_session_restore_(params.is_session_restore),
[email protected]56073dc2012-08-10 16:59:06343 host_desktop_type_(params.host_desktop_type),
[email protected]f9b6c092012-07-09 19:32:28344 ALLOW_THIS_IN_INITIALIZER_LIST(
345 unload_controller_(new chrome::UnloadController(this))),
[email protected]24f1477532011-11-22 22:55:36346 weak_factory_(this),
[email protected]fb7da4132011-03-15 15:51:00347 ALLOW_THIS_IN_INITIALIZER_LIST(
[email protected]ef557022012-03-16 10:05:33348 content_setting_bubble_model_delegate_(
349 new BrowserContentSettingBubbleModelDelegate(this))),
350 ALLOW_THIS_IN_INITIALIZER_LIST(
351 toolbar_model_delegate_(
352 new BrowserToolbarModelDelegate(this))),
353 ALLOW_THIS_IN_INITIALIZER_LIST(
[email protected]fb7da4132011-03-15 15:51:00354 tab_restore_service_delegate_(
[email protected]09b29342011-06-24 19:18:48355 new BrowserTabRestoreServiceDelegate(this))),
[email protected]afefa74e2011-07-26 05:04:23356 ALLOW_THIS_IN_INITIALIZER_LIST(
357 synced_window_delegate_(
358 new BrowserSyncedWindowDelegate(this))),
[email protected]8a5e0ca2011-08-25 06:30:47359 bookmark_bar_state_(BookmarkBar::HIDDEN),
[email protected]5d98294912012-06-27 22:57:40360 ALLOW_THIS_IN_INITIALIZER_LIST(
361 command_controller_(new chrome::BrowserCommandController(this))),
[email protected]2f516c792011-09-19 22:22:09362 window_has_shown_(false) {
[email protected]a4fe67012012-07-25 20:14:29363 if (!app_name_.empty())
364 chrome::RegisterAppPrefs(app_name_, profile_);
[email protected]2fc15ae2012-05-06 00:01:37365 tab_strip_model_->AddObserver(this);
366
[email protected]ef557022012-03-16 10:05:33367 toolbar_model_.reset(new ToolbarModel(toolbar_model_delegate_.get()));
[email protected]e3a5d5b2012-06-22 17:56:46368 search_model_.reset(new chrome::search::SearchModel(NULL));
369 search_delegate_.reset(
[email protected]51a53e8f62012-08-28 03:08:56370 new chrome::search::SearchDelegate(search_model_.get(),
371 toolbar_model_.get()));
[email protected]ef557022012-03-16 10:05:33372
[email protected]432115822011-07-10 15:52:27373 registrar_.Add(this, content::NOTIFICATION_SSL_VISIBLE_STATE_CHANGED,
[email protected]ad50def52011-10-19 23:17:07374 content::NotificationService::AllSources());
[email protected]432115822011-07-10 15:52:27375 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
[email protected]a93089b12011-11-22 20:47:38376 content::Source<Profile>(profile_->GetOriginalProfile()));
[email protected]432115822011-07-10 15:52:27377 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]a93089b12011-11-22 20:47:38378 content::Source<Profile>(profile_->GetOriginalProfile()));
[email protected]432115822011-07-10 15:52:27379 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]a93089b12011-11-22 20:47:38380 content::Source<Profile>(profile_->GetOriginalProfile()));
[email protected]432115822011-07-10 15:52:27381 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07382 content::NotificationService::AllSources());
[email protected]6b40bb582012-03-15 20:50:38383#if defined(ENABLE_THEMES)
[email protected]ffa6f592011-06-24 22:03:57384 registrar_.Add(
[email protected]432115822011-07-10 15:52:27385 this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
[email protected]6c2381d2011-10-19 02:52:53386 content::Source<ThemeService>(
387 ThemeServiceFactory::GetForProfile(profile_)));
[email protected]6b40bb582012-03-15 20:50:38388#endif
[email protected]ea049a02011-12-25 21:37:09389 registrar_.Add(this, chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
[email protected]ad50def52011-10-19 23:17:07390 content::NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29391
[email protected]97b46382011-05-02 13:23:04392 profile_pref_registrar_.Init(profile_->GetPrefs());
393 profile_pref_registrar_.Add(prefs::kDevToolsDisabled, this);
[email protected]3710d0002011-10-11 00:35:00394 profile_pref_registrar_.Add(prefs::kShowBookmarkBar, this);
[email protected]6c6b02d2011-09-02 03:36:47395 profile_pref_registrar_.Add(prefs::kHomePage, this);
[email protected]9e823662010-10-13 23:36:00396
initial.commit09911bf2008-07-26 23:55:29397 BrowserList::AddBrowser(this);
398
[email protected]97b46382011-05-02 13:23:04399 // NOTE: These prefs all need to be explicitly destroyed in the destructor
400 // or you'll get a nasty surprise when you run the incognito tests.
initial.commit09911bf2008-07-26 23:55:29401 encoding_auto_detect_.Init(prefs::kWebKitUsesUniversalDetector,
402 profile_->GetPrefs(), NULL);
[email protected]281918912010-05-27 22:05:13403
[email protected]7acfaf92012-07-11 15:51:59404 instant_controller_.reset(new chrome::BrowserInstantController(this));
[email protected]df25eb9f2012-09-13 17:43:23405
406#if 0
407 // Disabled for M22. See http://crbug.com/144326.
[email protected]65c81142012-07-31 19:44:43408 device_attached_intent_source_.reset(
409 new DeviceAttachedIntentSource(this, (this)));
[email protected]df25eb9f2012-09-13 17:43:23410#endif
[email protected]07d490bc2011-03-07 17:05:26411
[email protected]09b29342011-06-24 19:18:48412 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_INIT);
[email protected]a7be6982011-12-12 21:53:57413
[email protected]a4fe67012012-07-25 20:14:29414 FilePath profile_path = profile_->GetPath();
[email protected]a7be6982011-12-12 21:53:57415 ProfileMetrics::LogProfileLaunch(profile_path);
[email protected]a4fe67012012-07-25 20:14:29416
417 window_ = params.window ? params.window : CreateBrowserWindow(this);
418
419 // TODO(beng): move to BrowserFrameWin.
420#if defined(OS_WIN) && !defined(USE_AURA)
421 // Set the app user model id for this application to that of the application
422 // name. See http://crbug.com/7028.
423 ui::win::SetAppIdForWindow(
424 is_app() && !is_type_panel() ?
425 ShellIntegration::GetAppModelIdForProfile(UTF8ToWide(app_name_),
426 profile_->GetPath()) :
427 ShellIntegration::GetChromiumModelIdForProfile(profile_->GetPath()),
428 window()->GetNativeWindow());
429
430 if (is_type_panel()) {
431 ui::win::SetAppIconForWindow(ShellIntegration::GetChromiumIconPath(),
432 window()->GetNativeWindow());
433 }
434#endif
435
436 // Create the extension window controller before sending notifications.
437 extension_window_controller_.reset(
438 new BrowserExtensionWindowController(this));
439
440 // TODO(beng): Move BrowserList::AddBrowser() to the end of this function and
441 // replace uses of this with BL's notifications.
442 content::NotificationService::current()->Notify(
443 chrome::NOTIFICATION_BROWSER_WINDOW_READY,
444 content::Source<Browser>(this),
445 content::NotificationService::NoDetails());
446
447 // TODO(beng): move to ChromeBrowserMain:
448 PrefService* local_state = g_browser_process->local_state();
449 if (local_state && local_state->FindPreference(
450 prefs::kAutofillPersonalDataManagerFirstRun) &&
451 local_state->GetBoolean(prefs::kAutofillPersonalDataManagerFirstRun)) {
452 // Notify PDM that this is a first run.
453#if defined(OS_WIN)
454 ImportAutofillDataWin(PersonalDataManagerFactory::GetForProfile(profile_));
455#endif // defined(OS_WIN)
456 // Reset the preference so we don't call it again for subsequent windows.
457 local_state->ClearPref(prefs::kAutofillPersonalDataManagerFirstRun);
458 }
459
[email protected]6fb528e2012-08-01 20:32:33460 fullscreen_controller_.reset(new FullscreenController(this));
[email protected]c3166302012-09-25 21:27:08461 search_model_->AddObserver(this);
initial.commit09911bf2008-07-26 23:55:29462}
463
464Browser::~Browser() {
[email protected]2fc15ae2012-05-06 00:01:37465 // The tab strip should not have any tabs at this point.
466 if (!browser_shutdown::ShuttingDownWithoutClosingBrowsers())
467 DCHECK(tab_strip_model_->empty());
[email protected]c3166302012-09-25 21:27:08468
469 search_model_->RemoveObserver(this);
[email protected]2fc15ae2012-05-06 00:01:37470 tab_strip_model_->RemoveObserver(this);
471
initial.commit09911bf2008-07-26 23:55:29472 BrowserList::RemoveBrowser(this);
473
[email protected]bdc6ccd12012-03-20 22:06:13474 SessionService* session_service =
475 SessionServiceFactory::GetForProfile(profile_);
476 if (session_service)
477 session_service->WindowClosed(session_id_);
478
479 TabRestoreService* tab_restore_service =
480 TabRestoreServiceFactory::GetForProfile(profile());
481 if (tab_restore_service)
482 tab_restore_service->BrowserClosed(tab_restore_service_delegate());
483
[email protected]7be64502011-05-03 17:51:47484#if !defined(OS_MACOSX)
[email protected]d8748142012-05-16 21:13:43485 if (!browser::GetBrowserCount(profile_)) {
initial.commit09911bf2008-07-26 23:55:29486 // We're the last browser window with this profile. We need to nuke the
487 // TabRestoreService, which will start the shutdown of the
488 // NavigationControllers and allow for proper shutdown. If we don't do this
489 // chrome won't shutdown cleanly, and may end up crashing when some
490 // thread tries to use the IO thread (or another thread) that is no longer
491 // valid.
[email protected]fbc947b2009-06-19 13:28:24492 // This isn't a valid assumption for Mac OS, as it stays running after
493 // the last browser has closed. The Mac equivalent is in its app
494 // controller.
[email protected]92371eb2011-04-28 11:50:15495 TabRestoreServiceFactory::ResetForProfile(profile_);
initial.commit09911bf2008-07-26 23:55:29496 }
[email protected]fbc947b2009-06-19 13:28:24497#endif
initial.commit09911bf2008-07-26 23:55:29498
[email protected]97b46382011-05-02 13:23:04499 profile_pref_registrar_.RemoveAll();
[email protected]97b46382011-05-02 13:23:04500
[email protected]9a8c4022011-01-25 14:25:33501 encoding_auto_detect_.Destroy();
[email protected]9a8c4022011-01-25 14:25:33502
[email protected]5d98294912012-06-27 22:57:40503 command_controller_.reset();
504
initial.commit09911bf2008-07-26 23:55:29505 if (profile_->IsOffTheRecord() &&
[email protected]474e5db2011-08-12 13:02:23506 !BrowserList::IsOffTheRecordSessionActiveForProfile(profile_)) {
[email protected]5e919242012-02-13 23:59:35507 // An incognito profile is no longer needed, this indirectly frees
508 // its cache and cookies once it gets destroyed at the appropriate time.
[email protected]4dffabe2012-05-19 14:37:06509 ProfileDestroyer::DestroyProfileWhenAppropriate(profile_);
initial.commit09911bf2008-07-26 23:55:29510 }
511
512 // There may be pending file dialogs, we need to tell them that we've gone
513 // away so they don't try and call back to us.
514 if (select_file_dialog_.get())
515 select_file_dialog_->ListenerDestroyed();
516}
517
[email protected]36b6dcb2008-11-12 01:19:57518///////////////////////////////////////////////////////////////////////////////
[email protected]5c238752009-06-13 10:29:07519// Getters & Setters
520
[email protected]b77cb302009-10-29 04:09:17521FindBarController* Browser::GetFindBarController() {
522 if (!find_bar_controller_.get()) {
[email protected]632983f2011-08-08 22:51:24523 FindBar* find_bar = window_->CreateFindBar();
[email protected]b77cb302009-10-29 04:09:17524 find_bar_controller_.reset(new FindBarController(find_bar));
525 find_bar->SetFindBarController(find_bar_controller_.get());
[email protected]52877dbc62012-06-29 22:22:03526 find_bar_controller_->ChangeTabContents(chrome::GetActiveTabContents(this));
[email protected]b77cb302009-10-29 04:09:17527 find_bar_controller_->find_bar()->MoveWindowIfNecessary(gfx::Rect(), true);
528 }
529 return find_bar_controller_.get();
530}
531
[email protected]24db8a072009-10-29 20:35:37532bool Browser::HasFindBarController() const {
533 return find_bar_controller_.get() != NULL;
534}
535
[email protected]b35b26b32011-05-05 20:35:14536bool Browser::is_app() const {
537 return !app_name_.empty();
538}
539
540bool Browser::is_devtools() const {
541 return app_name_ == DevToolsWindow::kDevToolsApp;
542}
543
[email protected]5c238752009-06-13 10:29:07544///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:57545// Browser, State Storage and Retrieval for UI:
546
[email protected]9b125b732012-08-17 04:43:55547gfx::Image Browser::GetCurrentPageIcon() const {
[email protected]52877dbc62012-06-29 22:22:03548 TabContents* contents = chrome::GetActiveTabContents(this);
[email protected]ce5348a82008-12-18 18:36:23549 // |contents| can be NULL since GetCurrentPageIcon() is called by the window
550 // during the window's creation (before tabs have been added).
[email protected]61720892012-08-03 19:35:33551 return contents ?
[email protected]9b125b732012-08-17 04:43:55552 contents->favicon_tab_helper()->GetFavicon() : gfx::Image();
[email protected]36b6dcb2008-11-12 01:19:57553}
554
[email protected]731f8a42009-07-20 22:07:32555string16 Browser::GetWindowTitleForCurrentTab() const {
[email protected]52877dbc62012-06-29 22:22:03556 WebContents* contents = chrome::GetActiveWebContents(this);
[email protected]dda5dfa32009-07-10 00:33:29557 string16 title;
[email protected]c7c42332008-11-15 01:10:54558
[email protected]731f8a42009-07-20 22:07:32559 // |contents| can be NULL because GetWindowTitleForCurrentTab is called by the
560 // window during the window's creation (before tabs have been added).
[email protected]36b6dcb2008-11-12 01:19:57561 if (contents) {
[email protected]dda5dfa32009-07-10 00:33:29562 title = contents->GetTitle();
[email protected]36b6dcb2008-11-12 01:19:57563 FormatTitleForDisplay(&title);
564 }
565 if (title.empty())
[email protected]51d83bdf2011-12-09 16:09:29566 title = CoreTabHelper::GetDefaultTitle();
[email protected]36b6dcb2008-11-12 01:19:57567
[email protected]cae97652012-04-20 03:12:12568#if defined(OS_MACOSX) || defined(USE_ASH)
569 // On Mac or Ash, we don't want to suffix the page title with
[email protected]22a3daa2009-11-09 22:03:33570 // the application name.
[email protected]d904bc82009-07-07 00:41:04571 return title;
[email protected]7be64502011-05-03 17:51:47572#else
[email protected]edb5e6a2009-04-08 23:46:17573 int string_id = IDS_BROWSER_WINDOW_TITLE_FORMAT;
[email protected]dff529732010-04-01 23:50:55574 // Don't append the app name to window titles on app frames and app popups
[email protected]b35b26b32011-05-05 20:35:14575 if (is_app())
[email protected]edb5e6a2009-04-08 23:46:17576 string_id = IDS_BROWSER_WINDOW_TITLE_FORMAT_NO_LOGO;
[email protected]dda5dfa32009-07-10 00:33:29577 return l10n_util::GetStringFUTF16(string_id, title);
[email protected]57e68dba2009-05-15 21:30:50578#endif
[email protected]36b6dcb2008-11-12 01:19:57579}
580
581// static
[email protected]dda5dfa32009-07-10 00:33:29582void Browser::FormatTitleForDisplay(string16* title) {
[email protected]36b6dcb2008-11-12 01:19:57583 size_t current_index = 0;
584 size_t match_index;
[email protected]57ecc4b2010-08-11 03:02:51585 while ((match_index = title->find(L'\n', current_index)) != string16::npos) {
[email protected]810a52ef2010-01-08 01:22:15586 title->replace(match_index, 1, string16());
[email protected]36b6dcb2008-11-12 01:19:57587 current_index = match_index;
588 }
589}
590
591///////////////////////////////////////////////////////////////////////////////
592// Browser, OnBeforeUnload handling:
593
594bool Browser::ShouldCloseWindow() {
[email protected]69444cc2009-04-09 20:40:06595 if (!CanCloseWithInProgressDownloads())
596 return false;
597
[email protected]f9b6c092012-07-09 19:32:28598 return unload_controller_->ShouldCloseWindow();
599}
[email protected]69444cc2009-04-09 20:40:06600
[email protected]f9b6c092012-07-09 19:32:28601bool Browser::IsAttemptingToCloseBrowser() const {
602 return unload_controller_->is_attempting_to_close_browser();
[email protected]36b6dcb2008-11-12 01:19:57603}
604
605void Browser::OnWindowClosing() {
606 if (!ShouldCloseWindow())
607 return;
608
[email protected]c984d9f2010-07-20 20:52:20609 // Application should shutdown on last window close if the user is explicitly
610 // trying to quit, or if there is nothing keeping the browser alive (such as
611 // AppController on the Mac, or BackgroundContentsService for background
612 // pages).
613 bool should_quit_if_last_browser =
[email protected]2e6389f2012-05-18 19:41:25614 browser_shutdown::IsTryingToQuit() || !browser::WillKeepAlive();
[email protected]7dc8c6b2010-04-09 17:02:50615
[email protected]1e0c7402012-07-27 10:48:15616 if (should_quit_if_last_browser && BrowserList::size() == 1)
[email protected]36b6dcb2008-11-12 01:19:57617 browser_shutdown::OnShutdownStarting(browser_shutdown::WINDOW_CLOSE);
618
[email protected]92371eb2011-04-28 11:50:15619 // Don't use GetForProfileIfExisting here, we want to force creation of the
[email protected]36b6dcb2008-11-12 01:19:57620 // session service so that user can restore what was open.
[email protected]92371eb2011-04-28 11:50:15621 SessionService* session_service =
622 SessionServiceFactory::GetForProfile(profile());
[email protected]36b6dcb2008-11-12 01:19:57623 if (session_service)
624 session_service->WindowClosing(session_id());
625
[email protected]92371eb2011-04-28 11:50:15626 TabRestoreService* tab_restore_service =
627 TabRestoreServiceFactory::GetForProfile(profile());
[email protected]a636d8e52012-02-28 15:40:41628
629#if defined(USE_AURA)
630 if (tab_restore_service && is_app())
631 tab_restore_service->BrowserClosing(tab_restore_service_delegate());
632#endif
633
[email protected]b35b26b32011-05-05 20:35:14634 if (tab_restore_service && is_type_tabbed() && tab_count())
[email protected]fb7da4132011-03-15 15:51:00635 tab_restore_service->BrowserClosing(tab_restore_service_delegate());
[email protected]d8375fd2008-11-25 22:45:39636
[email protected]911f8152010-03-18 16:46:40637 // TODO(sky): convert session/tab restore to use notification.
[email protected]ad50def52011-10-19 23:17:07638 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27639 chrome::NOTIFICATION_BROWSER_CLOSING,
[email protected]6c2381d2011-10-19 02:52:53640 content::Source<Browser>(this),
[email protected]1e0c7402012-07-27 10:48:15641 content::NotificationService::NoDetails());
[email protected]911f8152010-03-18 16:46:40642
[email protected]52877dbc62012-06-29 22:22:03643 chrome::CloseAllTabs(this);
[email protected]36b6dcb2008-11-12 01:19:57644}
645
[email protected]28788c7d2011-05-20 23:03:45646void Browser::OnWindowActivated() {
647 // On some platforms we want to automatically reload tabs that are
648 // killed when the user selects them.
[email protected]52877dbc62012-06-29 22:22:03649 WebContents* contents = chrome::GetActiveWebContents(this);
[email protected]be1f56ab2011-12-22 06:55:31650 if (contents && contents->GetCrashedStatus() ==
[email protected]28788c7d2011-05-20 23:03:45651 base::TERMINATION_STATUS_PROCESS_WAS_KILLED) {
652 if (CommandLine::ForCurrentProcess()->HasSwitch(
653 switches::kReloadKilledTabs)) {
[email protected]a37d4b02012-06-25 21:56:10654 chrome::Reload(this, CURRENT_TAB);
[email protected]28788c7d2011-05-20 23:03:45655 }
656 }
657}
658
[email protected]69444cc2009-04-09 20:40:06659////////////////////////////////////////////////////////////////////////////////
660// In-progress download termination handling:
661
662void Browser::InProgressDownloadResponse(bool cancel_downloads) {
663 if (cancel_downloads) {
664 cancel_download_confirmation_state_ = RESPONSE_RECEIVED;
[email protected]a37d4b02012-06-25 21:56:10665 chrome::CloseWindow(this);
[email protected]69444cc2009-04-09 20:40:06666 return;
667 }
668
669 // Sets the confirmation state to NOT_PROMPTED so that if the user tries to
670 // close again we'll show the warning again.
671 cancel_download_confirmation_state_ = NOT_PROMPTED;
672
673 // Show the download page so the user can figure-out what downloads are still
674 // in-progress.
[email protected]5d9cace72012-06-21 16:07:12675 chrome::ShowDownloads(this);
[email protected]69444cc2009-04-09 20:40:06676}
677
[email protected]5a17d492011-10-24 19:37:03678Browser::DownloadClosePreventionType Browser::OkToCloseWithInProgressDownloads(
679 int* num_downloads_blocking) const {
680 DCHECK(num_downloads_blocking);
681 *num_downloads_blocking = 0;
682
[email protected]f9b6c092012-07-09 19:32:28683 if (IsAttemptingToCloseBrowser())
[email protected]5a17d492011-10-24 19:37:03684 return DOWNLOAD_CLOSE_OK;
685
686 // If we're not running a full browser process with a profile manager
687 // (testing), it's ok to close the browser.
688 if (!g_browser_process->profile_manager())
689 return DOWNLOAD_CLOSE_OK;
690
691 int total_download_count = DownloadService::DownloadCountAllProfiles();
692 if (total_download_count == 0)
693 return DOWNLOAD_CLOSE_OK; // No downloads; can definitely close.
694
695 // Figure out how many windows are open total, and associated with this
696 // profile, that are relevant for the ok-to-close decision.
697 int profile_window_count = 0;
698 int total_window_count = 0;
699 for (BrowserList::const_iterator iter = BrowserList::begin();
700 iter != BrowserList::end(); ++iter) {
701 // Don't count this browser window or any other in the process of closing.
702 Browser* const browser = *iter;
[email protected]f9b6c092012-07-09 19:32:28703 // Window closing may be delayed, and windows that are in the process of
704 // closing don't count against our totals.
705 if (browser == this || browser->IsAttemptingToCloseBrowser())
[email protected]5a17d492011-10-24 19:37:03706 continue;
707
708 if ((*iter)->profile() == profile())
709 profile_window_count++;
710 total_window_count++;
711 }
712
713 // If there aren't any other windows, we're at browser shutdown,
714 // which would cancel all current downloads.
715 if (total_window_count == 0) {
716 *num_downloads_blocking = total_download_count;
717 return DOWNLOAD_CLOSE_BROWSER_SHUTDOWN;
718 }
719
720 // If there aren't any other windows on our profile, and we're an incognito
721 // profile, and there are downloads associated with that profile,
722 // those downloads would be cancelled by our window (-> profile) close.
723 DownloadService* download_service =
724 DownloadServiceFactory::GetForProfile(profile());
725 if (profile_window_count == 0 && download_service->DownloadCount() > 0 &&
726 profile()->IsOffTheRecord()) {
727 *num_downloads_blocking = download_service->DownloadCount();
728 return DOWNLOAD_CLOSE_LAST_WINDOW_IN_INCOGNITO_PROFILE;
729 }
730
731 // Those are the only conditions under which we will block shutdown.
732 return DOWNLOAD_CLOSE_OK;
733}
734
[email protected]69444cc2009-04-09 20:40:06735////////////////////////////////////////////////////////////////////////////////
[email protected]c848d3d92010-09-16 21:57:45736// Browser, TabStripModel pass-thrus:
737
738int Browser::tab_count() const {
[email protected]2fc15ae2012-05-06 00:01:37739 return tab_strip_model_->count();
[email protected]c848d3d92010-09-16 21:57:45740}
741
[email protected]1ea49d52011-04-12 17:44:44742int Browser::active_index() const {
[email protected]2fc15ae2012-05-06 00:01:37743 return tab_strip_model_->active_index();
[email protected]c848d3d92010-09-16 21:57:45744}
745
[email protected]c848d3d92010-09-16 21:57:45746////////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:57747// Browser, Tab adding/showing functions:
748
[email protected]227393e2011-06-29 21:28:39749void Browser::WindowFullscreenStateChanged() {
[email protected]01a5a762011-11-21 05:40:55750 fullscreen_controller_->WindowFullscreenStateChanged();
[email protected]5d98294912012-06-27 22:57:40751 command_controller_->FullscreenStateChanged();
[email protected]9adb8b12011-12-09 06:49:13752 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TOGGLE_FULLSCREEN);
[email protected]ae2622c2009-07-30 23:47:58753}
754
[email protected]36b6dcb2008-11-12 01:19:57755///////////////////////////////////////////////////////////////////////////////
756// Browser, Assorted browser commands:
757
[email protected]41d9faf2012-02-28 23:46:02758void Browser::ToggleFullscreenModeWithExtension(const GURL& extension_url) {
759 fullscreen_controller_->ToggleFullscreenModeWithExtension(extension_url);
[email protected]9282cea2009-02-18 18:49:00760}
761
[email protected]d3766932011-08-04 22:18:23762#if defined(OS_MACOSX)
[email protected]184717d52012-02-22 04:46:16763void Browser::TogglePresentationMode() {
764 fullscreen_controller_->TogglePresentationMode();
[email protected]d3766932011-08-04 22:18:23765}
766#endif
767
[email protected]a3e18c42009-03-04 23:36:05768bool Browser::SupportsWindowFeature(WindowFeature feature) const {
[email protected]018cf362010-05-05 22:43:22769 return SupportsWindowFeatureImpl(feature, true);
770}
[email protected]f5bf8ccf2010-02-05 18:19:25771
[email protected]018cf362010-05-05 22:43:22772bool Browser::CanSupportWindowFeature(WindowFeature feature) const {
773 return SupportsWindowFeatureImpl(feature, false);
[email protected]a3e18c42009-03-04 23:36:05774}
775
[email protected]fbd77592008-11-12 20:50:27776void Browser::ToggleEncodingAutoDetect() {
[email protected]7f6f44c2011-12-14 13:23:38777 content::RecordAction(UserMetricsAction("AutoDetectChange"));
[email protected]fbd77592008-11-12 20:50:27778 encoding_auto_detect_.SetValue(!encoding_auto_detect_.GetValue());
[email protected]a697f4c2009-09-14 22:30:18779 // If "auto detect" is turned on, then any current override encoding
780 // is cleared. This also implicitly performs a reload.
781 // OTOH, if "auto detect" is turned off, we don't change the currently
782 // active encoding.
783 if (encoding_auto_detect_.GetValue()) {
[email protected]52877dbc62012-06-29 22:22:03784 WebContents* contents = chrome::GetActiveWebContents(this);
[email protected]a697f4c2009-09-14 22:30:18785 if (contents)
[email protected]b2fe07d12010-02-09 14:38:08786 contents->ResetOverrideEncoding();
[email protected]a697f4c2009-09-14 22:30:18787 }
[email protected]fbd77592008-11-12 20:50:27788}
789
790void Browser::OverrideEncoding(int encoding_id) {
[email protected]7f6f44c2011-12-14 13:23:38791 content::RecordAction(UserMetricsAction("OverrideEncoding"));
[email protected]41fc0322009-09-04 22:23:40792 const std::string selected_encoding =
[email protected]fbd77592008-11-12 20:50:27793 CharacterEncoding::GetCanonicalEncodingNameByCommandId(encoding_id);
[email protected]52877dbc62012-06-29 22:22:03794 WebContents* contents = chrome::GetActiveWebContents(this);
[email protected]57c6a652009-05-04 07:58:34795 if (!selected_encoding.empty() && contents)
[email protected]b2fe07d12010-02-09 14:38:08796 contents->SetOverrideEncoding(selected_encoding);
[email protected]fbd77592008-11-12 20:50:27797 // Update the list of recently selected encodings.
[email protected]41fc0322009-09-04 22:23:40798 std::string new_selected_encoding_list;
[email protected]ddd231e2010-06-29 20:35:19799 if (CharacterEncoding::UpdateRecentlySelectedEncoding(
800 profile_->GetPrefs()->GetString(prefs::kRecentlySelectedEncoding),
[email protected]41fc0322009-09-04 22:23:40801 encoding_id,
802 &new_selected_encoding_list)) {
[email protected]fbd77592008-11-12 20:50:27803 profile_->GetPrefs()->SetString(prefs::kRecentlySelectedEncoding,
[email protected]ddd231e2010-06-29 20:35:19804 new_selected_encoding_list);
[email protected]fbd77592008-11-12 20:50:27805 }
[email protected]36b6dcb2008-11-12 01:19:57806}
807
[email protected]84214982008-12-10 18:49:10808void Browser::OpenFile() {
[email protected]7f6f44c2011-12-14 13:23:38809 content::RecordAction(UserMetricsAction("OpenFile"));
[email protected]92f54082012-07-31 01:43:14810 select_file_dialog_ = ui::SelectFileDialog::Create(
[email protected]6e1fcd12012-07-02 17:14:20811 this, new ChromeSelectFilePolicy(
812 chrome::GetActiveWebContents(this)));
[email protected]84214982008-12-10 18:49:10813
[email protected]35896a32010-06-09 08:42:51814 const FilePath directory = profile_->last_selected_directory();
815
[email protected]84214982008-12-10 18:49:10816 // TODO(beng): figure out how to juggle this.
[email protected]90556dd2012-06-07 20:26:18817 gfx::NativeWindow parent_window = window_->GetNativeWindow();
[email protected]479cce782012-09-15 20:15:53818 ui::SelectFileDialog::FileTypeInfo file_types;
819 file_types.support_gdata = true;
[email protected]92f54082012-07-31 01:43:14820 select_file_dialog_->SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE,
[email protected]35896a32010-06-09 08:42:51821 string16(), directory,
[email protected]479cce782012-09-15 20:15:53822 &file_types, 0, FILE_PATH_LITERAL(""),
[email protected]d56bcd22009-03-16 19:51:56823 parent_window, NULL);
[email protected]84214982008-12-10 18:49:10824}
825
[email protected]92655362012-04-20 21:33:17826void Browser::UpdateDownloadShelfVisibility(bool visible) {
827 if (GetStatusBubble())
828 GetStatusBubble()->UpdateDownloadShelfVisibility(visible);
829}
830
[email protected]36b6dcb2008-11-12 01:19:57831///////////////////////////////////////////////////////////////////////////////
832
833// static
[email protected]d9083482012-01-06 00:38:46834bool Browser::RunUnloadEventsHelper(WebContents* contents) {
[email protected]075969d2012-04-17 20:05:55835 // If the WebContents is not connected yet, then there's no unload
836 // handler we can fire even if the WebContents has an unload listener.
[email protected]ecc523f62010-09-28 22:45:40837 // One case where we hit this is in a tab that has an infinite loop
838 // before load.
[email protected]5aab5e22010-12-08 22:13:29839 if (contents->NeedToFireBeforeUnload()) {
[email protected]ecc523f62010-09-28 22:45:40840 // If the page has unload listeners, then we tell the renderer to fire
841 // them. Once they have fired, we'll get a message back saying whether
842 // to proceed closing the page or not, which sends us back to this method
[email protected]5aab5e22010-12-08 22:13:29843 // with the NeedToFireBeforeUnload bit cleared.
[email protected]151a63d2011-12-20 22:32:52844 contents->GetRenderViewHost()->FirePageBeforeUnload(false);
[email protected]ecc523f62010-09-28 22:45:40845 return true;
846 }
847 return false;
848}
849
850// static
[email protected]1d3e9172012-06-12 02:59:13851void Browser::JSOutOfMemoryHelper(WebContents* web_contents) {
852 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
[email protected]3d61dd02012-06-07 00:05:53853 if (!tab_contents)
[email protected]2a6bc3e2011-12-28 23:51:33854 return;
855
[email protected]3d61dd02012-06-07 00:05:53856 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper();
[email protected]2a6bc3e2011-12-28 23:51:33857 infobar_helper->AddInfoBar(new SimpleAlertInfoBarDelegate(
858 infobar_helper,
859 NULL,
860 l10n_util::GetStringUTF16(IDS_JS_OUT_OF_MEMORY_PROMPT),
861 true));
[email protected]7d189022011-08-25 22:54:20862}
863
864// static
[email protected]1d3e9172012-06-12 02:59:13865void Browser::RegisterProtocolHandlerHelper(WebContents* web_contents,
[email protected]7d189022011-08-25 22:54:20866 const std::string& protocol,
867 const GURL& url,
[email protected]3a3b75a2012-06-01 08:38:36868 const string16& title,
[email protected]7ae9c322012-07-17 23:46:44869 bool user_gesture,
870 BrowserWindow* window) {
[email protected]1d3e9172012-06-12 02:59:13871 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
[email protected]3d61dd02012-06-07 00:05:53872 if (!tab_contents || tab_contents->profile()->IsOffTheRecord())
[email protected]7d189022011-08-25 22:54:20873 return;
874
[email protected]7d189022011-08-25 22:54:20875 ProtocolHandler handler =
876 ProtocolHandler::CreateProtocolHandler(protocol, url, title);
877
878 ProtocolHandlerRegistry* registry =
[email protected]3d61dd02012-06-07 00:05:53879 tab_contents->profile()->GetProtocolHandlerRegistry();
[email protected]7ae9c322012-07-17 23:46:44880 TabSpecificContentSettings* content_settings =
881 tab_contents->content_settings();
[email protected]7d189022011-08-25 22:54:20882
[email protected]96affbc82012-07-23 18:04:42883 if (registry->SilentlyHandleRegisterHandlerRequest(handler))
884 return;
885
[email protected]7ae9c322012-07-17 23:46:44886 if (!user_gesture && window) {
887 content_settings->set_pending_protocol_handler(handler);
888 content_settings->set_previous_protocol_handler(
889 registry->GetHandlerFor(handler.protocol()));
890 window->GetLocationBar()->UpdateContentSettingsIcons();
[email protected]3fa5e192012-07-15 00:40:34891 return;
[email protected]7ae9c322012-07-17 23:46:44892 }
893
894 // Make sure content-setting icon is turned off in case the page does
895 // ungestured and gestured RPH calls.
896 if (window) {
897 content_settings->ClearPendingProtocolHandler();
898 window->GetLocationBar()->UpdateContentSettingsIcons();
899 }
[email protected]3fa5e192012-07-15 00:40:34900
[email protected]96affbc82012-07-23 18:04:42901 content::RecordAction(
902 UserMetricsAction("RegisterProtocolHandler.InfoBar_Shown"));
903 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper();
[email protected]d3ddb6d2012-01-06 02:51:17904
[email protected]96affbc82012-07-23 18:04:42905 RegisterProtocolHandlerInfoBarDelegate* rph_delegate =
906 new RegisterProtocolHandlerInfoBarDelegate(infobar_helper,
907 registry,
908 handler);
[email protected]d3ddb6d2012-01-06 02:51:17909
[email protected]d715fb802012-08-20 16:34:13910 for (size_t i = 0; i < infobar_helper->GetInfoBarCount(); i++) {
[email protected]96affbc82012-07-23 18:04:42911 InfoBarDelegate* delegate = infobar_helper->GetInfoBarDelegateAt(i);
912 RegisterProtocolHandlerInfoBarDelegate* cast_delegate =
913 delegate->AsRegisterProtocolHandlerInfoBarDelegate();
914 if (cast_delegate != NULL && cast_delegate->IsReplacedBy(rph_delegate)) {
915 infobar_helper->ReplaceInfoBar(cast_delegate, rph_delegate);
916 rph_delegate = NULL;
917 break;
[email protected]d3ddb6d2012-01-06 02:51:17918 }
[email protected]7d189022011-08-25 22:54:20919 }
[email protected]96affbc82012-07-23 18:04:42920
921 if (rph_delegate != NULL)
922 infobar_helper->AddInfoBar(rph_delegate);
[email protected]7d189022011-08-25 22:54:20923}
924
925// static
[email protected]1d3e9172012-06-12 02:59:13926void Browser::FindReplyHelper(WebContents* web_contents,
[email protected]b888919c2011-09-02 00:32:16927 int request_id,
928 int number_of_matches,
929 const gfx::Rect& selection_rect,
930 int active_match_ordinal,
931 bool final_update) {
[email protected]1d3e9172012-06-12 02:59:13932 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
[email protected]3d61dd02012-06-07 00:05:53933 if (!tab_contents || !tab_contents->find_tab_helper())
[email protected]b888919c2011-09-02 00:32:16934 return;
935
[email protected]3d61dd02012-06-07 00:05:53936 tab_contents->find_tab_helper()->HandleFindReply(request_id,
937 number_of_matches,
938 selection_rect,
939 active_match_ordinal,
940 final_update);
[email protected]b888919c2011-09-02 00:32:16941}
942
[email protected]c55d3d122012-07-10 16:53:41943// static
944void Browser::RequestMediaAccessPermissionHelper(
945 content::WebContents* web_contents,
946 const content::MediaStreamRequest* request,
947 const content::MediaResponseCallback& callback) {
948 TabContents* tab = TabContents::FromWebContents(web_contents);
949
950 scoped_ptr<MediaStreamDevicesController>
951 controller(new MediaStreamDevicesController(tab->profile(),
952 request,
953 callback));
954 if (!controller->DismissInfoBarAndTakeActionOnSettings()) {
955 InfoBarTabHelper* infobar_helper = tab->infobar_tab_helper();
956 InfoBarDelegate* old_infobar = NULL;
[email protected]d715fb802012-08-20 16:34:13957 for (size_t i = 0; i < infobar_helper->GetInfoBarCount(); ++i) {
[email protected]c55d3d122012-07-10 16:53:41958 old_infobar = infobar_helper->GetInfoBarDelegateAt(i)->
959 AsMediaStreamInfoBarDelegate();
960 if (old_infobar)
961 break;
962 }
963
964 InfoBarDelegate* infobar =
965 new MediaStreamInfoBarDelegate(infobar_helper, controller.release());
966 if (old_infobar)
967 infobar_helper->ReplaceInfoBar(old_infobar, infobar);
968 else
969 infobar_helper->AddInfoBar(infobar);
970 }
971}
972
[email protected]3d61dd02012-06-07 00:05:53973void Browser::UpdateUIForNavigationInTab(TabContents* contents,
[email protected]ef557022012-03-16 10:05:33974 content::PageTransition transition,
975 bool user_initiated) {
[email protected]2fc15ae2012-05-06 00:01:37976 tab_strip_model_->TabNavigating(contents, transition);
[email protected]ef557022012-03-16 10:05:33977
[email protected]52877dbc62012-06-29 22:22:03978 bool contents_is_selected = contents == chrome::GetActiveTabContents(this);
[email protected]ef557022012-03-16 10:05:33979 if (user_initiated && contents_is_selected && window()->GetLocationBar()) {
980 // Forcibly reset the location bar if the url is going to change in the
981 // current tab, since otherwise it won't discard any ongoing user edits,
982 // since it doesn't realize this is a user-initiated action.
983 window()->GetLocationBar()->Revert();
984 }
985
986 if (GetStatusBubble())
987 GetStatusBubble()->Hide();
988
989 // Update the location bar. This is synchronous. We specifically don't
990 // update the load state since the load hasn't started yet and updating it
991 // will put it out of sync with the actual state like whether we're
992 // displaying a favicon, which controls the throbber. If we updated it here,
993 // the throbber will show the default favicon for a split second when
994 // navigating away from the new tab page.
995 ScheduleUIUpdate(contents->web_contents(), content::INVALIDATE_TYPE_URL);
996
[email protected]e1f288d2012-05-23 19:36:10997 if (contents_is_selected)
[email protected]ef557022012-03-16 10:05:33998 contents->web_contents()->Focus();
999}
1000
1001///////////////////////////////////////////////////////////////////////////////
1002// Browser, PageNavigator implementation:
1003
1004WebContents* Browser::OpenURL(const OpenURLParams& params) {
1005 return OpenURLFromTab(NULL, params);
1006}
1007
[email protected]485fba42009-03-24 23:27:291008///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:571009// Browser, TabStripModelObserver implementation:
1010
[email protected]3d61dd02012-06-07 00:05:531011void Browser::TabInsertedAt(TabContents* contents,
[email protected]36b6dcb2008-11-12 01:19:571012 int index,
1013 bool foreground) {
[email protected]6451fe92012-08-30 18:37:261014 SetAsDelegate(contents->web_contents(), this);
[email protected]50a9a76b2012-09-12 14:19:361015 SessionTabHelper* session_tab_helper =
1016 SessionTabHelper::FromWebContents(contents->web_contents());
1017 session_tab_helper->SetWindowID(session_id());
[email protected]e524ff92011-03-30 13:26:581018
[email protected]fb312eb2012-09-24 23:21:101019 content::NotificationService::current()->Notify(
1020 chrome::NOTIFICATION_TAB_PARENTED,
1021 content::Source<content::WebContents>(contents->web_contents()),
1022 content::NotificationService::NoDetails());
1023
[email protected]c37e3b62010-01-22 00:15:171024 SyncHistoryWithTabs(index);
[email protected]36b6dcb2008-11-12 01:19:571025
[email protected]159f7762008-12-19 14:58:271026 // Make sure the loading state is updated correctly, otherwise the throbber
1027 // won't start if the page is loading.
[email protected]2a6bc3e2011-12-28 23:51:331028 LoadingStateChanged(contents->web_contents());
[email protected]159f7762008-12-19 14:58:271029
[email protected]432115822011-07-10 15:52:271030 registrar_.Add(this, content::NOTIFICATION_INTERSTITIAL_ATTACHED,
[email protected]fbc5e5f92012-01-02 06:08:321031 content::Source<WebContents>(contents->web_contents()));
[email protected]e397a4472011-12-21 21:47:501032
1033 registrar_.Add(this, content::NOTIFICATION_INTERSTITIAL_DETACHED,
[email protected]fbc5e5f92012-01-02 06:08:321034 content::Source<WebContents>(contents->web_contents()));
[email protected]40600152012-09-26 10:04:311035 SessionService* session_service =
1036 SessionServiceFactory::GetForProfile(profile_);
1037 if (session_service)
1038 session_service->TabInserted(contents->web_contents());
[email protected]36b6dcb2008-11-12 01:19:571039}
1040
[email protected]f2cc7382010-10-15 18:03:051041void Browser::TabClosingAt(TabStripModel* tab_strip_model,
[email protected]3d61dd02012-06-07 00:05:531042 TabContents* contents,
[email protected]f2cc7382010-10-15 18:03:051043 int index) {
[email protected]d9083482012-01-06 00:38:461044 fullscreen_controller_->OnTabClosing(contents->web_contents());
[email protected]40600152012-09-26 10:04:311045 SessionService* session_service =
1046 SessionServiceFactory::GetForProfile(profile_);
1047 if (session_service)
1048 session_service->TabClosing(contents->web_contents());
[email protected]ad50def52011-10-19 23:17:071049 content::NotificationService::current()->Notify(
[email protected]884033e2012-04-16 19:38:421050 chrome::NOTIFICATION_TAB_CLOSING,
[email protected]c5eed492012-01-04 17:07:501051 content::Source<NavigationController>(
[email protected]cca0f1e2012-01-03 18:27:461052 &contents->web_contents()->GetController()),
[email protected]ad50def52011-10-19 23:17:071053 content::NotificationService::NoDetails());
[email protected]36b6dcb2008-11-12 01:19:571054
[email protected]075969d2012-04-17 20:05:551055 // Sever the WebContents' connection back to us.
[email protected]6451fe92012-08-30 18:37:261056 SetAsDelegate(contents->web_contents(), NULL);
[email protected]36b6dcb2008-11-12 01:19:571057}
1058
[email protected]3d61dd02012-06-07 00:05:531059void Browser::TabDetachedAt(TabContents* contents, int index) {
[email protected]c37e3b62010-01-22 00:15:171060 TabDetachedAtImpl(contents, index, DETACH_TYPE_DETACH);
[email protected]36b6dcb2008-11-12 01:19:571061}
1062
[email protected]3d61dd02012-06-07 00:05:531063void Browser::TabDeactivated(TabContents* contents) {
[email protected]01a5a762011-11-21 05:40:551064 fullscreen_controller_->OnTabDeactivated(contents);
[email protected]0dc50db2012-09-19 17:57:141065 search_delegate_->OnTabDeactivated(contents->web_contents());
[email protected]e3a5d5b2012-06-22 17:56:461066
[email protected]85ff2c42009-05-29 21:22:031067 // Save what the user's currently typing, so it can be restored when we
1068 // switch back to this tab.
[email protected]83a2610a2012-01-05 01:00:271069 window_->GetLocationBar()->SaveStateToContents(contents->web_contents());
[email protected]85ff2c42009-05-29 21:22:031070}
1071
[email protected]3d61dd02012-06-07 00:05:531072void Browser::ActiveTabChanged(TabContents* old_contents,
1073 TabContents* new_contents,
[email protected]3aca95d42011-05-24 22:06:541074 int index,
1075 bool user_gesture) {
[email protected]a8f024392011-01-13 21:50:161076 // On some platforms we want to automatically reload tabs that are
1077 // killed when the user selects them.
[email protected]9d7518f2012-04-17 02:15:201078 bool did_reload = false;
[email protected]d9083482012-01-06 00:38:461079 if (user_gesture && new_contents->web_contents()->GetCrashedStatus() ==
[email protected]a8f024392011-01-13 21:50:161080 base::TERMINATION_STATUS_PROCESS_WAS_KILLED) {
1081 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
[email protected]f8a35772011-02-02 23:17:561082 if (parsed_command_line.HasSwitch(switches::kReloadKilledTabs)) {
[email protected]734d26692012-03-26 18:00:251083 LOG(WARNING) << "Reloading killed tab at " << index;
[email protected]d6d4dae2012-04-18 17:47:321084 static int reload_count = 0;
[email protected]24c463a2012-04-20 22:33:501085 UMA_HISTOGRAM_CUSTOM_COUNTS(
1086 "Tabs.SadTab.ReloadCount", ++reload_count, 1, 1000, 50);
[email protected]a37d4b02012-06-25 21:56:101087 chrome::Reload(this, CURRENT_TAB);
[email protected]9d7518f2012-04-17 02:15:201088 did_reload = true;
[email protected]f8a35772011-02-02 23:17:561089 }
[email protected]a8f024392011-01-13 21:50:161090 }
1091
[email protected]002efb042011-10-07 17:30:241092 // Discarded tabs always get reloaded.
[email protected]52877dbc62012-06-29 22:22:031093 if (!did_reload && tab_strip_model_->IsTabDiscarded(index)) {
[email protected]d6d4dae2012-04-18 17:47:321094 LOG(WARNING) << "Reloading discarded tab at " << index;
1095 static int reload_count = 0;
[email protected]24c463a2012-04-20 22:33:501096 UMA_HISTOGRAM_CUSTOM_COUNTS(
1097 "Tabs.Discard.ReloadCount", ++reload_count, 1, 1000, 50);
[email protected]a37d4b02012-06-25 21:56:101098 chrome::Reload(this, CURRENT_TAB);
[email protected]002efb042011-10-07 17:30:241099 }
1100
[email protected]36b6dcb2008-11-12 01:19:571101 // If we have any update pending, do it now.
[email protected]24f1477532011-11-22 22:55:361102 if (chrome_updater_factory_.HasWeakPtrs() && old_contents)
[email protected]36b6dcb2008-11-12 01:19:571103 ProcessPendingUIUpdates();
1104
[email protected]36b6dcb2008-11-12 01:19:571105 // Propagate the profile to the location bar.
1106 UpdateToolbar(true);
1107
[email protected]e3a5d5b2012-06-22 17:56:461108 // Propagate tab state to toolbar, tab-strip, etc.
1109 UpdateSearchState(new_contents);
1110
[email protected]943d8122010-06-11 02:13:451111 // Update reload/stop state.
[email protected]5d98294912012-06-27 22:57:401112 command_controller_->LoadingStateChanged(
1113 new_contents->web_contents()->IsLoading(), true);
[email protected]36b6dcb2008-11-12 01:19:571114
[email protected]84214982008-12-10 18:49:101115 // Update commands to reflect current state.
[email protected]5d98294912012-06-27 22:57:401116 command_controller_->TabStateChanged();
[email protected]36b6dcb2008-11-12 01:19:571117
1118 // Reset the status bubble.
[email protected]be3877f2009-01-14 15:51:101119 StatusBubble* status_bubble = GetStatusBubble();
1120 if (status_bubble) {
1121 status_bubble->Hide();
[email protected]36b6dcb2008-11-12 01:19:571122
[email protected]be3877f2009-01-14 15:51:101123 // Show the loading state (if any).
[email protected]51d83bdf2011-12-09 16:09:291124 status_bubble->SetStatus(
[email protected]ece4aca2012-09-19 22:22:581125 CoreTabHelper::FromWebContents(chrome::GetActiveWebContents(this))->
1126 GetStatusText());
[email protected]be3877f2009-01-14 15:51:101127 }
[email protected]36b6dcb2008-11-12 01:19:571128
[email protected]24db8a072009-10-29 20:35:371129 if (HasFindBarController()) {
[email protected]9c318862011-02-01 22:27:241130 find_bar_controller_->ChangeTabContents(new_contents);
[email protected]b77cb302009-10-29 04:09:171131 find_bar_controller_->find_bar()->MoveWindowIfNecessary(gfx::Rect(), true);
[email protected]4801ecc2009-04-05 04:52:581132 }
1133
[email protected]36b6dcb2008-11-12 01:19:571134 // Update sessions. Don't force creation of sessions. If sessions doesn't
1135 // exist, the change will be picked up by sessions when created.
[email protected]92371eb2011-04-28 11:50:151136 SessionService* session_service =
1137 SessionServiceFactory::GetForProfileIfExisting(profile_);
[email protected]2fc15ae2012-05-06 00:01:371138 if (session_service && !tab_strip_model_->closing_all()) {
[email protected]41f022e2012-01-31 05:56:401139 session_service->SetSelectedTabInWindow(session_id(), active_index());
[email protected]36b6dcb2008-11-12 01:19:571140 }
[email protected]09b29342011-06-24 19:18:481141
1142 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_SWITCH);
[email protected]36b6dcb2008-11-12 01:19:571143}
1144
[email protected]3d61dd02012-06-07 00:05:531145void Browser::TabMoved(TabContents* contents,
[email protected]36b6dcb2008-11-12 01:19:571146 int from_index,
[email protected]1a242c32010-02-02 21:20:541147 int to_index) {
[email protected]36b6dcb2008-11-12 01:19:571148 DCHECK(from_index >= 0 && to_index >= 0);
1149 // Notify the history service.
1150 SyncHistoryWithTabs(std::min(from_index, to_index));
1151}
1152
[email protected]da25dd52011-01-11 19:06:301153void Browser::TabReplacedAt(TabStripModel* tab_strip_model,
[email protected]3d61dd02012-06-07 00:05:531154 TabContents* old_contents,
1155 TabContents* new_contents,
[email protected]8a34e6602010-10-02 17:29:431156 int index) {
[email protected]c37e3b62010-01-22 00:15:171157 TabDetachedAtImpl(old_contents, index, DETACH_TYPE_REPLACE);
[email protected]40600152012-09-26 10:04:311158 SessionService* session_service =
1159 SessionServiceFactory::GetForProfile(profile_);
1160 if (session_service)
1161 session_service->TabClosing(old_contents->web_contents());
[email protected]41f022e2012-01-31 05:56:401162 TabInsertedAt(new_contents, index, (index == active_index()));
[email protected]c37e3b62010-01-22 00:15:171163
[email protected]a26023822011-12-29 00:23:551164 int entry_count =
[email protected]d9083482012-01-06 00:38:461165 new_contents->web_contents()->GetController().GetEntryCount();
[email protected]c37e3b62010-01-22 00:15:171166 if (entry_count > 0) {
1167 // Send out notification so that observers are updated appropriately.
[email protected]d9083482012-01-06 00:38:461168 new_contents->web_contents()->GetController().NotifyEntryChanged(
1169 new_contents->web_contents()->GetController().GetEntryAtIndex(
[email protected]4a63e242011-12-12 15:23:081170 entry_count - 1),
[email protected]c37e3b62010-01-22 00:15:171171 entry_count - 1);
1172 }
[email protected]47e020a2010-10-15 14:43:371173
[email protected]47e020a2010-10-15 14:43:371174 if (session_service) {
1175 // The new_contents may end up with a different navigation stack. Force
1176 // the session service to update itself.
[email protected]52877dbc62012-06-29 22:22:031177 session_service->TabRestored(new_contents,
1178 tab_strip_model_->IsTabPinned(index));
[email protected]47e020a2010-10-15 14:43:371179 }
[email protected]2914600d2011-01-31 23:25:141180
[email protected]a0262432012-04-13 15:48:091181 content::DevToolsManager::GetInstance()->ContentsReplaced(
[email protected]d9083482012-01-06 00:38:461182 old_contents->web_contents(), new_contents->web_contents());
[email protected]c37e3b62010-01-22 00:15:171183}
1184
[email protected]3d61dd02012-06-07 00:05:531185void Browser::TabPinnedStateChanged(TabContents* contents, int index) {
[email protected]92371eb2011-04-28 11:50:151186 SessionService* session_service =
1187 SessionServiceFactory::GetForProfileIfExisting(profile());
[email protected]5c0e6482009-07-14 20:20:091188 if (session_service) {
[email protected]50a9a76b2012-09-12 14:19:361189 SessionTabHelper* session_tab_helper =
1190 SessionTabHelper::FromWebContents(contents->web_contents());
[email protected]5c0e6482009-07-14 20:20:091191 session_service->SetPinnedState(
1192 session_id(),
[email protected]50a9a76b2012-09-12 14:19:361193 session_tab_helper->session_id(),
[email protected]52877dbc62012-06-29 22:22:031194 tab_strip_model_->IsTabPinned(index));
[email protected]5c0e6482009-07-14 20:20:091195 }
1196}
1197
[email protected]36b6dcb2008-11-12 01:19:571198void Browser::TabStripEmpty() {
1199 // Close the frame after we return to the message loop (not immediately,
1200 // otherwise it will destroy this object before the stack has a chance to
1201 // cleanly unwind.)
1202 // Note: This will be called several times if TabStripEmpty is called several
1203 // times. This is because it does not close the window if tabs are
1204 // still present.
[email protected]0586b0e2010-02-12 21:38:371205 MessageLoop::current()->PostTask(
[email protected]24f1477532011-11-22 22:55:361206 FROM_HERE, base::Bind(&Browser::CloseFrame, weak_factory_.GetWeakPtr()));
[email protected]36b6dcb2008-11-12 01:19:571207}
1208
[email protected]b3996ba2012-08-08 00:39:131209bool Browser::PreHandleKeyboardEvent(content::WebContents* source,
1210 const NativeWebKeyboardEvent& event,
[email protected]fc761142012-06-08 04:49:201211 bool* is_keyboard_shortcut) {
1212 // Escape exits tabbed fullscreen mode.
1213 // TODO(koz): Write a test for this http://crbug.com/100441.
1214 if (event.windowsKeyCode == 27 &&
1215 fullscreen_controller_->HandleUserPressedEscape()) {
1216 return true;
1217 }
1218 return window()->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
1219}
1220
[email protected]b3996ba2012-08-08 00:39:131221void Browser::HandleKeyboardEvent(content::WebContents* source,
1222 const NativeWebKeyboardEvent& event) {
[email protected]fc761142012-06-08 04:49:201223 window()->HandleKeyboardEvent(event);
1224}
1225
[email protected]92655362012-04-20 21:33:171226bool Browser::TabsNeedBeforeUnloadFired() {
[email protected]f9b6c092012-07-09 19:32:281227 return unload_controller_->TabsNeedBeforeUnloadFired();
[email protected]92655362012-04-20 21:33:171228}
1229
[email protected]d013f142012-04-27 17:31:141230bool Browser::IsMouseLocked() const {
1231 return fullscreen_controller_->IsMouseLocked();
[email protected]92655362012-04-20 21:33:171232}
1233
1234void Browser::OnWindowDidShow() {
1235 if (window_has_shown_)
1236 return;
1237 window_has_shown_ = true;
1238
[email protected]b5febf72012-07-29 18:10:091239// CurrentProcessInfo::CreationTime() is currently only implemented on Mac and
1240// Windows.
1241#if defined(OS_MACOSX) || defined(OS_WIN)
1242 // Measure the latency from startup till the first browser window becomes
1243 // visible.
1244 static bool is_first_browser_window = true;
1245 if (is_first_browser_window &&
1246 !startup_metric_utils::WasNonBrowserUIDisplayed()) {
1247 is_first_browser_window = false;
1248 const base::Time* process_creation_time =
1249 base::CurrentProcessInfo::CreationTime();
1250
1251 if (process_creation_time) {
1252 UMA_HISTOGRAM_LONG_TIMES(
1253 "Startup.BrowserWindowDisplay",
1254 base::Time::Now() - *process_creation_time);
1255 }
1256 }
1257#endif // OS_MACOSX || OS_WIN
1258
[email protected]92655362012-04-20 21:33:171259 // Nothing to do for non-tabbed windows.
1260 if (!is_type_tabbed())
1261 return;
1262
1263 // Show any pending global error bubble.
1264 GlobalErrorService* service =
1265 GlobalErrorServiceFactory::GetForProfile(profile());
1266 GlobalError* error = service->GetFirstGlobalErrorWithBubbleView();
1267 if (error)
1268 error->ShowBubbleView(this);
1269}
1270
1271void Browser::ShowFirstRunBubble() {
1272 window()->GetLocationBar()->ShowFirstRunBubble();
1273}
1274
1275///////////////////////////////////////////////////////////////////////////////
[email protected]674bc592011-12-20 23:00:421276// Browser, content::WebContentsDelegate implementation:
initial.commit09911bf2008-07-26 23:55:291277
[email protected]e5d549d2011-12-28 01:29:201278WebContents* Browser::OpenURLFromTab(WebContents* source,
[email protected]00c37fc2011-08-02 00:22:501279 const OpenURLParams& params) {
[email protected]78e2edc2012-07-01 23:32:281280 chrome::NavigateParams nav_params(this, params.url, params.transition);
[email protected]52877dbc62012-06-29 22:22:031281 nav_params.source_contents = chrome::GetTabContentsAt(this,
[email protected]e52d0a42012-06-08 22:44:161282 tab_strip_model_->GetIndexOfWebContents(source));
[email protected]cb8fb01a2011-12-05 21:01:141283 nav_params.referrer = params.referrer;
[email protected]00c37fc2011-08-02 00:22:501284 nav_params.disposition = params.disposition;
1285 nav_params.tabstrip_add_types = TabStripModel::ADD_NONE;
[email protected]78e2edc2012-07-01 23:32:281286 nav_params.window_action = chrome::NavigateParams::SHOW_WINDOW;
[email protected]00c37fc2011-08-02 00:22:501287 nav_params.user_gesture = true;
1288 nav_params.override_encoding = params.override_encoding;
[email protected]e47ae9472011-10-13 19:48:341289 nav_params.is_renderer_initiated = params.is_renderer_initiated;
[email protected]4ad5d77d2011-12-03 02:00:481290 nav_params.transferred_global_request_id =
1291 params.transferred_global_request_id;
[email protected]78e2edc2012-07-01 23:32:281292 chrome::Navigate(&nav_params);
[email protected]00c37fc2011-08-02 00:22:501293
1294 return nav_params.target_contents ?
[email protected]d9083482012-01-06 00:38:461295 nav_params.target_contents->web_contents() : NULL;
initial.commit09911bf2008-07-26 23:55:291296}
1297
[email protected]2a6bc3e2011-12-28 23:51:331298void Browser::NavigationStateChanged(const WebContents* source,
initial.commit09911bf2008-07-26 23:55:291299 unsigned changed_flags) {
initial.commit09911bf2008-07-26 23:55:291300 // Only update the UI when something visible has changed.
[email protected]e83f1682008-09-07 23:57:401301 if (changed_flags)
initial.commit09911bf2008-07-26 23:55:291302 ScheduleUIUpdate(source, changed_flags);
1303
[email protected]83c2e232011-10-07 21:36:461304 // We can synchronously update commands since they will only change once per
1305 // navigation, so we don't have to worry about flickering. We do, however,
1306 // need to update the command state early on load to always present usable
1307 // actions in the face of slow-to-commit pages.
[email protected]d9083482012-01-06 00:38:461308 if (changed_flags & (content::INVALIDATE_TYPE_URL |
1309 content::INVALIDATE_TYPE_LOAD))
[email protected]5d98294912012-06-27 22:57:401310 command_controller_->TabStateChanged();
initial.commit09911bf2008-07-26 23:55:291311}
1312
[email protected]2a6bc3e2011-12-28 23:51:331313void Browser::AddNewContents(WebContents* source,
1314 WebContents* new_contents,
initial.commit09911bf2008-07-26 23:55:291315 WindowOpenDisposition disposition,
1316 const gfx::Rect& initial_pos,
[email protected]eda238a12012-09-07 23:44:001317 bool user_gesture,
1318 bool* was_blocked) {
[email protected]52877dbc62012-06-29 22:22:031319 chrome::AddWebContents(this, source, new_contents, disposition, initial_pos,
[email protected]eda238a12012-09-07 23:44:001320 user_gesture, was_blocked);
initial.commit09911bf2008-07-26 23:55:291321}
1322
[email protected]2a6bc3e2011-12-28 23:51:331323void Browser::ActivateContents(WebContents* contents) {
[email protected]52877dbc62012-06-29 22:22:031324 chrome::ActivateTabAt(this, tab_strip_model_->GetIndexOfWebContents(contents),
1325 false);
[email protected]f3e99e32008-07-30 04:48:391326 window_->Activate();
initial.commit09911bf2008-07-26 23:55:291327}
1328
[email protected]2a6bc3e2011-12-28 23:51:331329void Browser::DeactivateContents(WebContents* contents) {
[email protected]ea42e7782010-08-23 23:58:121330 window_->Deactivate();
1331}
1332
[email protected]2a6bc3e2011-12-28 23:51:331333void Browser::LoadingStateChanged(WebContents* source) {
[email protected]2fc15ae2012-05-06 00:01:371334 window_->UpdateLoadingAnimations(tab_strip_model_->TabsAreLoading());
[email protected]f3e99e32008-07-30 04:48:391335 window_->UpdateTitleBar();
initial.commit09911bf2008-07-26 23:55:291336
[email protected]52877dbc62012-06-29 22:22:031337 WebContents* selected_contents = chrome::GetActiveWebContents(this);
[email protected]3c9e1872010-11-18 16:17:491338 if (source == selected_contents) {
[email protected]f5d978c2011-07-21 14:43:511339 bool is_loading = source->IsLoading();
[email protected]5d98294912012-06-27 22:57:401340 command_controller_->LoadingStateChanged(is_loading, false);
[email protected]43d259d2010-09-03 21:37:461341 if (GetStatusBubble()) {
[email protected]0dd3a0ab2011-02-18 08:17:441342 GetStatusBubble()->SetStatus(
[email protected]ece4aca2012-09-19 22:22:581343 CoreTabHelper::FromWebContents(chrome::GetActiveWebContents(this))->
[email protected]52877dbc62012-06-29 22:22:031344 GetStatusText());
[email protected]43d259d2010-09-03 21:37:461345 }
initial.commit09911bf2008-07-26 23:55:291346 }
1347}
1348
[email protected]a81343d232011-12-27 07:39:201349void Browser::CloseContents(WebContents* source) {
[email protected]f9b6c092012-07-09 19:32:281350 if (unload_controller_->CanCloseContents(source))
1351 chrome::CloseWebContents(this, source);
initial.commit09911bf2008-07-26 23:55:291352}
1353
[email protected]2a6bc3e2011-12-28 23:51:331354void Browser::MoveContents(WebContents* source, const gfx::Rect& pos) {
[email protected]b35b26b32011-05-05 20:35:141355 if (!IsPopupOrPanel(source)) {
initial.commit09911bf2008-07-26 23:55:291356 NOTREACHED() << "moving invalid browser type";
1357 return;
1358 }
[email protected]e561e202010-03-24 17:57:121359 window_->SetBounds(pos);
initial.commit09911bf2008-07-26 23:55:291360}
1361
[email protected]2a6bc3e2011-12-28 23:51:331362bool Browser::IsPopupOrPanel(const WebContents* source) const {
initial.commit09911bf2008-07-26 23:55:291363 // A non-tabbed BROWSER is an unconstrained popup.
[email protected]b35b26b32011-05-05 20:35:141364 return is_type_popup() || is_type_panel();
initial.commit09911bf2008-07-26 23:55:291365}
1366
[email protected]2a6bc3e2011-12-28 23:51:331367void Browser::UpdateTargetURL(WebContents* source, int32 page_id,
[email protected]d952a052011-09-06 18:42:451368 const GURL& url) {
[email protected]be3877f2009-01-14 15:51:101369 if (!GetStatusBubble())
1370 return;
1371
[email protected]52877dbc62012-06-29 22:22:031372 if (source == chrome::GetActiveWebContents(this)) {
[email protected]36b6dcb2008-11-12 01:19:571373 PrefService* prefs = profile_->GetPrefs();
[email protected]65233052011-08-22 19:02:481374 GetStatusBubble()->SetURL(url, prefs->GetString(prefs::kAcceptLanguages));
[email protected]36b6dcb2008-11-12 01:19:571375 }
1376}
1377
[email protected]92655362012-04-20 21:33:171378void Browser::ContentsMouseEvent(
1379 WebContents* source, const gfx::Point& location, bool motion) {
1380 if (!GetStatusBubble())
1381 return;
1382
[email protected]52877dbc62012-06-29 22:22:031383 if (source == chrome::GetActiveWebContents(this)) {
[email protected]92655362012-04-20 21:33:171384 GetStatusBubble()->MouseMoved(location, !motion);
1385 if (!motion)
1386 GetStatusBubble()->SetURL(GURL(), std::string());
1387 }
[email protected]3a6a3b62009-05-27 21:36:201388}
1389
[email protected]36b6dcb2008-11-12 01:19:571390void Browser::ContentsZoomChange(bool zoom_in) {
[email protected]5d98294912012-06-27 22:57:401391 chrome::ExecuteCommand(this, zoom_in ? IDC_ZOOM_PLUS : IDC_ZOOM_MINUS);
[email protected]36b6dcb2008-11-12 01:19:571392}
1393
[email protected]2a6bc3e2011-12-28 23:51:331394void Browser::WebContentsFocused(WebContents* contents) {
1395 window_->WebContentsFocused(contents);
[email protected]7e383692009-06-12 19:14:541396}
1397
[email protected]b3996ba2012-08-08 00:39:131398bool Browser::TakeFocus(content::WebContents* source,
1399 bool reverse) {
[email protected]ad50def52011-10-19 23:17:071400 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271401 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:531402 content::Source<Browser>(this),
[email protected]ad50def52011-10-19 23:17:071403 content::NotificationService::NoDetails());
[email protected]130efb02009-09-18 18:54:351404 return false;
1405}
1406
[email protected]b7a756d42012-01-23 18:08:171407gfx::Rect Browser::GetRootWindowResizerRect() const {
1408 return window_->GetRootWindowResizerRect();
1409}
1410
[email protected]1d3e9172012-06-12 02:59:131411void Browser::BeforeUnloadFired(WebContents* web_contents,
[email protected]36b6dcb2008-11-12 01:19:571412 bool proceed,
1413 bool* proceed_to_fire_unload) {
[email protected]f9b6c092012-07-09 19:32:281414 *proceed_to_fire_unload =
1415 unload_controller_->BeforeUnloadFired(web_contents, proceed);
[email protected]36b6dcb2008-11-12 01:19:571416}
1417
[email protected]a26dc362010-04-23 01:48:581418void Browser::SetFocusToLocationBar(bool select_all) {
[email protected]1a380622009-02-17 17:33:131419 // Two differences between this and FocusLocationBar():
1420 // (1) This doesn't get recorded in user metrics, since it's called
1421 // internally.
1422 // (2) This checks whether the location bar can be focused, and if not, clears
1423 // the focus. FocusLocationBar() is only reached when the location bar is
1424 // focusable, but this may be reached at other times, e.g. while in
1425 // fullscreen mode, where we need to leave focus in a consistent state.
[email protected]a26dc362010-04-23 01:48:581426 window_->SetFocusToLocationBar(select_all);
[email protected]4bcefee2009-01-29 15:57:571427}
1428
[email protected]7d5925a2009-03-03 02:59:121429void Browser::RenderWidgetShowing() {
1430 window_->DisableInactiveFrame();
1431}
[email protected]a239c3f2009-02-17 22:13:191432
[email protected]0a2aeb82009-05-15 21:52:481433int Browser::GetExtraRenderViewHeight() const {
1434 return window_->GetExtraRenderViewHeight();
1435}
1436
[email protected]2a6bc3e2011-12-28 23:51:331437void Browser::OnStartDownload(WebContents* source,
[email protected]e582fdd2011-12-20 16:48:171438 content::DownloadItem* download) {
[email protected]6790f95f2012-09-21 19:49:451439 WebContents* constrained = GetConstrainingWebContents(source);
1440 if (constrained != source) {
[email protected]686493142011-07-15 21:47:221441 // Download in a constrained popup is shown in the tab that opened it.
[email protected]6790f95f2012-09-21 19:49:451442 constrained->GetDelegate()->OnStartDownload(constrained, download);
[email protected]686493142011-07-15 21:47:221443 return;
1444 }
1445
1446 if (!window())
1447 return;
1448
[email protected]27c14f02012-06-22 17:29:581449 // GetDownloadShelf creates the download shelf if it was not yet created.
1450 DownloadShelf* shelf = window()->GetDownloadShelf();
1451 shelf->AddDownload(new DownloadItemModel(download));
1452 // Don't show the animation for "Save file" downloads.
1453 // For non-theme extensions, we don't show the download animation.
1454 // Show animation in same window as the download shelf. Download shelf
[email protected]1f1cb852012-09-11 02:58:191455 // may not be in the same window that initiated the download.
[email protected]27c14f02012-06-22 17:29:581456 // Don't show the animation if the selected tab is not visible (i.e. the
1457 // window is minimized, we're in a unit test, etc.).
[email protected]52877dbc62012-06-29 22:22:031458 WebContents* shelf_tab = chrome::GetActiveWebContents(shelf->browser());
[email protected]27c14f02012-06-22 17:29:581459 if ((download->GetTotalBytes() > 0) &&
1460 !download_crx_util::IsExtensionDownload(*download) &&
1461 platform_util::IsVisible(shelf_tab->GetNativeView()) &&
1462 ui::Animation::ShouldRenderRichAnimation()) {
1463 DownloadStartedAnimation::Show(shelf_tab);
[email protected]a0e4b072011-08-17 01:47:071464 }
[email protected]686493142011-07-15 21:47:221465
1466 // If the download occurs in a new tab, close it.
[email protected]f5fa20e2011-12-21 22:35:561467 if (source->GetController().IsInitialNavigation() && tab_count() > 1)
[email protected]686493142011-07-15 21:47:221468 CloseContents(source);
1469}
1470
[email protected]2a6bc3e2011-12-28 23:51:331471void Browser::ViewSourceForTab(WebContents* source, const GURL& page_url) {
[email protected]1788e772010-12-15 16:40:501472 DCHECK(source);
[email protected]52877dbc62012-06-29 22:22:031473 TabContents* tab_contents = chrome::GetTabContentsAt(this,
[email protected]e52d0a42012-06-08 22:44:161474 tab_strip_model_->GetIndexOfWebContents(source));
[email protected]2cd4fde2012-06-26 03:10:261475 chrome::ViewSource(this, tab_contents);
[email protected]77d8d622010-12-15 10:30:121476}
1477
[email protected]2a6bc3e2011-12-28 23:51:331478void Browser::ViewSourceForFrame(WebContents* source,
[email protected]932b7a12011-03-09 12:50:271479 const GURL& frame_url,
1480 const std::string& frame_content_state) {
1481 DCHECK(source);
[email protected]52877dbc62012-06-29 22:22:031482 TabContents* tab_contents = chrome::GetTabContentsAt(this,
[email protected]e52d0a42012-06-08 22:44:161483 tab_strip_model_->GetIndexOfWebContents(source));
[email protected]2cd4fde2012-06-26 03:10:261484 chrome::ViewSource(this, tab_contents, frame_url, frame_content_state);
[email protected]932b7a12011-03-09 12:50:271485}
1486
[email protected]4fdf6742012-01-10 20:14:361487void Browser::ShowRepostFormWarningDialog(WebContents* source) {
[email protected]51296c62012-09-26 13:02:441488 TabModalConfirmDialog::Create(new RepostFormWarningController(source),
1489 TabContents::FromWebContents(source));
[email protected]14f3408a2009-08-31 20:53:531490}
1491
[email protected]4fdf6742012-01-10 20:14:361492bool Browser::ShouldCreateWebContents(
1493 WebContents* web_contents,
1494 int route_id,
1495 WindowContainerType window_container_type,
[email protected]03b6d552012-03-29 04:03:011496 const string16& frame_name,
1497 const GURL& target_url) {
[email protected]4fdf6742012-01-10 20:14:361498 if (window_container_type == WINDOW_CONTAINER_TYPE_BACKGROUND) {
1499 // If a BackgroundContents is created, suppress the normal WebContents.
1500 return !MaybeCreateBackgroundContents(
[email protected]03b6d552012-03-29 04:03:011501 route_id, web_contents, frame_name, target_url);
[email protected]4fdf6742012-01-10 20:14:361502 }
1503
1504 return true;
1505}
1506
1507void Browser::WebContentsCreated(WebContents* source_contents,
1508 int64 source_frame_id,
1509 const GURL& target_url,
1510 WebContents* new_contents) {
[email protected]6451fe92012-08-30 18:37:261511 // Adopt the WebContents now, so all observers are in place, as the network
[email protected]3d61dd02012-06-07 00:05:531512 // requests for its initial navigation will start immediately. The WebContents
1513 // will later be inserted into this browser using Browser::Navigate via
[email protected]6451fe92012-08-30 18:37:261514 // AddNewContents.
1515 AdoptAsTabContents(new_contents);
[email protected]4fdf6742012-01-10 20:14:361516
1517 // Notify.
1518 RetargetingDetails details;
1519 details.source_web_contents = source_contents;
1520 details.source_frame_id = source_frame_id;
1521 details.target_url = target_url;
1522 details.target_web_contents = new_contents;
1523 details.not_yet_in_tabstrip = true;
1524 content::NotificationService::current()->Notify(
1525 chrome::NOTIFICATION_RETARGETING,
1526 content::Source<Profile>(profile_),
1527 content::Details<RetargetingDetails>(&details));
[email protected]80c7b802011-10-28 19:59:081528}
1529
[email protected]2a6bc3e2011-12-28 23:51:331530void Browser::ContentRestrictionsChanged(WebContents* source) {
[email protected]5d98294912012-06-27 22:57:401531 command_controller_->ContentRestrictionsChanged();
[email protected]9e823662010-10-13 23:36:001532}
1533
[email protected]2a6bc3e2011-12-28 23:51:331534void Browser::RendererUnresponsive(WebContents* source) {
[email protected]c19d9542011-11-15 22:26:421535 // Ignore hangs if print preview is open.
[email protected]1d3e9172012-06-12 02:59:131536 TabContents* tab_contents = TabContents::FromWebContents(source);
[email protected]3d61dd02012-06-07 00:05:531537 if (tab_contents) {
[email protected]c19d9542011-11-15 22:26:421538 printing::PrintPreviewTabController* controller =
1539 printing::PrintPreviewTabController::GetInstance();
1540 if (controller) {
[email protected]3d61dd02012-06-07 00:05:531541 TabContents* preview_tab =
1542 controller->GetPrintPreviewForTab(tab_contents);
1543 if (preview_tab && preview_tab != tab_contents) {
[email protected]c19d9542011-11-15 22:26:421544 return;
1545 }
1546 }
1547 }
1548
[email protected]87586fb72012-07-02 13:15:021549 chrome::ShowHungRendererDialog(source);
[email protected]55452902011-06-01 21:57:471550}
1551
[email protected]2a6bc3e2011-12-28 23:51:331552void Browser::RendererResponsive(WebContents* source) {
[email protected]87586fb72012-07-02 13:15:021553 chrome::HideHungRendererDialog(source);
[email protected]55452902011-06-01 21:57:471554}
1555
[email protected]2a6bc3e2011-12-28 23:51:331556void Browser::WorkerCrashed(WebContents* source) {
[email protected]1d3e9172012-06-12 02:59:131557 TabContents* tab_contents = TabContents::FromWebContents(source);
[email protected]3d61dd02012-06-07 00:05:531558 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper();
[email protected]95a33ed62011-09-30 15:07:081559 infobar_helper->AddInfoBar(new SimpleAlertInfoBarDelegate(
1560 infobar_helper,
1561 NULL,
1562 l10n_util::GetStringUTF16(IDS_WEBWORKER_CRASHED_PROMPT),
[email protected]55452902011-06-01 21:57:471563 true));
[email protected]0dd3a0ab2011-02-18 08:17:441564}
1565
[email protected]1d3e9172012-06-12 02:59:131566void Browser::DidNavigateMainFramePostCommit(WebContents* web_contents) {
[email protected]52877dbc62012-06-29 22:22:031567 if (web_contents == chrome::GetActiveWebContents(this))
[email protected]09b29342011-06-24 19:18:481568 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]93f230e02011-06-01 14:40:001569}
1570
[email protected]1d3e9172012-06-12 02:59:131571void Browser::DidNavigateToPendingEntry(WebContents* web_contents) {
[email protected]52877dbc62012-06-29 22:22:031572 if (web_contents == chrome::GetActiveWebContents(this))
[email protected]09b29342011-06-24 19:18:481573 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]93f230e02011-06-01 14:40:001574}
1575
[email protected]3ab9cb82011-06-03 18:02:071576content::JavaScriptDialogCreator* Browser::GetJavaScriptDialogCreator() {
1577 return GetJavaScriptDialogCreatorInstance();
1578}
1579
[email protected]1d3e9172012-06-12 02:59:131580content::ColorChooser* Browser::OpenColorChooser(WebContents* web_contents,
[email protected]da8543762012-03-20 08:52:201581 int color_chooser_id,
[email protected]55578b0a2012-04-18 14:31:321582 SkColor color) {
[email protected]da8543762012-03-20 08:52:201583#if defined(OS_WIN)
1584 // On Windows, only create a color chooser if one doesn't exist, because we
1585 // can't close the old color chooser dialog.
1586 if (!color_chooser_.get())
[email protected]1d3e9172012-06-12 02:59:131587 color_chooser_.reset(content::ColorChooser::Create(color_chooser_id,
1588 web_contents,
[email protected]da8543762012-03-20 08:52:201589 color));
1590#else
1591 if (color_chooser_.get())
1592 color_chooser_->End();
[email protected]1d3e9172012-06-12 02:59:131593 color_chooser_.reset(content::ColorChooser::Create(color_chooser_id,
1594 web_contents,
[email protected]da8543762012-03-20 08:52:201595 color));
1596#endif
1597 return color_chooser_.get();
1598}
1599
1600void Browser::DidEndColorChooser() {
1601 color_chooser_.reset();
1602}
1603
[email protected]1d3e9172012-06-12 02:59:131604void Browser::RunFileChooser(WebContents* web_contents,
[email protected]8caadeb2011-11-22 02:45:231605 const content::FileChooserParams& params) {
[email protected]1d3e9172012-06-12 02:59:131606 FileSelectHelper::RunFileChooser(web_contents, params);
[email protected]3a29a6e2011-08-24 18:26:211607}
1608
[email protected]1d3e9172012-06-12 02:59:131609void Browser::EnumerateDirectory(WebContents* web_contents,
[email protected]33f8ad52012-05-22 18:10:131610 int request_id,
[email protected]3a29a6e2011-08-24 18:26:211611 const FilePath& path) {
[email protected]1d3e9172012-06-12 02:59:131612 FileSelectHelper::EnumerateDirectory(web_contents, request_id, path);
[email protected]3a29a6e2011-08-24 18:26:211613}
1614
[email protected]1d3e9172012-06-12 02:59:131615void Browser::ToggleFullscreenModeForTab(WebContents* web_contents,
[email protected]01a5a762011-11-21 05:40:551616 bool enter_fullscreen) {
[email protected]1d3e9172012-06-12 02:59:131617 fullscreen_controller_->ToggleFullscreenModeForTab(web_contents,
1618 enter_fullscreen);
[email protected]8a5e0ca2011-08-25 06:30:471619}
1620
[email protected]1d3e9172012-06-12 02:59:131621bool Browser::IsFullscreenForTabOrPending(
1622 const WebContents* web_contents) const {
1623 return fullscreen_controller_->IsFullscreenForTabOrPending(web_contents);
[email protected]5d5f7af2011-10-01 01:38:121624}
1625
[email protected]1d3e9172012-06-12 02:59:131626void Browser::JSOutOfMemory(WebContents* web_contents) {
1627 JSOutOfMemoryHelper(web_contents);
[email protected]7d189022011-08-25 22:54:201628}
1629
[email protected]1d3e9172012-06-12 02:59:131630void Browser::RegisterProtocolHandler(WebContents* web_contents,
[email protected]7d189022011-08-25 22:54:201631 const std::string& protocol,
1632 const GURL& url,
[email protected]3a3b75a2012-06-01 08:38:361633 const string16& title,
1634 bool user_gesture) {
[email protected]1d3e9172012-06-12 02:59:131635 RegisterProtocolHandlerHelper(
[email protected]7ae9c322012-07-17 23:46:441636 web_contents, protocol, url, title, user_gesture, window());
[email protected]7d189022011-08-25 22:54:201637}
1638
[email protected]3a3b75a2012-06-01 08:38:361639void Browser::RegisterIntentHandler(
[email protected]1d3e9172012-06-12 02:59:131640 WebContents* web_contents,
[email protected]3a3b75a2012-06-01 08:38:361641 const webkit_glue::WebIntentServiceData& data,
1642 bool user_gesture) {
[email protected]1d3e9172012-06-12 02:59:131643 RegisterIntentHandlerHelper(web_contents, data, user_gesture);
[email protected]7d189022011-08-25 22:54:201644}
1645
[email protected]0d9989d2011-12-21 20:26:001646void Browser::WebIntentDispatch(
[email protected]1d3e9172012-06-12 02:59:131647 WebContents* web_contents,
1648 content::WebIntentsDispatcher* intents_dispatcher) {
[email protected]59d73ec2012-09-01 02:24:271649 if (!web_intents::IsWebIntentsEnabledForProfile(profile_)) {
1650 web_intents::RecordIntentsDispatchDisabled();
[email protected]828e61af2011-09-14 19:45:061651 return;
[email protected]59d73ec2012-09-01 02:24:271652 }
[email protected]828e61af2011-09-14 19:45:061653
[email protected]59d73ec2012-09-01 02:24:271654 web_intents::RecordIntentDispatchRequested();
[email protected]86238d22012-05-22 20:00:111655
[email protected]bd52a982012-06-26 23:34:291656 if (!web_contents) {
1657 // Intent is system-caused and the picker will show over the currently
1658 // active web contents.
[email protected]52877dbc62012-06-29 22:22:031659 web_contents = chrome::GetActiveWebContents(this);
[email protected]bd52a982012-06-26 23:34:291660 }
[email protected]231cf3e2012-09-25 20:54:471661 WebIntentPickerController* web_intent_picker_controller =
1662 WebIntentPickerController::FromWebContents(web_contents);
1663 web_intent_picker_controller->SetIntentsDispatcher(intents_dispatcher);
1664 web_intent_picker_controller->ShowDialog(
[email protected]0d9989d2011-12-21 20:26:001665 intents_dispatcher->GetIntent().action,
1666 intents_dispatcher->GetIntent().type);
[email protected]b888919c2011-09-02 00:32:161667}
1668
[email protected]2a6bc3e2011-12-28 23:51:331669void Browser::UpdatePreferredSize(WebContents* source,
[email protected]0548c5352011-09-07 00:33:331670 const gfx::Size& pref_size) {
1671 window_->UpdatePreferredSize(source, pref_size);
1672}
1673
[email protected]61e2b3cc2012-03-02 16:13:341674void Browser::ResizeDueToAutoResize(WebContents* source,
1675 const gfx::Size& new_size) {
1676 window_->ResizeDueToAutoResize(source, new_size);
1677}
1678
[email protected]1d3e9172012-06-12 02:59:131679void Browser::FindReply(WebContents* web_contents,
[email protected]92655362012-04-20 21:33:171680 int request_id,
1681 int number_of_matches,
1682 const gfx::Rect& selection_rect,
1683 int active_match_ordinal,
1684 bool final_update) {
[email protected]1d3e9172012-06-12 02:59:131685 FindReplyHelper(web_contents, request_id, number_of_matches, selection_rect,
[email protected]92655362012-04-20 21:33:171686 active_match_ordinal, final_update);
1687}
1688
[email protected]1d3e9172012-06-12 02:59:131689void Browser::RequestToLockMouse(WebContents* web_contents,
[email protected]a9c81f02012-06-01 00:15:441690 bool user_gesture,
1691 bool last_unlocked_by_target) {
[email protected]1d3e9172012-06-12 02:59:131692 fullscreen_controller_->RequestToLockMouse(web_contents,
[email protected]a9c81f02012-06-01 00:15:441693 user_gesture,
1694 last_unlocked_by_target);
[email protected]55c87fa2011-10-15 07:28:281695}
1696
[email protected]5ef8e372011-10-18 04:25:551697void Browser::LostMouseLock() {
[email protected]01a5a762011-11-21 05:40:551698 fullscreen_controller_->LostMouseLock();
[email protected]5ef8e372011-10-18 04:25:551699}
1700
[email protected]f13b4202012-06-12 23:53:231701void Browser::RequestMediaAccessPermission(
1702 content::WebContents* web_contents,
1703 const content::MediaStreamRequest* request,
1704 const content::MediaResponseCallback& callback) {
[email protected]c55d3d122012-07-10 16:53:411705 RequestMediaAccessPermissionHelper(web_contents, request, callback);
[email protected]f13b4202012-06-12 23:53:231706}
1707
[email protected]36b6dcb2008-11-12 01:19:571708///////////////////////////////////////////////////////////////////////////////
[email protected]51d83bdf2011-12-09 16:09:291709// Browser, CoreTabHelperDelegate implementation:
[email protected]a0366a52011-02-04 20:04:211710
[email protected]ece4aca2012-09-19 22:22:581711void Browser::SwapTabContents(content::WebContents* old_contents,
1712 content::WebContents* new_contents) {
1713 int index = tab_strip_model_->GetIndexOfWebContents(old_contents);
[email protected]92655362012-04-20 21:33:171714 DCHECK_NE(TabStripModel::kNoTab, index);
[email protected]ece4aca2012-09-19 22:22:581715 TabContents* new_tab_contents = TabContents::FromWebContents(new_contents);
[email protected]2fc15ae2012-05-06 00:01:371716 tab_strip_model_->ReplaceTabContentsAt(index, new_tab_contents);
[email protected]92655362012-04-20 21:33:171717}
1718
[email protected]ece4aca2012-09-19 22:22:581719bool Browser::CanReloadContents(content::WebContents* web_contents) const {
[email protected]5d98294912012-06-27 22:57:401720 return chrome::CanReload(this);
[email protected]e1461952012-06-14 21:05:351721}
1722
[email protected]ece4aca2012-09-19 22:22:581723bool Browser::CanSaveContents(content::WebContents* web_contents) const {
[email protected]5d98294912012-06-27 22:57:401724 return chrome::CanSavePage(this);
[email protected]e1461952012-06-14 21:05:351725}
1726
[email protected]92655362012-04-20 21:33:171727///////////////////////////////////////////////////////////////////////////////
1728// Browser, SearchEngineTabHelperDelegate implementation:
1729
[email protected]3613347d2012-04-27 20:27:371730void Browser::ConfirmAddSearchProvider(TemplateURL* template_url,
[email protected]92655362012-04-20 21:33:171731 Profile* profile) {
1732 window()->ConfirmAddSearchProvider(template_url, profile);
1733}
1734
1735///////////////////////////////////////////////////////////////////////////////
1736// Browser, ConstrainedWindowTabHelperDelegate implementation:
1737
[email protected]3d61dd02012-06-07 00:05:531738void Browser::SetTabContentBlocked(TabContents* tab_contents, bool blocked) {
1739 int index = tab_strip_model_->GetIndexOfTabContents(tab_contents);
[email protected]92655362012-04-20 21:33:171740 if (index == TabStripModel::kNoTab) {
1741 NOTREACHED();
1742 return;
1743 }
[email protected]2fc15ae2012-05-06 00:01:371744 tab_strip_model_->SetTabBlocked(index, blocked);
[email protected]5d98294912012-06-27 22:57:401745 command_controller_->PrintingStateChanged();
[email protected]52877dbc62012-06-29 22:22:031746 if (!blocked && chrome::GetActiveTabContents(this) == tab_contents)
[email protected]3d61dd02012-06-07 00:05:531747 tab_contents->web_contents()->Focus();
[email protected]92655362012-04-20 21:33:171748}
1749
1750///////////////////////////////////////////////////////////////////////////////
1751// Browser, BlockedContentTabHelperDelegate implementation:
1752
[email protected]6790f95f2012-09-21 19:49:451753content::WebContents* Browser::GetConstrainingWebContents(
1754 content::WebContents* source) {
[email protected]92655362012-04-20 21:33:171755 return source;
1756}
1757
1758///////////////////////////////////////////////////////////////////////////////
1759// Browser, BookmarkTabHelperDelegate implementation:
1760
[email protected]1c5119c2012-09-19 00:08:571761void Browser::URLStarredChanged(content::WebContents* web_contents,
1762 bool starred) {
1763 if (web_contents == chrome::GetActiveWebContents(this))
[email protected]92655362012-04-20 21:33:171764 window_->SetStarredState(starred);
1765}
1766
1767///////////////////////////////////////////////////////////////////////////////
[email protected]6f80e932012-06-04 19:00:071768// Browser, ZoomObserver implementation:
1769
[email protected]dea9cee82012-09-25 03:10:521770void Browser::OnZoomChanged(content::WebContents* source,
1771 bool can_show_bubble) {
1772 if (source == chrome::GetActiveWebContents(this)) {
[email protected]6f80e932012-06-04 19:00:071773 // Only show the zoom bubble for zoom changes in the active window.
[email protected]5423c372012-08-22 05:50:161774 window_->ZoomChangedForActiveTab(can_show_bubble && window_->IsActive());
[email protected]6f80e932012-06-04 19:00:071775 }
1776}
1777
1778///////////////////////////////////////////////////////////////////////////////
[email protected]92f54082012-07-31 01:43:141779// Browser, ui::SelectFileDialog::Listener implementation:
[email protected]36b6dcb2008-11-12 01:19:571780
[email protected]561abe62009-04-06 18:08:341781void Browser::FileSelected(const FilePath& path, int index, void* params) {
[email protected]53f04c82012-07-26 02:31:091782 FileSelectedWithExtraInfo(ui::SelectedFileInfo(path, path), index, params);
1783}
1784
1785void Browser::FileSelectedWithExtraInfo(
1786 const ui::SelectedFileInfo& file_info,
1787 int index,
1788 void* params) {
1789 profile_->set_last_selected_directory(file_info.file_path.DirName());
1790
1791 const FilePath& path = file_info.local_path;
[email protected]72cbd322009-04-07 10:17:121792 GURL file_url = net::FilePathToFileURL(path);
[email protected]b7813a22012-04-04 18:41:021793
1794#if defined(OS_CHROMEOS)
[email protected]06809de2012-09-07 05:51:051795 gdata::util::ModifyDriveFileResourceUrl(profile_, path, &file_url);
[email protected]b7813a22012-04-04 18:41:021796#endif
1797
[email protected]e5d549d2011-12-28 01:29:201798 if (file_url.is_empty())
1799 return;
1800
1801 OpenURL(OpenURLParams(
1802 file_url, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
1803 false));
[email protected]36b6dcb2008-11-12 01:19:571804}
1805
1806///////////////////////////////////////////////////////////////////////////////
[email protected]6c2381d2011-10-19 02:52:531807// Browser, content::NotificationObserver implementation:
[email protected]36b6dcb2008-11-12 01:19:571808
[email protected]432115822011-07-10 15:52:271809void Browser::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:531810 const content::NotificationSource& source,
1811 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:271812 switch (type) {
[email protected]432115822011-07-10 15:52:271813 case content::NOTIFICATION_SSL_VISIBLE_STATE_CHANGED:
[email protected]e83f1682008-09-07 23:57:401814 // When the current tab's SSL state changes, we need to update the URL
[email protected]90e8d062008-09-08 02:26:321815 // bar to reflect the new state. Note that it's possible for the selected
1816 // tab contents to be NULL. This is because we listen for all sources
1817 // (NavigationControllers) for convenience, so the notification could
1818 // actually be for a different window while we're doing asynchronous
1819 // closing of this one.
[email protected]52877dbc62012-06-29 22:22:031820 if (chrome::GetActiveWebContents(this) &&
1821 &chrome::GetActiveWebContents(this)->GetController() ==
[email protected]c5eed492012-01-04 17:07:501822 content::Source<NavigationController>(source).ptr())
[email protected]36b6dcb2008-11-12 01:19:571823 UpdateToolbar(false);
[email protected]e83f1682008-09-07 23:57:401824 break;
1825
[email protected]432115822011-07-10 15:52:271826 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
[email protected]ce696a452011-05-31 17:47:361827 if (window()->GetLocationBar())
1828 window()->GetLocationBar()->UpdatePageActions();
[email protected]57f71b92009-09-11 19:31:381829
[email protected]b3f7fe22011-11-11 19:27:561830 // Close any tabs from the unloaded extension, unless it's terminated,
1831 // in which case let the sad tabs remain.
[email protected]1c321ee52012-05-21 03:02:341832 if (content::Details<extensions::UnloadedExtensionInfo>(
1833 details)->reason != extension_misc::UNLOAD_REASON_TERMINATE) {
[email protected]b3f7fe22011-11-11 19:27:561834 const Extension* extension =
[email protected]1c321ee52012-05-21 03:02:341835 content::Details<extensions::UnloadedExtensionInfo>(
1836 details)->extension;
[email protected]2fc15ae2012-05-06 00:01:371837 for (int i = tab_strip_model_->count() - 1; i >= 0; --i) {
[email protected]52877dbc62012-06-29 22:22:031838 WebContents* tc = chrome::GetTabContentsAt(this, i)->web_contents();
[email protected]1f1cb852012-09-11 02:58:191839 if (tc->GetURL().SchemeIs(chrome::kExtensionScheme) &&
1840 tc->GetURL().host() == extension->id())
[email protected]52877dbc62012-06-29 22:22:031841 chrome::CloseWebContents(this, tc);
[email protected]894bb502009-05-21 22:39:571842 }
1843 }
1844 break;
1845 }
1846
[email protected]432115822011-07-10 15:52:271847 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:531848 Profile* profile = content::Source<Profile>(source).ptr();
[email protected]fe8944f2011-08-30 17:47:321849 if (profile_->IsSameProfile(profile) && window()->GetLocationBar())
[email protected]ce696a452011-05-31 17:47:361850 window()->GetLocationBar()->InvalidatePageActions();
[email protected]371ed7a2009-08-25 15:22:461851 break;
1852 }
1853
[email protected]432115822011-07-10 15:52:271854 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED:
1855 case chrome::NOTIFICATION_EXTENSION_LOADED:
[email protected]1797f4a2011-08-30 18:00:281856 // During window creation on Windows we may end up calling into
1857 // SHAppBarMessage, which internally spawns a nested message loop. This
1858 // makes it possible for us to end up here before window creation has
1859 // completed,at which point window_ is NULL. See 94752 for details.
1860 if (window() && window()->GetLocationBar())
[email protected]ce696a452011-05-31 17:47:361861 window()->GetLocationBar()->UpdatePageActions();
[email protected]0dfe05c2011-02-23 23:03:361862 break;
[email protected]ad0c2e1b2010-01-30 00:00:101863
[email protected]6b40bb582012-03-15 20:50:381864#if defined(ENABLE_THEMES)
[email protected]432115822011-07-10 15:52:271865 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED:
[email protected]e001d412009-06-26 20:53:251866 window()->UserChangedTheme();
1867 break;
[email protected]6b40bb582012-03-15 20:50:381868#endif
[email protected]e001d412009-06-26 20:53:251869
[email protected]432115822011-07-10 15:52:271870 case chrome::NOTIFICATION_PREF_CHANGED: {
[email protected]6c2381d2011-10-19 02:52:531871 const std::string& pref_name =
1872 *content::Details<std::string>(details).ptr();
[email protected]7acfaf92012-07-11 15:51:591873 if (pref_name == prefs::kDevToolsDisabled) {
[email protected]97b46382011-05-02 13:23:041874 if (profile_->GetPrefs()->GetBoolean(prefs::kDevToolsDisabled))
[email protected]0e12d7d2011-12-01 16:21:441875 content::DevToolsManager::GetInstance()->CloseAllClientHosts();
[email protected]3710d0002011-10-11 00:35:001876 } else if (pref_name == prefs::kShowBookmarkBar) {
[email protected]3710d0002011-10-11 00:35:001877 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_PREF_CHANGE);
[email protected]6c6b02d2011-09-02 03:36:471878 } else if (pref_name == prefs::kHomePage) {
[email protected]6c2381d2011-10-19 02:52:531879 PrefService* pref_service = content::Source<PrefService>(source).ptr();
[email protected]6c6b02d2011-09-02 03:36:471880 MarkHomePageAsChanged(pref_service);
[email protected]2a7e7c152010-10-01 20:12:091881 } else {
[email protected]b9c505b2010-04-30 15:05:101882 NOTREACHED();
[email protected]2a7e7c152010-10-01 20:12:091883 }
[email protected]b9c505b2010-04-30 15:05:101884 break;
1885 }
1886
[email protected]ea049a02011-12-25 21:37:091887 case chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED: {
1888 WebContents* web_contents = content::Source<WebContents>(source).ptr();
[email protected]52877dbc62012-06-29 22:22:031889 if (web_contents == chrome::GetActiveWebContents(this)) {
[email protected]9a9a9e22011-05-13 22:20:351890 LocationBar* location_bar = window()->GetLocationBar();
1891 if (location_bar)
1892 location_bar->UpdateContentSettingsIcons();
1893 }
1894 break;
1895 }
1896
[email protected]432115822011-07-10 15:52:271897 case content::NOTIFICATION_INTERSTITIAL_ATTACHED:
[email protected]09b29342011-06-24 19:18:481898 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]e397a4472011-12-21 21:47:501899 break;
1900
1901 case content::NOTIFICATION_INTERSTITIAL_DETACHED:
1902 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]09b29342011-06-24 19:18:481903 break;
1904
[email protected]e83f1682008-09-07 23:57:401905 default:
1906 NOTREACHED() << "Got a notification we didn't register for.";
initial.commit09911bf2008-07-26 23:55:291907 }
1908}
1909
[email protected]c3166302012-09-25 21:27:081910void Browser::ModeChanged(const chrome::search::Mode& old_mode,
1911 const chrome::search::Mode& new_mode) {
1912 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
1913}
1914
[email protected]d3b98c82010-07-14 07:45:591915///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:571916// Browser, Command and state updating (private):
1917
[email protected]6c6b02d2011-09-02 03:36:471918void Browser::MarkHomePageAsChanged(PrefService* pref_service) {
1919 pref_service->SetBoolean(prefs::kHomePageChanged, true);
[email protected]6c6b02d2011-09-02 03:36:471920}
1921
[email protected]36b6dcb2008-11-12 01:19:571922///////////////////////////////////////////////////////////////////////////////
1923// Browser, UI update coalescing and handling (private):
1924
1925void Browser::UpdateToolbar(bool should_restore_state) {
[email protected]52877dbc62012-06-29 22:22:031926 window_->UpdateToolbar(chrome::GetActiveTabContents(this),
1927 should_restore_state);
initial.commit09911bf2008-07-26 23:55:291928}
1929
[email protected]e3a5d5b2012-06-22 17:56:461930void Browser::UpdateSearchState(TabContents* contents) {
1931 if (chrome::search::IsInstantExtendedAPIEnabled(profile_))
[email protected]0dc50db2012-09-19 17:57:141932 search_delegate_->OnTabActivated(contents->web_contents());
[email protected]e3a5d5b2012-06-22 17:56:461933}
1934
[email protected]2a6bc3e2011-12-28 23:51:331935void Browser::ScheduleUIUpdate(const WebContents* source,
[email protected]36b6dcb2008-11-12 01:19:571936 unsigned changed_flags) {
[email protected]a5231662009-09-26 00:56:001937 if (!source)
1938 return;
1939
[email protected]2b4355c2009-04-04 17:55:461940 // Do some synchronous updates.
[email protected]d9083482012-01-06 00:38:461941 if (changed_flags & content::INVALIDATE_TYPE_URL &&
[email protected]52877dbc62012-06-29 22:22:031942 source == chrome::GetActiveWebContents(this)) {
[email protected]36b6dcb2008-11-12 01:19:571943 // Only update the URL for the current tab. Note that we do not update
1944 // the navigation commands since those would have already been updated
1945 // synchronously by NavigationStateChanged.
1946 UpdateToolbar(false);
[email protected]d9083482012-01-06 00:38:461947 changed_flags &= ~content::INVALIDATE_TYPE_URL;
[email protected]36b6dcb2008-11-12 01:19:571948 }
[email protected]d9083482012-01-06 00:38:461949 if (changed_flags & content::INVALIDATE_TYPE_LOAD) {
[email protected]6ebdc9b2010-09-27 16:55:571950 // Update the loading state synchronously. This is so the throbber will
1951 // immediately start/stop, which gives a more snappy feel. We want to do
[email protected]a5231662009-09-26 00:56:001952 // this for any tab so they start & stop quickly.
[email protected]2fc15ae2012-05-06 00:01:371953 tab_strip_model_->UpdateTabContentsStateAt(
[email protected]52877dbc62012-06-29 22:22:031954 chrome::GetIndexOfTab(this, source),
[email protected]6ebdc9b2010-09-27 16:55:571955 TabStripModelObserver::LOADING_ONLY);
[email protected]d9083482012-01-06 00:38:461956 // The status bubble needs to be updated during INVALIDATE_TYPE_LOAD too,
1957 // but we do that asynchronously by not stripping INVALIDATE_TYPE_LOAD from
[email protected]6ebdc9b2010-09-27 16:55:571958 // changed_flags.
[email protected]8030f012009-09-25 18:09:371959 }
1960
[email protected]d9083482012-01-06 00:38:461961 if (changed_flags & content::INVALIDATE_TYPE_TITLE && !source->IsLoading()) {
[email protected]f1cd5e82009-10-23 17:19:031962 // To correctly calculate whether the title changed while not loading
1963 // we need to process the update synchronously. This state only matters for
1964 // the TabStripModel, so we notify the TabStripModel now and notify others
1965 // asynchronously.
[email protected]2fc15ae2012-05-06 00:01:371966 tab_strip_model_->UpdateTabContentsStateAt(
[email protected]52877dbc62012-06-29 22:22:031967 chrome::GetIndexOfTab(this, source),
[email protected]f1cd5e82009-10-23 17:19:031968 TabStripModelObserver::TITLE_NOT_LOADING);
1969 }
1970
[email protected]2b4355c2009-04-04 17:55:461971 // If the only updates were synchronously handled above, we're done.
[email protected]8030f012009-09-25 18:09:371972 if (changed_flags == 0)
[email protected]2b4355c2009-04-04 17:55:461973 return;
[email protected]36b6dcb2008-11-12 01:19:571974
1975 // Save the dirty bits.
[email protected]85354982009-09-25 19:58:401976 scheduled_updates_[source] |= changed_flags;
[email protected]36b6dcb2008-11-12 01:19:571977
[email protected]24f1477532011-11-22 22:55:361978 if (!chrome_updater_factory_.HasWeakPtrs()) {
[email protected]36b6dcb2008-11-12 01:19:571979 // No task currently scheduled, start another.
[email protected]0586b0e2010-02-12 21:38:371980 MessageLoop::current()->PostDelayedTask(
1981 FROM_HERE,
[email protected]24f1477532011-11-22 22:55:361982 base::Bind(&Browser::ProcessPendingUIUpdates,
1983 chrome_updater_factory_.GetWeakPtr()),
[email protected]11d6ab1a2012-01-12 21:37:261984 base::TimeDelta::FromMilliseconds(kUIUpdateCoalescingTimeMS));
[email protected]36b6dcb2008-11-12 01:19:571985 }
1986}
1987
1988void Browser::ProcessPendingUIUpdates() {
1989#ifndef NDEBUG
1990 // Validate that all tabs we have pending updates for exist. This is scary
1991 // because the pending list must be kept in sync with any detached or
[email protected]a5231662009-09-26 00:56:001992 // deleted tabs.
[email protected]85354982009-09-25 19:58:401993 for (UpdateMap::const_iterator i = scheduled_updates_.begin();
1994 i != scheduled_updates_.end(); ++i) {
[email protected]36b6dcb2008-11-12 01:19:571995 bool found = false;
1996 for (int tab = 0; tab < tab_count(); tab++) {
[email protected]52877dbc62012-06-29 22:22:031997 if (chrome::GetWebContentsAt(this, tab) == i->first) {
[email protected]36b6dcb2008-11-12 01:19:571998 found = true;
1999 break;
2000 }
2001 }
2002 DCHECK(found);
2003 }
2004#endif
2005
[email protected]24f1477532011-11-22 22:55:362006 chrome_updater_factory_.InvalidateWeakPtrs();
[email protected]36b6dcb2008-11-12 01:19:572007
[email protected]85354982009-09-25 19:58:402008 for (UpdateMap::const_iterator i = scheduled_updates_.begin();
2009 i != scheduled_updates_.end(); ++i) {
[email protected]36b6dcb2008-11-12 01:19:572010 // Do not dereference |contents|, it may be out-of-date!
[email protected]2a6bc3e2011-12-28 23:51:332011 const WebContents* contents = i->first;
[email protected]85354982009-09-25 19:58:402012 unsigned flags = i->second;
[email protected]36b6dcb2008-11-12 01:19:572013
[email protected]52877dbc62012-06-29 22:22:032014 if (contents == chrome::GetActiveWebContents(this)) {
[email protected]a5231662009-09-26 00:56:002015 // Updates that only matter when the tab is selected go here.
[email protected]f7f3a5f2009-05-01 22:02:342016
[email protected]d9083482012-01-06 00:38:462017 if (flags & content::INVALIDATE_TYPE_PAGE_ACTIONS) {
[email protected]9800de5e2011-03-11 18:19:492018 LocationBar* location_bar = window()->GetLocationBar();
2019 if (location_bar)
2020 location_bar->UpdatePageActions();
2021 }
[email protected]6ebdc9b2010-09-27 16:55:572022 // Updating the URL happens synchronously in ScheduleUIUpdate.
[email protected]d9083482012-01-06 00:38:462023 if (flags & content::INVALIDATE_TYPE_LOAD && GetStatusBubble()) {
[email protected]ece4aca2012-09-19 22:22:582024 GetStatusBubble()->SetStatus(
2025 CoreTabHelper::FromWebContents(chrome::GetActiveWebContents(this))->
2026 GetStatusText());
[email protected]0dd3a0ab2011-02-18 08:17:442027 }
[email protected]a5231662009-09-26 00:56:002028
[email protected]d9083482012-01-06 00:38:462029 if (flags & (content::INVALIDATE_TYPE_TAB |
2030 content::INVALIDATE_TYPE_TITLE)) {
[email protected]a5231662009-09-26 00:56:002031 window_->UpdateTitleBar();
2032 }
[email protected]4d34e2e2009-05-26 22:55:282033 }
[email protected]36b6dcb2008-11-12 01:19:572034
[email protected]a5231662009-09-26 00:56:002035 // Updates that don't depend upon the selected state go here.
[email protected]d9083482012-01-06 00:38:462036 if (flags &
2037 (content::INVALIDATE_TYPE_TAB | content::INVALIDATE_TYPE_TITLE)) {
[email protected]2fc15ae2012-05-06 00:01:372038 tab_strip_model_->UpdateTabContentsStateAt(
[email protected]e52d0a42012-06-08 22:44:162039 tab_strip_model_->GetIndexOfWebContents(contents),
[email protected]f1cd5e82009-10-23 17:19:032040 TabStripModelObserver::ALL);
[email protected]36b6dcb2008-11-12 01:19:572041 }
2042
2043 // We don't need to process INVALIDATE_STATE, since that's not visible.
2044 }
2045
2046 scheduled_updates_.clear();
2047}
2048
[email protected]d9083482012-01-06 00:38:462049void Browser::RemoveScheduledUpdatesFor(WebContents* contents) {
[email protected]36b6dcb2008-11-12 01:19:572050 if (!contents)
2051 return;
2052
[email protected]85354982009-09-25 19:58:402053 UpdateMap::iterator i = scheduled_updates_.find(contents);
2054 if (i != scheduled_updates_.end())
2055 scheduled_updates_.erase(i);
initial.commit09911bf2008-07-26 23:55:292056}
2057
[email protected]d938aed92009-01-22 19:49:332058
[email protected]36b6dcb2008-11-12 01:19:572059///////////////////////////////////////////////////////////////////////////////
2060// Browser, Getters for UI (private):
initial.commit09911bf2008-07-26 23:55:292061
[email protected]36b6dcb2008-11-12 01:19:572062StatusBubble* Browser::GetStatusBubble() {
[email protected]34930432009-11-09 00:12:092063 // In kiosk mode, we want to always hide the status bubble.
2064 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kKioskMode))
2065 return NULL;
[email protected]9b032bf2009-07-21 17:34:232066 return window_ ? window_->GetStatusBubble() : NULL;
initial.commit09911bf2008-07-26 23:55:292067}
2068
[email protected]36b6dcb2008-11-12 01:19:572069///////////////////////////////////////////////////////////////////////////////
2070// Browser, Session restore functions (private):
initial.commit09911bf2008-07-26 23:55:292071
2072void Browser::SyncHistoryWithTabs(int index) {
[email protected]92371eb2011-04-28 11:50:152073 SessionService* session_service =
2074 SessionServiceFactory::GetForProfileIfExisting(profile());
initial.commit09911bf2008-07-26 23:55:292075 if (session_service) {
2076 for (int i = index; i < tab_count(); ++i) {
[email protected]50a9a76b2012-09-12 14:19:362077 WebContents* web_contents = chrome::GetWebContentsAt(this, i);
2078 if (web_contents) {
2079 SessionTabHelper* session_tab_helper =
2080 SessionTabHelper::FromWebContents(web_contents);
initial.commit09911bf2008-07-26 23:55:292081 session_service->SetTabIndexInWindow(
[email protected]50a9a76b2012-09-12 14:19:362082 session_id(), session_tab_helper->session_id(), i);
[email protected]ecc523f62010-09-28 22:45:402083 session_service->SetPinnedState(
2084 session_id(),
[email protected]50a9a76b2012-09-12 14:19:362085 session_tab_helper->session_id(),
[email protected]52877dbc62012-06-29 22:22:032086 tab_strip_model_->IsTabPinned(i));
initial.commit09911bf2008-07-26 23:55:292087 }
2088 }
2089 }
2090}
2091
[email protected]36b6dcb2008-11-12 01:19:572092///////////////////////////////////////////////////////////////////////////////
[email protected]69444cc2009-04-09 20:40:062093// Browser, In-progress download termination handling (private):
2094
[email protected]b6a4ac2b2011-10-17 20:05:482095bool Browser::CanCloseWithInProgressDownloads() {
[email protected]5a17d492011-10-24 19:37:032096 // If we've prompted, we need to hear from the user before we
2097 // can close.
2098 if (cancel_download_confirmation_state_ != NOT_PROMPTED)
2099 return cancel_download_confirmation_state_ != WAITING_FOR_RESPONSE;
2100
2101 int num_downloads_blocking;
2102 if (DOWNLOAD_CLOSE_OK ==
2103 OkToCloseWithInProgressDownloads(&num_downloads_blocking))
[email protected]446295a2010-04-19 23:43:002104 return true;
2105
[email protected]5a17d492011-10-24 19:37:032106 // Closing this window will kill some downloads; prompt to make sure
2107 // that's ok.
[email protected]69444cc2009-04-09 20:40:062108 cancel_download_confirmation_state_ = WAITING_FOR_RESPONSE;
2109 window_->ConfirmBrowserCloseWithPendingDownloads();
2110
2111 // Return false so the browser does not close. We'll close if the user
2112 // confirms in the dialog.
2113 return false;
2114}
2115
2116///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:572117// Browser, Assorted utility functions (private):
initial.commit09911bf2008-07-26 23:55:292118
[email protected]36b6dcb2008-11-12 01:19:572119void Browser::CloseFrame() {
2120 window_->Close();
initial.commit09911bf2008-07-26 23:55:292121}
2122
[email protected]3d61dd02012-06-07 00:05:532123void Browser::TabDetachedAtImpl(TabContents* contents, int index,
[email protected]c37e3b62010-01-22 00:15:172124 DetachType type) {
2125 if (type == DETACH_TYPE_DETACH) {
[email protected]56580852010-11-17 16:09:152126 // Save the current location bar state, but only if the tab being detached
2127 // is the selected tab. Because saving state can conditionally revert the
2128 // location bar, saving the current tab's location bar state to a
2129 // non-selected tab can corrupt both tabs.
[email protected]52877dbc62012-06-29 22:22:032130 if (contents == chrome::GetActiveTabContents(this)) {
[email protected]9800de5e2011-03-11 18:19:492131 LocationBar* location_bar = window()->GetLocationBar();
2132 if (location_bar)
[email protected]83a2610a2012-01-05 01:00:272133 location_bar->SaveStateToContents(contents->web_contents());
[email protected]9800de5e2011-03-11 18:19:492134 }
[email protected]c37e3b62010-01-22 00:15:172135
[email protected]2fc15ae2012-05-06 00:01:372136 if (!tab_strip_model_->closing_all())
[email protected]c37e3b62010-01-22 00:15:172137 SyncHistoryWithTabs(0);
2138 }
2139
[email protected]6451fe92012-08-30 18:37:262140 SetAsDelegate(contents->web_contents(), NULL);
[email protected]d9083482012-01-06 00:38:462141 RemoveScheduledUpdatesFor(contents->web_contents());
[email protected]c37e3b62010-01-22 00:15:172142
[email protected]41f022e2012-01-31 05:56:402143 if (find_bar_controller_.get() && index == active_index()) {
[email protected]c37e3b62010-01-22 00:15:172144 find_bar_controller_->ChangeTabContents(NULL);
[email protected]ecc523f62010-09-28 22:45:402145 }
[email protected]c37e3b62010-01-22 00:15:172146
[email protected]e3a5d5b2012-06-22 17:56:462147 // Stop observing search model changes for this tab.
[email protected]0dc50db2012-09-19 17:57:142148 search_delegate_->OnTabDetached(contents->web_contents());
[email protected]e3a5d5b2012-06-22 17:56:462149
[email protected]432115822011-07-10 15:52:272150 registrar_.Remove(this, content::NOTIFICATION_INTERSTITIAL_ATTACHED,
[email protected]fbc5e5f92012-01-02 06:08:322151 content::Source<WebContents>(contents->web_contents()));
[email protected]e397a4472011-12-21 21:47:502152 registrar_.Remove(this, content::NOTIFICATION_INTERSTITIAL_DETACHED,
[email protected]4b19ea52012-01-02 20:15:252153 content::Source<WebContents>(contents->web_contents()));
[email protected]c37e3b62010-01-22 00:15:172154}
2155
[email protected]92655362012-04-20 21:33:172156bool Browser::SupportsWindowFeatureImpl(WindowFeature feature,
2157 bool check_fullscreen) const {
2158 // On Mac, fullscreen mode has most normal things (in a slide-down panel). On
2159 // other platforms, we hide some controls when in fullscreen mode.
2160 bool hide_ui_for_fullscreen = false;
2161#if !defined(OS_MACOSX)
2162 hide_ui_for_fullscreen = check_fullscreen && window_ &&
2163 window_->IsFullscreen();
2164#endif
2165
2166 unsigned int features = FEATURE_INFOBAR | FEATURE_DOWNLOADSHELF;
2167
2168 if (is_type_tabbed())
2169 features |= FEATURE_BOOKMARKBAR;
2170
2171 if (!hide_ui_for_fullscreen) {
2172 if (!is_type_tabbed())
2173 features |= FEATURE_TITLEBAR;
2174
2175 if (is_type_tabbed())
2176 features |= FEATURE_TABSTRIP;
2177
2178 if (is_type_tabbed())
2179 features |= FEATURE_TOOLBAR;
2180
2181 if (!is_app())
2182 features |= FEATURE_LOCATIONBAR;
2183 }
2184 return !!(features & feature);
2185}
2186
[email protected]09b29342011-06-24 19:18:482187void Browser::UpdateBookmarkBarState(BookmarkBarStateChangeReason reason) {
2188 BookmarkBar::State state;
[email protected]227393e2011-06-29 21:28:392189 // The bookmark bar is hidden in fullscreen mode, unless on the new tab page.
[email protected]3710d0002011-10-11 00:35:002190 if (browser_defaults::bookmarks_enabled &&
2191 profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar) &&
[email protected]227393e2011-06-29 21:28:392192 (!window_ || !window_->IsFullscreen())) {
[email protected]09b29342011-06-24 19:18:482193 state = BookmarkBar::SHOW;
2194 } else {
[email protected]1c5119c2012-09-19 00:08:572195 WebContents* web_contents = chrome::GetActiveWebContents(this);
2196 BookmarkTabHelper* bookmark_tab_helper =
2197 web_contents ? BookmarkTabHelper::FromWebContents(web_contents) : NULL;
2198 if (bookmark_tab_helper && bookmark_tab_helper->ShouldShowBookmarkBar())
[email protected]09b29342011-06-24 19:18:482199 state = BookmarkBar::DETACHED;
2200 else
2201 state = BookmarkBar::HIDDEN;
2202 }
[email protected]2d4e43a02012-06-26 00:11:552203
2204 // Only allow the bookmark bar to be shown in default mode.
2205 if (!search_model_->mode().is_default())
2206 state = BookmarkBar::HIDDEN;
2207
[email protected]09b29342011-06-24 19:18:482208 if (state == bookmark_bar_state_)
2209 return;
2210
2211 bookmark_bar_state_ = state;
2212
2213 if (!window_)
2214 return; // This is called from the constructor when window_ is NULL.
2215
2216 if (reason == BOOKMARK_BAR_STATE_CHANGE_TAB_SWITCH) {
2217 // Don't notify BrowserWindow on a tab switch as at the time this is invoked
2218 // BrowserWindow hasn't yet switched tabs. The BrowserWindow implementations
2219 // end up querying state once they process the tab switch.
2220 return;
2221 }
2222
2223 BookmarkBar::AnimateChangeType animate_type =
2224 (reason == BOOKMARK_BAR_STATE_CHANGE_PREF_CHANGE) ?
2225 BookmarkBar::ANIMATE_STATE_CHANGE :
2226 BookmarkBar::DONT_ANIMATE_STATE_CHANGE;
2227 window_->BookmarkBarStateChanged(animate_type);
2228}
[email protected]2e2cacc2011-07-12 21:54:262229
[email protected]92655362012-04-20 21:33:172230bool Browser::MaybeCreateBackgroundContents(int route_id,
2231 WebContents* opener_web_contents,
2232 const string16& frame_name,
2233 const GURL& target_url) {
2234 GURL opener_url = opener_web_contents->GetURL();
2235 ExtensionService* extensions_service = profile_->GetExtensionService();
2236
2237 if (!opener_url.is_valid() ||
2238 frame_name.empty() ||
2239 !extensions_service ||
2240 !extensions_service->is_ready())
2241 return false;
2242
2243 // Only hosted apps have web extents, so this ensures that only hosted apps
2244 // can create BackgroundContents. We don't have to check for background
2245 // permission as that is checked in RenderMessageFilter when the CreateWindow
2246 // message is processed.
2247 const Extension* extension =
2248 extensions_service->extensions()->GetHostedAppByURL(
2249 ExtensionURLInfo(opener_url));
2250 if (!extension)
2251 return false;
2252
2253 // No BackgroundContents allowed if BackgroundContentsService doesn't exist.
2254 BackgroundContentsService* service =
2255 BackgroundContentsServiceFactory::GetForProfile(profile_);
2256 if (!service)
2257 return false;
2258
2259 // Ensure that we're trying to open this from the extension's process.
2260 SiteInstance* opener_site_instance = opener_web_contents->GetSiteInstance();
2261 extensions::ProcessMap* process_map = extensions_service->process_map();
2262 if (!opener_site_instance->GetProcess() ||
2263 !process_map->Contains(
2264 extension->id(), opener_site_instance->GetProcess()->GetID())) {
2265 return false;
2266 }
2267
2268 // Only allow a single background contents per app.
2269 bool allow_js_access = extension->allow_background_js_access();
2270 BackgroundContents* existing =
2271 service->GetAppBackgroundContents(ASCIIToUTF16(extension->id()));
2272 if (existing) {
2273 // For non-scriptable background contents, ignore the request altogether,
2274 // (returning true, so that a regular WebContents isn't created either).
2275 if (!allow_js_access)
2276 return true;
2277 // For scriptable background pages, if one already exists, close it (even
2278 // if it was specified in the manifest).
2279 DLOG(INFO) << "Closing existing BackgroundContents for " << opener_url;
2280 delete existing;
2281 }
2282
2283 // If script access is not allowed, create the the background contents in a
2284 // new SiteInstance, so that a separate process is used.
2285 scoped_refptr<content::SiteInstance> site_instance =
2286 allow_js_access ?
2287 opener_site_instance :
2288 content::SiteInstance::Create(opener_web_contents->GetBrowserContext());
2289
2290 // Passed all the checks, so this should be created as a BackgroundContents.
2291 BackgroundContents* contents = service->CreateBackgroundContents(
2292 site_instance,
2293 route_id,
2294 profile_,
2295 frame_name,
2296 ASCIIToUTF16(extension->id()));
2297
2298 // When a separate process is used, the original renderer cannot access the
2299 // new window later, thus we need to navigate the window now.
2300 if (contents && !allow_js_access) {
2301 contents->web_contents()->GetController().LoadURL(
2302 target_url,
2303 content::Referrer(),
2304 content::PAGE_TRANSITION_LINK,
2305 std::string()); // No extra headers.
2306 }
2307
2308 return contents != NULL;
[email protected]afcb43562011-09-02 23:33:382309}