blob: 340171ac6e6621e1b1d832569b1ed6b2ecc8c964 [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]4f822f022012-12-20 19:11:4231#include "chrome/browser/api/infobars/infobar_service.h"
[email protected]77a91c72012-08-13 16:19:3432#include "chrome/browser/api/infobars/simple_alert_infobar_delegate.h"
[email protected]8e837ec2013-01-31 01:48:3333#include "chrome/browser/app_mode/app_mode_utils.h"
[email protected]ea9edcb02011-09-23 22:05:0434#include "chrome/browser/autofill/personal_data_manager_factory.h"
[email protected]a07676b22011-06-17 16:36:5335#include "chrome/browser/background/background_contents_service.h"
[email protected]4fdf6742012-01-10 20:14:3636#include "chrome/browser/background/background_contents_service_factory.h"
[email protected]a9afddb2009-02-12 17:49:4237#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]b3ac5c82009-10-08 20:56:5438#include "chrome/browser/bookmarks/bookmark_utils.h"
[email protected]5c238752009-06-13 10:29:0739#include "chrome/browser/browser_process.h"
[email protected]c61db1d2009-02-06 03:39:1840#include "chrome/browser/browser_shutdown.h"
[email protected]40d59ce52009-03-06 23:20:1441#include "chrome/browser/character_encoding.h"
[email protected]0f083402011-11-22 02:59:0142#include "chrome/browser/chrome_page_zoom.h"
[email protected]55c87fa2011-10-15 07:28:2843#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]7ae9c322012-07-17 23:46:4444#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]7d189022011-08-25 22:54:2045#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
46#include "chrome/browser/custom_handlers/register_protocol_handler_infobar_delegate.h"
[email protected]70019152012-12-19 11:44:1947#include "chrome/browser/devtools/devtools_toggle_action.h"
48#include "chrome/browser/devtools/devtools_window.h"
[email protected]59560e0b2009-06-04 03:30:2249#include "chrome/browser/download/download_item_model.h"
[email protected]9bb54ee2011-10-12 17:43:3550#include "chrome/browser/download/download_service.h"
51#include "chrome/browser/download/download_service_factory.h"
[email protected]a1bc3b82012-04-19 19:32:2052#include "chrome/browser/download/download_shelf.h"
[email protected]9c1662b2012-03-06 15:44:3353#include "chrome/browser/extensions/browser_extension_window_controller.h"
[email protected]10abd192010-09-30 02:03:4954#include "chrome/browser/extensions/extension_prefs.h"
[email protected]eaa7dd182010-12-14 11:09:0055#include "chrome/browser/extensions/extension_service.h"
[email protected]06bdd2b2012-11-30 18:47:1356#include "chrome/browser/extensions/extension_system.h"
[email protected]a6394ae2012-07-16 20:58:4357#include "chrome/browser/extensions/tab_helper.h"
[email protected]b375c5d2011-05-03 21:15:0458#include "chrome/browser/favicon/favicon_tab_helper.h"
[email protected]3a29a6e2011-08-24 18:26:2159#include "chrome/browser/file_select_helper.h"
[email protected]820735792010-07-29 23:40:0160#include "chrome/browser/first_run/first_run.h"
[email protected]f7578f52010-08-30 22:22:4961#include "chrome/browser/google/google_url_tracker.h"
[email protected]09e9f692013-03-07 23:53:0462#include "chrome/browser/instant/search.h"
[email protected]2e6389f2012-05-18 19:41:2563#include "chrome/browser/lifetime/application_lifetime.h"
[email protected]a239c3f2009-02-17 22:13:1964#include "chrome/browser/net/url_fixer_upper.h"
[email protected]2c4fb7b2011-04-02 06:33:2965#include "chrome/browser/notifications/notification_ui_manager.h"
[email protected]8c8fc292012-11-23 18:57:1666#include "chrome/browser/pepper_broker_infobar_delegate.h"
[email protected]4e94ab32011-08-05 05:28:2767#include "chrome/browser/prefs/incognito_mode_prefs.h"
[email protected]02337592010-09-27 18:38:2568#include "chrome/browser/printing/cloud_print/cloud_print_setup_flow.h"
[email protected]8ecad5e2010-12-02 21:18:3369#include "chrome/browser/profiles/profile.h"
[email protected]5e919242012-02-13 23:59:3570#include "chrome/browser/profiles/profile_destroyer.h"
[email protected]5a17d492011-10-24 19:37:0371#include "chrome/browser/profiles/profile_manager.h"
[email protected]a7be6982011-12-12 21:53:5772#include "chrome/browser/profiles/profile_metrics.h"
[email protected]67baffc82011-12-19 18:03:0773#include "chrome/browser/repost_form_warning_controller.h"
[email protected]85e921fb82009-02-11 23:19:4474#include "chrome/browser/sessions/session_service.h"
[email protected]92371eb2011-04-28 11:50:1575#include "chrome/browser/sessions/session_service_factory.h"
[email protected]bde40b822012-08-30 17:28:5676#include "chrome/browser/sessions/session_tab_helper.h"
[email protected]c61db1d2009-02-06 03:39:1877#include "chrome/browser/sessions/session_types.h"
[email protected]92926d92010-09-02 18:35:0678#include "chrome/browser/sessions/tab_restore_service.h"
[email protected]92371eb2011-04-28 11:50:1579#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]066629f2009-10-17 00:28:1380#include "chrome/browser/sync/profile_sync_service.h"
[email protected]5e61afb92012-01-27 20:09:1381#include "chrome/browser/sync/profile_sync_service_factory.h"
[email protected]40f047972009-11-25 03:54:4082#include "chrome/browser/sync/sync_ui_util.h"
[email protected]2c4fb7b2011-04-02 06:33:2983#include "chrome/browser/tab_contents/background_contents.h"
[email protected]4fdf6742012-01-10 20:14:3684#include "chrome/browser/tab_contents/retargeting_details.h"
[email protected]1ab4ddf2011-07-21 04:48:0485#include "chrome/browser/tab_contents/tab_util.h"
[email protected]ffa6f592011-06-24 22:03:5786#include "chrome/browser/themes/theme_service.h"
87#include "chrome/browser/themes/theme_service_factory.h"
[email protected]71a88bb2013-02-01 22:05:1588#include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_manager.h"
[email protected]27e5b3912012-12-16 00:45:3789#include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h"
[email protected]35699d572011-05-11 19:46:1490#include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
[email protected]5d98294912012-06-27 22:57:4091#include "chrome/browser/ui/browser_command_controller.h"
[email protected]a37d4b02012-06-25 21:56:1092#include "chrome/browser/ui/browser_commands.h"
[email protected]ef557022012-03-16 10:05:3393#include "chrome/browser/ui/browser_content_setting_bubble_model_delegate.h"
[email protected]55452902011-06-01 21:57:4794#include "chrome/browser/ui/browser_dialogs.h"
[email protected]d8748142012-05-16 21:13:4395#include "chrome/browser/ui/browser_finder.h"
[email protected]7acfaf92012-07-11 15:51:5996#include "chrome/browser/ui/browser_instant_controller.h"
[email protected]3539929f2013-02-01 05:59:1497#include "chrome/browser/ui/browser_iterator.h"
[email protected]6768ac02011-04-06 17:41:0498#include "chrome/browser/ui/browser_list.h"
[email protected]fcca741b2011-06-17 22:46:3799#include "chrome/browser/ui/browser_navigator.h"
[email protected]27e5b3912012-12-16 00:45:37100#include "chrome/browser/ui/browser_tab_contents.h"
[email protected]fb7da4132011-03-15 15:51:00101#include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
[email protected]855370052012-07-10 19:30:32102#include "chrome/browser/ui/browser_tab_strip_model_delegate.h"
[email protected]52877dbc62012-06-29 22:22:03103#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]ef557022012-03-16 10:05:33104#include "chrome/browser/ui/browser_toolbar_model_delegate.h"
[email protected]2cd4fde2012-06-26 03:10:26105#include "chrome/browser/ui/browser_ui_prefs.h"
[email protected]00070c732011-04-09 15:31:33106#include "chrome/browser/ui/browser_window.h"
[email protected]5d9cace72012-06-21 16:07:12107#include "chrome/browser/ui/chrome_pages.h"
[email protected]6e1fcd12012-07-02 17:14:20108#include "chrome/browser/ui/chrome_select_file_policy.h"
[email protected]a44657202012-01-09 05:48:31109#include "chrome/browser/ui/extensions/shell_window.h"
[email protected]45300ad42010-12-02 15:51:14110#include "chrome/browser/ui/find_bar/find_bar.h"
111#include "chrome/browser/ui/find_bar/find_bar_controller.h"
[email protected]c90c6ca2011-02-16 20:11:38112#include "chrome/browser/ui/find_bar/find_tab_helper.h"
[email protected]9d5c059a82012-06-29 20:13:07113#include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
[email protected]1ba55cf2012-06-29 19:11:39114#include "chrome/browser/ui/global_error/global_error.h"
115#include "chrome/browser/ui/global_error/global_error_service.h"
116#include "chrome/browser/ui/global_error/global_error_service_factory.h"
[email protected]f13b4202012-06-12 23:53:23117#include "chrome/browser/ui/media_stream_infobar_delegate.h"
[email protected]6a3ec2312010-12-02 19:30:19118#include "chrome/browser/ui/omnibox/location_bar.h"
[email protected]f7bbda82013-02-07 00:32:22119#include "chrome/browser/ui/screen_capture_infobar_delegate.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]d2b16572013-01-03 00:41:58134#include "chrome/browser/ui/web_contents_modal_dialog_manager.h"
[email protected]65549182012-02-25 00:45:40135#include "chrome/browser/ui/webui/signin/login_ui_service.h"
136#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
[email protected]03639422012-06-28 19:03:37137#include "chrome/browser/ui/window_sizer/window_sizer.h"
[email protected]27e5b3912012-12-16 00:45:37138#include "chrome/browser/ui/zoom/zoom_controller.h"
[email protected]b1b73942010-05-26 20:11:54139#include "chrome/browser/upgrade_detector.h"
[email protected]86b54012009-11-19 09:18:50140#include "chrome/browser/web_applications/web_app.h"
[email protected]d938aed92009-01-22 19:49:33141#include "chrome/common/chrome_constants.h"
[email protected]432115822011-07-10 15:52:27142#include "chrome/common/chrome_notification_types.h"
[email protected]d938aed92009-01-22 19:49:33143#include "chrome/common/chrome_switches.h"
[email protected]7d189022011-08-25 22:54:20144#include "chrome/common/custom_handlers/protocol_handler.h"
[email protected]9367eabc2013-03-01 01:29:29145#include "chrome/common/extensions/background_info.h"
[email protected]5b1a0e22009-05-26 19:00:58146#include "chrome/common/extensions/extension.h"
[email protected]46fd1ea42011-02-16 15:59:33147#include "chrome/common/extensions/extension_constants.h"
[email protected]8a53ee042009-01-21 16:41:33148#include "chrome/common/pref_names.h"
[email protected]7664ab32011-02-01 23:35:25149#include "chrome/common/profiling.h"
[email protected]c3917742012-11-21 01:51:14150#include "chrome/common/search_types.h"
[email protected]b5febf72012-07-29 18:10:09151#include "chrome/common/startup_metric_utils.h"
[email protected]bb461532010-11-26 21:50:23152#include "chrome/common/web_apps.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]8a53ee042009-01-21 16:41:33198#endif // OS_WIN
[email protected]e1acf6f2008-10-27 20:43:33199
[email protected]b7969202010-05-14 21:29:26200#if defined(OS_CHROMEOS)
[email protected]15de8142012-10-11 06:00:54201#include "chrome/browser/chromeos/drive/drive_file_system_util.h"
[email protected]eab8c0f2011-11-18 22:33:50202#endif
203
[email protected]dc04be7c2012-03-15 23:57:49204#if defined(USE_ASH)
[email protected]bafda132012-02-16 19:32:51205#include "ash/ash_switches.h"
[email protected]c2f0a892012-01-24 22:19:21206#endif
207
[email protected]8a53ee042009-01-21 16:41:33208using base::TimeDelta;
[email protected]b87ee522012-05-18 15:16:54209using content::NativeWebKeyboardEvent;
[email protected]c5eed492012-01-04 17:07:50210using content::NavigationController;
[email protected]10f417c52011-12-28 21:04:23211using content::NavigationEntry;
[email protected]e5d549d2011-12-28 01:29:20212using content::OpenURLParams;
[email protected]3a5180ae2011-12-21 02:39:38213using content::PluginService;
[email protected]e5d549d2011-12-28 01:29:20214using content::Referrer;
[email protected]b6583592012-01-25 19:52:33215using content::SiteInstance;
[email protected]7f6f44c2011-12-14 13:23:38216using content::UserMetricsAction;
[email protected]ea049a02011-12-25 21:37:09217using content::WebContents;
[email protected]1c321ee52012-05-21 03:02:34218using extensions::Extension;
[email protected]20c07f8e2012-05-31 08:43:14219using ui::WebDialogDelegate;
initial.commit09911bf2008-07-26 23:55:29220
[email protected]36b6dcb2008-11-12 01:19:57221///////////////////////////////////////////////////////////////////////////////
initial.commit09911bf2008-07-26 23:55:29222
[email protected]505323e22009-01-24 02:47:58223namespace {
224
[email protected]91a79232010-11-10 18:15:08225// The URL to be loaded to display the "Report a broken page" form.
226const char kBrokenPageUrl[] =
[email protected]95fc6f92011-04-13 00:37:20227 "https://www.google.com/support/chrome/bin/request.py?contact_type="
[email protected]91a79232010-11-10 18:15:08228 "broken_website&format=inproduct&p.page_title=$1&p.page_url=$2";
229
[email protected]91a79232010-11-10 18:15:08230// How long we wait before updating the browser chrome while loading a page.
231const int kUIUpdateCoalescingTimeMS = 200;
232
[email protected]a4fe67012012-07-25 20:14:29233BrowserWindow* CreateBrowserWindow(Browser* browser) {
[email protected]a4fe67012012-07-25 20:14:29234 return BrowserWindow::CreateBrowserWindow(browser);
235}
236
[email protected]b6975b92012-11-03 02:37:45237bool ShouldReloadCrashedTab(WebContents* contents) {
[email protected]273adae2012-11-14 02:27:05238#if defined(OS_CHROMEOS)
239 return contents->IsCrashed();
240#else
241 return false;
242#endif
[email protected]b6975b92012-11-03 02:37:45243}
[email protected]56073dc2012-08-10 16:59:06244
[email protected]505323e22009-01-24 02:47:58245} // namespace
246
[email protected]b35b26b32011-05-05 20:35:14247////////////////////////////////////////////////////////////////////////////////
248// Browser, CreateParams:
249
[email protected]196578252012-12-06 23:54:20250Browser::CreateParams::CreateParams(Profile* profile,
251 chrome::HostDesktopType host_desktop_type)
252 : type(TYPE_TABBED),
253 profile(profile),
254 host_desktop_type(host_desktop_type),
255 app_type(APP_TYPE_HOST),
256 initial_show_state(ui::SHOW_STATE_DEFAULT),
257 is_session_restore(false),
258 window(NULL) {
259}
260
[email protected]56073dc2012-08-10 16:59:06261Browser::CreateParams::CreateParams(Type type,
262 Profile* profile,
263 chrome::HostDesktopType host_desktop_type)
264 : type(type),
265 profile(profile),
266 host_desktop_type(host_desktop_type),
[email protected]0da58292012-03-22 20:37:21267 app_type(APP_TYPE_HOST),
[email protected]a636d8e52012-02-28 15:40:41268 initial_show_state(ui::SHOW_STATE_DEFAULT),
[email protected]a4fe67012012-07-25 20:14:29269 is_session_restore(false),
270 window(NULL) {
[email protected]b35b26b32011-05-05 20:35:14271}
272
[email protected]da22aa62012-04-04 18:54:35273// static
274Browser::CreateParams Browser::CreateParams::CreateForApp(
275 Type type,
276 const std::string& app_name,
277 const gfx::Rect& window_bounds,
[email protected]2b09ae552013-02-23 06:38:03278 Profile* profile,
279 chrome::HostDesktopType host_desktop_type) {
[email protected]da22aa62012-04-04 18:54:35280 DCHECK(type != TYPE_TABBED);
281 DCHECK(!app_name.empty());
282
[email protected]2b09ae552013-02-23 06:38:03283 CreateParams params(type, profile, host_desktop_type);
[email protected]da22aa62012-04-04 18:54:35284 params.app_name = app_name;
285 params.app_type = APP_TYPE_CHILD;
286 params.initial_bounds = window_bounds;
287
288 return params;
289}
290
291// static
292Browser::CreateParams Browser::CreateParams::CreateForDevTools(
[email protected]2b09ae552013-02-23 06:38:03293 Profile* profile,
294 chrome::HostDesktopType host_desktop_type) {
295 CreateParams params(TYPE_POPUP, profile, host_desktop_type);
[email protected]da22aa62012-04-04 18:54:35296 params.app_name = DevToolsWindow::kDevToolsApp;
297 return params;
298}
299
[email protected]20ca0382013-02-28 19:50:07300////////////////////////////////////////////////////////////////////////////////
301// Browser, InterstitialObserver:
302
303class Browser::InterstitialObserver : public content::WebContentsObserver {
304 public:
305 InterstitialObserver(Browser* browser, content::WebContents* web_contents)
306 : WebContentsObserver(web_contents),
307 browser_(browser) {
308 }
309
310 using content::WebContentsObserver::web_contents;
311
312 virtual void DidAttachInterstitialPage() OVERRIDE {
313 browser_->UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
314 }
315
316 virtual void DidDetachInterstitialPage() OVERRIDE {
317 browser_->UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
318 }
319
320 private:
321 Browser* browser_;
322
323 DISALLOW_COPY_AND_ASSIGN(InterstitialObserver);
324};
325
[email protected]36b6dcb2008-11-12 01:19:57326///////////////////////////////////////////////////////////////////////////////
327// Browser, Constructors, Creation, Showing:
initial.commit09911bf2008-07-26 23:55:29328
[email protected]a4fe67012012-07-25 20:14:29329Browser::Browser(const CreateParams& params)
330 : type_(params.type),
331 profile_(params.profile),
[email protected]f3e99e32008-07-30 04:48:39332 window_(NULL),
[email protected]ecc523f62010-09-28 22:45:40333 ALLOW_THIS_IN_INITIALIZER_LIST(
[email protected]855370052012-07-10 19:30:32334 tab_strip_model_delegate_(
335 new chrome::BrowserTabStripModelDelegate(this))),
[email protected]fa9d49ff2012-10-24 22:44:37336 tab_strip_model_(new TabStripModel(tab_strip_model_delegate_.get(),
337 params.profile)),
[email protected]a4fe67012012-07-25 20:14:29338 app_name_(params.app_name),
339 app_type_(params.app_type),
[email protected]15952e462008-11-14 00:29:05340 chrome_updater_factory_(this),
[email protected]69444cc2009-04-09 20:40:06341 cancel_download_confirmation_state_(NOT_PROMPTED),
[email protected]a4fe67012012-07-25 20:14:29342 override_bounds_(params.initial_bounds),
343 initial_show_state_(params.initial_show_state),
344 is_session_restore_(params.is_session_restore),
[email protected]56073dc2012-08-10 16:59:06345 host_desktop_type_(params.host_desktop_type),
[email protected]f9b6c092012-07-09 19:32:28346 ALLOW_THIS_IN_INITIALIZER_LIST(
347 unload_controller_(new chrome::UnloadController(this))),
[email protected]24f1477532011-11-22 22:55:36348 weak_factory_(this),
[email protected]fb7da4132011-03-15 15:51:00349 ALLOW_THIS_IN_INITIALIZER_LIST(
[email protected]ef557022012-03-16 10:05:33350 content_setting_bubble_model_delegate_(
351 new BrowserContentSettingBubbleModelDelegate(this))),
352 ALLOW_THIS_IN_INITIALIZER_LIST(
353 toolbar_model_delegate_(
354 new BrowserToolbarModelDelegate(this))),
355 ALLOW_THIS_IN_INITIALIZER_LIST(
[email protected]fb7da4132011-03-15 15:51:00356 tab_restore_service_delegate_(
[email protected]09b29342011-06-24 19:18:48357 new BrowserTabRestoreServiceDelegate(this))),
[email protected]afefa74e2011-07-26 05:04:23358 ALLOW_THIS_IN_INITIALIZER_LIST(
359 synced_window_delegate_(
360 new BrowserSyncedWindowDelegate(this))),
[email protected]8a5e0ca2011-08-25 06:30:47361 bookmark_bar_state_(BookmarkBar::HIDDEN),
[email protected]5d98294912012-06-27 22:57:40362 ALLOW_THIS_IN_INITIALIZER_LIST(
[email protected]557c4e22012-12-18 18:30:55363 command_controller_(new chrome::BrowserCommandController(
364 this, g_browser_process->profile_manager()))),
[email protected]2f516c792011-09-19 22:22:09365 window_has_shown_(false) {
[email protected]a4fe67012012-07-25 20:14:29366 if (!app_name_.empty())
367 chrome::RegisterAppPrefs(app_name_, profile_);
[email protected]2fc15ae2012-05-06 00:01:37368 tab_strip_model_->AddObserver(this);
369
[email protected]736d00322012-10-11 21:32:41370 toolbar_model_.reset(new ToolbarModelImpl(toolbar_model_delegate_.get()));
[email protected]f6a8dd632013-03-06 01:27:04371 search_model_.reset(new chrome::search::SearchModel());
[email protected]e3a5d5b2012-06-22 17:56:46372 search_delegate_.reset(
[email protected]51a53e8f62012-08-28 03:08:56373 new chrome::search::SearchDelegate(search_model_.get(),
374 toolbar_model_.get()));
[email protected]ef557022012-03-16 10:05:33375
[email protected]432115822011-07-10 15:52:27376 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
[email protected]a93089b12011-11-22 20:47:38377 content::Source<Profile>(profile_->GetOriginalProfile()));
[email protected]432115822011-07-10 15:52:27378 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]a93089b12011-11-22 20:47:38379 content::Source<Profile>(profile_->GetOriginalProfile()));
[email protected]432115822011-07-10 15:52:27380 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]a93089b12011-11-22 20:47:38381 content::Source<Profile>(profile_->GetOriginalProfile()));
[email protected]432115822011-07-10 15:52:27382 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07383 content::NotificationService::AllSources());
[email protected]6b40bb582012-03-15 20:50:38384#if defined(ENABLE_THEMES)
[email protected]ffa6f592011-06-24 22:03:57385 registrar_.Add(
[email protected]432115822011-07-10 15:52:27386 this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
[email protected]6c2381d2011-10-19 02:52:53387 content::Source<ThemeService>(
388 ThemeServiceFactory::GetForProfile(profile_)));
[email protected]6b40bb582012-03-15 20:50:38389#endif
[email protected]ea049a02011-12-25 21:37:09390 registrar_.Add(this, chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
[email protected]ad50def52011-10-19 23:17:07391 content::NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29392
[email protected]97b46382011-05-02 13:23:04393 profile_pref_registrar_.Init(profile_->GetPrefs());
[email protected]c31813a2012-11-15 18:47:26394 profile_pref_registrar_.Add(
395 prefs::kDevToolsDisabled,
396 base::Bind(&Browser::OnDevToolsDisabledChanged, base::Unretained(this)));
397 profile_pref_registrar_.Add(
398 prefs::kShowBookmarkBar,
399 base::Bind(&Browser::UpdateBookmarkBarState, base::Unretained(this),
400 BOOKMARK_BAR_STATE_CHANGE_PREF_CHANGE));
401 profile_pref_registrar_.Add(
402 prefs::kHomePage,
403 base::Bind(&Browser::MarkHomePageAsChanged, base::Unretained(this)));
[email protected]9e823662010-10-13 23:36:00404
initial.commit09911bf2008-07-26 23:55:29405 BrowserList::AddBrowser(this);
406
[email protected]97b46382011-05-02 13:23:04407 // NOTE: These prefs all need to be explicitly destroyed in the destructor
408 // or you'll get a nasty surprise when you run the incognito tests.
initial.commit09911bf2008-07-26 23:55:29409 encoding_auto_detect_.Init(prefs::kWebKitUsesUniversalDetector,
[email protected]96a5c342012-12-04 18:14:02410 profile_->GetPrefs());
[email protected]281918912010-05-27 22:05:13411
[email protected]e41982a72012-11-20 07:16:51412 if (is_type_tabbed())
413 instant_controller_.reset(new chrome::BrowserInstantController(this));
[email protected]df25eb9f2012-09-13 17:43:23414
[email protected]09b29342011-06-24 19:18:48415 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_INIT);
[email protected]a7be6982011-12-12 21:53:57416
[email protected]650b2d52013-02-10 03:41:45417 base::FilePath profile_path = profile_->GetPath();
[email protected]a7be6982011-12-12 21:53:57418 ProfileMetrics::LogProfileLaunch(profile_path);
[email protected]a4fe67012012-07-25 20:14:29419
420 window_ = params.window ? params.window : CreateBrowserWindow(this);
421
422 // TODO(beng): move to BrowserFrameWin.
423#if defined(OS_WIN) && !defined(USE_AURA)
424 // Set the app user model id for this application to that of the application
425 // name. See http://crbug.com/7028.
426 ui::win::SetAppIdForWindow(
427 is_app() && !is_type_panel() ?
[email protected]47e870b2013-02-24 21:14:53428 ShellIntegration::GetAppModelIdForProfile(UTF8ToWide(app_name_),
[email protected]a4fe67012012-07-25 20:14:29429 profile_->GetPath()) :
430 ShellIntegration::GetChromiumModelIdForProfile(profile_->GetPath()),
431 window()->GetNativeWindow());
432
433 if (is_type_panel()) {
[email protected]9c91fc22012-11-20 04:13:49434 ui::win::SetAppIconForWindow(ShellIntegration::GetChromiumIconLocation(),
[email protected]a4fe67012012-07-25 20:14:29435 window()->GetNativeWindow());
436 }
437#endif
438
439 // Create the extension window controller before sending notifications.
440 extension_window_controller_.reset(
441 new BrowserExtensionWindowController(this));
442
443 // TODO(beng): Move BrowserList::AddBrowser() to the end of this function and
444 // replace uses of this with BL's notifications.
445 content::NotificationService::current()->Notify(
446 chrome::NOTIFICATION_BROWSER_WINDOW_READY,
447 content::Source<Browser>(this),
448 content::NotificationService::NoDetails());
449
450 // TODO(beng): move to ChromeBrowserMain:
[email protected]dfcc05f2013-02-21 14:12:16451 if (first_run::ShouldDoPersonalDataManagerFirstRun()) {
[email protected]a4fe67012012-07-25 20:14:29452#if defined(OS_WIN)
[email protected]dfcc05f2013-02-21 14:12:16453 // Notify PDM that this is a first run.
[email protected]a4fe67012012-07-25 20:14:29454 ImportAutofillDataWin(PersonalDataManagerFactory::GetForProfile(profile_));
455#endif // defined(OS_WIN)
[email protected]a4fe67012012-07-25 20:14:29456 }
457
[email protected]6fb528e2012-08-01 20:32:33458 fullscreen_controller_.reset(new FullscreenController(this));
[email protected]c3166302012-09-25 21:27:08459 search_model_->AddObserver(this);
initial.commit09911bf2008-07-26 23:55:29460}
461
462Browser::~Browser() {
[email protected]2fc15ae2012-05-06 00:01:37463 // The tab strip should not have any tabs at this point.
[email protected]dcc87fb2013-03-01 04:13:59464 if (!browser_shutdown::ShuttingDownWithoutClosingBrowsers())
465 DCHECK(tab_strip_model_->empty());
[email protected]c3166302012-09-25 21:27:08466
467 search_model_->RemoveObserver(this);
[email protected]2fc15ae2012-05-06 00:01:37468 tab_strip_model_->RemoveObserver(this);
469
[email protected]557c4e22012-12-18 18:30:55470 // Destroy the BrowserCommandController before removing the browser, so that
471 // it doesn't act on any notifications that are sent as a result of removing
472 // the browser.
473 command_controller_.reset();
initial.commit09911bf2008-07-26 23:55:29474 BrowserList::RemoveBrowser(this);
475
[email protected]bdc6ccd12012-03-20 22:06:13476 SessionService* session_service =
477 SessionServiceFactory::GetForProfile(profile_);
478 if (session_service)
479 session_service->WindowClosed(session_id_);
480
481 TabRestoreService* tab_restore_service =
482 TabRestoreServiceFactory::GetForProfile(profile());
483 if (tab_restore_service)
484 tab_restore_service->BrowserClosed(tab_restore_service_delegate());
485
[email protected]7be64502011-05-03 17:51:47486#if !defined(OS_MACOSX)
[email protected]c987a242013-02-28 01:17:41487 if (!chrome::GetTotalBrowserCountForProfile(profile_)) {
initial.commit09911bf2008-07-26 23:55:29488 // We're the last browser window with this profile. We need to nuke the
489 // TabRestoreService, which will start the shutdown of the
490 // NavigationControllers and allow for proper shutdown. If we don't do this
491 // chrome won't shutdown cleanly, and may end up crashing when some
492 // thread tries to use the IO thread (or another thread) that is no longer
493 // valid.
[email protected]fbc947b2009-06-19 13:28:24494 // This isn't a valid assumption for Mac OS, as it stays running after
495 // the last browser has closed. The Mac equivalent is in its app
496 // controller.
[email protected]92371eb2011-04-28 11:50:15497 TabRestoreServiceFactory::ResetForProfile(profile_);
initial.commit09911bf2008-07-26 23:55:29498 }
[email protected]fbc947b2009-06-19 13:28:24499#endif
initial.commit09911bf2008-07-26 23:55:29500
[email protected]97b46382011-05-02 13:23:04501 profile_pref_registrar_.RemoveAll();
[email protected]97b46382011-05-02 13:23:04502
[email protected]9a8c4022011-01-25 14:25:33503 encoding_auto_detect_.Destroy();
[email protected]9a8c4022011-01-25 14:25:33504
initial.commit09911bf2008-07-26 23:55:29505 if (profile_->IsOffTheRecord() &&
[email protected]474e5db2011-08-12 13:02:23506 !BrowserList::IsOffTheRecordSessionActiveForProfile(profile_)) {
[email protected]5e919242012-02-13 23:59:35507 // An incognito profile is no longer needed, this indirectly frees
508 // its cache and cookies once it gets destroyed at the appropriate time.
[email protected]4dffabe2012-05-19 14:37:06509 ProfileDestroyer::DestroyProfileWhenAppropriate(profile_);
initial.commit09911bf2008-07-26 23:55:29510 }
511
512 // There may be pending file dialogs, we need to tell them that we've gone
513 // away so they don't try and call back to us.
514 if (select_file_dialog_.get())
515 select_file_dialog_->ListenerDestroyed();
516}
517
[email protected]36b6dcb2008-11-12 01:19:57518///////////////////////////////////////////////////////////////////////////////
[email protected]5c238752009-06-13 10:29:07519// Getters & Setters
520
[email protected]b77cb302009-10-29 04:09:17521FindBarController* Browser::GetFindBarController() {
522 if (!find_bar_controller_.get()) {
[email protected]632983f2011-08-08 22:51:24523 FindBar* find_bar = window_->CreateFindBar();
[email protected]b77cb302009-10-29 04:09:17524 find_bar_controller_.reset(new FindBarController(find_bar));
525 find_bar->SetFindBarController(find_bar_controller_.get());
[email protected]0d56fa7212012-11-15 23:34:02526 find_bar_controller_->ChangeWebContents(
527 tab_strip_model_->GetActiveWebContents());
[email protected]b77cb302009-10-29 04:09:17528 find_bar_controller_->find_bar()->MoveWindowIfNecessary(gfx::Rect(), true);
529 }
530 return find_bar_controller_.get();
531}
532
[email protected]24db8a072009-10-29 20:35:37533bool Browser::HasFindBarController() const {
534 return find_bar_controller_.get() != NULL;
535}
536
[email protected]b35b26b32011-05-05 20:35:14537bool Browser::is_app() const {
538 return !app_name_.empty();
539}
540
541bool Browser::is_devtools() const {
542 return app_name_ == DevToolsWindow::kDevToolsApp;
543}
544
[email protected]5c238752009-06-13 10:29:07545///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:57546// Browser, State Storage and Retrieval for UI:
547
[email protected]9b125b732012-08-17 04:43:55548gfx::Image Browser::GetCurrentPageIcon() const {
[email protected]0d56fa7212012-11-15 23:34:02549 WebContents* web_contents = tab_strip_model_->GetActiveWebContents();
[email protected]502b5612012-10-02 01:33:10550 // |web_contents| can be NULL since GetCurrentPageIcon() is called by the
551 // window during the window's creation (before tabs have been added).
552 FaviconTabHelper* favicon_tab_helper =
553 web_contents ? FaviconTabHelper::FromWebContents(web_contents) : NULL;
554 return favicon_tab_helper ? favicon_tab_helper->GetFavicon() : gfx::Image();
[email protected]36b6dcb2008-11-12 01:19:57555}
556
[email protected]731f8a42009-07-20 22:07:32557string16 Browser::GetWindowTitleForCurrentTab() const {
[email protected]0d56fa7212012-11-15 23:34:02558 WebContents* contents = tab_strip_model_->GetActiveWebContents();
[email protected]dda5dfa32009-07-10 00:33:29559 string16 title;
[email protected]c7c42332008-11-15 01:10:54560
[email protected]731f8a42009-07-20 22:07:32561 // |contents| can be NULL because GetWindowTitleForCurrentTab is called by the
562 // window during the window's creation (before tabs have been added).
[email protected]36b6dcb2008-11-12 01:19:57563 if (contents) {
[email protected]dda5dfa32009-07-10 00:33:29564 title = contents->GetTitle();
[email protected]36b6dcb2008-11-12 01:19:57565 FormatTitleForDisplay(&title);
566 }
567 if (title.empty())
[email protected]51d83bdf2011-12-09 16:09:29568 title = CoreTabHelper::GetDefaultTitle();
[email protected]36b6dcb2008-11-12 01:19:57569
[email protected]82b9ed82012-11-15 02:43:18570#if defined(OS_MACOSX)
571 // On Mac, we don't want to suffix the page title with
[email protected]22a3daa2009-11-09 22:03:33572 // the application name.
[email protected]d904bc82009-07-07 00:41:04573 return title;
[email protected]82b9ed82012-11-15 02:43:18574#elif defined(USE_ASH)
575 // On Ash, we don't want to suffix the page title with the application name,
576 // but on Windows, where USE_ASH can also be true, we still want the prefix
577 // on desktop.
578 if (host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH)
579 return title;
580#endif
[email protected]dff529732010-04-01 23:50:55581 // Don't append the app name to window titles on app frames and app popups
[email protected]9c55de82012-11-01 21:03:52582 return is_app() ?
583 title :
584 l10n_util::GetStringFUTF16(IDS_BROWSER_WINDOW_TITLE_FORMAT, title);
[email protected]36b6dcb2008-11-12 01:19:57585}
586
587// static
[email protected]dda5dfa32009-07-10 00:33:29588void Browser::FormatTitleForDisplay(string16* title) {
[email protected]36b6dcb2008-11-12 01:19:57589 size_t current_index = 0;
590 size_t match_index;
[email protected]57ecc4b2010-08-11 03:02:51591 while ((match_index = title->find(L'\n', current_index)) != string16::npos) {
[email protected]810a52ef2010-01-08 01:22:15592 title->replace(match_index, 1, string16());
[email protected]36b6dcb2008-11-12 01:19:57593 current_index = match_index;
594 }
595}
596
597///////////////////////////////////////////////////////////////////////////////
598// Browser, OnBeforeUnload handling:
599
600bool Browser::ShouldCloseWindow() {
[email protected]69444cc2009-04-09 20:40:06601 if (!CanCloseWithInProgressDownloads())
602 return false;
603
[email protected]f9b6c092012-07-09 19:32:28604 return unload_controller_->ShouldCloseWindow();
605}
[email protected]69444cc2009-04-09 20:40:06606
[email protected]f9b6c092012-07-09 19:32:28607bool Browser::IsAttemptingToCloseBrowser() const {
608 return unload_controller_->is_attempting_to_close_browser();
[email protected]36b6dcb2008-11-12 01:19:57609}
610
611void Browser::OnWindowClosing() {
612 if (!ShouldCloseWindow())
613 return;
614
[email protected]c984d9f2010-07-20 20:52:20615 // Application should shutdown on last window close if the user is explicitly
616 // trying to quit, or if there is nothing keeping the browser alive (such as
617 // AppController on the Mac, or BackgroundContentsService for background
618 // pages).
619 bool should_quit_if_last_browser =
[email protected]313fce12013-01-30 17:09:04620 browser_shutdown::IsTryingToQuit() || !chrome::WillKeepAlive();
[email protected]7dc8c6b2010-04-09 17:02:50621
[email protected]0665ebe2013-02-13 09:53:19622 if (should_quit_if_last_browser &&
[email protected]abcfa65a2013-02-21 20:13:58623 BrowserList::GetInstance(host_desktop_type_)->size() == 1) {
[email protected]36b6dcb2008-11-12 01:19:57624 browser_shutdown::OnShutdownStarting(browser_shutdown::WINDOW_CLOSE);
[email protected]0665ebe2013-02-13 09:53:19625 }
[email protected]36b6dcb2008-11-12 01:19:57626
[email protected]92371eb2011-04-28 11:50:15627 // Don't use GetForProfileIfExisting here, we want to force creation of the
[email protected]36b6dcb2008-11-12 01:19:57628 // session service so that user can restore what was open.
[email protected]92371eb2011-04-28 11:50:15629 SessionService* session_service =
630 SessionServiceFactory::GetForProfile(profile());
[email protected]36b6dcb2008-11-12 01:19:57631 if (session_service)
632 session_service->WindowClosing(session_id());
633
[email protected]92371eb2011-04-28 11:50:15634 TabRestoreService* tab_restore_service =
635 TabRestoreServiceFactory::GetForProfile(profile());
[email protected]a636d8e52012-02-28 15:40:41636
637#if defined(USE_AURA)
638 if (tab_restore_service && is_app())
639 tab_restore_service->BrowserClosing(tab_restore_service_delegate());
640#endif
641
[email protected]e0448872013-01-11 19:35:02642 if (tab_restore_service && is_type_tabbed() && tab_strip_model_->count())
[email protected]fb7da4132011-03-15 15:51:00643 tab_restore_service->BrowserClosing(tab_restore_service_delegate());
[email protected]d8375fd2008-11-25 22:45:39644
[email protected]911f8152010-03-18 16:46:40645 // TODO(sky): convert session/tab restore to use notification.
[email protected]ad50def52011-10-19 23:17:07646 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27647 chrome::NOTIFICATION_BROWSER_CLOSING,
[email protected]6c2381d2011-10-19 02:52:53648 content::Source<Browser>(this),
[email protected]1e0c7402012-07-27 10:48:15649 content::NotificationService::NoDetails());
[email protected]911f8152010-03-18 16:46:40650
[email protected]0d56fa7212012-11-15 23:34:02651 tab_strip_model_->CloseAllTabs();
[email protected]36b6dcb2008-11-12 01:19:57652}
653
[email protected]28788c7d2011-05-20 23:03:45654void Browser::OnWindowActivated() {
655 // On some platforms we want to automatically reload tabs that are
656 // killed when the user selects them.
[email protected]0d56fa7212012-11-15 23:34:02657 WebContents* contents = tab_strip_model_->GetActiveWebContents();
[email protected]b6975b92012-11-03 02:37:45658 if (contents && ShouldReloadCrashedTab(contents))
659 chrome::Reload(this, CURRENT_TAB);
[email protected]28788c7d2011-05-20 23:03:45660}
661
[email protected]69444cc2009-04-09 20:40:06662////////////////////////////////////////////////////////////////////////////////
663// In-progress download termination handling:
664
665void Browser::InProgressDownloadResponse(bool cancel_downloads) {
666 if (cancel_downloads) {
667 cancel_download_confirmation_state_ = RESPONSE_RECEIVED;
[email protected]a37d4b02012-06-25 21:56:10668 chrome::CloseWindow(this);
[email protected]69444cc2009-04-09 20:40:06669 return;
670 }
671
672 // Sets the confirmation state to NOT_PROMPTED so that if the user tries to
673 // close again we'll show the warning again.
674 cancel_download_confirmation_state_ = NOT_PROMPTED;
675
676 // Show the download page so the user can figure-out what downloads are still
677 // in-progress.
[email protected]5d9cace72012-06-21 16:07:12678 chrome::ShowDownloads(this);
[email protected]69444cc2009-04-09 20:40:06679}
680
[email protected]5a17d492011-10-24 19:37:03681Browser::DownloadClosePreventionType Browser::OkToCloseWithInProgressDownloads(
682 int* num_downloads_blocking) const {
683 DCHECK(num_downloads_blocking);
684 *num_downloads_blocking = 0;
685
[email protected]f9b6c092012-07-09 19:32:28686 if (IsAttemptingToCloseBrowser())
[email protected]5a17d492011-10-24 19:37:03687 return DOWNLOAD_CLOSE_OK;
688
689 // If we're not running a full browser process with a profile manager
690 // (testing), it's ok to close the browser.
691 if (!g_browser_process->profile_manager())
692 return DOWNLOAD_CLOSE_OK;
693
694 int total_download_count = DownloadService::DownloadCountAllProfiles();
695 if (total_download_count == 0)
696 return DOWNLOAD_CLOSE_OK; // No downloads; can definitely close.
697
698 // Figure out how many windows are open total, and associated with this
699 // profile, that are relevant for the ok-to-close decision.
700 int profile_window_count = 0;
701 int total_window_count = 0;
[email protected]3539929f2013-02-01 05:59:14702 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
[email protected]5a17d492011-10-24 19:37:03703 // Don't count this browser window or any other in the process of closing.
[email protected]3539929f2013-02-01 05:59:14704 Browser* const browser = *it;
[email protected]f9b6c092012-07-09 19:32:28705 // Window closing may be delayed, and windows that are in the process of
706 // closing don't count against our totals.
707 if (browser == this || browser->IsAttemptingToCloseBrowser())
[email protected]5a17d492011-10-24 19:37:03708 continue;
709
[email protected]3539929f2013-02-01 05:59:14710 if (it->profile() == profile())
[email protected]5a17d492011-10-24 19:37:03711 profile_window_count++;
712 total_window_count++;
713 }
714
715 // If there aren't any other windows, we're at browser shutdown,
716 // which would cancel all current downloads.
717 if (total_window_count == 0) {
718 *num_downloads_blocking = total_download_count;
719 return DOWNLOAD_CLOSE_BROWSER_SHUTDOWN;
720 }
721
722 // If there aren't any other windows on our profile, and we're an incognito
723 // profile, and there are downloads associated with that profile,
724 // those downloads would be cancelled by our window (-> profile) close.
725 DownloadService* download_service =
726 DownloadServiceFactory::GetForProfile(profile());
727 if (profile_window_count == 0 && download_service->DownloadCount() > 0 &&
728 profile()->IsOffTheRecord()) {
729 *num_downloads_blocking = download_service->DownloadCount();
730 return DOWNLOAD_CLOSE_LAST_WINDOW_IN_INCOGNITO_PROFILE;
731 }
732
733 // Those are the only conditions under which we will block shutdown.
734 return DOWNLOAD_CLOSE_OK;
735}
736
[email protected]69444cc2009-04-09 20:40:06737////////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:57738// Browser, Tab adding/showing functions:
739
[email protected]227393e2011-06-29 21:28:39740void Browser::WindowFullscreenStateChanged() {
[email protected]01a5a762011-11-21 05:40:55741 fullscreen_controller_->WindowFullscreenStateChanged();
[email protected]5d98294912012-06-27 22:57:40742 command_controller_->FullscreenStateChanged();
[email protected]9adb8b12011-12-09 06:49:13743 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TOGGLE_FULLSCREEN);
[email protected]ae2622c2009-07-30 23:47:58744}
745
[email protected]e67ebf32013-02-13 11:07:19746void Browser::VisibleSSLStateChanged(content::WebContents* web_contents) {
747 // When the current tab's SSL state changes, we need to update the URL
748 // bar to reflect the new state.
749 DCHECK(web_contents);
750 if (tab_strip_model_->GetActiveWebContents() == web_contents)
751 UpdateToolbar(false);
752}
753
[email protected]36b6dcb2008-11-12 01:19:57754///////////////////////////////////////////////////////////////////////////////
755// Browser, Assorted browser commands:
756
[email protected]41d9faf2012-02-28 23:46:02757void Browser::ToggleFullscreenModeWithExtension(const GURL& extension_url) {
758 fullscreen_controller_->ToggleFullscreenModeWithExtension(extension_url);
[email protected]9282cea2009-02-18 18:49:00759}
760
[email protected]a3e18c42009-03-04 23:36:05761bool Browser::SupportsWindowFeature(WindowFeature feature) const {
[email protected]018cf362010-05-05 22:43:22762 return SupportsWindowFeatureImpl(feature, true);
763}
[email protected]f5bf8ccf2010-02-05 18:19:25764
[email protected]018cf362010-05-05 22:43:22765bool Browser::CanSupportWindowFeature(WindowFeature feature) const {
766 return SupportsWindowFeatureImpl(feature, false);
[email protected]a3e18c42009-03-04 23:36:05767}
768
[email protected]fbd77592008-11-12 20:50:27769void Browser::ToggleEncodingAutoDetect() {
[email protected]7f6f44c2011-12-14 13:23:38770 content::RecordAction(UserMetricsAction("AutoDetectChange"));
[email protected]fbd77592008-11-12 20:50:27771 encoding_auto_detect_.SetValue(!encoding_auto_detect_.GetValue());
[email protected]a697f4c2009-09-14 22:30:18772 // If "auto detect" is turned on, then any current override encoding
773 // is cleared. This also implicitly performs a reload.
774 // OTOH, if "auto detect" is turned off, we don't change the currently
775 // active encoding.
776 if (encoding_auto_detect_.GetValue()) {
[email protected]0d56fa7212012-11-15 23:34:02777 WebContents* contents = tab_strip_model_->GetActiveWebContents();
[email protected]a697f4c2009-09-14 22:30:18778 if (contents)
[email protected]b2fe07d12010-02-09 14:38:08779 contents->ResetOverrideEncoding();
[email protected]a697f4c2009-09-14 22:30:18780 }
[email protected]fbd77592008-11-12 20:50:27781}
782
783void Browser::OverrideEncoding(int encoding_id) {
[email protected]7f6f44c2011-12-14 13:23:38784 content::RecordAction(UserMetricsAction("OverrideEncoding"));
[email protected]41fc0322009-09-04 22:23:40785 const std::string selected_encoding =
[email protected]fbd77592008-11-12 20:50:27786 CharacterEncoding::GetCanonicalEncodingNameByCommandId(encoding_id);
[email protected]0d56fa7212012-11-15 23:34:02787 WebContents* contents = tab_strip_model_->GetActiveWebContents();
[email protected]57c6a652009-05-04 07:58:34788 if (!selected_encoding.empty() && contents)
[email protected]b2fe07d12010-02-09 14:38:08789 contents->SetOverrideEncoding(selected_encoding);
[email protected]fbd77592008-11-12 20:50:27790 // Update the list of recently selected encodings.
[email protected]41fc0322009-09-04 22:23:40791 std::string new_selected_encoding_list;
[email protected]ddd231e2010-06-29 20:35:19792 if (CharacterEncoding::UpdateRecentlySelectedEncoding(
793 profile_->GetPrefs()->GetString(prefs::kRecentlySelectedEncoding),
[email protected]41fc0322009-09-04 22:23:40794 encoding_id,
795 &new_selected_encoding_list)) {
[email protected]fbd77592008-11-12 20:50:27796 profile_->GetPrefs()->SetString(prefs::kRecentlySelectedEncoding,
[email protected]ddd231e2010-06-29 20:35:19797 new_selected_encoding_list);
[email protected]fbd77592008-11-12 20:50:27798 }
[email protected]36b6dcb2008-11-12 01:19:57799}
800
[email protected]84214982008-12-10 18:49:10801void Browser::OpenFile() {
[email protected]7f6f44c2011-12-14 13:23:38802 content::RecordAction(UserMetricsAction("OpenFile"));
[email protected]92f54082012-07-31 01:43:14803 select_file_dialog_ = ui::SelectFileDialog::Create(
[email protected]6e1fcd12012-07-02 17:14:20804 this, new ChromeSelectFilePolicy(
[email protected]0d56fa7212012-11-15 23:34:02805 tab_strip_model_->GetActiveWebContents()));
[email protected]84214982008-12-10 18:49:10806
[email protected]650b2d52013-02-10 03:41:45807 const base::FilePath directory = profile_->last_selected_directory();
[email protected]35896a32010-06-09 08:42:51808
[email protected]84214982008-12-10 18:49:10809 // TODO(beng): figure out how to juggle this.
[email protected]90556dd2012-06-07 20:26:18810 gfx::NativeWindow parent_window = window_->GetNativeWindow();
[email protected]479cce782012-09-15 20:15:53811 ui::SelectFileDialog::FileTypeInfo file_types;
[email protected]599538b2013-02-20 14:15:14812 file_types.support_drive = true;
[email protected]92f54082012-07-31 01:43:14813 select_file_dialog_->SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE,
[email protected]35896a32010-06-09 08:42:51814 string16(), directory,
[email protected]479cce782012-09-15 20:15:53815 &file_types, 0, FILE_PATH_LITERAL(""),
[email protected]d56bcd22009-03-16 19:51:56816 parent_window, NULL);
[email protected]84214982008-12-10 18:49:10817}
818
[email protected]92655362012-04-20 21:33:17819void Browser::UpdateDownloadShelfVisibility(bool visible) {
820 if (GetStatusBubble())
821 GetStatusBubble()->UpdateDownloadShelfVisibility(visible);
822}
823
[email protected]36b6dcb2008-11-12 01:19:57824///////////////////////////////////////////////////////////////////////////////
825
826// static
[email protected]d9083482012-01-06 00:38:46827bool Browser::RunUnloadEventsHelper(WebContents* contents) {
[email protected]075969d2012-04-17 20:05:55828 // If the WebContents is not connected yet, then there's no unload
829 // handler we can fire even if the WebContents has an unload listener.
[email protected]ecc523f62010-09-28 22:45:40830 // One case where we hit this is in a tab that has an infinite loop
831 // before load.
[email protected]5aab5e22010-12-08 22:13:29832 if (contents->NeedToFireBeforeUnload()) {
[email protected]ecc523f62010-09-28 22:45:40833 // If the page has unload listeners, then we tell the renderer to fire
834 // them. Once they have fired, we'll get a message back saying whether
835 // to proceed closing the page or not, which sends us back to this method
[email protected]5aab5e22010-12-08 22:13:29836 // with the NeedToFireBeforeUnload bit cleared.
[email protected]151a63d2011-12-20 22:32:52837 contents->GetRenderViewHost()->FirePageBeforeUnload(false);
[email protected]ecc523f62010-09-28 22:45:40838 return true;
839 }
840 return false;
841}
842
843// static
[email protected]1d3e9172012-06-12 02:59:13844void Browser::JSOutOfMemoryHelper(WebContents* web_contents) {
[email protected]4f822f022012-12-20 19:11:42845 InfoBarService* infobar_service =
846 InfoBarService::FromWebContents(web_contents);
847 if (!infobar_service)
[email protected]2a6bc3e2011-12-28 23:51:33848 return;
849
[email protected]0be09932013-01-08 02:03:50850 SimpleAlertInfoBarDelegate::Create(
851 infobar_service, NULL,
852 l10n_util::GetStringUTF16(IDS_JS_OUT_OF_MEMORY_PROMPT), true);
[email protected]7d189022011-08-25 22:54:20853}
854
855// static
[email protected]1d3e9172012-06-12 02:59:13856void Browser::RegisterProtocolHandlerHelper(WebContents* web_contents,
[email protected]7d189022011-08-25 22:54:20857 const std::string& protocol,
858 const GURL& url,
[email protected]3a3b75a2012-06-01 08:38:36859 const string16& title,
[email protected]7ae9c322012-07-17 23:46:44860 bool user_gesture,
861 BrowserWindow* window) {
[email protected]aa190632012-12-04 07:39:53862 Profile* profile =
863 Profile::FromBrowserContext(web_contents->GetBrowserContext());
864 if (profile->IsOffTheRecord())
[email protected]7d189022011-08-25 22:54:20865 return;
866
[email protected]7d189022011-08-25 22:54:20867 ProtocolHandler handler =
868 ProtocolHandler::CreateProtocolHandler(protocol, url, title);
869
[email protected]aa190632012-12-04 07:39:53870 ProtocolHandlerRegistry* registry = profile->GetProtocolHandlerRegistry();
[email protected]c5f51192012-09-28 04:09:50871 TabSpecificContentSettings* tab_content_settings =
872 TabSpecificContentSettings::FromWebContents(web_contents);
[email protected]7d189022011-08-25 22:54:20873
[email protected]96affbc82012-07-23 18:04:42874 if (registry->SilentlyHandleRegisterHandlerRequest(handler))
875 return;
876
[email protected]7ae9c322012-07-17 23:46:44877 if (!user_gesture && window) {
[email protected]c5f51192012-09-28 04:09:50878 tab_content_settings->set_pending_protocol_handler(handler);
879 tab_content_settings->set_previous_protocol_handler(
[email protected]7ae9c322012-07-17 23:46:44880 registry->GetHandlerFor(handler.protocol()));
881 window->GetLocationBar()->UpdateContentSettingsIcons();
[email protected]3fa5e192012-07-15 00:40:34882 return;
[email protected]7ae9c322012-07-17 23:46:44883 }
884
885 // Make sure content-setting icon is turned off in case the page does
886 // ungestured and gestured RPH calls.
887 if (window) {
[email protected]c5f51192012-09-28 04:09:50888 tab_content_settings->ClearPendingProtocolHandler();
[email protected]7ae9c322012-07-17 23:46:44889 window->GetLocationBar()->UpdateContentSettingsIcons();
890 }
[email protected]3fa5e192012-07-15 00:40:34891
[email protected]0be09932013-01-08 02:03:50892 RegisterProtocolHandlerInfoBarDelegate::Create(
893 InfoBarService::FromWebContents(web_contents), registry, handler);
[email protected]7d189022011-08-25 22:54:20894}
895
896// static
[email protected]1d3e9172012-06-12 02:59:13897void Browser::FindReplyHelper(WebContents* web_contents,
[email protected]b888919c2011-09-02 00:32:16898 int request_id,
899 int number_of_matches,
900 const gfx::Rect& selection_rect,
901 int active_match_ordinal,
902 bool final_update) {
[email protected]bc852c932012-09-29 21:38:34903 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(web_contents);
904 if (!find_tab_helper)
[email protected]b888919c2011-09-02 00:32:16905 return;
906
[email protected]bc852c932012-09-29 21:38:34907 find_tab_helper->HandleFindReply(request_id,
908 number_of_matches,
909 selection_rect,
910 active_match_ordinal,
911 final_update);
[email protected]b888919c2011-09-02 00:32:16912}
913
[email protected]e232c992012-12-06 12:43:20914void Browser::UpdateUIForNavigationInTab(WebContents* contents,
[email protected]ef557022012-03-16 10:05:33915 content::PageTransition transition,
916 bool user_initiated) {
[email protected]e232c992012-12-06 12:43:20917 tab_strip_model_->TabNavigating(contents, transition);
[email protected]ef557022012-03-16 10:05:33918
[email protected]0d56fa7212012-11-15 23:34:02919 bool contents_is_selected =
[email protected]e232c992012-12-06 12:43:20920 contents == tab_strip_model_->GetActiveWebContents();
[email protected]ef557022012-03-16 10:05:33921 if (user_initiated && contents_is_selected && window()->GetLocationBar()) {
922 // Forcibly reset the location bar if the url is going to change in the
923 // current tab, since otherwise it won't discard any ongoing user edits,
924 // since it doesn't realize this is a user-initiated action.
925 window()->GetLocationBar()->Revert();
926 }
927
928 if (GetStatusBubble())
929 GetStatusBubble()->Hide();
930
931 // Update the location bar. This is synchronous. We specifically don't
932 // update the load state since the load hasn't started yet and updating it
933 // will put it out of sync with the actual state like whether we're
934 // displaying a favicon, which controls the throbber. If we updated it here,
935 // the throbber will show the default favicon for a split second when
936 // navigating away from the new tab page.
[email protected]e232c992012-12-06 12:43:20937 ScheduleUIUpdate(contents, content::INVALIDATE_TYPE_URL);
[email protected]ef557022012-03-16 10:05:33938
[email protected]e1f288d2012-05-23 19:36:10939 if (contents_is_selected)
[email protected]5b4ee2772013-03-04 22:15:29940 contents->GetView()->SetInitialFocus();
[email protected]ef557022012-03-16 10:05:33941}
942
943///////////////////////////////////////////////////////////////////////////////
944// Browser, PageNavigator implementation:
945
946WebContents* Browser::OpenURL(const OpenURLParams& params) {
947 return OpenURLFromTab(NULL, params);
948}
949
[email protected]485fba42009-03-24 23:27:29950///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:57951// Browser, TabStripModelObserver implementation:
952
[email protected]409ea2972012-11-10 19:54:43953void Browser::TabInsertedAt(WebContents* contents,
[email protected]36b6dcb2008-11-12 01:19:57954 int index,
955 bool foreground) {
[email protected]409ea2972012-11-10 19:54:43956 SetAsDelegate(contents, this);
[email protected]50a9a76b2012-09-12 14:19:36957 SessionTabHelper* session_tab_helper =
[email protected]409ea2972012-11-10 19:54:43958 SessionTabHelper::FromWebContents(contents);
[email protected]50a9a76b2012-09-12 14:19:36959 session_tab_helper->SetWindowID(session_id());
[email protected]e524ff92011-03-30 13:26:58960
[email protected]fb312eb2012-09-24 23:21:10961 content::NotificationService::current()->Notify(
962 chrome::NOTIFICATION_TAB_PARENTED,
[email protected]409ea2972012-11-10 19:54:43963 content::Source<content::WebContents>(contents),
[email protected]fb312eb2012-09-24 23:21:10964 content::NotificationService::NoDetails());
965
[email protected]c37e3b62010-01-22 00:15:17966 SyncHistoryWithTabs(index);
[email protected]36b6dcb2008-11-12 01:19:57967
[email protected]159f7762008-12-19 14:58:27968 // Make sure the loading state is updated correctly, otherwise the throbber
969 // won't start if the page is loading.
[email protected]409ea2972012-11-10 19:54:43970 LoadingStateChanged(contents);
[email protected]159f7762008-12-19 14:58:27971
[email protected]20ca0382013-02-28 19:50:07972 interstitial_observers_.push_back(new InterstitialObserver(this, contents));
[email protected]e397a4472011-12-21 21:47:50973
[email protected]40600152012-09-26 10:04:31974 SessionService* session_service =
975 SessionServiceFactory::GetForProfile(profile_);
976 if (session_service)
[email protected]409ea2972012-11-10 19:54:43977 session_service->TabInserted(contents);
[email protected]36b6dcb2008-11-12 01:19:57978}
979
[email protected]f2cc7382010-10-15 18:03:05980void Browser::TabClosingAt(TabStripModel* tab_strip_model,
[email protected]0be097d2012-11-10 00:20:16981 WebContents* contents,
[email protected]f2cc7382010-10-15 18:03:05982 int index) {
[email protected]0be097d2012-11-10 00:20:16983 fullscreen_controller_->OnTabClosing(contents);
[email protected]40600152012-09-26 10:04:31984 SessionService* session_service =
985 SessionServiceFactory::GetForProfile(profile_);
986 if (session_service)
[email protected]0be097d2012-11-10 00:20:16987 session_service->TabClosing(contents);
[email protected]ad50def52011-10-19 23:17:07988 content::NotificationService::current()->Notify(
[email protected]884033e2012-04-16 19:38:42989 chrome::NOTIFICATION_TAB_CLOSING,
[email protected]0be097d2012-11-10 00:20:16990 content::Source<NavigationController>(&contents->GetController()),
[email protected]ad50def52011-10-19 23:17:07991 content::NotificationService::NoDetails());
[email protected]36b6dcb2008-11-12 01:19:57992
[email protected]075969d2012-04-17 20:05:55993 // Sever the WebContents' connection back to us.
[email protected]0be097d2012-11-10 00:20:16994 SetAsDelegate(contents, NULL);
[email protected]36b6dcb2008-11-12 01:19:57995}
996
[email protected]e89cfcb2012-11-11 14:47:24997void Browser::TabDetachedAt(WebContents* contents, int index) {
[email protected]c37e3b62010-01-22 00:15:17998 TabDetachedAtImpl(contents, index, DETACH_TYPE_DETACH);
[email protected]36b6dcb2008-11-12 01:19:57999}
1000
[email protected]023c01cc2012-11-12 15:41:111001void Browser::TabDeactivated(WebContents* contents) {
[email protected]01a5a762011-11-21 05:40:551002 fullscreen_controller_->OnTabDeactivated(contents);
[email protected]023c01cc2012-11-12 15:41:111003 search_delegate_->OnTabDeactivated(contents);
[email protected]e3a5d5b2012-06-22 17:56:461004
[email protected]85ff2c42009-05-29 21:22:031005 // Save what the user's currently typing, so it can be restored when we
1006 // switch back to this tab.
[email protected]023c01cc2012-11-12 15:41:111007 window_->GetLocationBar()->SaveStateToContents(contents);
[email protected]3d6a8952012-12-14 03:18:071008
1009 if (instant_controller_)
1010 instant_controller_->TabDeactivated(contents);
[email protected]85ff2c42009-05-29 21:22:031011}
1012
[email protected]8e707792012-11-13 10:32:121013void Browser::ActiveTabChanged(WebContents* old_contents,
1014 WebContents* new_contents,
[email protected]3aca95d42011-05-24 22:06:541015 int index,
1016 bool user_gesture) {
[email protected]d500b1c32013-03-12 06:26:081017 content::RecordAction(UserMetricsAction("ActiveTabChanged"));
1018
[email protected]a8f024392011-01-13 21:50:161019 // On some platforms we want to automatically reload tabs that are
1020 // killed when the user selects them.
[email protected]9d7518f2012-04-17 02:15:201021 bool did_reload = false;
[email protected]8e707792012-11-13 10:32:121022 if (user_gesture && ShouldReloadCrashedTab(new_contents)) {
[email protected]b6975b92012-11-03 02:37:451023 LOG(WARNING) << "Reloading killed tab at " << index;
1024 static int reload_count = 0;
1025 UMA_HISTOGRAM_CUSTOM_COUNTS(
1026 "Tabs.SadTab.ReloadCount", ++reload_count, 1, 1000, 50);
1027 chrome::Reload(this, CURRENT_TAB);
1028 did_reload = true;
[email protected]a8f024392011-01-13 21:50:161029 }
1030
[email protected]002efb042011-10-07 17:30:241031 // Discarded tabs always get reloaded.
[email protected]52877dbc62012-06-29 22:22:031032 if (!did_reload && tab_strip_model_->IsTabDiscarded(index)) {
[email protected]d6d4dae2012-04-18 17:47:321033 LOG(WARNING) << "Reloading discarded tab at " << index;
1034 static int reload_count = 0;
[email protected]24c463a2012-04-20 22:33:501035 UMA_HISTOGRAM_CUSTOM_COUNTS(
1036 "Tabs.Discard.ReloadCount", ++reload_count, 1, 1000, 50);
[email protected]a37d4b02012-06-25 21:56:101037 chrome::Reload(this, CURRENT_TAB);
[email protected]002efb042011-10-07 17:30:241038 }
1039
[email protected]36b6dcb2008-11-12 01:19:571040 // If we have any update pending, do it now.
[email protected]24f1477532011-11-22 22:55:361041 if (chrome_updater_factory_.HasWeakPtrs() && old_contents)
[email protected]36b6dcb2008-11-12 01:19:571042 ProcessPendingUIUpdates();
1043
[email protected]36b6dcb2008-11-12 01:19:571044 // Propagate the profile to the location bar.
1045 UpdateToolbar(true);
1046
[email protected]e3a5d5b2012-06-22 17:56:461047 // Propagate tab state to toolbar, tab-strip, etc.
1048 UpdateSearchState(new_contents);
1049
[email protected]943d8122010-06-11 02:13:451050 // Update reload/stop state.
[email protected]8e707792012-11-13 10:32:121051 command_controller_->LoadingStateChanged(new_contents->IsLoading(), true);
[email protected]36b6dcb2008-11-12 01:19:571052
[email protected]84214982008-12-10 18:49:101053 // Update commands to reflect current state.
[email protected]5d98294912012-06-27 22:57:401054 command_controller_->TabStateChanged();
[email protected]36b6dcb2008-11-12 01:19:571055
1056 // Reset the status bubble.
[email protected]be3877f2009-01-14 15:51:101057 StatusBubble* status_bubble = GetStatusBubble();
1058 if (status_bubble) {
1059 status_bubble->Hide();
[email protected]36b6dcb2008-11-12 01:19:571060
[email protected]be3877f2009-01-14 15:51:101061 // Show the loading state (if any).
[email protected]0d56fa7212012-11-15 23:34:021062 status_bubble->SetStatus(CoreTabHelper::FromWebContents(
1063 tab_strip_model_->GetActiveWebContents())->GetStatusText());
[email protected]be3877f2009-01-14 15:51:101064 }
[email protected]36b6dcb2008-11-12 01:19:571065
[email protected]24db8a072009-10-29 20:35:371066 if (HasFindBarController()) {
[email protected]8e707792012-11-13 10:32:121067 find_bar_controller_->ChangeWebContents(new_contents);
[email protected]b77cb302009-10-29 04:09:171068 find_bar_controller_->find_bar()->MoveWindowIfNecessary(gfx::Rect(), true);
[email protected]4801ecc2009-04-05 04:52:581069 }
1070
[email protected]36b6dcb2008-11-12 01:19:571071 // Update sessions. Don't force creation of sessions. If sessions doesn't
1072 // exist, the change will be picked up by sessions when created.
[email protected]92371eb2011-04-28 11:50:151073 SessionService* session_service =
1074 SessionServiceFactory::GetForProfileIfExisting(profile_);
[email protected]2fc15ae2012-05-06 00:01:371075 if (session_service && !tab_strip_model_->closing_all()) {
[email protected]04e3ead52013-01-14 01:09:201076 session_service->SetSelectedTabInWindow(session_id(),
1077 tab_strip_model_->active_index());
[email protected]36b6dcb2008-11-12 01:19:571078 }
[email protected]09b29342011-06-24 19:18:481079
1080 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_SWITCH);
[email protected]e41982a72012-11-20 07:16:511081
1082 // This needs to be called after UpdateSearchState().
1083 if (instant_controller_)
1084 instant_controller_->ActiveTabChanged();
[email protected]36b6dcb2008-11-12 01:19:571085}
1086
[email protected]3abdff22012-11-13 10:28:451087void Browser::TabMoved(WebContents* contents,
[email protected]36b6dcb2008-11-12 01:19:571088 int from_index,
[email protected]1a242c32010-02-02 21:20:541089 int to_index) {
[email protected]36b6dcb2008-11-12 01:19:571090 DCHECK(from_index >= 0 && to_index >= 0);
1091 // Notify the history service.
1092 SyncHistoryWithTabs(std::min(from_index, to_index));
1093}
1094
[email protected]da25dd52011-01-11 19:06:301095void Browser::TabReplacedAt(TabStripModel* tab_strip_model,
[email protected]b624ddc2012-11-15 18:04:131096 WebContents* old_contents,
1097 WebContents* new_contents,
[email protected]8a34e6602010-10-02 17:29:431098 int index) {
[email protected]b624ddc2012-11-15 18:04:131099 TabDetachedAtImpl(old_contents, index, DETACH_TYPE_REPLACE);
[email protected]40600152012-09-26 10:04:311100 SessionService* session_service =
1101 SessionServiceFactory::GetForProfile(profile_);
1102 if (session_service)
[email protected]b624ddc2012-11-15 18:04:131103 session_service->TabClosing(old_contents);
[email protected]04e3ead52013-01-14 01:09:201104 TabInsertedAt(new_contents,
1105 index,
1106 (index == tab_strip_model_->active_index()));
[email protected]c37e3b62010-01-22 00:15:171107
[email protected]b624ddc2012-11-15 18:04:131108 int entry_count = new_contents->GetController().GetEntryCount();
[email protected]c37e3b62010-01-22 00:15:171109 if (entry_count > 0) {
1110 // Send out notification so that observers are updated appropriately.
[email protected]b624ddc2012-11-15 18:04:131111 new_contents->GetController().NotifyEntryChanged(
1112 new_contents->GetController().GetEntryAtIndex(entry_count - 1),
[email protected]c37e3b62010-01-22 00:15:171113 entry_count - 1);
1114 }
[email protected]47e020a2010-10-15 14:43:371115
[email protected]47e020a2010-10-15 14:43:371116 if (session_service) {
1117 // The new_contents may end up with a different navigation stack. Force
1118 // the session service to update itself.
[email protected]52877dbc62012-06-29 22:22:031119 session_service->TabRestored(new_contents,
1120 tab_strip_model_->IsTabPinned(index));
[email protected]47e020a2010-10-15 14:43:371121 }
[email protected]c37e3b62010-01-22 00:15:171122}
1123
[email protected]2c7dba42012-11-09 17:05:421124void Browser::TabPinnedStateChanged(WebContents* contents, int index) {
[email protected]92371eb2011-04-28 11:50:151125 SessionService* session_service =
1126 SessionServiceFactory::GetForProfileIfExisting(profile());
[email protected]5c0e6482009-07-14 20:20:091127 if (session_service) {
[email protected]50a9a76b2012-09-12 14:19:361128 SessionTabHelper* session_tab_helper =
[email protected]2c7dba42012-11-09 17:05:421129 SessionTabHelper::FromWebContents(contents);
1130 session_service->SetPinnedState(session_id(),
1131 session_tab_helper->session_id(),
1132 tab_strip_model_->IsTabPinned(index));
[email protected]5c0e6482009-07-14 20:20:091133 }
1134}
1135
[email protected]36b6dcb2008-11-12 01:19:571136void Browser::TabStripEmpty() {
1137 // Close the frame after we return to the message loop (not immediately,
1138 // otherwise it will destroy this object before the stack has a chance to
1139 // cleanly unwind.)
1140 // Note: This will be called several times if TabStripEmpty is called several
1141 // times. This is because it does not close the window if tabs are
1142 // still present.
[email protected]0586b0e2010-02-12 21:38:371143 MessageLoop::current()->PostTask(
[email protected]24f1477532011-11-22 22:55:361144 FROM_HERE, base::Bind(&Browser::CloseFrame, weak_factory_.GetWeakPtr()));
[email protected]e41982a72012-11-20 07:16:511145
1146 // Instant may have visible WebContents that need to be detached before the
1147 // window system closes.
1148 instant_controller_.reset();
[email protected]36b6dcb2008-11-12 01:19:571149}
1150
[email protected]067310262012-11-22 14:30:411151bool Browser::CanOverscrollContent() const {
1152#if defined(USE_AURA)
[email protected]b75ba132013-02-26 19:38:311153 bool overscroll_enabled = !CommandLine::ForCurrentProcess()->
1154 HasSwitch(switches::kDisableOverscrollHistoryNavigation);
[email protected]507f6cd2013-02-16 00:51:161155 return overscroll_enabled ? !is_app() && !is_devtools() && is_type_tabbed() :
1156 false;
[email protected]067310262012-11-22 14:30:411157#else
1158 return false;
1159#endif
1160}
1161
[email protected]b3996ba2012-08-08 00:39:131162bool Browser::PreHandleKeyboardEvent(content::WebContents* source,
1163 const NativeWebKeyboardEvent& event,
[email protected]fc761142012-06-08 04:49:201164 bool* is_keyboard_shortcut) {
1165 // Escape exits tabbed fullscreen mode.
1166 // TODO(koz): Write a test for this http://crbug.com/100441.
1167 if (event.windowsKeyCode == 27 &&
1168 fullscreen_controller_->HandleUserPressedEscape()) {
1169 return true;
1170 }
1171 return window()->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
1172}
1173
[email protected]b3996ba2012-08-08 00:39:131174void Browser::HandleKeyboardEvent(content::WebContents* source,
1175 const NativeWebKeyboardEvent& event) {
[email protected]fc761142012-06-08 04:49:201176 window()->HandleKeyboardEvent(event);
1177}
1178
[email protected]92655362012-04-20 21:33:171179bool Browser::TabsNeedBeforeUnloadFired() {
[email protected]f9b6c092012-07-09 19:32:281180 return unload_controller_->TabsNeedBeforeUnloadFired();
[email protected]92655362012-04-20 21:33:171181}
1182
[email protected]d013f142012-04-27 17:31:141183bool Browser::IsMouseLocked() const {
1184 return fullscreen_controller_->IsMouseLocked();
[email protected]92655362012-04-20 21:33:171185}
1186
1187void Browser::OnWindowDidShow() {
1188 if (window_has_shown_)
1189 return;
1190 window_has_shown_ = true;
1191
[email protected]b5febf72012-07-29 18:10:091192// CurrentProcessInfo::CreationTime() is currently only implemented on Mac and
1193// Windows.
1194#if defined(OS_MACOSX) || defined(OS_WIN)
1195 // Measure the latency from startup till the first browser window becomes
1196 // visible.
1197 static bool is_first_browser_window = true;
1198 if (is_first_browser_window &&
1199 !startup_metric_utils::WasNonBrowserUIDisplayed()) {
1200 is_first_browser_window = false;
1201 const base::Time* process_creation_time =
1202 base::CurrentProcessInfo::CreationTime();
1203
1204 if (process_creation_time) {
1205 UMA_HISTOGRAM_LONG_TIMES(
1206 "Startup.BrowserWindowDisplay",
1207 base::Time::Now() - *process_creation_time);
1208 }
1209 }
[email protected]9c55de82012-11-01 21:03:521210#endif // defined(OS_MACOSX) || defined(OS_WIN)
[email protected]b5febf72012-07-29 18:10:091211
[email protected]92655362012-04-20 21:33:171212 // Nothing to do for non-tabbed windows.
1213 if (!is_type_tabbed())
1214 return;
1215
1216 // Show any pending global error bubble.
1217 GlobalErrorService* service =
1218 GlobalErrorServiceFactory::GetForProfile(profile());
1219 GlobalError* error = service->GetFirstGlobalErrorWithBubbleView();
1220 if (error)
1221 error->ShowBubbleView(this);
1222}
1223
1224void Browser::ShowFirstRunBubble() {
1225 window()->GetLocationBar()->ShowFirstRunBubble();
1226}
1227
[email protected]ddd28ae2013-03-04 21:11:541228void Browser::MaybeUpdateBookmarkBarStateForInstantOverlay(
[email protected]04143362012-11-28 14:25:181229 const chrome::search::Mode& mode) {
1230 // This is invoked by a platform-specific implementation of
[email protected]ddd28ae2013-03-04 21:11:541231 // |InstantOverlayController| to update bookmark bar state according to
1232 // Instant overlay state.
[email protected]04143362012-11-28 14:25:181233 // ModeChanged() updates bookmark bar state for all mode transitions except
[email protected]74c96b82013-02-04 17:16:371234 // when new mode is |SEARCH_SUGGESTIONS|, because that needs to be done when
1235 // the suggestions are ready.
[email protected]04143362012-11-28 14:25:181236 if (mode.is_search_suggestions() &&
1237 bookmark_bar_state_ == BookmarkBar::SHOW) {
[email protected]e2395da2013-01-23 22:05:351238 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]04143362012-11-28 14:25:181239 }
1240}
1241
[email protected]b3756012013-03-06 17:43:021242void Browser::ShowDownload(content::DownloadItem* download) {
1243 if (!window())
1244 return;
1245
1246 // If the download occurs in a new tab, and it's not a save page
1247 // download (started before initial navigation completed) close it.
1248 WebContents* source = download->GetWebContents();
1249 if (source && source->GetController().IsInitialNavigation() &&
1250 tab_strip_model_->count() > 1 && !download->IsSavePackageDownload()) {
1251 CloseContents(source);
1252 }
1253
1254 // Some (app downloads) are not supposed to appear on the shelf.
1255 if (!DownloadItemModel(download).ShouldShowInShelf())
1256 return;
1257
1258 // GetDownloadShelf creates the download shelf if it was not yet created.
1259 DownloadShelf* shelf = window()->GetDownloadShelf();
1260 shelf->AddDownload(download);
1261}
1262
[email protected]92655362012-04-20 21:33:171263///////////////////////////////////////////////////////////////////////////////
[email protected]674bc592011-12-20 23:00:421264// Browser, content::WebContentsDelegate implementation:
initial.commit09911bf2008-07-26 23:55:291265
[email protected]e5d549d2011-12-28 01:29:201266WebContents* Browser::OpenURLFromTab(WebContents* source,
[email protected]00c37fc2011-08-02 00:22:501267 const OpenURLParams& params) {
[email protected]78e2edc2012-07-01 23:32:281268 chrome::NavigateParams nav_params(this, params.url, params.transition);
[email protected]19c64142013-03-06 20:08:531269 FillNavigateParamsFromOpenURLParams(&nav_params, params);
[email protected]e232c992012-12-06 12:43:201270 nav_params.source_contents = source;
[email protected]00c37fc2011-08-02 00:22:501271 nav_params.tabstrip_add_types = TabStripModel::ADD_NONE;
[email protected]78e2edc2012-07-01 23:32:281272 nav_params.window_action = chrome::NavigateParams::SHOW_WINDOW;
[email protected]00c37fc2011-08-02 00:22:501273 nav_params.user_gesture = true;
[email protected]78e2edc2012-07-01 23:32:281274 chrome::Navigate(&nav_params);
[email protected]00c37fc2011-08-02 00:22:501275
[email protected]e232c992012-12-06 12:43:201276 return nav_params.target_contents;
initial.commit09911bf2008-07-26 23:55:291277}
1278
[email protected]2a6bc3e2011-12-28 23:51:331279void Browser::NavigationStateChanged(const WebContents* source,
initial.commit09911bf2008-07-26 23:55:291280 unsigned changed_flags) {
initial.commit09911bf2008-07-26 23:55:291281 // Only update the UI when something visible has changed.
[email protected]e83f1682008-09-07 23:57:401282 if (changed_flags)
initial.commit09911bf2008-07-26 23:55:291283 ScheduleUIUpdate(source, changed_flags);
1284
[email protected]83c2e232011-10-07 21:36:461285 // We can synchronously update commands since they will only change once per
1286 // navigation, so we don't have to worry about flickering. We do, however,
1287 // need to update the command state early on load to always present usable
1288 // actions in the face of slow-to-commit pages.
[email protected]d9083482012-01-06 00:38:461289 if (changed_flags & (content::INVALIDATE_TYPE_URL |
1290 content::INVALIDATE_TYPE_LOAD))
[email protected]5d98294912012-06-27 22:57:401291 command_controller_->TabStateChanged();
initial.commit09911bf2008-07-26 23:55:291292}
1293
[email protected]2a6bc3e2011-12-28 23:51:331294void Browser::AddNewContents(WebContents* source,
1295 WebContents* new_contents,
initial.commit09911bf2008-07-26 23:55:291296 WindowOpenDisposition disposition,
1297 const gfx::Rect& initial_pos,
[email protected]eda238a12012-09-07 23:44:001298 bool user_gesture,
1299 bool* was_blocked) {
[email protected]52877dbc62012-06-29 22:22:031300 chrome::AddWebContents(this, source, new_contents, disposition, initial_pos,
[email protected]eda238a12012-09-07 23:44:001301 user_gesture, was_blocked);
initial.commit09911bf2008-07-26 23:55:291302}
1303
[email protected]2a6bc3e2011-12-28 23:51:331304void Browser::ActivateContents(WebContents* contents) {
[email protected]0d56fa7212012-11-15 23:34:021305 tab_strip_model_->ActivateTabAt(
1306 tab_strip_model_->GetIndexOfWebContents(contents), false);
[email protected]f3e99e32008-07-30 04:48:391307 window_->Activate();
initial.commit09911bf2008-07-26 23:55:291308}
1309
[email protected]2a6bc3e2011-12-28 23:51:331310void Browser::DeactivateContents(WebContents* contents) {
[email protected]ea42e7782010-08-23 23:58:121311 window_->Deactivate();
1312}
1313
[email protected]2a6bc3e2011-12-28 23:51:331314void Browser::LoadingStateChanged(WebContents* source) {
[email protected]2fc15ae2012-05-06 00:01:371315 window_->UpdateLoadingAnimations(tab_strip_model_->TabsAreLoading());
[email protected]f3e99e32008-07-30 04:48:391316 window_->UpdateTitleBar();
initial.commit09911bf2008-07-26 23:55:291317
[email protected]0d56fa7212012-11-15 23:34:021318 WebContents* selected_contents = tab_strip_model_->GetActiveWebContents();
[email protected]3c9e1872010-11-18 16:17:491319 if (source == selected_contents) {
[email protected]f5d978c2011-07-21 14:43:511320 bool is_loading = source->IsLoading();
[email protected]5d98294912012-06-27 22:57:401321 command_controller_->LoadingStateChanged(is_loading, false);
[email protected]43d259d2010-09-03 21:37:461322 if (GetStatusBubble()) {
[email protected]0d56fa7212012-11-15 23:34:021323 GetStatusBubble()->SetStatus(CoreTabHelper::FromWebContents(
1324 tab_strip_model_->GetActiveWebContents())->GetStatusText());
[email protected]43d259d2010-09-03 21:37:461325 }
initial.commit09911bf2008-07-26 23:55:291326 }
1327}
1328
[email protected]a81343d232011-12-27 07:39:201329void Browser::CloseContents(WebContents* source) {
[email protected]f9b6c092012-07-09 19:32:281330 if (unload_controller_->CanCloseContents(source))
[email protected]81ddd3402012-12-28 00:55:261331 chrome::CloseWebContents(this, source, true);
initial.commit09911bf2008-07-26 23:55:291332}
1333
[email protected]2a6bc3e2011-12-28 23:51:331334void Browser::MoveContents(WebContents* source, const gfx::Rect& pos) {
[email protected]b35b26b32011-05-05 20:35:141335 if (!IsPopupOrPanel(source)) {
initial.commit09911bf2008-07-26 23:55:291336 NOTREACHED() << "moving invalid browser type";
1337 return;
1338 }
[email protected]e561e202010-03-24 17:57:121339 window_->SetBounds(pos);
initial.commit09911bf2008-07-26 23:55:291340}
1341
[email protected]2a6bc3e2011-12-28 23:51:331342bool Browser::IsPopupOrPanel(const WebContents* source) const {
initial.commit09911bf2008-07-26 23:55:291343 // A non-tabbed BROWSER is an unconstrained popup.
[email protected]b35b26b32011-05-05 20:35:141344 return is_type_popup() || is_type_panel();
initial.commit09911bf2008-07-26 23:55:291345}
1346
[email protected]2a6bc3e2011-12-28 23:51:331347void Browser::UpdateTargetURL(WebContents* source, int32 page_id,
[email protected]d952a052011-09-06 18:42:451348 const GURL& url) {
[email protected]be3877f2009-01-14 15:51:101349 if (!GetStatusBubble())
1350 return;
1351
[email protected]0d56fa7212012-11-15 23:34:021352 if (source == tab_strip_model_->GetActiveWebContents()) {
[email protected]36b6dcb2008-11-12 01:19:571353 PrefService* prefs = profile_->GetPrefs();
[email protected]65233052011-08-22 19:02:481354 GetStatusBubble()->SetURL(url, prefs->GetString(prefs::kAcceptLanguages));
[email protected]36b6dcb2008-11-12 01:19:571355 }
1356}
1357
[email protected]92655362012-04-20 21:33:171358void Browser::ContentsMouseEvent(
1359 WebContents* source, const gfx::Point& location, bool motion) {
1360 if (!GetStatusBubble())
1361 return;
1362
[email protected]0d56fa7212012-11-15 23:34:021363 if (source == tab_strip_model_->GetActiveWebContents()) {
[email protected]92655362012-04-20 21:33:171364 GetStatusBubble()->MouseMoved(location, !motion);
1365 if (!motion)
1366 GetStatusBubble()->SetURL(GURL(), std::string());
1367 }
[email protected]3a6a3b62009-05-27 21:36:201368}
1369
[email protected]36b6dcb2008-11-12 01:19:571370void Browser::ContentsZoomChange(bool zoom_in) {
[email protected]5d98294912012-06-27 22:57:401371 chrome::ExecuteCommand(this, zoom_in ? IDC_ZOOM_PLUS : IDC_ZOOM_MINUS);
[email protected]36b6dcb2008-11-12 01:19:571372}
1373
[email protected]2a6bc3e2011-12-28 23:51:331374void Browser::WebContentsFocused(WebContents* contents) {
1375 window_->WebContentsFocused(contents);
[email protected]7e383692009-06-12 19:14:541376}
1377
[email protected]b3996ba2012-08-08 00:39:131378bool Browser::TakeFocus(content::WebContents* source,
1379 bool reverse) {
[email protected]ad50def52011-10-19 23:17:071380 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271381 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:531382 content::Source<Browser>(this),
[email protected]ad50def52011-10-19 23:17:071383 content::NotificationService::NoDetails());
[email protected]130efb02009-09-18 18:54:351384 return false;
1385}
1386
[email protected]b7a756d42012-01-23 18:08:171387gfx::Rect Browser::GetRootWindowResizerRect() const {
1388 return window_->GetRootWindowResizerRect();
1389}
1390
[email protected]1d3e9172012-06-12 02:59:131391void Browser::BeforeUnloadFired(WebContents* web_contents,
[email protected]36b6dcb2008-11-12 01:19:571392 bool proceed,
1393 bool* proceed_to_fire_unload) {
[email protected]f9b6c092012-07-09 19:32:281394 *proceed_to_fire_unload =
1395 unload_controller_->BeforeUnloadFired(web_contents, proceed);
[email protected]36b6dcb2008-11-12 01:19:571396}
1397
[email protected]0c9406632013-02-08 01:13:331398bool Browser::ShouldFocusLocationBarByDefault(WebContents* source) {
[email protected]3424e40f2013-03-05 00:51:521399 return chrome::search::IsInstantNTP(source);
[email protected]0c9406632013-02-08 01:13:331400}
1401
[email protected]a26dc362010-04-23 01:48:581402void Browser::SetFocusToLocationBar(bool select_all) {
[email protected]1a380622009-02-17 17:33:131403 // Two differences between this and FocusLocationBar():
1404 // (1) This doesn't get recorded in user metrics, since it's called
1405 // internally.
1406 // (2) This checks whether the location bar can be focused, and if not, clears
1407 // the focus. FocusLocationBar() is only reached when the location bar is
1408 // focusable, but this may be reached at other times, e.g. while in
1409 // fullscreen mode, where we need to leave focus in a consistent state.
[email protected]a26dc362010-04-23 01:48:581410 window_->SetFocusToLocationBar(select_all);
[email protected]4bcefee2009-01-29 15:57:571411}
1412
[email protected]7d5925a2009-03-03 02:59:121413void Browser::RenderWidgetShowing() {
1414 window_->DisableInactiveFrame();
1415}
[email protected]a239c3f2009-02-17 22:13:191416
[email protected]0a2aeb82009-05-15 21:52:481417int Browser::GetExtraRenderViewHeight() const {
1418 return window_->GetExtraRenderViewHeight();
1419}
1420
[email protected]2a6bc3e2011-12-28 23:51:331421void Browser::ViewSourceForTab(WebContents* source, const GURL& page_url) {
[email protected]1788e772010-12-15 16:40:501422 DCHECK(source);
[email protected]ab93b6372012-11-28 05:20:221423 chrome::ViewSource(this, source);
[email protected]77d8d622010-12-15 10:30:121424}
1425
[email protected]2a6bc3e2011-12-28 23:51:331426void Browser::ViewSourceForFrame(WebContents* source,
[email protected]932b7a12011-03-09 12:50:271427 const GURL& frame_url,
1428 const std::string& frame_content_state) {
1429 DCHECK(source);
[email protected]ab93b6372012-11-28 05:20:221430 chrome::ViewSource(this, source, frame_url, frame_content_state);
[email protected]932b7a12011-03-09 12:50:271431}
1432
[email protected]4fdf6742012-01-10 20:14:361433void Browser::ShowRepostFormWarningDialog(WebContents* source) {
[email protected]51296c62012-09-26 13:02:441434 TabModalConfirmDialog::Create(new RepostFormWarningController(source),
[email protected]70320ab2012-11-02 19:07:161435 source);
[email protected]14f3408a2009-08-31 20:53:531436}
1437
[email protected]4fdf6742012-01-10 20:14:361438bool Browser::ShouldCreateWebContents(
1439 WebContents* web_contents,
1440 int route_id,
1441 WindowContainerType window_container_type,
[email protected]03b6d552012-03-29 04:03:011442 const string16& frame_name,
1443 const GURL& target_url) {
[email protected]4fdf6742012-01-10 20:14:361444 if (window_container_type == WINDOW_CONTAINER_TYPE_BACKGROUND) {
1445 // If a BackgroundContents is created, suppress the normal WebContents.
1446 return !MaybeCreateBackgroundContents(
[email protected]03b6d552012-03-29 04:03:011447 route_id, web_contents, frame_name, target_url);
[email protected]4fdf6742012-01-10 20:14:361448 }
1449
1450 return true;
1451}
1452
1453void Browser::WebContentsCreated(WebContents* source_contents,
1454 int64 source_frame_id,
1455 const GURL& target_url,
1456 WebContents* new_contents) {
[email protected]6451fe92012-08-30 18:37:261457 // Adopt the WebContents now, so all observers are in place, as the network
[email protected]3d61dd02012-06-07 00:05:531458 // requests for its initial navigation will start immediately. The WebContents
1459 // will later be inserted into this browser using Browser::Navigate via
[email protected]6451fe92012-08-30 18:37:261460 // AddNewContents.
[email protected]27e5b3912012-12-16 00:45:371461 BrowserTabContents::AttachTabHelpers(new_contents);
[email protected]4fdf6742012-01-10 20:14:361462
1463 // Notify.
1464 RetargetingDetails details;
1465 details.source_web_contents = source_contents;
1466 details.source_frame_id = source_frame_id;
1467 details.target_url = target_url;
1468 details.target_web_contents = new_contents;
1469 details.not_yet_in_tabstrip = true;
1470 content::NotificationService::current()->Notify(
1471 chrome::NOTIFICATION_RETARGETING,
1472 content::Source<Profile>(profile_),
1473 content::Details<RetargetingDetails>(&details));
[email protected]80c7b802011-10-28 19:59:081474}
1475
[email protected]2a6bc3e2011-12-28 23:51:331476void Browser::ContentRestrictionsChanged(WebContents* source) {
[email protected]5d98294912012-06-27 22:57:401477 command_controller_->ContentRestrictionsChanged();
[email protected]9e823662010-10-13 23:36:001478}
1479
[email protected]2a6bc3e2011-12-28 23:51:331480void Browser::RendererUnresponsive(WebContents* source) {
[email protected]a7ed0ca2012-12-06 19:30:061481 // Ignore hangs if a tab is blocked.
1482 int index = tab_strip_model_->GetIndexOfWebContents(source);
1483 DCHECK_NE(TabStripModel::kNoTab, index);
1484 if (tab_strip_model_->IsTabBlocked(index))
1485 return;
[email protected]c19d9542011-11-15 22:26:421486
[email protected]87586fb72012-07-02 13:15:021487 chrome::ShowHungRendererDialog(source);
[email protected]55452902011-06-01 21:57:471488}
1489
[email protected]2a6bc3e2011-12-28 23:51:331490void Browser::RendererResponsive(WebContents* source) {
[email protected]87586fb72012-07-02 13:15:021491 chrome::HideHungRendererDialog(source);
[email protected]55452902011-06-01 21:57:471492}
1493
[email protected]2a6bc3e2011-12-28 23:51:331494void Browser::WorkerCrashed(WebContents* source) {
[email protected]0be09932013-01-08 02:03:501495 SimpleAlertInfoBarDelegate::Create(
1496 InfoBarService::FromWebContents(source), NULL,
1497 l10n_util::GetStringUTF16(IDS_WEBWORKER_CRASHED_PROMPT), true);
[email protected]0dd3a0ab2011-02-18 08:17:441498}
1499
[email protected]1d3e9172012-06-12 02:59:131500void Browser::DidNavigateMainFramePostCommit(WebContents* web_contents) {
[email protected]0d56fa7212012-11-15 23:34:021501 if (web_contents == tab_strip_model_->GetActiveWebContents())
[email protected]09b29342011-06-24 19:18:481502 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]93f230e02011-06-01 14:40:001503}
1504
[email protected]1d3e9172012-06-12 02:59:131505void Browser::DidNavigateToPendingEntry(WebContents* web_contents) {
[email protected]0d56fa7212012-11-15 23:34:021506 if (web_contents == tab_strip_model_->GetActiveWebContents())
[email protected]09b29342011-06-24 19:18:481507 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]93f230e02011-06-01 14:40:001508}
1509
[email protected]71a88bb2013-02-01 22:05:151510content::JavaScriptDialogManager* Browser::GetJavaScriptDialogManager() {
1511 return GetJavaScriptDialogManagerInstance();
[email protected]3ab9cb82011-06-03 18:02:071512}
1513
[email protected]1d3e9172012-06-12 02:59:131514content::ColorChooser* Browser::OpenColorChooser(WebContents* web_contents,
[email protected]da8543762012-03-20 08:52:201515 int color_chooser_id,
[email protected]55578b0a2012-04-18 14:31:321516 SkColor color) {
[email protected]da8543762012-03-20 08:52:201517#if defined(OS_WIN)
1518 // On Windows, only create a color chooser if one doesn't exist, because we
1519 // can't close the old color chooser dialog.
1520 if (!color_chooser_.get())
[email protected]1d3e9172012-06-12 02:59:131521 color_chooser_.reset(content::ColorChooser::Create(color_chooser_id,
1522 web_contents,
[email protected]da8543762012-03-20 08:52:201523 color));
1524#else
1525 if (color_chooser_.get())
1526 color_chooser_->End();
[email protected]1d3e9172012-06-12 02:59:131527 color_chooser_.reset(content::ColorChooser::Create(color_chooser_id,
1528 web_contents,
[email protected]da8543762012-03-20 08:52:201529 color));
1530#endif
1531 return color_chooser_.get();
1532}
1533
1534void Browser::DidEndColorChooser() {
1535 color_chooser_.reset();
1536}
1537
[email protected]1d3e9172012-06-12 02:59:131538void Browser::RunFileChooser(WebContents* web_contents,
[email protected]8caadeb2011-11-22 02:45:231539 const content::FileChooserParams& params) {
[email protected]1d3e9172012-06-12 02:59:131540 FileSelectHelper::RunFileChooser(web_contents, params);
[email protected]3a29a6e2011-08-24 18:26:211541}
1542
[email protected]1d3e9172012-06-12 02:59:131543void Browser::EnumerateDirectory(WebContents* web_contents,
[email protected]33f8ad52012-05-22 18:10:131544 int request_id,
[email protected]650b2d52013-02-10 03:41:451545 const base::FilePath& path) {
[email protected]1d3e9172012-06-12 02:59:131546 FileSelectHelper::EnumerateDirectory(web_contents, request_id, path);
[email protected]3a29a6e2011-08-24 18:26:211547}
1548
[email protected]1d3e9172012-06-12 02:59:131549void Browser::ToggleFullscreenModeForTab(WebContents* web_contents,
[email protected]01a5a762011-11-21 05:40:551550 bool enter_fullscreen) {
[email protected]1d3e9172012-06-12 02:59:131551 fullscreen_controller_->ToggleFullscreenModeForTab(web_contents,
1552 enter_fullscreen);
[email protected]8a5e0ca2011-08-25 06:30:471553}
1554
[email protected]1d3e9172012-06-12 02:59:131555bool Browser::IsFullscreenForTabOrPending(
1556 const WebContents* web_contents) const {
1557 return fullscreen_controller_->IsFullscreenForTabOrPending(web_contents);
[email protected]5d5f7af2011-10-01 01:38:121558}
1559
[email protected]1d3e9172012-06-12 02:59:131560void Browser::JSOutOfMemory(WebContents* web_contents) {
1561 JSOutOfMemoryHelper(web_contents);
[email protected]7d189022011-08-25 22:54:201562}
1563
[email protected]1d3e9172012-06-12 02:59:131564void Browser::RegisterProtocolHandler(WebContents* web_contents,
[email protected]7d189022011-08-25 22:54:201565 const std::string& protocol,
1566 const GURL& url,
[email protected]3a3b75a2012-06-01 08:38:361567 const string16& title,
1568 bool user_gesture) {
[email protected]1d3e9172012-06-12 02:59:131569 RegisterProtocolHandlerHelper(
[email protected]7ae9c322012-07-17 23:46:441570 web_contents, protocol, url, title, user_gesture, window());
[email protected]7d189022011-08-25 22:54:201571}
1572
[email protected]2a6bc3e2011-12-28 23:51:331573void Browser::UpdatePreferredSize(WebContents* source,
[email protected]0548c5352011-09-07 00:33:331574 const gfx::Size& pref_size) {
1575 window_->UpdatePreferredSize(source, pref_size);
1576}
1577
[email protected]61e2b3cc2012-03-02 16:13:341578void Browser::ResizeDueToAutoResize(WebContents* source,
1579 const gfx::Size& new_size) {
1580 window_->ResizeDueToAutoResize(source, new_size);
1581}
1582
[email protected]1d3e9172012-06-12 02:59:131583void Browser::FindReply(WebContents* web_contents,
[email protected]92655362012-04-20 21:33:171584 int request_id,
1585 int number_of_matches,
1586 const gfx::Rect& selection_rect,
1587 int active_match_ordinal,
1588 bool final_update) {
[email protected]1d3e9172012-06-12 02:59:131589 FindReplyHelper(web_contents, request_id, number_of_matches, selection_rect,
[email protected]92655362012-04-20 21:33:171590 active_match_ordinal, final_update);
1591}
1592
[email protected]1d3e9172012-06-12 02:59:131593void Browser::RequestToLockMouse(WebContents* web_contents,
[email protected]a9c81f02012-06-01 00:15:441594 bool user_gesture,
1595 bool last_unlocked_by_target) {
[email protected]1d3e9172012-06-12 02:59:131596 fullscreen_controller_->RequestToLockMouse(web_contents,
[email protected]a9c81f02012-06-01 00:15:441597 user_gesture,
1598 last_unlocked_by_target);
[email protected]55c87fa2011-10-15 07:28:281599}
1600
[email protected]5ef8e372011-10-18 04:25:551601void Browser::LostMouseLock() {
[email protected]01a5a762011-11-21 05:40:551602 fullscreen_controller_->LostMouseLock();
[email protected]5ef8e372011-10-18 04:25:551603}
1604
[email protected]f13b4202012-06-12 23:53:231605void Browser::RequestMediaAccessPermission(
1606 content::WebContents* web_contents,
[email protected]33662e52013-01-07 21:31:091607 const content::MediaStreamRequest& request,
[email protected]f13b4202012-06-12 23:53:231608 const content::MediaResponseCallback& callback) {
[email protected]f7bbda82013-02-07 00:32:221609 // The case when microphone access is requested together with screen capturing
1610 // is not supported yet. Just check requested video type to decide which
1611 // infobar to show.
1612 //
1613 // TODO(sergeyu): Add support for video stream with microphone, e.g. refactor
1614 // MediaStreamDevicesController to use a single infobar for both permissions,
1615 // or maybe show two infobars.
1616 if (request.video_type == content::MEDIA_SCREEN_VIDEO_CAPTURE)
1617 ScreenCaptureInfoBarDelegate::Create(web_contents, request, callback);
1618 else
1619 MediaStreamInfoBarDelegate::Create(web_contents, request, callback);
[email protected]f13b4202012-06-12 23:53:231620}
1621
[email protected]8c8fc292012-11-23 18:57:161622bool Browser::RequestPpapiBrokerPermission(
1623 WebContents* web_contents,
1624 const GURL& url,
[email protected]650b2d52013-02-10 03:41:451625 const base::FilePath& plugin_path,
[email protected]8c8fc292012-11-23 18:57:161626 const base::Callback<void(bool)>& callback) {
[email protected]0be09932013-01-08 02:03:501627 PepperBrokerInfoBarDelegate::Create(web_contents, url, plugin_path, callback);
[email protected]8c8fc292012-11-23 18:57:161628 return true;
1629}
1630
[email protected]36b6dcb2008-11-12 01:19:571631///////////////////////////////////////////////////////////////////////////////
[email protected]51d83bdf2011-12-09 16:09:291632// Browser, CoreTabHelperDelegate implementation:
[email protected]a0366a52011-02-04 20:04:211633
[email protected]ece4aca2012-09-19 22:22:581634void Browser::SwapTabContents(content::WebContents* old_contents,
1635 content::WebContents* new_contents) {
1636 int index = tab_strip_model_->GetIndexOfWebContents(old_contents);
[email protected]92655362012-04-20 21:33:171637 DCHECK_NE(TabStripModel::kNoTab, index);
[email protected]59fa29fb2012-12-07 22:13:421638 tab_strip_model_->ReplaceWebContentsAt(index, new_contents);
[email protected]92655362012-04-20 21:33:171639}
1640
[email protected]ece4aca2012-09-19 22:22:581641bool Browser::CanReloadContents(content::WebContents* web_contents) const {
[email protected]5d98294912012-06-27 22:57:401642 return chrome::CanReload(this);
[email protected]e1461952012-06-14 21:05:351643}
1644
[email protected]ece4aca2012-09-19 22:22:581645bool Browser::CanSaveContents(content::WebContents* web_contents) const {
[email protected]5d98294912012-06-27 22:57:401646 return chrome::CanSavePage(this);
[email protected]e1461952012-06-14 21:05:351647}
1648
[email protected]92655362012-04-20 21:33:171649///////////////////////////////////////////////////////////////////////////////
1650// Browser, SearchEngineTabHelperDelegate implementation:
1651
[email protected]3613347d2012-04-27 20:27:371652void Browser::ConfirmAddSearchProvider(TemplateURL* template_url,
[email protected]92655362012-04-20 21:33:171653 Profile* profile) {
1654 window()->ConfirmAddSearchProvider(template_url, profile);
1655}
1656
1657///////////////////////////////////////////////////////////////////////////////
[email protected]d2b16572013-01-03 00:41:581658// Browser, WebContentsModalDialogManagerDelegate implementation:
[email protected]92655362012-04-20 21:33:171659
[email protected]c5731a1c2012-12-22 10:18:451660void Browser::SetWebContentsBlocked(content::WebContents* web_contents,
1661 bool blocked) {
[email protected]c6464ad2012-10-03 00:27:281662 int index = tab_strip_model_->GetIndexOfWebContents(web_contents);
[email protected]92655362012-04-20 21:33:171663 if (index == TabStripModel::kNoTab) {
1664 NOTREACHED();
1665 return;
1666 }
[email protected]2fc15ae2012-05-06 00:01:371667 tab_strip_model_->SetTabBlocked(index, blocked);
[email protected]0d56fa7212012-11-15 23:34:021668 if (!blocked && tab_strip_model_->GetActiveWebContents() == web_contents)
[email protected]f3615f02013-02-26 06:09:061669 web_contents->GetView()->Focus();
[email protected]92655362012-04-20 21:33:171670}
1671
[email protected]c5731a1c2012-12-22 10:18:451672bool Browser::GetDialogTopCenter(gfx::Point* point) {
[email protected]0d7f382f2012-10-26 00:57:511673 int y = 0;
1674 if (window_->GetConstrainedWindowTopY(&y)) {
1675 *point = gfx::Point(window_->GetBounds().width() / 2, y);
1676 return true;
1677 }
1678
1679 return false;
[email protected]6a151762012-10-03 22:33:191680}
1681
[email protected]92655362012-04-20 21:33:171682///////////////////////////////////////////////////////////////////////////////
1683// Browser, BlockedContentTabHelperDelegate implementation:
1684
[email protected]6790f95f2012-09-21 19:49:451685content::WebContents* Browser::GetConstrainingWebContents(
1686 content::WebContents* source) {
[email protected]92655362012-04-20 21:33:171687 return source;
1688}
1689
1690///////////////////////////////////////////////////////////////////////////////
1691// Browser, BookmarkTabHelperDelegate implementation:
1692
[email protected]1c5119c2012-09-19 00:08:571693void Browser::URLStarredChanged(content::WebContents* web_contents,
1694 bool starred) {
[email protected]0d56fa7212012-11-15 23:34:021695 if (web_contents == tab_strip_model_->GetActiveWebContents())
[email protected]92655362012-04-20 21:33:171696 window_->SetStarredState(starred);
1697}
1698
1699///////////////////////////////////////////////////////////////////////////////
[email protected]6f80e932012-06-04 19:00:071700// Browser, ZoomObserver implementation:
1701
[email protected]dea9cee82012-09-25 03:10:521702void Browser::OnZoomChanged(content::WebContents* source,
1703 bool can_show_bubble) {
[email protected]0d56fa7212012-11-15 23:34:021704 if (source == tab_strip_model_->GetActiveWebContents()) {
[email protected]6f80e932012-06-04 19:00:071705 // Only show the zoom bubble for zoom changes in the active window.
[email protected]5423c372012-08-22 05:50:161706 window_->ZoomChangedForActiveTab(can_show_bubble && window_->IsActive());
[email protected]6f80e932012-06-04 19:00:071707 }
1708}
1709
1710///////////////////////////////////////////////////////////////////////////////
[email protected]92f54082012-07-31 01:43:141711// Browser, ui::SelectFileDialog::Listener implementation:
[email protected]36b6dcb2008-11-12 01:19:571712
[email protected]650b2d52013-02-10 03:41:451713void Browser::FileSelected(const base::FilePath& path, int index,
1714 void* params) {
[email protected]53f04c82012-07-26 02:31:091715 FileSelectedWithExtraInfo(ui::SelectedFileInfo(path, path), index, params);
1716}
1717
1718void Browser::FileSelectedWithExtraInfo(
1719 const ui::SelectedFileInfo& file_info,
1720 int index,
1721 void* params) {
1722 profile_->set_last_selected_directory(file_info.file_path.DirName());
1723
[email protected]650b2d52013-02-10 03:41:451724 const base::FilePath& path = file_info.local_path;
[email protected]72cbd322009-04-07 10:17:121725 GURL file_url = net::FilePathToFileURL(path);
[email protected]b7813a22012-04-04 18:41:021726
1727#if defined(OS_CHROMEOS)
[email protected]d9d04df2012-10-12 07:06:351728 drive::util::ModifyDriveFileResourceUrl(profile_, path, &file_url);
[email protected]b7813a22012-04-04 18:41:021729#endif
1730
[email protected]e5d549d2011-12-28 01:29:201731 if (file_url.is_empty())
1732 return;
1733
1734 OpenURL(OpenURLParams(
1735 file_url, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
1736 false));
[email protected]36b6dcb2008-11-12 01:19:571737}
1738
1739///////////////////////////////////////////////////////////////////////////////
[email protected]6c2381d2011-10-19 02:52:531740// Browser, content::NotificationObserver implementation:
[email protected]36b6dcb2008-11-12 01:19:571741
[email protected]432115822011-07-10 15:52:271742void Browser::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:531743 const content::NotificationSource& source,
1744 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:271745 switch (type) {
[email protected]432115822011-07-10 15:52:271746 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
[email protected]ce696a452011-05-31 17:47:361747 if (window()->GetLocationBar())
1748 window()->GetLocationBar()->UpdatePageActions();
[email protected]57f71b92009-09-11 19:31:381749
[email protected]b3f7fe22011-11-11 19:27:561750 // Close any tabs from the unloaded extension, unless it's terminated,
1751 // in which case let the sad tabs remain.
[email protected]1c321ee52012-05-21 03:02:341752 if (content::Details<extensions::UnloadedExtensionInfo>(
1753 details)->reason != extension_misc::UNLOAD_REASON_TERMINATE) {
[email protected]b3f7fe22011-11-11 19:27:561754 const Extension* extension =
[email protected]1c321ee52012-05-21 03:02:341755 content::Details<extensions::UnloadedExtensionInfo>(
1756 details)->extension;
[email protected]e344b5a2012-11-27 07:04:351757 // Iterate backwards as we may remove items while iterating.
[email protected]2fc15ae2012-05-06 00:01:371758 for (int i = tab_strip_model_->count() - 1; i >= 0; --i) {
[email protected]0d56fa7212012-11-15 23:34:021759 WebContents* web_contents = tab_strip_model_->GetWebContentsAt(i);
[email protected]e344b5a2012-11-27 07:04:351760 // Two cases are handled here:
1761 // - The scheme check is for when an extension page is loaded in a
1762 // tab, e.g. chrome-extension://id/page.html.
1763 // - The extension_app check is for apps, which can have non-extension
1764 // schemes, e.g. https://mail.google.com if you have the Gmail app
1765 // installed.
1766 if ((web_contents->GetURL().SchemeIs(extensions::kExtensionScheme) &&
1767 web_contents->GetURL().host() == extension->id()) ||
1768 (extensions::TabHelper::FromWebContents(
1769 web_contents)->extension_app() == extension)) {
[email protected]526b45e2012-11-28 20:22:461770 tab_strip_model_->CloseWebContentsAt(i, TabStripModel::CLOSE_NONE);
[email protected]e344b5a2012-11-27 07:04:351771 }
[email protected]894bb502009-05-21 22:39:571772 }
1773 }
1774 break;
1775 }
1776
[email protected]432115822011-07-10 15:52:271777 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:531778 Profile* profile = content::Source<Profile>(source).ptr();
[email protected]fe8944f2011-08-30 17:47:321779 if (profile_->IsSameProfile(profile) && window()->GetLocationBar())
[email protected]ce696a452011-05-31 17:47:361780 window()->GetLocationBar()->InvalidatePageActions();
[email protected]371ed7a2009-08-25 15:22:461781 break;
1782 }
1783
[email protected]432115822011-07-10 15:52:271784 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED:
1785 case chrome::NOTIFICATION_EXTENSION_LOADED:
[email protected]1797f4a2011-08-30 18:00:281786 // During window creation on Windows we may end up calling into
1787 // SHAppBarMessage, which internally spawns a nested message loop. This
1788 // makes it possible for us to end up here before window creation has
1789 // completed,at which point window_ is NULL. See 94752 for details.
1790 if (window() && window()->GetLocationBar())
[email protected]ce696a452011-05-31 17:47:361791 window()->GetLocationBar()->UpdatePageActions();
[email protected]0dfe05c2011-02-23 23:03:361792 break;
[email protected]ad0c2e1b2010-01-30 00:00:101793
[email protected]6b40bb582012-03-15 20:50:381794#if defined(ENABLE_THEMES)
[email protected]432115822011-07-10 15:52:271795 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED:
[email protected]e001d412009-06-26 20:53:251796 window()->UserChangedTheme();
1797 break;
[email protected]6b40bb582012-03-15 20:50:381798#endif
[email protected]e001d412009-06-26 20:53:251799
[email protected]ea049a02011-12-25 21:37:091800 case chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED: {
1801 WebContents* web_contents = content::Source<WebContents>(source).ptr();
[email protected]0d56fa7212012-11-15 23:34:021802 if (web_contents == tab_strip_model_->GetActiveWebContents()) {
[email protected]9a9a9e22011-05-13 22:20:351803 LocationBar* location_bar = window()->GetLocationBar();
1804 if (location_bar)
1805 location_bar->UpdateContentSettingsIcons();
1806 }
1807 break;
1808 }
1809
[email protected]e83f1682008-09-07 23:57:401810 default:
1811 NOTREACHED() << "Got a notification we didn't register for.";
initial.commit09911bf2008-07-26 23:55:291812 }
1813}
1814
[email protected]c3166302012-09-25 21:27:081815void Browser::ModeChanged(const chrome::search::Mode& old_mode,
1816 const chrome::search::Mode& new_mode) {
[email protected]74c96b82013-02-04 17:16:371817 // If new mode is |SEARCH_SUGGESTIONS|, don't update bookmark bar state now;
[email protected]ddd28ae2013-03-04 21:11:541818 // wait till the Instant overlay is ready to show suggestions before hiding
1819 // the bookmark bar (in MaybeUpdateBookmarkBarStateForInstantOverlay()).
[email protected]74c96b82013-02-04 17:16:371820 // TODO(kuan): but for now, only delay updating bookmark bar state if origin
1821 // is |DEFAULT|; other origins require more complex logic to be implemented
1822 // to prevent jankiness caused by hiding bookmark bar, so just hide the
1823 // bookmark bar immediately and tolerate the jankiness for a while.
[email protected]04143362012-11-28 14:25:181824 // For other mode transitions, update bookmark bar state accordingly.
[email protected]74c96b82013-02-04 17:16:371825 if (new_mode.is_search_suggestions() &&
1826 new_mode.is_origin_default() &&
[email protected]04143362012-11-28 14:25:181827 bookmark_bar_state_ == BookmarkBar::SHOW) {
1828 return;
1829 }
[email protected]c3166302012-09-25 21:27:081830 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
1831}
1832
[email protected]d3b98c82010-07-14 07:45:591833///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:571834// Browser, Command and state updating (private):
1835
[email protected]c31813a2012-11-15 18:47:261836void Browser::OnDevToolsDisabledChanged() {
1837 if (profile_->GetPrefs()->GetBoolean(prefs::kDevToolsDisabled))
1838 content::DevToolsManager::GetInstance()->CloseAllClientHosts();
1839}
1840
1841void Browser::MarkHomePageAsChanged() {
1842 profile_->GetPrefs()->SetBoolean(prefs::kHomePageChanged, true);
[email protected]6c6b02d2011-09-02 03:36:471843}
1844
[email protected]36b6dcb2008-11-12 01:19:571845///////////////////////////////////////////////////////////////////////////////
1846// Browser, UI update coalescing and handling (private):
1847
1848void Browser::UpdateToolbar(bool should_restore_state) {
[email protected]36ad4aa82012-11-21 19:20:481849 window_->UpdateToolbar(tab_strip_model_->GetActiveWebContents(),
[email protected]52877dbc62012-06-29 22:22:031850 should_restore_state);
initial.commit09911bf2008-07-26 23:55:291851}
1852
[email protected]8e707792012-11-13 10:32:121853void Browser::UpdateSearchState(WebContents* contents) {
[email protected]f6a8dd632013-03-06 01:27:041854 if (chrome::search::IsInstantExtendedAPIEnabled())
[email protected]8e707792012-11-13 10:32:121855 search_delegate_->OnTabActivated(contents);
[email protected]e3a5d5b2012-06-22 17:56:461856}
1857
[email protected]2a6bc3e2011-12-28 23:51:331858void Browser::ScheduleUIUpdate(const WebContents* source,
[email protected]36b6dcb2008-11-12 01:19:571859 unsigned changed_flags) {
[email protected]a5231662009-09-26 00:56:001860 if (!source)
1861 return;
1862
[email protected]2b4355c2009-04-04 17:55:461863 // Do some synchronous updates.
[email protected]d9083482012-01-06 00:38:461864 if (changed_flags & content::INVALIDATE_TYPE_URL &&
[email protected]0d56fa7212012-11-15 23:34:021865 source == tab_strip_model_->GetActiveWebContents()) {
[email protected]36b6dcb2008-11-12 01:19:571866 // Only update the URL for the current tab. Note that we do not update
1867 // the navigation commands since those would have already been updated
1868 // synchronously by NavigationStateChanged.
1869 UpdateToolbar(false);
[email protected]d9083482012-01-06 00:38:461870 changed_flags &= ~content::INVALIDATE_TYPE_URL;
[email protected]36b6dcb2008-11-12 01:19:571871 }
[email protected]d9083482012-01-06 00:38:461872 if (changed_flags & content::INVALIDATE_TYPE_LOAD) {
[email protected]6ebdc9b2010-09-27 16:55:571873 // Update the loading state synchronously. This is so the throbber will
1874 // immediately start/stop, which gives a more snappy feel. We want to do
[email protected]a5231662009-09-26 00:56:001875 // this for any tab so they start & stop quickly.
[email protected]526b45e2012-11-28 20:22:461876 tab_strip_model_->UpdateWebContentsStateAt(
[email protected]0d56fa7212012-11-15 23:34:021877 tab_strip_model_->GetIndexOfWebContents(source),
[email protected]6ebdc9b2010-09-27 16:55:571878 TabStripModelObserver::LOADING_ONLY);
[email protected]d9083482012-01-06 00:38:461879 // The status bubble needs to be updated during INVALIDATE_TYPE_LOAD too,
1880 // but we do that asynchronously by not stripping INVALIDATE_TYPE_LOAD from
[email protected]6ebdc9b2010-09-27 16:55:571881 // changed_flags.
[email protected]8030f012009-09-25 18:09:371882 }
1883
[email protected]d9083482012-01-06 00:38:461884 if (changed_flags & content::INVALIDATE_TYPE_TITLE && !source->IsLoading()) {
[email protected]f1cd5e82009-10-23 17:19:031885 // To correctly calculate whether the title changed while not loading
1886 // we need to process the update synchronously. This state only matters for
1887 // the TabStripModel, so we notify the TabStripModel now and notify others
1888 // asynchronously.
[email protected]526b45e2012-11-28 20:22:461889 tab_strip_model_->UpdateWebContentsStateAt(
[email protected]0d56fa7212012-11-15 23:34:021890 tab_strip_model_->GetIndexOfWebContents(source),
[email protected]f1cd5e82009-10-23 17:19:031891 TabStripModelObserver::TITLE_NOT_LOADING);
1892 }
1893
[email protected]2b4355c2009-04-04 17:55:461894 // If the only updates were synchronously handled above, we're done.
[email protected]8030f012009-09-25 18:09:371895 if (changed_flags == 0)
[email protected]2b4355c2009-04-04 17:55:461896 return;
[email protected]36b6dcb2008-11-12 01:19:571897
1898 // Save the dirty bits.
[email protected]85354982009-09-25 19:58:401899 scheduled_updates_[source] |= changed_flags;
[email protected]36b6dcb2008-11-12 01:19:571900
[email protected]24f1477532011-11-22 22:55:361901 if (!chrome_updater_factory_.HasWeakPtrs()) {
[email protected]36b6dcb2008-11-12 01:19:571902 // No task currently scheduled, start another.
[email protected]0586b0e2010-02-12 21:38:371903 MessageLoop::current()->PostDelayedTask(
1904 FROM_HERE,
[email protected]24f1477532011-11-22 22:55:361905 base::Bind(&Browser::ProcessPendingUIUpdates,
1906 chrome_updater_factory_.GetWeakPtr()),
[email protected]11d6ab1a2012-01-12 21:37:261907 base::TimeDelta::FromMilliseconds(kUIUpdateCoalescingTimeMS));
[email protected]36b6dcb2008-11-12 01:19:571908 }
1909}
1910
1911void Browser::ProcessPendingUIUpdates() {
1912#ifndef NDEBUG
1913 // Validate that all tabs we have pending updates for exist. This is scary
1914 // because the pending list must be kept in sync with any detached or
[email protected]a5231662009-09-26 00:56:001915 // deleted tabs.
[email protected]85354982009-09-25 19:58:401916 for (UpdateMap::const_iterator i = scheduled_updates_.begin();
1917 i != scheduled_updates_.end(); ++i) {
[email protected]36b6dcb2008-11-12 01:19:571918 bool found = false;
[email protected]e0448872013-01-11 19:35:021919 for (int tab = 0; tab < tab_strip_model_->count(); tab++) {
[email protected]0d56fa7212012-11-15 23:34:021920 if (tab_strip_model_->GetWebContentsAt(tab) == i->first) {
[email protected]36b6dcb2008-11-12 01:19:571921 found = true;
1922 break;
1923 }
1924 }
1925 DCHECK(found);
1926 }
1927#endif
1928
[email protected]24f1477532011-11-22 22:55:361929 chrome_updater_factory_.InvalidateWeakPtrs();
[email protected]36b6dcb2008-11-12 01:19:571930
[email protected]85354982009-09-25 19:58:401931 for (UpdateMap::const_iterator i = scheduled_updates_.begin();
1932 i != scheduled_updates_.end(); ++i) {
[email protected]36b6dcb2008-11-12 01:19:571933 // Do not dereference |contents|, it may be out-of-date!
[email protected]2a6bc3e2011-12-28 23:51:331934 const WebContents* contents = i->first;
[email protected]85354982009-09-25 19:58:401935 unsigned flags = i->second;
[email protected]36b6dcb2008-11-12 01:19:571936
[email protected]0d56fa7212012-11-15 23:34:021937 if (contents == tab_strip_model_->GetActiveWebContents()) {
[email protected]a5231662009-09-26 00:56:001938 // Updates that only matter when the tab is selected go here.
[email protected]f7f3a5f2009-05-01 22:02:341939
[email protected]d9083482012-01-06 00:38:461940 if (flags & content::INVALIDATE_TYPE_PAGE_ACTIONS) {
[email protected]9800de5e2011-03-11 18:19:491941 LocationBar* location_bar = window()->GetLocationBar();
1942 if (location_bar)
1943 location_bar->UpdatePageActions();
1944 }
[email protected]6ebdc9b2010-09-27 16:55:571945 // Updating the URL happens synchronously in ScheduleUIUpdate.
[email protected]d9083482012-01-06 00:38:461946 if (flags & content::INVALIDATE_TYPE_LOAD && GetStatusBubble()) {
[email protected]0d56fa7212012-11-15 23:34:021947 GetStatusBubble()->SetStatus(CoreTabHelper::FromWebContents(
1948 tab_strip_model_->GetActiveWebContents())->GetStatusText());
[email protected]0dd3a0ab2011-02-18 08:17:441949 }
[email protected]a5231662009-09-26 00:56:001950
[email protected]d9083482012-01-06 00:38:461951 if (flags & (content::INVALIDATE_TYPE_TAB |
1952 content::INVALIDATE_TYPE_TITLE)) {
[email protected]a5231662009-09-26 00:56:001953 window_->UpdateTitleBar();
1954 }
[email protected]4d34e2e2009-05-26 22:55:281955 }
[email protected]36b6dcb2008-11-12 01:19:571956
[email protected]a5231662009-09-26 00:56:001957 // Updates that don't depend upon the selected state go here.
[email protected]d9083482012-01-06 00:38:461958 if (flags &
1959 (content::INVALIDATE_TYPE_TAB | content::INVALIDATE_TYPE_TITLE)) {
[email protected]526b45e2012-11-28 20:22:461960 tab_strip_model_->UpdateWebContentsStateAt(
[email protected]e52d0a42012-06-08 22:44:161961 tab_strip_model_->GetIndexOfWebContents(contents),
[email protected]f1cd5e82009-10-23 17:19:031962 TabStripModelObserver::ALL);
[email protected]36b6dcb2008-11-12 01:19:571963 }
1964
1965 // We don't need to process INVALIDATE_STATE, since that's not visible.
1966 }
1967
1968 scheduled_updates_.clear();
1969}
1970
[email protected]d9083482012-01-06 00:38:461971void Browser::RemoveScheduledUpdatesFor(WebContents* contents) {
[email protected]36b6dcb2008-11-12 01:19:571972 if (!contents)
1973 return;
1974
[email protected]85354982009-09-25 19:58:401975 UpdateMap::iterator i = scheduled_updates_.find(contents);
1976 if (i != scheduled_updates_.end())
1977 scheduled_updates_.erase(i);
initial.commit09911bf2008-07-26 23:55:291978}
1979
[email protected]36b6dcb2008-11-12 01:19:571980///////////////////////////////////////////////////////////////////////////////
1981// Browser, Getters for UI (private):
initial.commit09911bf2008-07-26 23:55:291982
[email protected]36b6dcb2008-11-12 01:19:571983StatusBubble* Browser::GetStatusBubble() {
[email protected]8e837ec2013-01-31 01:48:331984 // In kiosk and exclusive app mode, we want to always hide the status bubble.
1985 if (chrome::IsRunningInAppMode())
[email protected]34930432009-11-09 00:12:091986 return NULL;
[email protected]8e837ec2013-01-31 01:48:331987
[email protected]9b032bf2009-07-21 17:34:231988 return window_ ? window_->GetStatusBubble() : NULL;
initial.commit09911bf2008-07-26 23:55:291989}
1990
[email protected]36b6dcb2008-11-12 01:19:571991///////////////////////////////////////////////////////////////////////////////
1992// Browser, Session restore functions (private):
initial.commit09911bf2008-07-26 23:55:291993
1994void Browser::SyncHistoryWithTabs(int index) {
[email protected]92371eb2011-04-28 11:50:151995 SessionService* session_service =
1996 SessionServiceFactory::GetForProfileIfExisting(profile());
initial.commit09911bf2008-07-26 23:55:291997 if (session_service) {
[email protected]e0448872013-01-11 19:35:021998 for (int i = index; i < tab_strip_model_->count(); ++i) {
[email protected]0d56fa7212012-11-15 23:34:021999 WebContents* web_contents = tab_strip_model_->GetWebContentsAt(i);
[email protected]50a9a76b2012-09-12 14:19:362000 if (web_contents) {
2001 SessionTabHelper* session_tab_helper =
2002 SessionTabHelper::FromWebContents(web_contents);
initial.commit09911bf2008-07-26 23:55:292003 session_service->SetTabIndexInWindow(
[email protected]50a9a76b2012-09-12 14:19:362004 session_id(), session_tab_helper->session_id(), i);
[email protected]ecc523f62010-09-28 22:45:402005 session_service->SetPinnedState(
2006 session_id(),
[email protected]50a9a76b2012-09-12 14:19:362007 session_tab_helper->session_id(),
[email protected]52877dbc62012-06-29 22:22:032008 tab_strip_model_->IsTabPinned(i));
initial.commit09911bf2008-07-26 23:55:292009 }
2010 }
2011 }
2012}
2013
[email protected]36b6dcb2008-11-12 01:19:572014///////////////////////////////////////////////////////////////////////////////
[email protected]69444cc2009-04-09 20:40:062015// Browser, In-progress download termination handling (private):
2016
[email protected]b6a4ac2b2011-10-17 20:05:482017bool Browser::CanCloseWithInProgressDownloads() {
[email protected]5a17d492011-10-24 19:37:032018 // If we've prompted, we need to hear from the user before we
2019 // can close.
2020 if (cancel_download_confirmation_state_ != NOT_PROMPTED)
2021 return cancel_download_confirmation_state_ != WAITING_FOR_RESPONSE;
2022
2023 int num_downloads_blocking;
2024 if (DOWNLOAD_CLOSE_OK ==
2025 OkToCloseWithInProgressDownloads(&num_downloads_blocking))
[email protected]446295a2010-04-19 23:43:002026 return true;
2027
[email protected]5a17d492011-10-24 19:37:032028 // Closing this window will kill some downloads; prompt to make sure
2029 // that's ok.
[email protected]69444cc2009-04-09 20:40:062030 cancel_download_confirmation_state_ = WAITING_FOR_RESPONSE;
2031 window_->ConfirmBrowserCloseWithPendingDownloads();
2032
2033 // Return false so the browser does not close. We'll close if the user
2034 // confirms in the dialog.
2035 return false;
2036}
2037
2038///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:572039// Browser, Assorted utility functions (private):
initial.commit09911bf2008-07-26 23:55:292040
[email protected]27e5b3912012-12-16 00:45:372041void Browser::SetAsDelegate(WebContents* web_contents, Browser* delegate) {
2042 // WebContents...
2043 web_contents->SetDelegate(delegate);
2044
2045 // ...and all the helpers.
2046 BlockedContentTabHelper::FromWebContents(web_contents)->
2047 set_delegate(delegate);
2048 BookmarkTabHelper::FromWebContents(web_contents)->set_delegate(delegate);
[email protected]d2b16572013-01-03 00:41:582049 WebContentsModalDialogManager::FromWebContents(web_contents)->
[email protected]27e5b3912012-12-16 00:45:372050 set_delegate(delegate);
2051 CoreTabHelper::FromWebContents(web_contents)->set_delegate(delegate);
2052 SearchEngineTabHelper::FromWebContents(web_contents)->set_delegate(delegate);
2053 ZoomController::FromWebContents(web_contents)->set_observer(delegate);
2054}
2055
[email protected]36b6dcb2008-11-12 01:19:572056void Browser::CloseFrame() {
2057 window_->Close();
initial.commit09911bf2008-07-26 23:55:292058}
2059
[email protected]e89cfcb2012-11-11 14:47:242060void Browser::TabDetachedAtImpl(content::WebContents* contents,
2061 int index,
[email protected]c37e3b62010-01-22 00:15:172062 DetachType type) {
2063 if (type == DETACH_TYPE_DETACH) {
[email protected]56580852010-11-17 16:09:152064 // Save the current location bar state, but only if the tab being detached
2065 // is the selected tab. Because saving state can conditionally revert the
2066 // location bar, saving the current tab's location bar state to a
2067 // non-selected tab can corrupt both tabs.
[email protected]0d56fa7212012-11-15 23:34:022068 if (contents == tab_strip_model_->GetActiveWebContents()) {
[email protected]9800de5e2011-03-11 18:19:492069 LocationBar* location_bar = window()->GetLocationBar();
2070 if (location_bar)
[email protected]e89cfcb2012-11-11 14:47:242071 location_bar->SaveStateToContents(contents);
[email protected]9800de5e2011-03-11 18:19:492072 }
[email protected]c37e3b62010-01-22 00:15:172073
[email protected]2fc15ae2012-05-06 00:01:372074 if (!tab_strip_model_->closing_all())
[email protected]c37e3b62010-01-22 00:15:172075 SyncHistoryWithTabs(0);
2076 }
2077
[email protected]e89cfcb2012-11-11 14:47:242078 SetAsDelegate(contents, NULL);
2079 RemoveScheduledUpdatesFor(contents);
[email protected]c37e3b62010-01-22 00:15:172080
[email protected]04e3ead52013-01-14 01:09:202081 if (find_bar_controller_.get() && index == tab_strip_model_->active_index()) {
[email protected]ba3d09a12012-11-07 12:13:522082 find_bar_controller_->ChangeWebContents(NULL);
[email protected]ecc523f62010-09-28 22:45:402083 }
[email protected]c37e3b62010-01-22 00:15:172084
[email protected]e3a5d5b2012-06-22 17:56:462085 // Stop observing search model changes for this tab.
[email protected]e89cfcb2012-11-11 14:47:242086 search_delegate_->OnTabDetached(contents);
[email protected]e3a5d5b2012-06-22 17:56:462087
[email protected]20ca0382013-02-28 19:50:072088 for (size_t i = 0; i < interstitial_observers_.size(); i++) {
2089 if (interstitial_observers_[i]->web_contents() != contents)
2090 continue;
2091
2092 delete interstitial_observers_[i];
2093 interstitial_observers_.erase(interstitial_observers_.begin() + i);
2094 return;
2095 }
[email protected]c37e3b62010-01-22 00:15:172096}
2097
[email protected]92655362012-04-20 21:33:172098bool Browser::SupportsWindowFeatureImpl(WindowFeature feature,
2099 bool check_fullscreen) const {
[email protected]c3e32942012-12-11 10:26:202100 bool hide_ui_for_fullscreen = check_fullscreen && ShouldHideUIForFullscreen();
[email protected]92655362012-04-20 21:33:172101
2102 unsigned int features = FEATURE_INFOBAR | FEATURE_DOWNLOADSHELF;
2103
2104 if (is_type_tabbed())
2105 features |= FEATURE_BOOKMARKBAR;
2106
2107 if (!hide_ui_for_fullscreen) {
2108 if (!is_type_tabbed())
2109 features |= FEATURE_TITLEBAR;
2110
2111 if (is_type_tabbed())
2112 features |= FEATURE_TABSTRIP;
2113
2114 if (is_type_tabbed())
2115 features |= FEATURE_TOOLBAR;
2116
2117 if (!is_app())
2118 features |= FEATURE_LOCATIONBAR;
2119 }
2120 return !!(features & feature);
2121}
2122
[email protected]09b29342011-06-24 19:18:482123void Browser::UpdateBookmarkBarState(BookmarkBarStateChangeReason reason) {
2124 BookmarkBar::State state;
[email protected]227393e2011-06-29 21:28:392125 // The bookmark bar is hidden in fullscreen mode, unless on the new tab page.
[email protected]3710d0002011-10-11 00:35:002126 if (browser_defaults::bookmarks_enabled &&
2127 profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar) &&
[email protected]c3e32942012-12-11 10:26:202128 !ShouldHideUIForFullscreen()) {
[email protected]04143362012-11-28 14:25:182129 state = BookmarkBar::SHOW;
[email protected]09b29342011-06-24 19:18:482130 } else {
[email protected]0d56fa7212012-11-15 23:34:022131 WebContents* web_contents = tab_strip_model_->GetActiveWebContents();
[email protected]1c5119c2012-09-19 00:08:572132 BookmarkTabHelper* bookmark_tab_helper =
2133 web_contents ? BookmarkTabHelper::FromWebContents(web_contents) : NULL;
2134 if (bookmark_tab_helper && bookmark_tab_helper->ShouldShowBookmarkBar())
[email protected]09b29342011-06-24 19:18:482135 state = BookmarkBar::DETACHED;
2136 else
2137 state = BookmarkBar::HIDDEN;
2138 }
[email protected]2d4e43a02012-06-26 00:11:552139
[email protected]d2ec8802013-01-18 12:46:062140 // Don't allow the bookmark bar to be shown in suggestions mode.
[email protected]e2395da2013-01-23 22:05:352141#if !defined(OS_MACOSX)
[email protected]d2ec8802013-01-18 12:46:062142 if (search_model_->mode().is_search_suggestions())
[email protected]2d4e43a02012-06-26 00:11:552143 state = BookmarkBar::HIDDEN;
[email protected]e2395da2013-01-23 22:05:352144#endif
[email protected]2d4e43a02012-06-26 00:11:552145
[email protected]09b29342011-06-24 19:18:482146 if (state == bookmark_bar_state_)
2147 return;
2148
2149 bookmark_bar_state_ = state;
2150
[email protected]09b29342011-06-24 19:18:482151 if (!window_)
2152 return; // This is called from the constructor when window_ is NULL.
2153
2154 if (reason == BOOKMARK_BAR_STATE_CHANGE_TAB_SWITCH) {
2155 // Don't notify BrowserWindow on a tab switch as at the time this is invoked
2156 // BrowserWindow hasn't yet switched tabs. The BrowserWindow implementations
2157 // end up querying state once they process the tab switch.
2158 return;
2159 }
2160
[email protected]e2395da2013-01-23 22:05:352161 bool shouldAnimate = reason == BOOKMARK_BAR_STATE_CHANGE_PREF_CHANGE;
2162 window_->BookmarkBarStateChanged(shouldAnimate ?
[email protected]09b29342011-06-24 19:18:482163 BookmarkBar::ANIMATE_STATE_CHANGE :
[email protected]e2395da2013-01-23 22:05:352164 BookmarkBar::DONT_ANIMATE_STATE_CHANGE);
[email protected]09b29342011-06-24 19:18:482165}
[email protected]2e2cacc2011-07-12 21:54:262166
[email protected]c3e32942012-12-11 10:26:202167bool Browser::ShouldHideUIForFullscreen() const {
[email protected]6a414ff2013-02-27 08:22:542168 // Windows and GTK remove the top controls in fullscreen, but Mac and Ash
2169 // keep the controls in a slide-down panel.
2170 return window_ && window_->ShouldHideUIForFullscreen();
[email protected]c3e32942012-12-11 10:26:202171}
2172
[email protected]92655362012-04-20 21:33:172173bool Browser::MaybeCreateBackgroundContents(int route_id,
2174 WebContents* opener_web_contents,
2175 const string16& frame_name,
2176 const GURL& target_url) {
2177 GURL opener_url = opener_web_contents->GetURL();
[email protected]06bdd2b2012-11-30 18:47:132178 ExtensionService* extensions_service =
2179 extensions::ExtensionSystem::Get(profile_)->extension_service();
[email protected]92655362012-04-20 21:33:172180
2181 if (!opener_url.is_valid() ||
2182 frame_name.empty() ||
2183 !extensions_service ||
2184 !extensions_service->is_ready())
2185 return false;
2186
2187 // Only hosted apps have web extents, so this ensures that only hosted apps
2188 // can create BackgroundContents. We don't have to check for background
2189 // permission as that is checked in RenderMessageFilter when the CreateWindow
2190 // message is processed.
2191 const Extension* extension =
2192 extensions_service->extensions()->GetHostedAppByURL(
2193 ExtensionURLInfo(opener_url));
2194 if (!extension)
2195 return false;
2196
2197 // No BackgroundContents allowed if BackgroundContentsService doesn't exist.
2198 BackgroundContentsService* service =
2199 BackgroundContentsServiceFactory::GetForProfile(profile_);
2200 if (!service)
2201 return false;
2202
2203 // Ensure that we're trying to open this from the extension's process.
2204 SiteInstance* opener_site_instance = opener_web_contents->GetSiteInstance();
2205 extensions::ProcessMap* process_map = extensions_service->process_map();
2206 if (!opener_site_instance->GetProcess() ||
2207 !process_map->Contains(
2208 extension->id(), opener_site_instance->GetProcess()->GetID())) {
2209 return false;
2210 }
2211
2212 // Only allow a single background contents per app.
[email protected]9367eabc2013-03-01 01:29:292213 bool allow_js_access = extensions::BackgroundInfo::AllowJSAccess(extension);
[email protected]92655362012-04-20 21:33:172214 BackgroundContents* existing =
2215 service->GetAppBackgroundContents(ASCIIToUTF16(extension->id()));
2216 if (existing) {
2217 // For non-scriptable background contents, ignore the request altogether,
2218 // (returning true, so that a regular WebContents isn't created either).
2219 if (!allow_js_access)
2220 return true;
2221 // For scriptable background pages, if one already exists, close it (even
2222 // if it was specified in the manifest).
2223 DLOG(INFO) << "Closing existing BackgroundContents for " << opener_url;
2224 delete existing;
2225 }
2226
2227 // If script access is not allowed, create the the background contents in a
2228 // new SiteInstance, so that a separate process is used.
2229 scoped_refptr<content::SiteInstance> site_instance =
2230 allow_js_access ?
2231 opener_site_instance :
2232 content::SiteInstance::Create(opener_web_contents->GetBrowserContext());
2233
2234 // Passed all the checks, so this should be created as a BackgroundContents.
2235 BackgroundContents* contents = service->CreateBackgroundContents(
2236 site_instance,
2237 route_id,
2238 profile_,
2239 frame_name,
2240 ASCIIToUTF16(extension->id()));
2241
2242 // When a separate process is used, the original renderer cannot access the
2243 // new window later, thus we need to navigate the window now.
2244 if (contents && !allow_js_access) {
2245 contents->web_contents()->GetController().LoadURL(
2246 target_url,
2247 content::Referrer(),
2248 content::PAGE_TRANSITION_LINK,
2249 std::string()); // No extra headers.
2250 }
2251
2252 return contents != NULL;
[email protected]afcb43562011-09-02 23:33:382253}