blob: 0525e02fffecdf9cef21665c93a8cabe13fb5940 [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]d09a4ce1c2013-07-24 17:37:0222#include "base/process/process_info.h"
[email protected]3ea1b182013-02-08 22:38:4123#include "base/strings/string_number_conversions.h"
[email protected]11521182013-06-11 04:06:3624#include "base/strings/string_util.h"
25#include "base/strings/stringprintf.h"
[email protected]5846d582013-06-08 16:02:1226#include "base/strings/utf_string_conversions.h"
[email protected]34b99632011-01-01 01:01:0627#include "base/threading/thread.h"
28#include "base/threading/thread_restrictions.h"
[email protected]bd64063d2013-06-28 15:16:2229#include "base/time/time.h"
[email protected]1a3aba82010-11-08 23:52:5430#include "chrome/app/chrome_command_ids.h"
[email protected]8e837ec2013-01-31 01:48:3331#include "chrome/browser/app_mode/app_mode_utils.h"
[email protected]ea9edcb02011-09-23 22:05:0432#include "chrome/browser/autofill/personal_data_manager_factory.h"
[email protected]a07676b22011-06-17 16:36:5333#include "chrome/browser/background/background_contents_service.h"
[email protected]4fdf6742012-01-10 20:14:3634#include "chrome/browser/background/background_contents_service_factory.h"
[email protected]5c238752009-06-13 10:29:0735#include "chrome/browser/browser_process.h"
[email protected]c61db1d2009-02-06 03:39:1836#include "chrome/browser/browser_shutdown.h"
[email protected]40d59ce52009-03-06 23:20:1437#include "chrome/browser/character_encoding.h"
[email protected]dcc8fbc2013-07-12 00:54:0938#include "chrome/browser/chrome_notification_types.h"
[email protected]0f083402011-11-22 02:59:0139#include "chrome/browser/chrome_page_zoom.h"
[email protected]55c87fa2011-10-15 07:28:2840#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]7ae9c322012-07-17 23:46:4441#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]7d189022011-08-25 22:54:2042#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
[email protected]8a39b472013-04-04 17:07:4243#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
[email protected]7d189022011-08-25 22:54:2044#include "chrome/browser/custom_handlers/register_protocol_handler_infobar_delegate.h"
[email protected]f1d20572014-02-14 01:27:1445#include "chrome/browser/custom_handlers/register_protocol_handler_permission_request.h"
[email protected]5fec4702013-11-20 04:37:3846#include "chrome/browser/defaults.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]63d1f9b2014-07-05 19:09:0353#include "chrome/browser/extensions/api/tabs/tabs_event_router.h"
54#include "chrome/browser/extensions/api/tabs/tabs_windows_api.h"
[email protected]9c1662b2012-03-06 15:44:3355#include "chrome/browser/extensions/browser_extension_window_controller.h"
[email protected]eaa7dd182010-12-14 11:09:0056#include "chrome/browser/extensions/extension_service.h"
benwells39f23ae2014-08-27 08:01:5257#include "chrome/browser/extensions/extension_util.h"
[email protected]a6394ae2012-07-16 20:58:4358#include "chrome/browser/extensions/tab_helper.h"
[email protected]b375c5d2011-05-03 21:15:0459#include "chrome/browser/favicon/favicon_tab_helper.h"
[email protected]3a29a6e2011-08-24 18:26:2160#include "chrome/browser/file_select_helper.h"
[email protected]820735792010-07-29 23:40:0161#include "chrome/browser/first_run/first_run.h"
[email protected]82808d02014-04-10 00:10:5962#include "chrome/browser/history/top_sites.h"
[email protected]4a8adfa02013-03-19 22:37:4663#include "chrome/browser/infobars/infobar_service.h"
64#include "chrome/browser/infobars/simple_alert_infobar_delegate.h"
[email protected]2e6389f2012-05-18 19:41:2565#include "chrome/browser/lifetime/application_lifetime.h"
[email protected]2c4fb7b2011-04-02 06:33:2966#include "chrome/browser/notifications/notification_ui_manager.h"
[email protected]8c8fc292012-11-23 18:57:1667#include "chrome/browser/pepper_broker_infobar_delegate.h"
[email protected]4e94ab32011-08-05 05:28:2768#include "chrome/browser/prefs/incognito_mode_prefs.h"
[email protected]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]a7be6982011-12-12 21:53:5771#include "chrome/browser/profiles/profile_metrics.h"
[email protected]67baffc82011-12-19 18:03:0772#include "chrome/browser/repost_form_warning_controller.h"
[email protected]a7b8e43d2013-03-18 18:52:4373#include "chrome/browser/search/search.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]255ab972014-06-04 12:14:2288#include "chrome/browser/translate/chrome_translate_client.h"
[email protected]71a88bb2013-02-01 22:05:1589#include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_manager.h"
[email protected]b4db6d42014-06-07 04:36:5890#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
[email protected]5635e6142013-07-18 08:10:4091#include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
[email protected]35699d572011-05-11 19:46:1492#include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
[email protected]4db2e912014-03-04 07:08:5893#include "chrome/browser/ui/bookmarks/bookmark_utils.h"
[email protected]5d98294912012-06-27 22:57:4094#include "chrome/browser/ui/browser_command_controller.h"
[email protected]a37d4b02012-06-25 21:56:1095#include "chrome/browser/ui/browser_commands.h"
[email protected]ef557022012-03-16 10:05:3396#include "chrome/browser/ui/browser_content_setting_bubble_model_delegate.h"
[email protected]3155c5272014-01-21 12:38:1297#include "chrome/browser/ui/browser_content_translate_driver_observer.h"
[email protected]55452902011-06-01 21:57:4798#include "chrome/browser/ui/browser_dialogs.h"
[email protected]d8748142012-05-16 21:13:4399#include "chrome/browser/ui/browser_finder.h"
[email protected]7acfaf92012-07-11 15:51:59100#include "chrome/browser/ui/browser_instant_controller.h"
[email protected]3539929f2013-02-01 05:59:14101#include "chrome/browser/ui/browser_iterator.h"
[email protected]6768ac02011-04-06 17:41:04102#include "chrome/browser/ui/browser_list.h"
[email protected]fcca741b2011-06-17 22:46:37103#include "chrome/browser/ui/browser_navigator.h"
[email protected]fb7da4132011-03-15 15:51:00104#include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
[email protected]855370052012-07-10 19:30:32105#include "chrome/browser/ui/browser_tab_strip_model_delegate.h"
[email protected]52877dbc62012-06-29 22:22:03106#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]ef557022012-03-16 10:05:33107#include "chrome/browser/ui/browser_toolbar_model_delegate.h"
[email protected]2cd4fde2012-06-26 03:10:26108#include "chrome/browser/ui/browser_ui_prefs.h"
[email protected]00070c732011-04-09 15:31:33109#include "chrome/browser/ui/browser_window.h"
[email protected]5d9cace72012-06-21 16:07:12110#include "chrome/browser/ui/chrome_pages.h"
[email protected]6e1fcd12012-07-02 17:14:20111#include "chrome/browser/ui/chrome_select_file_policy.h"
[email protected]88c92012013-07-02 11:56:34112#include "chrome/browser/ui/fast_unload_controller.h"
[email protected]45300ad42010-12-02 15:51:14113#include "chrome/browser/ui/find_bar/find_bar.h"
114#include "chrome/browser/ui/find_bar/find_bar_controller.h"
[email protected]c90c6ca2011-02-16 20:11:38115#include "chrome/browser/ui/find_bar/find_tab_helper.h"
[email protected]9d5c059a82012-06-29 20:13:07116#include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
[email protected]1ba55cf2012-06-29 19:11:39117#include "chrome/browser/ui/global_error/global_error.h"
118#include "chrome/browser/ui/global_error/global_error_service.h"
119#include "chrome/browser/ui/global_error/global_error_service_factory.h"
[email protected]d57ce6a2014-07-03 15:39:23120#include "chrome/browser/ui/location_bar/location_bar.h"
[email protected]859ec322013-09-13 16:24:56121#include "chrome/browser/ui/media_utils.h"
[email protected]e3a5d5b2012-06-22 17:56:46122#include "chrome/browser/ui/search/search_delegate.h"
123#include "chrome/browser/ui/search/search_model.h"
[email protected]549f62b2013-10-26 12:54:42124#include "chrome/browser/ui/search/search_tab_helper.h"
[email protected]27e5b3912012-12-16 00:45:37125#include "chrome/browser/ui/search_engines/search_engine_tab_helper.h"
[email protected]f9253672014-07-31 03:35:38126#include "chrome/browser/ui/settings_window_manager.h"
[email protected]5d9cace72012-06-21 16:07:12127#include "chrome/browser/ui/singleton_tabs.h"
[email protected]6a3ec2312010-12-02 19:30:19128#include "chrome/browser/ui/status_bubble.h"
[email protected]3da95a102011-11-30 21:47:45129#include "chrome/browser/ui/sync/browser_synced_window_delegate.h"
[email protected]51d83bdf2011-12-09 16:09:29130#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
[email protected]e0bc75d52014-01-30 23:42:59131#include "chrome/browser/ui/tab_helpers.h"
[email protected]51296c62012-09-26 13:02:44132#include "chrome/browser/ui/tab_modal_confirm_dialog.h"
[email protected]6a3ec2312010-12-02 19:30:19133#include "chrome/browser/ui/tabs/tab_menu_model.h"
[email protected]b56e2e32012-05-11 21:18:04134#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]82808d02014-04-10 00:10:59135#include "chrome/browser/ui/tabs/tab_strip_model_utils.h"
[email protected]736d00322012-10-11 21:32:41136#include "chrome/browser/ui/toolbar/toolbar_model_impl.h"
[email protected]f9b6c092012-07-09 19:32:28137#include "chrome/browser/ui/unload_controller.h"
[email protected]edc3af82013-12-12 21:24:07138#include "chrome/browser/ui/validation_message_bubble.h"
[email protected]f1d20572014-02-14 01:27:14139#include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
[email protected]65549182012-02-25 00:45:40140#include "chrome/browser/ui/webui/signin/login_ui_service.h"
141#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
[email protected]03639422012-06-28 19:03:37142#include "chrome/browser/ui/window_sizer/window_sizer.h"
[email protected]27e5b3912012-12-16 00:45:37143#include "chrome/browser/ui/zoom/zoom_controller.h"
[email protected]b1b73942010-05-26 20:11:54144#include "chrome/browser/upgrade_detector.h"
[email protected]86b54012009-11-19 09:18:50145#include "chrome/browser/web_applications/web_app.h"
[email protected]d938aed92009-01-22 19:49:33146#include "chrome/common/chrome_constants.h"
147#include "chrome/common/chrome_switches.h"
[email protected]7d189022011-08-25 22:54:20148#include "chrome/common/custom_handlers/protocol_handler.h"
[email protected]8a53ee042009-01-21 16:41:33149#include "chrome/common/pref_names.h"
[email protected]7664ab32011-02-01 23:35:25150#include "chrome/common/profiling.h"
[email protected]c3917742012-11-21 01:51:14151#include "chrome/common/search_types.h"
[email protected]c744f7d22013-04-12 20:29:04152#include "chrome/common/url_constants.h"
thestig884a1602014-08-27 01:29:39153#include "chrome/grit/chromium_strings.h"
154#include "chrome/grit/generated_resources.h"
thestigaeddd322014-08-27 06:58:39155#include "chrome/grit/locale_settings.h"
[email protected]a90c8ca2014-05-20 17:16:04156#include "components/bookmarks/browser/bookmark_model.h"
157#include "components/bookmarks/browser/bookmark_utils.h"
[email protected]a1fcc1d2014-06-08 11:47:58158#include "components/google/core/browser/google_url_tracker.h"
[email protected]720b10492014-07-23 08:48:40159#include "components/search/search.h"
[email protected]7a83dd072013-09-03 12:09:17160#include "components/startup_metric_utils/startup_metric_utils.h"
[email protected]010152f2014-07-15 00:16:47161#include "components/web_modal/popup_manager.h"
[email protected]e41d0082013-05-16 04:37:54162#include "components/web_modal/web_contents_modal_dialog_manager.h"
[email protected]b50452f2014-08-18 12:31:44163#include "content/public/browser/devtools_agent_host.h"
[email protected]e582fdd2011-12-20 16:48:17164#include "content/public/browser/download_item.h"
165#include "content/public/browser/download_manager.h"
[email protected]cadaec52012-02-08 21:53:13166#include "content/public/browser/interstitial_page.h"
[email protected]d9083482012-01-06 00:38:46167#include "content/public/browser/invalidate_type.h"
[email protected]cdcb1dee2012-01-04 00:46:20168#include "content/public/browser/navigation_controller.h"
[email protected]022af742011-12-28 18:37:25169#include "content/public/browser/navigation_entry.h"
[email protected]7f6f44c2011-12-14 13:23:38170#include "content/public/browser/notification_details.h"
171#include "content/public/browser/notification_service.h"
[email protected]3a5180ae2011-12-21 02:39:38172#include "content/public/browser/plugin_service.h"
[email protected]9c1662b2012-03-06 15:44:33173#include "content/public/browser/render_process_host.h"
174#include "content/public/browser/render_view_host.h"
[email protected]edc3af82013-12-12 21:24:07175#include "content/public/browser/render_widget_host_view.h"
[email protected]b6583592012-01-25 19:52:33176#include "content/public/browser/site_instance.h"
[email protected]7f6f44c2011-12-14 13:23:38177#include "content/public/browser/user_metrics.h"
[email protected]d9083482012-01-06 00:38:46178#include "content/public/browser/web_contents.h"
[email protected]c08950d22011-10-13 22:20:29179#include "content/public/common/content_switches.h"
[email protected]0f083402011-11-22 02:59:01180#include "content/public/common/page_zoom.h"
[email protected]cadaec52012-02-08 21:53:13181#include "content/public/common/renderer_preferences.h"
[email protected]d7bd3e52013-07-21 04:29:20182#include "content/public/common/webplugininfo.h"
[email protected]489db0842014-01-22 18:20:03183#include "extensions/browser/extension_prefs.h"
[email protected]59b0e602014-01-30 00:41:24184#include "extensions/browser/extension_system.h"
[email protected]885c0e92012-11-13 20:27:42185#include "extensions/common/constants.h"
[email protected]e4452d32013-11-15 23:07:41186#include "extensions/common/extension.h"
[email protected]558878cc82013-11-09 01:25:51187#include "extensions/common/manifest_handlers/background_info.h"
[email protected]d96cf752014-04-09 04:05:28188#include "net/base/filename_util.h"
[email protected]be28b5f42012-07-20 11:31:25189#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]4d7c4ef2012-03-16 01:47:12190#include "net/cookies/cookie_monster.h"
[email protected]8a53ee042009-01-21 16:41:33191#include "net/url_request/url_request_context.h"
[email protected]453c1b22013-07-23 09:31:48192#include "third_party/WebKit/public/web/WebWindowFeatures.h"
[email protected]c051a1b2011-01-21 23:30:17193#include "ui/base/l10n/l10n_util.h"
[email protected]f47621b2013-01-22 20:50:33194#include "ui/base/window_open_disposition.h"
[email protected]08397d52011-02-05 01:53:38195#include "ui/gfx/point.h"
[email protected]4344a3c2013-01-17 23:49:20196#include "ui/shell_dialogs/selected_file_info.h"
[email protected]8a53ee042009-01-21 16:41:33197
198#if defined(OS_WIN)
[email protected]77cbe50e2012-06-14 02:44:38199#include "base/win/metro.h"
[email protected]b17d41c2011-02-17 22:09:59200#include "chrome/browser/ssl/ssl_error_info.h"
[email protected]a5d1e1e2010-09-23 19:34:12201#include "chrome/browser/task_manager/task_manager.h"
[email protected]6a3ec2312010-12-02 19:30:19202#include "chrome/browser/ui/view_ids.h"
[email protected]526a16b2013-06-20 11:44:33203#include "components/autofill/core/browser/autofill_ie_toolbar_import_win.h"
[email protected]dcd57762011-06-25 12:18:51204#include "ui/base/win/shell.h"
[email protected]8a53ee042009-01-21 16:41:33205#endif // OS_WIN
[email protected]e1acf6f2008-10-27 20:43:33206
[email protected]b7969202010-05-14 21:29:26207#if defined(OS_CHROMEOS)
[email protected]4fa2fd5d2013-04-26 03:42:52208#include "chrome/browser/chromeos/drive/file_system_util.h"
[email protected]eab8c0f2011-11-18 22:33:50209#endif
210
[email protected]dc04be7c2012-03-15 23:57:49211#if defined(USE_ASH)
[email protected]bafda132012-02-16 19:32:51212#include "ash/ash_switches.h"
[email protected]c2f0a892012-01-24 22:19:21213#endif
214
[email protected]8a53ee042009-01-21 16:41:33215using base::TimeDelta;
[email protected]e6e30ac2014-01-13 21:24:39216using base::UserMetricsAction;
[email protected]b87ee522012-05-18 15:16:54217using content::NativeWebKeyboardEvent;
[email protected]c5eed492012-01-04 17:07:50218using content::NavigationController;
[email protected]10f417c52011-12-28 21:04:23219using content::NavigationEntry;
[email protected]e5d549d2011-12-28 01:29:20220using content::OpenURLParams;
[email protected]3a5180ae2011-12-21 02:39:38221using content::PluginService;
[email protected]e5d549d2011-12-28 01:29:20222using content::Referrer;
[email protected]edc3af82013-12-12 21:24:07223using content::RenderWidgetHostView;
[email protected]b6583592012-01-25 19:52:33224using content::SiteInstance;
[email protected]ea049a02011-12-25 21:37:09225using content::WebContents;
[email protected]1c321ee52012-05-21 03:02:34226using extensions::Extension;
[email protected]20c07f8e2012-05-31 08:43:14227using ui::WebDialogDelegate;
[email protected]e41d0082013-05-16 04:37:54228using web_modal::WebContentsModalDialogManager;
[email protected]a1221aea2013-11-07 01:31:30229using blink::WebWindowFeatures;
initial.commit09911bf2008-07-26 23:55:29230
[email protected]36b6dcb2008-11-12 01:19:57231///////////////////////////////////////////////////////////////////////////////
initial.commit09911bf2008-07-26 23:55:29232
[email protected]505323e22009-01-24 02:47:58233namespace {
234
[email protected]91a79232010-11-10 18:15:08235// How long we wait before updating the browser chrome while loading a page.
236const int kUIUpdateCoalescingTimeMS = 200;
237
[email protected]a4fe67012012-07-25 20:14:29238BrowserWindow* CreateBrowserWindow(Browser* browser) {
[email protected]a4fe67012012-07-25 20:14:29239 return BrowserWindow::CreateBrowserWindow(browser);
240}
241
[email protected]88c92012013-07-02 11:56:34242// Is the fast tab unload experiment enabled?
243bool IsFastTabUnloadEnabled() {
244 return CommandLine::ForCurrentProcess()->HasSwitch(
245 switches::kEnableFastUnload);
246}
247
[email protected]505323e22009-01-24 02:47:58248} // namespace
249
[email protected]b35b26b32011-05-05 20:35:14250////////////////////////////////////////////////////////////////////////////////
251// Browser, CreateParams:
252
[email protected]196578252012-12-06 23:54:20253Browser::CreateParams::CreateParams(Profile* profile,
254 chrome::HostDesktopType host_desktop_type)
255 : type(TYPE_TABBED),
256 profile(profile),
257 host_desktop_type(host_desktop_type),
[email protected]d2202e22014-04-14 20:20:53258 trusted_source(false),
[email protected]196578252012-12-06 23:54:20259 initial_show_state(ui::SHOW_STATE_DEFAULT),
260 is_session_restore(false),
261 window(NULL) {
262}
263
[email protected]56073dc2012-08-10 16:59:06264Browser::CreateParams::CreateParams(Type type,
265 Profile* profile,
266 chrome::HostDesktopType host_desktop_type)
267 : type(type),
268 profile(profile),
269 host_desktop_type(host_desktop_type),
[email protected]d2202e22014-04-14 20:20:53270 trusted_source(false),
[email protected]a636d8e52012-02-28 15:40:41271 initial_show_state(ui::SHOW_STATE_DEFAULT),
[email protected]a4fe67012012-07-25 20:14:29272 is_session_restore(false),
273 window(NULL) {
[email protected]b35b26b32011-05-05 20:35:14274}
275
[email protected]da22aa62012-04-04 18:54:35276// static
277Browser::CreateParams Browser::CreateParams::CreateForApp(
[email protected]da22aa62012-04-04 18:54:35278 const std::string& app_name,
[email protected]d2202e22014-04-14 20:20:53279 bool trusted_source,
[email protected]da22aa62012-04-04 18:54:35280 const gfx::Rect& window_bounds,
[email protected]2b09ae552013-02-23 06:38:03281 Profile* profile,
282 chrome::HostDesktopType host_desktop_type) {
[email protected]da22aa62012-04-04 18:54:35283 DCHECK(!app_name.empty());
284
[email protected]d2202e22014-04-14 20:20:53285 CreateParams params(TYPE_POPUP, profile, host_desktop_type);
[email protected]da22aa62012-04-04 18:54:35286 params.app_name = app_name;
[email protected]d2202e22014-04-14 20:20:53287 params.trusted_source = trusted_source;
[email protected]da22aa62012-04-04 18:54:35288 params.initial_bounds = window_bounds;
289
290 return params;
291}
292
293// static
294Browser::CreateParams Browser::CreateParams::CreateForDevTools(
[email protected]2b09ae552013-02-23 06:38:03295 Profile* profile,
296 chrome::HostDesktopType host_desktop_type) {
297 CreateParams params(TYPE_POPUP, profile, host_desktop_type);
[email protected]da22aa62012-04-04 18:54:35298 params.app_name = DevToolsWindow::kDevToolsApp;
[email protected]d2202e22014-04-14 20:20:53299 params.trusted_source = true;
[email protected]da22aa62012-04-04 18:54:35300 return params;
301}
302
[email protected]20ca0382013-02-28 19:50:07303////////////////////////////////////////////////////////////////////////////////
304// Browser, InterstitialObserver:
305
306class Browser::InterstitialObserver : public content::WebContentsObserver {
307 public:
308 InterstitialObserver(Browser* browser, content::WebContents* web_contents)
309 : WebContentsObserver(web_contents),
310 browser_(browser) {
311 }
312
[email protected]20ca0382013-02-28 19:50:07313 virtual void DidAttachInterstitialPage() OVERRIDE {
314 browser_->UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
315 }
316
317 virtual void DidDetachInterstitialPage() OVERRIDE {
318 browser_->UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
319 }
320
321 private:
322 Browser* browser_;
323
324 DISALLOW_COPY_AND_ASSIGN(InterstitialObserver);
325};
326
[email protected]36b6dcb2008-11-12 01:19:57327///////////////////////////////////////////////////////////////////////////////
328// Browser, Constructors, Creation, Showing:
initial.commit09911bf2008-07-26 23:55:29329
[email protected]a4fe67012012-07-25 20:14:29330Browser::Browser(const CreateParams& params)
331 : type_(params.type),
332 profile_(params.profile),
[email protected]f3e99e32008-07-30 04:48:39333 window_(NULL),
[email protected]d4b2d232013-04-30 21:14:23334 tab_strip_model_delegate_(new chrome::BrowserTabStripModelDelegate(this)),
[email protected]fa9d49ff2012-10-24 22:44:37335 tab_strip_model_(new TabStripModel(tab_strip_model_delegate_.get(),
336 params.profile)),
[email protected]a4fe67012012-07-25 20:14:29337 app_name_(params.app_name),
[email protected]d2202e22014-04-14 20:20:53338 is_trusted_source_(params.trusted_source),
[email protected]69444cc2009-04-09 20:40:06339 cancel_download_confirmation_state_(NOT_PROMPTED),
[email protected]a4fe67012012-07-25 20:14:29340 override_bounds_(params.initial_bounds),
341 initial_show_state_(params.initial_show_state),
342 is_session_restore_(params.is_session_restore),
[email protected]d58737692013-11-19 04:00:41343 host_desktop_type_(BrowserWindow::AdjustHostDesktopType(
344 params.host_desktop_type)),
[email protected]d4b2d232013-04-30 21:14:23345 content_setting_bubble_model_delegate_(
346 new BrowserContentSettingBubbleModelDelegate(this)),
347 toolbar_model_delegate_(new BrowserToolbarModelDelegate(this)),
348 tab_restore_service_delegate_(new BrowserTabRestoreServiceDelegate(this)),
349 synced_window_delegate_(new BrowserSyncedWindowDelegate(this)),
[email protected]8a5e0ca2011-08-25 06:30:47350 bookmark_bar_state_(BookmarkBar::HIDDEN),
[email protected]338416c02014-05-13 16:47:06351 command_controller_(new chrome::BrowserCommandController(this)),
[email protected]07f0f2cf2013-10-09 03:43:18352 window_has_shown_(false),
353 chrome_updater_factory_(this),
[email protected]3155c5272014-01-21 12:38:12354 translate_driver_observer_(
mohan.reddy1f994392014-09-17 05:30:05355 new BrowserContentTranslateDriverObserver(this)),
356 weak_factory_(this) {
[email protected]3d0294ef2013-06-04 19:19:06357 // If this causes a crash then a window is being opened using a profile type
358 // that is disallowed by policy. The crash prevents the disabled window type
359 // from opening at all, but the path that triggered it should be fixed.
360 CHECK(IncognitoModePrefs::CanOpenBrowser(profile_));
[email protected]d5838dd2014-01-08 02:46:33361 CHECK(!profile_->IsGuestSession() || profile_->IsOffTheRecord())
362 << "Only off the record browser may be opened in guest mode";
[email protected]3d0294ef2013-06-04 19:19:06363
[email protected]88c92012013-07-02 11:56:34364 // TODO(jeremy): Move to initializer list once flag is removed.
365 if (IsFastTabUnloadEnabled())
366 fast_unload_controller_.reset(new chrome::FastUnloadController(this));
367 else
368 unload_controller_.reset(new chrome::UnloadController(this));
369
[email protected]2fc15ae2012-05-06 00:01:37370 tab_strip_model_->AddObserver(this);
371
[email protected]736d00322012-10-11 21:32:41372 toolbar_model_.reset(new ToolbarModelImpl(toolbar_model_delegate_.get()));
[email protected]165fe422013-03-27 06:34:03373 search_model_.reset(new SearchModel());
[email protected]a40be8b2013-08-22 20:12:14374 search_delegate_.reset(new SearchDelegate(search_model_.get()));
[email protected]ef557022012-03-16 10:05:33375
[email protected]fdc76192014-04-20 21:54:41376 registrar_.Add(this,
[email protected]adf5a102014-07-31 12:44:06377 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
[email protected]a93089b12011-11-22 20:47:38378 content::Source<Profile>(profile_->GetOriginalProfile()));
[email protected]bbf8d30b2014-05-29 16:45:07379 registrar_.Add(this,
[email protected]adf5a102014-07-31 12:44:06380 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
[email protected]a93089b12011-11-22 20:47:38381 content::Source<Profile>(profile_->GetOriginalProfile()));
[email protected]adf5a102014-07-31 12:44:06382 registrar_.Add(this,
383 extensions::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED,
384 content::Source<Profile>(profile_->GetOriginalProfile()));
385 registrar_.Add(this,
386 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07387 content::NotificationService::AllSources());
[email protected]6b40bb582012-03-15 20:50:38388#if defined(ENABLE_THEMES)
[email protected]ffa6f592011-06-24 22:03:57389 registrar_.Add(
[email protected]432115822011-07-10 15:52:27390 this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
[email protected]6c2381d2011-10-19 02:52:53391 content::Source<ThemeService>(
392 ThemeServiceFactory::GetForProfile(profile_)));
[email protected]6b40bb582012-03-15 20:50:38393#endif
[email protected]ea049a02011-12-25 21:37:09394 registrar_.Add(this, chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
[email protected]ad50def52011-10-19 23:17:07395 content::NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29396
[email protected]97b46382011-05-02 13:23:04397 profile_pref_registrar_.Init(profile_->GetPrefs());
[email protected]c31813a2012-11-15 18:47:26398 profile_pref_registrar_.Add(
399 prefs::kDevToolsDisabled,
400 base::Bind(&Browser::OnDevToolsDisabledChanged, base::Unretained(this)));
401 profile_pref_registrar_.Add(
tfarina3bddbe112014-08-28 05:29:32402 bookmarks::prefs::kShowBookmarkBar,
[email protected]c31813a2012-11-15 18:47:26403 base::Bind(&Browser::UpdateBookmarkBarState, base::Unretained(this),
404 BOOKMARK_BAR_STATE_CHANGE_PREF_CHANGE));
[email protected]9e823662010-10-13 23:36:00405
initial.commit09911bf2008-07-26 23:55:29406 BrowserList::AddBrowser(this);
407
[email protected]97b46382011-05-02 13:23:04408 // NOTE: These prefs all need to be explicitly destroyed in the destructor
409 // or you'll get a nasty surprise when you run the incognito tests.
initial.commit09911bf2008-07-26 23:55:29410 encoding_auto_detect_.Init(prefs::kWebKitUsesUniversalDetector,
[email protected]96a5c342012-12-04 18:14:02411 profile_->GetPrefs());
[email protected]281918912010-05-27 22:05:13412
[email protected]a780c7b22013-08-02 18:36:59413 if (chrome::IsInstantExtendedAPIEnabled() && is_type_tabbed())
[email protected]165fe422013-03-27 06:34:03414 instant_controller_.reset(new BrowserInstantController(this));
[email protected]df25eb9f2012-09-13 17:43:23415
[email protected]09b29342011-06-24 19:18:48416 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_INIT);
[email protected]a7be6982011-12-12 21:53:57417
[email protected]26e92df2013-04-11 16:05:21418 ProfileMetrics::LogProfileLaunch(profile_);
[email protected]a4fe67012012-07-25 20:14:29419
420 window_ = params.window ? params.window : CreateBrowserWindow(this);
421
[email protected]a4fe67012012-07-25 20:14:29422 // Create the extension window controller before sending notifications.
423 extension_window_controller_.reset(
424 new BrowserExtensionWindowController(this));
425
[email protected]7d52ea4c62013-11-13 03:52:14426 SessionService* session_service =
427 SessionServiceFactory::GetForProfileForSessionRestore(profile_);
428 if (session_service)
429 session_service->WindowOpened(this);
430
[email protected]a4fe67012012-07-25 20:14:29431 // TODO(beng): Move BrowserList::AddBrowser() to the end of this function and
432 // replace uses of this with BL's notifications.
433 content::NotificationService::current()->Notify(
434 chrome::NOTIFICATION_BROWSER_WINDOW_READY,
435 content::Source<Browser>(this),
436 content::NotificationService::NoDetails());
437
438 // TODO(beng): move to ChromeBrowserMain:
[email protected]dfcc05f2013-02-21 14:12:16439 if (first_run::ShouldDoPersonalDataManagerFirstRun()) {
[email protected]a4fe67012012-07-25 20:14:29440#if defined(OS_WIN)
[email protected]dfcc05f2013-02-21 14:12:16441 // Notify PDM that this is a first run.
[email protected]bfd4b812013-04-11 02:02:11442 ImportAutofillDataWin(
443 autofill::PersonalDataManagerFactory::GetForProfile(profile_));
[email protected]a4fe67012012-07-25 20:14:29444#endif // defined(OS_WIN)
[email protected]a4fe67012012-07-25 20:14:29445 }
446
[email protected]6fb528e2012-08-01 20:32:33447 fullscreen_controller_.reset(new FullscreenController(this));
[email protected]010152f2014-07-15 00:16:47448
449 // Must be initialized after window_.
450 // Also: surprise! a modal dialog host is not necessary to host modal dialogs
451 // without a modal dialog host, so that value may be null.
452 popup_manager_.reset(new web_modal::PopupManager(
453 GetWebContentsModalDialogHost()));
initial.commit09911bf2008-07-26 23:55:29454}
455
456Browser::~Browser() {
[email protected]ebe07772014-05-22 04:16:06457 // Stop observing notifications before continuing with destruction. Profile
458 // destruction will unload extensions and reentrant calls to Browser:: should
459 // be avoided while it is being torn down.
460 registrar_.RemoveAll();
461
[email protected]2fc15ae2012-05-06 00:01:37462 // The tab strip should not have any tabs at this point.
[email protected]050e77182014-03-25 15:29:16463 DCHECK(tab_strip_model_->empty());
[email protected]2fc15ae2012-05-06 00:01:37464 tab_strip_model_->RemoveObserver(this);
465
[email protected]557c4e22012-12-18 18:30:55466 // Destroy the BrowserCommandController before removing the browser, so that
467 // it doesn't act on any notifications that are sent as a result of removing
468 // the browser.
469 command_controller_.reset();
initial.commit09911bf2008-07-26 23:55:29470 BrowserList::RemoveBrowser(this);
471
[email protected]bdc6ccd12012-03-20 22:06:13472 SessionService* session_service =
473 SessionServiceFactory::GetForProfile(profile_);
474 if (session_service)
475 session_service->WindowClosed(session_id_);
476
477 TabRestoreService* tab_restore_service =
478 TabRestoreServiceFactory::GetForProfile(profile());
479 if (tab_restore_service)
480 tab_restore_service->BrowserClosed(tab_restore_service_delegate());
481
[email protected]7be64502011-05-03 17:51:47482#if !defined(OS_MACOSX)
[email protected]c987a242013-02-28 01:17:41483 if (!chrome::GetTotalBrowserCountForProfile(profile_)) {
initial.commit09911bf2008-07-26 23:55:29484 // We're the last browser window with this profile. We need to nuke the
485 // TabRestoreService, which will start the shutdown of the
486 // NavigationControllers and allow for proper shutdown. If we don't do this
487 // chrome won't shutdown cleanly, and may end up crashing when some
488 // thread tries to use the IO thread (or another thread) that is no longer
489 // valid.
[email protected]fbc947b2009-06-19 13:28:24490 // This isn't a valid assumption for Mac OS, as it stays running after
491 // the last browser has closed. The Mac equivalent is in its app
492 // controller.
[email protected]92371eb2011-04-28 11:50:15493 TabRestoreServiceFactory::ResetForProfile(profile_);
initial.commit09911bf2008-07-26 23:55:29494 }
[email protected]fbc947b2009-06-19 13:28:24495#endif
initial.commit09911bf2008-07-26 23:55:29496
[email protected]97b46382011-05-02 13:23:04497 profile_pref_registrar_.RemoveAll();
[email protected]97b46382011-05-02 13:23:04498
[email protected]9a8c4022011-01-25 14:25:33499 encoding_auto_detect_.Destroy();
[email protected]9a8c4022011-01-25 14:25:33500
[email protected]fb0f9ebc2013-05-22 06:25:42501 // Destroy BrowserExtensionWindowController before the incognito profile
502 // is destroyed to make sure the chrome.windows.onRemoved event is sent.
503 extension_window_controller_.reset();
504
[email protected]6af41782013-06-22 13:49:11505 // Destroy BrowserInstantController before the incongnito profile is destroyed
506 // because the InstantController destructor depends on this profile.
507 instant_controller_.reset();
508
initial.commit09911bf2008-07-26 23:55:29509 if (profile_->IsOffTheRecord() &&
[email protected]474e5db2011-08-12 13:02:23510 !BrowserList::IsOffTheRecordSessionActiveForProfile(profile_)) {
[email protected]5e919242012-02-13 23:59:35511 // An incognito profile is no longer needed, this indirectly frees
512 // its cache and cookies once it gets destroyed at the appropriate time.
[email protected]4dffabe2012-05-19 14:37:06513 ProfileDestroyer::DestroyProfileWhenAppropriate(profile_);
initial.commit09911bf2008-07-26 23:55:29514 }
515
516 // There may be pending file dialogs, we need to tell them that we've gone
517 // away so they don't try and call back to us.
518 if (select_file_dialog_.get())
519 select_file_dialog_->ListenerDestroyed();
[email protected]5fec4702013-11-20 04:37:38520
521 int num_downloads;
522 if (OkToCloseWithInProgressDownloads(&num_downloads) ==
523 DOWNLOAD_CLOSE_BROWSER_SHUTDOWN &&
524 !browser_defaults::kBrowserAliveWithNoWindows) {
525 DownloadService::CancelAllDownloads();
526 }
initial.commit09911bf2008-07-26 23:55:29527}
528
[email protected]36b6dcb2008-11-12 01:19:57529///////////////////////////////////////////////////////////////////////////////
[email protected]5c238752009-06-13 10:29:07530// Getters & Setters
531
[email protected]b77cb302009-10-29 04:09:17532FindBarController* Browser::GetFindBarController() {
533 if (!find_bar_controller_.get()) {
[email protected]632983f2011-08-08 22:51:24534 FindBar* find_bar = window_->CreateFindBar();
[email protected]b77cb302009-10-29 04:09:17535 find_bar_controller_.reset(new FindBarController(find_bar));
536 find_bar->SetFindBarController(find_bar_controller_.get());
[email protected]0d56fa7212012-11-15 23:34:02537 find_bar_controller_->ChangeWebContents(
538 tab_strip_model_->GetActiveWebContents());
[email protected]b77cb302009-10-29 04:09:17539 find_bar_controller_->find_bar()->MoveWindowIfNecessary(gfx::Rect(), true);
540 }
541 return find_bar_controller_.get();
542}
543
[email protected]24db8a072009-10-29 20:35:37544bool Browser::HasFindBarController() const {
545 return find_bar_controller_.get() != NULL;
546}
547
[email protected]b35b26b32011-05-05 20:35:14548bool Browser::is_app() const {
549 return !app_name_.empty();
550}
551
552bool Browser::is_devtools() const {
553 return app_name_ == DevToolsWindow::kDevToolsApp;
554}
555
[email protected]5c238752009-06-13 10:29:07556///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:57557// Browser, State Storage and Retrieval for UI:
558
[email protected]9b125b732012-08-17 04:43:55559gfx::Image Browser::GetCurrentPageIcon() const {
[email protected]0d56fa7212012-11-15 23:34:02560 WebContents* web_contents = tab_strip_model_->GetActiveWebContents();
[email protected]502b5612012-10-02 01:33:10561 // |web_contents| can be NULL since GetCurrentPageIcon() is called by the
562 // window during the window's creation (before tabs have been added).
563 FaviconTabHelper* favicon_tab_helper =
564 web_contents ? FaviconTabHelper::FromWebContents(web_contents) : NULL;
565 return favicon_tab_helper ? favicon_tab_helper->GetFavicon() : gfx::Image();
[email protected]36b6dcb2008-11-12 01:19:57566}
567
[email protected]dcd0249872013-12-06 23:58:45568base::string16 Browser::GetWindowTitleForCurrentTab() const {
[email protected]0d56fa7212012-11-15 23:34:02569 WebContents* contents = tab_strip_model_->GetActiveWebContents();
[email protected]dcd0249872013-12-06 23:58:45570 base::string16 title;
[email protected]c7c42332008-11-15 01:10:54571
[email protected]731f8a42009-07-20 22:07:32572 // |contents| can be NULL because GetWindowTitleForCurrentTab is called by the
573 // window during the window's creation (before tabs have been added).
[email protected]36b6dcb2008-11-12 01:19:57574 if (contents) {
[email protected]f53ec988d2014-08-22 01:38:09575 // Streamlined hosted apps use the host instead of the title.
benwells39f23ae2014-08-27 08:01:52576 if (is_app() && extensions::util::IsStreamlinedHostedAppsEnabled())
[email protected]f53ec988d2014-08-22 01:38:09577 return base::UTF8ToUTF16(contents->GetURL().host());
[email protected]f53ec988d2014-08-22 01:38:09578
[email protected]dda5dfa32009-07-10 00:33:29579 title = contents->GetTitle();
[email protected]36b6dcb2008-11-12 01:19:57580 FormatTitleForDisplay(&title);
581 }
582 if (title.empty())
[email protected]51d83bdf2011-12-09 16:09:29583 title = CoreTabHelper::GetDefaultTitle();
[email protected]36b6dcb2008-11-12 01:19:57584
[email protected]82b9ed82012-11-15 02:43:18585#if defined(OS_MACOSX)
586 // On Mac, we don't want to suffix the page title with
[email protected]22a3daa2009-11-09 22:03:33587 // the application name.
[email protected]d904bc82009-07-07 00:41:04588 return title;
[email protected]82b9ed82012-11-15 02:43:18589#elif defined(USE_ASH)
590 // On Ash, we don't want to suffix the page title with the application name,
591 // but on Windows, where USE_ASH can also be true, we still want the prefix
592 // on desktop.
593 if (host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH)
594 return title;
595#endif
[email protected]dff529732010-04-01 23:50:55596 // Don't append the app name to window titles on app frames and app popups
[email protected]9c55de82012-11-01 21:03:52597 return is_app() ?
598 title :
599 l10n_util::GetStringFUTF16(IDS_BROWSER_WINDOW_TITLE_FORMAT, title);
[email protected]36b6dcb2008-11-12 01:19:57600}
601
602// static
[email protected]dcd0249872013-12-06 23:58:45603void Browser::FormatTitleForDisplay(base::string16* title) {
[email protected]36b6dcb2008-11-12 01:19:57604 size_t current_index = 0;
605 size_t match_index;
[email protected]dcd0249872013-12-06 23:58:45606 while ((match_index = title->find(L'\n', current_index)) !=
607 base::string16::npos) {
608 title->replace(match_index, 1, base::string16());
[email protected]36b6dcb2008-11-12 01:19:57609 current_index = match_index;
610 }
611}
612
613///////////////////////////////////////////////////////////////////////////////
614// Browser, OnBeforeUnload handling:
615
616bool Browser::ShouldCloseWindow() {
[email protected]69444cc2009-04-09 20:40:06617 if (!CanCloseWithInProgressDownloads())
618 return false;
619
[email protected]88c92012013-07-02 11:56:34620 if (IsFastTabUnloadEnabled())
621 return fast_unload_controller_->ShouldCloseWindow();
[email protected]f9b6c092012-07-09 19:32:28622 return unload_controller_->ShouldCloseWindow();
623}
[email protected]69444cc2009-04-09 20:40:06624
[email protected]2e9d79f2013-08-16 05:45:56625bool Browser::CallBeforeUnloadHandlers(
626 const base::Callback<void(bool)>& on_close_confirmed) {
[email protected]edfca702013-08-16 08:58:14627 cancel_download_confirmation_state_ = RESPONSE_RECEIVED;
[email protected]2e9d79f2013-08-16 05:45:56628 if (IsFastTabUnloadEnabled()) {
629 return fast_unload_controller_->CallBeforeUnloadHandlers(
630 on_close_confirmed);
631 }
632 return unload_controller_->CallBeforeUnloadHandlers(on_close_confirmed);
633}
634
635void Browser::ResetBeforeUnloadHandlers() {
[email protected]edfca702013-08-16 08:58:14636 cancel_download_confirmation_state_ = NOT_PROMPTED;
[email protected]2e9d79f2013-08-16 05:45:56637 if (IsFastTabUnloadEnabled())
638 fast_unload_controller_->ResetBeforeUnloadHandlers();
639 else
640 unload_controller_->ResetBeforeUnloadHandlers();
641}
642
[email protected]88c92012013-07-02 11:56:34643bool Browser::HasCompletedUnloadProcessing() const {
644 DCHECK(IsFastTabUnloadEnabled());
645 return fast_unload_controller_->HasCompletedUnloadProcessing();
646}
647
[email protected]f9b6c092012-07-09 19:32:28648bool Browser::IsAttemptingToCloseBrowser() const {
[email protected]88c92012013-07-02 11:56:34649 if (IsFastTabUnloadEnabled())
650 return fast_unload_controller_->is_attempting_to_close_browser();
[email protected]f9b6c092012-07-09 19:32:28651 return unload_controller_->is_attempting_to_close_browser();
[email protected]36b6dcb2008-11-12 01:19:57652}
653
654void Browser::OnWindowClosing() {
655 if (!ShouldCloseWindow())
656 return;
657
[email protected]c984d9f2010-07-20 20:52:20658 // Application should shutdown on last window close if the user is explicitly
659 // trying to quit, or if there is nothing keeping the browser alive (such as
660 // AppController on the Mac, or BackgroundContentsService for background
661 // pages).
662 bool should_quit_if_last_browser =
[email protected]313fce12013-01-30 17:09:04663 browser_shutdown::IsTryingToQuit() || !chrome::WillKeepAlive();
[email protected]7dc8c6b2010-04-09 17:02:50664
[email protected]29e1da632013-12-18 19:27:27665 if (should_quit_if_last_browser && chrome::ShouldStartShutdown(this))
[email protected]36b6dcb2008-11-12 01:19:57666 browser_shutdown::OnShutdownStarting(browser_shutdown::WINDOW_CLOSE);
667
[email protected]92371eb2011-04-28 11:50:15668 // Don't use GetForProfileIfExisting here, we want to force creation of the
[email protected]36b6dcb2008-11-12 01:19:57669 // session service so that user can restore what was open.
[email protected]92371eb2011-04-28 11:50:15670 SessionService* session_service =
671 SessionServiceFactory::GetForProfile(profile());
[email protected]36b6dcb2008-11-12 01:19:57672 if (session_service)
673 session_service->WindowClosing(session_id());
674
[email protected]92371eb2011-04-28 11:50:15675 TabRestoreService* tab_restore_service =
676 TabRestoreServiceFactory::GetForProfile(profile());
[email protected]a636d8e52012-02-28 15:40:41677
678#if defined(USE_AURA)
[email protected]d019fa582013-11-04 23:45:49679 if (tab_restore_service && is_app() && !is_devtools())
[email protected]a636d8e52012-02-28 15:40:41680 tab_restore_service->BrowserClosing(tab_restore_service_delegate());
681#endif
682
[email protected]e0448872013-01-11 19:35:02683 if (tab_restore_service && is_type_tabbed() && tab_strip_model_->count())
[email protected]fb7da4132011-03-15 15:51:00684 tab_restore_service->BrowserClosing(tab_restore_service_delegate());
[email protected]d8375fd2008-11-25 22:45:39685
[email protected]911f8152010-03-18 16:46:40686 // TODO(sky): convert session/tab restore to use notification.
[email protected]ad50def52011-10-19 23:17:07687 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27688 chrome::NOTIFICATION_BROWSER_CLOSING,
[email protected]6c2381d2011-10-19 02:52:53689 content::Source<Browser>(this),
[email protected]1e0c7402012-07-27 10:48:15690 content::NotificationService::NoDetails());
[email protected]ed1bb7202013-06-21 05:16:44691
[email protected]88c92012013-07-02 11:56:34692 if (!IsFastTabUnloadEnabled())
693 tab_strip_model_->CloseAllTabs();
[email protected]36b6dcb2008-11-12 01:19:57694}
695
[email protected]69444cc2009-04-09 20:40:06696////////////////////////////////////////////////////////////////////////////////
697// In-progress download termination handling:
698
699void Browser::InProgressDownloadResponse(bool cancel_downloads) {
700 if (cancel_downloads) {
701 cancel_download_confirmation_state_ = RESPONSE_RECEIVED;
[email protected]a37d4b02012-06-25 21:56:10702 chrome::CloseWindow(this);
[email protected]69444cc2009-04-09 20:40:06703 return;
704 }
705
706 // Sets the confirmation state to NOT_PROMPTED so that if the user tries to
707 // close again we'll show the warning again.
708 cancel_download_confirmation_state_ = NOT_PROMPTED;
709
710 // Show the download page so the user can figure-out what downloads are still
711 // in-progress.
[email protected]5d9cace72012-06-21 16:07:12712 chrome::ShowDownloads(this);
[email protected]674b65fb2014-02-19 02:43:42713
714 // Reset UnloadController::is_attempting_to_close_browser_ so that we don't
715 // prompt every time any tab is closed. http://crbug.com/305516
716 if (IsFastTabUnloadEnabled())
717 fast_unload_controller_->CancelWindowClose();
718 else
719 unload_controller_->CancelWindowClose();
[email protected]69444cc2009-04-09 20:40:06720}
721
[email protected]5a17d492011-10-24 19:37:03722Browser::DownloadClosePreventionType Browser::OkToCloseWithInProgressDownloads(
723 int* num_downloads_blocking) const {
724 DCHECK(num_downloads_blocking);
725 *num_downloads_blocking = 0;
726
[email protected]5a17d492011-10-24 19:37:03727 // If we're not running a full browser process with a profile manager
728 // (testing), it's ok to close the browser.
729 if (!g_browser_process->profile_manager())
730 return DOWNLOAD_CLOSE_OK;
731
[email protected]422a7d12013-10-21 12:10:42732 int total_download_count =
733 DownloadService::NonMaliciousDownloadCountAllProfiles();
[email protected]5a17d492011-10-24 19:37:03734 if (total_download_count == 0)
735 return DOWNLOAD_CLOSE_OK; // No downloads; can definitely close.
736
737 // Figure out how many windows are open total, and associated with this
738 // profile, that are relevant for the ok-to-close decision.
739 int profile_window_count = 0;
740 int total_window_count = 0;
[email protected]3539929f2013-02-01 05:59:14741 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
[email protected]5a17d492011-10-24 19:37:03742 // Don't count this browser window or any other in the process of closing.
[email protected]3539929f2013-02-01 05:59:14743 Browser* const browser = *it;
[email protected]f9b6c092012-07-09 19:32:28744 // Window closing may be delayed, and windows that are in the process of
745 // closing don't count against our totals.
746 if (browser == this || browser->IsAttemptingToCloseBrowser())
[email protected]5a17d492011-10-24 19:37:03747 continue;
748
[email protected]3539929f2013-02-01 05:59:14749 if (it->profile() == profile())
[email protected]5a17d492011-10-24 19:37:03750 profile_window_count++;
751 total_window_count++;
752 }
753
754 // If there aren't any other windows, we're at browser shutdown,
755 // which would cancel all current downloads.
756 if (total_window_count == 0) {
757 *num_downloads_blocking = total_download_count;
758 return DOWNLOAD_CLOSE_BROWSER_SHUTDOWN;
759 }
760
761 // If there aren't any other windows on our profile, and we're an incognito
762 // profile, and there are downloads associated with that profile,
763 // those downloads would be cancelled by our window (-> profile) close.
764 DownloadService* download_service =
[email protected]423939d2013-07-31 20:00:08765 DownloadServiceFactory::GetForBrowserContext(profile());
[email protected]d07d63fc2013-07-18 19:44:54766 if ((profile_window_count == 0) &&
[email protected]422a7d12013-10-21 12:10:42767 (download_service->NonMaliciousDownloadCount() > 0) &&
[email protected]5a17d492011-10-24 19:37:03768 profile()->IsOffTheRecord()) {
[email protected]422a7d12013-10-21 12:10:42769 *num_downloads_blocking = download_service->NonMaliciousDownloadCount();
[email protected]5a17d492011-10-24 19:37:03770 return DOWNLOAD_CLOSE_LAST_WINDOW_IN_INCOGNITO_PROFILE;
771 }
772
773 // Those are the only conditions under which we will block shutdown.
774 return DOWNLOAD_CLOSE_OK;
775}
776
[email protected]69444cc2009-04-09 20:40:06777////////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:57778// Browser, Tab adding/showing functions:
779
[email protected]227393e2011-06-29 21:28:39780void Browser::WindowFullscreenStateChanged() {
[email protected]01a5a762011-11-21 05:40:55781 fullscreen_controller_->WindowFullscreenStateChanged();
[email protected]5d98294912012-06-27 22:57:40782 command_controller_->FullscreenStateChanged();
[email protected]9adb8b12011-12-09 06:49:13783 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TOGGLE_FULLSCREEN);
[email protected]ae2622c2009-07-30 23:47:58784}
785
[email protected]36b6dcb2008-11-12 01:19:57786///////////////////////////////////////////////////////////////////////////////
787// Browser, Assorted browser commands:
788
[email protected]41d9faf2012-02-28 23:46:02789void Browser::ToggleFullscreenModeWithExtension(const GURL& extension_url) {
[email protected]7005a3e2014-03-12 01:43:22790 fullscreen_controller_->
791 ToggleBrowserFullscreenModeWithExtension(extension_url);
[email protected]9282cea2009-02-18 18:49:00792}
793
[email protected]a3e18c42009-03-04 23:36:05794bool Browser::SupportsWindowFeature(WindowFeature feature) const {
[email protected]018cf362010-05-05 22:43:22795 return SupportsWindowFeatureImpl(feature, true);
796}
[email protected]f5bf8ccf2010-02-05 18:19:25797
[email protected]018cf362010-05-05 22:43:22798bool Browser::CanSupportWindowFeature(WindowFeature feature) const {
799 return SupportsWindowFeatureImpl(feature, false);
[email protected]a3e18c42009-03-04 23:36:05800}
801
[email protected]fbd77592008-11-12 20:50:27802void Browser::ToggleEncodingAutoDetect() {
[email protected]7f6f44c2011-12-14 13:23:38803 content::RecordAction(UserMetricsAction("AutoDetectChange"));
[email protected]fbd77592008-11-12 20:50:27804 encoding_auto_detect_.SetValue(!encoding_auto_detect_.GetValue());
[email protected]a697f4c2009-09-14 22:30:18805 // If "auto detect" is turned on, then any current override encoding
806 // is cleared. This also implicitly performs a reload.
807 // OTOH, if "auto detect" is turned off, we don't change the currently
808 // active encoding.
809 if (encoding_auto_detect_.GetValue()) {
[email protected]0d56fa7212012-11-15 23:34:02810 WebContents* contents = tab_strip_model_->GetActiveWebContents();
[email protected]a697f4c2009-09-14 22:30:18811 if (contents)
[email protected]b2fe07d12010-02-09 14:38:08812 contents->ResetOverrideEncoding();
[email protected]a697f4c2009-09-14 22:30:18813 }
[email protected]fbd77592008-11-12 20:50:27814}
815
816void Browser::OverrideEncoding(int encoding_id) {
[email protected]7f6f44c2011-12-14 13:23:38817 content::RecordAction(UserMetricsAction("OverrideEncoding"));
[email protected]41fc0322009-09-04 22:23:40818 const std::string selected_encoding =
[email protected]fbd77592008-11-12 20:50:27819 CharacterEncoding::GetCanonicalEncodingNameByCommandId(encoding_id);
[email protected]0d56fa7212012-11-15 23:34:02820 WebContents* contents = tab_strip_model_->GetActiveWebContents();
[email protected]57c6a652009-05-04 07:58:34821 if (!selected_encoding.empty() && contents)
[email protected]b2fe07d12010-02-09 14:38:08822 contents->SetOverrideEncoding(selected_encoding);
[email protected]fbd77592008-11-12 20:50:27823 // Update the list of recently selected encodings.
[email protected]41fc0322009-09-04 22:23:40824 std::string new_selected_encoding_list;
[email protected]ddd231e2010-06-29 20:35:19825 if (CharacterEncoding::UpdateRecentlySelectedEncoding(
826 profile_->GetPrefs()->GetString(prefs::kRecentlySelectedEncoding),
[email protected]41fc0322009-09-04 22:23:40827 encoding_id,
828 &new_selected_encoding_list)) {
[email protected]fbd77592008-11-12 20:50:27829 profile_->GetPrefs()->SetString(prefs::kRecentlySelectedEncoding,
[email protected]ddd231e2010-06-29 20:35:19830 new_selected_encoding_list);
[email protected]fbd77592008-11-12 20:50:27831 }
[email protected]36b6dcb2008-11-12 01:19:57832}
833
[email protected]84214982008-12-10 18:49:10834void Browser::OpenFile() {
[email protected]7f6f44c2011-12-14 13:23:38835 content::RecordAction(UserMetricsAction("OpenFile"));
[email protected]92f54082012-07-31 01:43:14836 select_file_dialog_ = ui::SelectFileDialog::Create(
[email protected]6e1fcd12012-07-02 17:14:20837 this, new ChromeSelectFilePolicy(
[email protected]0d56fa7212012-11-15 23:34:02838 tab_strip_model_->GetActiveWebContents()));
[email protected]84214982008-12-10 18:49:10839
[email protected]650b2d52013-02-10 03:41:45840 const base::FilePath directory = profile_->last_selected_directory();
[email protected]35896a32010-06-09 08:42:51841
[email protected]84214982008-12-10 18:49:10842 // TODO(beng): figure out how to juggle this.
[email protected]90556dd2012-06-07 20:26:18843 gfx::NativeWindow parent_window = window_->GetNativeWindow();
[email protected]479cce782012-09-15 20:15:53844 ui::SelectFileDialog::FileTypeInfo file_types;
[email protected]599538b2013-02-20 14:15:14845 file_types.support_drive = true;
[email protected]92f54082012-07-31 01:43:14846 select_file_dialog_->SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE,
[email protected]dcd0249872013-12-06 23:58:45847 base::string16(),
[email protected]007b3f82013-04-09 08:46:45848 directory,
849 &file_types,
850 0,
851 base::FilePath::StringType(),
852 parent_window,
853 NULL);
[email protected]84214982008-12-10 18:49:10854}
855
[email protected]92655362012-04-20 21:33:17856void Browser::UpdateDownloadShelfVisibility(bool visible) {
857 if (GetStatusBubble())
858 GetStatusBubble()->UpdateDownloadShelfVisibility(visible);
859}
860
[email protected]36b6dcb2008-11-12 01:19:57861///////////////////////////////////////////////////////////////////////////////
862
[email protected]e232c992012-12-06 12:43:20863void Browser::UpdateUIForNavigationInTab(WebContents* contents,
[email protected]ef557022012-03-16 10:05:33864 content::PageTransition transition,
865 bool user_initiated) {
[email protected]e232c992012-12-06 12:43:20866 tab_strip_model_->TabNavigating(contents, transition);
[email protected]ef557022012-03-16 10:05:33867
[email protected]0d56fa7212012-11-15 23:34:02868 bool contents_is_selected =
[email protected]e232c992012-12-06 12:43:20869 contents == tab_strip_model_->GetActiveWebContents();
[email protected]ef557022012-03-16 10:05:33870 if (user_initiated && contents_is_selected && window()->GetLocationBar()) {
871 // Forcibly reset the location bar if the url is going to change in the
872 // current tab, since otherwise it won't discard any ongoing user edits,
873 // since it doesn't realize this is a user-initiated action.
874 window()->GetLocationBar()->Revert();
875 }
876
877 if (GetStatusBubble())
878 GetStatusBubble()->Hide();
879
880 // Update the location bar. This is synchronous. We specifically don't
881 // update the load state since the load hasn't started yet and updating it
882 // will put it out of sync with the actual state like whether we're
883 // displaying a favicon, which controls the throbber. If we updated it here,
884 // the throbber will show the default favicon for a split second when
885 // navigating away from the new tab page.
[email protected]e232c992012-12-06 12:43:20886 ScheduleUIUpdate(contents, content::INVALIDATE_TYPE_URL);
[email protected]ef557022012-03-16 10:05:33887
[email protected]e1f288d2012-05-23 19:36:10888 if (contents_is_selected)
[email protected]fc2b46b2014-05-03 16:33:45889 contents->SetInitialFocus();
[email protected]ef557022012-03-16 10:05:33890}
891
892///////////////////////////////////////////////////////////////////////////////
893// Browser, PageNavigator implementation:
894
895WebContents* Browser::OpenURL(const OpenURLParams& params) {
896 return OpenURLFromTab(NULL, params);
897}
898
[email protected]485fba42009-03-24 23:27:29899///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:57900// Browser, TabStripModelObserver implementation:
901
[email protected]409ea2972012-11-10 19:54:43902void Browser::TabInsertedAt(WebContents* contents,
[email protected]36b6dcb2008-11-12 01:19:57903 int index,
904 bool foreground) {
[email protected]63d1f9b2014-07-05 19:09:03905 SetAsDelegate(contents, true);
[email protected]010152f2014-07-15 00:16:47906
907 if (popup_manager_)
908 popup_manager_->RegisterWith(contents);
909
[email protected]50a9a76b2012-09-12 14:19:36910 SessionTabHelper* session_tab_helper =
[email protected]409ea2972012-11-10 19:54:43911 SessionTabHelper::FromWebContents(contents);
[email protected]50a9a76b2012-09-12 14:19:36912 session_tab_helper->SetWindowID(session_id());
[email protected]e524ff92011-03-30 13:26:58913
[email protected]fb312eb2012-09-24 23:21:10914 content::NotificationService::current()->Notify(
915 chrome::NOTIFICATION_TAB_PARENTED,
[email protected]409ea2972012-11-10 19:54:43916 content::Source<content::WebContents>(contents),
[email protected]fb312eb2012-09-24 23:21:10917 content::NotificationService::NoDetails());
918
[email protected]c37e3b62010-01-22 00:15:17919 SyncHistoryWithTabs(index);
[email protected]36b6dcb2008-11-12 01:19:57920
[email protected]159f7762008-12-19 14:58:27921 // Make sure the loading state is updated correctly, otherwise the throbber
922 // won't start if the page is loading.
[email protected]e3b10d12014-03-28 16:06:09923 LoadingStateChanged(contents, true);
[email protected]159f7762008-12-19 14:58:27924
[email protected]20ca0382013-02-28 19:50:07925 interstitial_observers_.push_back(new InterstitialObserver(this, contents));
[email protected]e397a4472011-12-21 21:47:50926
[email protected]40600152012-09-26 10:04:31927 SessionService* session_service =
928 SessionServiceFactory::GetForProfile(profile_);
[email protected]d4c9caa2013-04-22 18:30:32929 if (session_service) {
[email protected]409ea2972012-11-10 19:54:43930 session_service->TabInserted(contents);
[email protected]d4c9caa2013-04-22 18:30:32931 int new_active_index = tab_strip_model_->active_index();
932 if (index < new_active_index)
933 session_service->SetSelectedTabInWindow(session_id(),
934 new_active_index);
935 }
[email protected]36b6dcb2008-11-12 01:19:57936}
937
[email protected]f2cc7382010-10-15 18:03:05938void Browser::TabClosingAt(TabStripModel* tab_strip_model,
[email protected]0be097d2012-11-10 00:20:16939 WebContents* contents,
[email protected]f2cc7382010-10-15 18:03:05940 int index) {
[email protected]0be097d2012-11-10 00:20:16941 fullscreen_controller_->OnTabClosing(contents);
[email protected]40600152012-09-26 10:04:31942 SessionService* session_service =
943 SessionServiceFactory::GetForProfile(profile_);
944 if (session_service)
[email protected]0be097d2012-11-10 00:20:16945 session_service->TabClosing(contents);
[email protected]ad50def52011-10-19 23:17:07946 content::NotificationService::current()->Notify(
[email protected]884033e2012-04-16 19:38:42947 chrome::NOTIFICATION_TAB_CLOSING,
[email protected]0be097d2012-11-10 00:20:16948 content::Source<NavigationController>(&contents->GetController()),
[email protected]ad50def52011-10-19 23:17:07949 content::NotificationService::NoDetails());
[email protected]36b6dcb2008-11-12 01:19:57950
[email protected]010152f2014-07-15 00:16:47951 if (popup_manager_)
952 popup_manager_->UnregisterWith(contents);
953
[email protected]075969d2012-04-17 20:05:55954 // Sever the WebContents' connection back to us.
[email protected]63d1f9b2014-07-05 19:09:03955 SetAsDelegate(contents, false);
[email protected]36b6dcb2008-11-12 01:19:57956}
957
[email protected]e89cfcb2012-11-11 14:47:24958void Browser::TabDetachedAt(WebContents* contents, int index) {
[email protected]d4c9caa2013-04-22 18:30:32959 // TabDetachedAt is called before TabStripModel has updated the
960 // active index.
961 int old_active_index = tab_strip_model_->active_index();
962 if (index < old_active_index && !tab_strip_model_->closing_all()) {
963 SessionService* session_service =
964 SessionServiceFactory::GetForProfileIfExisting(profile_);
965 if (session_service)
966 session_service->SetSelectedTabInWindow(session_id(),
967 old_active_index - 1);
968 }
[email protected]010152f2014-07-15 00:16:47969
970 if (popup_manager_)
971 popup_manager_->UnregisterWith(contents);
972
[email protected]c37e3b62010-01-22 00:15:17973 TabDetachedAtImpl(contents, index, DETACH_TYPE_DETACH);
[email protected]36b6dcb2008-11-12 01:19:57974}
975
[email protected]023c01cc2012-11-12 15:41:11976void Browser::TabDeactivated(WebContents* contents) {
[email protected]01a5a762011-11-21 05:40:55977 fullscreen_controller_->OnTabDeactivated(contents);
[email protected]023c01cc2012-11-12 15:41:11978 search_delegate_->OnTabDeactivated(contents);
[email protected]549f62b2013-10-26 12:54:42979 SearchTabHelper::FromWebContents(contents)->OnTabDeactivated();
[email protected]e3a5d5b2012-06-22 17:56:46980
[email protected]85ff2c42009-05-29 21:22:03981 // Save what the user's currently typing, so it can be restored when we
982 // switch back to this tab.
[email protected]023c01cc2012-11-12 15:41:11983 window_->GetLocationBar()->SaveStateToContents(contents);
[email protected]3d6a8952012-12-14 03:18:07984
985 if (instant_controller_)
986 instant_controller_->TabDeactivated(contents);
[email protected]85ff2c42009-05-29 21:22:03987}
988
[email protected]8e707792012-11-13 10:32:12989void Browser::ActiveTabChanged(WebContents* old_contents,
990 WebContents* new_contents,
[email protected]3aca95d42011-05-24 22:06:54991 int index,
[email protected]5f2de242013-04-05 02:06:07992 int reason) {
[email protected]d500b1c32013-03-12 06:26:08993 content::RecordAction(UserMetricsAction("ActiveTabChanged"));
994
[email protected]da9ac4ee2013-10-02 12:19:05995 // Update the bookmark state, since the BrowserWindow may query it during
996 // OnActiveTabChanged() below.
997 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_SWITCH);
998
999 // Let the BrowserWindow do its handling. On e.g. views this changes the
1000 // focused object, which should happen before we update the toolbar below,
[email protected]08726d5e2013-09-24 21:52:551001 // since the omnibox expects the correct element to already be focused when it
1002 // is updated.
1003 window_->OnActiveTabChanged(old_contents, new_contents, index, reason);
1004
[email protected]949f62a32014-02-13 00:53:101005 fullscreen_controller_->OnTabDetachedFromView(old_contents);
1006
[email protected]002efb042011-10-07 17:30:241007 // Discarded tabs always get reloaded.
[email protected]00a0fad2013-05-15 09:08:381008 if (tab_strip_model_->IsTabDiscarded(index)) {
[email protected]d6d4dae2012-04-18 17:47:321009 LOG(WARNING) << "Reloading discarded tab at " << index;
1010 static int reload_count = 0;
[email protected]24c463a2012-04-20 22:33:501011 UMA_HISTOGRAM_CUSTOM_COUNTS(
1012 "Tabs.Discard.ReloadCount", ++reload_count, 1, 1000, 50);
[email protected]a37d4b02012-06-25 21:56:101013 chrome::Reload(this, CURRENT_TAB);
[email protected]002efb042011-10-07 17:30:241014 }
1015
[email protected]36b6dcb2008-11-12 01:19:571016 // If we have any update pending, do it now.
[email protected]24f1477532011-11-22 22:55:361017 if (chrome_updater_factory_.HasWeakPtrs() && old_contents)
[email protected]36b6dcb2008-11-12 01:19:571018 ProcessPendingUIUpdates();
1019
[email protected]36b6dcb2008-11-12 01:19:571020 // Propagate the profile to the location bar.
[email protected]9bc3f242013-04-05 23:27:361021 UpdateToolbar((reason & CHANGE_REASON_REPLACED) == 0);
[email protected]36b6dcb2008-11-12 01:19:571022
[email protected]8845bb42013-10-29 00:36:291023 if (chrome::IsInstantExtendedAPIEnabled())
1024 search_delegate_->OnTabActivated(new_contents);
[email protected]e3a5d5b2012-06-22 17:56:461025
[email protected]943d8122010-06-11 02:13:451026 // Update reload/stop state.
[email protected]8e707792012-11-13 10:32:121027 command_controller_->LoadingStateChanged(new_contents->IsLoading(), true);
[email protected]36b6dcb2008-11-12 01:19:571028
[email protected]84214982008-12-10 18:49:101029 // Update commands to reflect current state.
[email protected]5d98294912012-06-27 22:57:401030 command_controller_->TabStateChanged();
[email protected]36b6dcb2008-11-12 01:19:571031
1032 // Reset the status bubble.
[email protected]be3877f2009-01-14 15:51:101033 StatusBubble* status_bubble = GetStatusBubble();
1034 if (status_bubble) {
1035 status_bubble->Hide();
[email protected]36b6dcb2008-11-12 01:19:571036
[email protected]be3877f2009-01-14 15:51:101037 // Show the loading state (if any).
[email protected]0d56fa7212012-11-15 23:34:021038 status_bubble->SetStatus(CoreTabHelper::FromWebContents(
1039 tab_strip_model_->GetActiveWebContents())->GetStatusText());
[email protected]be3877f2009-01-14 15:51:101040 }
[email protected]36b6dcb2008-11-12 01:19:571041
[email protected]826eabe12013-09-30 20:33:381042 if (HasFindBarController()) {
1043 find_bar_controller_->ChangeWebContents(new_contents);
1044 find_bar_controller_->find_bar()->MoveWindowIfNecessary(gfx::Rect(), true);
1045 }
1046
[email protected]36b6dcb2008-11-12 01:19:571047 // Update sessions. Don't force creation of sessions. If sessions doesn't
1048 // exist, the change will be picked up by sessions when created.
[email protected]92371eb2011-04-28 11:50:151049 SessionService* session_service =
1050 SessionServiceFactory::GetForProfileIfExisting(profile_);
[email protected]2fc15ae2012-05-06 00:01:371051 if (session_service && !tab_strip_model_->closing_all()) {
[email protected]04e3ead52013-01-14 01:09:201052 session_service->SetSelectedTabInWindow(session_id(),
1053 tab_strip_model_->active_index());
[email protected]36b6dcb2008-11-12 01:19:571054 }
[email protected]09b29342011-06-24 19:18:481055
[email protected]8845bb42013-10-29 00:36:291056 // This needs to be called after notifying SearchDelegate.
[email protected]e41982a72012-11-20 07:16:511057 if (instant_controller_)
1058 instant_controller_->ActiveTabChanged();
[email protected]10090d52013-07-25 20:41:371059
[email protected]b4db6d42014-06-07 04:36:581060 autofill::ChromeAutofillClient::FromWebContents(new_contents)->TabActivated();
[email protected]549f62b2013-10-26 12:54:421061 SearchTabHelper::FromWebContents(new_contents)->OnTabActivated();
[email protected]36b6dcb2008-11-12 01:19:571062}
1063
[email protected]3abdff22012-11-13 10:28:451064void Browser::TabMoved(WebContents* contents,
[email protected]36b6dcb2008-11-12 01:19:571065 int from_index,
[email protected]1a242c32010-02-02 21:20:541066 int to_index) {
[email protected]36b6dcb2008-11-12 01:19:571067 DCHECK(from_index >= 0 && to_index >= 0);
1068 // Notify the history service.
1069 SyncHistoryWithTabs(std::min(from_index, to_index));
1070}
1071
[email protected]da25dd52011-01-11 19:06:301072void Browser::TabReplacedAt(TabStripModel* tab_strip_model,
[email protected]b624ddc2012-11-15 18:04:131073 WebContents* old_contents,
1074 WebContents* new_contents,
[email protected]8a34e6602010-10-02 17:29:431075 int index) {
[email protected]b624ddc2012-11-15 18:04:131076 TabDetachedAtImpl(old_contents, index, DETACH_TYPE_REPLACE);
[email protected]aa0ec7b2013-06-27 18:04:191077 fullscreen_controller_->OnTabClosing(old_contents);
[email protected]40600152012-09-26 10:04:311078 SessionService* session_service =
1079 SessionServiceFactory::GetForProfile(profile_);
1080 if (session_service)
[email protected]b624ddc2012-11-15 18:04:131081 session_service->TabClosing(old_contents);
[email protected]04e3ead52013-01-14 01:09:201082 TabInsertedAt(new_contents,
1083 index,
1084 (index == tab_strip_model_->active_index()));
[email protected]c37e3b62010-01-22 00:15:171085
[email protected]b624ddc2012-11-15 18:04:131086 int entry_count = new_contents->GetController().GetEntryCount();
[email protected]c37e3b62010-01-22 00:15:171087 if (entry_count > 0) {
1088 // Send out notification so that observers are updated appropriately.
[email protected]b624ddc2012-11-15 18:04:131089 new_contents->GetController().NotifyEntryChanged(
1090 new_contents->GetController().GetEntryAtIndex(entry_count - 1),
[email protected]c37e3b62010-01-22 00:15:171091 entry_count - 1);
1092 }
[email protected]47e020a2010-10-15 14:43:371093
[email protected]47e020a2010-10-15 14:43:371094 if (session_service) {
1095 // The new_contents may end up with a different navigation stack. Force
1096 // the session service to update itself.
[email protected]52877dbc62012-06-29 22:22:031097 session_service->TabRestored(new_contents,
1098 tab_strip_model_->IsTabPinned(index));
[email protected]47e020a2010-10-15 14:43:371099 }
[email protected]c37e3b62010-01-22 00:15:171100}
1101
[email protected]2c7dba42012-11-09 17:05:421102void Browser::TabPinnedStateChanged(WebContents* contents, int index) {
[email protected]92371eb2011-04-28 11:50:151103 SessionService* session_service =
1104 SessionServiceFactory::GetForProfileIfExisting(profile());
[email protected]5c0e6482009-07-14 20:20:091105 if (session_service) {
[email protected]50a9a76b2012-09-12 14:19:361106 SessionTabHelper* session_tab_helper =
[email protected]2c7dba42012-11-09 17:05:421107 SessionTabHelper::FromWebContents(contents);
1108 session_service->SetPinnedState(session_id(),
1109 session_tab_helper->session_id(),
1110 tab_strip_model_->IsTabPinned(index));
[email protected]5c0e6482009-07-14 20:20:091111 }
1112}
1113
[email protected]36b6dcb2008-11-12 01:19:571114void Browser::TabStripEmpty() {
1115 // Close the frame after we return to the message loop (not immediately,
1116 // otherwise it will destroy this object before the stack has a chance to
1117 // cleanly unwind.)
1118 // Note: This will be called several times if TabStripEmpty is called several
1119 // times. This is because it does not close the window if tabs are
1120 // still present.
[email protected]3264d162013-05-08 22:02:111121 base::MessageLoop::current()->PostTask(
[email protected]24f1477532011-11-22 22:55:361122 FROM_HERE, base::Bind(&Browser::CloseFrame, weak_factory_.GetWeakPtr()));
[email protected]e41982a72012-11-20 07:16:511123
1124 // Instant may have visible WebContents that need to be detached before the
1125 // window system closes.
1126 instant_controller_.reset();
[email protected]36b6dcb2008-11-12 01:19:571127}
1128
[email protected]067310262012-11-22 14:30:411129bool Browser::CanOverscrollContent() const {
1130#if defined(USE_AURA)
[email protected]b1d5b192014-03-04 05:36:201131 const std::string value = CommandLine::ForCurrentProcess()->
1132 GetSwitchValueASCII(switches::kOverscrollHistoryNavigation);
1133 bool overscroll_enabled = value != "0";
[email protected]86f98a22013-03-20 14:35:001134 if (!overscroll_enabled)
1135 return false;
1136 if (is_app() || is_devtools() || !is_type_tabbed())
1137 return false;
1138
1139 // The detached bookmark bar has appearance of floating above the
1140 // web-contents. This does not play nicely with overscroll navigation
1141 // gestures. So disable overscroll navigation when the bookmark bar is in the
[email protected]b1d5b192014-03-04 05:36:201142 // detached state and the overscroll effect moves the layers.
1143 if (value == "1" && bookmark_bar_state_ == BookmarkBar::DETACHED)
[email protected]86f98a22013-03-20 14:35:001144 return false;
1145 return true;
[email protected]067310262012-11-22 14:30:411146#else
1147 return false;
1148#endif
1149}
1150
[email protected]aa62afd2014-04-22 19:22:461151bool Browser::ShouldPreserveAbortedURLs(WebContents* source) {
1152 // Allow failed URLs to stick around in the omnibox on the NTP, but not when
1153 // other pages have committed.
1154 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext());
1155 if (!profile || !source->GetController().GetLastCommittedEntry())
1156 return false;
1157 GURL committed_url(source->GetController().GetLastCommittedEntry()->GetURL());
1158 return chrome::IsNTPURL(committed_url, profile);
1159}
1160
[email protected]b3996ba2012-08-08 00:39:131161bool Browser::PreHandleKeyboardEvent(content::WebContents* source,
1162 const NativeWebKeyboardEvent& event,
[email protected]fc761142012-06-08 04:49:201163 bool* is_keyboard_shortcut) {
1164 // Escape exits tabbed fullscreen mode.
1165 // TODO(koz): Write a test for this http://crbug.com/100441.
1166 if (event.windowsKeyCode == 27 &&
1167 fullscreen_controller_->HandleUserPressedEscape()) {
1168 return true;
1169 }
1170 return window()->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
1171}
1172
[email protected]b3996ba2012-08-08 00:39:131173void Browser::HandleKeyboardEvent(content::WebContents* source,
1174 const NativeWebKeyboardEvent& event) {
[email protected]93e0baba2014-04-08 20:04:021175 DevToolsWindow* devtools_window =
1176 DevToolsWindow::GetInstanceForInspectedWebContents(source);
1177 bool handled = false;
1178 if (devtools_window)
1179 handled = devtools_window->ForwardKeyboardEvent(event);
1180
1181 if (!handled)
1182 window()->HandleKeyboardEvent(event);
[email protected]fc761142012-06-08 04:49:201183}
1184
[email protected]ed1bb7202013-06-21 05:16:441185bool Browser::TabsNeedBeforeUnloadFired() {
[email protected]88c92012013-07-02 11:56:341186 if (IsFastTabUnloadEnabled())
1187 return fast_unload_controller_->TabsNeedBeforeUnloadFired();
[email protected]ed1bb7202013-06-21 05:16:441188 return unload_controller_->TabsNeedBeforeUnloadFired();
1189}
1190
[email protected]888878e82013-07-24 22:49:401191void Browser::OverscrollUpdate(int delta_y) {
1192 window_->OverscrollUpdate(delta_y);
1193}
1194
[email protected]edc3af82013-12-12 21:24:071195void Browser::ShowValidationMessage(content::WebContents* web_contents,
1196 const gfx::Rect& anchor_in_root_view,
[email protected]ebf04a862013-12-17 08:38:581197 const base::string16& main_text,
1198 const base::string16& sub_text) {
[email protected]edc3af82013-12-12 21:24:071199 RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
1200 if (rwhv) {
1201 validation_message_bubble_ =
1202 chrome::ValidationMessageBubble::CreateAndShow(
1203 rwhv->GetRenderWidgetHost(),
1204 anchor_in_root_view,
1205 main_text,
1206 sub_text);
1207 }
1208}
1209
1210void Browser::HideValidationMessage(content::WebContents* web_contents) {
1211 validation_message_bubble_.reset();
1212}
1213
1214void Browser::MoveValidationMessage(content::WebContents* web_contents,
1215 const gfx::Rect& anchor_in_root_view) {
1216 if (!validation_message_bubble_)
1217 return;
1218 RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
1219 if (rwhv) {
1220 validation_message_bubble_->SetPositionRelativeToAnchor(
1221 rwhv->GetRenderWidgetHost(), anchor_in_root_view);
1222 }
1223}
1224
[email protected]7a0c5aea2014-03-05 17:41:171225bool Browser::PreHandleGestureEvent(content::WebContents* source,
1226 const blink::WebGestureEvent& event) {
1227 // Disable pinch zooming in undocked dev tools window due to poor UX.
1228 if (app_name() == DevToolsWindow::kDevToolsApp)
1229 return event.type == blink::WebGestureEvent::GesturePinchBegin ||
1230 event.type == blink::WebGestureEvent::GesturePinchUpdate ||
1231 event.type == blink::WebGestureEvent::GesturePinchEnd;
1232
1233 return false;
1234}
1235
[email protected]f9253672014-07-31 03:35:381236bool Browser::CanDragEnter(content::WebContents* source,
1237 const content::DropData& data,
1238 blink::WebDragOperationsMask operations_allowed) {
1239 // Disallow drag-and-drop navigation for Settings windows which do not support
1240 // external navigation.
1241 if ((operations_allowed & blink::WebDragOperationLink) &&
1242 chrome::SettingsWindowManager::GetInstance()->IsSettingsBrowser(this)) {
1243 return false;
1244 }
1245 return true;
1246}
1247
[email protected]d013f142012-04-27 17:31:141248bool Browser::IsMouseLocked() const {
1249 return fullscreen_controller_->IsMouseLocked();
[email protected]92655362012-04-20 21:33:171250}
1251
1252void Browser::OnWindowDidShow() {
1253 if (window_has_shown_)
1254 return;
1255 window_has_shown_ = true;
1256
[email protected]36e8fd42013-08-08 17:24:181257// CurrentProcessInfo::CreationTime() is missing on some platforms.
1258#if defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_LINUX)
[email protected]b5febf72012-07-29 18:10:091259 // Measure the latency from startup till the first browser window becomes
1260 // visible.
1261 static bool is_first_browser_window = true;
1262 if (is_first_browser_window &&
1263 !startup_metric_utils::WasNonBrowserUIDisplayed()) {
1264 is_first_browser_window = false;
[email protected]36e8fd42013-08-08 17:24:181265 const base::Time process_creation_time =
[email protected]b5febf72012-07-29 18:10:091266 base::CurrentProcessInfo::CreationTime();
1267
[email protected]36e8fd42013-08-08 17:24:181268 if (!process_creation_time.is_null()) {
[email protected]b5febf72012-07-29 18:10:091269 UMA_HISTOGRAM_LONG_TIMES(
1270 "Startup.BrowserWindowDisplay",
[email protected]36e8fd42013-08-08 17:24:181271 base::Time::Now() - process_creation_time);
[email protected]b5febf72012-07-29 18:10:091272 }
1273 }
[email protected]36e8fd42013-08-08 17:24:181274#endif // defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_LINUX)
[email protected]b5febf72012-07-29 18:10:091275
[email protected]92655362012-04-20 21:33:171276 // Nothing to do for non-tabbed windows.
1277 if (!is_type_tabbed())
1278 return;
1279
1280 // Show any pending global error bubble.
1281 GlobalErrorService* service =
1282 GlobalErrorServiceFactory::GetForProfile(profile());
1283 GlobalError* error = service->GetFirstGlobalErrorWithBubbleView();
1284 if (error)
1285 error->ShowBubbleView(this);
1286}
1287
1288void Browser::ShowFirstRunBubble() {
1289 window()->GetLocationBar()->ShowFirstRunBubble();
1290}
1291
[email protected]b3756012013-03-06 17:43:021292void Browser::ShowDownload(content::DownloadItem* download) {
1293 if (!window())
1294 return;
1295
1296 // If the download occurs in a new tab, and it's not a save page
1297 // download (started before initial navigation completed) close it.
1298 WebContents* source = download->GetWebContents();
1299 if (source && source->GetController().IsInitialNavigation() &&
1300 tab_strip_model_->count() > 1 && !download->IsSavePackageDownload()) {
1301 CloseContents(source);
1302 }
1303
1304 // Some (app downloads) are not supposed to appear on the shelf.
1305 if (!DownloadItemModel(download).ShouldShowInShelf())
1306 return;
1307
1308 // GetDownloadShelf creates the download shelf if it was not yet created.
1309 DownloadShelf* shelf = window()->GetDownloadShelf();
1310 shelf->AddDownload(download);
1311}
1312
[email protected]92655362012-04-20 21:33:171313///////////////////////////////////////////////////////////////////////////////
[email protected]674bc592011-12-20 23:00:421314// Browser, content::WebContentsDelegate implementation:
initial.commit09911bf2008-07-26 23:55:291315
[email protected]e5d549d2011-12-28 01:29:201316WebContents* Browser::OpenURLFromTab(WebContents* source,
[email protected]00c37fc2011-08-02 00:22:501317 const OpenURLParams& params) {
[email protected]78e2edc2012-07-01 23:32:281318 chrome::NavigateParams nav_params(this, params.url, params.transition);
[email protected]19c64142013-03-06 20:08:531319 FillNavigateParamsFromOpenURLParams(&nav_params, params);
[email protected]e232c992012-12-06 12:43:201320 nav_params.source_contents = source;
[email protected]00c37fc2011-08-02 00:22:501321 nav_params.tabstrip_add_types = TabStripModel::ADD_NONE;
[email protected]dee1b5a02014-04-22 11:21:561322 if (params.user_gesture)
1323 nav_params.window_action = chrome::NavigateParams::SHOW_WINDOW;
[email protected]e7f2e7c2013-07-15 09:41:301324 nav_params.user_gesture = params.user_gesture;
1325
[email protected]5635e6142013-07-18 08:10:401326 PopupBlockerTabHelper* popup_blocker_helper = NULL;
[email protected]e7f2e7c2013-07-15 09:41:301327 if (source)
[email protected]5635e6142013-07-18 08:10:401328 popup_blocker_helper = PopupBlockerTabHelper::FromWebContents(source);
[email protected]e7f2e7c2013-07-15 09:41:301329
[email protected]992e2912013-08-19 15:05:241330 if (popup_blocker_helper) {
[email protected]e7f2e7c2013-07-15 09:41:301331 if ((params.disposition == NEW_POPUP ||
1332 params.disposition == NEW_FOREGROUND_TAB ||
[email protected]7f53dff2013-08-05 10:45:501333 params.disposition == NEW_BACKGROUND_TAB ||
1334 params.disposition == NEW_WINDOW) &&
[email protected]e7f2e7c2013-07-15 09:41:301335 !params.user_gesture && !CommandLine::ForCurrentProcess()->HasSwitch(
1336 switches::kDisablePopupBlocking)) {
[email protected]67264062013-07-25 12:39:261337 if (popup_blocker_helper->MaybeBlockPopup(nav_params,
1338 WebWindowFeatures())) {
[email protected]5635e6142013-07-18 08:10:401339 return NULL;
[email protected]67264062013-07-25 12:39:261340 }
[email protected]e7f2e7c2013-07-15 09:41:301341 }
1342 }
1343
[email protected]78e2edc2012-07-01 23:32:281344 chrome::Navigate(&nav_params);
[email protected]00c37fc2011-08-02 00:22:501345
[email protected]e232c992012-12-06 12:43:201346 return nav_params.target_contents;
initial.commit09911bf2008-07-26 23:55:291347}
1348
[email protected]2a6bc3e2011-12-28 23:51:331349void Browser::NavigationStateChanged(const WebContents* source,
[email protected]7f924832014-08-09 05:57:221350 content::InvalidateTypes changed_flags) {
initial.commit09911bf2008-07-26 23:55:291351 // Only update the UI when something visible has changed.
[email protected]e83f1682008-09-07 23:57:401352 if (changed_flags)
initial.commit09911bf2008-07-26 23:55:291353 ScheduleUIUpdate(source, changed_flags);
1354
[email protected]83c2e232011-10-07 21:36:461355 // We can synchronously update commands since they will only change once per
1356 // navigation, so we don't have to worry about flickering. We do, however,
1357 // need to update the command state early on load to always present usable
1358 // actions in the face of slow-to-commit pages.
[email protected]d9083482012-01-06 00:38:461359 if (changed_flags & (content::INVALIDATE_TYPE_URL |
1360 content::INVALIDATE_TYPE_LOAD))
[email protected]5d98294912012-06-27 22:57:401361 command_controller_->TabStateChanged();
initial.commit09911bf2008-07-26 23:55:291362}
1363
[email protected]25af6ec2014-06-06 19:35:071364void Browser::VisibleSSLStateChanged(const WebContents* source) {
1365 // When the current tab's SSL state changes, we need to update the URL
1366 // bar to reflect the new state.
1367 DCHECK(source);
1368 if (tab_strip_model_->GetActiveWebContents() == source)
1369 UpdateToolbar(false);
1370}
1371
[email protected]2a6bc3e2011-12-28 23:51:331372void Browser::AddNewContents(WebContents* source,
1373 WebContents* new_contents,
initial.commit09911bf2008-07-26 23:55:291374 WindowOpenDisposition disposition,
1375 const gfx::Rect& initial_pos,
[email protected]eda238a12012-09-07 23:44:001376 bool user_gesture,
1377 bool* was_blocked) {
[email protected]52877dbc62012-06-29 22:22:031378 chrome::AddWebContents(this, source, new_contents, disposition, initial_pos,
[email protected]eda238a12012-09-07 23:44:001379 user_gesture, was_blocked);
initial.commit09911bf2008-07-26 23:55:291380}
1381
[email protected]2a6bc3e2011-12-28 23:51:331382void Browser::ActivateContents(WebContents* contents) {
[email protected]0d56fa7212012-11-15 23:34:021383 tab_strip_model_->ActivateTabAt(
1384 tab_strip_model_->GetIndexOfWebContents(contents), false);
[email protected]f3e99e32008-07-30 04:48:391385 window_->Activate();
initial.commit09911bf2008-07-26 23:55:291386}
1387
[email protected]2a6bc3e2011-12-28 23:51:331388void Browser::DeactivateContents(WebContents* contents) {
[email protected]ea42e7782010-08-23 23:58:121389 window_->Deactivate();
1390}
1391
[email protected]e3b10d12014-03-28 16:06:091392void Browser::LoadingStateChanged(WebContents* source,
1393 bool to_different_document) {
[email protected]2fc15ae2012-05-06 00:01:371394 window_->UpdateLoadingAnimations(tab_strip_model_->TabsAreLoading());
[email protected]f3e99e32008-07-30 04:48:391395 window_->UpdateTitleBar();
initial.commit09911bf2008-07-26 23:55:291396
[email protected]0d56fa7212012-11-15 23:34:021397 WebContents* selected_contents = tab_strip_model_->GetActiveWebContents();
[email protected]3c9e1872010-11-18 16:17:491398 if (source == selected_contents) {
[email protected]e3b10d12014-03-28 16:06:091399 bool is_loading = source->IsLoading() && to_different_document;
[email protected]5d98294912012-06-27 22:57:401400 command_controller_->LoadingStateChanged(is_loading, false);
[email protected]43d259d2010-09-03 21:37:461401 if (GetStatusBubble()) {
[email protected]0d56fa7212012-11-15 23:34:021402 GetStatusBubble()->SetStatus(CoreTabHelper::FromWebContents(
1403 tab_strip_model_->GetActiveWebContents())->GetStatusText());
[email protected]43d259d2010-09-03 21:37:461404 }
initial.commit09911bf2008-07-26 23:55:291405 }
1406}
1407
[email protected]a81343d232011-12-27 07:39:201408void Browser::CloseContents(WebContents* source) {
[email protected]88c92012013-07-02 11:56:341409 bool can_close_contents;
1410 if (IsFastTabUnloadEnabled())
1411 can_close_contents = fast_unload_controller_->CanCloseContents(source);
1412 else
1413 can_close_contents = unload_controller_->CanCloseContents(source);
1414
1415 if (can_close_contents)
[email protected]81ddd3402012-12-28 00:55:261416 chrome::CloseWebContents(this, source, true);
initial.commit09911bf2008-07-26 23:55:291417}
1418
[email protected]2a6bc3e2011-12-28 23:51:331419void Browser::MoveContents(WebContents* source, const gfx::Rect& pos) {
[email protected]b35b26b32011-05-05 20:35:141420 if (!IsPopupOrPanel(source)) {
initial.commit09911bf2008-07-26 23:55:291421 NOTREACHED() << "moving invalid browser type";
1422 return;
1423 }
[email protected]e561e202010-03-24 17:57:121424 window_->SetBounds(pos);
initial.commit09911bf2008-07-26 23:55:291425}
1426
[email protected]2a6bc3e2011-12-28 23:51:331427bool Browser::IsPopupOrPanel(const WebContents* source) const {
[email protected]7efaed362013-04-04 09:33:321428 return is_type_popup();
initial.commit09911bf2008-07-26 23:55:291429}
1430
[email protected]2a6bc3e2011-12-28 23:51:331431void Browser::UpdateTargetURL(WebContents* source, int32 page_id,
[email protected]d952a052011-09-06 18:42:451432 const GURL& url) {
[email protected]be3877f2009-01-14 15:51:101433 if (!GetStatusBubble())
1434 return;
1435
[email protected]0d56fa7212012-11-15 23:34:021436 if (source == tab_strip_model_->GetActiveWebContents()) {
[email protected]36b6dcb2008-11-12 01:19:571437 PrefService* prefs = profile_->GetPrefs();
[email protected]65233052011-08-22 19:02:481438 GetStatusBubble()->SetURL(url, prefs->GetString(prefs::kAcceptLanguages));
[email protected]36b6dcb2008-11-12 01:19:571439 }
1440}
1441
[email protected]92655362012-04-20 21:33:171442void Browser::ContentsMouseEvent(
1443 WebContents* source, const gfx::Point& location, bool motion) {
1444 if (!GetStatusBubble())
1445 return;
1446
[email protected]0d56fa7212012-11-15 23:34:021447 if (source == tab_strip_model_->GetActiveWebContents()) {
[email protected]92655362012-04-20 21:33:171448 GetStatusBubble()->MouseMoved(location, !motion);
1449 if (!motion)
1450 GetStatusBubble()->SetURL(GURL(), std::string());
1451 }
[email protected]3a6a3b62009-05-27 21:36:201452}
1453
[email protected]36b6dcb2008-11-12 01:19:571454void Browser::ContentsZoomChange(bool zoom_in) {
[email protected]5d98294912012-06-27 22:57:401455 chrome::ExecuteCommand(this, zoom_in ? IDC_ZOOM_PLUS : IDC_ZOOM_MINUS);
[email protected]36b6dcb2008-11-12 01:19:571456}
1457
[email protected]2a6bc3e2011-12-28 23:51:331458void Browser::WebContentsFocused(WebContents* contents) {
1459 window_->WebContentsFocused(contents);
[email protected]7e383692009-06-12 19:14:541460}
1461
[email protected]b3996ba2012-08-08 00:39:131462bool Browser::TakeFocus(content::WebContents* source,
1463 bool reverse) {
[email protected]ad50def52011-10-19 23:17:071464 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271465 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:531466 content::Source<Browser>(this),
[email protected]ad50def52011-10-19 23:17:071467 content::NotificationService::NoDetails());
[email protected]130efb02009-09-18 18:54:351468 return false;
1469}
1470
[email protected]b7a756d42012-01-23 18:08:171471gfx::Rect Browser::GetRootWindowResizerRect() const {
1472 return window_->GetRootWindowResizerRect();
1473}
1474
[email protected]1d3e9172012-06-12 02:59:131475void Browser::BeforeUnloadFired(WebContents* web_contents,
[email protected]36b6dcb2008-11-12 01:19:571476 bool proceed,
1477 bool* proceed_to_fire_unload) {
[email protected]90354712013-11-16 00:06:301478 if (is_devtools() && DevToolsWindow::HandleBeforeUnload(web_contents,
1479 proceed, proceed_to_fire_unload))
1480 return;
1481
[email protected]88c92012013-07-02 11:56:341482 if (IsFastTabUnloadEnabled()) {
1483 *proceed_to_fire_unload =
1484 fast_unload_controller_->BeforeUnloadFired(web_contents, proceed);
1485 } else {
1486 *proceed_to_fire_unload =
1487 unload_controller_->BeforeUnloadFired(web_contents, proceed);
1488 }
[email protected]36b6dcb2008-11-12 01:19:571489}
1490
[email protected]0c9406632013-02-08 01:13:331491bool Browser::ShouldFocusLocationBarByDefault(WebContents* source) {
[email protected]77fd82e52013-03-12 22:24:261492 const content::NavigationEntry* entry =
[email protected]06cb23b2013-12-17 02:56:491493 source->GetController().GetActiveEntry();
[email protected]e4916412013-04-25 20:12:151494 if (entry) {
1495 GURL url = entry->GetURL();
1496 GURL virtual_url = entry->GetVirtualURL();
[email protected]2d9748b22014-02-11 00:17:291497 if ((url.SchemeIs(content::kChromeUIScheme) &&
[email protected]e4916412013-04-25 20:12:151498 url.host() == chrome::kChromeUINewTabHost) ||
[email protected]2d9748b22014-02-11 00:17:291499 (virtual_url.SchemeIs(content::kChromeUIScheme) &&
[email protected]e4916412013-04-25 20:12:151500 virtual_url.host() == chrome::kChromeUINewTabHost)) {
1501 return true;
1502 }
[email protected]c744f7d22013-04-12 20:29:041503 }
1504
[email protected]165fe422013-03-27 06:34:031505 return chrome::NavEntryIsInstantNTP(source, entry);
[email protected]0c9406632013-02-08 01:13:331506}
1507
[email protected]a26dc362010-04-23 01:48:581508void Browser::SetFocusToLocationBar(bool select_all) {
[email protected]1a380622009-02-17 17:33:131509 // Two differences between this and FocusLocationBar():
1510 // (1) This doesn't get recorded in user metrics, since it's called
1511 // internally.
1512 // (2) This checks whether the location bar can be focused, and if not, clears
1513 // the focus. FocusLocationBar() is only reached when the location bar is
1514 // focusable, but this may be reached at other times, e.g. while in
1515 // fullscreen mode, where we need to leave focus in a consistent state.
[email protected]a26dc362010-04-23 01:48:581516 window_->SetFocusToLocationBar(select_all);
[email protected]4bcefee2009-01-29 15:57:571517}
1518
[email protected]0a2aeb82009-05-15 21:52:481519int Browser::GetExtraRenderViewHeight() const {
1520 return window_->GetExtraRenderViewHeight();
1521}
1522
[email protected]2a6bc3e2011-12-28 23:51:331523void Browser::ViewSourceForTab(WebContents* source, const GURL& page_url) {
[email protected]1788e772010-12-15 16:40:501524 DCHECK(source);
[email protected]ab93b6372012-11-28 05:20:221525 chrome::ViewSource(this, source);
[email protected]77d8d622010-12-15 10:30:121526}
1527
[email protected]2a6bc3e2011-12-28 23:51:331528void Browser::ViewSourceForFrame(WebContents* source,
[email protected]932b7a12011-03-09 12:50:271529 const GURL& frame_url,
[email protected]691aa2f2013-05-28 22:52:041530 const content::PageState& frame_page_state) {
[email protected]932b7a12011-03-09 12:50:271531 DCHECK(source);
[email protected]691aa2f2013-05-28 22:52:041532 chrome::ViewSource(this, source, frame_url, frame_page_state);
[email protected]932b7a12011-03-09 12:50:271533}
1534
[email protected]4fdf6742012-01-10 20:14:361535void Browser::ShowRepostFormWarningDialog(WebContents* source) {
[email protected]51296c62012-09-26 13:02:441536 TabModalConfirmDialog::Create(new RepostFormWarningController(source),
[email protected]70320ab2012-11-02 19:07:161537 source);
[email protected]14f3408a2009-08-31 20:53:531538}
1539
[email protected]4fdf6742012-01-10 20:14:361540bool Browser::ShouldCreateWebContents(
1541 WebContents* web_contents,
1542 int route_id,
1543 WindowContainerType window_container_type,
[email protected]dcd0249872013-12-06 23:58:451544 const base::string16& frame_name,
[email protected]4543b67c2013-07-11 18:18:361545 const GURL& target_url,
[email protected]dd6730412013-08-14 15:03:371546 const std::string& partition_id,
1547 content::SessionStorageNamespace* session_storage_namespace) {
[email protected]4fdf6742012-01-10 20:14:361548 if (window_container_type == WINDOW_CONTAINER_TYPE_BACKGROUND) {
1549 // If a BackgroundContents is created, suppress the normal WebContents.
[email protected]dd6730412013-08-14 15:03:371550 return !MaybeCreateBackgroundContents(route_id,
1551 web_contents,
1552 frame_name,
1553 target_url,
1554 partition_id,
1555 session_storage_namespace);
[email protected]4fdf6742012-01-10 20:14:361556 }
1557
1558 return true;
1559}
1560
1561void Browser::WebContentsCreated(WebContents* source_contents,
[email protected]a7531d772014-03-25 16:15:071562 int opener_render_frame_id,
[email protected]dcd0249872013-12-06 23:58:451563 const base::string16& frame_name,
[email protected]4fdf6742012-01-10 20:14:361564 const GURL& target_url,
1565 WebContents* new_contents) {
[email protected]6451fe92012-08-30 18:37:261566 // Adopt the WebContents now, so all observers are in place, as the network
[email protected]3d61dd02012-06-07 00:05:531567 // requests for its initial navigation will start immediately. The WebContents
1568 // will later be inserted into this browser using Browser::Navigate via
[email protected]6451fe92012-08-30 18:37:261569 // AddNewContents.
[email protected]e0bc75d52014-01-30 23:42:591570 TabHelpers::AttachTabHelpers(new_contents);
[email protected]4fdf6742012-01-10 20:14:361571
1572 // Notify.
1573 RetargetingDetails details;
1574 details.source_web_contents = source_contents;
[email protected]a7531d772014-03-25 16:15:071575 details.source_render_frame_id = opener_render_frame_id;
[email protected]4fdf6742012-01-10 20:14:361576 details.target_url = target_url;
1577 details.target_web_contents = new_contents;
1578 details.not_yet_in_tabstrip = true;
1579 content::NotificationService::current()->Notify(
1580 chrome::NOTIFICATION_RETARGETING,
1581 content::Source<Profile>(profile_),
1582 content::Details<RetargetingDetails>(&details));
[email protected]80c7b802011-10-28 19:59:081583}
1584
[email protected]2a6bc3e2011-12-28 23:51:331585void Browser::RendererUnresponsive(WebContents* source) {
[email protected]a7ed0ca2012-12-06 19:30:061586 // Ignore hangs if a tab is blocked.
1587 int index = tab_strip_model_->GetIndexOfWebContents(source);
1588 DCHECK_NE(TabStripModel::kNoTab, index);
1589 if (tab_strip_model_->IsTabBlocked(index))
1590 return;
[email protected]c19d9542011-11-15 22:26:421591
[email protected]87586fb72012-07-02 13:15:021592 chrome::ShowHungRendererDialog(source);
[email protected]55452902011-06-01 21:57:471593}
1594
[email protected]2a6bc3e2011-12-28 23:51:331595void Browser::RendererResponsive(WebContents* source) {
[email protected]87586fb72012-07-02 13:15:021596 chrome::HideHungRendererDialog(source);
[email protected]55452902011-06-01 21:57:471597}
1598
[email protected]2a6bc3e2011-12-28 23:51:331599void Browser::WorkerCrashed(WebContents* source) {
[email protected]0be09932013-01-08 02:03:501600 SimpleAlertInfoBarDelegate::Create(
[email protected]051655ad2014-04-18 15:09:411601 InfoBarService::FromWebContents(source),
1602 infobars::InfoBarDelegate::kNoIconID,
[email protected]0be09932013-01-08 02:03:501603 l10n_util::GetStringUTF16(IDS_WEBWORKER_CRASHED_PROMPT), true);
[email protected]0dd3a0ab2011-02-18 08:17:441604}
1605
[email protected]1d3e9172012-06-12 02:59:131606void Browser::DidNavigateMainFramePostCommit(WebContents* web_contents) {
[email protected]0d56fa7212012-11-15 23:34:021607 if (web_contents == tab_strip_model_->GetActiveWebContents())
[email protected]09b29342011-06-24 19:18:481608 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]93f230e02011-06-01 14:40:001609}
1610
[email protected]1d3e9172012-06-12 02:59:131611void Browser::DidNavigateToPendingEntry(WebContents* web_contents) {
[email protected]0d56fa7212012-11-15 23:34:021612 if (web_contents == tab_strip_model_->GetActiveWebContents())
[email protected]09b29342011-06-24 19:18:481613 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]93f230e02011-06-01 14:40:001614}
1615
[email protected]71a88bb2013-02-01 22:05:151616content::JavaScriptDialogManager* Browser::GetJavaScriptDialogManager() {
1617 return GetJavaScriptDialogManagerInstance();
[email protected]3ab9cb82011-06-03 18:02:071618}
1619
[email protected]8bc5ff02013-11-29 06:34:031620content::ColorChooser* Browser::OpenColorChooser(
1621 WebContents* web_contents,
1622 SkColor initial_color,
1623 const std::vector<content::ColorSuggestion>& suggestions) {
[email protected]cb805452013-05-22 15:16:211624 return chrome::ShowColorChooser(web_contents, initial_color);
[email protected]da8543762012-03-20 08:52:201625}
1626
[email protected]1d3e9172012-06-12 02:59:131627void Browser::RunFileChooser(WebContents* web_contents,
[email protected]8caadeb2011-11-22 02:45:231628 const content::FileChooserParams& params) {
[email protected]1d3e9172012-06-12 02:59:131629 FileSelectHelper::RunFileChooser(web_contents, params);
[email protected]3a29a6e2011-08-24 18:26:211630}
1631
[email protected]1d3e9172012-06-12 02:59:131632void Browser::EnumerateDirectory(WebContents* web_contents,
[email protected]33f8ad52012-05-22 18:10:131633 int request_id,
[email protected]650b2d52013-02-10 03:41:451634 const base::FilePath& path) {
[email protected]1d3e9172012-06-12 02:59:131635 FileSelectHelper::EnumerateDirectory(web_contents, request_id, path);
[email protected]3a29a6e2011-08-24 18:26:211636}
1637
[email protected]4aebbcaa2013-09-17 22:26:491638bool Browser::EmbedsFullscreenWidget() const {
[email protected]40f12b92014-07-15 19:56:111639 return true;
[email protected]4aebbcaa2013-09-17 22:26:491640}
1641
[email protected]1d3e9172012-06-12 02:59:131642void Browser::ToggleFullscreenModeForTab(WebContents* web_contents,
[email protected]01a5a762011-11-21 05:40:551643 bool enter_fullscreen) {
[email protected]1d3e9172012-06-12 02:59:131644 fullscreen_controller_->ToggleFullscreenModeForTab(web_contents,
1645 enter_fullscreen);
[email protected]8a5e0ca2011-08-25 06:30:471646}
1647
[email protected]1d3e9172012-06-12 02:59:131648bool Browser::IsFullscreenForTabOrPending(
1649 const WebContents* web_contents) const {
1650 return fullscreen_controller_->IsFullscreenForTabOrPending(web_contents);
[email protected]5d5f7af2011-10-01 01:38:121651}
1652
[email protected]1d3e9172012-06-12 02:59:131653void Browser::RegisterProtocolHandler(WebContents* web_contents,
[email protected]7d189022011-08-25 22:54:201654 const std::string& protocol,
1655 const GURL& url,
[email protected]3a3b75a2012-06-01 08:38:361656 bool user_gesture) {
[email protected]a23abe472014-07-29 23:46:311657 content::BrowserContext* context = web_contents->GetBrowserContext();
1658 if (context->IsOffTheRecord())
[email protected]e1ff4a42013-12-16 20:29:011659 return;
1660
1661 ProtocolHandler handler =
[email protected]c125e532014-05-10 20:39:061662 ProtocolHandler::CreateProtocolHandler(protocol, url);
[email protected]e1ff4a42013-12-16 20:29:011663
1664 ProtocolHandlerRegistry* registry =
[email protected]a23abe472014-07-29 23:46:311665 ProtocolHandlerRegistryFactory::GetForBrowserContext(context);
[email protected]e1ff4a42013-12-16 20:29:011666 if (registry->SilentlyHandleRegisterHandlerRequest(handler))
1667 return;
1668
1669 TabSpecificContentSettings* tab_content_settings =
1670 TabSpecificContentSettings::FromWebContents(web_contents);
1671 if (!user_gesture && window_) {
1672 tab_content_settings->set_pending_protocol_handler(handler);
1673 tab_content_settings->set_previous_protocol_handler(
1674 registry->GetHandlerFor(handler.protocol()));
1675 window_->GetLocationBar()->UpdateContentSettingsIcons();
1676 return;
1677 }
1678
1679 // Make sure content-setting icon is turned off in case the page does
1680 // ungestured and gestured RPH calls.
1681 if (window_) {
1682 tab_content_settings->ClearPendingProtocolHandler();
1683 window_->GetLocationBar()->UpdateContentSettingsIcons();
1684 }
1685
[email protected]f1d20572014-02-14 01:27:141686 PermissionBubbleManager* bubble_manager =
1687 PermissionBubbleManager::FromWebContents(web_contents);
1688 if (PermissionBubbleManager::Enabled() && bubble_manager) {
1689 bubble_manager->AddRequest(
[email protected]d23cdeee2014-03-10 06:39:531690 new RegisterProtocolHandlerPermissionRequest(registry, handler,
1691 url, user_gesture));
[email protected]f1d20572014-02-14 01:27:141692 } else {
1693 RegisterProtocolHandlerInfoBarDelegate::Create(
1694 InfoBarService::FromWebContents(web_contents), registry, handler);
1695 }
[email protected]7d189022011-08-25 22:54:201696}
1697
[email protected]f5273e52014-07-14 16:30:201698void Browser::UnregisterProtocolHandler(WebContents* web_contents,
1699 const std::string& protocol,
1700 const GURL& url,
1701 bool user_gesture) {
1702 // user_gesture will be used in case we decide to have confirmation bubble
1703 // for user while un-registering the handler.
[email protected]a23abe472014-07-29 23:46:311704 content::BrowserContext* context = web_contents->GetBrowserContext();
1705 if (context->IsOffTheRecord())
[email protected]f5273e52014-07-14 16:30:201706 return;
1707
1708 ProtocolHandler handler =
1709 ProtocolHandler::CreateProtocolHandler(protocol, url);
1710
1711 ProtocolHandlerRegistry* registry =
[email protected]a23abe472014-07-29 23:46:311712 ProtocolHandlerRegistryFactory::GetForBrowserContext(context);
[email protected]f5273e52014-07-14 16:30:201713 registry->RemoveHandler(handler);
1714}
1715
[email protected]2a6bc3e2011-12-28 23:51:331716void Browser::UpdatePreferredSize(WebContents* source,
[email protected]0548c5352011-09-07 00:33:331717 const gfx::Size& pref_size) {
1718 window_->UpdatePreferredSize(source, pref_size);
1719}
1720
[email protected]61e2b3cc2012-03-02 16:13:341721void Browser::ResizeDueToAutoResize(WebContents* source,
1722 const gfx::Size& new_size) {
1723 window_->ResizeDueToAutoResize(source, new_size);
1724}
1725
[email protected]1d3e9172012-06-12 02:59:131726void Browser::FindReply(WebContents* web_contents,
[email protected]92655362012-04-20 21:33:171727 int request_id,
1728 int number_of_matches,
1729 const gfx::Rect& selection_rect,
1730 int active_match_ordinal,
1731 bool final_update) {
[email protected]e1ff4a42013-12-16 20:29:011732 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(web_contents);
1733 if (!find_tab_helper)
1734 return;
1735
1736 find_tab_helper->HandleFindReply(request_id,
1737 number_of_matches,
1738 selection_rect,
1739 active_match_ordinal,
1740 final_update);
[email protected]92655362012-04-20 21:33:171741}
1742
[email protected]1d3e9172012-06-12 02:59:131743void Browser::RequestToLockMouse(WebContents* web_contents,
[email protected]a9c81f02012-06-01 00:15:441744 bool user_gesture,
1745 bool last_unlocked_by_target) {
[email protected]1d3e9172012-06-12 02:59:131746 fullscreen_controller_->RequestToLockMouse(web_contents,
[email protected]a9c81f02012-06-01 00:15:441747 user_gesture,
1748 last_unlocked_by_target);
[email protected]55c87fa2011-10-15 07:28:281749}
1750
[email protected]5ef8e372011-10-18 04:25:551751void Browser::LostMouseLock() {
[email protected]01a5a762011-11-21 05:40:551752 fullscreen_controller_->LostMouseLock();
[email protected]5ef8e372011-10-18 04:25:551753}
1754
[email protected]f13b4202012-06-12 23:53:231755void Browser::RequestMediaAccessPermission(
1756 content::WebContents* web_contents,
[email protected]33662e52013-01-07 21:31:091757 const content::MediaStreamRequest& request,
[email protected]f13b4202012-06-12 23:53:231758 const content::MediaResponseCallback& callback) {
[email protected]859ec322013-09-13 16:24:561759 ::RequestMediaAccessPermission(web_contents, profile_, request, callback);
[email protected]f13b4202012-06-12 23:53:231760}
1761
grunell657d4d82014-09-18 00:09:431762bool Browser::CheckMediaAccessPermission(content::WebContents* web_contents,
1763 const GURL& security_origin,
1764 content::MediaStreamType type) {
1765 return ::CheckMediaAccessPermission(web_contents, security_origin, type);
1766}
1767
[email protected]8c8fc292012-11-23 18:57:161768bool Browser::RequestPpapiBrokerPermission(
1769 WebContents* web_contents,
1770 const GURL& url,
[email protected]650b2d52013-02-10 03:41:451771 const base::FilePath& plugin_path,
[email protected]8c8fc292012-11-23 18:57:161772 const base::Callback<void(bool)>& callback) {
[email protected]0be09932013-01-08 02:03:501773 PepperBrokerInfoBarDelegate::Create(web_contents, url, plugin_path, callback);
[email protected]8c8fc292012-11-23 18:57:161774 return true;
1775}
1776
[email protected]fc2b46b2014-05-03 16:33:451777gfx::Size Browser::GetSizeForNewRenderView(WebContents* web_contents) const {
[email protected]dc0fd432013-08-27 15:29:211778 // When navigating away from NTP with unpinned bookmark bar, the bookmark bar
1779 // would disappear on non-NTP pages, resulting in a bigger size for the new
1780 // render view.
[email protected]fc2b46b2014-05-03 16:33:451781 gfx::Size size = web_contents->GetContainerBounds().size();
[email protected]dc0fd432013-08-27 15:29:211782 // Don't change render view size if bookmark bar is currently not detached,
1783 // or there's no pending entry, or navigating to a NTP page.
1784 if (size.IsEmpty() || bookmark_bar_state_ != BookmarkBar::DETACHED)
1785 return size;
1786 const NavigationEntry* pending_entry =
1787 web_contents->GetController().GetPendingEntry();
1788 if (pending_entry &&
1789 !chrome::IsNTPURL(pending_entry->GetVirtualURL(), profile_)) {
1790 size.Enlarge(
1791 0, window()->GetRenderViewHeightInsetWithDetachedBookmarkBar());
1792 }
1793 return size;
1794}
1795
[email protected]36b6dcb2008-11-12 01:19:571796///////////////////////////////////////////////////////////////////////////////
[email protected]51d83bdf2011-12-09 16:09:291797// Browser, CoreTabHelperDelegate implementation:
[email protected]a0366a52011-02-04 20:04:211798
[email protected]ece4aca2012-09-19 22:22:581799void Browser::SwapTabContents(content::WebContents* old_contents,
[email protected]9aa1f6282014-01-30 08:47:411800 content::WebContents* new_contents,
1801 bool did_start_load,
1802 bool did_finish_load) {
[email protected]ece4aca2012-09-19 22:22:581803 int index = tab_strip_model_->GetIndexOfWebContents(old_contents);
[email protected]92655362012-04-20 21:33:171804 DCHECK_NE(TabStripModel::kNoTab, index);
[email protected]59fa29fb2012-12-07 22:13:421805 tab_strip_model_->ReplaceWebContentsAt(index, new_contents);
[email protected]92655362012-04-20 21:33:171806}
1807
[email protected]ece4aca2012-09-19 22:22:581808bool Browser::CanReloadContents(content::WebContents* web_contents) const {
[email protected]5d98294912012-06-27 22:57:401809 return chrome::CanReload(this);
[email protected]e1461952012-06-14 21:05:351810}
1811
[email protected]ece4aca2012-09-19 22:22:581812bool Browser::CanSaveContents(content::WebContents* web_contents) const {
[email protected]5d98294912012-06-27 22:57:401813 return chrome::CanSavePage(this);
[email protected]e1461952012-06-14 21:05:351814}
1815
[email protected]92655362012-04-20 21:33:171816///////////////////////////////////////////////////////////////////////////////
1817// Browser, SearchEngineTabHelperDelegate implementation:
1818
[email protected]3613347d2012-04-27 20:27:371819void Browser::ConfirmAddSearchProvider(TemplateURL* template_url,
[email protected]92655362012-04-20 21:33:171820 Profile* profile) {
1821 window()->ConfirmAddSearchProvider(template_url, profile);
1822}
1823
1824///////////////////////////////////////////////////////////////////////////////
[email protected]82808d02014-04-10 00:10:591825// Browser, SearchTabHelperDelegate implementation:
1826
1827void Browser::NavigateOnThumbnailClick(const GURL& url,
1828 WindowOpenDisposition disposition,
1829 content::WebContents* source_contents) {
1830 DCHECK(source_contents);
1831 // We're guaranteed that AUTO_BOOKMARK is the right transition since this only
1832 // gets called to handle clicks in the new tab page (to navigate to most
1833 // visited item URLs) and in the search results page (to navigate to
1834 // privileged destinations (e.g. chrome://URLs)).
1835 //
1836 // TODO(kmadhusu): Page transitions to privileged destinations should be
1837 // marked as "LINK" instead of "AUTO_BOOKMARK"?
1838 chrome::NavigateParams params(this, url,
1839 content::PAGE_TRANSITION_AUTO_BOOKMARK);
1840 params.referrer = content::Referrer();
1841 params.source_contents = source_contents;
1842 params.disposition = disposition;
1843 params.is_renderer_initiated = false;
1844 params.initiating_profile = profile_;
1845 chrome::Navigate(&params);
1846}
1847
1848void Browser::OnWebContentsInstantSupportDisabled(
1849 const content::WebContents* web_contents) {
1850 DCHECK(web_contents);
1851 if (tab_strip_model_->GetActiveWebContents() == web_contents)
1852 UpdateToolbar(false);
1853}
1854
1855OmniboxView* Browser::GetOmniboxView() {
1856 return window_->GetLocationBar()->GetOmniboxView();
1857}
1858
1859std::set<std::string> Browser::GetOpenUrls() {
1860 history::TopSites* top_sites = profile_->GetTopSites();
1861 if (!top_sites) // NULL for Incognito profiles.
1862 return std::set<std::string>();
1863
1864 std::set<std::string> open_urls;
1865 chrome::GetOpenUrls(*tab_strip_model_, *top_sites, &open_urls);
1866 return open_urls;
1867}
1868
1869///////////////////////////////////////////////////////////////////////////////
[email protected]e41d0082013-05-16 04:37:541870// Browser, web_modal::WebContentsModalDialogManagerDelegate implementation:
[email protected]92655362012-04-20 21:33:171871
[email protected]c5731a1c2012-12-22 10:18:451872void Browser::SetWebContentsBlocked(content::WebContents* web_contents,
1873 bool blocked) {
[email protected]c6464ad2012-10-03 00:27:281874 int index = tab_strip_model_->GetIndexOfWebContents(web_contents);
[email protected]92655362012-04-20 21:33:171875 if (index == TabStripModel::kNoTab) {
1876 NOTREACHED();
1877 return;
1878 }
[email protected]2fc15ae2012-05-06 00:01:371879 tab_strip_model_->SetTabBlocked(index, blocked);
[email protected]0d56fa7212012-11-15 23:34:021880 if (!blocked && tab_strip_model_->GetActiveWebContents() == web_contents)
[email protected]fc2b46b2014-05-03 16:33:451881 web_contents->Focus();
[email protected]92655362012-04-20 21:33:171882}
1883
[email protected]e41d0082013-05-16 04:37:541884web_modal::WebContentsModalDialogHost*
1885Browser::GetWebContentsModalDialogHost() {
[email protected]7519c522013-04-02 01:07:231886 return window_->GetWebContentsModalDialogHost();
[email protected]6a151762012-10-03 22:33:191887}
1888
[email protected]92655362012-04-20 21:33:171889///////////////////////////////////////////////////////////////////////////////
[email protected]92655362012-04-20 21:33:171890// Browser, BookmarkTabHelperDelegate implementation:
1891
[email protected]1c5119c2012-09-19 00:08:571892void Browser::URLStarredChanged(content::WebContents* web_contents,
1893 bool starred) {
[email protected]0d56fa7212012-11-15 23:34:021894 if (web_contents == tab_strip_model_->GetActiveWebContents())
[email protected]92655362012-04-20 21:33:171895 window_->SetStarredState(starred);
1896}
1897
1898///////////////////////////////////////////////////////////////////////////////
[email protected]6f80e932012-06-04 19:00:071899// Browser, ZoomObserver implementation:
1900
[email protected]63d1f9b2014-07-05 19:09:031901void Browser::OnZoomChanged(const ZoomController::ZoomChangedEventData& data) {
1902 if (data.web_contents == tab_strip_model_->GetActiveWebContents()) {
[email protected]6f80e932012-06-04 19:00:071903 // Only show the zoom bubble for zoom changes in the active window.
[email protected]63d1f9b2014-07-05 19:09:031904 window_->ZoomChangedForActiveTab(data.can_show_bubble &&
[email protected]d27c65262014-07-10 02:51:541905 window_->IsActive());
[email protected]d93dbd12014-08-04 23:42:531906 // Change the zoom commands state based on the zoom state
1907 command_controller_->ZoomStateChanged();
[email protected]6f80e932012-06-04 19:00:071908 }
1909}
1910
1911///////////////////////////////////////////////////////////////////////////////
[email protected]92f54082012-07-31 01:43:141912// Browser, ui::SelectFileDialog::Listener implementation:
[email protected]36b6dcb2008-11-12 01:19:571913
[email protected]650b2d52013-02-10 03:41:451914void Browser::FileSelected(const base::FilePath& path, int index,
1915 void* params) {
[email protected]53f04c82012-07-26 02:31:091916 FileSelectedWithExtraInfo(ui::SelectedFileInfo(path, path), index, params);
1917}
1918
[email protected]a573bc32013-04-09 15:09:541919void Browser::FileSelectedWithExtraInfo(const ui::SelectedFileInfo& file_info,
1920 int index,
1921 void* params) {
[email protected]53f04c82012-07-26 02:31:091922 profile_->set_last_selected_directory(file_info.file_path.DirName());
1923
[email protected]a573bc32013-04-09 15:09:541924 GURL url = net::FilePathToFileURL(file_info.local_path);
[email protected]b7813a22012-04-04 18:41:021925
1926#if defined(OS_CHROMEOS)
[email protected]a573bc32013-04-09 15:09:541927 drive::util::MaybeSetDriveURL(profile_, file_info.file_path, &url);
[email protected]b7813a22012-04-04 18:41:021928#endif
1929
[email protected]a573bc32013-04-09 15:09:541930 if (url.is_empty())
[email protected]e5d549d2011-12-28 01:29:201931 return;
1932
1933 OpenURL(OpenURLParams(
[email protected]a573bc32013-04-09 15:09:541934 url, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
[email protected]36b6dcb2008-11-12 01:19:571935}
1936
1937///////////////////////////////////////////////////////////////////////////////
[email protected]6c2381d2011-10-19 02:52:531938// Browser, content::NotificationObserver implementation:
[email protected]36b6dcb2008-11-12 01:19:571939
[email protected]432115822011-07-10 15:52:271940void Browser::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:531941 const content::NotificationSource& source,
1942 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:271943 switch (type) {
[email protected]adf5a102014-07-31 12:44:061944 case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
[email protected]4db2e912014-03-04 07:08:581945 chrome::UpdateCommandEnabled(
1946 this,
1947 IDC_BOOKMARK_PAGE,
1948 !chrome::ShouldRemoveBookmarkThisPageUI(profile_));
[email protected]0db25656b2014-03-25 22:46:501949 chrome::UpdateCommandEnabled(
1950 this,
1951 IDC_BOOKMARK_ALL_TABS,
1952 !chrome::ShouldRemoveBookmarkOpenPagesUI(profile_));
[email protected]4db2e912014-03-04 07:08:581953
[email protected]ce696a452011-05-31 17:47:361954 if (window()->GetLocationBar())
1955 window()->GetLocationBar()->UpdatePageActions();
[email protected]57f71b92009-09-11 19:31:381956
[email protected]8a39b472013-04-04 17:07:421957 const extensions::UnloadedExtensionInfo* extension_info =
1958 content::Details<extensions::UnloadedExtensionInfo>(details).ptr();
1959
[email protected]b3f7fe22011-11-11 19:27:561960 // Close any tabs from the unloaded extension, unless it's terminated,
1961 // in which case let the sad tabs remain.
[email protected]b0af4792013-10-23 09:12:131962 if (extension_info->reason !=
1963 extensions::UnloadedExtensionInfo::REASON_TERMINATE) {
[email protected]8a39b472013-04-04 17:07:421964 const Extension* extension = extension_info->extension;
[email protected]e344b5a2012-11-27 07:04:351965 // Iterate backwards as we may remove items while iterating.
[email protected]2fc15ae2012-05-06 00:01:371966 for (int i = tab_strip_model_->count() - 1; i >= 0; --i) {
[email protected]0d56fa7212012-11-15 23:34:021967 WebContents* web_contents = tab_strip_model_->GetWebContentsAt(i);
[email protected]e344b5a2012-11-27 07:04:351968 // Two cases are handled here:
1969 // - The scheme check is for when an extension page is loaded in a
1970 // tab, e.g. chrome-extension://id/page.html.
1971 // - The extension_app check is for apps, which can have non-extension
1972 // schemes, e.g. https://mail.google.com if you have the Gmail app
1973 // installed.
1974 if ((web_contents->GetURL().SchemeIs(extensions::kExtensionScheme) &&
1975 web_contents->GetURL().host() == extension->id()) ||
1976 (extensions::TabHelper::FromWebContents(
1977 web_contents)->extension_app() == extension)) {
[email protected]526b45e2012-11-28 20:22:461978 tab_strip_model_->CloseWebContentsAt(i, TabStripModel::CLOSE_NONE);
[email protected]e344b5a2012-11-27 07:04:351979 }
[email protected]894bb502009-05-21 22:39:571980 }
1981 }
1982 break;
1983 }
1984
[email protected]adf5a102014-07-31 12:44:061985 case extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:531986 Profile* profile = content::Source<Profile>(source).ptr();
[email protected]fe8944f2011-08-30 17:47:321987 if (profile_->IsSameProfile(profile) && window()->GetLocationBar())
[email protected]ce696a452011-05-31 17:47:361988 window()->GetLocationBar()->InvalidatePageActions();
[email protected]371ed7a2009-08-25 15:22:461989 break;
1990 }
1991
[email protected]adf5a102014-07-31 12:44:061992 case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED:
[email protected]4db2e912014-03-04 07:08:581993 chrome::UpdateCommandEnabled(
1994 this,
1995 IDC_BOOKMARK_PAGE,
1996 !chrome::ShouldRemoveBookmarkThisPageUI(profile_));
[email protected]0db25656b2014-03-25 22:46:501997 chrome::UpdateCommandEnabled(
1998 this,
1999 IDC_BOOKMARK_ALL_TABS,
2000 !chrome::ShouldRemoveBookmarkOpenPagesUI(profile_));
[email protected]4db2e912014-03-04 07:08:582001 // fallthrough
[email protected]adf5a102014-07-31 12:44:062002 case extensions::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED:
[email protected]1797f4a2011-08-30 18:00:282003 // During window creation on Windows we may end up calling into
2004 // SHAppBarMessage, which internally spawns a nested message loop. This
2005 // makes it possible for us to end up here before window creation has
[email protected]8a39b472013-04-04 17:07:422006 // completed, at which point window_ is NULL. See 94752 for details.
[email protected]1797f4a2011-08-30 18:00:282007 if (window() && window()->GetLocationBar())
[email protected]ce696a452011-05-31 17:47:362008 window()->GetLocationBar()->UpdatePageActions();
[email protected]0dfe05c2011-02-23 23:03:362009 break;
[email protected]ad0c2e1b2010-01-30 00:00:102010
[email protected]6b40bb582012-03-15 20:50:382011#if defined(ENABLE_THEMES)
[email protected]432115822011-07-10 15:52:272012 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED:
[email protected]e001d412009-06-26 20:53:252013 window()->UserChangedTheme();
2014 break;
[email protected]6b40bb582012-03-15 20:50:382015#endif
[email protected]e001d412009-06-26 20:53:252016
[email protected]ea049a02011-12-25 21:37:092017 case chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED: {
2018 WebContents* web_contents = content::Source<WebContents>(source).ptr();
[email protected]0d56fa7212012-11-15 23:34:022019 if (web_contents == tab_strip_model_->GetActiveWebContents()) {
[email protected]9a9a9e22011-05-13 22:20:352020 LocationBar* location_bar = window()->GetLocationBar();
2021 if (location_bar)
2022 location_bar->UpdateContentSettingsIcons();
2023 }
2024 break;
2025 }
2026
[email protected]e83f1682008-09-07 23:57:402027 default:
2028 NOTREACHED() << "Got a notification we didn't register for.";
initial.commit09911bf2008-07-26 23:55:292029 }
2030}
2031
[email protected]d3b98c82010-07-14 07:45:592032///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:572033// Browser, Command and state updating (private):
2034
[email protected]c31813a2012-11-15 18:47:262035void Browser::OnDevToolsDisabledChanged() {
2036 if (profile_->GetPrefs()->GetBoolean(prefs::kDevToolsDisabled))
[email protected]b50452f2014-08-18 12:31:442037 content::DevToolsAgentHost::DetachAllClients();
[email protected]c31813a2012-11-15 18:47:262038}
2039
[email protected]36b6dcb2008-11-12 01:19:572040///////////////////////////////////////////////////////////////////////////////
2041// Browser, UI update coalescing and handling (private):
2042
2043void Browser::UpdateToolbar(bool should_restore_state) {
[email protected]2e7fe4312013-08-21 17:19:482044 window_->UpdateToolbar(should_restore_state ?
2045 tab_strip_model_->GetActiveWebContents() : NULL);
initial.commit09911bf2008-07-26 23:55:292046}
2047
[email protected]2a6bc3e2011-12-28 23:51:332048void Browser::ScheduleUIUpdate(const WebContents* source,
[email protected]36b6dcb2008-11-12 01:19:572049 unsigned changed_flags) {
[email protected]913664d2013-12-17 11:43:382050 DCHECK(source);
2051 int index = tab_strip_model_->GetIndexOfWebContents(source);
2052 DCHECK_NE(TabStripModel::kNoTab, index);
[email protected]a5231662009-09-26 00:56:002053
[email protected]2b4355c2009-04-04 17:55:462054 // Do some synchronous updates.
[email protected]d9083482012-01-06 00:38:462055 if (changed_flags & content::INVALIDATE_TYPE_URL &&
[email protected]0d56fa7212012-11-15 23:34:022056 source == tab_strip_model_->GetActiveWebContents()) {
[email protected]36b6dcb2008-11-12 01:19:572057 // Only update the URL for the current tab. Note that we do not update
2058 // the navigation commands since those would have already been updated
2059 // synchronously by NavigationStateChanged.
2060 UpdateToolbar(false);
[email protected]d9083482012-01-06 00:38:462061 changed_flags &= ~content::INVALIDATE_TYPE_URL;
[email protected]36b6dcb2008-11-12 01:19:572062 }
[email protected]d9083482012-01-06 00:38:462063 if (changed_flags & content::INVALIDATE_TYPE_LOAD) {
[email protected]6ebdc9b2010-09-27 16:55:572064 // Update the loading state synchronously. This is so the throbber will
2065 // immediately start/stop, which gives a more snappy feel. We want to do
[email protected]a5231662009-09-26 00:56:002066 // this for any tab so they start & stop quickly.
[email protected]526b45e2012-11-28 20:22:462067 tab_strip_model_->UpdateWebContentsStateAt(
[email protected]0d56fa7212012-11-15 23:34:022068 tab_strip_model_->GetIndexOfWebContents(source),
[email protected]6ebdc9b2010-09-27 16:55:572069 TabStripModelObserver::LOADING_ONLY);
[email protected]d9083482012-01-06 00:38:462070 // The status bubble needs to be updated during INVALIDATE_TYPE_LOAD too,
2071 // but we do that asynchronously by not stripping INVALIDATE_TYPE_LOAD from
[email protected]6ebdc9b2010-09-27 16:55:572072 // changed_flags.
[email protected]8030f012009-09-25 18:09:372073 }
2074
[email protected]d9083482012-01-06 00:38:462075 if (changed_flags & content::INVALIDATE_TYPE_TITLE && !source->IsLoading()) {
[email protected]f1cd5e82009-10-23 17:19:032076 // To correctly calculate whether the title changed while not loading
2077 // we need to process the update synchronously. This state only matters for
2078 // the TabStripModel, so we notify the TabStripModel now and notify others
2079 // asynchronously.
[email protected]526b45e2012-11-28 20:22:462080 tab_strip_model_->UpdateWebContentsStateAt(
[email protected]0d56fa7212012-11-15 23:34:022081 tab_strip_model_->GetIndexOfWebContents(source),
[email protected]f1cd5e82009-10-23 17:19:032082 TabStripModelObserver::TITLE_NOT_LOADING);
2083 }
2084
[email protected]2b4355c2009-04-04 17:55:462085 // If the only updates were synchronously handled above, we're done.
[email protected]8030f012009-09-25 18:09:372086 if (changed_flags == 0)
[email protected]2b4355c2009-04-04 17:55:462087 return;
[email protected]36b6dcb2008-11-12 01:19:572088
2089 // Save the dirty bits.
[email protected]85354982009-09-25 19:58:402090 scheduled_updates_[source] |= changed_flags;
[email protected]36b6dcb2008-11-12 01:19:572091
[email protected]24f1477532011-11-22 22:55:362092 if (!chrome_updater_factory_.HasWeakPtrs()) {
[email protected]36b6dcb2008-11-12 01:19:572093 // No task currently scheduled, start another.
[email protected]3264d162013-05-08 22:02:112094 base::MessageLoop::current()->PostDelayedTask(
[email protected]0586b0e2010-02-12 21:38:372095 FROM_HERE,
[email protected]24f1477532011-11-22 22:55:362096 base::Bind(&Browser::ProcessPendingUIUpdates,
2097 chrome_updater_factory_.GetWeakPtr()),
[email protected]11d6ab1a2012-01-12 21:37:262098 base::TimeDelta::FromMilliseconds(kUIUpdateCoalescingTimeMS));
[email protected]36b6dcb2008-11-12 01:19:572099 }
2100}
2101
2102void Browser::ProcessPendingUIUpdates() {
2103#ifndef NDEBUG
2104 // Validate that all tabs we have pending updates for exist. This is scary
2105 // because the pending list must be kept in sync with any detached or
[email protected]a5231662009-09-26 00:56:002106 // deleted tabs.
[email protected]85354982009-09-25 19:58:402107 for (UpdateMap::const_iterator i = scheduled_updates_.begin();
2108 i != scheduled_updates_.end(); ++i) {
[email protected]36b6dcb2008-11-12 01:19:572109 bool found = false;
[email protected]e0448872013-01-11 19:35:022110 for (int tab = 0; tab < tab_strip_model_->count(); tab++) {
[email protected]0d56fa7212012-11-15 23:34:022111 if (tab_strip_model_->GetWebContentsAt(tab) == i->first) {
[email protected]36b6dcb2008-11-12 01:19:572112 found = true;
2113 break;
2114 }
2115 }
2116 DCHECK(found);
2117 }
2118#endif
2119
[email protected]24f1477532011-11-22 22:55:362120 chrome_updater_factory_.InvalidateWeakPtrs();
[email protected]36b6dcb2008-11-12 01:19:572121
[email protected]85354982009-09-25 19:58:402122 for (UpdateMap::const_iterator i = scheduled_updates_.begin();
2123 i != scheduled_updates_.end(); ++i) {
[email protected]36b6dcb2008-11-12 01:19:572124 // Do not dereference |contents|, it may be out-of-date!
[email protected]2a6bc3e2011-12-28 23:51:332125 const WebContents* contents = i->first;
[email protected]85354982009-09-25 19:58:402126 unsigned flags = i->second;
[email protected]36b6dcb2008-11-12 01:19:572127
[email protected]0d56fa7212012-11-15 23:34:022128 if (contents == tab_strip_model_->GetActiveWebContents()) {
[email protected]a5231662009-09-26 00:56:002129 // Updates that only matter when the tab is selected go here.
[email protected]f7f3a5f2009-05-01 22:02:342130
[email protected]6ebdc9b2010-09-27 16:55:572131 // Updating the URL happens synchronously in ScheduleUIUpdate.
[email protected]d9083482012-01-06 00:38:462132 if (flags & content::INVALIDATE_TYPE_LOAD && GetStatusBubble()) {
[email protected]0d56fa7212012-11-15 23:34:022133 GetStatusBubble()->SetStatus(CoreTabHelper::FromWebContents(
2134 tab_strip_model_->GetActiveWebContents())->GetStatusText());
[email protected]0dd3a0ab2011-02-18 08:17:442135 }
[email protected]a5231662009-09-26 00:56:002136
[email protected]d9083482012-01-06 00:38:462137 if (flags & (content::INVALIDATE_TYPE_TAB |
2138 content::INVALIDATE_TYPE_TITLE)) {
[email protected]a5231662009-09-26 00:56:002139 window_->UpdateTitleBar();
2140 }
[email protected]4d34e2e2009-05-26 22:55:282141 }
[email protected]36b6dcb2008-11-12 01:19:572142
[email protected]a5231662009-09-26 00:56:002143 // Updates that don't depend upon the selected state go here.
[email protected]d9083482012-01-06 00:38:462144 if (flags &
2145 (content::INVALIDATE_TYPE_TAB | content::INVALIDATE_TYPE_TITLE)) {
[email protected]526b45e2012-11-28 20:22:462146 tab_strip_model_->UpdateWebContentsStateAt(
[email protected]e52d0a42012-06-08 22:44:162147 tab_strip_model_->GetIndexOfWebContents(contents),
[email protected]f1cd5e82009-10-23 17:19:032148 TabStripModelObserver::ALL);
[email protected]36b6dcb2008-11-12 01:19:572149 }
2150
[email protected]13155a22013-03-19 23:33:542151 // Update the bookmark bar. It may happen that the tab is crashed, and if
2152 // so, the bookmark bar should be hidden.
2153 if (flags & content::INVALIDATE_TYPE_TAB)
2154 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
2155
[email protected]36b6dcb2008-11-12 01:19:572156 // We don't need to process INVALIDATE_STATE, since that's not visible.
2157 }
2158
2159 scheduled_updates_.clear();
2160}
2161
[email protected]d9083482012-01-06 00:38:462162void Browser::RemoveScheduledUpdatesFor(WebContents* contents) {
[email protected]36b6dcb2008-11-12 01:19:572163 if (!contents)
2164 return;
2165
[email protected]85354982009-09-25 19:58:402166 UpdateMap::iterator i = scheduled_updates_.find(contents);
2167 if (i != scheduled_updates_.end())
2168 scheduled_updates_.erase(i);
initial.commit09911bf2008-07-26 23:55:292169}
2170
[email protected]36b6dcb2008-11-12 01:19:572171///////////////////////////////////////////////////////////////////////////////
2172// Browser, Getters for UI (private):
initial.commit09911bf2008-07-26 23:55:292173
[email protected]36b6dcb2008-11-12 01:19:572174StatusBubble* Browser::GetStatusBubble() {
[email protected]8e837ec2013-01-31 01:48:332175 // In kiosk and exclusive app mode, we want to always hide the status bubble.
2176 if (chrome::IsRunningInAppMode())
[email protected]34930432009-11-09 00:12:092177 return NULL;
[email protected]8e837ec2013-01-31 01:48:332178
[email protected]9b032bf2009-07-21 17:34:232179 return window_ ? window_->GetStatusBubble() : NULL;
initial.commit09911bf2008-07-26 23:55:292180}
2181
[email protected]36b6dcb2008-11-12 01:19:572182///////////////////////////////////////////////////////////////////////////////
2183// Browser, Session restore functions (private):
initial.commit09911bf2008-07-26 23:55:292184
2185void Browser::SyncHistoryWithTabs(int index) {
[email protected]92371eb2011-04-28 11:50:152186 SessionService* session_service =
2187 SessionServiceFactory::GetForProfileIfExisting(profile());
initial.commit09911bf2008-07-26 23:55:292188 if (session_service) {
[email protected]e0448872013-01-11 19:35:022189 for (int i = index; i < tab_strip_model_->count(); ++i) {
[email protected]0d56fa7212012-11-15 23:34:022190 WebContents* web_contents = tab_strip_model_->GetWebContentsAt(i);
[email protected]50a9a76b2012-09-12 14:19:362191 if (web_contents) {
2192 SessionTabHelper* session_tab_helper =
2193 SessionTabHelper::FromWebContents(web_contents);
initial.commit09911bf2008-07-26 23:55:292194 session_service->SetTabIndexInWindow(
[email protected]50a9a76b2012-09-12 14:19:362195 session_id(), session_tab_helper->session_id(), i);
[email protected]ecc523f62010-09-28 22:45:402196 session_service->SetPinnedState(
2197 session_id(),
[email protected]50a9a76b2012-09-12 14:19:362198 session_tab_helper->session_id(),
[email protected]52877dbc62012-06-29 22:22:032199 tab_strip_model_->IsTabPinned(i));
initial.commit09911bf2008-07-26 23:55:292200 }
2201 }
2202 }
2203}
2204
[email protected]36b6dcb2008-11-12 01:19:572205///////////////////////////////////////////////////////////////////////////////
[email protected]69444cc2009-04-09 20:40:062206// Browser, In-progress download termination handling (private):
2207
[email protected]b6a4ac2b2011-10-17 20:05:482208bool Browser::CanCloseWithInProgressDownloads() {
[email protected]5a17d492011-10-24 19:37:032209 // If we've prompted, we need to hear from the user before we
2210 // can close.
2211 if (cancel_download_confirmation_state_ != NOT_PROMPTED)
2212 return cancel_download_confirmation_state_ != WAITING_FOR_RESPONSE;
2213
2214 int num_downloads_blocking;
[email protected]edfca702013-08-16 08:58:142215 Browser::DownloadClosePreventionType dialog_type =
2216 OkToCloseWithInProgressDownloads(&num_downloads_blocking);
2217 if (dialog_type == DOWNLOAD_CLOSE_OK)
[email protected]446295a2010-04-19 23:43:002218 return true;
2219
[email protected]5a17d492011-10-24 19:37:032220 // Closing this window will kill some downloads; prompt to make sure
2221 // that's ok.
[email protected]69444cc2009-04-09 20:40:062222 cancel_download_confirmation_state_ = WAITING_FOR_RESPONSE;
[email protected]edfca702013-08-16 08:58:142223 window_->ConfirmBrowserCloseWithPendingDownloads(
2224 num_downloads_blocking,
2225 dialog_type,
2226 false,
2227 base::Bind(&Browser::InProgressDownloadResponse,
2228 weak_factory_.GetWeakPtr()));
[email protected]69444cc2009-04-09 20:40:062229
2230 // Return false so the browser does not close. We'll close if the user
2231 // confirms in the dialog.
2232 return false;
2233}
2234
2235///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:572236// Browser, Assorted utility functions (private):
initial.commit09911bf2008-07-26 23:55:292237
[email protected]63d1f9b2014-07-05 19:09:032238void Browser::SetAsDelegate(WebContents* web_contents, bool set_delegate) {
2239 Browser* delegate = set_delegate ? this : NULL;
[email protected]27e5b3912012-12-16 00:45:372240 // WebContents...
2241 web_contents->SetDelegate(delegate);
2242
2243 // ...and all the helpers.
[email protected]27e5b3912012-12-16 00:45:372244 BookmarkTabHelper::FromWebContents(web_contents)->set_delegate(delegate);
[email protected]d2b16572013-01-03 00:41:582245 WebContentsModalDialogManager::FromWebContents(web_contents)->
[email protected]5c2c7792013-08-20 18:39:162246 SetDelegate(delegate);
[email protected]27e5b3912012-12-16 00:45:372247 CoreTabHelper::FromWebContents(web_contents)->set_delegate(delegate);
2248 SearchEngineTabHelper::FromWebContents(web_contents)->set_delegate(delegate);
[email protected]82808d02014-04-10 00:10:592249 SearchTabHelper::FromWebContents(web_contents)->set_delegate(delegate);
droger38f0d102014-09-16 15:37:412250 translate::ContentTranslateDriver& content_translate_driver =
2251 ChromeTranslateClient::FromWebContents(web_contents)->translate_driver();
2252 if (delegate) {
[email protected]63d1f9b2014-07-05 19:09:032253 ZoomController::FromWebContents(web_contents)->AddObserver(this);
droger38f0d102014-09-16 15:37:412254 content_translate_driver.AddObserver(translate_driver_observer_.get());
2255 } else {
[email protected]63d1f9b2014-07-05 19:09:032256 ZoomController::FromWebContents(web_contents)->RemoveObserver(this);
droger38f0d102014-09-16 15:37:412257 content_translate_driver.RemoveObserver(translate_driver_observer_.get());
2258 }
[email protected]27e5b3912012-12-16 00:45:372259}
2260
[email protected]36b6dcb2008-11-12 01:19:572261void Browser::CloseFrame() {
2262 window_->Close();
initial.commit09911bf2008-07-26 23:55:292263}
2264
[email protected]e89cfcb2012-11-11 14:47:242265void Browser::TabDetachedAtImpl(content::WebContents* contents,
2266 int index,
[email protected]c37e3b62010-01-22 00:15:172267 DetachType type) {
2268 if (type == DETACH_TYPE_DETACH) {
[email protected]56580852010-11-17 16:09:152269 // Save the current location bar state, but only if the tab being detached
2270 // is the selected tab. Because saving state can conditionally revert the
2271 // location bar, saving the current tab's location bar state to a
2272 // non-selected tab can corrupt both tabs.
[email protected]0d56fa7212012-11-15 23:34:022273 if (contents == tab_strip_model_->GetActiveWebContents()) {
[email protected]9800de5e2011-03-11 18:19:492274 LocationBar* location_bar = window()->GetLocationBar();
2275 if (location_bar)
[email protected]e89cfcb2012-11-11 14:47:242276 location_bar->SaveStateToContents(contents);
[email protected]9800de5e2011-03-11 18:19:492277 }
[email protected]c37e3b62010-01-22 00:15:172278
[email protected]2fc15ae2012-05-06 00:01:372279 if (!tab_strip_model_->closing_all())
[email protected]c37e3b62010-01-22 00:15:172280 SyncHistoryWithTabs(0);
2281 }
2282
[email protected]63d1f9b2014-07-05 19:09:032283 SetAsDelegate(contents, false);
[email protected]e89cfcb2012-11-11 14:47:242284 RemoveScheduledUpdatesFor(contents);
[email protected]c37e3b62010-01-22 00:15:172285
[email protected]04e3ead52013-01-14 01:09:202286 if (find_bar_controller_.get() && index == tab_strip_model_->active_index()) {
[email protected]ba3d09a12012-11-07 12:13:522287 find_bar_controller_->ChangeWebContents(NULL);
[email protected]ecc523f62010-09-28 22:45:402288 }
[email protected]c37e3b62010-01-22 00:15:172289
[email protected]e3a5d5b2012-06-22 17:56:462290 // Stop observing search model changes for this tab.
[email protected]e89cfcb2012-11-11 14:47:242291 search_delegate_->OnTabDetached(contents);
[email protected]e3a5d5b2012-06-22 17:56:462292
[email protected]20ca0382013-02-28 19:50:072293 for (size_t i = 0; i < interstitial_observers_.size(); i++) {
2294 if (interstitial_observers_[i]->web_contents() != contents)
2295 continue;
2296
2297 delete interstitial_observers_[i];
2298 interstitial_observers_.erase(interstitial_observers_.begin() + i);
2299 return;
2300 }
[email protected]c37e3b62010-01-22 00:15:172301}
2302
[email protected]8a585cb2014-03-21 17:13:522303bool Browser::ShouldShowLocationBar() const {
[email protected]d2202e22014-04-14 20:20:532304 // Tabbed browser always show a location bar.
2305 if (is_type_tabbed())
[email protected]8a585cb2014-03-21 17:13:522306 return true;
[email protected]d2202e22014-04-14 20:20:532307
[email protected]489465d2014-05-01 07:54:102308 if (is_app()) {
benwells39f23ae2014-08-27 08:01:522309 if (extensions::util::IsStreamlinedHostedAppsEnabled() &&
[email protected]b0fc9e82014-08-19 05:48:192310 host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH) {
benwells39f23ae2014-08-27 08:01:522311 // If streamlined hosted apps are enabled, show the location bar for
[email protected]b0fc9e82014-08-19 05:48:192312 // bookmark apps, except on ash which has the toolbar merged into the
2313 // frame.
[email protected]489465d2014-05-01 07:54:102314 ExtensionService* service =
2315 extensions::ExtensionSystem::Get(profile_)->extension_service();
2316 const extensions::Extension* extension =
2317 service ? service->GetInstalledExtension(
2318 web_app::GetExtensionIdFromApplicationName(app_name()))
2319 : NULL;
2320 return (!extension || extension->from_bookmark()) &&
2321 app_name() != DevToolsWindow::kDevToolsApp;
2322 } else {
2323 return false;
2324 }
[email protected]d2202e22014-04-14 20:20:532325 }
[email protected]8a585cb2014-03-21 17:13:522326
[email protected]489465d2014-05-01 07:54:102327 // Trusted app windows and system windows never show a location bar.
[email protected]2fb38882014-04-23 09:02:092328 return !is_trusted_source();
[email protected]8a585cb2014-03-21 17:13:522329}
2330
[email protected]92655362012-04-20 21:33:172331bool Browser::SupportsWindowFeatureImpl(WindowFeature feature,
2332 bool check_fullscreen) const {
[email protected]c3e32942012-12-11 10:26:202333 bool hide_ui_for_fullscreen = check_fullscreen && ShouldHideUIForFullscreen();
[email protected]92655362012-04-20 21:33:172334
2335 unsigned int features = FEATURE_INFOBAR | FEATURE_DOWNLOADSHELF;
2336
2337 if (is_type_tabbed())
2338 features |= FEATURE_BOOKMARKBAR;
2339
2340 if (!hide_ui_for_fullscreen) {
2341 if (!is_type_tabbed())
2342 features |= FEATURE_TITLEBAR;
2343
2344 if (is_type_tabbed())
2345 features |= FEATURE_TABSTRIP;
2346
2347 if (is_type_tabbed())
2348 features |= FEATURE_TOOLBAR;
2349
[email protected]8a585cb2014-03-21 17:13:522350 if (ShouldShowLocationBar())
[email protected]92655362012-04-20 21:33:172351 features |= FEATURE_LOCATIONBAR;
2352 }
2353 return !!(features & feature);
2354}
2355
[email protected]09b29342011-06-24 19:18:482356void Browser::UpdateBookmarkBarState(BookmarkBarStateChangeReason reason) {
2357 BookmarkBar::State state;
[email protected]a7666d82014-04-04 21:34:032358 // The bookmark bar is always hidden for Guest Sessions and in fullscreen
2359 // mode, unless on the new tab page.
2360 if (profile_->IsGuestSession()) {
2361 state = BookmarkBar::HIDDEN;
2362 } else if (browser_defaults::bookmarks_enabled &&
tfarina3bddbe112014-08-28 05:29:322363 profile_->GetPrefs()->GetBoolean(bookmarks::prefs::kShowBookmarkBar) &&
[email protected]c3e32942012-12-11 10:26:202364 !ShouldHideUIForFullscreen()) {
[email protected]04143362012-11-28 14:25:182365 state = BookmarkBar::SHOW;
[email protected]09b29342011-06-24 19:18:482366 } else {
[email protected]0d56fa7212012-11-15 23:34:022367 WebContents* web_contents = tab_strip_model_->GetActiveWebContents();
[email protected]1c5119c2012-09-19 00:08:572368 BookmarkTabHelper* bookmark_tab_helper =
2369 web_contents ? BookmarkTabHelper::FromWebContents(web_contents) : NULL;
2370 if (bookmark_tab_helper && bookmark_tab_helper->ShouldShowBookmarkBar())
[email protected]09b29342011-06-24 19:18:482371 state = BookmarkBar::DETACHED;
2372 else
2373 state = BookmarkBar::HIDDEN;
2374 }
[email protected]2d4e43a02012-06-26 00:11:552375
[email protected]09b29342011-06-24 19:18:482376 if (state == bookmark_bar_state_)
2377 return;
2378
2379 bookmark_bar_state_ = state;
2380
2381 if (!window_)
2382 return; // This is called from the constructor when window_ is NULL.
2383
2384 if (reason == BOOKMARK_BAR_STATE_CHANGE_TAB_SWITCH) {
2385 // Don't notify BrowserWindow on a tab switch as at the time this is invoked
2386 // BrowserWindow hasn't yet switched tabs. The BrowserWindow implementations
2387 // end up querying state once they process the tab switch.
2388 return;
2389 }
2390
[email protected]e2395da2013-01-23 22:05:352391 bool shouldAnimate = reason == BOOKMARK_BAR_STATE_CHANGE_PREF_CHANGE;
2392 window_->BookmarkBarStateChanged(shouldAnimate ?
[email protected]09b29342011-06-24 19:18:482393 BookmarkBar::ANIMATE_STATE_CHANGE :
[email protected]e2395da2013-01-23 22:05:352394 BookmarkBar::DONT_ANIMATE_STATE_CHANGE);
[email protected]09b29342011-06-24 19:18:482395}
[email protected]2e2cacc2011-07-12 21:54:262396
[email protected]c3e32942012-12-11 10:26:202397bool Browser::ShouldHideUIForFullscreen() const {
[email protected]6a414ff2013-02-27 08:22:542398 // Windows and GTK remove the top controls in fullscreen, but Mac and Ash
2399 // keep the controls in a slide-down panel.
2400 return window_ && window_->ShouldHideUIForFullscreen();
[email protected]c3e32942012-12-11 10:26:202401}
2402
[email protected]dd6730412013-08-14 15:03:372403bool Browser::MaybeCreateBackgroundContents(
2404 int route_id,
2405 WebContents* opener_web_contents,
[email protected]dcd0249872013-12-06 23:58:452406 const base::string16& frame_name,
[email protected]dd6730412013-08-14 15:03:372407 const GURL& target_url,
2408 const std::string& partition_id,
2409 content::SessionStorageNamespace* session_storage_namespace) {
[email protected]92655362012-04-20 21:33:172410 GURL opener_url = opener_web_contents->GetURL();
[email protected]06bdd2b2012-11-30 18:47:132411 ExtensionService* extensions_service =
2412 extensions::ExtensionSystem::Get(profile_)->extension_service();
[email protected]92655362012-04-20 21:33:172413
2414 if (!opener_url.is_valid() ||
2415 frame_name.empty() ||
2416 !extensions_service ||
2417 !extensions_service->is_ready())
2418 return false;
2419
2420 // Only hosted apps have web extents, so this ensures that only hosted apps
2421 // can create BackgroundContents. We don't have to check for background
2422 // permission as that is checked in RenderMessageFilter when the CreateWindow
2423 // message is processed.
2424 const Extension* extension =
[email protected]be9915fb2013-07-18 09:28:552425 extensions_service->extensions()->GetHostedAppByURL(opener_url);
[email protected]92655362012-04-20 21:33:172426 if (!extension)
2427 return false;
2428
2429 // No BackgroundContents allowed if BackgroundContentsService doesn't exist.
2430 BackgroundContentsService* service =
2431 BackgroundContentsServiceFactory::GetForProfile(profile_);
2432 if (!service)
2433 return false;
2434
2435 // Ensure that we're trying to open this from the extension's process.
2436 SiteInstance* opener_site_instance = opener_web_contents->GetSiteInstance();
[email protected]fafdc842014-01-17 18:09:082437 extensions::ProcessMap* process_map = extensions::ProcessMap::Get(profile_);
[email protected]92655362012-04-20 21:33:172438 if (!opener_site_instance->GetProcess() ||
2439 !process_map->Contains(
2440 extension->id(), opener_site_instance->GetProcess()->GetID())) {
2441 return false;
2442 }
2443
2444 // Only allow a single background contents per app.
[email protected]9367eabc2013-03-01 01:29:292445 bool allow_js_access = extensions::BackgroundInfo::AllowJSAccess(extension);
[email protected]92655362012-04-20 21:33:172446 BackgroundContents* existing =
[email protected]1f136902013-12-24 23:26:452447 service->GetAppBackgroundContents(base::ASCIIToUTF16(extension->id()));
[email protected]92655362012-04-20 21:33:172448 if (existing) {
2449 // For non-scriptable background contents, ignore the request altogether,
2450 // (returning true, so that a regular WebContents isn't created either).
2451 if (!allow_js_access)
2452 return true;
2453 // For scriptable background pages, if one already exists, close it (even
2454 // if it was specified in the manifest).
[email protected]92655362012-04-20 21:33:172455 delete existing;
2456 }
2457
2458 // If script access is not allowed, create the the background contents in a
2459 // new SiteInstance, so that a separate process is used.
2460 scoped_refptr<content::SiteInstance> site_instance =
2461 allow_js_access ?
2462 opener_site_instance :
2463 content::SiteInstance::Create(opener_web_contents->GetBrowserContext());
2464
2465 // Passed all the checks, so this should be created as a BackgroundContents.
[email protected]39790b7f2013-06-03 00:10:592466 BackgroundContents* contents =
2467 service->CreateBackgroundContents(site_instance.get(),
2468 route_id,
2469 profile_,
2470 frame_name,
[email protected]1f136902013-12-24 23:26:452471 base::ASCIIToUTF16(extension->id()),
[email protected]dd6730412013-08-14 15:03:372472 partition_id,
2473 session_storage_namespace);
[email protected]92655362012-04-20 21:33:172474
2475 // When a separate process is used, the original renderer cannot access the
2476 // new window later, thus we need to navigate the window now.
2477 if (contents && !allow_js_access) {
2478 contents->web_contents()->GetController().LoadURL(
2479 target_url,
2480 content::Referrer(),
2481 content::PAGE_TRANSITION_LINK,
2482 std::string()); // No extra headers.
2483 }
2484
2485 return contents != NULL;
[email protected]afcb43562011-09-02 23:33:382486}