blob: ee31fec1940198d8d8173d161f80f3cbb621d2f0 [file] [log] [blame]
[email protected]e41982a72012-11-20 07:16:511// Copyright 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]9c55de82012-11-01 21:03:5210#endif // defined(OS_WIN)
[email protected]aca02cf2010-05-03 18:56:0111
[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]835d7c82010-10-14 04:38:3819#include "base/metrics/histogram.h"
[email protected]b02d0382009-11-30 21:19:5020#include "base/path_service.h"
[email protected]3853a4c2013-02-11 17:15:5721#include "base/prefs/pref_service.h"
[email protected]b5febf72012-07-29 18:10:0922#include "base/process_info.h"
[email protected]b6a4ac2b2011-10-17 20:05:4823#include "base/string_util.h"
[email protected]5a17d492011-10-24 19:37:0324#include "base/stringprintf.h"
[email protected]3ea1b182013-02-08 22:38:4125#include "base/strings/string_number_conversions.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]8e837ec2013-01-31 01:48:3331#include "chrome/browser/app_mode/app_mode_utils.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"
[email protected]8a39b472013-04-04 17:07:4244#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
[email protected]7d189022011-08-25 22:54:2045#include "chrome/browser/custom_handlers/register_protocol_handler_infobar_delegate.h"
[email protected]70019152012-12-19 11:44:1946#include "chrome/browser/devtools/devtools_toggle_action.h"
47#include "chrome/browser/devtools/devtools_window.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]9c1662b2012-03-06 15:44:3352#include "chrome/browser/extensions/browser_extension_window_controller.h"
[email protected]10abd192010-09-30 02:03:4953#include "chrome/browser/extensions/extension_prefs.h"
[email protected]eaa7dd182010-12-14 11:09:0054#include "chrome/browser/extensions/extension_service.h"
[email protected]06bdd2b2012-11-30 18:47:1355#include "chrome/browser/extensions/extension_system.h"
[email protected]a6394ae2012-07-16 20:58:4356#include "chrome/browser/extensions/tab_helper.h"
[email protected]b375c5d2011-05-03 21:15:0457#include "chrome/browser/favicon/favicon_tab_helper.h"
[email protected]3a29a6e2011-08-24 18:26:2158#include "chrome/browser/file_select_helper.h"
[email protected]820735792010-07-29 23:40:0159#include "chrome/browser/first_run/first_run.h"
[email protected]f7578f52010-08-30 22:22:4960#include "chrome/browser/google/google_url_tracker.h"
[email protected]4a8adfa02013-03-19 22:37:4661#include "chrome/browser/infobars/infobar_service.h"
62#include "chrome/browser/infobars/simple_alert_infobar_delegate.h"
[email protected]2e6389f2012-05-18 19:41:2563#include "chrome/browser/lifetime/application_lifetime.h"
[email protected]b8c4d5d2013-03-24 03:12:2864#include "chrome/browser/media/media_capture_devices_dispatcher.h"
[email protected]a239c3f2009-02-17 22:13:1965#include "chrome/browser/net/url_fixer_upper.h"
[email protected]2c4fb7b2011-04-02 06:33:2966#include "chrome/browser/notifications/notification_ui_manager.h"
[email protected]8c8fc292012-11-23 18:57:1667#include "chrome/browser/pepper_broker_infobar_delegate.h"
[email protected]4e94ab32011-08-05 05:28:2768#include "chrome/browser/prefs/incognito_mode_prefs.h"
[email protected]02337592010-09-27 18:38:2569#include "chrome/browser/printing/cloud_print/cloud_print_setup_flow.h"
[email protected]8ecad5e2010-12-02 21:18:3370#include "chrome/browser/profiles/profile.h"
[email protected]5e919242012-02-13 23:59:3571#include "chrome/browser/profiles/profile_destroyer.h"
[email protected]5a17d492011-10-24 19:37:0372#include "chrome/browser/profiles/profile_manager.h"
[email protected]a7be6982011-12-12 21:53:5773#include "chrome/browser/profiles/profile_metrics.h"
[email protected]67baffc82011-12-19 18:03:0774#include "chrome/browser/repost_form_warning_controller.h"
[email protected]a7b8e43d2013-03-18 18:52:4375#include "chrome/browser/search/search.h"
[email protected]85e921fb82009-02-11 23:19:4476#include "chrome/browser/sessions/session_service.h"
[email protected]92371eb2011-04-28 11:50:1577#include "chrome/browser/sessions/session_service_factory.h"
[email protected]bde40b822012-08-30 17:28:5678#include "chrome/browser/sessions/session_tab_helper.h"
[email protected]c61db1d2009-02-06 03:39:1879#include "chrome/browser/sessions/session_types.h"
[email protected]92926d92010-09-02 18:35:0680#include "chrome/browser/sessions/tab_restore_service.h"
[email protected]92371eb2011-04-28 11:50:1581#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]066629f2009-10-17 00:28:1382#include "chrome/browser/sync/profile_sync_service.h"
[email protected]5e61afb92012-01-27 20:09:1383#include "chrome/browser/sync/profile_sync_service_factory.h"
[email protected]40f047972009-11-25 03:54:4084#include "chrome/browser/sync/sync_ui_util.h"
[email protected]2c4fb7b2011-04-02 06:33:2985#include "chrome/browser/tab_contents/background_contents.h"
[email protected]4fdf6742012-01-10 20:14:3686#include "chrome/browser/tab_contents/retargeting_details.h"
[email protected]1ab4ddf2011-07-21 04:48:0487#include "chrome/browser/tab_contents/tab_util.h"
[email protected]ffa6f592011-06-24 22:03:5788#include "chrome/browser/themes/theme_service.h"
89#include "chrome/browser/themes/theme_service_factory.h"
[email protected]71a88bb2013-02-01 22:05:1590#include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_manager.h"
[email protected]27e5b3912012-12-16 00:45:3791#include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h"
[email protected]35699d572011-05-11 19:46:1492#include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
[email protected]5d98294912012-06-27 22:57:4093#include "chrome/browser/ui/browser_command_controller.h"
[email protected]a37d4b02012-06-25 21:56:1094#include "chrome/browser/ui/browser_commands.h"
[email protected]ef557022012-03-16 10:05:3395#include "chrome/browser/ui/browser_content_setting_bubble_model_delegate.h"
[email protected]55452902011-06-01 21:57:4796#include "chrome/browser/ui/browser_dialogs.h"
[email protected]d8748142012-05-16 21:13:4397#include "chrome/browser/ui/browser_finder.h"
[email protected]7acfaf92012-07-11 15:51:5998#include "chrome/browser/ui/browser_instant_controller.h"
[email protected]3539929f2013-02-01 05:59:1499#include "chrome/browser/ui/browser_iterator.h"
[email protected]6768ac02011-04-06 17:41:04100#include "chrome/browser/ui/browser_list.h"
[email protected]fcca741b2011-06-17 22:46:37101#include "chrome/browser/ui/browser_navigator.h"
[email protected]27e5b3912012-12-16 00:45:37102#include "chrome/browser/ui/browser_tab_contents.h"
[email protected]fb7da4132011-03-15 15:51:00103#include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
[email protected]855370052012-07-10 19:30:32104#include "chrome/browser/ui/browser_tab_strip_model_delegate.h"
[email protected]52877dbc62012-06-29 22:22:03105#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]ef557022012-03-16 10:05:33106#include "chrome/browser/ui/browser_toolbar_model_delegate.h"
[email protected]2cd4fde2012-06-26 03:10:26107#include "chrome/browser/ui/browser_ui_prefs.h"
[email protected]00070c732011-04-09 15:31:33108#include "chrome/browser/ui/browser_window.h"
[email protected]5d9cace72012-06-21 16:07:12109#include "chrome/browser/ui/chrome_pages.h"
[email protected]6e1fcd12012-07-02 17:14:20110#include "chrome/browser/ui/chrome_select_file_policy.h"
[email protected]a44657202012-01-09 05:48:31111#include "chrome/browser/ui/extensions/shell_window.h"
[email protected]45300ad42010-12-02 15:51:14112#include "chrome/browser/ui/find_bar/find_bar.h"
113#include "chrome/browser/ui/find_bar/find_bar_controller.h"
[email protected]c90c6ca2011-02-16 20:11:38114#include "chrome/browser/ui/find_bar/find_tab_helper.h"
[email protected]9d5c059a82012-06-29 20:13:07115#include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
[email protected]1ba55cf2012-06-29 19:11:39116#include "chrome/browser/ui/global_error/global_error.h"
117#include "chrome/browser/ui/global_error/global_error_service.h"
118#include "chrome/browser/ui/global_error/global_error_service_factory.h"
[email protected]6a3ec2312010-12-02 19:30:19119#include "chrome/browser/ui/omnibox/location_bar.h"
[email protected]e3a5d5b2012-06-22 17:56:46120#include "chrome/browser/ui/search/search_delegate.h"
121#include "chrome/browser/ui/search/search_model.h"
[email protected]27e5b3912012-12-16 00:45:37122#include "chrome/browser/ui/search_engines/search_engine_tab_helper.h"
[email protected]5d9cace72012-06-21 16:07:12123#include "chrome/browser/ui/singleton_tabs.h"
[email protected]6a3ec2312010-12-02 19:30:19124#include "chrome/browser/ui/status_bubble.h"
[email protected]3da95a102011-11-30 21:47:45125#include "chrome/browser/ui/sync/browser_synced_window_delegate.h"
[email protected]51d83bdf2011-12-09 16:09:29126#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
[email protected]51296c62012-09-26 13:02:44127#include "chrome/browser/ui/tab_modal_confirm_dialog.h"
[email protected]9a800652010-12-02 17:08:44128#include "chrome/browser/ui/tabs/dock_info.h"
[email protected]6a3ec2312010-12-02 19:30:19129#include "chrome/browser/ui/tabs/tab_menu_model.h"
[email protected]b56e2e32012-05-11 21:18:04130#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]736d00322012-10-11 21:32:41131#include "chrome/browser/ui/toolbar/toolbar_model_impl.h"
[email protected]f9b6c092012-07-09 19:32:28132#include "chrome/browser/ui/unload_controller.h"
[email protected]f847e6082011-03-24 00:08:26133#include "chrome/browser/ui/web_applications/web_app_ui.h"
[email protected]65549182012-02-25 00:45:40134#include "chrome/browser/ui/webui/signin/login_ui_service.h"
135#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
[email protected]03639422012-06-28 19:03:37136#include "chrome/browser/ui/window_sizer/window_sizer.h"
[email protected]27e5b3912012-12-16 00:45:37137#include "chrome/browser/ui/zoom/zoom_controller.h"
[email protected]b1b73942010-05-26 20:11:54138#include "chrome/browser/upgrade_detector.h"
[email protected]86b54012009-11-19 09:18:50139#include "chrome/browser/web_applications/web_app.h"
[email protected]d938aed92009-01-22 19:49:33140#include "chrome/common/chrome_constants.h"
[email protected]432115822011-07-10 15:52:27141#include "chrome/common/chrome_notification_types.h"
[email protected]d938aed92009-01-22 19:49:33142#include "chrome/common/chrome_switches.h"
[email protected]7d189022011-08-25 22:54:20143#include "chrome/common/custom_handlers/protocol_handler.h"
[email protected]9367eabc2013-03-01 01:29:29144#include "chrome/common/extensions/background_info.h"
[email protected]5b1a0e22009-05-26 19:00:58145#include "chrome/common/extensions/extension.h"
[email protected]46fd1ea42011-02-16 15:59:33146#include "chrome/common/extensions/extension_constants.h"
[email protected]8a53ee042009-01-21 16:41:33147#include "chrome/common/pref_names.h"
[email protected]7664ab32011-02-01 23:35:25148#include "chrome/common/profiling.h"
[email protected]c3917742012-11-21 01:51:14149#include "chrome/common/search_types.h"
[email protected]b5febf72012-07-29 18:10:09150#include "chrome/common/startup_metric_utils.h"
[email protected]c744f7d22013-04-12 20:29:04151#include "chrome/common/url_constants.h"
[email protected]e41d0082013-05-16 04:37:54152#include "components/web_modal/web_contents_modal_dialog_manager.h"
[email protected]da8543762012-03-20 08:52:20153#include "content/public/browser/color_chooser.h"
[email protected]0e12d7d2011-12-01 16:21:44154#include "content/public/browser/devtools_manager.h"
[email protected]e582fdd2011-12-20 16:48:17155#include "content/public/browser/download_item.h"
156#include "content/public/browser/download_manager.h"
[email protected]cadaec52012-02-08 21:53:13157#include "content/public/browser/interstitial_page.h"
[email protected]d9083482012-01-06 00:38:46158#include "content/public/browser/invalidate_type.h"
[email protected]cdcb1dee2012-01-04 00:46:20159#include "content/public/browser/navigation_controller.h"
[email protected]022af742011-12-28 18:37:25160#include "content/public/browser/navigation_entry.h"
[email protected]7f6f44c2011-12-14 13:23:38161#include "content/public/browser/notification_details.h"
162#include "content/public/browser/notification_service.h"
[email protected]3a5180ae2011-12-21 02:39:38163#include "content/public/browser/plugin_service.h"
[email protected]9c1662b2012-03-06 15:44:33164#include "content/public/browser/render_process_host.h"
165#include "content/public/browser/render_view_host.h"
[email protected]b6583592012-01-25 19:52:33166#include "content/public/browser/site_instance.h"
[email protected]7f6f44c2011-12-14 13:23:38167#include "content/public/browser/user_metrics.h"
[email protected]d9083482012-01-06 00:38:46168#include "content/public/browser/web_contents.h"
[email protected]8643e6d2012-01-18 20:26:10169#include "content/public/browser/web_contents_view.h"
[email protected]4573fbd2011-10-31 20:25:18170#include "content/public/common/content_restriction.h"
[email protected]c08950d22011-10-13 22:20:29171#include "content/public/common/content_switches.h"
[email protected]0f083402011-11-22 02:59:01172#include "content/public/common/page_zoom.h"
[email protected]cadaec52012-02-08 21:53:13173#include "content/public/common/renderer_preferences.h"
[email protected]885c0e92012-11-13 20:27:42174#include "extensions/common/constants.h"
[email protected]a3e18c42009-03-04 23:36:05175#include "grit/chromium_strings.h"
176#include "grit/generated_resources.h"
177#include "grit/locale_settings.h"
[email protected]2a2813352012-07-11 22:20:23178#include "grit/theme_resources.h"
[email protected]8a53ee042009-01-21 16:41:33179#include "net/base/net_util.h"
[email protected]be28b5f42012-07-20 11:31:25180#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]4d7c4ef2012-03-16 01:47:12181#include "net/cookies/cookie_monster.h"
[email protected]8a53ee042009-01-21 16:41:33182#include "net/url_request/url_request_context.h"
[email protected]c051a1b2011-01-21 23:30:17183#include "ui/base/l10n/l10n_util.h"
[email protected]f47621b2013-01-22 20:50:33184#include "ui/base/window_open_disposition.h"
[email protected]08397d52011-02-05 01:53:38185#include "ui/gfx/point.h"
[email protected]4344a3c2013-01-17 23:49:20186#include "ui/shell_dialogs/selected_file_info.h"
[email protected]4a19be92011-09-22 14:25:02187#include "webkit/glue/webkit_glue.h"
[email protected]3a5180ae2011-12-21 02:39:38188#include "webkit/plugins/webplugininfo.h"
[email protected]8a53ee042009-01-21 16:41:33189
190#if defined(OS_WIN)
[email protected]77cbe50e2012-06-14 02:44:38191#include "base/win/metro.h"
[email protected]12f520c2010-01-06 18:11:15192#include "chrome/browser/shell_integration.h"
[email protected]b17d41c2011-02-17 22:09:59193#include "chrome/browser/ssl/ssl_error_info.h"
[email protected]a5d1e1e2010-09-23 19:34:12194#include "chrome/browser/task_manager/task_manager.h"
[email protected]6a3ec2312010-12-02 19:30:19195#include "chrome/browser/ui/view_ids.h"
[email protected]e4b2fa32013-03-09 22:56:56196#include "components/autofill/browser/autofill_ie_toolbar_import_win.h"
[email protected]dcd57762011-06-25 12:18:51197#include "ui/base/win/shell.h"
[email protected]5b55e1e92013-05-08 21:00:29198#include "ui/views/win/hwnd_util.h"
[email protected]8a53ee042009-01-21 16:41:33199#endif // OS_WIN
[email protected]e1acf6f2008-10-27 20:43:33200
[email protected]b7969202010-05-14 21:29:26201#if defined(OS_CHROMEOS)
[email protected]4fa2fd5d2013-04-26 03:42:52202#include "chrome/browser/chromeos/drive/file_system_util.h"
[email protected]eab8c0f2011-11-18 22:33:50203#endif
204
[email protected]dc04be7c2012-03-15 23:57:49205#if defined(USE_ASH)
[email protected]bafda132012-02-16 19:32:51206#include "ash/ash_switches.h"
[email protected]c2f0a892012-01-24 22:19:21207#endif
208
[email protected]8a53ee042009-01-21 16:41:33209using base::TimeDelta;
[email protected]b87ee522012-05-18 15:16:54210using content::NativeWebKeyboardEvent;
[email protected]c5eed492012-01-04 17:07:50211using content::NavigationController;
[email protected]10f417c52011-12-28 21:04:23212using content::NavigationEntry;
[email protected]e5d549d2011-12-28 01:29:20213using content::OpenURLParams;
[email protected]3a5180ae2011-12-21 02:39:38214using content::PluginService;
[email protected]e5d549d2011-12-28 01:29:20215using content::Referrer;
[email protected]b6583592012-01-25 19:52:33216using content::SiteInstance;
[email protected]7f6f44c2011-12-14 13:23:38217using content::UserMetricsAction;
[email protected]ea049a02011-12-25 21:37:09218using content::WebContents;
[email protected]1c321ee52012-05-21 03:02:34219using extensions::Extension;
[email protected]20c07f8e2012-05-31 08:43:14220using ui::WebDialogDelegate;
[email protected]e41d0082013-05-16 04:37:54221using web_modal::WebContentsModalDialogManager;
initial.commit09911bf2008-07-26 23:55:29222
[email protected]36b6dcb2008-11-12 01:19:57223///////////////////////////////////////////////////////////////////////////////
initial.commit09911bf2008-07-26 23:55:29224
[email protected]505323e22009-01-24 02:47:58225namespace {
226
[email protected]91a79232010-11-10 18:15:08227// The URL to be loaded to display the "Report a broken page" form.
228const char kBrokenPageUrl[] =
[email protected]95fc6f92011-04-13 00:37:20229 "https://www.google.com/support/chrome/bin/request.py?contact_type="
[email protected]91a79232010-11-10 18:15:08230 "broken_website&format=inproduct&p.page_title=$1&p.page_url=$2";
231
[email protected]91a79232010-11-10 18:15:08232// How long we wait before updating the browser chrome while loading a page.
233const int kUIUpdateCoalescingTimeMS = 200;
234
[email protected]a4fe67012012-07-25 20:14:29235BrowserWindow* CreateBrowserWindow(Browser* browser) {
[email protected]a4fe67012012-07-25 20:14:29236 return BrowserWindow::CreateBrowserWindow(browser);
237}
238
[email protected]505323e22009-01-24 02:47:58239} // namespace
240
[email protected]b35b26b32011-05-05 20:35:14241////////////////////////////////////////////////////////////////////////////////
242// Browser, CreateParams:
243
[email protected]196578252012-12-06 23:54:20244Browser::CreateParams::CreateParams(Profile* profile,
245 chrome::HostDesktopType host_desktop_type)
246 : type(TYPE_TABBED),
247 profile(profile),
248 host_desktop_type(host_desktop_type),
249 app_type(APP_TYPE_HOST),
250 initial_show_state(ui::SHOW_STATE_DEFAULT),
251 is_session_restore(false),
252 window(NULL) {
253}
254
[email protected]56073dc2012-08-10 16:59:06255Browser::CreateParams::CreateParams(Type type,
256 Profile* profile,
257 chrome::HostDesktopType host_desktop_type)
258 : type(type),
259 profile(profile),
260 host_desktop_type(host_desktop_type),
[email protected]0da58292012-03-22 20:37:21261 app_type(APP_TYPE_HOST),
[email protected]a636d8e52012-02-28 15:40:41262 initial_show_state(ui::SHOW_STATE_DEFAULT),
[email protected]a4fe67012012-07-25 20:14:29263 is_session_restore(false),
264 window(NULL) {
[email protected]b35b26b32011-05-05 20:35:14265}
266
[email protected]da22aa62012-04-04 18:54:35267// static
268Browser::CreateParams Browser::CreateParams::CreateForApp(
269 Type type,
270 const std::string& app_name,
271 const gfx::Rect& window_bounds,
[email protected]2b09ae552013-02-23 06:38:03272 Profile* profile,
273 chrome::HostDesktopType host_desktop_type) {
[email protected]da22aa62012-04-04 18:54:35274 DCHECK(type != TYPE_TABBED);
275 DCHECK(!app_name.empty());
276
[email protected]2b09ae552013-02-23 06:38:03277 CreateParams params(type, profile, host_desktop_type);
[email protected]da22aa62012-04-04 18:54:35278 params.app_name = app_name;
279 params.app_type = APP_TYPE_CHILD;
280 params.initial_bounds = window_bounds;
281
282 return params;
283}
284
285// static
286Browser::CreateParams Browser::CreateParams::CreateForDevTools(
[email protected]2b09ae552013-02-23 06:38:03287 Profile* profile,
288 chrome::HostDesktopType host_desktop_type) {
289 CreateParams params(TYPE_POPUP, profile, host_desktop_type);
[email protected]da22aa62012-04-04 18:54:35290 params.app_name = DevToolsWindow::kDevToolsApp;
291 return params;
292}
293
[email protected]20ca0382013-02-28 19:50:07294////////////////////////////////////////////////////////////////////////////////
295// Browser, InterstitialObserver:
296
297class Browser::InterstitialObserver : public content::WebContentsObserver {
298 public:
299 InterstitialObserver(Browser* browser, content::WebContents* web_contents)
300 : WebContentsObserver(web_contents),
301 browser_(browser) {
302 }
303
304 using content::WebContentsObserver::web_contents;
305
306 virtual void DidAttachInterstitialPage() OVERRIDE {
307 browser_->UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
308 }
309
310 virtual void DidDetachInterstitialPage() OVERRIDE {
311 browser_->UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
312 }
313
314 private:
315 Browser* browser_;
316
317 DISALLOW_COPY_AND_ASSIGN(InterstitialObserver);
318};
319
[email protected]36b6dcb2008-11-12 01:19:57320///////////////////////////////////////////////////////////////////////////////
321// Browser, Constructors, Creation, Showing:
initial.commit09911bf2008-07-26 23:55:29322
[email protected]a4fe67012012-07-25 20:14:29323Browser::Browser(const CreateParams& params)
324 : type_(params.type),
325 profile_(params.profile),
[email protected]f3e99e32008-07-30 04:48:39326 window_(NULL),
[email protected]d4b2d232013-04-30 21:14:23327 tab_strip_model_delegate_(new chrome::BrowserTabStripModelDelegate(this)),
[email protected]fa9d49ff2012-10-24 22:44:37328 tab_strip_model_(new TabStripModel(tab_strip_model_delegate_.get(),
329 params.profile)),
[email protected]a4fe67012012-07-25 20:14:29330 app_name_(params.app_name),
331 app_type_(params.app_type),
[email protected]15952e462008-11-14 00:29:05332 chrome_updater_factory_(this),
[email protected]69444cc2009-04-09 20:40:06333 cancel_download_confirmation_state_(NOT_PROMPTED),
[email protected]a4fe67012012-07-25 20:14:29334 override_bounds_(params.initial_bounds),
335 initial_show_state_(params.initial_show_state),
336 is_session_restore_(params.is_session_restore),
[email protected]56073dc2012-08-10 16:59:06337 host_desktop_type_(params.host_desktop_type),
[email protected]d4b2d232013-04-30 21:14:23338 unload_controller_(new chrome::UnloadController(this)),
[email protected]24f1477532011-11-22 22:55:36339 weak_factory_(this),
[email protected]d4b2d232013-04-30 21:14:23340 content_setting_bubble_model_delegate_(
341 new BrowserContentSettingBubbleModelDelegate(this)),
342 toolbar_model_delegate_(new BrowserToolbarModelDelegate(this)),
343 tab_restore_service_delegate_(new BrowserTabRestoreServiceDelegate(this)),
344 synced_window_delegate_(new BrowserSyncedWindowDelegate(this)),
[email protected]8a5e0ca2011-08-25 06:30:47345 bookmark_bar_state_(BookmarkBar::HIDDEN),
[email protected]d4b2d232013-04-30 21:14:23346 command_controller_(new chrome::BrowserCommandController(
347 this, g_browser_process->profile_manager())),
[email protected]2f516c792011-09-19 22:22:09348 window_has_shown_(false) {
[email protected]a4fe67012012-07-25 20:14:29349 if (!app_name_.empty())
350 chrome::RegisterAppPrefs(app_name_, profile_);
[email protected]2fc15ae2012-05-06 00:01:37351 tab_strip_model_->AddObserver(this);
352
[email protected]736d00322012-10-11 21:32:41353 toolbar_model_.reset(new ToolbarModelImpl(toolbar_model_delegate_.get()));
[email protected]165fe422013-03-27 06:34:03354 search_model_.reset(new SearchModel());
[email protected]e3a5d5b2012-06-22 17:56:46355 search_delegate_.reset(
[email protected]165fe422013-03-27 06:34:03356 new SearchDelegate(search_model_.get(), toolbar_model_.get()));
[email protected]ef557022012-03-16 10:05:33357
[email protected]432115822011-07-10 15:52:27358 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
[email protected]a93089b12011-11-22 20:47:38359 content::Source<Profile>(profile_->GetOriginalProfile()));
[email protected]432115822011-07-10 15:52:27360 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]a93089b12011-11-22 20:47:38361 content::Source<Profile>(profile_->GetOriginalProfile()));
[email protected]432115822011-07-10 15:52:27362 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]a93089b12011-11-22 20:47:38363 content::Source<Profile>(profile_->GetOriginalProfile()));
[email protected]432115822011-07-10 15:52:27364 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07365 content::NotificationService::AllSources());
[email protected]6b40bb582012-03-15 20:50:38366#if defined(ENABLE_THEMES)
[email protected]ffa6f592011-06-24 22:03:57367 registrar_.Add(
[email protected]432115822011-07-10 15:52:27368 this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
[email protected]6c2381d2011-10-19 02:52:53369 content::Source<ThemeService>(
370 ThemeServiceFactory::GetForProfile(profile_)));
[email protected]6b40bb582012-03-15 20:50:38371#endif
[email protected]ea049a02011-12-25 21:37:09372 registrar_.Add(this, chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
[email protected]ad50def52011-10-19 23:17:07373 content::NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29374
[email protected]97b46382011-05-02 13:23:04375 profile_pref_registrar_.Init(profile_->GetPrefs());
[email protected]c31813a2012-11-15 18:47:26376 profile_pref_registrar_.Add(
377 prefs::kDevToolsDisabled,
378 base::Bind(&Browser::OnDevToolsDisabledChanged, base::Unretained(this)));
379 profile_pref_registrar_.Add(
380 prefs::kShowBookmarkBar,
381 base::Bind(&Browser::UpdateBookmarkBarState, base::Unretained(this),
382 BOOKMARK_BAR_STATE_CHANGE_PREF_CHANGE));
383 profile_pref_registrar_.Add(
384 prefs::kHomePage,
385 base::Bind(&Browser::MarkHomePageAsChanged, base::Unretained(this)));
[email protected]9e823662010-10-13 23:36:00386
initial.commit09911bf2008-07-26 23:55:29387 BrowserList::AddBrowser(this);
388
[email protected]97b46382011-05-02 13:23:04389 // NOTE: These prefs all need to be explicitly destroyed in the destructor
390 // or you'll get a nasty surprise when you run the incognito tests.
initial.commit09911bf2008-07-26 23:55:29391 encoding_auto_detect_.Init(prefs::kWebKitUsesUniversalDetector,
[email protected]96a5c342012-12-04 18:14:02392 profile_->GetPrefs());
[email protected]281918912010-05-27 22:05:13393
[email protected]e41982a72012-11-20 07:16:51394 if (is_type_tabbed())
[email protected]165fe422013-03-27 06:34:03395 instant_controller_.reset(new BrowserInstantController(this));
[email protected]df25eb9f2012-09-13 17:43:23396
[email protected]09b29342011-06-24 19:18:48397 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_INIT);
[email protected]a7be6982011-12-12 21:53:57398
[email protected]26e92df2013-04-11 16:05:21399 ProfileMetrics::LogProfileLaunch(profile_);
[email protected]a4fe67012012-07-25 20:14:29400
401 window_ = params.window ? params.window : CreateBrowserWindow(this);
402
403 // TODO(beng): move to BrowserFrameWin.
[email protected]5b55e1e92013-05-08 21:00:29404#if defined(OS_WIN)
[email protected]a4fe67012012-07-25 20:14:29405 // Set the app user model id for this application to that of the application
406 // name. See http://crbug.com/7028.
407 ui::win::SetAppIdForWindow(
[email protected]7efaed362013-04-04 09:33:32408 is_app() ?
[email protected]47e870b2013-02-24 21:14:53409 ShellIntegration::GetAppModelIdForProfile(UTF8ToWide(app_name_),
[email protected]a4fe67012012-07-25 20:14:29410 profile_->GetPath()) :
411 ShellIntegration::GetChromiumModelIdForProfile(profile_->GetPath()),
[email protected]5b55e1e92013-05-08 21:00:29412 views::HWNDForNativeWindow(window()->GetNativeWindow()));
[email protected]a4fe67012012-07-25 20:14:29413#endif
414
415 // Create the extension window controller before sending notifications.
416 extension_window_controller_.reset(
417 new BrowserExtensionWindowController(this));
418
419 // TODO(beng): Move BrowserList::AddBrowser() to the end of this function and
420 // replace uses of this with BL's notifications.
421 content::NotificationService::current()->Notify(
422 chrome::NOTIFICATION_BROWSER_WINDOW_READY,
423 content::Source<Browser>(this),
424 content::NotificationService::NoDetails());
425
426 // TODO(beng): move to ChromeBrowserMain:
[email protected]dfcc05f2013-02-21 14:12:16427 if (first_run::ShouldDoPersonalDataManagerFirstRun()) {
[email protected]a4fe67012012-07-25 20:14:29428#if defined(OS_WIN)
[email protected]dfcc05f2013-02-21 14:12:16429 // Notify PDM that this is a first run.
[email protected]bfd4b812013-04-11 02:02:11430 ImportAutofillDataWin(
431 autofill::PersonalDataManagerFactory::GetForProfile(profile_));
[email protected]a4fe67012012-07-25 20:14:29432#endif // defined(OS_WIN)
[email protected]a4fe67012012-07-25 20:14:29433 }
434
[email protected]6fb528e2012-08-01 20:32:33435 fullscreen_controller_.reset(new FullscreenController(this));
[email protected]c3166302012-09-25 21:27:08436 search_model_->AddObserver(this);
initial.commit09911bf2008-07-26 23:55:29437}
438
439Browser::~Browser() {
[email protected]2fc15ae2012-05-06 00:01:37440 // The tab strip should not have any tabs at this point.
[email protected]dcc87fb2013-03-01 04:13:59441 if (!browser_shutdown::ShuttingDownWithoutClosingBrowsers())
442 DCHECK(tab_strip_model_->empty());
[email protected]c3166302012-09-25 21:27:08443
444 search_model_->RemoveObserver(this);
[email protected]2fc15ae2012-05-06 00:01:37445 tab_strip_model_->RemoveObserver(this);
446
[email protected]557c4e22012-12-18 18:30:55447 // Destroy the BrowserCommandController before removing the browser, so that
448 // it doesn't act on any notifications that are sent as a result of removing
449 // the browser.
450 command_controller_.reset();
initial.commit09911bf2008-07-26 23:55:29451 BrowserList::RemoveBrowser(this);
452
[email protected]bdc6ccd12012-03-20 22:06:13453 SessionService* session_service =
454 SessionServiceFactory::GetForProfile(profile_);
455 if (session_service)
456 session_service->WindowClosed(session_id_);
457
458 TabRestoreService* tab_restore_service =
459 TabRestoreServiceFactory::GetForProfile(profile());
460 if (tab_restore_service)
461 tab_restore_service->BrowserClosed(tab_restore_service_delegate());
462
[email protected]7be64502011-05-03 17:51:47463#if !defined(OS_MACOSX)
[email protected]c987a242013-02-28 01:17:41464 if (!chrome::GetTotalBrowserCountForProfile(profile_)) {
initial.commit09911bf2008-07-26 23:55:29465 // We're the last browser window with this profile. We need to nuke the
466 // TabRestoreService, which will start the shutdown of the
467 // NavigationControllers and allow for proper shutdown. If we don't do this
468 // chrome won't shutdown cleanly, and may end up crashing when some
469 // thread tries to use the IO thread (or another thread) that is no longer
470 // valid.
[email protected]fbc947b2009-06-19 13:28:24471 // This isn't a valid assumption for Mac OS, as it stays running after
472 // the last browser has closed. The Mac equivalent is in its app
473 // controller.
[email protected]92371eb2011-04-28 11:50:15474 TabRestoreServiceFactory::ResetForProfile(profile_);
initial.commit09911bf2008-07-26 23:55:29475 }
[email protected]fbc947b2009-06-19 13:28:24476#endif
initial.commit09911bf2008-07-26 23:55:29477
[email protected]97b46382011-05-02 13:23:04478 profile_pref_registrar_.RemoveAll();
[email protected]97b46382011-05-02 13:23:04479
[email protected]9a8c4022011-01-25 14:25:33480 encoding_auto_detect_.Destroy();
[email protected]9a8c4022011-01-25 14:25:33481
initial.commit09911bf2008-07-26 23:55:29482 if (profile_->IsOffTheRecord() &&
[email protected]474e5db2011-08-12 13:02:23483 !BrowserList::IsOffTheRecordSessionActiveForProfile(profile_)) {
[email protected]5e919242012-02-13 23:59:35484 // An incognito profile is no longer needed, this indirectly frees
485 // its cache and cookies once it gets destroyed at the appropriate time.
[email protected]4dffabe2012-05-19 14:37:06486 ProfileDestroyer::DestroyProfileWhenAppropriate(profile_);
initial.commit09911bf2008-07-26 23:55:29487 }
488
489 // There may be pending file dialogs, we need to tell them that we've gone
490 // away so they don't try and call back to us.
491 if (select_file_dialog_.get())
492 select_file_dialog_->ListenerDestroyed();
493}
494
[email protected]36b6dcb2008-11-12 01:19:57495///////////////////////////////////////////////////////////////////////////////
[email protected]5c238752009-06-13 10:29:07496// Getters & Setters
497
[email protected]b77cb302009-10-29 04:09:17498FindBarController* Browser::GetFindBarController() {
499 if (!find_bar_controller_.get()) {
[email protected]632983f2011-08-08 22:51:24500 FindBar* find_bar = window_->CreateFindBar();
[email protected]b77cb302009-10-29 04:09:17501 find_bar_controller_.reset(new FindBarController(find_bar));
502 find_bar->SetFindBarController(find_bar_controller_.get());
[email protected]0d56fa7212012-11-15 23:34:02503 find_bar_controller_->ChangeWebContents(
504 tab_strip_model_->GetActiveWebContents());
[email protected]b77cb302009-10-29 04:09:17505 find_bar_controller_->find_bar()->MoveWindowIfNecessary(gfx::Rect(), true);
506 }
507 return find_bar_controller_.get();
508}
509
[email protected]24db8a072009-10-29 20:35:37510bool Browser::HasFindBarController() const {
511 return find_bar_controller_.get() != NULL;
512}
513
[email protected]b35b26b32011-05-05 20:35:14514bool Browser::is_app() const {
515 return !app_name_.empty();
516}
517
518bool Browser::is_devtools() const {
519 return app_name_ == DevToolsWindow::kDevToolsApp;
520}
521
[email protected]5c238752009-06-13 10:29:07522///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:57523// Browser, State Storage and Retrieval for UI:
524
[email protected]9b125b732012-08-17 04:43:55525gfx::Image Browser::GetCurrentPageIcon() const {
[email protected]0d56fa7212012-11-15 23:34:02526 WebContents* web_contents = tab_strip_model_->GetActiveWebContents();
[email protected]502b5612012-10-02 01:33:10527 // |web_contents| can be NULL since GetCurrentPageIcon() is called by the
528 // window during the window's creation (before tabs have been added).
529 FaviconTabHelper* favicon_tab_helper =
530 web_contents ? FaviconTabHelper::FromWebContents(web_contents) : NULL;
531 return favicon_tab_helper ? favicon_tab_helper->GetFavicon() : gfx::Image();
[email protected]36b6dcb2008-11-12 01:19:57532}
533
[email protected]731f8a42009-07-20 22:07:32534string16 Browser::GetWindowTitleForCurrentTab() const {
[email protected]0d56fa7212012-11-15 23:34:02535 WebContents* contents = tab_strip_model_->GetActiveWebContents();
[email protected]dda5dfa32009-07-10 00:33:29536 string16 title;
[email protected]c7c42332008-11-15 01:10:54537
[email protected]731f8a42009-07-20 22:07:32538 // |contents| can be NULL because GetWindowTitleForCurrentTab is called by the
539 // window during the window's creation (before tabs have been added).
[email protected]36b6dcb2008-11-12 01:19:57540 if (contents) {
[email protected]dda5dfa32009-07-10 00:33:29541 title = contents->GetTitle();
[email protected]36b6dcb2008-11-12 01:19:57542 FormatTitleForDisplay(&title);
543 }
544 if (title.empty())
[email protected]51d83bdf2011-12-09 16:09:29545 title = CoreTabHelper::GetDefaultTitle();
[email protected]36b6dcb2008-11-12 01:19:57546
[email protected]82b9ed82012-11-15 02:43:18547#if defined(OS_MACOSX)
548 // On Mac, we don't want to suffix the page title with
[email protected]22a3daa2009-11-09 22:03:33549 // the application name.
[email protected]d904bc82009-07-07 00:41:04550 return title;
[email protected]82b9ed82012-11-15 02:43:18551#elif defined(USE_ASH)
552 // On Ash, we don't want to suffix the page title with the application name,
553 // but on Windows, where USE_ASH can also be true, we still want the prefix
554 // on desktop.
555 if (host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH)
556 return title;
557#endif
[email protected]dff529732010-04-01 23:50:55558 // Don't append the app name to window titles on app frames and app popups
[email protected]9c55de82012-11-01 21:03:52559 return is_app() ?
560 title :
561 l10n_util::GetStringFUTF16(IDS_BROWSER_WINDOW_TITLE_FORMAT, title);
[email protected]36b6dcb2008-11-12 01:19:57562}
563
564// static
[email protected]dda5dfa32009-07-10 00:33:29565void Browser::FormatTitleForDisplay(string16* title) {
[email protected]36b6dcb2008-11-12 01:19:57566 size_t current_index = 0;
567 size_t match_index;
[email protected]57ecc4b2010-08-11 03:02:51568 while ((match_index = title->find(L'\n', current_index)) != string16::npos) {
[email protected]810a52ef2010-01-08 01:22:15569 title->replace(match_index, 1, string16());
[email protected]36b6dcb2008-11-12 01:19:57570 current_index = match_index;
571 }
572}
573
574///////////////////////////////////////////////////////////////////////////////
575// Browser, OnBeforeUnload handling:
576
577bool Browser::ShouldCloseWindow() {
[email protected]69444cc2009-04-09 20:40:06578 if (!CanCloseWithInProgressDownloads())
579 return false;
580
[email protected]f9b6c092012-07-09 19:32:28581 return unload_controller_->ShouldCloseWindow();
582}
[email protected]69444cc2009-04-09 20:40:06583
[email protected]f9b6c092012-07-09 19:32:28584bool Browser::IsAttemptingToCloseBrowser() const {
585 return unload_controller_->is_attempting_to_close_browser();
[email protected]36b6dcb2008-11-12 01:19:57586}
587
588void Browser::OnWindowClosing() {
589 if (!ShouldCloseWindow())
590 return;
591
[email protected]c984d9f2010-07-20 20:52:20592 // Application should shutdown on last window close if the user is explicitly
593 // trying to quit, or if there is nothing keeping the browser alive (such as
594 // AppController on the Mac, or BackgroundContentsService for background
595 // pages).
596 bool should_quit_if_last_browser =
[email protected]313fce12013-01-30 17:09:04597 browser_shutdown::IsTryingToQuit() || !chrome::WillKeepAlive();
[email protected]7dc8c6b2010-04-09 17:02:50598
[email protected]3c9185a22013-04-18 21:12:53599 if (should_quit_if_last_browser && chrome::ShouldStartShutdown(this))
[email protected]36b6dcb2008-11-12 01:19:57600 browser_shutdown::OnShutdownStarting(browser_shutdown::WINDOW_CLOSE);
601
[email protected]92371eb2011-04-28 11:50:15602 // Don't use GetForProfileIfExisting here, we want to force creation of the
[email protected]36b6dcb2008-11-12 01:19:57603 // session service so that user can restore what was open.
[email protected]92371eb2011-04-28 11:50:15604 SessionService* session_service =
605 SessionServiceFactory::GetForProfile(profile());
[email protected]36b6dcb2008-11-12 01:19:57606 if (session_service)
607 session_service->WindowClosing(session_id());
608
[email protected]92371eb2011-04-28 11:50:15609 TabRestoreService* tab_restore_service =
610 TabRestoreServiceFactory::GetForProfile(profile());
[email protected]a636d8e52012-02-28 15:40:41611
612#if defined(USE_AURA)
613 if (tab_restore_service && is_app())
614 tab_restore_service->BrowserClosing(tab_restore_service_delegate());
615#endif
616
[email protected]e0448872013-01-11 19:35:02617 if (tab_restore_service && is_type_tabbed() && tab_strip_model_->count())
[email protected]fb7da4132011-03-15 15:51:00618 tab_restore_service->BrowserClosing(tab_restore_service_delegate());
[email protected]d8375fd2008-11-25 22:45:39619
[email protected]911f8152010-03-18 16:46:40620 // TODO(sky): convert session/tab restore to use notification.
[email protected]ad50def52011-10-19 23:17:07621 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27622 chrome::NOTIFICATION_BROWSER_CLOSING,
[email protected]6c2381d2011-10-19 02:52:53623 content::Source<Browser>(this),
[email protected]1e0c7402012-07-27 10:48:15624 content::NotificationService::NoDetails());
[email protected]911f8152010-03-18 16:46:40625
[email protected]656d0992013-04-19 12:12:36626 tab_strip_model_->CloseAllTabs();
[email protected]36b6dcb2008-11-12 01:19:57627}
628
[email protected]69444cc2009-04-09 20:40:06629////////////////////////////////////////////////////////////////////////////////
630// In-progress download termination handling:
631
632void Browser::InProgressDownloadResponse(bool cancel_downloads) {
633 if (cancel_downloads) {
634 cancel_download_confirmation_state_ = RESPONSE_RECEIVED;
[email protected]a37d4b02012-06-25 21:56:10635 chrome::CloseWindow(this);
[email protected]69444cc2009-04-09 20:40:06636 return;
637 }
638
639 // Sets the confirmation state to NOT_PROMPTED so that if the user tries to
640 // close again we'll show the warning again.
641 cancel_download_confirmation_state_ = NOT_PROMPTED;
642
643 // Show the download page so the user can figure-out what downloads are still
644 // in-progress.
[email protected]5d9cace72012-06-21 16:07:12645 chrome::ShowDownloads(this);
[email protected]69444cc2009-04-09 20:40:06646}
647
[email protected]5a17d492011-10-24 19:37:03648Browser::DownloadClosePreventionType Browser::OkToCloseWithInProgressDownloads(
649 int* num_downloads_blocking) const {
650 DCHECK(num_downloads_blocking);
651 *num_downloads_blocking = 0;
652
[email protected]f9b6c092012-07-09 19:32:28653 if (IsAttemptingToCloseBrowser())
[email protected]5a17d492011-10-24 19:37:03654 return DOWNLOAD_CLOSE_OK;
655
656 // If we're not running a full browser process with a profile manager
657 // (testing), it's ok to close the browser.
658 if (!g_browser_process->profile_manager())
659 return DOWNLOAD_CLOSE_OK;
660
661 int total_download_count = DownloadService::DownloadCountAllProfiles();
662 if (total_download_count == 0)
663 return DOWNLOAD_CLOSE_OK; // No downloads; can definitely close.
664
665 // Figure out how many windows are open total, and associated with this
666 // profile, that are relevant for the ok-to-close decision.
667 int profile_window_count = 0;
668 int total_window_count = 0;
[email protected]3539929f2013-02-01 05:59:14669 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
[email protected]5a17d492011-10-24 19:37:03670 // Don't count this browser window or any other in the process of closing.
[email protected]3539929f2013-02-01 05:59:14671 Browser* const browser = *it;
[email protected]f9b6c092012-07-09 19:32:28672 // Window closing may be delayed, and windows that are in the process of
673 // closing don't count against our totals.
674 if (browser == this || browser->IsAttemptingToCloseBrowser())
[email protected]5a17d492011-10-24 19:37:03675 continue;
676
[email protected]3539929f2013-02-01 05:59:14677 if (it->profile() == profile())
[email protected]5a17d492011-10-24 19:37:03678 profile_window_count++;
679 total_window_count++;
680 }
681
682 // If there aren't any other windows, we're at browser shutdown,
683 // which would cancel all current downloads.
684 if (total_window_count == 0) {
685 *num_downloads_blocking = total_download_count;
686 return DOWNLOAD_CLOSE_BROWSER_SHUTDOWN;
687 }
688
689 // If there aren't any other windows on our profile, and we're an incognito
690 // profile, and there are downloads associated with that profile,
691 // those downloads would be cancelled by our window (-> profile) close.
692 DownloadService* download_service =
693 DownloadServiceFactory::GetForProfile(profile());
694 if (profile_window_count == 0 && download_service->DownloadCount() > 0 &&
695 profile()->IsOffTheRecord()) {
696 *num_downloads_blocking = download_service->DownloadCount();
697 return DOWNLOAD_CLOSE_LAST_WINDOW_IN_INCOGNITO_PROFILE;
698 }
699
700 // Those are the only conditions under which we will block shutdown.
701 return DOWNLOAD_CLOSE_OK;
702}
703
[email protected]69444cc2009-04-09 20:40:06704////////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:57705// Browser, Tab adding/showing functions:
706
[email protected]227393e2011-06-29 21:28:39707void Browser::WindowFullscreenStateChanged() {
[email protected]01a5a762011-11-21 05:40:55708 fullscreen_controller_->WindowFullscreenStateChanged();
[email protected]5d98294912012-06-27 22:57:40709 command_controller_->FullscreenStateChanged();
[email protected]9adb8b12011-12-09 06:49:13710 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TOGGLE_FULLSCREEN);
[email protected]ae2622c2009-07-30 23:47:58711}
712
[email protected]e67ebf32013-02-13 11:07:19713void Browser::VisibleSSLStateChanged(content::WebContents* web_contents) {
714 // When the current tab's SSL state changes, we need to update the URL
715 // bar to reflect the new state.
716 DCHECK(web_contents);
717 if (tab_strip_model_->GetActiveWebContents() == web_contents)
718 UpdateToolbar(false);
719}
720
[email protected]36b6dcb2008-11-12 01:19:57721///////////////////////////////////////////////////////////////////////////////
722// Browser, Assorted browser commands:
723
[email protected]41d9faf2012-02-28 23:46:02724void Browser::ToggleFullscreenModeWithExtension(const GURL& extension_url) {
725 fullscreen_controller_->ToggleFullscreenModeWithExtension(extension_url);
[email protected]9282cea2009-02-18 18:49:00726}
727
[email protected]a3e18c42009-03-04 23:36:05728bool Browser::SupportsWindowFeature(WindowFeature feature) const {
[email protected]018cf362010-05-05 22:43:22729 return SupportsWindowFeatureImpl(feature, true);
730}
[email protected]f5bf8ccf2010-02-05 18:19:25731
[email protected]018cf362010-05-05 22:43:22732bool Browser::CanSupportWindowFeature(WindowFeature feature) const {
733 return SupportsWindowFeatureImpl(feature, false);
[email protected]a3e18c42009-03-04 23:36:05734}
735
[email protected]fbd77592008-11-12 20:50:27736void Browser::ToggleEncodingAutoDetect() {
[email protected]7f6f44c2011-12-14 13:23:38737 content::RecordAction(UserMetricsAction("AutoDetectChange"));
[email protected]fbd77592008-11-12 20:50:27738 encoding_auto_detect_.SetValue(!encoding_auto_detect_.GetValue());
[email protected]a697f4c2009-09-14 22:30:18739 // If "auto detect" is turned on, then any current override encoding
740 // is cleared. This also implicitly performs a reload.
741 // OTOH, if "auto detect" is turned off, we don't change the currently
742 // active encoding.
743 if (encoding_auto_detect_.GetValue()) {
[email protected]0d56fa7212012-11-15 23:34:02744 WebContents* contents = tab_strip_model_->GetActiveWebContents();
[email protected]a697f4c2009-09-14 22:30:18745 if (contents)
[email protected]b2fe07d12010-02-09 14:38:08746 contents->ResetOverrideEncoding();
[email protected]a697f4c2009-09-14 22:30:18747 }
[email protected]fbd77592008-11-12 20:50:27748}
749
750void Browser::OverrideEncoding(int encoding_id) {
[email protected]7f6f44c2011-12-14 13:23:38751 content::RecordAction(UserMetricsAction("OverrideEncoding"));
[email protected]41fc0322009-09-04 22:23:40752 const std::string selected_encoding =
[email protected]fbd77592008-11-12 20:50:27753 CharacterEncoding::GetCanonicalEncodingNameByCommandId(encoding_id);
[email protected]0d56fa7212012-11-15 23:34:02754 WebContents* contents = tab_strip_model_->GetActiveWebContents();
[email protected]57c6a652009-05-04 07:58:34755 if (!selected_encoding.empty() && contents)
[email protected]b2fe07d12010-02-09 14:38:08756 contents->SetOverrideEncoding(selected_encoding);
[email protected]fbd77592008-11-12 20:50:27757 // Update the list of recently selected encodings.
[email protected]41fc0322009-09-04 22:23:40758 std::string new_selected_encoding_list;
[email protected]ddd231e2010-06-29 20:35:19759 if (CharacterEncoding::UpdateRecentlySelectedEncoding(
760 profile_->GetPrefs()->GetString(prefs::kRecentlySelectedEncoding),
[email protected]41fc0322009-09-04 22:23:40761 encoding_id,
762 &new_selected_encoding_list)) {
[email protected]fbd77592008-11-12 20:50:27763 profile_->GetPrefs()->SetString(prefs::kRecentlySelectedEncoding,
[email protected]ddd231e2010-06-29 20:35:19764 new_selected_encoding_list);
[email protected]fbd77592008-11-12 20:50:27765 }
[email protected]36b6dcb2008-11-12 01:19:57766}
767
[email protected]84214982008-12-10 18:49:10768void Browser::OpenFile() {
[email protected]7f6f44c2011-12-14 13:23:38769 content::RecordAction(UserMetricsAction("OpenFile"));
[email protected]92f54082012-07-31 01:43:14770 select_file_dialog_ = ui::SelectFileDialog::Create(
[email protected]6e1fcd12012-07-02 17:14:20771 this, new ChromeSelectFilePolicy(
[email protected]0d56fa7212012-11-15 23:34:02772 tab_strip_model_->GetActiveWebContents()));
[email protected]84214982008-12-10 18:49:10773
[email protected]650b2d52013-02-10 03:41:45774 const base::FilePath directory = profile_->last_selected_directory();
[email protected]35896a32010-06-09 08:42:51775
[email protected]84214982008-12-10 18:49:10776 // TODO(beng): figure out how to juggle this.
[email protected]90556dd2012-06-07 20:26:18777 gfx::NativeWindow parent_window = window_->GetNativeWindow();
[email protected]479cce782012-09-15 20:15:53778 ui::SelectFileDialog::FileTypeInfo file_types;
[email protected]599538b2013-02-20 14:15:14779 file_types.support_drive = true;
[email protected]92f54082012-07-31 01:43:14780 select_file_dialog_->SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE,
[email protected]007b3f82013-04-09 08:46:45781 string16(),
782 directory,
783 &file_types,
784 0,
785 base::FilePath::StringType(),
786 parent_window,
787 NULL);
[email protected]84214982008-12-10 18:49:10788}
789
[email protected]92655362012-04-20 21:33:17790void Browser::UpdateDownloadShelfVisibility(bool visible) {
791 if (GetStatusBubble())
792 GetStatusBubble()->UpdateDownloadShelfVisibility(visible);
793}
794
[email protected]36b6dcb2008-11-12 01:19:57795///////////////////////////////////////////////////////////////////////////////
796
797// static
[email protected]d9083482012-01-06 00:38:46798bool Browser::RunUnloadEventsHelper(WebContents* contents) {
[email protected]075969d2012-04-17 20:05:55799 // If the WebContents is not connected yet, then there's no unload
800 // handler we can fire even if the WebContents has an unload listener.
[email protected]ecc523f62010-09-28 22:45:40801 // One case where we hit this is in a tab that has an infinite loop
802 // before load.
[email protected]5aab5e22010-12-08 22:13:29803 if (contents->NeedToFireBeforeUnload()) {
[email protected]ecc523f62010-09-28 22:45:40804 // If the page has unload listeners, then we tell the renderer to fire
805 // them. Once they have fired, we'll get a message back saying whether
806 // to proceed closing the page or not, which sends us back to this method
[email protected]5aab5e22010-12-08 22:13:29807 // with the NeedToFireBeforeUnload bit cleared.
[email protected]151a63d2011-12-20 22:32:52808 contents->GetRenderViewHost()->FirePageBeforeUnload(false);
[email protected]ecc523f62010-09-28 22:45:40809 return true;
810 }
811 return false;
812}
813
814// static
[email protected]1d3e9172012-06-12 02:59:13815void Browser::JSOutOfMemoryHelper(WebContents* web_contents) {
[email protected]4f822f022012-12-20 19:11:42816 InfoBarService* infobar_service =
817 InfoBarService::FromWebContents(web_contents);
818 if (!infobar_service)
[email protected]2a6bc3e2011-12-28 23:51:33819 return;
820
[email protected]0be09932013-01-08 02:03:50821 SimpleAlertInfoBarDelegate::Create(
822 infobar_service, NULL,
823 l10n_util::GetStringUTF16(IDS_JS_OUT_OF_MEMORY_PROMPT), true);
[email protected]7d189022011-08-25 22:54:20824}
825
826// static
[email protected]1d3e9172012-06-12 02:59:13827void Browser::RegisterProtocolHandlerHelper(WebContents* web_contents,
[email protected]7d189022011-08-25 22:54:20828 const std::string& protocol,
829 const GURL& url,
[email protected]3a3b75a2012-06-01 08:38:36830 const string16& title,
[email protected]7ae9c322012-07-17 23:46:44831 bool user_gesture,
832 BrowserWindow* window) {
[email protected]aa190632012-12-04 07:39:53833 Profile* profile =
834 Profile::FromBrowserContext(web_contents->GetBrowserContext());
835 if (profile->IsOffTheRecord())
[email protected]7d189022011-08-25 22:54:20836 return;
837
[email protected]7d189022011-08-25 22:54:20838 ProtocolHandler handler =
839 ProtocolHandler::CreateProtocolHandler(protocol, url, title);
840
[email protected]8a39b472013-04-04 17:07:42841 ProtocolHandlerRegistry* registry =
842 ProtocolHandlerRegistryFactory::GetForProfile(profile);
[email protected]96affbc82012-07-23 18:04:42843 if (registry->SilentlyHandleRegisterHandlerRequest(handler))
844 return;
845
[email protected]8a39b472013-04-04 17:07:42846 TabSpecificContentSettings* tab_content_settings =
847 TabSpecificContentSettings::FromWebContents(web_contents);
[email protected]7ae9c322012-07-17 23:46:44848 if (!user_gesture && window) {
[email protected]c5f51192012-09-28 04:09:50849 tab_content_settings->set_pending_protocol_handler(handler);
850 tab_content_settings->set_previous_protocol_handler(
[email protected]7ae9c322012-07-17 23:46:44851 registry->GetHandlerFor(handler.protocol()));
852 window->GetLocationBar()->UpdateContentSettingsIcons();
[email protected]3fa5e192012-07-15 00:40:34853 return;
[email protected]7ae9c322012-07-17 23:46:44854 }
855
856 // Make sure content-setting icon is turned off in case the page does
857 // ungestured and gestured RPH calls.
858 if (window) {
[email protected]c5f51192012-09-28 04:09:50859 tab_content_settings->ClearPendingProtocolHandler();
[email protected]7ae9c322012-07-17 23:46:44860 window->GetLocationBar()->UpdateContentSettingsIcons();
861 }
[email protected]3fa5e192012-07-15 00:40:34862
[email protected]0be09932013-01-08 02:03:50863 RegisterProtocolHandlerInfoBarDelegate::Create(
864 InfoBarService::FromWebContents(web_contents), registry, handler);
[email protected]7d189022011-08-25 22:54:20865}
866
867// static
[email protected]1d3e9172012-06-12 02:59:13868void Browser::FindReplyHelper(WebContents* web_contents,
[email protected]b888919c2011-09-02 00:32:16869 int request_id,
870 int number_of_matches,
871 const gfx::Rect& selection_rect,
872 int active_match_ordinal,
873 bool final_update) {
[email protected]bc852c932012-09-29 21:38:34874 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(web_contents);
875 if (!find_tab_helper)
[email protected]b888919c2011-09-02 00:32:16876 return;
877
[email protected]bc852c932012-09-29 21:38:34878 find_tab_helper->HandleFindReply(request_id,
879 number_of_matches,
880 selection_rect,
881 active_match_ordinal,
882 final_update);
[email protected]b888919c2011-09-02 00:32:16883}
884
[email protected]e232c992012-12-06 12:43:20885void Browser::UpdateUIForNavigationInTab(WebContents* contents,
[email protected]ef557022012-03-16 10:05:33886 content::PageTransition transition,
887 bool user_initiated) {
[email protected]e232c992012-12-06 12:43:20888 tab_strip_model_->TabNavigating(contents, transition);
[email protected]ef557022012-03-16 10:05:33889
[email protected]0d56fa7212012-11-15 23:34:02890 bool contents_is_selected =
[email protected]e232c992012-12-06 12:43:20891 contents == tab_strip_model_->GetActiveWebContents();
[email protected]ef557022012-03-16 10:05:33892 if (user_initiated && contents_is_selected && window()->GetLocationBar()) {
893 // Forcibly reset the location bar if the url is going to change in the
894 // current tab, since otherwise it won't discard any ongoing user edits,
895 // since it doesn't realize this is a user-initiated action.
896 window()->GetLocationBar()->Revert();
897 }
898
899 if (GetStatusBubble())
900 GetStatusBubble()->Hide();
901
902 // Update the location bar. This is synchronous. We specifically don't
903 // update the load state since the load hasn't started yet and updating it
904 // will put it out of sync with the actual state like whether we're
905 // displaying a favicon, which controls the throbber. If we updated it here,
906 // the throbber will show the default favicon for a split second when
907 // navigating away from the new tab page.
[email protected]e232c992012-12-06 12:43:20908 ScheduleUIUpdate(contents, content::INVALIDATE_TYPE_URL);
[email protected]ef557022012-03-16 10:05:33909
[email protected]e1f288d2012-05-23 19:36:10910 if (contents_is_selected)
[email protected]5b4ee2772013-03-04 22:15:29911 contents->GetView()->SetInitialFocus();
[email protected]ef557022012-03-16 10:05:33912}
913
914///////////////////////////////////////////////////////////////////////////////
915// Browser, PageNavigator implementation:
916
917WebContents* Browser::OpenURL(const OpenURLParams& params) {
918 return OpenURLFromTab(NULL, params);
919}
920
[email protected]485fba42009-03-24 23:27:29921///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:57922// Browser, TabStripModelObserver implementation:
923
[email protected]409ea2972012-11-10 19:54:43924void Browser::TabInsertedAt(WebContents* contents,
[email protected]36b6dcb2008-11-12 01:19:57925 int index,
926 bool foreground) {
[email protected]409ea2972012-11-10 19:54:43927 SetAsDelegate(contents, this);
[email protected]50a9a76b2012-09-12 14:19:36928 SessionTabHelper* session_tab_helper =
[email protected]409ea2972012-11-10 19:54:43929 SessionTabHelper::FromWebContents(contents);
[email protected]50a9a76b2012-09-12 14:19:36930 session_tab_helper->SetWindowID(session_id());
[email protected]e524ff92011-03-30 13:26:58931
[email protected]fb312eb2012-09-24 23:21:10932 content::NotificationService::current()->Notify(
933 chrome::NOTIFICATION_TAB_PARENTED,
[email protected]409ea2972012-11-10 19:54:43934 content::Source<content::WebContents>(contents),
[email protected]fb312eb2012-09-24 23:21:10935 content::NotificationService::NoDetails());
936
[email protected]c37e3b62010-01-22 00:15:17937 SyncHistoryWithTabs(index);
[email protected]36b6dcb2008-11-12 01:19:57938
[email protected]159f7762008-12-19 14:58:27939 // Make sure the loading state is updated correctly, otherwise the throbber
940 // won't start if the page is loading.
[email protected]409ea2972012-11-10 19:54:43941 LoadingStateChanged(contents);
[email protected]159f7762008-12-19 14:58:27942
[email protected]20ca0382013-02-28 19:50:07943 interstitial_observers_.push_back(new InterstitialObserver(this, contents));
[email protected]e397a4472011-12-21 21:47:50944
[email protected]40600152012-09-26 10:04:31945 SessionService* session_service =
946 SessionServiceFactory::GetForProfile(profile_);
[email protected]d4c9caa2013-04-22 18:30:32947 if (session_service) {
[email protected]409ea2972012-11-10 19:54:43948 session_service->TabInserted(contents);
[email protected]d4c9caa2013-04-22 18:30:32949 int new_active_index = tab_strip_model_->active_index();
950 if (index < new_active_index)
951 session_service->SetSelectedTabInWindow(session_id(),
952 new_active_index);
953 }
[email protected]36b6dcb2008-11-12 01:19:57954}
955
[email protected]f2cc7382010-10-15 18:03:05956void Browser::TabClosingAt(TabStripModel* tab_strip_model,
[email protected]0be097d2012-11-10 00:20:16957 WebContents* contents,
[email protected]f2cc7382010-10-15 18:03:05958 int index) {
[email protected]0be097d2012-11-10 00:20:16959 fullscreen_controller_->OnTabClosing(contents);
[email protected]40600152012-09-26 10:04:31960 SessionService* session_service =
961 SessionServiceFactory::GetForProfile(profile_);
962 if (session_service)
[email protected]0be097d2012-11-10 00:20:16963 session_service->TabClosing(contents);
[email protected]ad50def52011-10-19 23:17:07964 content::NotificationService::current()->Notify(
[email protected]884033e2012-04-16 19:38:42965 chrome::NOTIFICATION_TAB_CLOSING,
[email protected]0be097d2012-11-10 00:20:16966 content::Source<NavigationController>(&contents->GetController()),
[email protected]ad50def52011-10-19 23:17:07967 content::NotificationService::NoDetails());
[email protected]36b6dcb2008-11-12 01:19:57968
[email protected]075969d2012-04-17 20:05:55969 // Sever the WebContents' connection back to us.
[email protected]0be097d2012-11-10 00:20:16970 SetAsDelegate(contents, NULL);
[email protected]36b6dcb2008-11-12 01:19:57971}
972
[email protected]e89cfcb2012-11-11 14:47:24973void Browser::TabDetachedAt(WebContents* contents, int index) {
[email protected]d4c9caa2013-04-22 18:30:32974 // TabDetachedAt is called before TabStripModel has updated the
975 // active index.
976 int old_active_index = tab_strip_model_->active_index();
977 if (index < old_active_index && !tab_strip_model_->closing_all()) {
978 SessionService* session_service =
979 SessionServiceFactory::GetForProfileIfExisting(profile_);
980 if (session_service)
981 session_service->SetSelectedTabInWindow(session_id(),
982 old_active_index - 1);
983 }
[email protected]c37e3b62010-01-22 00:15:17984 TabDetachedAtImpl(contents, index, DETACH_TYPE_DETACH);
[email protected]36b6dcb2008-11-12 01:19:57985}
986
[email protected]023c01cc2012-11-12 15:41:11987void Browser::TabDeactivated(WebContents* contents) {
[email protected]01a5a762011-11-21 05:40:55988 fullscreen_controller_->OnTabDeactivated(contents);
[email protected]023c01cc2012-11-12 15:41:11989 search_delegate_->OnTabDeactivated(contents);
[email protected]e3a5d5b2012-06-22 17:56:46990
[email protected]85ff2c42009-05-29 21:22:03991 // Save what the user's currently typing, so it can be restored when we
992 // switch back to this tab.
[email protected]023c01cc2012-11-12 15:41:11993 window_->GetLocationBar()->SaveStateToContents(contents);
[email protected]3d6a8952012-12-14 03:18:07994
995 if (instant_controller_)
996 instant_controller_->TabDeactivated(contents);
[email protected]85ff2c42009-05-29 21:22:03997}
998
[email protected]8e707792012-11-13 10:32:12999void Browser::ActiveTabChanged(WebContents* old_contents,
1000 WebContents* new_contents,
[email protected]3aca95d42011-05-24 22:06:541001 int index,
[email protected]5f2de242013-04-05 02:06:071002 int reason) {
[email protected]d500b1c32013-03-12 06:26:081003 content::RecordAction(UserMetricsAction("ActiveTabChanged"));
1004
[email protected]002efb042011-10-07 17:30:241005 // Discarded tabs always get reloaded.
[email protected]00a0fad2013-05-15 09:08:381006 if (tab_strip_model_->IsTabDiscarded(index)) {
[email protected]d6d4dae2012-04-18 17:47:321007 LOG(WARNING) << "Reloading discarded tab at " << index;
1008 static int reload_count = 0;
[email protected]24c463a2012-04-20 22:33:501009 UMA_HISTOGRAM_CUSTOM_COUNTS(
1010 "Tabs.Discard.ReloadCount", ++reload_count, 1, 1000, 50);
[email protected]a37d4b02012-06-25 21:56:101011 chrome::Reload(this, CURRENT_TAB);
[email protected]002efb042011-10-07 17:30:241012 }
1013
[email protected]36b6dcb2008-11-12 01:19:571014 // If we have any update pending, do it now.
[email protected]24f1477532011-11-22 22:55:361015 if (chrome_updater_factory_.HasWeakPtrs() && old_contents)
[email protected]36b6dcb2008-11-12 01:19:571016 ProcessPendingUIUpdates();
1017
[email protected]36b6dcb2008-11-12 01:19:571018 // Propagate the profile to the location bar.
[email protected]9bc3f242013-04-05 23:27:361019 UpdateToolbar((reason & CHANGE_REASON_REPLACED) == 0);
[email protected]36b6dcb2008-11-12 01:19:571020
[email protected]e3a5d5b2012-06-22 17:56:461021 // Propagate tab state to toolbar, tab-strip, etc.
1022 UpdateSearchState(new_contents);
1023
[email protected]943d8122010-06-11 02:13:451024 // Update reload/stop state.
[email protected]8e707792012-11-13 10:32:121025 command_controller_->LoadingStateChanged(new_contents->IsLoading(), true);
[email protected]36b6dcb2008-11-12 01:19:571026
[email protected]84214982008-12-10 18:49:101027 // Update commands to reflect current state.
[email protected]5d98294912012-06-27 22:57:401028 command_controller_->TabStateChanged();
[email protected]36b6dcb2008-11-12 01:19:571029
1030 // Reset the status bubble.
[email protected]be3877f2009-01-14 15:51:101031 StatusBubble* status_bubble = GetStatusBubble();
1032 if (status_bubble) {
1033 status_bubble->Hide();
[email protected]36b6dcb2008-11-12 01:19:571034
[email protected]be3877f2009-01-14 15:51:101035 // Show the loading state (if any).
[email protected]0d56fa7212012-11-15 23:34:021036 status_bubble->SetStatus(CoreTabHelper::FromWebContents(
1037 tab_strip_model_->GetActiveWebContents())->GetStatusText());
[email protected]be3877f2009-01-14 15:51:101038 }
[email protected]36b6dcb2008-11-12 01:19:571039
[email protected]24db8a072009-10-29 20:35:371040 if (HasFindBarController()) {
[email protected]8e707792012-11-13 10:32:121041 find_bar_controller_->ChangeWebContents(new_contents);
[email protected]b77cb302009-10-29 04:09:171042 find_bar_controller_->find_bar()->MoveWindowIfNecessary(gfx::Rect(), true);
[email protected]4801ecc2009-04-05 04:52:581043 }
1044
[email protected]36b6dcb2008-11-12 01:19:571045 // Update sessions. Don't force creation of sessions. If sessions doesn't
1046 // exist, the change will be picked up by sessions when created.
[email protected]92371eb2011-04-28 11:50:151047 SessionService* session_service =
1048 SessionServiceFactory::GetForProfileIfExisting(profile_);
[email protected]2fc15ae2012-05-06 00:01:371049 if (session_service && !tab_strip_model_->closing_all()) {
[email protected]04e3ead52013-01-14 01:09:201050 session_service->SetSelectedTabInWindow(session_id(),
1051 tab_strip_model_->active_index());
[email protected]36b6dcb2008-11-12 01:19:571052 }
[email protected]09b29342011-06-24 19:18:481053
1054 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_SWITCH);
[email protected]e41982a72012-11-20 07:16:511055
1056 // This needs to be called after UpdateSearchState().
1057 if (instant_controller_)
1058 instant_controller_->ActiveTabChanged();
[email protected]36b6dcb2008-11-12 01:19:571059}
1060
[email protected]3abdff22012-11-13 10:28:451061void Browser::TabMoved(WebContents* contents,
[email protected]36b6dcb2008-11-12 01:19:571062 int from_index,
[email protected]1a242c32010-02-02 21:20:541063 int to_index) {
[email protected]36b6dcb2008-11-12 01:19:571064 DCHECK(from_index >= 0 && to_index >= 0);
1065 // Notify the history service.
1066 SyncHistoryWithTabs(std::min(from_index, to_index));
1067}
1068
[email protected]da25dd52011-01-11 19:06:301069void Browser::TabReplacedAt(TabStripModel* tab_strip_model,
[email protected]b624ddc2012-11-15 18:04:131070 WebContents* old_contents,
1071 WebContents* new_contents,
[email protected]8a34e6602010-10-02 17:29:431072 int index) {
[email protected]b624ddc2012-11-15 18:04:131073 TabDetachedAtImpl(old_contents, index, DETACH_TYPE_REPLACE);
[email protected]40600152012-09-26 10:04:311074 SessionService* session_service =
1075 SessionServiceFactory::GetForProfile(profile_);
1076 if (session_service)
[email protected]b624ddc2012-11-15 18:04:131077 session_service->TabClosing(old_contents);
[email protected]04e3ead52013-01-14 01:09:201078 TabInsertedAt(new_contents,
1079 index,
1080 (index == tab_strip_model_->active_index()));
[email protected]c37e3b62010-01-22 00:15:171081
[email protected]b624ddc2012-11-15 18:04:131082 int entry_count = new_contents->GetController().GetEntryCount();
[email protected]c37e3b62010-01-22 00:15:171083 if (entry_count > 0) {
1084 // Send out notification so that observers are updated appropriately.
[email protected]b624ddc2012-11-15 18:04:131085 new_contents->GetController().NotifyEntryChanged(
1086 new_contents->GetController().GetEntryAtIndex(entry_count - 1),
[email protected]c37e3b62010-01-22 00:15:171087 entry_count - 1);
1088 }
[email protected]47e020a2010-10-15 14:43:371089
[email protected]47e020a2010-10-15 14:43:371090 if (session_service) {
1091 // The new_contents may end up with a different navigation stack. Force
1092 // the session service to update itself.
[email protected]52877dbc62012-06-29 22:22:031093 session_service->TabRestored(new_contents,
1094 tab_strip_model_->IsTabPinned(index));
[email protected]47e020a2010-10-15 14:43:371095 }
[email protected]c37e3b62010-01-22 00:15:171096}
1097
[email protected]2c7dba42012-11-09 17:05:421098void Browser::TabPinnedStateChanged(WebContents* contents, int index) {
[email protected]92371eb2011-04-28 11:50:151099 SessionService* session_service =
1100 SessionServiceFactory::GetForProfileIfExisting(profile());
[email protected]5c0e6482009-07-14 20:20:091101 if (session_service) {
[email protected]50a9a76b2012-09-12 14:19:361102 SessionTabHelper* session_tab_helper =
[email protected]2c7dba42012-11-09 17:05:421103 SessionTabHelper::FromWebContents(contents);
1104 session_service->SetPinnedState(session_id(),
1105 session_tab_helper->session_id(),
1106 tab_strip_model_->IsTabPinned(index));
[email protected]5c0e6482009-07-14 20:20:091107 }
1108}
1109
[email protected]36b6dcb2008-11-12 01:19:571110void Browser::TabStripEmpty() {
1111 // Close the frame after we return to the message loop (not immediately,
1112 // otherwise it will destroy this object before the stack has a chance to
1113 // cleanly unwind.)
1114 // Note: This will be called several times if TabStripEmpty is called several
1115 // times. This is because it does not close the window if tabs are
1116 // still present.
[email protected]3264d162013-05-08 22:02:111117 base::MessageLoop::current()->PostTask(
[email protected]24f1477532011-11-22 22:55:361118 FROM_HERE, base::Bind(&Browser::CloseFrame, weak_factory_.GetWeakPtr()));
[email protected]e41982a72012-11-20 07:16:511119
1120 // Instant may have visible WebContents that need to be detached before the
1121 // window system closes.
1122 instant_controller_.reset();
[email protected]36b6dcb2008-11-12 01:19:571123}
1124
[email protected]067310262012-11-22 14:30:411125bool Browser::CanOverscrollContent() const {
1126#if defined(USE_AURA)
[email protected]d545b452013-04-03 19:28:211127 bool overscroll_enabled = CommandLine::ForCurrentProcess()->
[email protected]177260c2013-04-24 01:47:381128 GetSwitchValueASCII(switches::kOverscrollHistoryNavigation) != "0";
[email protected]86f98a22013-03-20 14:35:001129 if (!overscroll_enabled)
1130 return false;
1131 if (is_app() || is_devtools() || !is_type_tabbed())
1132 return false;
1133
1134 // The detached bookmark bar has appearance of floating above the
1135 // web-contents. This does not play nicely with overscroll navigation
1136 // gestures. So disable overscroll navigation when the bookmark bar is in the
1137 // detached state.
1138 if (bookmark_bar_state_ == BookmarkBar::DETACHED)
1139 return false;
1140 return true;
[email protected]067310262012-11-22 14:30:411141#else
1142 return false;
1143#endif
1144}
1145
[email protected]b3996ba2012-08-08 00:39:131146bool Browser::PreHandleKeyboardEvent(content::WebContents* source,
1147 const NativeWebKeyboardEvent& event,
[email protected]fc761142012-06-08 04:49:201148 bool* is_keyboard_shortcut) {
1149 // Escape exits tabbed fullscreen mode.
1150 // TODO(koz): Write a test for this http://crbug.com/100441.
1151 if (event.windowsKeyCode == 27 &&
1152 fullscreen_controller_->HandleUserPressedEscape()) {
1153 return true;
1154 }
1155 return window()->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
1156}
1157
[email protected]b3996ba2012-08-08 00:39:131158void Browser::HandleKeyboardEvent(content::WebContents* source,
1159 const NativeWebKeyboardEvent& event) {
[email protected]fc761142012-06-08 04:49:201160 window()->HandleKeyboardEvent(event);
1161}
1162
[email protected]656d0992013-04-19 12:12:361163bool Browser::TabsNeedBeforeUnloadFired() {
1164 return unload_controller_->TabsNeedBeforeUnloadFired();
1165}
1166
[email protected]d013f142012-04-27 17:31:141167bool Browser::IsMouseLocked() const {
1168 return fullscreen_controller_->IsMouseLocked();
[email protected]92655362012-04-20 21:33:171169}
1170
1171void Browser::OnWindowDidShow() {
1172 if (window_has_shown_)
1173 return;
1174 window_has_shown_ = true;
1175
[email protected]b5febf72012-07-29 18:10:091176// CurrentProcessInfo::CreationTime() is currently only implemented on Mac and
1177// Windows.
1178#if defined(OS_MACOSX) || defined(OS_WIN)
1179 // Measure the latency from startup till the first browser window becomes
1180 // visible.
1181 static bool is_first_browser_window = true;
1182 if (is_first_browser_window &&
1183 !startup_metric_utils::WasNonBrowserUIDisplayed()) {
1184 is_first_browser_window = false;
1185 const base::Time* process_creation_time =
1186 base::CurrentProcessInfo::CreationTime();
1187
1188 if (process_creation_time) {
1189 UMA_HISTOGRAM_LONG_TIMES(
1190 "Startup.BrowserWindowDisplay",
1191 base::Time::Now() - *process_creation_time);
1192 }
1193 }
[email protected]9c55de82012-11-01 21:03:521194#endif // defined(OS_MACOSX) || defined(OS_WIN)
[email protected]b5febf72012-07-29 18:10:091195
[email protected]92655362012-04-20 21:33:171196 // Nothing to do for non-tabbed windows.
1197 if (!is_type_tabbed())
1198 return;
1199
1200 // Show any pending global error bubble.
1201 GlobalErrorService* service =
1202 GlobalErrorServiceFactory::GetForProfile(profile());
1203 GlobalError* error = service->GetFirstGlobalErrorWithBubbleView();
1204 if (error)
1205 error->ShowBubbleView(this);
1206}
1207
1208void Browser::ShowFirstRunBubble() {
1209 window()->GetLocationBar()->ShowFirstRunBubble();
1210}
1211
[email protected]b3756012013-03-06 17:43:021212void Browser::ShowDownload(content::DownloadItem* download) {
1213 if (!window())
1214 return;
1215
1216 // If the download occurs in a new tab, and it's not a save page
1217 // download (started before initial navigation completed) close it.
1218 WebContents* source = download->GetWebContents();
1219 if (source && source->GetController().IsInitialNavigation() &&
1220 tab_strip_model_->count() > 1 && !download->IsSavePackageDownload()) {
1221 CloseContents(source);
1222 }
1223
1224 // Some (app downloads) are not supposed to appear on the shelf.
1225 if (!DownloadItemModel(download).ShouldShowInShelf())
1226 return;
1227
1228 // GetDownloadShelf creates the download shelf if it was not yet created.
1229 DownloadShelf* shelf = window()->GetDownloadShelf();
1230 shelf->AddDownload(download);
1231}
1232
[email protected]92655362012-04-20 21:33:171233///////////////////////////////////////////////////////////////////////////////
[email protected]674bc592011-12-20 23:00:421234// Browser, content::WebContentsDelegate implementation:
initial.commit09911bf2008-07-26 23:55:291235
[email protected]e5d549d2011-12-28 01:29:201236WebContents* Browser::OpenURLFromTab(WebContents* source,
[email protected]00c37fc2011-08-02 00:22:501237 const OpenURLParams& params) {
[email protected]78e2edc2012-07-01 23:32:281238 chrome::NavigateParams nav_params(this, params.url, params.transition);
[email protected]19c64142013-03-06 20:08:531239 FillNavigateParamsFromOpenURLParams(&nav_params, params);
[email protected]e232c992012-12-06 12:43:201240 nav_params.source_contents = source;
[email protected]00c37fc2011-08-02 00:22:501241 nav_params.tabstrip_add_types = TabStripModel::ADD_NONE;
[email protected]50592b52013-05-02 22:26:251242 nav_params.window_action = chrome::NavigateParams::SHOW_WINDOW;
[email protected]00c37fc2011-08-02 00:22:501243 nav_params.user_gesture = true;
[email protected]78e2edc2012-07-01 23:32:281244 chrome::Navigate(&nav_params);
[email protected]00c37fc2011-08-02 00:22:501245
[email protected]e232c992012-12-06 12:43:201246 return nav_params.target_contents;
initial.commit09911bf2008-07-26 23:55:291247}
1248
[email protected]2a6bc3e2011-12-28 23:51:331249void Browser::NavigationStateChanged(const WebContents* source,
initial.commit09911bf2008-07-26 23:55:291250 unsigned changed_flags) {
initial.commit09911bf2008-07-26 23:55:291251 // Only update the UI when something visible has changed.
[email protected]e83f1682008-09-07 23:57:401252 if (changed_flags)
initial.commit09911bf2008-07-26 23:55:291253 ScheduleUIUpdate(source, changed_flags);
1254
[email protected]83c2e232011-10-07 21:36:461255 // We can synchronously update commands since they will only change once per
1256 // navigation, so we don't have to worry about flickering. We do, however,
1257 // need to update the command state early on load to always present usable
1258 // actions in the face of slow-to-commit pages.
[email protected]d9083482012-01-06 00:38:461259 if (changed_flags & (content::INVALIDATE_TYPE_URL |
1260 content::INVALIDATE_TYPE_LOAD))
[email protected]5d98294912012-06-27 22:57:401261 command_controller_->TabStateChanged();
initial.commit09911bf2008-07-26 23:55:291262}
1263
[email protected]2a6bc3e2011-12-28 23:51:331264void Browser::AddNewContents(WebContents* source,
1265 WebContents* new_contents,
initial.commit09911bf2008-07-26 23:55:291266 WindowOpenDisposition disposition,
1267 const gfx::Rect& initial_pos,
[email protected]eda238a12012-09-07 23:44:001268 bool user_gesture,
1269 bool* was_blocked) {
[email protected]52877dbc62012-06-29 22:22:031270 chrome::AddWebContents(this, source, new_contents, disposition, initial_pos,
[email protected]eda238a12012-09-07 23:44:001271 user_gesture, was_blocked);
initial.commit09911bf2008-07-26 23:55:291272}
1273
[email protected]2a6bc3e2011-12-28 23:51:331274void Browser::ActivateContents(WebContents* contents) {
[email protected]0d56fa7212012-11-15 23:34:021275 tab_strip_model_->ActivateTabAt(
1276 tab_strip_model_->GetIndexOfWebContents(contents), false);
[email protected]f3e99e32008-07-30 04:48:391277 window_->Activate();
initial.commit09911bf2008-07-26 23:55:291278}
1279
[email protected]2a6bc3e2011-12-28 23:51:331280void Browser::DeactivateContents(WebContents* contents) {
[email protected]ea42e7782010-08-23 23:58:121281 window_->Deactivate();
1282}
1283
[email protected]2a6bc3e2011-12-28 23:51:331284void Browser::LoadingStateChanged(WebContents* source) {
[email protected]2fc15ae2012-05-06 00:01:371285 window_->UpdateLoadingAnimations(tab_strip_model_->TabsAreLoading());
[email protected]f3e99e32008-07-30 04:48:391286 window_->UpdateTitleBar();
initial.commit09911bf2008-07-26 23:55:291287
[email protected]0d56fa7212012-11-15 23:34:021288 WebContents* selected_contents = tab_strip_model_->GetActiveWebContents();
[email protected]3c9e1872010-11-18 16:17:491289 if (source == selected_contents) {
[email protected]f5d978c2011-07-21 14:43:511290 bool is_loading = source->IsLoading();
[email protected]5d98294912012-06-27 22:57:401291 command_controller_->LoadingStateChanged(is_loading, false);
[email protected]43d259d2010-09-03 21:37:461292 if (GetStatusBubble()) {
[email protected]0d56fa7212012-11-15 23:34:021293 GetStatusBubble()->SetStatus(CoreTabHelper::FromWebContents(
1294 tab_strip_model_->GetActiveWebContents())->GetStatusText());
[email protected]43d259d2010-09-03 21:37:461295 }
initial.commit09911bf2008-07-26 23:55:291296 }
1297}
1298
[email protected]a81343d232011-12-27 07:39:201299void Browser::CloseContents(WebContents* source) {
[email protected]f9b6c092012-07-09 19:32:281300 if (unload_controller_->CanCloseContents(source))
[email protected]81ddd3402012-12-28 00:55:261301 chrome::CloseWebContents(this, source, true);
initial.commit09911bf2008-07-26 23:55:291302}
1303
[email protected]2a6bc3e2011-12-28 23:51:331304void Browser::MoveContents(WebContents* source, const gfx::Rect& pos) {
[email protected]b35b26b32011-05-05 20:35:141305 if (!IsPopupOrPanel(source)) {
initial.commit09911bf2008-07-26 23:55:291306 NOTREACHED() << "moving invalid browser type";
1307 return;
1308 }
[email protected]e561e202010-03-24 17:57:121309 window_->SetBounds(pos);
initial.commit09911bf2008-07-26 23:55:291310}
1311
[email protected]2a6bc3e2011-12-28 23:51:331312bool Browser::IsPopupOrPanel(const WebContents* source) const {
[email protected]7efaed362013-04-04 09:33:321313 return is_type_popup();
initial.commit09911bf2008-07-26 23:55:291314}
1315
[email protected]2a6bc3e2011-12-28 23:51:331316void Browser::UpdateTargetURL(WebContents* source, int32 page_id,
[email protected]d952a052011-09-06 18:42:451317 const GURL& url) {
[email protected]be3877f2009-01-14 15:51:101318 if (!GetStatusBubble())
1319 return;
1320
[email protected]0d56fa7212012-11-15 23:34:021321 if (source == tab_strip_model_->GetActiveWebContents()) {
[email protected]36b6dcb2008-11-12 01:19:571322 PrefService* prefs = profile_->GetPrefs();
[email protected]65233052011-08-22 19:02:481323 GetStatusBubble()->SetURL(url, prefs->GetString(prefs::kAcceptLanguages));
[email protected]36b6dcb2008-11-12 01:19:571324 }
1325}
1326
[email protected]92655362012-04-20 21:33:171327void Browser::ContentsMouseEvent(
1328 WebContents* source, const gfx::Point& location, bool motion) {
1329 if (!GetStatusBubble())
1330 return;
1331
[email protected]0d56fa7212012-11-15 23:34:021332 if (source == tab_strip_model_->GetActiveWebContents()) {
[email protected]92655362012-04-20 21:33:171333 GetStatusBubble()->MouseMoved(location, !motion);
1334 if (!motion)
1335 GetStatusBubble()->SetURL(GURL(), std::string());
1336 }
[email protected]3a6a3b62009-05-27 21:36:201337}
1338
[email protected]36b6dcb2008-11-12 01:19:571339void Browser::ContentsZoomChange(bool zoom_in) {
[email protected]5d98294912012-06-27 22:57:401340 chrome::ExecuteCommand(this, zoom_in ? IDC_ZOOM_PLUS : IDC_ZOOM_MINUS);
[email protected]36b6dcb2008-11-12 01:19:571341}
1342
[email protected]2a6bc3e2011-12-28 23:51:331343void Browser::WebContentsFocused(WebContents* contents) {
1344 window_->WebContentsFocused(contents);
[email protected]7e383692009-06-12 19:14:541345}
1346
[email protected]b3996ba2012-08-08 00:39:131347bool Browser::TakeFocus(content::WebContents* source,
1348 bool reverse) {
[email protected]ad50def52011-10-19 23:17:071349 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271350 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:531351 content::Source<Browser>(this),
[email protected]ad50def52011-10-19 23:17:071352 content::NotificationService::NoDetails());
[email protected]130efb02009-09-18 18:54:351353 return false;
1354}
1355
[email protected]b7a756d42012-01-23 18:08:171356gfx::Rect Browser::GetRootWindowResizerRect() const {
1357 return window_->GetRootWindowResizerRect();
1358}
1359
[email protected]1d3e9172012-06-12 02:59:131360void Browser::BeforeUnloadFired(WebContents* web_contents,
[email protected]36b6dcb2008-11-12 01:19:571361 bool proceed,
1362 bool* proceed_to_fire_unload) {
[email protected]f9b6c092012-07-09 19:32:281363 *proceed_to_fire_unload =
1364 unload_controller_->BeforeUnloadFired(web_contents, proceed);
[email protected]36b6dcb2008-11-12 01:19:571365}
1366
[email protected]0c9406632013-02-08 01:13:331367bool Browser::ShouldFocusLocationBarByDefault(WebContents* source) {
[email protected]77fd82e52013-03-12 22:24:261368 const content::NavigationEntry* entry =
1369 source->GetController().GetActiveEntry();
[email protected]e4916412013-04-25 20:12:151370 if (entry) {
1371 GURL url = entry->GetURL();
1372 GURL virtual_url = entry->GetVirtualURL();
1373 if ((url.SchemeIs(chrome::kChromeUIScheme) &&
1374 url.host() == chrome::kChromeUINewTabHost) ||
1375 (virtual_url.SchemeIs(chrome::kChromeUIScheme) &&
1376 virtual_url.host() == chrome::kChromeUINewTabHost)) {
1377 return true;
1378 }
[email protected]c744f7d22013-04-12 20:29:041379 }
1380
[email protected]165fe422013-03-27 06:34:031381 return chrome::NavEntryIsInstantNTP(source, entry);
[email protected]0c9406632013-02-08 01:13:331382}
1383
[email protected]a26dc362010-04-23 01:48:581384void Browser::SetFocusToLocationBar(bool select_all) {
[email protected]1a380622009-02-17 17:33:131385 // Two differences between this and FocusLocationBar():
1386 // (1) This doesn't get recorded in user metrics, since it's called
1387 // internally.
1388 // (2) This checks whether the location bar can be focused, and if not, clears
1389 // the focus. FocusLocationBar() is only reached when the location bar is
1390 // focusable, but this may be reached at other times, e.g. while in
1391 // fullscreen mode, where we need to leave focus in a consistent state.
[email protected]a26dc362010-04-23 01:48:581392 window_->SetFocusToLocationBar(select_all);
[email protected]4bcefee2009-01-29 15:57:571393}
1394
[email protected]7d5925a2009-03-03 02:59:121395void Browser::RenderWidgetShowing() {
1396 window_->DisableInactiveFrame();
1397}
[email protected]a239c3f2009-02-17 22:13:191398
[email protected]0a2aeb82009-05-15 21:52:481399int Browser::GetExtraRenderViewHeight() const {
1400 return window_->GetExtraRenderViewHeight();
1401}
1402
[email protected]2a6bc3e2011-12-28 23:51:331403void Browser::ViewSourceForTab(WebContents* source, const GURL& page_url) {
[email protected]1788e772010-12-15 16:40:501404 DCHECK(source);
[email protected]ab93b6372012-11-28 05:20:221405 chrome::ViewSource(this, source);
[email protected]77d8d622010-12-15 10:30:121406}
1407
[email protected]2a6bc3e2011-12-28 23:51:331408void Browser::ViewSourceForFrame(WebContents* source,
[email protected]932b7a12011-03-09 12:50:271409 const GURL& frame_url,
1410 const std::string& frame_content_state) {
1411 DCHECK(source);
[email protected]ab93b6372012-11-28 05:20:221412 chrome::ViewSource(this, source, frame_url, frame_content_state);
[email protected]932b7a12011-03-09 12:50:271413}
1414
[email protected]4fdf6742012-01-10 20:14:361415void Browser::ShowRepostFormWarningDialog(WebContents* source) {
[email protected]51296c62012-09-26 13:02:441416 TabModalConfirmDialog::Create(new RepostFormWarningController(source),
[email protected]70320ab2012-11-02 19:07:161417 source);
[email protected]14f3408a2009-08-31 20:53:531418}
1419
[email protected]4fdf6742012-01-10 20:14:361420bool Browser::ShouldCreateWebContents(
1421 WebContents* web_contents,
1422 int route_id,
1423 WindowContainerType window_container_type,
[email protected]03b6d552012-03-29 04:03:011424 const string16& frame_name,
1425 const GURL& target_url) {
[email protected]4fdf6742012-01-10 20:14:361426 if (window_container_type == WINDOW_CONTAINER_TYPE_BACKGROUND) {
1427 // If a BackgroundContents is created, suppress the normal WebContents.
1428 return !MaybeCreateBackgroundContents(
[email protected]03b6d552012-03-29 04:03:011429 route_id, web_contents, frame_name, target_url);
[email protected]4fdf6742012-01-10 20:14:361430 }
1431
1432 return true;
1433}
1434
1435void Browser::WebContentsCreated(WebContents* source_contents,
1436 int64 source_frame_id,
[email protected]50de3222013-03-20 15:36:131437 const string16& frame_name,
[email protected]4fdf6742012-01-10 20:14:361438 const GURL& target_url,
1439 WebContents* new_contents) {
[email protected]6451fe92012-08-30 18:37:261440 // Adopt the WebContents now, so all observers are in place, as the network
[email protected]3d61dd02012-06-07 00:05:531441 // requests for its initial navigation will start immediately. The WebContents
1442 // will later be inserted into this browser using Browser::Navigate via
[email protected]6451fe92012-08-30 18:37:261443 // AddNewContents.
[email protected]27e5b3912012-12-16 00:45:371444 BrowserTabContents::AttachTabHelpers(new_contents);
[email protected]4fdf6742012-01-10 20:14:361445
1446 // Notify.
1447 RetargetingDetails details;
1448 details.source_web_contents = source_contents;
1449 details.source_frame_id = source_frame_id;
1450 details.target_url = target_url;
1451 details.target_web_contents = new_contents;
1452 details.not_yet_in_tabstrip = true;
1453 content::NotificationService::current()->Notify(
1454 chrome::NOTIFICATION_RETARGETING,
1455 content::Source<Profile>(profile_),
1456 content::Details<RetargetingDetails>(&details));
[email protected]80c7b802011-10-28 19:59:081457}
1458
[email protected]2a6bc3e2011-12-28 23:51:331459void Browser::ContentRestrictionsChanged(WebContents* source) {
[email protected]5d98294912012-06-27 22:57:401460 command_controller_->ContentRestrictionsChanged();
[email protected]9e823662010-10-13 23:36:001461}
1462
[email protected]2a6bc3e2011-12-28 23:51:331463void Browser::RendererUnresponsive(WebContents* source) {
[email protected]a7ed0ca2012-12-06 19:30:061464 // Ignore hangs if a tab is blocked.
1465 int index = tab_strip_model_->GetIndexOfWebContents(source);
1466 DCHECK_NE(TabStripModel::kNoTab, index);
1467 if (tab_strip_model_->IsTabBlocked(index))
1468 return;
[email protected]c19d9542011-11-15 22:26:421469
[email protected]87586fb72012-07-02 13:15:021470 chrome::ShowHungRendererDialog(source);
[email protected]55452902011-06-01 21:57:471471}
1472
[email protected]2a6bc3e2011-12-28 23:51:331473void Browser::RendererResponsive(WebContents* source) {
[email protected]87586fb72012-07-02 13:15:021474 chrome::HideHungRendererDialog(source);
[email protected]55452902011-06-01 21:57:471475}
1476
[email protected]2a6bc3e2011-12-28 23:51:331477void Browser::WorkerCrashed(WebContents* source) {
[email protected]0be09932013-01-08 02:03:501478 SimpleAlertInfoBarDelegate::Create(
1479 InfoBarService::FromWebContents(source), NULL,
1480 l10n_util::GetStringUTF16(IDS_WEBWORKER_CRASHED_PROMPT), true);
[email protected]0dd3a0ab2011-02-18 08:17:441481}
1482
[email protected]1d3e9172012-06-12 02:59:131483void Browser::DidNavigateMainFramePostCommit(WebContents* web_contents) {
[email protected]0d56fa7212012-11-15 23:34:021484 if (web_contents == tab_strip_model_->GetActiveWebContents())
[email protected]09b29342011-06-24 19:18:481485 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]93f230e02011-06-01 14:40:001486}
1487
[email protected]1d3e9172012-06-12 02:59:131488void Browser::DidNavigateToPendingEntry(WebContents* web_contents) {
[email protected]0d56fa7212012-11-15 23:34:021489 if (web_contents == tab_strip_model_->GetActiveWebContents())
[email protected]09b29342011-06-24 19:18:481490 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]93f230e02011-06-01 14:40:001491}
1492
[email protected]71a88bb2013-02-01 22:05:151493content::JavaScriptDialogManager* Browser::GetJavaScriptDialogManager() {
1494 return GetJavaScriptDialogManagerInstance();
[email protected]3ab9cb82011-06-03 18:02:071495}
1496
[email protected]1d3e9172012-06-12 02:59:131497content::ColorChooser* Browser::OpenColorChooser(WebContents* web_contents,
[email protected]da8543762012-03-20 08:52:201498 int color_chooser_id,
[email protected]55578b0a2012-04-18 14:31:321499 SkColor color) {
[email protected]da8543762012-03-20 08:52:201500#if defined(OS_WIN)
1501 // On Windows, only create a color chooser if one doesn't exist, because we
1502 // can't close the old color chooser dialog.
1503 if (!color_chooser_.get())
[email protected]1d3e9172012-06-12 02:59:131504 color_chooser_.reset(content::ColorChooser::Create(color_chooser_id,
1505 web_contents,
[email protected]da8543762012-03-20 08:52:201506 color));
1507#else
1508 if (color_chooser_.get())
1509 color_chooser_->End();
[email protected]1d3e9172012-06-12 02:59:131510 color_chooser_.reset(content::ColorChooser::Create(color_chooser_id,
1511 web_contents,
[email protected]da8543762012-03-20 08:52:201512 color));
1513#endif
1514 return color_chooser_.get();
1515}
1516
1517void Browser::DidEndColorChooser() {
1518 color_chooser_.reset();
1519}
1520
[email protected]1d3e9172012-06-12 02:59:131521void Browser::RunFileChooser(WebContents* web_contents,
[email protected]8caadeb2011-11-22 02:45:231522 const content::FileChooserParams& params) {
[email protected]1d3e9172012-06-12 02:59:131523 FileSelectHelper::RunFileChooser(web_contents, params);
[email protected]3a29a6e2011-08-24 18:26:211524}
1525
[email protected]1d3e9172012-06-12 02:59:131526void Browser::EnumerateDirectory(WebContents* web_contents,
[email protected]33f8ad52012-05-22 18:10:131527 int request_id,
[email protected]650b2d52013-02-10 03:41:451528 const base::FilePath& path) {
[email protected]1d3e9172012-06-12 02:59:131529 FileSelectHelper::EnumerateDirectory(web_contents, request_id, path);
[email protected]3a29a6e2011-08-24 18:26:211530}
1531
[email protected]1d3e9172012-06-12 02:59:131532void Browser::ToggleFullscreenModeForTab(WebContents* web_contents,
[email protected]01a5a762011-11-21 05:40:551533 bool enter_fullscreen) {
[email protected]1d3e9172012-06-12 02:59:131534 fullscreen_controller_->ToggleFullscreenModeForTab(web_contents,
1535 enter_fullscreen);
[email protected]8a5e0ca2011-08-25 06:30:471536}
1537
[email protected]1d3e9172012-06-12 02:59:131538bool Browser::IsFullscreenForTabOrPending(
1539 const WebContents* web_contents) const {
1540 return fullscreen_controller_->IsFullscreenForTabOrPending(web_contents);
[email protected]5d5f7af2011-10-01 01:38:121541}
1542
[email protected]1d3e9172012-06-12 02:59:131543void Browser::JSOutOfMemory(WebContents* web_contents) {
1544 JSOutOfMemoryHelper(web_contents);
[email protected]7d189022011-08-25 22:54:201545}
1546
[email protected]1d3e9172012-06-12 02:59:131547void Browser::RegisterProtocolHandler(WebContents* web_contents,
[email protected]7d189022011-08-25 22:54:201548 const std::string& protocol,
1549 const GURL& url,
[email protected]3a3b75a2012-06-01 08:38:361550 const string16& title,
1551 bool user_gesture) {
[email protected]1d3e9172012-06-12 02:59:131552 RegisterProtocolHandlerHelper(
[email protected]7ae9c322012-07-17 23:46:441553 web_contents, protocol, url, title, user_gesture, window());
[email protected]7d189022011-08-25 22:54:201554}
1555
[email protected]2a6bc3e2011-12-28 23:51:331556void Browser::UpdatePreferredSize(WebContents* source,
[email protected]0548c5352011-09-07 00:33:331557 const gfx::Size& pref_size) {
1558 window_->UpdatePreferredSize(source, pref_size);
1559}
1560
[email protected]61e2b3cc2012-03-02 16:13:341561void Browser::ResizeDueToAutoResize(WebContents* source,
1562 const gfx::Size& new_size) {
1563 window_->ResizeDueToAutoResize(source, new_size);
1564}
1565
[email protected]1d3e9172012-06-12 02:59:131566void Browser::FindReply(WebContents* web_contents,
[email protected]92655362012-04-20 21:33:171567 int request_id,
1568 int number_of_matches,
1569 const gfx::Rect& selection_rect,
1570 int active_match_ordinal,
1571 bool final_update) {
[email protected]1d3e9172012-06-12 02:59:131572 FindReplyHelper(web_contents, request_id, number_of_matches, selection_rect,
[email protected]92655362012-04-20 21:33:171573 active_match_ordinal, final_update);
1574}
1575
[email protected]1d3e9172012-06-12 02:59:131576void Browser::RequestToLockMouse(WebContents* web_contents,
[email protected]a9c81f02012-06-01 00:15:441577 bool user_gesture,
1578 bool last_unlocked_by_target) {
[email protected]1d3e9172012-06-12 02:59:131579 fullscreen_controller_->RequestToLockMouse(web_contents,
[email protected]a9c81f02012-06-01 00:15:441580 user_gesture,
1581 last_unlocked_by_target);
[email protected]55c87fa2011-10-15 07:28:281582}
1583
[email protected]5ef8e372011-10-18 04:25:551584void Browser::LostMouseLock() {
[email protected]01a5a762011-11-21 05:40:551585 fullscreen_controller_->LostMouseLock();
[email protected]5ef8e372011-10-18 04:25:551586}
1587
[email protected]f13b4202012-06-12 23:53:231588void Browser::RequestMediaAccessPermission(
1589 content::WebContents* web_contents,
[email protected]33662e52013-01-07 21:31:091590 const content::MediaStreamRequest& request,
[email protected]f13b4202012-06-12 23:53:231591 const content::MediaResponseCallback& callback) {
[email protected]eb1a18ce2013-04-09 22:28:161592 const extensions::Extension* extension = NULL;
1593 GURL origin(request.security_origin);
1594 if (origin.SchemeIs(extensions::kExtensionScheme)) {
1595 ExtensionService* extensions_service =
1596 extensions::ExtensionSystem::Get(profile_)->extension_service();
1597 extension = extensions_service->extensions()->GetByID(origin.host());
1598 DCHECK(extension);
1599 }
1600
1601 MediaCaptureDevicesDispatcher::GetInstance()->ProcessMediaAccessRequest(
1602 web_contents, request, callback, extension);
[email protected]f13b4202012-06-12 23:53:231603}
1604
[email protected]8c8fc292012-11-23 18:57:161605bool Browser::RequestPpapiBrokerPermission(
1606 WebContents* web_contents,
1607 const GURL& url,
[email protected]650b2d52013-02-10 03:41:451608 const base::FilePath& plugin_path,
[email protected]8c8fc292012-11-23 18:57:161609 const base::Callback<void(bool)>& callback) {
[email protected]0be09932013-01-08 02:03:501610 PepperBrokerInfoBarDelegate::Create(web_contents, url, plugin_path, callback);
[email protected]8c8fc292012-11-23 18:57:161611 return true;
1612}
1613
[email protected]36b6dcb2008-11-12 01:19:571614///////////////////////////////////////////////////////////////////////////////
[email protected]51d83bdf2011-12-09 16:09:291615// Browser, CoreTabHelperDelegate implementation:
[email protected]a0366a52011-02-04 20:04:211616
[email protected]ece4aca2012-09-19 22:22:581617void Browser::SwapTabContents(content::WebContents* old_contents,
1618 content::WebContents* new_contents) {
1619 int index = tab_strip_model_->GetIndexOfWebContents(old_contents);
[email protected]92655362012-04-20 21:33:171620 DCHECK_NE(TabStripModel::kNoTab, index);
[email protected]59fa29fb2012-12-07 22:13:421621 tab_strip_model_->ReplaceWebContentsAt(index, new_contents);
[email protected]92655362012-04-20 21:33:171622}
1623
[email protected]ece4aca2012-09-19 22:22:581624bool Browser::CanReloadContents(content::WebContents* web_contents) const {
[email protected]5d98294912012-06-27 22:57:401625 return chrome::CanReload(this);
[email protected]e1461952012-06-14 21:05:351626}
1627
[email protected]ece4aca2012-09-19 22:22:581628bool Browser::CanSaveContents(content::WebContents* web_contents) const {
[email protected]5d98294912012-06-27 22:57:401629 return chrome::CanSavePage(this);
[email protected]e1461952012-06-14 21:05:351630}
1631
[email protected]92655362012-04-20 21:33:171632///////////////////////////////////////////////////////////////////////////////
1633// Browser, SearchEngineTabHelperDelegate implementation:
1634
[email protected]3613347d2012-04-27 20:27:371635void Browser::ConfirmAddSearchProvider(TemplateURL* template_url,
[email protected]92655362012-04-20 21:33:171636 Profile* profile) {
1637 window()->ConfirmAddSearchProvider(template_url, profile);
1638}
1639
1640///////////////////////////////////////////////////////////////////////////////
[email protected]e41d0082013-05-16 04:37:541641// Browser, web_modal::WebContentsModalDialogManagerDelegate implementation:
[email protected]92655362012-04-20 21:33:171642
[email protected]c5731a1c2012-12-22 10:18:451643void Browser::SetWebContentsBlocked(content::WebContents* web_contents,
1644 bool blocked) {
[email protected]e41d0082013-05-16 04:37:541645 ChromeWebModalDialogManagerDelegate::SetWebContentsBlocked(web_contents,
1646 blocked);
[email protected]c6464ad2012-10-03 00:27:281647 int index = tab_strip_model_->GetIndexOfWebContents(web_contents);
[email protected]92655362012-04-20 21:33:171648 if (index == TabStripModel::kNoTab) {
1649 NOTREACHED();
1650 return;
1651 }
[email protected]2fc15ae2012-05-06 00:01:371652 tab_strip_model_->SetTabBlocked(index, blocked);
[email protected]0d56fa7212012-11-15 23:34:021653 if (!blocked && tab_strip_model_->GetActiveWebContents() == web_contents)
[email protected]f3615f02013-02-26 06:09:061654 web_contents->GetView()->Focus();
[email protected]92655362012-04-20 21:33:171655}
1656
[email protected]e41d0082013-05-16 04:37:541657web_modal::WebContentsModalDialogHost*
1658Browser::GetWebContentsModalDialogHost() {
[email protected]7519c522013-04-02 01:07:231659 return window_->GetWebContentsModalDialogHost();
[email protected]6a151762012-10-03 22:33:191660}
1661
[email protected]92655362012-04-20 21:33:171662///////////////////////////////////////////////////////////////////////////////
1663// Browser, BlockedContentTabHelperDelegate implementation:
1664
[email protected]6790f95f2012-09-21 19:49:451665content::WebContents* Browser::GetConstrainingWebContents(
1666 content::WebContents* source) {
[email protected]92655362012-04-20 21:33:171667 return source;
1668}
1669
1670///////////////////////////////////////////////////////////////////////////////
1671// Browser, BookmarkTabHelperDelegate implementation:
1672
[email protected]1c5119c2012-09-19 00:08:571673void Browser::URLStarredChanged(content::WebContents* web_contents,
1674 bool starred) {
[email protected]0d56fa7212012-11-15 23:34:021675 if (web_contents == tab_strip_model_->GetActiveWebContents())
[email protected]92655362012-04-20 21:33:171676 window_->SetStarredState(starred);
1677}
1678
1679///////////////////////////////////////////////////////////////////////////////
[email protected]6f80e932012-06-04 19:00:071680// Browser, ZoomObserver implementation:
1681
[email protected]dea9cee82012-09-25 03:10:521682void Browser::OnZoomChanged(content::WebContents* source,
1683 bool can_show_bubble) {
[email protected]0d56fa7212012-11-15 23:34:021684 if (source == tab_strip_model_->GetActiveWebContents()) {
[email protected]6f80e932012-06-04 19:00:071685 // Only show the zoom bubble for zoom changes in the active window.
[email protected]5423c372012-08-22 05:50:161686 window_->ZoomChangedForActiveTab(can_show_bubble && window_->IsActive());
[email protected]6f80e932012-06-04 19:00:071687 }
1688}
1689
1690///////////////////////////////////////////////////////////////////////////////
[email protected]92f54082012-07-31 01:43:141691// Browser, ui::SelectFileDialog::Listener implementation:
[email protected]36b6dcb2008-11-12 01:19:571692
[email protected]650b2d52013-02-10 03:41:451693void Browser::FileSelected(const base::FilePath& path, int index,
1694 void* params) {
[email protected]53f04c82012-07-26 02:31:091695 FileSelectedWithExtraInfo(ui::SelectedFileInfo(path, path), index, params);
1696}
1697
[email protected]a573bc32013-04-09 15:09:541698void Browser::FileSelectedWithExtraInfo(const ui::SelectedFileInfo& file_info,
1699 int index,
1700 void* params) {
[email protected]53f04c82012-07-26 02:31:091701 profile_->set_last_selected_directory(file_info.file_path.DirName());
1702
[email protected]a573bc32013-04-09 15:09:541703 GURL url = net::FilePathToFileURL(file_info.local_path);
[email protected]b7813a22012-04-04 18:41:021704
1705#if defined(OS_CHROMEOS)
[email protected]a573bc32013-04-09 15:09:541706 drive::util::MaybeSetDriveURL(profile_, file_info.file_path, &url);
[email protected]b7813a22012-04-04 18:41:021707#endif
1708
[email protected]a573bc32013-04-09 15:09:541709 if (url.is_empty())
[email protected]e5d549d2011-12-28 01:29:201710 return;
1711
1712 OpenURL(OpenURLParams(
[email protected]a573bc32013-04-09 15:09:541713 url, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
[email protected]36b6dcb2008-11-12 01:19:571714}
1715
1716///////////////////////////////////////////////////////////////////////////////
[email protected]6c2381d2011-10-19 02:52:531717// Browser, content::NotificationObserver implementation:
[email protected]36b6dcb2008-11-12 01:19:571718
[email protected]432115822011-07-10 15:52:271719void Browser::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:531720 const content::NotificationSource& source,
1721 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:271722 switch (type) {
[email protected]432115822011-07-10 15:52:271723 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
[email protected]ce696a452011-05-31 17:47:361724 if (window()->GetLocationBar())
1725 window()->GetLocationBar()->UpdatePageActions();
[email protected]57f71b92009-09-11 19:31:381726
[email protected]8a39b472013-04-04 17:07:421727 const extensions::UnloadedExtensionInfo* extension_info =
1728 content::Details<extensions::UnloadedExtensionInfo>(details).ptr();
1729
[email protected]b3f7fe22011-11-11 19:27:561730 // Close any tabs from the unloaded extension, unless it's terminated,
1731 // in which case let the sad tabs remain.
[email protected]8a39b472013-04-04 17:07:421732 if (extension_info->reason != extension_misc::UNLOAD_REASON_TERMINATE) {
1733 const Extension* extension = extension_info->extension;
[email protected]e344b5a2012-11-27 07:04:351734 // Iterate backwards as we may remove items while iterating.
[email protected]2fc15ae2012-05-06 00:01:371735 for (int i = tab_strip_model_->count() - 1; i >= 0; --i) {
[email protected]0d56fa7212012-11-15 23:34:021736 WebContents* web_contents = tab_strip_model_->GetWebContentsAt(i);
[email protected]e344b5a2012-11-27 07:04:351737 // Two cases are handled here:
1738 // - The scheme check is for when an extension page is loaded in a
1739 // tab, e.g. chrome-extension://id/page.html.
1740 // - The extension_app check is for apps, which can have non-extension
1741 // schemes, e.g. https://mail.google.com if you have the Gmail app
1742 // installed.
1743 if ((web_contents->GetURL().SchemeIs(extensions::kExtensionScheme) &&
1744 web_contents->GetURL().host() == extension->id()) ||
1745 (extensions::TabHelper::FromWebContents(
1746 web_contents)->extension_app() == extension)) {
[email protected]526b45e2012-11-28 20:22:461747 tab_strip_model_->CloseWebContentsAt(i, TabStripModel::CLOSE_NONE);
[email protected]e344b5a2012-11-27 07:04:351748 }
[email protected]894bb502009-05-21 22:39:571749 }
1750 }
1751 break;
1752 }
1753
[email protected]432115822011-07-10 15:52:271754 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:531755 Profile* profile = content::Source<Profile>(source).ptr();
[email protected]fe8944f2011-08-30 17:47:321756 if (profile_->IsSameProfile(profile) && window()->GetLocationBar())
[email protected]ce696a452011-05-31 17:47:361757 window()->GetLocationBar()->InvalidatePageActions();
[email protected]371ed7a2009-08-25 15:22:461758 break;
1759 }
1760
[email protected]432115822011-07-10 15:52:271761 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED:
1762 case chrome::NOTIFICATION_EXTENSION_LOADED:
[email protected]1797f4a2011-08-30 18:00:281763 // During window creation on Windows we may end up calling into
1764 // SHAppBarMessage, which internally spawns a nested message loop. This
1765 // makes it possible for us to end up here before window creation has
[email protected]8a39b472013-04-04 17:07:421766 // completed, at which point window_ is NULL. See 94752 for details.
[email protected]1797f4a2011-08-30 18:00:281767 if (window() && window()->GetLocationBar())
[email protected]ce696a452011-05-31 17:47:361768 window()->GetLocationBar()->UpdatePageActions();
[email protected]0dfe05c2011-02-23 23:03:361769 break;
[email protected]ad0c2e1b2010-01-30 00:00:101770
[email protected]6b40bb582012-03-15 20:50:381771#if defined(ENABLE_THEMES)
[email protected]432115822011-07-10 15:52:271772 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED:
[email protected]e001d412009-06-26 20:53:251773 window()->UserChangedTheme();
1774 break;
[email protected]6b40bb582012-03-15 20:50:381775#endif
[email protected]e001d412009-06-26 20:53:251776
[email protected]ea049a02011-12-25 21:37:091777 case chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED: {
1778 WebContents* web_contents = content::Source<WebContents>(source).ptr();
[email protected]0d56fa7212012-11-15 23:34:021779 if (web_contents == tab_strip_model_->GetActiveWebContents()) {
[email protected]9a9a9e22011-05-13 22:20:351780 LocationBar* location_bar = window()->GetLocationBar();
1781 if (location_bar)
1782 location_bar->UpdateContentSettingsIcons();
1783 }
1784 break;
1785 }
1786
[email protected]e83f1682008-09-07 23:57:401787 default:
1788 NOTREACHED() << "Got a notification we didn't register for.";
initial.commit09911bf2008-07-26 23:55:291789 }
1790}
1791
[email protected]165fe422013-03-27 06:34:031792void Browser::ModelChanged(const SearchModel::State& old_state,
1793 const SearchModel::State& new_state) {
1794 if (SearchModel::ShouldChangeTopBarsVisibility(old_state, new_state))
[email protected]5ee671f2013-03-19 11:23:051795 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]c3166302012-09-25 21:27:081796}
1797
[email protected]d3b98c82010-07-14 07:45:591798///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:571799// Browser, Command and state updating (private):
1800
[email protected]c31813a2012-11-15 18:47:261801void Browser::OnDevToolsDisabledChanged() {
1802 if (profile_->GetPrefs()->GetBoolean(prefs::kDevToolsDisabled))
1803 content::DevToolsManager::GetInstance()->CloseAllClientHosts();
1804}
1805
1806void Browser::MarkHomePageAsChanged() {
1807 profile_->GetPrefs()->SetBoolean(prefs::kHomePageChanged, true);
[email protected]6c6b02d2011-09-02 03:36:471808}
1809
[email protected]36b6dcb2008-11-12 01:19:571810///////////////////////////////////////////////////////////////////////////////
1811// Browser, UI update coalescing and handling (private):
1812
1813void Browser::UpdateToolbar(bool should_restore_state) {
[email protected]36ad4aa82012-11-21 19:20:481814 window_->UpdateToolbar(tab_strip_model_->GetActiveWebContents(),
[email protected]52877dbc62012-06-29 22:22:031815 should_restore_state);
initial.commit09911bf2008-07-26 23:55:291816}
1817
[email protected]8e707792012-11-13 10:32:121818void Browser::UpdateSearchState(WebContents* contents) {
[email protected]165fe422013-03-27 06:34:031819 if (chrome::IsInstantExtendedAPIEnabled())
[email protected]8e707792012-11-13 10:32:121820 search_delegate_->OnTabActivated(contents);
[email protected]e3a5d5b2012-06-22 17:56:461821}
1822
[email protected]2a6bc3e2011-12-28 23:51:331823void Browser::ScheduleUIUpdate(const WebContents* source,
[email protected]36b6dcb2008-11-12 01:19:571824 unsigned changed_flags) {
[email protected]a5231662009-09-26 00:56:001825 if (!source)
1826 return;
1827
[email protected]2b4355c2009-04-04 17:55:461828 // Do some synchronous updates.
[email protected]d9083482012-01-06 00:38:461829 if (changed_flags & content::INVALIDATE_TYPE_URL &&
[email protected]0d56fa7212012-11-15 23:34:021830 source == tab_strip_model_->GetActiveWebContents()) {
[email protected]36b6dcb2008-11-12 01:19:571831 // Only update the URL for the current tab. Note that we do not update
1832 // the navigation commands since those would have already been updated
1833 // synchronously by NavigationStateChanged.
1834 UpdateToolbar(false);
[email protected]d9083482012-01-06 00:38:461835 changed_flags &= ~content::INVALIDATE_TYPE_URL;
[email protected]36b6dcb2008-11-12 01:19:571836 }
[email protected]d9083482012-01-06 00:38:461837 if (changed_flags & content::INVALIDATE_TYPE_LOAD) {
[email protected]6ebdc9b2010-09-27 16:55:571838 // Update the loading state synchronously. This is so the throbber will
1839 // immediately start/stop, which gives a more snappy feel. We want to do
[email protected]a5231662009-09-26 00:56:001840 // this for any tab so they start & stop quickly.
[email protected]526b45e2012-11-28 20:22:461841 tab_strip_model_->UpdateWebContentsStateAt(
[email protected]0d56fa7212012-11-15 23:34:021842 tab_strip_model_->GetIndexOfWebContents(source),
[email protected]6ebdc9b2010-09-27 16:55:571843 TabStripModelObserver::LOADING_ONLY);
[email protected]d9083482012-01-06 00:38:461844 // The status bubble needs to be updated during INVALIDATE_TYPE_LOAD too,
1845 // but we do that asynchronously by not stripping INVALIDATE_TYPE_LOAD from
[email protected]6ebdc9b2010-09-27 16:55:571846 // changed_flags.
[email protected]8030f012009-09-25 18:09:371847 }
1848
[email protected]d9083482012-01-06 00:38:461849 if (changed_flags & content::INVALIDATE_TYPE_TITLE && !source->IsLoading()) {
[email protected]f1cd5e82009-10-23 17:19:031850 // To correctly calculate whether the title changed while not loading
1851 // we need to process the update synchronously. This state only matters for
1852 // the TabStripModel, so we notify the TabStripModel now and notify others
1853 // asynchronously.
[email protected]526b45e2012-11-28 20:22:461854 tab_strip_model_->UpdateWebContentsStateAt(
[email protected]0d56fa7212012-11-15 23:34:021855 tab_strip_model_->GetIndexOfWebContents(source),
[email protected]f1cd5e82009-10-23 17:19:031856 TabStripModelObserver::TITLE_NOT_LOADING);
1857 }
1858
[email protected]2b4355c2009-04-04 17:55:461859 // If the only updates were synchronously handled above, we're done.
[email protected]8030f012009-09-25 18:09:371860 if (changed_flags == 0)
[email protected]2b4355c2009-04-04 17:55:461861 return;
[email protected]36b6dcb2008-11-12 01:19:571862
1863 // Save the dirty bits.
[email protected]85354982009-09-25 19:58:401864 scheduled_updates_[source] |= changed_flags;
[email protected]36b6dcb2008-11-12 01:19:571865
[email protected]24f1477532011-11-22 22:55:361866 if (!chrome_updater_factory_.HasWeakPtrs()) {
[email protected]36b6dcb2008-11-12 01:19:571867 // No task currently scheduled, start another.
[email protected]3264d162013-05-08 22:02:111868 base::MessageLoop::current()->PostDelayedTask(
[email protected]0586b0e2010-02-12 21:38:371869 FROM_HERE,
[email protected]24f1477532011-11-22 22:55:361870 base::Bind(&Browser::ProcessPendingUIUpdates,
1871 chrome_updater_factory_.GetWeakPtr()),
[email protected]11d6ab1a2012-01-12 21:37:261872 base::TimeDelta::FromMilliseconds(kUIUpdateCoalescingTimeMS));
[email protected]36b6dcb2008-11-12 01:19:571873 }
1874}
1875
1876void Browser::ProcessPendingUIUpdates() {
1877#ifndef NDEBUG
1878 // Validate that all tabs we have pending updates for exist. This is scary
1879 // because the pending list must be kept in sync with any detached or
[email protected]a5231662009-09-26 00:56:001880 // deleted tabs.
[email protected]85354982009-09-25 19:58:401881 for (UpdateMap::const_iterator i = scheduled_updates_.begin();
1882 i != scheduled_updates_.end(); ++i) {
[email protected]36b6dcb2008-11-12 01:19:571883 bool found = false;
[email protected]e0448872013-01-11 19:35:021884 for (int tab = 0; tab < tab_strip_model_->count(); tab++) {
[email protected]0d56fa7212012-11-15 23:34:021885 if (tab_strip_model_->GetWebContentsAt(tab) == i->first) {
[email protected]36b6dcb2008-11-12 01:19:571886 found = true;
1887 break;
1888 }
1889 }
1890 DCHECK(found);
1891 }
1892#endif
1893
[email protected]24f1477532011-11-22 22:55:361894 chrome_updater_factory_.InvalidateWeakPtrs();
[email protected]36b6dcb2008-11-12 01:19:571895
[email protected]85354982009-09-25 19:58:401896 for (UpdateMap::const_iterator i = scheduled_updates_.begin();
1897 i != scheduled_updates_.end(); ++i) {
[email protected]36b6dcb2008-11-12 01:19:571898 // Do not dereference |contents|, it may be out-of-date!
[email protected]2a6bc3e2011-12-28 23:51:331899 const WebContents* contents = i->first;
[email protected]85354982009-09-25 19:58:401900 unsigned flags = i->second;
[email protected]36b6dcb2008-11-12 01:19:571901
[email protected]0d56fa7212012-11-15 23:34:021902 if (contents == tab_strip_model_->GetActiveWebContents()) {
[email protected]a5231662009-09-26 00:56:001903 // Updates that only matter when the tab is selected go here.
[email protected]f7f3a5f2009-05-01 22:02:341904
[email protected]d9083482012-01-06 00:38:461905 if (flags & content::INVALIDATE_TYPE_PAGE_ACTIONS) {
[email protected]9800de5e2011-03-11 18:19:491906 LocationBar* location_bar = window()->GetLocationBar();
1907 if (location_bar)
1908 location_bar->UpdatePageActions();
1909 }
[email protected]6ebdc9b2010-09-27 16:55:571910 // Updating the URL happens synchronously in ScheduleUIUpdate.
[email protected]d9083482012-01-06 00:38:461911 if (flags & content::INVALIDATE_TYPE_LOAD && GetStatusBubble()) {
[email protected]0d56fa7212012-11-15 23:34:021912 GetStatusBubble()->SetStatus(CoreTabHelper::FromWebContents(
1913 tab_strip_model_->GetActiveWebContents())->GetStatusText());
[email protected]0dd3a0ab2011-02-18 08:17:441914 }
[email protected]a5231662009-09-26 00:56:001915
[email protected]d9083482012-01-06 00:38:461916 if (flags & (content::INVALIDATE_TYPE_TAB |
1917 content::INVALIDATE_TYPE_TITLE)) {
[email protected]a5231662009-09-26 00:56:001918 window_->UpdateTitleBar();
1919 }
[email protected]4d34e2e2009-05-26 22:55:281920 }
[email protected]36b6dcb2008-11-12 01:19:571921
[email protected]a5231662009-09-26 00:56:001922 // Updates that don't depend upon the selected state go here.
[email protected]d9083482012-01-06 00:38:461923 if (flags &
1924 (content::INVALIDATE_TYPE_TAB | content::INVALIDATE_TYPE_TITLE)) {
[email protected]526b45e2012-11-28 20:22:461925 tab_strip_model_->UpdateWebContentsStateAt(
[email protected]e52d0a42012-06-08 22:44:161926 tab_strip_model_->GetIndexOfWebContents(contents),
[email protected]f1cd5e82009-10-23 17:19:031927 TabStripModelObserver::ALL);
[email protected]36b6dcb2008-11-12 01:19:571928 }
1929
[email protected]13155a22013-03-19 23:33:541930 // Update the bookmark bar. It may happen that the tab is crashed, and if
1931 // so, the bookmark bar should be hidden.
1932 if (flags & content::INVALIDATE_TYPE_TAB)
1933 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
1934
[email protected]36b6dcb2008-11-12 01:19:571935 // We don't need to process INVALIDATE_STATE, since that's not visible.
1936 }
1937
1938 scheduled_updates_.clear();
1939}
1940
[email protected]d9083482012-01-06 00:38:461941void Browser::RemoveScheduledUpdatesFor(WebContents* contents) {
[email protected]36b6dcb2008-11-12 01:19:571942 if (!contents)
1943 return;
1944
[email protected]85354982009-09-25 19:58:401945 UpdateMap::iterator i = scheduled_updates_.find(contents);
1946 if (i != scheduled_updates_.end())
1947 scheduled_updates_.erase(i);
initial.commit09911bf2008-07-26 23:55:291948}
1949
[email protected]36b6dcb2008-11-12 01:19:571950///////////////////////////////////////////////////////////////////////////////
1951// Browser, Getters for UI (private):
initial.commit09911bf2008-07-26 23:55:291952
[email protected]36b6dcb2008-11-12 01:19:571953StatusBubble* Browser::GetStatusBubble() {
[email protected]8e837ec2013-01-31 01:48:331954 // In kiosk and exclusive app mode, we want to always hide the status bubble.
1955 if (chrome::IsRunningInAppMode())
[email protected]34930432009-11-09 00:12:091956 return NULL;
[email protected]8e837ec2013-01-31 01:48:331957
[email protected]9b032bf2009-07-21 17:34:231958 return window_ ? window_->GetStatusBubble() : NULL;
initial.commit09911bf2008-07-26 23:55:291959}
1960
[email protected]36b6dcb2008-11-12 01:19:571961///////////////////////////////////////////////////////////////////////////////
1962// Browser, Session restore functions (private):
initial.commit09911bf2008-07-26 23:55:291963
1964void Browser::SyncHistoryWithTabs(int index) {
[email protected]92371eb2011-04-28 11:50:151965 SessionService* session_service =
1966 SessionServiceFactory::GetForProfileIfExisting(profile());
initial.commit09911bf2008-07-26 23:55:291967 if (session_service) {
[email protected]e0448872013-01-11 19:35:021968 for (int i = index; i < tab_strip_model_->count(); ++i) {
[email protected]0d56fa7212012-11-15 23:34:021969 WebContents* web_contents = tab_strip_model_->GetWebContentsAt(i);
[email protected]50a9a76b2012-09-12 14:19:361970 if (web_contents) {
1971 SessionTabHelper* session_tab_helper =
1972 SessionTabHelper::FromWebContents(web_contents);
initial.commit09911bf2008-07-26 23:55:291973 session_service->SetTabIndexInWindow(
[email protected]50a9a76b2012-09-12 14:19:361974 session_id(), session_tab_helper->session_id(), i);
[email protected]ecc523f62010-09-28 22:45:401975 session_service->SetPinnedState(
1976 session_id(),
[email protected]50a9a76b2012-09-12 14:19:361977 session_tab_helper->session_id(),
[email protected]52877dbc62012-06-29 22:22:031978 tab_strip_model_->IsTabPinned(i));
initial.commit09911bf2008-07-26 23:55:291979 }
1980 }
1981 }
1982}
1983
[email protected]36b6dcb2008-11-12 01:19:571984///////////////////////////////////////////////////////////////////////////////
[email protected]69444cc2009-04-09 20:40:061985// Browser, In-progress download termination handling (private):
1986
[email protected]b6a4ac2b2011-10-17 20:05:481987bool Browser::CanCloseWithInProgressDownloads() {
[email protected]5a17d492011-10-24 19:37:031988 // If we've prompted, we need to hear from the user before we
1989 // can close.
1990 if (cancel_download_confirmation_state_ != NOT_PROMPTED)
1991 return cancel_download_confirmation_state_ != WAITING_FOR_RESPONSE;
1992
1993 int num_downloads_blocking;
1994 if (DOWNLOAD_CLOSE_OK ==
1995 OkToCloseWithInProgressDownloads(&num_downloads_blocking))
[email protected]446295a2010-04-19 23:43:001996 return true;
1997
[email protected]5a17d492011-10-24 19:37:031998 // Closing this window will kill some downloads; prompt to make sure
1999 // that's ok.
[email protected]69444cc2009-04-09 20:40:062000 cancel_download_confirmation_state_ = WAITING_FOR_RESPONSE;
2001 window_->ConfirmBrowserCloseWithPendingDownloads();
2002
2003 // Return false so the browser does not close. We'll close if the user
2004 // confirms in the dialog.
2005 return false;
2006}
2007
2008///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:572009// Browser, Assorted utility functions (private):
initial.commit09911bf2008-07-26 23:55:292010
[email protected]27e5b3912012-12-16 00:45:372011void Browser::SetAsDelegate(WebContents* web_contents, Browser* delegate) {
2012 // WebContents...
2013 web_contents->SetDelegate(delegate);
2014
2015 // ...and all the helpers.
2016 BlockedContentTabHelper::FromWebContents(web_contents)->
2017 set_delegate(delegate);
2018 BookmarkTabHelper::FromWebContents(web_contents)->set_delegate(delegate);
[email protected]d2b16572013-01-03 00:41:582019 WebContentsModalDialogManager::FromWebContents(web_contents)->
[email protected]27e5b3912012-12-16 00:45:372020 set_delegate(delegate);
2021 CoreTabHelper::FromWebContents(web_contents)->set_delegate(delegate);
2022 SearchEngineTabHelper::FromWebContents(web_contents)->set_delegate(delegate);
2023 ZoomController::FromWebContents(web_contents)->set_observer(delegate);
2024}
2025
[email protected]36b6dcb2008-11-12 01:19:572026void Browser::CloseFrame() {
2027 window_->Close();
initial.commit09911bf2008-07-26 23:55:292028}
2029
[email protected]e89cfcb2012-11-11 14:47:242030void Browser::TabDetachedAtImpl(content::WebContents* contents,
2031 int index,
[email protected]c37e3b62010-01-22 00:15:172032 DetachType type) {
2033 if (type == DETACH_TYPE_DETACH) {
[email protected]56580852010-11-17 16:09:152034 // Save the current location bar state, but only if the tab being detached
2035 // is the selected tab. Because saving state can conditionally revert the
2036 // location bar, saving the current tab's location bar state to a
2037 // non-selected tab can corrupt both tabs.
[email protected]0d56fa7212012-11-15 23:34:022038 if (contents == tab_strip_model_->GetActiveWebContents()) {
[email protected]9800de5e2011-03-11 18:19:492039 LocationBar* location_bar = window()->GetLocationBar();
2040 if (location_bar)
[email protected]e89cfcb2012-11-11 14:47:242041 location_bar->SaveStateToContents(contents);
[email protected]9800de5e2011-03-11 18:19:492042 }
[email protected]c37e3b62010-01-22 00:15:172043
[email protected]2fc15ae2012-05-06 00:01:372044 if (!tab_strip_model_->closing_all())
[email protected]c37e3b62010-01-22 00:15:172045 SyncHistoryWithTabs(0);
2046 }
2047
[email protected]e89cfcb2012-11-11 14:47:242048 SetAsDelegate(contents, NULL);
2049 RemoveScheduledUpdatesFor(contents);
[email protected]c37e3b62010-01-22 00:15:172050
[email protected]04e3ead52013-01-14 01:09:202051 if (find_bar_controller_.get() && index == tab_strip_model_->active_index()) {
[email protected]ba3d09a12012-11-07 12:13:522052 find_bar_controller_->ChangeWebContents(NULL);
[email protected]ecc523f62010-09-28 22:45:402053 }
[email protected]c37e3b62010-01-22 00:15:172054
[email protected]e3a5d5b2012-06-22 17:56:462055 // Stop observing search model changes for this tab.
[email protected]e89cfcb2012-11-11 14:47:242056 search_delegate_->OnTabDetached(contents);
[email protected]e3a5d5b2012-06-22 17:56:462057
[email protected]20ca0382013-02-28 19:50:072058 for (size_t i = 0; i < interstitial_observers_.size(); i++) {
2059 if (interstitial_observers_[i]->web_contents() != contents)
2060 continue;
2061
2062 delete interstitial_observers_[i];
2063 interstitial_observers_.erase(interstitial_observers_.begin() + i);
2064 return;
2065 }
[email protected]c37e3b62010-01-22 00:15:172066}
2067
[email protected]92655362012-04-20 21:33:172068bool Browser::SupportsWindowFeatureImpl(WindowFeature feature,
2069 bool check_fullscreen) const {
[email protected]c3e32942012-12-11 10:26:202070 bool hide_ui_for_fullscreen = check_fullscreen && ShouldHideUIForFullscreen();
[email protected]92655362012-04-20 21:33:172071
2072 unsigned int features = FEATURE_INFOBAR | FEATURE_DOWNLOADSHELF;
2073
2074 if (is_type_tabbed())
2075 features |= FEATURE_BOOKMARKBAR;
2076
2077 if (!hide_ui_for_fullscreen) {
2078 if (!is_type_tabbed())
2079 features |= FEATURE_TITLEBAR;
2080
2081 if (is_type_tabbed())
2082 features |= FEATURE_TABSTRIP;
2083
2084 if (is_type_tabbed())
2085 features |= FEATURE_TOOLBAR;
2086
2087 if (!is_app())
2088 features |= FEATURE_LOCATIONBAR;
2089 }
2090 return !!(features & feature);
2091}
2092
[email protected]09b29342011-06-24 19:18:482093void Browser::UpdateBookmarkBarState(BookmarkBarStateChangeReason reason) {
2094 BookmarkBar::State state;
[email protected]227393e2011-06-29 21:28:392095 // The bookmark bar is hidden in fullscreen mode, unless on the new tab page.
[email protected]3710d0002011-10-11 00:35:002096 if (browser_defaults::bookmarks_enabled &&
2097 profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar) &&
[email protected]c3e32942012-12-11 10:26:202098 !ShouldHideUIForFullscreen()) {
[email protected]04143362012-11-28 14:25:182099 state = BookmarkBar::SHOW;
[email protected]09b29342011-06-24 19:18:482100 } else {
[email protected]0d56fa7212012-11-15 23:34:022101 WebContents* web_contents = tab_strip_model_->GetActiveWebContents();
[email protected]1c5119c2012-09-19 00:08:572102 BookmarkTabHelper* bookmark_tab_helper =
2103 web_contents ? BookmarkTabHelper::FromWebContents(web_contents) : NULL;
2104 if (bookmark_tab_helper && bookmark_tab_helper->ShouldShowBookmarkBar())
[email protected]09b29342011-06-24 19:18:482105 state = BookmarkBar::DETACHED;
2106 else
2107 state = BookmarkBar::HIDDEN;
2108 }
[email protected]2d4e43a02012-06-26 00:11:552109
[email protected]5ee671f2013-03-19 11:23:052110 // Bookmark bar may need to be hidden for |SEARCH_SUGGESTIONS| and
2111 // |SEARCH_RESULTS| modes as per SearchBox API or Instant overlay or if it's
[email protected]aa7388ad2013-04-09 19:44:562112 // detached when origin is not |NTP|.
[email protected]5ee671f2013-03-19 11:23:052113 // TODO(sail): remove conditional MACOSX flag when bookmark bar is actually
2114 // hidden on mac; for now, mac keeps the bookmark bar shown but changes its
2115 // z-order to stack it below contents.
[email protected]e2395da2013-01-23 22:05:352116#if !defined(OS_MACOSX)
[email protected]5ee671f2013-03-19 11:23:052117 if (search_model_->mode().is_search() &&
[email protected]aa7388ad2013-04-09 19:44:562118 ((state == BookmarkBar::DETACHED &&
2119 !search_model_->mode().is_origin_ntp()) ||
2120 !search_model_->top_bars_visible())) {
[email protected]2d4e43a02012-06-26 00:11:552121 state = BookmarkBar::HIDDEN;
[email protected]5ee671f2013-03-19 11:23:052122 }
2123#else
2124 // TODO(sail): remove this when the above block is enabled for mac.
[email protected]aa7388ad2013-04-09 19:44:562125 if (state == BookmarkBar::DETACHED && search_model_->mode().is_search() &&
2126 !search_model_->mode().is_origin_ntp()) {
[email protected]fde71ee2013-03-12 20:21:032127 state = BookmarkBar::HIDDEN;
[email protected]aa7388ad2013-04-09 19:44:562128 }
[email protected]5ee671f2013-03-19 11:23:052129#endif // !defined(OS_MACOSX)
[email protected]fde71ee2013-03-12 20:21:032130
[email protected]09b29342011-06-24 19:18:482131 if (state == bookmark_bar_state_)
2132 return;
2133
2134 bookmark_bar_state_ = state;
2135
2136 if (!window_)
2137 return; // This is called from the constructor when window_ is NULL.
2138
2139 if (reason == BOOKMARK_BAR_STATE_CHANGE_TAB_SWITCH) {
2140 // Don't notify BrowserWindow on a tab switch as at the time this is invoked
2141 // BrowserWindow hasn't yet switched tabs. The BrowserWindow implementations
2142 // end up querying state once they process the tab switch.
2143 return;
2144 }
2145
[email protected]e2395da2013-01-23 22:05:352146 bool shouldAnimate = reason == BOOKMARK_BAR_STATE_CHANGE_PREF_CHANGE;
2147 window_->BookmarkBarStateChanged(shouldAnimate ?
[email protected]09b29342011-06-24 19:18:482148 BookmarkBar::ANIMATE_STATE_CHANGE :
[email protected]e2395da2013-01-23 22:05:352149 BookmarkBar::DONT_ANIMATE_STATE_CHANGE);
[email protected]09b29342011-06-24 19:18:482150}
[email protected]2e2cacc2011-07-12 21:54:262151
[email protected]c3e32942012-12-11 10:26:202152bool Browser::ShouldHideUIForFullscreen() const {
[email protected]6a414ff2013-02-27 08:22:542153 // Windows and GTK remove the top controls in fullscreen, but Mac and Ash
2154 // keep the controls in a slide-down panel.
2155 return window_ && window_->ShouldHideUIForFullscreen();
[email protected]c3e32942012-12-11 10:26:202156}
2157
[email protected]92655362012-04-20 21:33:172158bool Browser::MaybeCreateBackgroundContents(int route_id,
2159 WebContents* opener_web_contents,
2160 const string16& frame_name,
2161 const GURL& target_url) {
2162 GURL opener_url = opener_web_contents->GetURL();
[email protected]06bdd2b2012-11-30 18:47:132163 ExtensionService* extensions_service =
2164 extensions::ExtensionSystem::Get(profile_)->extension_service();
[email protected]92655362012-04-20 21:33:172165
2166 if (!opener_url.is_valid() ||
2167 frame_name.empty() ||
2168 !extensions_service ||
2169 !extensions_service->is_ready())
2170 return false;
2171
2172 // Only hosted apps have web extents, so this ensures that only hosted apps
2173 // can create BackgroundContents. We don't have to check for background
2174 // permission as that is checked in RenderMessageFilter when the CreateWindow
2175 // message is processed.
2176 const Extension* extension =
2177 extensions_service->extensions()->GetHostedAppByURL(
2178 ExtensionURLInfo(opener_url));
2179 if (!extension)
2180 return false;
2181
2182 // No BackgroundContents allowed if BackgroundContentsService doesn't exist.
2183 BackgroundContentsService* service =
2184 BackgroundContentsServiceFactory::GetForProfile(profile_);
2185 if (!service)
2186 return false;
2187
2188 // Ensure that we're trying to open this from the extension's process.
2189 SiteInstance* opener_site_instance = opener_web_contents->GetSiteInstance();
2190 extensions::ProcessMap* process_map = extensions_service->process_map();
2191 if (!opener_site_instance->GetProcess() ||
2192 !process_map->Contains(
2193 extension->id(), opener_site_instance->GetProcess()->GetID())) {
2194 return false;
2195 }
2196
2197 // Only allow a single background contents per app.
[email protected]9367eabc2013-03-01 01:29:292198 bool allow_js_access = extensions::BackgroundInfo::AllowJSAccess(extension);
[email protected]92655362012-04-20 21:33:172199 BackgroundContents* existing =
2200 service->GetAppBackgroundContents(ASCIIToUTF16(extension->id()));
2201 if (existing) {
2202 // For non-scriptable background contents, ignore the request altogether,
2203 // (returning true, so that a regular WebContents isn't created either).
2204 if (!allow_js_access)
2205 return true;
2206 // For scriptable background pages, if one already exists, close it (even
2207 // if it was specified in the manifest).
2208 DLOG(INFO) << "Closing existing BackgroundContents for " << opener_url;
2209 delete existing;
2210 }
2211
2212 // If script access is not allowed, create the the background contents in a
2213 // new SiteInstance, so that a separate process is used.
2214 scoped_refptr<content::SiteInstance> site_instance =
2215 allow_js_access ?
2216 opener_site_instance :
2217 content::SiteInstance::Create(opener_web_contents->GetBrowserContext());
2218
2219 // Passed all the checks, so this should be created as a BackgroundContents.
2220 BackgroundContents* contents = service->CreateBackgroundContents(
2221 site_instance,
2222 route_id,
2223 profile_,
2224 frame_name,
2225 ASCIIToUTF16(extension->id()));
2226
2227 // When a separate process is used, the original renderer cannot access the
2228 // new window later, thus we need to navigate the window now.
2229 if (contents && !allow_js_access) {
2230 contents->web_contents()->GetController().LoadURL(
2231 target_url,
2232 content::Referrer(),
2233 content::PAGE_TRANSITION_LINK,
2234 std::string()); // No extra headers.
2235 }
2236
2237 return contents != NULL;
[email protected]afcb43562011-09-02 23:33:382238}