blob: 39519544c8df67a06252595604997d394b10d618 [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
avi655876a2015-12-25 07:18:157#include <stddef.h>
[email protected]aca02cf2010-05-03 18:56:018
[email protected]5dcbc02f2010-01-26 22:32:069#include <algorithm>
10#include <string>
juncaicd975582015-12-21 22:58:1011#include <utility>
[email protected]5dcbc02f2010-01-26 22:32:0612
[email protected]b02d0382009-11-30 21:19:5013#include "base/base_paths.h"
[email protected]24f1477532011-11-22 22:55:3614#include "base/bind.h"
[email protected]8a53ee042009-01-21 16:41:3315#include "base/command_line.h"
skyostil380bb2222015-06-12 12:07:0516#include "base/location.h"
[email protected]8a53ee042009-01-21 16:41:3317#include "base/logging.h"
avi655876a2015-12-25 07:18:1518#include "base/macros.h"
[email protected]835d7c82010-10-14 04:38:3819#include "base/metrics/histogram.h"
[email protected]d09a4ce1c2013-07-24 17:37:0220#include "base/process/process_info.h"
erikchen174b78c62015-03-11 22:02:5421#include "base/profiler/scoped_tracker.h"
skyostil380bb2222015-06-12 12:07:0522#include "base/single_thread_task_runner.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"
gabb15e19072016-05-11 20:45:4129#include "base/threading/thread_task_runner_handle.h"
[email protected]bd64063d2013-06-28 15:16:2230#include "base/time/time.h"
avi655876a2015-12-25 07:18:1531#include "build/build_config.h"
[email protected]1a3aba82010-11-08 23:52:5432#include "chrome/app/chrome_command_ids.h"
[email protected]8e837ec2013-01-31 01:48:3333#include "chrome/browser/app_mode/app_mode_utils.h"
[email protected]ea9edcb02011-09-23 22:05:0434#include "chrome/browser/autofill/personal_data_manager_factory.h"
avi71d0f762014-12-15 16:08:4735#include "chrome/browser/background/background_contents.h"
[email protected]a07676b22011-06-17 16:36:5336#include "chrome/browser/background/background_contents_service.h"
[email protected]4fdf6742012-01-10 20:14:3637#include "chrome/browser/background/background_contents_service_factory.h"
horo9638ad272016-01-25 14:36:4538#include "chrome/browser/banners/app_banner_manager_desktop.h"
pfeldman7a69caa2016-05-07 21:33:5039#include "chrome/browser/banners/app_banner_manager_emulation.h"
[email protected]5c238752009-06-13 10:29:0740#include "chrome/browser/browser_process.h"
[email protected]c61db1d2009-02-06 03:39:1841#include "chrome/browser/browser_shutdown.h"
[email protected]40d59ce52009-03-06 23:20:1442#include "chrome/browser/character_encoding.h"
[email protected]dcc8fbc2013-07-12 00:54:0943#include "chrome/browser/chrome_notification_types.h"
[email protected]7ae9c322012-07-17 23:46:4444#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]7d189022011-08-25 22:54:2045#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
[email protected]8a39b472013-04-04 17:07:4246#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
[email protected]f1d20572014-02-14 01:27:1447#include "chrome/browser/custom_handlers/register_protocol_handler_permission_request.h"
[email protected]5fec4702013-11-20 04:37:3848#include "chrome/browser/defaults.h"
[email protected]70019152012-12-19 11:44:1949#include "chrome/browser/devtools/devtools_toggle_action.h"
50#include "chrome/browser/devtools/devtools_window.h"
[email protected]9bb54ee2011-10-12 17:43:3551#include "chrome/browser/download/download_service.h"
52#include "chrome/browser/download/download_service_factory.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"
benwells919e79b2015-03-19 04:58:1957#include "chrome/browser/extensions/extension_ui_util.h"
benwells39f23ae2014-08-27 08:01:5258#include "chrome/browser/extensions/extension_util.h"
[email protected]a6394ae2012-07-16 20:58:4359#include "chrome/browser/extensions/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"
jitendra.ks30f03392015-01-28 09:47:1862#include "chrome/browser/history/top_sites_factory.h"
[email protected]4a8adfa02013-03-19 22:37:4663#include "chrome/browser/infobars/infobar_service.h"
[email protected]2e6389f2012-05-18 19:41:2564#include "chrome/browser/lifetime/application_lifetime.h"
dgn02377782016-03-12 00:58:3865#include "chrome/browser/lifetime/keep_alive_registry.h"
dgnfe075c82016-03-18 11:25:3566#include "chrome/browser/lifetime/keep_alive_types.h"
67#include "chrome/browser/lifetime/scoped_keep_alive.h"
georgesak6fc95e92015-10-26 21:27:0668#include "chrome/browser/memory/tab_manager_web_contents_data.h"
[email protected]2c4fb7b2011-04-02 06:33:2969#include "chrome/browser/notifications/notification_ui_manager.h"
[email protected]8c8fc292012-11-23 18:57:1670#include "chrome/browser/pepper_broker_infobar_delegate.h"
tsergeanta771c23e2016-07-13 04:54:5271#include "chrome/browser/permissions/permission_request_manager.h"
[email protected]4e94ab32011-08-05 05:28:2772#include "chrome/browser/prefs/incognito_mode_prefs.h"
[email protected]8ecad5e2010-12-02 21:18:3373#include "chrome/browser/profiles/profile.h"
[email protected]5e919242012-02-13 23:59:3574#include "chrome/browser/profiles/profile_destroyer.h"
[email protected]a7be6982011-12-12 21:53:5775#include "chrome/browser/profiles/profile_metrics.h"
mlerman2c1c3782015-04-23 18:37:4376#include "chrome/browser/profiles/profiles_state.h"
[email protected]67baffc82011-12-19 18:03:0777#include "chrome/browser/repost_form_warning_controller.h"
[email protected]a7b8e43d2013-03-18 18:52:4378#include "chrome/browser/search/search.h"
georgesak5582cbe2015-05-22 22:08:0779#include "chrome/browser/sessions/session_restore.h"
[email protected]85e921fb82009-02-11 23:19:4480#include "chrome/browser/sessions/session_service.h"
[email protected]92371eb2011-04-28 11:50:1581#include "chrome/browser/sessions/session_service_factory.h"
[email protected]bde40b822012-08-30 17:28:5682#include "chrome/browser/sessions/session_tab_helper.h"
[email protected]92371eb2011-04-28 11:50:1583#include "chrome/browser/sessions/tab_restore_service_factory.h"
estark880a5a42015-11-30 18:21:2184#include "chrome/browser/ssl/chrome_security_state_model_client.h"
[email protected]5e61afb92012-01-27 20:09:1385#include "chrome/browser/sync/profile_sync_service_factory.h"
[email protected]40f047972009-11-25 03:54:4086#include "chrome/browser/sync/sync_ui_util.h"
[email protected]4fdf6742012-01-10 20:14:3687#include "chrome/browser/tab_contents/retargeting_details.h"
[email protected]1ab4ddf2011-07-21 04:48:0488#include "chrome/browser/tab_contents/tab_util.h"
afakhry8394f942015-07-09 18:46:0989#include "chrome/browser/task_management/web_contents_tags.h"
[email protected]ffa6f592011-06-24 22:03:5790#include "chrome/browser/themes/theme_service.h"
91#include "chrome/browser/themes/theme_service_factory.h"
[email protected]255ab972014-06-04 12:14:2292#include "chrome/browser/translate/chrome_translate_client.h"
[email protected]b4db6d42014-06-07 04:36:5893#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
[email protected]5635e6142013-07-18 08:10:4094#include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
juncaibf183dd2016-05-27 16:57:2495#include "chrome/browser/ui/bluetooth/bluetooth_chooser_controller.h"
juncaicd975582015-12-21 22:58:1096#include "chrome/browser/ui/bluetooth/bluetooth_chooser_desktop.h"
[email protected]35699d572011-05-11 19:46:1497#include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
[email protected]4db2e912014-03-04 07:08:5898#include "chrome/browser/ui/bookmarks/bookmark_utils.h"
[email protected]5d98294912012-06-27 22:57:4099#include "chrome/browser/ui/browser_command_controller.h"
[email protected]a37d4b02012-06-25 21:56:10100#include "chrome/browser/ui/browser_commands.h"
[email protected]ef557022012-03-16 10:05:33101#include "chrome/browser/ui/browser_content_setting_bubble_model_delegate.h"
[email protected]55452902011-06-01 21:57:47102#include "chrome/browser/ui/browser_dialogs.h"
[email protected]d8748142012-05-16 21:13:43103#include "chrome/browser/ui/browser_finder.h"
[email protected]7acfaf92012-07-11 15:51:59104#include "chrome/browser/ui/browser_instant_controller.h"
[email protected]6768ac02011-04-06 17:41:04105#include "chrome/browser/ui/browser_list.h"
blundellbde024d2015-09-30 13:47:07106#include "chrome/browser/ui/browser_live_tab_context.h"
[email protected]fcca741b2011-06-17 22:46:37107#include "chrome/browser/ui/browser_navigator.h"
thestige80821242015-09-30 23:46:08108#include "chrome/browser/ui/browser_navigator_params.h"
[email protected]855370052012-07-10 19:30:32109#include "chrome/browser/ui/browser_tab_strip_model_delegate.h"
[email protected]52877dbc62012-06-29 22:22:03110#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]ef557022012-03-16 10:05:33111#include "chrome/browser/ui/browser_toolbar_model_delegate.h"
[email protected]2cd4fde2012-06-26 03:10:26112#include "chrome/browser/ui/browser_ui_prefs.h"
[email protected]00070c732011-04-09 15:31:33113#include "chrome/browser/ui/browser_window.h"
juncaicd975582015-12-21 22:58:10114#include "chrome/browser/ui/chrome_bubble_manager.h"
[email protected]5d9cace72012-06-21 16:07:12115#include "chrome/browser/ui/chrome_pages.h"
[email protected]6e1fcd12012-07-02 17:14:20116#include "chrome/browser/ui/chrome_select_file_policy.h"
sriramsr3e76a412014-12-18 19:25:29117#include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
sriramsra41db562015-01-26 22:45:04118#include "chrome/browser/ui/exclusive_access/mouse_lock_controller.h"
benwells11bd6ed2015-06-05 04:20:41119#include "chrome/browser/ui/extensions/hosted_app_browser_controller.h"
[email protected]88c92012013-07-02 11:56:34120#include "chrome/browser/ui/fast_unload_controller.h"
[email protected]45300ad42010-12-02 15:51:14121#include "chrome/browser/ui/find_bar/find_bar.h"
122#include "chrome/browser/ui/find_bar/find_bar_controller.h"
[email protected]c90c6ca2011-02-16 20:11:38123#include "chrome/browser/ui/find_bar/find_tab_helper.h"
[email protected]1ba55cf2012-06-29 19:11:39124#include "chrome/browser/ui/global_error/global_error.h"
125#include "chrome/browser/ui/global_error/global_error_service.h"
126#include "chrome/browser/ui/global_error/global_error_service_factory.h"
[email protected]d57ce6a2014-07-03 15:39:23127#include "chrome/browser/ui/location_bar/location_bar.h"
[email protected]859ec322013-09-13 16:24:56128#include "chrome/browser/ui/media_utils.h"
[email protected]e3a5d5b2012-06-22 17:56:46129#include "chrome/browser/ui/search/search_delegate.h"
130#include "chrome/browser/ui/search/search_model.h"
[email protected]549f62b2013-10-26 12:54:42131#include "chrome/browser/ui/search/search_tab_helper.h"
[email protected]f9253672014-07-31 03:35:38132#include "chrome/browser/ui/settings_window_manager.h"
[email protected]5d9cace72012-06-21 16:07:12133#include "chrome/browser/ui/singleton_tabs.h"
[email protected]6a3ec2312010-12-02 19:30:19134#include "chrome/browser/ui/status_bubble.h"
[email protected]3da95a102011-11-30 21:47:45135#include "chrome/browser/ui/sync/browser_synced_window_delegate.h"
[email protected]51d83bdf2011-12-09 16:09:29136#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
andresantoso063151d2014-12-18 22:37:55137#include "chrome/browser/ui/tab_dialogs.h"
[email protected]e0bc75d52014-01-30 23:42:59138#include "chrome/browser/ui/tab_helpers.h"
[email protected]51296c62012-09-26 13:02:44139#include "chrome/browser/ui/tab_modal_confirm_dialog.h"
[email protected]6a3ec2312010-12-02 19:30:19140#include "chrome/browser/ui/tabs/tab_menu_model.h"
[email protected]b56e2e32012-05-11 21:18:04141#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]82808d02014-04-10 00:10:59142#include "chrome/browser/ui/tabs/tab_strip_model_utils.h"
jared.sohna1409622015-08-08 07:16:14143#include "chrome/browser/ui/tabs/tab_utils.h"
[email protected]f9b6c092012-07-09 19:32:28144#include "chrome/browser/ui/unload_controller.h"
[email protected]edc3af82013-12-12 21:24:07145#include "chrome/browser/ui/validation_message_bubble.h"
juncaibf183dd2016-05-27 16:57:24146#include "chrome/browser/ui/website_settings/chooser_bubble_delegate.h"
[email protected]65549182012-02-25 00:45:40147#include "chrome/browser/ui/webui/signin/login_ui_service.h"
148#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
[email protected]03639422012-06-28 19:03:37149#include "chrome/browser/ui/window_sizer/window_sizer.h"
[email protected]b1b73942010-05-26 20:11:54150#include "chrome/browser/upgrade_detector.h"
[email protected]d938aed92009-01-22 19:49:33151#include "chrome/common/chrome_constants.h"
152#include "chrome/common/chrome_switches.h"
[email protected]7d189022011-08-25 22:54:20153#include "chrome/common/custom_handlers/protocol_handler.h"
[email protected]8a53ee042009-01-21 16:41:33154#include "chrome/common/pref_names.h"
[email protected]7664ab32011-02-01 23:35:25155#include "chrome/common/profiling.h"
treib4dff7cf2016-07-06 08:53:30156#include "chrome/common/search/search_types.h"
[email protected]c744f7d22013-04-12 20:29:04157#include "chrome/common/url_constants.h"
thestig884a1602014-08-27 01:29:39158#include "chrome/grit/chromium_strings.h"
159#include "chrome/grit/generated_resources.h"
thestigaeddd322014-08-27 06:58:39160#include "chrome/grit/locale_settings.h"
oshimaf65398422014-11-18 23:30:42161#include "components/app_modal/javascript_dialog_manager.h"
[email protected]a90c8ca2014-05-20 17:16:04162#include "components/bookmarks/browser/bookmark_model.h"
163#include "components/bookmarks/browser/bookmark_utils.h"
brettwe1f0af8b2015-10-09 21:30:46164#include "components/bookmarks/common/bookmark_pref_names.h"
blundell7282b512015-11-09 07:21:11165#include "components/browser_sync/browser/profile_sync_service.h"
juncaicd975582015-12-21 22:58:10166#include "components/bubble/bubble_controller.h"
mukai8eaec822014-10-25 17:53:16167#include "components/content_settings/core/browser/host_content_settings_map.h"
sdefresne455826972015-04-10 15:25:15168#include "components/favicon/content/content_favicon_driver.h"
sdefresne0da3bc02015-01-29 18:26:35169#include "components/history/core/browser/top_sites.h"
brettwb1fc1b82016-02-02 00:19:08170#include "components/prefs/pref_service.h"
[email protected]720b10492014-07-23 08:48:40171#include "components/search/search.h"
estarkd9e54fb2016-01-11 19:37:12172#include "components/security_state/security_state_model.h"
blundell47c6d8a2015-09-24 11:06:40173#include "components/sessions/core/session_types.h"
blundella08c5dd2015-09-18 06:14:16174#include "components/sessions/core/tab_restore_service.h"
fdoray45c02c162015-11-09 20:14:30175#include "components/startup_metric_utils/browser/startup_metric_utils.h"
sdefresne3adc092b2016-02-04 14:18:36176#include "components/toolbar/toolbar_model_impl.h"
pkastingcc29d20e2015-02-25 06:05:53177#include "components/translate/core/browser/language_state.h"
[email protected]e41d0082013-05-16 04:37:54178#include "components/web_modal/web_contents_modal_dialog_manager.h"
juncaia64f769c22016-06-04 00:55:32179#include "components/zoom/zoom_controller.h"
[email protected]b50452f2014-08-18 12:31:44180#include "content/public/browser/devtools_agent_host.h"
[email protected]cadaec52012-02-08 21:53:13181#include "content/public/browser/interstitial_page.h"
[email protected]d9083482012-01-06 00:38:46182#include "content/public/browser/invalidate_type.h"
[email protected]cdcb1dee2012-01-04 00:46:20183#include "content/public/browser/navigation_controller.h"
[email protected]022af742011-12-28 18:37:25184#include "content/public/browser/navigation_entry.h"
[email protected]7f6f44c2011-12-14 13:23:38185#include "content/public/browser/notification_details.h"
186#include "content/public/browser/notification_service.h"
[email protected]3a5180ae2011-12-21 02:39:38187#include "content/public/browser/plugin_service.h"
horo0ff5dcb2016-01-29 10:15:08188#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:33189#include "content/public/browser/render_process_host.h"
190#include "content/public/browser/render_view_host.h"
avif9ab5d942015-10-15 14:05:44191#include "content/public/browser/render_widget_host.h"
[email protected]edc3af82013-12-12 21:24:07192#include "content/public/browser/render_widget_host_view.h"
[email protected]b6583592012-01-25 19:52:33193#include "content/public/browser/site_instance.h"
[email protected]7f6f44c2011-12-14 13:23:38194#include "content/public/browser/user_metrics.h"
[email protected]d9083482012-01-06 00:38:46195#include "content/public/browser/web_contents.h"
sdefresne4d623d702016-02-03 20:07:57196#include "content/public/common/content_constants.h"
[email protected]c08950d22011-10-13 22:20:29197#include "content/public/common/content_switches.h"
[email protected]0f083402011-11-22 02:59:01198#include "content/public/common/page_zoom.h"
[email protected]cadaec52012-02-08 21:53:13199#include "content/public/common/renderer_preferences.h"
estark36948be2015-08-05 21:41:25200#include "content/public/common/ssl_status.h"
[email protected]d7bd3e52013-07-21 04:29:20201#include "content/public/common/webplugininfo.h"
[email protected]489db0842014-01-22 18:20:03202#include "extensions/browser/extension_prefs.h"
jitendra.ksa0055532014-10-07 05:54:46203#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:24204#include "extensions/browser/extension_system.h"
[email protected]885c0e92012-11-13 20:27:42205#include "extensions/common/constants.h"
[email protected]e4452d32013-11-15 23:07:41206#include "extensions/common/extension.h"
[email protected]558878cc82013-11-09 01:25:51207#include "extensions/common/manifest_handlers/background_info.h"
[email protected]d96cf752014-04-09 04:05:28208#include "net/base/filename_util.h"
[email protected]be28b5f42012-07-20 11:31:25209#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]4d7c4ef2012-03-16 01:47:12210#include "net/cookies/cookie_monster.h"
[email protected]8a53ee042009-01-21 16:41:33211#include "net/url_request/url_request_context.h"
[email protected]453c1b22013-07-23 09:31:48212#include "third_party/WebKit/public/web/WebWindowFeatures.h"
[email protected]c051a1b2011-01-21 23:30:17213#include "ui/base/l10n/l10n_util.h"
[email protected]f47621b2013-01-22 20:50:33214#include "ui/base/window_open_disposition.h"
tfarina655f81d2014-12-23 02:38:50215#include "ui/gfx/geometry/point.h"
[email protected]4344a3c2013-01-17 23:49:20216#include "ui/shell_dialogs/selected_file_info.h"
[email protected]8a53ee042009-01-21 16:41:33217
218#if defined(OS_WIN)
avi655876a2015-12-25 07:18:15219#include <windows.h>
220#include <shellapi.h>
[email protected]6a3ec2312010-12-02 19:30:19221#include "chrome/browser/ui/view_ids.h"
[email protected]526a16b2013-06-20 11:44:33222#include "components/autofill/core/browser/autofill_ie_toolbar_import_win.h"
mfomitchevf09a66bb2014-10-02 02:20:54223#include "ui/base/touch/touch_device.h"
[email protected]dcd57762011-06-25 12:18:51224#include "ui/base/win/shell.h"
[email protected]8a53ee042009-01-21 16:41:33225#endif // OS_WIN
[email protected]e1acf6f2008-10-27 20:43:33226
[email protected]b7969202010-05-14 21:29:26227#if defined(OS_CHROMEOS)
hirono90949af2014-09-25 04:23:37228#include "chrome/browser/chromeos/fileapi/external_file_url_util.h"
[email protected]eab8c0f2011-11-18 22:33:50229#endif
230
[email protected]dc04be7c2012-03-15 23:57:49231#if defined(USE_ASH)
mswd9b1b342016-06-17 20:19:57232#include "ash/common/ash_switches.h"
thestig028978692015-10-08 07:39:40233#include "ash/shell.h"
[email protected]c2f0a892012-01-24 22:19:21234#endif
235
[email protected]8a53ee042009-01-21 16:41:33236using base::TimeDelta;
[email protected]e6e30ac2014-01-13 21:24:39237using base::UserMetricsAction;
[email protected]b87ee522012-05-18 15:16:54238using content::NativeWebKeyboardEvent;
[email protected]c5eed492012-01-04 17:07:50239using content::NavigationController;
[email protected]10f417c52011-12-28 21:04:23240using content::NavigationEntry;
[email protected]e5d549d2011-12-28 01:29:20241using content::OpenURLParams;
[email protected]3a5180ae2011-12-21 02:39:38242using content::PluginService;
[email protected]e5d549d2011-12-28 01:29:20243using content::Referrer;
[email protected]edc3af82013-12-12 21:24:07244using content::RenderWidgetHostView;
[email protected]b6583592012-01-25 19:52:33245using content::SiteInstance;
[email protected]ea049a02011-12-25 21:37:09246using content::WebContents;
[email protected]1c321ee52012-05-21 03:02:34247using extensions::Extension;
estarkd9e54fb2016-01-11 19:37:12248using security_state::SecurityStateModel;
[email protected]20c07f8e2012-05-31 08:43:14249using ui::WebDialogDelegate;
[email protected]e41d0082013-05-16 04:37:54250using web_modal::WebContentsModalDialogManager;
[email protected]a1221aea2013-11-07 01:31:30251using blink::WebWindowFeatures;
initial.commit09911bf2008-07-26 23:55:29252
[email protected]36b6dcb2008-11-12 01:19:57253///////////////////////////////////////////////////////////////////////////////
initial.commit09911bf2008-07-26 23:55:29254
[email protected]505323e22009-01-24 02:47:58255namespace {
256
[email protected]91a79232010-11-10 18:15:08257// How long we wait before updating the browser chrome while loading a page.
258const int kUIUpdateCoalescingTimeMS = 200;
259
[email protected]a4fe67012012-07-25 20:14:29260BrowserWindow* CreateBrowserWindow(Browser* browser) {
[email protected]a4fe67012012-07-25 20:14:29261 return BrowserWindow::CreateBrowserWindow(browser);
262}
263
[email protected]88c92012013-07-02 11:56:34264// Is the fast tab unload experiment enabled?
265bool IsFastTabUnloadEnabled() {
avi556c05022014-12-22 23:31:43266 return base::CommandLine::ForCurrentProcess()->HasSwitch(
267 switches::kEnableFastUnload);
[email protected]88c92012013-07-02 11:56:34268}
269
[email protected]505323e22009-01-24 02:47:58270} // namespace
271
[email protected]b35b26b32011-05-05 20:35:14272////////////////////////////////////////////////////////////////////////////////
273// Browser, CreateParams:
274
scottmg851949002016-02-09 20:09:44275Browser::CreateParams::CreateParams(Profile* profile)
[email protected]196578252012-12-06 23:54:20276 : type(TYPE_TABBED),
277 profile(profile),
[email protected]d2202e22014-04-14 20:20:53278 trusted_source(false),
[email protected]196578252012-12-06 23:54:20279 initial_show_state(ui::SHOW_STATE_DEFAULT),
280 is_session_restore(false),
scottmg851949002016-02-09 20:09:44281 window(NULL) {}
[email protected]196578252012-12-06 23:54:20282
scottmg851949002016-02-09 20:09:44283Browser::CreateParams::CreateParams(Type type, Profile* profile)
[email protected]56073dc2012-08-10 16:59:06284 : type(type),
285 profile(profile),
[email protected]d2202e22014-04-14 20:20:53286 trusted_source(false),
[email protected]a636d8e52012-02-28 15:40:41287 initial_show_state(ui::SHOW_STATE_DEFAULT),
[email protected]a4fe67012012-07-25 20:14:29288 is_session_restore(false),
scottmg851949002016-02-09 20:09:44289 window(NULL) {}
[email protected]b35b26b32011-05-05 20:35:14290
vmpstrb8aacbe2016-02-26 02:00:48291Browser::CreateParams::CreateParams(const CreateParams& other) = default;
292
[email protected]da22aa62012-04-04 18:54:35293// static
294Browser::CreateParams Browser::CreateParams::CreateForApp(
[email protected]da22aa62012-04-04 18:54:35295 const std::string& app_name,
[email protected]d2202e22014-04-14 20:20:53296 bool trusted_source,
[email protected]da22aa62012-04-04 18:54:35297 const gfx::Rect& window_bounds,
scottmg851949002016-02-09 20:09:44298 Profile* profile) {
[email protected]da22aa62012-04-04 18:54:35299 DCHECK(!app_name.empty());
300
scottmg851949002016-02-09 20:09:44301 CreateParams params(TYPE_POPUP, profile);
[email protected]da22aa62012-04-04 18:54:35302 params.app_name = app_name;
[email protected]d2202e22014-04-14 20:20:53303 params.trusted_source = trusted_source;
[email protected]da22aa62012-04-04 18:54:35304 params.initial_bounds = window_bounds;
305
306 return params;
307}
308
309// static
310Browser::CreateParams Browser::CreateParams::CreateForDevTools(
scottmg851949002016-02-09 20:09:44311 Profile* profile) {
312 CreateParams params(TYPE_POPUP, profile);
[email protected]da22aa62012-04-04 18:54:35313 params.app_name = DevToolsWindow::kDevToolsApp;
[email protected]d2202e22014-04-14 20:20:53314 params.trusted_source = true;
[email protected]da22aa62012-04-04 18:54:35315 return params;
316}
317
[email protected]20ca0382013-02-28 19:50:07318////////////////////////////////////////////////////////////////////////////////
319// Browser, InterstitialObserver:
320
321class Browser::InterstitialObserver : public content::WebContentsObserver {
322 public:
323 InterstitialObserver(Browser* browser, content::WebContents* web_contents)
324 : WebContentsObserver(web_contents),
325 browser_(browser) {
326 }
327
dcheng5dd5ff62014-10-21 12:42:38328 void DidAttachInterstitialPage() override {
[email protected]20ca0382013-02-28 19:50:07329 browser_->UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
330 }
331
dcheng5dd5ff62014-10-21 12:42:38332 void DidDetachInterstitialPage() override {
[email protected]20ca0382013-02-28 19:50:07333 browser_->UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
334 }
335
336 private:
337 Browser* browser_;
338
339 DISALLOW_COPY_AND_ASSIGN(InterstitialObserver);
340};
341
[email protected]36b6dcb2008-11-12 01:19:57342///////////////////////////////////////////////////////////////////////////////
343// Browser, Constructors, Creation, Showing:
initial.commit09911bf2008-07-26 23:55:29344
[email protected]a4fe67012012-07-25 20:14:29345Browser::Browser(const CreateParams& params)
jitendra.ksa0055532014-10-07 05:54:46346 : extension_registry_observer_(this),
347 type_(params.type),
[email protected]a4fe67012012-07-25 20:14:29348 profile_(params.profile),
[email protected]f3e99e32008-07-30 04:48:39349 window_(NULL),
[email protected]d4b2d232013-04-30 21:14:23350 tab_strip_model_delegate_(new chrome::BrowserTabStripModelDelegate(this)),
jitendra.ksa0055532014-10-07 05:54:46351 tab_strip_model_(
352 new TabStripModel(tab_strip_model_delegate_.get(), params.profile)),
[email protected]a4fe67012012-07-25 20:14:29353 app_name_(params.app_name),
[email protected]d2202e22014-04-14 20:20:53354 is_trusted_source_(params.trusted_source),
[email protected]69444cc2009-04-09 20:40:06355 cancel_download_confirmation_state_(NOT_PROMPTED),
[email protected]a4fe67012012-07-25 20:14:29356 override_bounds_(params.initial_bounds),
357 initial_show_state_(params.initial_show_state),
thomasanderson06405c52016-05-03 22:52:22358 initial_workspace_(params.initial_workspace),
[email protected]a4fe67012012-07-25 20:14:29359 is_session_restore_(params.is_session_restore),
[email protected]d4b2d232013-04-30 21:14:23360 content_setting_bubble_model_delegate_(
361 new BrowserContentSettingBubbleModelDelegate(this)),
362 toolbar_model_delegate_(new BrowserToolbarModelDelegate(this)),
blundellbde024d2015-09-30 13:47:07363 live_tab_context_(new BrowserLiveTabContext(this)),
[email protected]d4b2d232013-04-30 21:14:23364 synced_window_delegate_(new BrowserSyncedWindowDelegate(this)),
[email protected]8a5e0ca2011-08-25 06:30:47365 bookmark_bar_state_(BookmarkBar::HIDDEN),
[email protected]338416c02014-05-13 16:47:06366 command_controller_(new chrome::BrowserCommandController(this)),
[email protected]07f0f2cf2013-10-09 03:43:18367 window_has_shown_(false),
anujk.sharma62e915a2014-12-11 19:23:16368 chrome_updater_factory_(this),
mohan.reddy1f994392014-09-17 05:30:05369 weak_factory_(this) {
[email protected]3d0294ef2013-06-04 19:19:06370 // If this causes a crash then a window is being opened using a profile type
371 // that is disallowed by policy. The crash prevents the disabled window type
372 // from opening at all, but the path that triggered it should be fixed.
373 CHECK(IncognitoModePrefs::CanOpenBrowser(profile_));
[email protected]d5838dd2014-01-08 02:46:33374 CHECK(!profile_->IsGuestSession() || profile_->IsOffTheRecord())
375 << "Only off the record browser may be opened in guest mode";
mlerman7831f57d2015-05-25 11:40:15376 DCHECK(!profile_->IsSystemProfile())
377 << "The system profile should never have a real browser.";
378 // TODO(mlerman): After this hits stable channel, see if there are counts
379 // for this metric. If not, change the DCHECK above to a CHECK.
380 if (profile_->IsSystemProfile())
381 content::RecordAction(base::UserMetricsAction("BrowserForSystemProfile"));
[email protected]3d0294ef2013-06-04 19:19:06382
[email protected]88c92012013-07-02 11:56:34383 // TODO(jeremy): Move to initializer list once flag is removed.
384 if (IsFastTabUnloadEnabled())
385 fast_unload_controller_.reset(new chrome::FastUnloadController(this));
386 else
387 unload_controller_.reset(new chrome::UnloadController(this));
388
[email protected]2fc15ae2012-05-06 00:01:37389 tab_strip_model_->AddObserver(this);
390
sdefresne4d623d702016-02-03 20:07:57391 toolbar_model_.reset(new ToolbarModelImpl(toolbar_model_delegate_.get(),
392 content::kMaxURLDisplayChars));
[email protected]165fe422013-03-27 06:34:03393 search_model_.reset(new SearchModel());
[email protected]a40be8b2013-08-22 20:12:14394 search_delegate_.reset(new SearchDelegate(search_model_.get()));
[email protected]ef557022012-03-16 10:05:33395
jitendra.ksa0055532014-10-07 05:54:46396 extension_registry_observer_.Add(
397 extensions::ExtensionRegistry::Get(profile_));
[email protected]adf5a102014-07-31 12:44:06398 registrar_.Add(this,
399 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07400 content::NotificationService::AllSources());
[email protected]6b40bb582012-03-15 20:50:38401#if defined(ENABLE_THEMES)
[email protected]ffa6f592011-06-24 22:03:57402 registrar_.Add(
[email protected]432115822011-07-10 15:52:27403 this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
[email protected]6c2381d2011-10-19 02:52:53404 content::Source<ThemeService>(
405 ThemeServiceFactory::GetForProfile(profile_)));
[email protected]6b40bb582012-03-15 20:50:38406#endif
[email protected]ea049a02011-12-25 21:37:09407 registrar_.Add(this, chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
[email protected]ad50def52011-10-19 23:17:07408 content::NotificationService::AllSources());
initial.commit09911bf2008-07-26 23:55:29409
[email protected]97b46382011-05-02 13:23:04410 profile_pref_registrar_.Init(profile_->GetPrefs());
[email protected]c31813a2012-11-15 18:47:26411 profile_pref_registrar_.Add(
412 prefs::kDevToolsDisabled,
413 base::Bind(&Browser::OnDevToolsDisabledChanged, base::Unretained(this)));
414 profile_pref_registrar_.Add(
tfarina3bddbe112014-08-28 05:29:32415 bookmarks::prefs::kShowBookmarkBar,
[email protected]c31813a2012-11-15 18:47:26416 base::Bind(&Browser::UpdateBookmarkBarState, base::Unretained(this),
417 BOOKMARK_BAR_STATE_CHANGE_PREF_CHANGE));
[email protected]9e823662010-10-13 23:36:00418
[email protected]97b46382011-05-02 13:23:04419 // NOTE: These prefs all need to be explicitly destroyed in the destructor
420 // or you'll get a nasty surprise when you run the incognito tests.
initial.commit09911bf2008-07-26 23:55:29421 encoding_auto_detect_.Init(prefs::kWebKitUsesUniversalDetector,
[email protected]96a5c342012-12-04 18:14:02422 profile_->GetPrefs());
[email protected]281918912010-05-27 22:05:13423
sdefresne51bbec7b2015-08-03 14:18:13424 if (search::IsInstantExtendedAPIEnabled() && is_type_tabbed())
[email protected]165fe422013-03-27 06:34:03425 instant_controller_.reset(new BrowserInstantController(this));
[email protected]df25eb9f2012-09-13 17:43:23426
benwells11bd6ed2015-06-05 04:20:41427 if (extensions::HostedAppBrowserController::IsForHostedApp(this)) {
428 hosted_app_controller_.reset(
429 new extensions::HostedAppBrowserController(this));
benwellsa2a8a9f72015-04-07 07:09:54430 }
431
[email protected]09b29342011-06-24 19:18:48432 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_INIT);
[email protected]a7be6982011-12-12 21:53:57433
[email protected]26e92df2013-04-11 16:05:21434 ProfileMetrics::LogProfileLaunch(profile_);
[email protected]a4fe67012012-07-25 20:14:29435
436 window_ = params.window ? params.window : CreateBrowserWindow(this);
437
benwells11bd6ed2015-06-05 04:20:41438 if (hosted_app_controller_)
439 hosted_app_controller_->UpdateLocationBarVisibility(false);
benwellsa2a8a9f72015-04-07 07:09:54440
[email protected]a4fe67012012-07-25 20:14:29441 // Create the extension window controller before sending notifications.
442 extension_window_controller_.reset(
443 new BrowserExtensionWindowController(this));
444
[email protected]7d52ea4c62013-11-13 03:52:14445 SessionService* session_service =
446 SessionServiceFactory::GetForProfileForSessionRestore(profile_);
447 if (session_service)
448 session_service->WindowOpened(this);
449
[email protected]a4fe67012012-07-25 20:14:29450 // TODO(beng): move to ChromeBrowserMain:
[email protected]dfcc05f2013-02-21 14:12:16451 if (first_run::ShouldDoPersonalDataManagerFirstRun()) {
[email protected]a4fe67012012-07-25 20:14:29452#if defined(OS_WIN)
[email protected]dfcc05f2013-02-21 14:12:16453 // Notify PDM that this is a first run.
[email protected]bfd4b812013-04-11 02:02:11454 ImportAutofillDataWin(
455 autofill::PersonalDataManagerFactory::GetForProfile(profile_));
[email protected]a4fe67012012-07-25 20:14:29456#endif // defined(OS_WIN)
[email protected]a4fe67012012-07-25 20:14:29457 }
458
sriramsr39b6b7872015-03-04 00:03:32459 exclusive_access_manager_.reset(
460 new ExclusiveAccessManager(window_->GetExclusiveAccessContext()));
[email protected]010152f2014-07-15 00:16:47461
jackhou75d58b12015-07-07 08:16:01462 // TODO(beng): Move BrowserList::AddBrowser() to the end of this function and
463 // replace uses of this with BL's notifications.
464 BrowserList::AddBrowser(this);
465 content::NotificationService::current()->Notify(
466 chrome::NOTIFICATION_BROWSER_WINDOW_READY, content::Source<Browser>(this),
467 content::NotificationService::NoDetails());
initial.commit09911bf2008-07-26 23:55:29468}
469
470Browser::~Browser() {
[email protected]ebe07772014-05-22 04:16:06471 // Stop observing notifications before continuing with destruction. Profile
472 // destruction will unload extensions and reentrant calls to Browser:: should
473 // be avoided while it is being torn down.
474 registrar_.RemoveAll();
jitendra.ksa0055532014-10-07 05:54:46475 extension_registry_observer_.RemoveAll();
[email protected]ebe07772014-05-22 04:16:06476
[email protected]2fc15ae2012-05-06 00:01:37477 // The tab strip should not have any tabs at this point.
[email protected]050e77182014-03-25 15:29:16478 DCHECK(tab_strip_model_->empty());
[email protected]2fc15ae2012-05-06 00:01:37479 tab_strip_model_->RemoveObserver(this);
hcarmonaaa431c02015-08-28 18:45:57480 bubble_manager_.reset();
[email protected]2fc15ae2012-05-06 00:01:37481
[email protected]557c4e22012-12-18 18:30:55482 // Destroy the BrowserCommandController before removing the browser, so that
483 // it doesn't act on any notifications that are sent as a result of removing
484 // the browser.
485 command_controller_.reset();
initial.commit09911bf2008-07-26 23:55:29486 BrowserList::RemoveBrowser(this);
487
[email protected]bdc6ccd12012-03-20 22:06:13488 SessionService* session_service =
489 SessionServiceFactory::GetForProfile(profile_);
490 if (session_service)
491 session_service->WindowClosed(session_id_);
492
blundell74001adc2015-09-18 11:04:25493 sessions::TabRestoreService* tab_restore_service =
[email protected]bdc6ccd12012-03-20 22:06:13494 TabRestoreServiceFactory::GetForProfile(profile());
495 if (tab_restore_service)
blundellbde024d2015-09-30 13:47:07496 tab_restore_service->BrowserClosed(live_tab_context());
[email protected]bdc6ccd12012-03-20 22:06:13497
[email protected]97b46382011-05-02 13:23:04498 profile_pref_registrar_.RemoveAll();
[email protected]97b46382011-05-02 13:23:04499
[email protected]9a8c4022011-01-25 14:25:33500 encoding_auto_detect_.Destroy();
[email protected]9a8c4022011-01-25 14:25:33501
[email protected]fb0f9ebc2013-05-22 06:25:42502 // Destroy BrowserExtensionWindowController before the incognito profile
503 // is destroyed to make sure the chrome.windows.onRemoved event is sent.
504 extension_window_controller_.reset();
505
[email protected]6af41782013-06-22 13:49:11506 // Destroy BrowserInstantController before the incongnito profile is destroyed
507 // because the InstantController destructor depends on this profile.
508 instant_controller_.reset();
509
initial.commit09911bf2008-07-26 23:55:29510 if (profile_->IsOffTheRecord() &&
trizzofo49b95f52016-07-02 00:26:13511 !BrowserList::IsIncognitoSessionActiveForProfile(profile_)) {
mlerman67fa17a2015-02-13 21:59:22512 if (profile_->IsGuestSession()) {
513// ChromeOS handles guest data independently.
514#if !defined(OS_CHROMEOS)
515 // Clear all browsing data once a Guest Session completes. The Guest
516 // profile has BrowserContextKeyedServices that the Incognito profile
517 // doesn't, so the ProfileDestroyer can't delete it properly.
518 // TODO(mlerman): Delete the guest using an improved ProfileDestroyer.
mlerman2c1c3782015-04-23 18:37:43519 profiles::RemoveBrowsingDataForProfile(profile_->GetPath());
mlerman67fa17a2015-02-13 21:59:22520#endif
521 } else {
522 // An incognito profile is no longer needed, this indirectly frees
523 // its cache and cookies once it gets destroyed at the appropriate time.
524 ProfileDestroyer::DestroyProfileWhenAppropriate(profile_);
525 }
initial.commit09911bf2008-07-26 23:55:29526 }
527
528 // There may be pending file dialogs, we need to tell them that we've gone
529 // away so they don't try and call back to us.
530 if (select_file_dialog_.get())
531 select_file_dialog_->ListenerDestroyed();
[email protected]5fec4702013-11-20 04:37:38532
533 int num_downloads;
534 if (OkToCloseWithInProgressDownloads(&num_downloads) ==
535 DOWNLOAD_CLOSE_BROWSER_SHUTDOWN &&
536 !browser_defaults::kBrowserAliveWithNoWindows) {
537 DownloadService::CancelAllDownloads();
538 }
initial.commit09911bf2008-07-26 23:55:29539}
540
[email protected]36b6dcb2008-11-12 01:19:57541///////////////////////////////////////////////////////////////////////////////
[email protected]5c238752009-06-13 10:29:07542// Getters & Setters
543
hcarmonaaa431c02015-08-28 18:45:57544ChromeBubbleManager* Browser::GetBubbleManager() {
545 if (!bubble_manager_)
546 bubble_manager_.reset(new ChromeBubbleManager(tab_strip_model_.get()));
547 return bubble_manager_.get();
548}
549
[email protected]b77cb302009-10-29 04:09:17550FindBarController* Browser::GetFindBarController() {
551 if (!find_bar_controller_.get()) {
[email protected]632983f2011-08-08 22:51:24552 FindBar* find_bar = window_->CreateFindBar();
[email protected]b77cb302009-10-29 04:09:17553 find_bar_controller_.reset(new FindBarController(find_bar));
554 find_bar->SetFindBarController(find_bar_controller_.get());
[email protected]0d56fa7212012-11-15 23:34:02555 find_bar_controller_->ChangeWebContents(
556 tab_strip_model_->GetActiveWebContents());
andresantoso6a7691b2014-12-10 23:37:41557 find_bar_controller_->find_bar()->MoveWindowIfNecessary(gfx::Rect());
[email protected]b77cb302009-10-29 04:09:17558 }
559 return find_bar_controller_.get();
560}
561
[email protected]24db8a072009-10-29 20:35:37562bool Browser::HasFindBarController() const {
563 return find_bar_controller_.get() != NULL;
564}
565
[email protected]b35b26b32011-05-05 20:35:14566bool Browser::is_app() const {
567 return !app_name_.empty();
568}
569
570bool Browser::is_devtools() const {
571 return app_name_ == DevToolsWindow::kDevToolsApp;
572}
573
[email protected]5c238752009-06-13 10:29:07574///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:57575// Browser, State Storage and Retrieval for UI:
576
[email protected]9b125b732012-08-17 04:43:55577gfx::Image Browser::GetCurrentPageIcon() const {
[email protected]0d56fa7212012-11-15 23:34:02578 WebContents* web_contents = tab_strip_model_->GetActiveWebContents();
[email protected]502b5612012-10-02 01:33:10579 // |web_contents| can be NULL since GetCurrentPageIcon() is called by the
580 // window during the window's creation (before tabs have been added).
sdefresne455826972015-04-10 15:25:15581 favicon::FaviconDriver* favicon_driver =
582 web_contents
583 ? favicon::ContentFaviconDriver::FromWebContents(web_contents)
584 : nullptr;
585 return favicon_driver ? favicon_driver->GetFavicon() : gfx::Image();
[email protected]36b6dcb2008-11-12 01:19:57586}
587
varkha48f84c4b2016-06-27 18:10:33588base::string16 Browser::GetWindowTitleForCurrentTab(
589 bool include_app_name) const {
[email protected]0d56fa7212012-11-15 23:34:02590 WebContents* contents = tab_strip_model_->GetActiveWebContents();
[email protected]dcd0249872013-12-06 23:58:45591 base::string16 title;
[email protected]c7c42332008-11-15 01:10:54592
[email protected]731f8a42009-07-20 22:07:32593 // |contents| can be NULL because GetWindowTitleForCurrentTab is called by the
594 // window during the window's creation (before tabs have been added).
[email protected]36b6dcb2008-11-12 01:19:57595 if (contents) {
benwells502df692014-12-02 20:41:45596 // The web app frame uses the host instead of the title.
597 if (ShouldUseWebAppFrame())
[email protected]f53ec988d2014-08-22 01:38:09598 return base::UTF8ToUTF16(contents->GetURL().host());
[email protected]f53ec988d2014-08-22 01:38:09599
[email protected]dda5dfa32009-07-10 00:33:29600 title = contents->GetTitle();
[email protected]36b6dcb2008-11-12 01:19:57601 FormatTitleForDisplay(&title);
602 }
603 if (title.empty())
[email protected]51d83bdf2011-12-09 16:09:29604 title = CoreTabHelper::GetDefaultTitle();
[email protected]36b6dcb2008-11-12 01:19:57605
varkha48f84c4b2016-06-27 18:10:33606#if defined(OS_MACOSX)
607 // On Mac, we don't want to suffix the page title with the application name.
[email protected]d904bc82009-07-07 00:41:04608 return title;
[email protected]82b9ed82012-11-15 02:43:18609#endif
varkha48f84c4b2016-06-27 18:10:33610 // Include the app name in window titles for tabbed browser windows when
611 // requested with |include_app_name|.
612 return (!is_app() && include_app_name) ?
613 l10n_util::GetStringFUTF16(IDS_BROWSER_WINDOW_TITLE_FORMAT, title):
614 title;
[email protected]36b6dcb2008-11-12 01:19:57615}
616
617// static
[email protected]dcd0249872013-12-06 23:58:45618void Browser::FormatTitleForDisplay(base::string16* title) {
[email protected]36b6dcb2008-11-12 01:19:57619 size_t current_index = 0;
620 size_t match_index;
[email protected]dcd0249872013-12-06 23:58:45621 while ((match_index = title->find(L'\n', current_index)) !=
622 base::string16::npos) {
623 title->replace(match_index, 1, base::string16());
[email protected]36b6dcb2008-11-12 01:19:57624 current_index = match_index;
625 }
626}
627
628///////////////////////////////////////////////////////////////////////////////
629// Browser, OnBeforeUnload handling:
630
631bool Browser::ShouldCloseWindow() {
[email protected]69444cc2009-04-09 20:40:06632 if (!CanCloseWithInProgressDownloads())
633 return false;
634
[email protected]88c92012013-07-02 11:56:34635 if (IsFastTabUnloadEnabled())
636 return fast_unload_controller_->ShouldCloseWindow();
[email protected]f9b6c092012-07-09 19:32:28637 return unload_controller_->ShouldCloseWindow();
638}
[email protected]69444cc2009-04-09 20:40:06639
[email protected]2e9d79f2013-08-16 05:45:56640bool Browser::CallBeforeUnloadHandlers(
641 const base::Callback<void(bool)>& on_close_confirmed) {
[email protected]edfca702013-08-16 08:58:14642 cancel_download_confirmation_state_ = RESPONSE_RECEIVED;
[email protected]2e9d79f2013-08-16 05:45:56643 if (IsFastTabUnloadEnabled()) {
644 return fast_unload_controller_->CallBeforeUnloadHandlers(
645 on_close_confirmed);
646 }
647 return unload_controller_->CallBeforeUnloadHandlers(on_close_confirmed);
648}
649
650void Browser::ResetBeforeUnloadHandlers() {
[email protected]edfca702013-08-16 08:58:14651 cancel_download_confirmation_state_ = NOT_PROMPTED;
[email protected]2e9d79f2013-08-16 05:45:56652 if (IsFastTabUnloadEnabled())
653 fast_unload_controller_->ResetBeforeUnloadHandlers();
654 else
655 unload_controller_->ResetBeforeUnloadHandlers();
656}
657
[email protected]88c92012013-07-02 11:56:34658bool Browser::HasCompletedUnloadProcessing() const {
659 DCHECK(IsFastTabUnloadEnabled());
660 return fast_unload_controller_->HasCompletedUnloadProcessing();
661}
662
[email protected]f9b6c092012-07-09 19:32:28663bool Browser::IsAttemptingToCloseBrowser() const {
[email protected]88c92012013-07-02 11:56:34664 if (IsFastTabUnloadEnabled())
665 return fast_unload_controller_->is_attempting_to_close_browser();
[email protected]f9b6c092012-07-09 19:32:28666 return unload_controller_->is_attempting_to_close_browser();
[email protected]36b6dcb2008-11-12 01:19:57667}
668
changwan6ed4d432016-05-19 22:03:54669bool Browser::ShouldRunUnloadListenerBeforeClosing(
670 content::WebContents* web_contents) {
671 if (IsFastTabUnloadEnabled())
672 return fast_unload_controller_->ShouldRunUnloadEventsHelper(web_contents);
673 return unload_controller_->ShouldRunUnloadEventsHelper(web_contents);
674}
675
676bool Browser::RunUnloadListenerBeforeClosing(
677 content::WebContents* web_contents) {
678 if (IsFastTabUnloadEnabled())
679 return fast_unload_controller_->RunUnloadEventsHelper(web_contents);
680 return unload_controller_->RunUnloadEventsHelper(web_contents);
681}
682
[email protected]36b6dcb2008-11-12 01:19:57683void Browser::OnWindowClosing() {
684 if (!ShouldCloseWindow())
685 return;
686
[email protected]c984d9f2010-07-20 20:52:20687 // Application should shutdown on last window close if the user is explicitly
688 // trying to quit, or if there is nothing keeping the browser alive (such as
689 // AppController on the Mac, or BackgroundContentsService for background
690 // pages).
691 bool should_quit_if_last_browser =
dgn02377782016-03-12 00:58:38692 browser_shutdown::IsTryingToQuit() ||
693 !KeepAliveRegistry::GetInstance()->IsKeepingAlive();
[email protected]7dc8c6b2010-04-09 17:02:50694
thestig028978692015-10-08 07:39:40695 if (should_quit_if_last_browser && ShouldStartShutdown())
[email protected]36b6dcb2008-11-12 01:19:57696 browser_shutdown::OnShutdownStarting(browser_shutdown::WINDOW_CLOSE);
697
[email protected]92371eb2011-04-28 11:50:15698 // Don't use GetForProfileIfExisting here, we want to force creation of the
[email protected]36b6dcb2008-11-12 01:19:57699 // session service so that user can restore what was open.
[email protected]92371eb2011-04-28 11:50:15700 SessionService* session_service =
701 SessionServiceFactory::GetForProfile(profile());
[email protected]36b6dcb2008-11-12 01:19:57702 if (session_service)
703 session_service->WindowClosing(session_id());
704
blundell74001adc2015-09-18 11:04:25705 sessions::TabRestoreService* tab_restore_service =
[email protected]92371eb2011-04-28 11:50:15706 TabRestoreServiceFactory::GetForProfile(profile());
[email protected]a636d8e52012-02-28 15:40:41707
708#if defined(USE_AURA)
[email protected]d019fa582013-11-04 23:45:49709 if (tab_restore_service && is_app() && !is_devtools())
blundellbde024d2015-09-30 13:47:07710 tab_restore_service->BrowserClosing(live_tab_context());
[email protected]a636d8e52012-02-28 15:40:41711#endif
712
[email protected]e0448872013-01-11 19:35:02713 if (tab_restore_service && is_type_tabbed() && tab_strip_model_->count())
blundellbde024d2015-09-30 13:47:07714 tab_restore_service->BrowserClosing(live_tab_context());
[email protected]d8375fd2008-11-25 22:45:39715
[email protected]911f8152010-03-18 16:46:40716 // TODO(sky): convert session/tab restore to use notification.
[email protected]ad50def52011-10-19 23:17:07717 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27718 chrome::NOTIFICATION_BROWSER_CLOSING,
[email protected]6c2381d2011-10-19 02:52:53719 content::Source<Browser>(this),
[email protected]1e0c7402012-07-27 10:48:15720 content::NotificationService::NoDetails());
[email protected]ed1bb7202013-06-21 05:16:44721
[email protected]88c92012013-07-02 11:56:34722 if (!IsFastTabUnloadEnabled())
723 tab_strip_model_->CloseAllTabs();
[email protected]36b6dcb2008-11-12 01:19:57724}
725
[email protected]69444cc2009-04-09 20:40:06726////////////////////////////////////////////////////////////////////////////////
727// In-progress download termination handling:
728
729void Browser::InProgressDownloadResponse(bool cancel_downloads) {
730 if (cancel_downloads) {
731 cancel_download_confirmation_state_ = RESPONSE_RECEIVED;
[email protected]a37d4b02012-06-25 21:56:10732 chrome::CloseWindow(this);
[email protected]69444cc2009-04-09 20:40:06733 return;
734 }
735
736 // Sets the confirmation state to NOT_PROMPTED so that if the user tries to
737 // close again we'll show the warning again.
738 cancel_download_confirmation_state_ = NOT_PROMPTED;
739
740 // Show the download page so the user can figure-out what downloads are still
741 // in-progress.
[email protected]5d9cace72012-06-21 16:07:12742 chrome::ShowDownloads(this);
[email protected]674b65fb2014-02-19 02:43:42743
744 // Reset UnloadController::is_attempting_to_close_browser_ so that we don't
745 // prompt every time any tab is closed. http://crbug.com/305516
746 if (IsFastTabUnloadEnabled())
747 fast_unload_controller_->CancelWindowClose();
748 else
749 unload_controller_->CancelWindowClose();
[email protected]69444cc2009-04-09 20:40:06750}
751
[email protected]5a17d492011-10-24 19:37:03752Browser::DownloadClosePreventionType Browser::OkToCloseWithInProgressDownloads(
753 int* num_downloads_blocking) const {
754 DCHECK(num_downloads_blocking);
755 *num_downloads_blocking = 0;
756
[email protected]5a17d492011-10-24 19:37:03757 // If we're not running a full browser process with a profile manager
758 // (testing), it's ok to close the browser.
759 if (!g_browser_process->profile_manager())
760 return DOWNLOAD_CLOSE_OK;
761
[email protected]422a7d12013-10-21 12:10:42762 int total_download_count =
763 DownloadService::NonMaliciousDownloadCountAllProfiles();
[email protected]5a17d492011-10-24 19:37:03764 if (total_download_count == 0)
765 return DOWNLOAD_CLOSE_OK; // No downloads; can definitely close.
766
767 // Figure out how many windows are open total, and associated with this
768 // profile, that are relevant for the ok-to-close decision.
769 int profile_window_count = 0;
770 int total_window_count = 0;
scottmg8abbff832016-01-28 22:57:37771 for (auto* browser : *BrowserList::GetInstance()) {
[email protected]5a17d492011-10-24 19:37:03772 // Don't count this browser window or any other in the process of closing.
[email protected]f9b6c092012-07-09 19:32:28773 // Window closing may be delayed, and windows that are in the process of
774 // closing don't count against our totals.
775 if (browser == this || browser->IsAttemptingToCloseBrowser())
[email protected]5a17d492011-10-24 19:37:03776 continue;
777
scottmg8abbff832016-01-28 22:57:37778 if (browser->profile() == profile())
[email protected]5a17d492011-10-24 19:37:03779 profile_window_count++;
780 total_window_count++;
781 }
782
783 // If there aren't any other windows, we're at browser shutdown,
784 // which would cancel all current downloads.
785 if (total_window_count == 0) {
786 *num_downloads_blocking = total_download_count;
787 return DOWNLOAD_CLOSE_BROWSER_SHUTDOWN;
788 }
789
790 // If there aren't any other windows on our profile, and we're an incognito
791 // profile, and there are downloads associated with that profile,
792 // those downloads would be cancelled by our window (-> profile) close.
793 DownloadService* download_service =
[email protected]423939d2013-07-31 20:00:08794 DownloadServiceFactory::GetForBrowserContext(profile());
[email protected]d07d63fc2013-07-18 19:44:54795 if ((profile_window_count == 0) &&
[email protected]422a7d12013-10-21 12:10:42796 (download_service->NonMaliciousDownloadCount() > 0) &&
[email protected]5a17d492011-10-24 19:37:03797 profile()->IsOffTheRecord()) {
[email protected]422a7d12013-10-21 12:10:42798 *num_downloads_blocking = download_service->NonMaliciousDownloadCount();
[email protected]5a17d492011-10-24 19:37:03799 return DOWNLOAD_CLOSE_LAST_WINDOW_IN_INCOGNITO_PROFILE;
800 }
801
802 // Those are the only conditions under which we will block shutdown.
803 return DOWNLOAD_CLOSE_OK;
804}
805
[email protected]69444cc2009-04-09 20:40:06806////////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:57807// Browser, Tab adding/showing functions:
808
[email protected]227393e2011-06-29 21:28:39809void Browser::WindowFullscreenStateChanged() {
sriramsra41db562015-01-26 22:45:04810 exclusive_access_manager_->fullscreen_controller()
811 ->WindowFullscreenStateChanged();
[email protected]5d98294912012-06-27 22:57:40812 command_controller_->FullscreenStateChanged();
[email protected]9adb8b12011-12-09 06:49:13813 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TOGGLE_FULLSCREEN);
[email protected]ae2622c2009-07-30 23:47:58814}
815
[email protected]36b6dcb2008-11-12 01:19:57816///////////////////////////////////////////////////////////////////////////////
817// Browser, Assorted browser commands:
818
[email protected]41d9faf2012-02-28 23:46:02819void Browser::ToggleFullscreenModeWithExtension(const GURL& extension_url) {
sriramsra41db562015-01-26 22:45:04820 exclusive_access_manager_->fullscreen_controller()
821 ->ToggleBrowserFullscreenModeWithExtension(extension_url);
[email protected]9282cea2009-02-18 18:49:00822}
823
[email protected]a3e18c42009-03-04 23:36:05824bool Browser::SupportsWindowFeature(WindowFeature feature) const {
[email protected]018cf362010-05-05 22:43:22825 return SupportsWindowFeatureImpl(feature, true);
826}
[email protected]f5bf8ccf2010-02-05 18:19:25827
[email protected]018cf362010-05-05 22:43:22828bool Browser::CanSupportWindowFeature(WindowFeature feature) const {
829 return SupportsWindowFeatureImpl(feature, false);
[email protected]a3e18c42009-03-04 23:36:05830}
831
[email protected]fbd77592008-11-12 20:50:27832void Browser::ToggleEncodingAutoDetect() {
[email protected]7f6f44c2011-12-14 13:23:38833 content::RecordAction(UserMetricsAction("AutoDetectChange"));
[email protected]fbd77592008-11-12 20:50:27834 encoding_auto_detect_.SetValue(!encoding_auto_detect_.GetValue());
[email protected]a697f4c2009-09-14 22:30:18835 // If "auto detect" is turned on, then any current override encoding
836 // is cleared. This also implicitly performs a reload.
837 // OTOH, if "auto detect" is turned off, we don't change the currently
838 // active encoding.
839 if (encoding_auto_detect_.GetValue()) {
[email protected]0d56fa7212012-11-15 23:34:02840 WebContents* contents = tab_strip_model_->GetActiveWebContents();
[email protected]a697f4c2009-09-14 22:30:18841 if (contents)
[email protected]b2fe07d12010-02-09 14:38:08842 contents->ResetOverrideEncoding();
[email protected]a697f4c2009-09-14 22:30:18843 }
[email protected]fbd77592008-11-12 20:50:27844}
845
846void Browser::OverrideEncoding(int encoding_id) {
[email protected]7f6f44c2011-12-14 13:23:38847 content::RecordAction(UserMetricsAction("OverrideEncoding"));
[email protected]41fc0322009-09-04 22:23:40848 const std::string selected_encoding =
[email protected]fbd77592008-11-12 20:50:27849 CharacterEncoding::GetCanonicalEncodingNameByCommandId(encoding_id);
[email protected]0d56fa7212012-11-15 23:34:02850 WebContents* contents = tab_strip_model_->GetActiveWebContents();
[email protected]57c6a652009-05-04 07:58:34851 if (!selected_encoding.empty() && contents)
[email protected]b2fe07d12010-02-09 14:38:08852 contents->SetOverrideEncoding(selected_encoding);
[email protected]fbd77592008-11-12 20:50:27853 // Update the list of recently selected encodings.
[email protected]41fc0322009-09-04 22:23:40854 std::string new_selected_encoding_list;
[email protected]ddd231e2010-06-29 20:35:19855 if (CharacterEncoding::UpdateRecentlySelectedEncoding(
856 profile_->GetPrefs()->GetString(prefs::kRecentlySelectedEncoding),
[email protected]41fc0322009-09-04 22:23:40857 encoding_id,
858 &new_selected_encoding_list)) {
[email protected]fbd77592008-11-12 20:50:27859 profile_->GetPrefs()->SetString(prefs::kRecentlySelectedEncoding,
[email protected]ddd231e2010-06-29 20:35:19860 new_selected_encoding_list);
[email protected]fbd77592008-11-12 20:50:27861 }
[email protected]36b6dcb2008-11-12 01:19:57862}
863
[email protected]84214982008-12-10 18:49:10864void Browser::OpenFile() {
[email protected]7f6f44c2011-12-14 13:23:38865 content::RecordAction(UserMetricsAction("OpenFile"));
[email protected]92f54082012-07-31 01:43:14866 select_file_dialog_ = ui::SelectFileDialog::Create(
[email protected]6e1fcd12012-07-02 17:14:20867 this, new ChromeSelectFilePolicy(
[email protected]0d56fa7212012-11-15 23:34:02868 tab_strip_model_->GetActiveWebContents()));
[email protected]84214982008-12-10 18:49:10869
[email protected]650b2d52013-02-10 03:41:45870 const base::FilePath directory = profile_->last_selected_directory();
[email protected]35896a32010-06-09 08:42:51871
[email protected]84214982008-12-10 18:49:10872 // TODO(beng): figure out how to juggle this.
[email protected]90556dd2012-06-07 20:26:18873 gfx::NativeWindow parent_window = window_->GetNativeWindow();
[email protected]479cce782012-09-15 20:15:53874 ui::SelectFileDialog::FileTypeInfo file_types;
hirono4d4d3392016-02-04 05:03:50875 file_types.allowed_paths = ui::SelectFileDialog::FileTypeInfo::ANY_PATH;
[email protected]92f54082012-07-31 01:43:14876 select_file_dialog_->SelectFile(ui::SelectFileDialog::SELECT_OPEN_FILE,
[email protected]dcd0249872013-12-06 23:58:45877 base::string16(),
[email protected]007b3f82013-04-09 08:46:45878 directory,
879 &file_types,
880 0,
881 base::FilePath::StringType(),
882 parent_window,
883 NULL);
[email protected]84214982008-12-10 18:49:10884}
885
[email protected]92655362012-04-20 21:33:17886void Browser::UpdateDownloadShelfVisibility(bool visible) {
887 if (GetStatusBubble())
888 GetStatusBubble()->UpdateDownloadShelfVisibility(visible);
889}
890
[email protected]36b6dcb2008-11-12 01:19:57891///////////////////////////////////////////////////////////////////////////////
892
[email protected]e232c992012-12-06 12:43:20893void Browser::UpdateUIForNavigationInTab(WebContents* contents,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35894 ui::PageTransition transition,
[email protected]ef557022012-03-16 10:05:33895 bool user_initiated) {
[email protected]e232c992012-12-06 12:43:20896 tab_strip_model_->TabNavigating(contents, transition);
[email protected]ef557022012-03-16 10:05:33897
[email protected]0d56fa7212012-11-15 23:34:02898 bool contents_is_selected =
[email protected]e232c992012-12-06 12:43:20899 contents == tab_strip_model_->GetActiveWebContents();
[email protected]ef557022012-03-16 10:05:33900 if (user_initiated && contents_is_selected && window()->GetLocationBar()) {
901 // Forcibly reset the location bar if the url is going to change in the
902 // current tab, since otherwise it won't discard any ongoing user edits,
903 // since it doesn't realize this is a user-initiated action.
904 window()->GetLocationBar()->Revert();
905 }
906
907 if (GetStatusBubble())
908 GetStatusBubble()->Hide();
909
910 // Update the location bar. This is synchronous. We specifically don't
911 // update the load state since the load hasn't started yet and updating it
912 // will put it out of sync with the actual state like whether we're
913 // displaying a favicon, which controls the throbber. If we updated it here,
914 // the throbber will show the default favicon for a split second when
915 // navigating away from the new tab page.
[email protected]e232c992012-12-06 12:43:20916 ScheduleUIUpdate(contents, content::INVALIDATE_TYPE_URL);
[email protected]ef557022012-03-16 10:05:33917
[email protected]e1f288d2012-05-23 19:36:10918 if (contents_is_selected)
[email protected]fc2b46b2014-05-03 16:33:45919 contents->SetInitialFocus();
[email protected]ef557022012-03-16 10:05:33920}
921
anthonyvde659ee92016-02-04 23:16:56922void Browser::ShowModalSigninWindow(profiles::BubbleViewMode mode,
923 signin_metrics::AccessPoint access_point) {
924 signin_view_controller_.ShowModalSignin(mode, this, access_point);
925}
926
927void Browser::CloseModalSigninWindow() {
928 signin_view_controller_.CloseModalSignin();
929}
930
931void Browser::ShowModalSyncConfirmationWindow() {
932 signin_view_controller_.ShowModalSyncConfirmationDialog(this);
933}
934
dgnfe075c82016-03-18 11:25:35935void Browser::RegisterKeepAlive() {
936 keep_alive_.reset(new ScopedKeepAlive(KeepAliveOrigin::BROWSER,
937 KeepAliveRestartOption::DISABLED));
938}
939void Browser::UnregisterKeepAlive() {
940 keep_alive_.reset();
941}
942
[email protected]ef557022012-03-16 10:05:33943///////////////////////////////////////////////////////////////////////////////
944// Browser, PageNavigator implementation:
945
946WebContents* Browser::OpenURL(const OpenURLParams& params) {
947 return OpenURLFromTab(NULL, params);
948}
949
[email protected]485fba42009-03-24 23:27:29950///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:57951// Browser, TabStripModelObserver implementation:
952
[email protected]409ea2972012-11-10 19:54:43953void Browser::TabInsertedAt(WebContents* contents,
[email protected]36b6dcb2008-11-12 01:19:57954 int index,
955 bool foreground) {
[email protected]63d1f9b2014-07-05 19:09:03956 SetAsDelegate(contents, true);
[email protected]010152f2014-07-15 00:16:47957
[email protected]50a9a76b2012-09-12 14:19:36958 SessionTabHelper* session_tab_helper =
[email protected]409ea2972012-11-10 19:54:43959 SessionTabHelper::FromWebContents(contents);
[email protected]50a9a76b2012-09-12 14:19:36960 session_tab_helper->SetWindowID(session_id());
[email protected]e524ff92011-03-30 13:26:58961
[email protected]fb312eb2012-09-24 23:21:10962 content::NotificationService::current()->Notify(
963 chrome::NOTIFICATION_TAB_PARENTED,
[email protected]409ea2972012-11-10 19:54:43964 content::Source<content::WebContents>(contents),
[email protected]fb312eb2012-09-24 23:21:10965 content::NotificationService::NoDetails());
966
[email protected]c37e3b62010-01-22 00:15:17967 SyncHistoryWithTabs(index);
[email protected]36b6dcb2008-11-12 01:19:57968
[email protected]159f7762008-12-19 14:58:27969 // Make sure the loading state is updated correctly, otherwise the throbber
970 // won't start if the page is loading.
[email protected]e3b10d12014-03-28 16:06:09971 LoadingStateChanged(contents, true);
[email protected]159f7762008-12-19 14:58:27972
[email protected]20ca0382013-02-28 19:50:07973 interstitial_observers_.push_back(new InterstitialObserver(this, contents));
[email protected]e397a4472011-12-21 21:47:50974
[email protected]40600152012-09-26 10:04:31975 SessionService* session_service =
976 SessionServiceFactory::GetForProfile(profile_);
[email protected]d4c9caa2013-04-22 18:30:32977 if (session_service) {
[email protected]409ea2972012-11-10 19:54:43978 session_service->TabInserted(contents);
[email protected]d4c9caa2013-04-22 18:30:32979 int new_active_index = tab_strip_model_->active_index();
980 if (index < new_active_index)
981 session_service->SetSelectedTabInWindow(session_id(),
982 new_active_index);
983 }
[email protected]36b6dcb2008-11-12 01:19:57984}
985
[email protected]f2cc7382010-10-15 18:03:05986void Browser::TabClosingAt(TabStripModel* tab_strip_model,
[email protected]0be097d2012-11-10 00:20:16987 WebContents* contents,
[email protected]f2cc7382010-10-15 18:03:05988 int index) {
sriramsra41db562015-01-26 22:45:04989 exclusive_access_manager_->OnTabClosing(contents);
[email protected]40600152012-09-26 10:04:31990 SessionService* session_service =
991 SessionServiceFactory::GetForProfile(profile_);
992 if (session_service)
[email protected]0be097d2012-11-10 00:20:16993 session_service->TabClosing(contents);
[email protected]ad50def52011-10-19 23:17:07994 content::NotificationService::current()->Notify(
[email protected]884033e2012-04-16 19:38:42995 chrome::NOTIFICATION_TAB_CLOSING,
[email protected]0be097d2012-11-10 00:20:16996 content::Source<NavigationController>(&contents->GetController()),
[email protected]ad50def52011-10-19 23:17:07997 content::NotificationService::NoDetails());
[email protected]36b6dcb2008-11-12 01:19:57998
[email protected]075969d2012-04-17 20:05:55999 // Sever the WebContents' connection back to us.
[email protected]63d1f9b2014-07-05 19:09:031000 SetAsDelegate(contents, false);
[email protected]36b6dcb2008-11-12 01:19:571001}
1002
[email protected]e89cfcb2012-11-11 14:47:241003void Browser::TabDetachedAt(WebContents* contents, int index) {
[email protected]d4c9caa2013-04-22 18:30:321004 // TabDetachedAt is called before TabStripModel has updated the
1005 // active index.
1006 int old_active_index = tab_strip_model_->active_index();
1007 if (index < old_active_index && !tab_strip_model_->closing_all()) {
1008 SessionService* session_service =
1009 SessionServiceFactory::GetForProfileIfExisting(profile_);
1010 if (session_service)
1011 session_service->SetSelectedTabInWindow(session_id(),
1012 old_active_index - 1);
1013 }
[email protected]010152f2014-07-15 00:16:471014
[email protected]c37e3b62010-01-22 00:15:171015 TabDetachedAtImpl(contents, index, DETACH_TYPE_DETACH);
[email protected]36b6dcb2008-11-12 01:19:571016}
1017
[email protected]023c01cc2012-11-12 15:41:111018void Browser::TabDeactivated(WebContents* contents) {
sriramsra41db562015-01-26 22:45:041019 exclusive_access_manager_->OnTabDeactivated(contents);
[email protected]023c01cc2012-11-12 15:41:111020 search_delegate_->OnTabDeactivated(contents);
[email protected]549f62b2013-10-26 12:54:421021 SearchTabHelper::FromWebContents(contents)->OnTabDeactivated();
[email protected]e3a5d5b2012-06-22 17:56:461022
[email protected]85ff2c42009-05-29 21:22:031023 // Save what the user's currently typing, so it can be restored when we
1024 // switch back to this tab.
[email protected]023c01cc2012-11-12 15:41:111025 window_->GetLocationBar()->SaveStateToContents(contents);
[email protected]3d6a8952012-12-14 03:18:071026
1027 if (instant_controller_)
1028 instant_controller_->TabDeactivated(contents);
[email protected]85ff2c42009-05-29 21:22:031029}
1030
[email protected]8e707792012-11-13 10:32:121031void Browser::ActiveTabChanged(WebContents* old_contents,
1032 WebContents* new_contents,
[email protected]3aca95d42011-05-24 22:06:541033 int index,
[email protected]5f2de242013-04-05 02:06:071034 int reason) {
[email protected]d500b1c32013-03-12 06:26:081035 content::RecordAction(UserMetricsAction("ActiveTabChanged"));
1036
[email protected]da9ac4ee2013-10-02 12:19:051037 // Update the bookmark state, since the BrowserWindow may query it during
1038 // OnActiveTabChanged() below.
1039 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_SWITCH);
1040
1041 // Let the BrowserWindow do its handling. On e.g. views this changes the
1042 // focused object, which should happen before we update the toolbar below,
[email protected]08726d5e2013-09-24 21:52:551043 // since the omnibox expects the correct element to already be focused when it
1044 // is updated.
1045 window_->OnActiveTabChanged(old_contents, new_contents, index, reason);
1046
sriramsra41db562015-01-26 22:45:041047 exclusive_access_manager_->OnTabDetachedFromView(old_contents);
[email protected]949f62a32014-02-13 00:53:101048
[email protected]002efb042011-10-07 17:30:241049 // Discarded tabs always get reloaded.
georgesak6fc95e92015-10-26 21:27:061050 // TODO(georgesak): Validate the usefulness of this. And if needed then move
1051 // to TabManager.
georgesak032d5c42016-01-20 05:09:171052 if (g_browser_process->GetTabManager() &&
1053 g_browser_process->GetTabManager()->IsTabDiscarded(new_contents))
[email protected]a37d4b02012-06-25 21:56:101054 chrome::Reload(this, CURRENT_TAB);
[email protected]002efb042011-10-07 17:30:241055
[email protected]36b6dcb2008-11-12 01:19:571056 // If we have any update pending, do it now.
[email protected]24f1477532011-11-22 22:55:361057 if (chrome_updater_factory_.HasWeakPtrs() && old_contents)
[email protected]36b6dcb2008-11-12 01:19:571058 ProcessPendingUIUpdates();
1059
[email protected]36b6dcb2008-11-12 01:19:571060 // Propagate the profile to the location bar.
[email protected]9bc3f242013-04-05 23:27:361061 UpdateToolbar((reason & CHANGE_REASON_REPLACED) == 0);
[email protected]36b6dcb2008-11-12 01:19:571062
sdefresne51bbec7b2015-08-03 14:18:131063 if (search::IsInstantExtendedAPIEnabled())
[email protected]8845bb42013-10-29 00:36:291064 search_delegate_->OnTabActivated(new_contents);
[email protected]e3a5d5b2012-06-22 17:56:461065
[email protected]943d8122010-06-11 02:13:451066 // Update reload/stop state.
[email protected]8e707792012-11-13 10:32:121067 command_controller_->LoadingStateChanged(new_contents->IsLoading(), true);
[email protected]36b6dcb2008-11-12 01:19:571068
[email protected]84214982008-12-10 18:49:101069 // Update commands to reflect current state.
[email protected]5d98294912012-06-27 22:57:401070 command_controller_->TabStateChanged();
[email protected]36b6dcb2008-11-12 01:19:571071
1072 // Reset the status bubble.
[email protected]be3877f2009-01-14 15:51:101073 StatusBubble* status_bubble = GetStatusBubble();
1074 if (status_bubble) {
1075 status_bubble->Hide();
[email protected]36b6dcb2008-11-12 01:19:571076
[email protected]be3877f2009-01-14 15:51:101077 // Show the loading state (if any).
[email protected]0d56fa7212012-11-15 23:34:021078 status_bubble->SetStatus(CoreTabHelper::FromWebContents(
1079 tab_strip_model_->GetActiveWebContents())->GetStatusText());
[email protected]be3877f2009-01-14 15:51:101080 }
[email protected]36b6dcb2008-11-12 01:19:571081
[email protected]826eabe12013-09-30 20:33:381082 if (HasFindBarController()) {
1083 find_bar_controller_->ChangeWebContents(new_contents);
andresantoso6a7691b2014-12-10 23:37:411084 find_bar_controller_->find_bar()->MoveWindowIfNecessary(gfx::Rect());
[email protected]826eabe12013-09-30 20:33:381085 }
1086
georgesak5582cbe2015-05-22 22:08:071087 // Update sessions (selected tab index and last active time). Don't force
1088 // creation of sessions. If sessions doesn't exist, the change will be picked
1089 // up by sessions when created.
[email protected]92371eb2011-04-28 11:50:151090 SessionService* session_service =
1091 SessionServiceFactory::GetForProfileIfExisting(profile_);
[email protected]2fc15ae2012-05-06 00:01:371092 if (session_service && !tab_strip_model_->closing_all()) {
[email protected]04e3ead52013-01-14 01:09:201093 session_service->SetSelectedTabInWindow(session_id(),
1094 tab_strip_model_->active_index());
chrisha0598fc312016-01-07 20:40:411095 SessionTabHelper* session_tab_helper =
1096 SessionTabHelper::FromWebContents(new_contents);
1097 session_service->SetLastActiveTime(session_id(),
1098 session_tab_helper->session_id(),
1099 base::TimeTicks::Now());
[email protected]36b6dcb2008-11-12 01:19:571100 }
[email protected]09b29342011-06-24 19:18:481101
[email protected]8845bb42013-10-29 00:36:291102 // This needs to be called after notifying SearchDelegate.
[email protected]e41982a72012-11-20 07:16:511103 if (instant_controller_)
1104 instant_controller_->ActiveTabChanged();
[email protected]10090d52013-07-25 20:41:371105
[email protected]549f62b2013-10-26 12:54:421106 SearchTabHelper::FromWebContents(new_contents)->OnTabActivated();
[email protected]36b6dcb2008-11-12 01:19:571107}
1108
[email protected]3abdff22012-11-13 10:28:451109void Browser::TabMoved(WebContents* contents,
[email protected]36b6dcb2008-11-12 01:19:571110 int from_index,
[email protected]1a242c32010-02-02 21:20:541111 int to_index) {
[email protected]36b6dcb2008-11-12 01:19:571112 DCHECK(from_index >= 0 && to_index >= 0);
1113 // Notify the history service.
1114 SyncHistoryWithTabs(std::min(from_index, to_index));
1115}
1116
[email protected]da25dd52011-01-11 19:06:301117void Browser::TabReplacedAt(TabStripModel* tab_strip_model,
[email protected]b624ddc2012-11-15 18:04:131118 WebContents* old_contents,
1119 WebContents* new_contents,
[email protected]8a34e6602010-10-02 17:29:431120 int index) {
[email protected]b624ddc2012-11-15 18:04:131121 TabDetachedAtImpl(old_contents, index, DETACH_TYPE_REPLACE);
sriramsra41db562015-01-26 22:45:041122 exclusive_access_manager_->OnTabClosing(old_contents);
[email protected]40600152012-09-26 10:04:311123 SessionService* session_service =
1124 SessionServiceFactory::GetForProfile(profile_);
1125 if (session_service)
[email protected]b624ddc2012-11-15 18:04:131126 session_service->TabClosing(old_contents);
[email protected]04e3ead52013-01-14 01:09:201127 TabInsertedAt(new_contents,
1128 index,
1129 (index == tab_strip_model_->active_index()));
[email protected]c37e3b62010-01-22 00:15:171130
creisf82a92ee2015-10-19 19:59:081131 if (!new_contents->GetController().IsInitialBlankNavigation()) {
[email protected]c37e3b62010-01-22 00:15:171132 // Send out notification so that observers are updated appropriately.
creisf82a92ee2015-10-19 19:59:081133 int entry_count = new_contents->GetController().GetEntryCount();
[email protected]b624ddc2012-11-15 18:04:131134 new_contents->GetController().NotifyEntryChanged(
avi47179332015-05-20 21:01:111135 new_contents->GetController().GetEntryAtIndex(entry_count - 1));
[email protected]c37e3b62010-01-22 00:15:171136 }
[email protected]47e020a2010-10-15 14:43:371137
[email protected]47e020a2010-10-15 14:43:371138 if (session_service) {
1139 // The new_contents may end up with a different navigation stack. Force
1140 // the session service to update itself.
[email protected]52877dbc62012-06-29 22:22:031141 session_service->TabRestored(new_contents,
1142 tab_strip_model_->IsTabPinned(index));
[email protected]47e020a2010-10-15 14:43:371143 }
[email protected]c37e3b62010-01-22 00:15:171144}
1145
[email protected]2c7dba42012-11-09 17:05:421146void Browser::TabPinnedStateChanged(WebContents* contents, int index) {
[email protected]92371eb2011-04-28 11:50:151147 SessionService* session_service =
1148 SessionServiceFactory::GetForProfileIfExisting(profile());
[email protected]5c0e6482009-07-14 20:20:091149 if (session_service) {
[email protected]50a9a76b2012-09-12 14:19:361150 SessionTabHelper* session_tab_helper =
[email protected]2c7dba42012-11-09 17:05:421151 SessionTabHelper::FromWebContents(contents);
1152 session_service->SetPinnedState(session_id(),
1153 session_tab_helper->session_id(),
1154 tab_strip_model_->IsTabPinned(index));
[email protected]5c0e6482009-07-14 20:20:091155 }
1156}
1157
[email protected]36b6dcb2008-11-12 01:19:571158void Browser::TabStripEmpty() {
1159 // Close the frame after we return to the message loop (not immediately,
1160 // otherwise it will destroy this object before the stack has a chance to
1161 // cleanly unwind.)
1162 // Note: This will be called several times if TabStripEmpty is called several
1163 // times. This is because it does not close the window if tabs are
1164 // still present.
skyostil380bb2222015-06-12 12:07:051165 base::ThreadTaskRunnerHandle::Get()->PostTask(
[email protected]24f1477532011-11-22 22:55:361166 FROM_HERE, base::Bind(&Browser::CloseFrame, weak_factory_.GetWeakPtr()));
[email protected]e41982a72012-11-20 07:16:511167
1168 // Instant may have visible WebContents that need to be detached before the
1169 // window system closes.
1170 instant_controller_.reset();
[email protected]36b6dcb2008-11-12 01:19:571171}
1172
[email protected]067310262012-11-22 14:30:411173bool Browser::CanOverscrollContent() const {
mfomitchevf09a66bb2014-10-02 02:20:541174#if defined(OS_WIN)
1175 // Don't enable overscroll on Windows machines unless they have a touch
1176 // screen as these machines typically don't have a touchpad capable of
1177 // horizontal scrolling. We are purposefully biased towards "no" here,
1178 // so that we don't waste resources capturing screenshots for horizontal
1179 // overscroll navigation unnecessarily.
afakhry661e756a2015-12-01 23:50:041180 bool allow_overscroll = ui::GetTouchScreensAvailability() ==
1181 ui::TouchScreensAvailability::ENABLED;
mfomitchevf09a66bb2014-10-02 02:20:541182#elif defined(USE_AURA)
1183 bool allow_overscroll = true;
1184#else
1185 bool allow_overscroll = false;
1186#endif
1187
1188 if (!allow_overscroll)
1189 return false;
1190
avi556c05022014-12-22 23:31:431191 const std::string value =
1192 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
1193 switches::kOverscrollHistoryNavigation);
[email protected]b1d5b192014-03-04 05:36:201194 bool overscroll_enabled = value != "0";
[email protected]86f98a22013-03-20 14:35:001195 if (!overscroll_enabled)
1196 return false;
1197 if (is_app() || is_devtools() || !is_type_tabbed())
1198 return false;
1199
1200 // The detached bookmark bar has appearance of floating above the
1201 // web-contents. This does not play nicely with overscroll navigation
1202 // gestures. So disable overscroll navigation when the bookmark bar is in the
[email protected]b1d5b192014-03-04 05:36:201203 // detached state and the overscroll effect moves the layers.
1204 if (value == "1" && bookmark_bar_state_ == BookmarkBar::DETACHED)
[email protected]86f98a22013-03-20 14:35:001205 return false;
1206 return true;
[email protected]067310262012-11-22 14:30:411207}
1208
[email protected]aa62afd2014-04-22 19:22:461209bool Browser::ShouldPreserveAbortedURLs(WebContents* source) {
1210 // Allow failed URLs to stick around in the omnibox on the NTP, but not when
1211 // other pages have committed.
1212 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext());
1213 if (!profile || !source->GetController().GetLastCommittedEntry())
1214 return false;
1215 GURL committed_url(source->GetController().GetLastCommittedEntry()->GetURL());
sdefresne51bbec7b2015-08-03 14:18:131216 return search::IsNTPURL(committed_url, profile);
[email protected]aa62afd2014-04-22 19:22:461217}
1218
calamity6160055c2015-04-30 06:45:261219void Browser::SetFocusToLocationBar(bool select_all) {
1220 // Two differences between this and FocusLocationBar():
1221 // (1) This doesn't get recorded in user metrics, since it's called
1222 // internally.
1223 // (2) This checks whether the location bar can be focused, and if not, clears
1224 // the focus. FocusLocationBar() is only reached when the location bar is
1225 // focusable, but this may be reached at other times, e.g. while in
1226 // fullscreen mode, where we need to leave focus in a consistent state.
1227 window_->SetFocusToLocationBar(select_all);
1228}
1229
[email protected]b3996ba2012-08-08 00:39:131230bool Browser::PreHandleKeyboardEvent(content::WebContents* source,
1231 const NativeWebKeyboardEvent& event,
[email protected]fc761142012-06-08 04:49:201232 bool* is_keyboard_shortcut) {
mgiuca6fb14632015-12-08 03:59:471233 // Forward keyboard events to the manager for fullscreen / mouse lock. This
1234 // may consume the event (e.g., Esc exits fullscreen mode).
[email protected]fc761142012-06-08 04:49:201235 // TODO(koz): Write a test for this http://crbug.com/100441.
mgiuca6fb14632015-12-08 03:59:471236 if (exclusive_access_manager_->HandleUserKeyPress(event))
[email protected]fc761142012-06-08 04:49:201237 return true;
mgiuca6fb14632015-12-08 03:59:471238
[email protected]fc761142012-06-08 04:49:201239 return window()->PreHandleKeyboardEvent(event, is_keyboard_shortcut);
1240}
1241
[email protected]b3996ba2012-08-08 00:39:131242void Browser::HandleKeyboardEvent(content::WebContents* source,
1243 const NativeWebKeyboardEvent& event) {
[email protected]93e0baba2014-04-08 20:04:021244 DevToolsWindow* devtools_window =
1245 DevToolsWindow::GetInstanceForInspectedWebContents(source);
1246 bool handled = false;
1247 if (devtools_window)
1248 handled = devtools_window->ForwardKeyboardEvent(event);
1249
1250 if (!handled)
1251 window()->HandleKeyboardEvent(event);
[email protected]fc761142012-06-08 04:49:201252}
1253
[email protected]ed1bb7202013-06-21 05:16:441254bool Browser::TabsNeedBeforeUnloadFired() {
[email protected]88c92012013-07-02 11:56:341255 if (IsFastTabUnloadEnabled())
1256 return fast_unload_controller_->TabsNeedBeforeUnloadFired();
[email protected]ed1bb7202013-06-21 05:16:441257 return unload_controller_->TabsNeedBeforeUnloadFired();
1258}
1259
[email protected]edc3af82013-12-12 21:24:071260void Browser::ShowValidationMessage(content::WebContents* web_contents,
1261 const gfx::Rect& anchor_in_root_view,
[email protected]ebf04a862013-12-17 08:38:581262 const base::string16& main_text,
1263 const base::string16& sub_text) {
estadeba9261e2016-06-28 00:47:111264 // If the web contents is unparented (e.g. in a blocked popup) it does not
1265 // make sense to show a validation message. See http://crbug.com/616990
1266 if (!web_contents->GetTopLevelNativeWindow())
1267 return;
andresantoso49f9a7a2015-01-28 00:03:171268 validation_message_bubble_ =
1269 TabDialogs::FromWebContents(web_contents)
1270 ->ShowValidationMessage(anchor_in_root_view, main_text, sub_text);
[email protected]edc3af82013-12-12 21:24:071271}
1272
1273void Browser::HideValidationMessage(content::WebContents* web_contents) {
estade53f173912016-04-18 02:22:081274 if (validation_message_bubble_)
1275 validation_message_bubble_->CloseValidationMessage();
[email protected]edc3af82013-12-12 21:24:071276}
1277
1278void Browser::MoveValidationMessage(content::WebContents* web_contents,
1279 const gfx::Rect& anchor_in_root_view) {
1280 if (!validation_message_bubble_)
1281 return;
1282 RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView();
1283 if (rwhv) {
1284 validation_message_bubble_->SetPositionRelativeToAnchor(
1285 rwhv->GetRenderWidgetHost(), anchor_in_root_view);
1286 }
1287}
1288
[email protected]7a0c5aea2014-03-05 17:41:171289bool Browser::PreHandleGestureEvent(content::WebContents* source,
1290 const blink::WebGestureEvent& event) {
1291 // Disable pinch zooming in undocked dev tools window due to poor UX.
1292 if (app_name() == DevToolsWindow::kDevToolsApp)
1293 return event.type == blink::WebGestureEvent::GesturePinchBegin ||
1294 event.type == blink::WebGestureEvent::GesturePinchUpdate ||
1295 event.type == blink::WebGestureEvent::GesturePinchEnd;
1296
1297 return false;
1298}
1299
[email protected]f9253672014-07-31 03:35:381300bool Browser::CanDragEnter(content::WebContents* source,
1301 const content::DropData& data,
1302 blink::WebDragOperationsMask operations_allowed) {
1303 // Disallow drag-and-drop navigation for Settings windows which do not support
1304 // external navigation.
1305 if ((operations_allowed & blink::WebDragOperationLink) &&
1306 chrome::SettingsWindowManager::GetInstance()->IsSettingsBrowser(this)) {
1307 return false;
1308 }
1309 return true;
1310}
1311
estark5ea80e52015-06-19 18:43:121312content::SecurityStyle Browser::GetSecurityStyle(
1313 WebContents* web_contents,
1314 content::SecurityStyleExplanations* security_style_explanations) {
estark880a5a42015-11-30 18:21:211315 ChromeSecurityStateModelClient* model_client =
1316 ChromeSecurityStateModelClient::FromWebContents(web_contents);
1317 DCHECK(model_client);
estark09dbe8e2016-05-20 23:16:561318 return model_client->GetSecurityStyle(model_client->GetSecurityInfo(),
1319 security_style_explanations);
lgarron662dd522015-06-08 23:20:011320}
1321
pfeldman61aa8a1592015-09-03 22:08:181322void Browser::ShowCertificateViewerInDevTools(
1323 content::WebContents* web_contents, int cert_id) {
1324 DevToolsWindow* devtools_window =
1325 DevToolsWindow::GetInstanceForInspectedWebContents(web_contents);
1326 if (devtools_window)
1327 devtools_window->ShowCertificateViewer(cert_id);
1328}
1329
dcheng9603ab92016-04-08 04:17:321330std::unique_ptr<content::BluetoothChooser> Browser::RunBluetoothChooser(
jyasskin93649a52016-02-17 19:57:021331 content::RenderFrameHost* frame,
1332 const content::BluetoothChooser::EventHandler& event_handler) {
juncaibf183dd2016-05-27 16:57:241333 std::unique_ptr<BluetoothChooserController> bluetooth_chooser_controller(
1334 new BluetoothChooserController(frame, event_handler));
juncaicd975582015-12-21 22:58:101335
juncaibf183dd2016-05-27 16:57:241336 std::unique_ptr<BluetoothChooserDesktop> bluetooth_chooser_desktop(
1337 new BluetoothChooserDesktop(bluetooth_chooser_controller.get()));
1338
1339 std::unique_ptr<ChooserBubbleDelegate> chooser_bubble_delegate(
1340 new ChooserBubbleDelegate(frame,
1341 std::move(bluetooth_chooser_controller)));
juncaicd975582015-12-21 22:58:101342
jyasskin93649a52016-02-17 19:57:021343 Browser* browser = chrome::FindBrowserWithWebContents(
1344 WebContents::FromRenderFrameHost(frame));
juncaibf183dd2016-05-27 16:57:241345 BubbleReference bubble_reference = browser->GetBubbleManager()->ShowBubble(
1346 std::move(chooser_bubble_delegate));
juncaicd975582015-12-21 22:58:101347
1348 return std::move(bluetooth_chooser_desktop);
1349}
1350
pfeldman5f0773a42016-03-29 16:56:331351void Browser::RequestAppBannerFromDevTools(content::WebContents* web_contents) {
pfeldman7a69caa2016-05-07 21:33:501352 banners::AppBannerManagerEmulation::CreateForWebContents(web_contents);
1353 banners::AppBannerManagerEmulation* manager =
1354 banners::AppBannerManagerEmulation::FromWebContents(web_contents);
dominicknecc79172016-06-29 05:04:241355 manager->RequestAppBanner(web_contents->GetLastCommittedURL(), true);
horo9638ad272016-01-25 14:36:451356}
1357
[email protected]d013f142012-04-27 17:31:141358bool Browser::IsMouseLocked() const {
sriramsra41db562015-01-26 22:45:041359 return exclusive_access_manager_->mouse_lock_controller()->IsMouseLocked();
[email protected]92655362012-04-20 21:33:171360}
1361
1362void Browser::OnWindowDidShow() {
1363 if (window_has_shown_)
1364 return;
1365 window_has_shown_ = true;
1366
gab4768aee2015-11-11 23:37:191367 startup_metric_utils::RecordBrowserWindowDisplay(base::TimeTicks::Now());
[email protected]b5febf72012-07-29 18:10:091368
warx6f9ac8d2016-06-10 06:45:501369 // Nothing to do for non-tabbed and minimized windows.
1370 if (!is_type_tabbed() || window_->IsMinimized())
[email protected]92655362012-04-20 21:33:171371 return;
1372
1373 // Show any pending global error bubble.
1374 GlobalErrorService* service =
1375 GlobalErrorServiceFactory::GetForProfile(profile());
1376 GlobalError* error = service->GetFirstGlobalErrorWithBubbleView();
1377 if (error)
1378 error->ShowBubbleView(this);
1379}
1380
1381void Browser::ShowFirstRunBubble() {
1382 window()->GetLocationBar()->ShowFirstRunBubble();
1383}
1384
1385///////////////////////////////////////////////////////////////////////////////
[email protected]674bc592011-12-20 23:00:421386// Browser, content::WebContentsDelegate implementation:
initial.commit09911bf2008-07-26 23:55:291387
[email protected]e5d549d2011-12-28 01:29:201388WebContents* Browser::OpenURLFromTab(WebContents* source,
[email protected]00c37fc2011-08-02 00:22:501389 const OpenURLParams& params) {
dgozman3ef307abb2015-03-03 14:39:501390 if (is_devtools()) {
1391 DevToolsWindow* window = DevToolsWindow::AsDevToolsWindow(source);
1392 DCHECK(window);
1393 return window->OpenURLFromTab(source, params);
1394 }
1395
[email protected]78e2edc2012-07-01 23:32:281396 chrome::NavigateParams nav_params(this, params.url, params.transition);
[email protected]19c64142013-03-06 20:08:531397 FillNavigateParamsFromOpenURLParams(&nav_params, params);
[email protected]e232c992012-12-06 12:43:201398 nav_params.source_contents = source;
[email protected]00c37fc2011-08-02 00:22:501399 nav_params.tabstrip_add_types = TabStripModel::ADD_NONE;
[email protected]dee1b5a02014-04-22 11:21:561400 if (params.user_gesture)
1401 nav_params.window_action = chrome::NavigateParams::SHOW_WINDOW;
[email protected]e7f2e7c2013-07-15 09:41:301402 nav_params.user_gesture = params.user_gesture;
1403
[email protected]5635e6142013-07-18 08:10:401404 PopupBlockerTabHelper* popup_blocker_helper = NULL;
[email protected]e7f2e7c2013-07-15 09:41:301405 if (source)
[email protected]5635e6142013-07-18 08:10:401406 popup_blocker_helper = PopupBlockerTabHelper::FromWebContents(source);
[email protected]e7f2e7c2013-07-15 09:41:301407
[email protected]992e2912013-08-19 15:05:241408 if (popup_blocker_helper) {
[email protected]e7f2e7c2013-07-15 09:41:301409 if ((params.disposition == NEW_POPUP ||
1410 params.disposition == NEW_FOREGROUND_TAB ||
[email protected]7f53dff2013-08-05 10:45:501411 params.disposition == NEW_BACKGROUND_TAB ||
1412 params.disposition == NEW_WINDOW) &&
avi556c05022014-12-22 23:31:431413 !params.user_gesture &&
1414 !base::CommandLine::ForCurrentProcess()->HasSwitch(
1415 switches::kDisablePopupBlocking)) {
[email protected]67264062013-07-25 12:39:261416 if (popup_blocker_helper->MaybeBlockPopup(nav_params,
1417 WebWindowFeatures())) {
[email protected]5635e6142013-07-18 08:10:401418 return NULL;
[email protected]67264062013-07-25 12:39:261419 }
[email protected]e7f2e7c2013-07-15 09:41:301420 }
1421 }
1422
[email protected]78e2edc2012-07-01 23:32:281423 chrome::Navigate(&nav_params);
[email protected]00c37fc2011-08-02 00:22:501424
[email protected]e232c992012-12-06 12:43:201425 return nav_params.target_contents;
initial.commit09911bf2008-07-26 23:55:291426}
1427
creiscde28de2014-11-26 17:10:361428void Browser::NavigationStateChanged(WebContents* source,
[email protected]7f924832014-08-09 05:57:221429 content::InvalidateTypes changed_flags) {
erikchen174b78c62015-03-11 22:02:541430 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466285
1431 // is fixed.
1432 tracked_objects::ScopedTracker tracking_profile1(
1433 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1434 "466285 Browser::NavigationStateChanged::ScheduleUIUpdate"));
initial.commit09911bf2008-07-26 23:55:291435 // Only update the UI when something visible has changed.
[email protected]e83f1682008-09-07 23:57:401436 if (changed_flags)
initial.commit09911bf2008-07-26 23:55:291437 ScheduleUIUpdate(source, changed_flags);
1438
erikchen174b78c62015-03-11 22:02:541439 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466285
1440 // is fixed.
1441 tracked_objects::ScopedTracker tracking_profile2(
1442 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1443 "466285 Browser::NavigationStateChanged::TabStateChanged"));
[email protected]83c2e232011-10-07 21:36:461444 // We can synchronously update commands since they will only change once per
1445 // navigation, so we don't have to worry about flickering. We do, however,
1446 // need to update the command state early on load to always present usable
1447 // actions in the face of slow-to-commit pages.
[email protected]d9083482012-01-06 00:38:461448 if (changed_flags & (content::INVALIDATE_TYPE_URL |
thomasanderson9094aa52016-04-01 17:32:571449 content::INVALIDATE_TYPE_LOAD |
1450 content::INVALIDATE_TYPE_TAB))
[email protected]5d98294912012-06-27 22:57:401451 command_controller_->TabStateChanged();
benwellsa2a8a9f72015-04-07 07:09:541452
benwells11bd6ed2015-06-05 04:20:411453 if (hosted_app_controller_)
1454 hosted_app_controller_->UpdateLocationBarVisibility(true);
initial.commit09911bf2008-07-26 23:55:291455}
1456
estark25f9af12015-09-09 19:37:041457void Browser::VisibleSSLStateChanged(const WebContents* source) {
[email protected]25af6ec2014-06-06 19:35:071458 // When the current tab's SSL state changes, we need to update the URL
1459 // bar to reflect the new state.
1460 DCHECK(source);
1461 if (tab_strip_model_->GetActiveWebContents() == source)
1462 UpdateToolbar(false);
1463}
1464
[email protected]2a6bc3e2011-12-28 23:51:331465void Browser::AddNewContents(WebContents* source,
1466 WebContents* new_contents,
initial.commit09911bf2008-07-26 23:55:291467 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:391468 const gfx::Rect& initial_rect,
[email protected]eda238a12012-09-07 23:44:001469 bool user_gesture,
1470 bool* was_blocked) {
bokan107a47f2015-02-03 23:23:391471 chrome::AddWebContents(this, source, new_contents, disposition, initial_rect,
[email protected]eda238a12012-09-07 23:44:001472 user_gesture, was_blocked);
initial.commit09911bf2008-07-26 23:55:291473}
1474
[email protected]2a6bc3e2011-12-28 23:51:331475void Browser::ActivateContents(WebContents* contents) {
[email protected]0d56fa7212012-11-15 23:34:021476 tab_strip_model_->ActivateTabAt(
1477 tab_strip_model_->GetIndexOfWebContents(contents), false);
[email protected]f3e99e32008-07-30 04:48:391478 window_->Activate();
initial.commit09911bf2008-07-26 23:55:291479}
1480
[email protected]e3b10d12014-03-28 16:06:091481void Browser::LoadingStateChanged(WebContents* source,
1482 bool to_different_document) {
[email protected]2fc15ae2012-05-06 00:01:371483 window_->UpdateLoadingAnimations(tab_strip_model_->TabsAreLoading());
[email protected]f3e99e32008-07-30 04:48:391484 window_->UpdateTitleBar();
initial.commit09911bf2008-07-26 23:55:291485
[email protected]0d56fa7212012-11-15 23:34:021486 WebContents* selected_contents = tab_strip_model_->GetActiveWebContents();
[email protected]3c9e1872010-11-18 16:17:491487 if (source == selected_contents) {
[email protected]e3b10d12014-03-28 16:06:091488 bool is_loading = source->IsLoading() && to_different_document;
[email protected]5d98294912012-06-27 22:57:401489 command_controller_->LoadingStateChanged(is_loading, false);
[email protected]43d259d2010-09-03 21:37:461490 if (GetStatusBubble()) {
[email protected]0d56fa7212012-11-15 23:34:021491 GetStatusBubble()->SetStatus(CoreTabHelper::FromWebContents(
1492 tab_strip_model_->GetActiveWebContents())->GetStatusText());
[email protected]43d259d2010-09-03 21:37:461493 }
initial.commit09911bf2008-07-26 23:55:291494 }
1495}
1496
[email protected]a81343d232011-12-27 07:39:201497void Browser::CloseContents(WebContents* source) {
[email protected]88c92012013-07-02 11:56:341498 bool can_close_contents;
1499 if (IsFastTabUnloadEnabled())
1500 can_close_contents = fast_unload_controller_->CanCloseContents(source);
1501 else
1502 can_close_contents = unload_controller_->CanCloseContents(source);
1503
1504 if (can_close_contents)
[email protected]81ddd3402012-12-28 00:55:261505 chrome::CloseWebContents(this, source, true);
initial.commit09911bf2008-07-26 23:55:291506}
1507
[email protected]2a6bc3e2011-12-28 23:51:331508void Browser::MoveContents(WebContents* source, const gfx::Rect& pos) {
[email protected]b35b26b32011-05-05 20:35:141509 if (!IsPopupOrPanel(source)) {
initial.commit09911bf2008-07-26 23:55:291510 NOTREACHED() << "moving invalid browser type";
1511 return;
1512 }
[email protected]e561e202010-03-24 17:57:121513 window_->SetBounds(pos);
initial.commit09911bf2008-07-26 23:55:291514}
1515
[email protected]2a6bc3e2011-12-28 23:51:331516bool Browser::IsPopupOrPanel(const WebContents* source) const {
[email protected]7efaed362013-04-04 09:33:321517 return is_type_popup();
initial.commit09911bf2008-07-26 23:55:291518}
1519
avi450ce00e2014-09-24 01:58:521520void Browser::UpdateTargetURL(WebContents* source, const GURL& url) {
[email protected]be3877f2009-01-14 15:51:101521 if (!GetStatusBubble())
1522 return;
1523
jshin1fb76462016-04-05 22:13:031524 if (source == tab_strip_model_->GetActiveWebContents())
1525 GetStatusBubble()->SetURL(url);
[email protected]36b6dcb2008-11-12 01:19:571526}
1527
mgiuca1c92cab42016-01-07 03:18:271528void Browser::ContentsMouseEvent(WebContents* source,
1529 const gfx::Point& location,
1530 bool motion,
1531 bool exited) {
mgiucaef9a2862016-02-19 03:11:291532 exclusive_access_manager_->OnUserInput();
1533
mgiuca1c92cab42016-01-07 03:18:271534 // Mouse motion events update the status bubble, if it exists.
1535 if (!GetStatusBubble() || (!motion && !exited))
[email protected]92655362012-04-20 21:33:171536 return;
1537
[email protected]0d56fa7212012-11-15 23:34:021538 if (source == tab_strip_model_->GetActiveWebContents()) {
mgiuca1c92cab42016-01-07 03:18:271539 GetStatusBubble()->MouseMoved(location, exited);
1540 if (exited)
jshin1fb76462016-04-05 22:13:031541 GetStatusBubble()->SetURL(GURL());
[email protected]92655362012-04-20 21:33:171542 }
[email protected]3a6a3b62009-05-27 21:36:201543}
1544
[email protected]36b6dcb2008-11-12 01:19:571545void Browser::ContentsZoomChange(bool zoom_in) {
[email protected]5d98294912012-06-27 22:57:401546 chrome::ExecuteCommand(this, zoom_in ? IDC_ZOOM_PLUS : IDC_ZOOM_MINUS);
[email protected]36b6dcb2008-11-12 01:19:571547}
1548
[email protected]b3996ba2012-08-08 00:39:131549bool Browser::TakeFocus(content::WebContents* source,
1550 bool reverse) {
[email protected]ad50def52011-10-19 23:17:071551 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271552 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
[email protected]6c2381d2011-10-19 02:52:531553 content::Source<Browser>(this),
[email protected]ad50def52011-10-19 23:17:071554 content::NotificationService::NoDetails());
[email protected]130efb02009-09-18 18:54:351555 return false;
1556}
1557
[email protected]b7a756d42012-01-23 18:08:171558gfx::Rect Browser::GetRootWindowResizerRect() const {
1559 return window_->GetRootWindowResizerRect();
1560}
1561
[email protected]1d3e9172012-06-12 02:59:131562void Browser::BeforeUnloadFired(WebContents* web_contents,
[email protected]36b6dcb2008-11-12 01:19:571563 bool proceed,
1564 bool* proceed_to_fire_unload) {
[email protected]90354712013-11-16 00:06:301565 if (is_devtools() && DevToolsWindow::HandleBeforeUnload(web_contents,
1566 proceed, proceed_to_fire_unload))
1567 return;
1568
[email protected]88c92012013-07-02 11:56:341569 if (IsFastTabUnloadEnabled()) {
1570 *proceed_to_fire_unload =
1571 fast_unload_controller_->BeforeUnloadFired(web_contents, proceed);
1572 } else {
1573 *proceed_to_fire_unload =
1574 unload_controller_->BeforeUnloadFired(web_contents, proceed);
1575 }
[email protected]36b6dcb2008-11-12 01:19:571576}
1577
[email protected]0c9406632013-02-08 01:13:331578bool Browser::ShouldFocusLocationBarByDefault(WebContents* source) {
[email protected]77fd82e52013-03-12 22:24:261579 const content::NavigationEntry* entry =
[email protected]06cb23b2013-12-17 02:56:491580 source->GetController().GetActiveEntry();
[email protected]e4916412013-04-25 20:12:151581 if (entry) {
1582 GURL url = entry->GetURL();
1583 GURL virtual_url = entry->GetVirtualURL();
[email protected]2d9748b22014-02-11 00:17:291584 if ((url.SchemeIs(content::kChromeUIScheme) &&
[email protected]e4916412013-04-25 20:12:151585 url.host() == chrome::kChromeUINewTabHost) ||
[email protected]2d9748b22014-02-11 00:17:291586 (virtual_url.SchemeIs(content::kChromeUIScheme) &&
[email protected]e4916412013-04-25 20:12:151587 virtual_url.host() == chrome::kChromeUINewTabHost)) {
1588 return true;
1589 }
[email protected]c744f7d22013-04-12 20:29:041590 }
1591
sdefresne51bbec7b2015-08-03 14:18:131592 return search::NavEntryIsInstantNTP(source, entry);
[email protected]0c9406632013-02-08 01:13:331593}
1594
[email protected]2a6bc3e2011-12-28 23:51:331595void Browser::ViewSourceForTab(WebContents* source, const GURL& page_url) {
[email protected]1788e772010-12-15 16:40:501596 DCHECK(source);
[email protected]ab93b6372012-11-28 05:20:221597 chrome::ViewSource(this, source);
[email protected]77d8d622010-12-15 10:30:121598}
1599
[email protected]2a6bc3e2011-12-28 23:51:331600void Browser::ViewSourceForFrame(WebContents* source,
[email protected]932b7a12011-03-09 12:50:271601 const GURL& frame_url,
[email protected]691aa2f2013-05-28 22:52:041602 const content::PageState& frame_page_state) {
[email protected]932b7a12011-03-09 12:50:271603 DCHECK(source);
[email protected]691aa2f2013-05-28 22:52:041604 chrome::ViewSource(this, source, frame_url, frame_page_state);
[email protected]932b7a12011-03-09 12:50:271605}
1606
[email protected]4fdf6742012-01-10 20:14:361607void Browser::ShowRepostFormWarningDialog(WebContents* source) {
[email protected]51296c62012-09-26 13:02:441608 TabModalConfirmDialog::Create(new RepostFormWarningController(source),
[email protected]70320ab2012-11-02 19:07:161609 source);
[email protected]14f3408a2009-08-31 20:53:531610}
1611
[email protected]4fdf6742012-01-10 20:14:361612bool Browser::ShouldCreateWebContents(
1613 WebContents* web_contents,
dcheng3ce04b62015-10-26 23:30:551614 int32_t route_id,
1615 int32_t main_frame_route_id,
1616 int32_t main_frame_widget_route_id,
[email protected]4fdf6742012-01-10 20:14:361617 WindowContainerType window_container_type,
nasko48321ca32015-07-02 20:44:121618 const std::string& frame_name,
[email protected]4543b67c2013-07-11 18:18:361619 const GURL& target_url,
[email protected]dd6730412013-08-14 15:03:371620 const std::string& partition_id,
1621 content::SessionStorageNamespace* session_storage_namespace) {
[email protected]4fdf6742012-01-10 20:14:361622 if (window_container_type == WINDOW_CONTAINER_TYPE_BACKGROUND) {
1623 // If a BackgroundContents is created, suppress the normal WebContents.
dcheng3ce04b62015-10-26 23:30:551624 return !MaybeCreateBackgroundContents(
1625 route_id, main_frame_route_id, main_frame_widget_route_id, web_contents,
1626 frame_name, target_url, partition_id, session_storage_namespace);
[email protected]4fdf6742012-01-10 20:14:361627 }
1628
1629 return true;
1630}
1631
1632void Browser::WebContentsCreated(WebContents* source_contents,
[email protected]a7531d772014-03-25 16:15:071633 int opener_render_frame_id,
nasko48321ca32015-07-02 20:44:121634 const std::string& frame_name,
[email protected]4fdf6742012-01-10 20:14:361635 const GURL& target_url,
1636 WebContents* new_contents) {
[email protected]6451fe92012-08-30 18:37:261637 // Adopt the WebContents now, so all observers are in place, as the network
[email protected]3d61dd02012-06-07 00:05:531638 // requests for its initial navigation will start immediately. The WebContents
1639 // will later be inserted into this browser using Browser::Navigate via
[email protected]6451fe92012-08-30 18:37:261640 // AddNewContents.
[email protected]e0bc75d52014-01-30 23:42:591641 TabHelpers::AttachTabHelpers(new_contents);
[email protected]4fdf6742012-01-10 20:14:361642
afakhry8394f942015-07-09 18:46:091643 // Make the tab show up in the task manager.
1644 task_management::WebContentsTags::CreateForTabContents(new_contents);
1645
[email protected]4fdf6742012-01-10 20:14:361646 // Notify.
1647 RetargetingDetails details;
1648 details.source_web_contents = source_contents;
[email protected]a7531d772014-03-25 16:15:071649 details.source_render_frame_id = opener_render_frame_id;
[email protected]4fdf6742012-01-10 20:14:361650 details.target_url = target_url;
1651 details.target_web_contents = new_contents;
1652 details.not_yet_in_tabstrip = true;
1653 content::NotificationService::current()->Notify(
1654 chrome::NOTIFICATION_RETARGETING,
1655 content::Source<Profile>(profile_),
1656 content::Details<RetargetingDetails>(&details));
[email protected]80c7b802011-10-28 19:59:081657}
1658
[email protected]2a6bc3e2011-12-28 23:51:331659void Browser::RendererUnresponsive(WebContents* source) {
[email protected]a7ed0ca2012-12-06 19:30:061660 // Ignore hangs if a tab is blocked.
1661 int index = tab_strip_model_->GetIndexOfWebContents(source);
1662 DCHECK_NE(TabStripModel::kNoTab, index);
1663 if (tab_strip_model_->IsTabBlocked(index))
1664 return;
[email protected]c19d9542011-11-15 22:26:421665
andresantoso063151d2014-12-18 22:37:551666 TabDialogs::FromWebContents(source)->ShowHungRendererDialog();
[email protected]55452902011-06-01 21:57:471667}
1668
[email protected]2a6bc3e2011-12-28 23:51:331669void Browser::RendererResponsive(WebContents* source) {
andresantoso063151d2014-12-18 22:37:551670 TabDialogs::FromWebContents(source)->HideHungRendererDialog();
[email protected]55452902011-06-01 21:57:471671}
1672
[email protected]1d3e9172012-06-12 02:59:131673void Browser::DidNavigateMainFramePostCommit(WebContents* web_contents) {
[email protected]0d56fa7212012-11-15 23:34:021674 if (web_contents == tab_strip_model_->GetActiveWebContents())
[email protected]09b29342011-06-24 19:18:481675 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
[email protected]93f230e02011-06-01 14:40:001676}
1677
mathiash72a5e462014-11-19 08:18:501678content::JavaScriptDialogManager* Browser::GetJavaScriptDialogManager(
1679 WebContents* source) {
oshima0929be2a2014-11-19 22:21:031680 return app_modal::JavaScriptDialogManager::GetInstance();
[email protected]3ab9cb82011-06-03 18:02:071681}
1682
[email protected]8bc5ff02013-11-29 06:34:031683content::ColorChooser* Browser::OpenColorChooser(
1684 WebContents* web_contents,
1685 SkColor initial_color,
1686 const std::vector<content::ColorSuggestion>& suggestions) {
[email protected]cb805452013-05-22 15:16:211687 return chrome::ShowColorChooser(web_contents, initial_color);
[email protected]da8543762012-03-20 08:52:201688}
1689
nasko35169af2016-06-10 18:47:131690void Browser::RunFileChooser(content::RenderFrameHost* render_frame_host,
[email protected]8caadeb2011-11-22 02:45:231691 const content::FileChooserParams& params) {
nasko35169af2016-06-10 18:47:131692 FileSelectHelper::RunFileChooser(render_frame_host, params);
[email protected]3a29a6e2011-08-24 18:26:211693}
1694
[email protected]1d3e9172012-06-12 02:59:131695void Browser::EnumerateDirectory(WebContents* web_contents,
[email protected]33f8ad52012-05-22 18:10:131696 int request_id,
[email protected]650b2d52013-02-10 03:41:451697 const base::FilePath& path) {
[email protected]1d3e9172012-06-12 02:59:131698 FileSelectHelper::EnumerateDirectory(web_contents, request_id, path);
[email protected]3a29a6e2011-08-24 18:26:211699}
1700
[email protected]4aebbcaa2013-09-17 22:26:491701bool Browser::EmbedsFullscreenWidget() const {
[email protected]40f12b92014-07-15 19:56:111702 return true;
[email protected]4aebbcaa2013-09-17 22:26:491703}
1704
mlamouri7a78d6fd2015-01-17 13:23:531705void Browser::EnterFullscreenModeForTab(WebContents* web_contents,
1706 const GURL& origin) {
sriramsra41db562015-01-26 22:45:041707 exclusive_access_manager_->fullscreen_controller()->EnterFullscreenModeForTab(
1708 web_contents, origin);
mlamouri7a78d6fd2015-01-17 13:23:531709}
1710
1711void Browser::ExitFullscreenModeForTab(WebContents* web_contents) {
sriramsra41db562015-01-26 22:45:041712 exclusive_access_manager_->fullscreen_controller()->ExitFullscreenModeForTab(
1713 web_contents);
[email protected]8a5e0ca2011-08-25 06:30:471714}
1715
[email protected]1d3e9172012-06-12 02:59:131716bool Browser::IsFullscreenForTabOrPending(
1717 const WebContents* web_contents) const {
sriramsra41db562015-01-26 22:45:041718 return exclusive_access_manager_->fullscreen_controller()
1719 ->IsFullscreenForTabOrPending(web_contents);
[email protected]5d5f7af2011-10-01 01:38:121720}
1721
mikhail.pozdnyakov76208cb2015-09-07 10:31:401722blink::WebDisplayMode Browser::GetDisplayMode(
1723 const WebContents* web_contents) const {
1724 if (window_->IsFullscreen())
1725 return blink::WebDisplayModeFullscreen;
1726
1727 if (is_type_popup())
1728 return blink::WebDisplayModeStandalone;
1729
1730 return blink::WebDisplayModeBrowser;
1731}
1732
[email protected]1d3e9172012-06-12 02:59:131733void Browser::RegisterProtocolHandler(WebContents* web_contents,
[email protected]7d189022011-08-25 22:54:201734 const std::string& protocol,
1735 const GURL& url,
[email protected]3a3b75a2012-06-01 08:38:361736 bool user_gesture) {
[email protected]a23abe472014-07-29 23:46:311737 content::BrowserContext* context = web_contents->GetBrowserContext();
1738 if (context->IsOffTheRecord())
[email protected]e1ff4a42013-12-16 20:29:011739 return;
1740
1741 ProtocolHandler handler =
[email protected]c125e532014-05-10 20:39:061742 ProtocolHandler::CreateProtocolHandler(protocol, url);
[email protected]e1ff4a42013-12-16 20:29:011743
1744 ProtocolHandlerRegistry* registry =
[email protected]a23abe472014-07-29 23:46:311745 ProtocolHandlerRegistryFactory::GetForBrowserContext(context);
[email protected]e1ff4a42013-12-16 20:29:011746 if (registry->SilentlyHandleRegisterHandlerRequest(handler))
1747 return;
1748
1749 TabSpecificContentSettings* tab_content_settings =
1750 TabSpecificContentSettings::FromWebContents(web_contents);
1751 if (!user_gesture && window_) {
1752 tab_content_settings->set_pending_protocol_handler(handler);
1753 tab_content_settings->set_previous_protocol_handler(
1754 registry->GetHandlerFor(handler.protocol()));
1755 window_->GetLocationBar()->UpdateContentSettingsIcons();
1756 return;
1757 }
1758
1759 // Make sure content-setting icon is turned off in case the page does
1760 // ungestured and gestured RPH calls.
1761 if (window_) {
1762 tab_content_settings->ClearPendingProtocolHandler();
1763 window_->GetLocationBar()->UpdateContentSettingsIcons();
1764 }
1765
tsergeanta771c23e2016-07-13 04:54:521766 PermissionRequestManager* permission_request_manager =
1767 PermissionRequestManager::FromWebContents(web_contents);
1768 if (permission_request_manager) {
1769 permission_request_manager->AddRequest(
[email protected]d23cdeee2014-03-10 06:39:531770 new RegisterProtocolHandlerPermissionRequest(registry, handler,
1771 url, user_gesture));
[email protected]f1d20572014-02-14 01:27:141772 }
[email protected]7d189022011-08-25 22:54:201773}
1774
[email protected]f5273e52014-07-14 16:30:201775void Browser::UnregisterProtocolHandler(WebContents* web_contents,
1776 const std::string& protocol,
1777 const GURL& url,
1778 bool user_gesture) {
1779 // user_gesture will be used in case we decide to have confirmation bubble
1780 // for user while un-registering the handler.
[email protected]a23abe472014-07-29 23:46:311781 content::BrowserContext* context = web_contents->GetBrowserContext();
1782 if (context->IsOffTheRecord())
[email protected]f5273e52014-07-14 16:30:201783 return;
1784
1785 ProtocolHandler handler =
1786 ProtocolHandler::CreateProtocolHandler(protocol, url);
1787
1788 ProtocolHandlerRegistry* registry =
[email protected]a23abe472014-07-29 23:46:311789 ProtocolHandlerRegistryFactory::GetForBrowserContext(context);
[email protected]f5273e52014-07-14 16:30:201790 registry->RemoveHandler(handler);
1791}
1792
[email protected]2a6bc3e2011-12-28 23:51:331793void Browser::UpdatePreferredSize(WebContents* source,
[email protected]0548c5352011-09-07 00:33:331794 const gfx::Size& pref_size) {
1795 window_->UpdatePreferredSize(source, pref_size);
1796}
1797
[email protected]61e2b3cc2012-03-02 16:13:341798void Browser::ResizeDueToAutoResize(WebContents* source,
1799 const gfx::Size& new_size) {
1800 window_->ResizeDueToAutoResize(source, new_size);
1801}
1802
[email protected]1d3e9172012-06-12 02:59:131803void Browser::FindReply(WebContents* web_contents,
[email protected]92655362012-04-20 21:33:171804 int request_id,
1805 int number_of_matches,
1806 const gfx::Rect& selection_rect,
1807 int active_match_ordinal,
1808 bool final_update) {
[email protected]e1ff4a42013-12-16 20:29:011809 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(web_contents);
1810 if (!find_tab_helper)
1811 return;
1812
1813 find_tab_helper->HandleFindReply(request_id,
1814 number_of_matches,
1815 selection_rect,
1816 active_match_ordinal,
1817 final_update);
[email protected]92655362012-04-20 21:33:171818}
1819
[email protected]1d3e9172012-06-12 02:59:131820void Browser::RequestToLockMouse(WebContents* web_contents,
[email protected]a9c81f02012-06-01 00:15:441821 bool user_gesture,
1822 bool last_unlocked_by_target) {
sriramsra41db562015-01-26 22:45:041823 exclusive_access_manager_->mouse_lock_controller()->RequestToLockMouse(
1824 web_contents, user_gesture, last_unlocked_by_target);
[email protected]55c87fa2011-10-15 07:28:281825}
1826
[email protected]5ef8e372011-10-18 04:25:551827void Browser::LostMouseLock() {
sriramsra41db562015-01-26 22:45:041828 exclusive_access_manager_->mouse_lock_controller()->LostMouseLock();
[email protected]5ef8e372011-10-18 04:25:551829}
1830
[email protected]f13b4202012-06-12 23:53:231831void Browser::RequestMediaAccessPermission(
1832 content::WebContents* web_contents,
[email protected]33662e52013-01-07 21:31:091833 const content::MediaStreamRequest& request,
[email protected]f13b4202012-06-12 23:53:231834 const content::MediaResponseCallback& callback) {
[email protected]859ec322013-09-13 16:24:561835 ::RequestMediaAccessPermission(web_contents, profile_, request, callback);
[email protected]f13b4202012-06-12 23:53:231836}
1837
grunell657d4d82014-09-18 00:09:431838bool Browser::CheckMediaAccessPermission(content::WebContents* web_contents,
1839 const GURL& security_origin,
1840 content::MediaStreamType type) {
1841 return ::CheckMediaAccessPermission(web_contents, security_origin, type);
1842}
1843
[email protected]8c8fc292012-11-23 18:57:161844bool Browser::RequestPpapiBrokerPermission(
1845 WebContents* web_contents,
1846 const GURL& url,
[email protected]650b2d52013-02-10 03:41:451847 const base::FilePath& plugin_path,
[email protected]8c8fc292012-11-23 18:57:161848 const base::Callback<void(bool)>& callback) {
[email protected]0be09932013-01-08 02:03:501849 PepperBrokerInfoBarDelegate::Create(web_contents, url, plugin_path, callback);
[email protected]8c8fc292012-11-23 18:57:161850 return true;
1851}
1852
[email protected]fc2b46b2014-05-03 16:33:451853gfx::Size Browser::GetSizeForNewRenderView(WebContents* web_contents) const {
[email protected]dc0fd432013-08-27 15:29:211854 // When navigating away from NTP with unpinned bookmark bar, the bookmark bar
1855 // would disappear on non-NTP pages, resulting in a bigger size for the new
1856 // render view.
[email protected]fc2b46b2014-05-03 16:33:451857 gfx::Size size = web_contents->GetContainerBounds().size();
[email protected]dc0fd432013-08-27 15:29:211858 // Don't change render view size if bookmark bar is currently not detached,
1859 // or there's no pending entry, or navigating to a NTP page.
1860 if (size.IsEmpty() || bookmark_bar_state_ != BookmarkBar::DETACHED)
1861 return size;
1862 const NavigationEntry* pending_entry =
1863 web_contents->GetController().GetPendingEntry();
1864 if (pending_entry &&
sdefresne51bbec7b2015-08-03 14:18:131865 !search::IsNTPURL(pending_entry->GetVirtualURL(), profile_)) {
[email protected]dc0fd432013-08-27 15:29:211866 size.Enlarge(
1867 0, window()->GetRenderViewHeightInsetWithDetachedBookmarkBar());
1868 }
1869 return size;
1870}
1871
[email protected]36b6dcb2008-11-12 01:19:571872///////////////////////////////////////////////////////////////////////////////
[email protected]51d83bdf2011-12-09 16:09:291873// Browser, CoreTabHelperDelegate implementation:
[email protected]a0366a52011-02-04 20:04:211874
[email protected]ece4aca2012-09-19 22:22:581875void Browser::SwapTabContents(content::WebContents* old_contents,
[email protected]9aa1f6282014-01-30 08:47:411876 content::WebContents* new_contents,
1877 bool did_start_load,
1878 bool did_finish_load) {
[email protected]ece4aca2012-09-19 22:22:581879 int index = tab_strip_model_->GetIndexOfWebContents(old_contents);
[email protected]92655362012-04-20 21:33:171880 DCHECK_NE(TabStripModel::kNoTab, index);
[email protected]59fa29fb2012-12-07 22:13:421881 tab_strip_model_->ReplaceWebContentsAt(index, new_contents);
[email protected]92655362012-04-20 21:33:171882}
1883
[email protected]ece4aca2012-09-19 22:22:581884bool Browser::CanReloadContents(content::WebContents* web_contents) const {
[email protected]5d98294912012-06-27 22:57:401885 return chrome::CanReload(this);
[email protected]e1461952012-06-14 21:05:351886}
1887
[email protected]ece4aca2012-09-19 22:22:581888bool Browser::CanSaveContents(content::WebContents* web_contents) const {
[email protected]5d98294912012-06-27 22:57:401889 return chrome::CanSavePage(this);
[email protected]e1461952012-06-14 21:05:351890}
1891
[email protected]92655362012-04-20 21:33:171892///////////////////////////////////////////////////////////////////////////////
[email protected]82808d02014-04-10 00:10:591893// Browser, SearchTabHelperDelegate implementation:
1894
[email protected]82808d02014-04-10 00:10:591895void Browser::OnWebContentsInstantSupportDisabled(
1896 const content::WebContents* web_contents) {
1897 DCHECK(web_contents);
1898 if (tab_strip_model_->GetActiveWebContents() == web_contents)
1899 UpdateToolbar(false);
1900}
1901
1902OmniboxView* Browser::GetOmniboxView() {
1903 return window_->GetLocationBar()->GetOmniboxView();
1904}
1905
1906std::set<std::string> Browser::GetOpenUrls() {
jitendra.ks30f03392015-01-28 09:47:181907 scoped_refptr<history::TopSites> top_sites =
1908 TopSitesFactory::GetForProfile(profile_);
[email protected]82808d02014-04-10 00:10:591909 if (!top_sites) // NULL for Incognito profiles.
1910 return std::set<std::string>();
1911
1912 std::set<std::string> open_urls;
1913 chrome::GetOpenUrls(*tab_strip_model_, *top_sites, &open_urls);
1914 return open_urls;
1915}
1916
1917///////////////////////////////////////////////////////////////////////////////
[email protected]e41d0082013-05-16 04:37:541918// Browser, web_modal::WebContentsModalDialogManagerDelegate implementation:
[email protected]92655362012-04-20 21:33:171919
[email protected]c5731a1c2012-12-22 10:18:451920void Browser::SetWebContentsBlocked(content::WebContents* web_contents,
1921 bool blocked) {
[email protected]c6464ad2012-10-03 00:27:281922 int index = tab_strip_model_->GetIndexOfWebContents(web_contents);
[email protected]92655362012-04-20 21:33:171923 if (index == TabStripModel::kNoTab) {
1924 NOTREACHED();
1925 return;
1926 }
[email protected]2fc15ae2012-05-06 00:01:371927 tab_strip_model_->SetTabBlocked(index, blocked);
[email protected]0d56fa7212012-11-15 23:34:021928 if (!blocked && tab_strip_model_->GetActiveWebContents() == web_contents)
[email protected]fc2b46b2014-05-03 16:33:451929 web_contents->Focus();
[email protected]92655362012-04-20 21:33:171930}
1931
[email protected]e41d0082013-05-16 04:37:541932web_modal::WebContentsModalDialogHost*
1933Browser::GetWebContentsModalDialogHost() {
[email protected]7519c522013-04-02 01:07:231934 return window_->GetWebContentsModalDialogHost();
[email protected]6a151762012-10-03 22:33:191935}
1936
[email protected]92655362012-04-20 21:33:171937///////////////////////////////////////////////////////////////////////////////
[email protected]92655362012-04-20 21:33:171938// Browser, BookmarkTabHelperDelegate implementation:
1939
[email protected]1c5119c2012-09-19 00:08:571940void Browser::URLStarredChanged(content::WebContents* web_contents,
1941 bool starred) {
[email protected]0d56fa7212012-11-15 23:34:021942 if (web_contents == tab_strip_model_->GetActiveWebContents())
[email protected]92655362012-04-20 21:33:171943 window_->SetStarredState(starred);
1944}
1945
1946///////////////////////////////////////////////////////////////////////////////
[email protected]6f80e932012-06-04 19:00:071947// Browser, ZoomObserver implementation:
1948
wjmaclean7f63c6b2014-12-09 14:59:551949void Browser::OnZoomChanged(
juncaia64f769c22016-06-04 00:55:321950 const zoom::ZoomController::ZoomChangedEventData& data) {
[email protected]63d1f9b2014-07-05 19:09:031951 if (data.web_contents == tab_strip_model_->GetActiveWebContents()) {
wjmacleanff3d0f62015-02-25 19:38:361952 window_->ZoomChangedForActiveTab(data.can_show_bubble);
[email protected]d93dbd12014-08-04 23:42:531953 // Change the zoom commands state based on the zoom state
1954 command_controller_->ZoomStateChanged();
[email protected]6f80e932012-06-04 19:00:071955 }
1956}
1957
1958///////////////////////////////////////////////////////////////////////////////
[email protected]92f54082012-07-31 01:43:141959// Browser, ui::SelectFileDialog::Listener implementation:
[email protected]36b6dcb2008-11-12 01:19:571960
[email protected]650b2d52013-02-10 03:41:451961void Browser::FileSelected(const base::FilePath& path, int index,
1962 void* params) {
[email protected]53f04c82012-07-26 02:31:091963 FileSelectedWithExtraInfo(ui::SelectedFileInfo(path, path), index, params);
1964}
1965
[email protected]a573bc32013-04-09 15:09:541966void Browser::FileSelectedWithExtraInfo(const ui::SelectedFileInfo& file_info,
1967 int index,
1968 void* params) {
[email protected]53f04c82012-07-26 02:31:091969 profile_->set_last_selected_directory(file_info.file_path.DirName());
1970
[email protected]a573bc32013-04-09 15:09:541971 GURL url = net::FilePathToFileURL(file_info.local_path);
[email protected]b7813a22012-04-04 18:41:021972
1973#if defined(OS_CHROMEOS)
hirono90949af2014-09-25 04:23:371974 const GURL external_url =
1975 chromeos::CreateExternalFileURLFromPath(profile_, file_info.file_path);
1976 if (!external_url.is_empty())
1977 url = external_url;
[email protected]b7813a22012-04-04 18:41:021978#endif
1979
[email protected]a573bc32013-04-09 15:09:541980 if (url.is_empty())
[email protected]e5d549d2011-12-28 01:29:201981 return;
1982
1983 OpenURL(OpenURLParams(
Sylvain Defresnec6ccc77d2014-09-19 10:19:351984 url, Referrer(), CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false));
[email protected]36b6dcb2008-11-12 01:19:571985}
1986
1987///////////////////////////////////////////////////////////////////////////////
[email protected]6c2381d2011-10-19 02:52:531988// Browser, content::NotificationObserver implementation:
[email protected]36b6dcb2008-11-12 01:19:571989
[email protected]432115822011-07-10 15:52:271990void Browser::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:531991 const content::NotificationSource& source,
1992 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:271993 switch (type) {
[email protected]adf5a102014-07-31 12:44:061994 case extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:531995 Profile* profile = content::Source<Profile>(source).ptr();
[email protected]fe8944f2011-08-30 17:47:321996 if (profile_->IsSameProfile(profile) && window()->GetLocationBar())
erikchen545918a2014-12-31 02:34:061997 window()->GetLocationBar()->UpdatePageActions();
[email protected]371ed7a2009-08-25 15:22:461998 break;
1999 }
2000
[email protected]6b40bb582012-03-15 20:50:382001#if defined(ENABLE_THEMES)
[email protected]432115822011-07-10 15:52:272002 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED:
[email protected]e001d412009-06-26 20:53:252003 window()->UserChangedTheme();
2004 break;
[email protected]6b40bb582012-03-15 20:50:382005#endif
[email protected]e001d412009-06-26 20:53:252006
[email protected]ea049a02011-12-25 21:37:092007 case chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED: {
2008 WebContents* web_contents = content::Source<WebContents>(source).ptr();
[email protected]0d56fa7212012-11-15 23:34:022009 if (web_contents == tab_strip_model_->GetActiveWebContents()) {
[email protected]9a9a9e22011-05-13 22:20:352010 LocationBar* location_bar = window()->GetLocationBar();
2011 if (location_bar)
2012 location_bar->UpdateContentSettingsIcons();
2013 }
2014 break;
2015 }
2016
[email protected]e83f1682008-09-07 23:57:402017 default:
2018 NOTREACHED() << "Got a notification we didn't register for.";
initial.commit09911bf2008-07-26 23:55:292019 }
2020}
2021
thestiga84d8e62014-10-17 22:27:552022#if defined(ENABLE_EXTENSIONS)
[email protected]d3b98c82010-07-14 07:45:592023///////////////////////////////////////////////////////////////////////////////
jitendra.ksa0055532014-10-07 05:54:462024// Browser, extensions::ExtensionRegistryObserver implementation:
2025
2026void Browser::OnExtensionUninstalled(content::BrowserContext* browser_context,
2027 const extensions::Extension* extension,
2028 extensions::UninstallReason reason) {
2029 // During window creation on Windows we may end up calling into
2030 // SHAppBarMessage, which internally spawns a nested message loop.This
2031 // makes it possible for us to end up here before window creation has
2032 // completed, at which point window_ is NULL. See 94752 for details.
2033
2034 if (window() && window()->GetLocationBar())
2035 window()->GetLocationBar()->UpdatePageActions();
2036}
2037
2038void Browser::OnExtensionLoaded(content::BrowserContext* browser_context,
2039 const extensions::Extension* extension) {
wittman76df71db32014-12-18 23:26:582040 command_controller_->ExtensionStateChanged();
jitendra.ksa0055532014-10-07 05:54:462041}
2042
2043void Browser::OnExtensionUnloaded(
2044 content::BrowserContext* browser_context,
2045 const extensions::Extension* extension,
2046 extensions::UnloadedExtensionInfo::Reason reason) {
wittman76df71db32014-12-18 23:26:582047 command_controller_->ExtensionStateChanged();
jitendra.ksa0055532014-10-07 05:54:462048 if (window()->GetLocationBar())
2049 window()->GetLocationBar()->UpdatePageActions();
2050
2051 // Close any tabs from the unloaded extension, unless it's terminated,
2052 // in which case let the sad tabs remain.
jared.sohna1409622015-08-08 07:16:142053 // Also, if tab is muted and the cause is the unloaded extension, unmute it.
jitendra.ksa0055532014-10-07 05:54:462054 if (reason != extensions::UnloadedExtensionInfo::REASON_TERMINATE) {
2055 // Iterate backwards as we may remove items while iterating.
2056 for (int i = tab_strip_model_->count() - 1; i >= 0; --i) {
2057 WebContents* web_contents = tab_strip_model_->GetWebContentsAt(i);
2058 // Two cases are handled here:
2059
2060 // - The scheme check is for when an extension page is loaded in a
2061 // tab, e.g. chrome-extension://id/page.html.
2062 // - The extension_app check is for apps, which can have non-extension
2063 // schemes, e.g. https://mail.google.com if you have the Gmail app
2064 // installed.
2065 if ((web_contents->GetURL().SchemeIs(extensions::kExtensionScheme) &&
2066 web_contents->GetURL().host() == extension->id()) ||
2067 (extensions::TabHelper::FromWebContents(web_contents)
2068 ->extension_app() == extension)) {
2069 tab_strip_model_->CloseWebContentsAt(i, TabStripModel::CLOSE_NONE);
jared.sohna1409622015-08-08 07:16:142070 } else {
miu895bb4e2015-08-11 23:03:042071 chrome::UnmuteIfMutedByExtension(web_contents, extension->id());
jitendra.ksa0055532014-10-07 05:54:462072 }
2073 }
2074 }
2075}
thestiga84d8e62014-10-17 22:27:552076#endif // defined(ENABLE_EXTENSIONS)
jitendra.ksa0055532014-10-07 05:54:462077
2078///////////////////////////////////////////////////////////////////////////////
pkastingcc29d20e2015-02-25 06:05:532079// Browser, translate::ContentTranslateDriver::Observer implementation:
2080
2081void Browser::OnIsPageTranslatedChanged(content::WebContents* source) {
2082 DCHECK(source);
2083 if (tab_strip_model_->GetActiveWebContents() == source) {
2084 window_->SetTranslateIconToggled(
2085 ChromeTranslateClient::FromWebContents(
2086 source)->GetLanguageState().IsPageTranslated());
2087 }
2088}
2089
2090void Browser::OnTranslateEnabledChanged(content::WebContents* source) {
2091 DCHECK(source);
2092 if (tab_strip_model_->GetActiveWebContents() == source)
2093 UpdateToolbar(false);
2094}
2095
2096///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:572097// Browser, Command and state updating (private):
2098
[email protected]c31813a2012-11-15 18:47:262099void Browser::OnDevToolsDisabledChanged() {
2100 if (profile_->GetPrefs()->GetBoolean(prefs::kDevToolsDisabled))
[email protected]b50452f2014-08-18 12:31:442101 content::DevToolsAgentHost::DetachAllClients();
[email protected]c31813a2012-11-15 18:47:262102}
2103
[email protected]36b6dcb2008-11-12 01:19:572104///////////////////////////////////////////////////////////////////////////////
2105// Browser, UI update coalescing and handling (private):
2106
2107void Browser::UpdateToolbar(bool should_restore_state) {
[email protected]2e7fe4312013-08-21 17:19:482108 window_->UpdateToolbar(should_restore_state ?
2109 tab_strip_model_->GetActiveWebContents() : NULL);
initial.commit09911bf2008-07-26 23:55:292110}
2111
creiscde28de2014-11-26 17:10:362112void Browser::ScheduleUIUpdate(WebContents* source,
[email protected]36b6dcb2008-11-12 01:19:572113 unsigned changed_flags) {
[email protected]913664d2013-12-17 11:43:382114 DCHECK(source);
2115 int index = tab_strip_model_->GetIndexOfWebContents(source);
2116 DCHECK_NE(TabStripModel::kNoTab, index);
[email protected]a5231662009-09-26 00:56:002117
erikchen174b78c62015-03-11 22:02:542118 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466285
2119 // is fixed.
2120 tracked_objects::ScopedTracker tracking_profile1(
2121 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2122 "466285 Browser::ScheduleUIUpdate::Toolbar"));
[email protected]2b4355c2009-04-04 17:55:462123 // Do some synchronous updates.
creiscde28de2014-11-26 17:10:362124 if (changed_flags & content::INVALIDATE_TYPE_URL) {
2125 if (source == tab_strip_model_->GetActiveWebContents()) {
2126 // Only update the URL for the current tab. Note that we do not update
2127 // the navigation commands since those would have already been updated
2128 // synchronously by NavigationStateChanged.
2129 UpdateToolbar(false);
2130 } else {
2131 // Clear the saved tab state for the tab that navigated, so that we don't
2132 // restore any user text after the old URL has been invalidated (e.g.,
2133 // after a new navigation commits in that tab while unfocused).
2134 window_->ResetToolbarTabState(source);
2135 }
[email protected]d9083482012-01-06 00:38:462136 changed_flags &= ~content::INVALIDATE_TYPE_URL;
[email protected]36b6dcb2008-11-12 01:19:572137 }
erikchen174b78c62015-03-11 22:02:542138
2139 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466285
2140 // is fixed.
2141 tracked_objects::ScopedTracker tracking_profile2(
2142 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2143 "466285 Browser::ScheduleUIUpdate::TabStripModel"));
[email protected]d9083482012-01-06 00:38:462144 if (changed_flags & content::INVALIDATE_TYPE_LOAD) {
[email protected]6ebdc9b2010-09-27 16:55:572145 // Update the loading state synchronously. This is so the throbber will
2146 // immediately start/stop, which gives a more snappy feel. We want to do
[email protected]a5231662009-09-26 00:56:002147 // this for any tab so they start & stop quickly.
[email protected]526b45e2012-11-28 20:22:462148 tab_strip_model_->UpdateWebContentsStateAt(
[email protected]0d56fa7212012-11-15 23:34:022149 tab_strip_model_->GetIndexOfWebContents(source),
[email protected]6ebdc9b2010-09-27 16:55:572150 TabStripModelObserver::LOADING_ONLY);
[email protected]d9083482012-01-06 00:38:462151 // The status bubble needs to be updated during INVALIDATE_TYPE_LOAD too,
2152 // but we do that asynchronously by not stripping INVALIDATE_TYPE_LOAD from
[email protected]6ebdc9b2010-09-27 16:55:572153 // changed_flags.
[email protected]8030f012009-09-25 18:09:372154 }
2155
[email protected]d9083482012-01-06 00:38:462156 if (changed_flags & content::INVALIDATE_TYPE_TITLE && !source->IsLoading()) {
[email protected]f1cd5e82009-10-23 17:19:032157 // To correctly calculate whether the title changed while not loading
2158 // we need to process the update synchronously. This state only matters for
2159 // the TabStripModel, so we notify the TabStripModel now and notify others
2160 // asynchronously.
[email protected]526b45e2012-11-28 20:22:462161 tab_strip_model_->UpdateWebContentsStateAt(
[email protected]0d56fa7212012-11-15 23:34:022162 tab_strip_model_->GetIndexOfWebContents(source),
[email protected]f1cd5e82009-10-23 17:19:032163 TabStripModelObserver::TITLE_NOT_LOADING);
2164 }
2165
[email protected]2b4355c2009-04-04 17:55:462166 // If the only updates were synchronously handled above, we're done.
[email protected]8030f012009-09-25 18:09:372167 if (changed_flags == 0)
[email protected]2b4355c2009-04-04 17:55:462168 return;
[email protected]36b6dcb2008-11-12 01:19:572169
2170 // Save the dirty bits.
[email protected]85354982009-09-25 19:58:402171 scheduled_updates_[source] |= changed_flags;
[email protected]36b6dcb2008-11-12 01:19:572172
[email protected]24f1477532011-11-22 22:55:362173 if (!chrome_updater_factory_.HasWeakPtrs()) {
[email protected]36b6dcb2008-11-12 01:19:572174 // No task currently scheduled, start another.
skyostil380bb2222015-06-12 12:07:052175 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
2176 FROM_HERE, base::Bind(&Browser::ProcessPendingUIUpdates,
2177 chrome_updater_factory_.GetWeakPtr()),
[email protected]11d6ab1a2012-01-12 21:37:262178 base::TimeDelta::FromMilliseconds(kUIUpdateCoalescingTimeMS));
[email protected]36b6dcb2008-11-12 01:19:572179 }
2180}
2181
2182void Browser::ProcessPendingUIUpdates() {
2183#ifndef NDEBUG
2184 // Validate that all tabs we have pending updates for exist. This is scary
2185 // because the pending list must be kept in sync with any detached or
[email protected]a5231662009-09-26 00:56:002186 // deleted tabs.
[email protected]85354982009-09-25 19:58:402187 for (UpdateMap::const_iterator i = scheduled_updates_.begin();
2188 i != scheduled_updates_.end(); ++i) {
[email protected]36b6dcb2008-11-12 01:19:572189 bool found = false;
[email protected]e0448872013-01-11 19:35:022190 for (int tab = 0; tab < tab_strip_model_->count(); tab++) {
[email protected]0d56fa7212012-11-15 23:34:022191 if (tab_strip_model_->GetWebContentsAt(tab) == i->first) {
[email protected]36b6dcb2008-11-12 01:19:572192 found = true;
2193 break;
2194 }
2195 }
2196 DCHECK(found);
2197 }
2198#endif
2199
[email protected]24f1477532011-11-22 22:55:362200 chrome_updater_factory_.InvalidateWeakPtrs();
[email protected]36b6dcb2008-11-12 01:19:572201
robliao1766df52015-03-14 01:06:262202 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/467185
2203 // is fixed.
2204 tracked_objects::ScopedTracker tracking_profile1(
2205 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2206 "467185 Browser::ProcessPendingUIUpdates1"));
2207
[email protected]85354982009-09-25 19:58:402208 for (UpdateMap::const_iterator i = scheduled_updates_.begin();
2209 i != scheduled_updates_.end(); ++i) {
[email protected]36b6dcb2008-11-12 01:19:572210 // Do not dereference |contents|, it may be out-of-date!
[email protected]2a6bc3e2011-12-28 23:51:332211 const WebContents* contents = i->first;
[email protected]85354982009-09-25 19:58:402212 unsigned flags = i->second;
[email protected]36b6dcb2008-11-12 01:19:572213
[email protected]0d56fa7212012-11-15 23:34:022214 if (contents == tab_strip_model_->GetActiveWebContents()) {
robliao1766df52015-03-14 01:06:262215 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/467185
2216 // is fixed.
2217 tracked_objects::ScopedTracker tracking_profile2(
2218 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2219 "467185 Browser::ProcessPendingUIUpdates2"));
2220
[email protected]a5231662009-09-26 00:56:002221 // Updates that only matter when the tab is selected go here.
[email protected]f7f3a5f2009-05-01 22:02:342222
[email protected]6ebdc9b2010-09-27 16:55:572223 // Updating the URL happens synchronously in ScheduleUIUpdate.
[email protected]d9083482012-01-06 00:38:462224 if (flags & content::INVALIDATE_TYPE_LOAD && GetStatusBubble()) {
[email protected]0d56fa7212012-11-15 23:34:022225 GetStatusBubble()->SetStatus(CoreTabHelper::FromWebContents(
2226 tab_strip_model_->GetActiveWebContents())->GetStatusText());
[email protected]0dd3a0ab2011-02-18 08:17:442227 }
[email protected]a5231662009-09-26 00:56:002228
[email protected]d9083482012-01-06 00:38:462229 if (flags & (content::INVALIDATE_TYPE_TAB |
2230 content::INVALIDATE_TYPE_TITLE)) {
[email protected]a5231662009-09-26 00:56:002231 window_->UpdateTitleBar();
2232 }
[email protected]4d34e2e2009-05-26 22:55:282233 }
[email protected]36b6dcb2008-11-12 01:19:572234
[email protected]a5231662009-09-26 00:56:002235 // Updates that don't depend upon the selected state go here.
[email protected]d9083482012-01-06 00:38:462236 if (flags &
2237 (content::INVALIDATE_TYPE_TAB | content::INVALIDATE_TYPE_TITLE)) {
robliao1766df52015-03-14 01:06:262238 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/467185
2239 // is fixed.
2240 tracked_objects::ScopedTracker tracking_profile3(
2241 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2242 "467185 Browser::ProcessPendingUIUpdates3"));
[email protected]526b45e2012-11-28 20:22:462243 tab_strip_model_->UpdateWebContentsStateAt(
[email protected]e52d0a42012-06-08 22:44:162244 tab_strip_model_->GetIndexOfWebContents(contents),
[email protected]f1cd5e82009-10-23 17:19:032245 TabStripModelObserver::ALL);
[email protected]36b6dcb2008-11-12 01:19:572246 }
2247
[email protected]13155a22013-03-19 23:33:542248 // Update the bookmark bar. It may happen that the tab is crashed, and if
2249 // so, the bookmark bar should be hidden.
robliao1766df52015-03-14 01:06:262250 if (flags & content::INVALIDATE_TYPE_TAB) {
2251 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/467185
2252 // is fixed.
2253 tracked_objects::ScopedTracker tracking_profile4(
2254 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2255 "467185 Browser::ProcessPendingUIUpdates4"));
[email protected]13155a22013-03-19 23:33:542256 UpdateBookmarkBarState(BOOKMARK_BAR_STATE_CHANGE_TAB_STATE);
robliao1766df52015-03-14 01:06:262257 }
[email protected]13155a22013-03-19 23:33:542258
[email protected]36b6dcb2008-11-12 01:19:572259 // We don't need to process INVALIDATE_STATE, since that's not visible.
2260 }
2261
2262 scheduled_updates_.clear();
2263}
2264
[email protected]d9083482012-01-06 00:38:462265void Browser::RemoveScheduledUpdatesFor(WebContents* contents) {
[email protected]36b6dcb2008-11-12 01:19:572266 if (!contents)
2267 return;
2268
[email protected]85354982009-09-25 19:58:402269 UpdateMap::iterator i = scheduled_updates_.find(contents);
2270 if (i != scheduled_updates_.end())
2271 scheduled_updates_.erase(i);
initial.commit09911bf2008-07-26 23:55:292272}
2273
[email protected]36b6dcb2008-11-12 01:19:572274///////////////////////////////////////////////////////////////////////////////
2275// Browser, Getters for UI (private):
initial.commit09911bf2008-07-26 23:55:292276
[email protected]36b6dcb2008-11-12 01:19:572277StatusBubble* Browser::GetStatusBubble() {
[email protected]8e837ec2013-01-31 01:48:332278 // In kiosk and exclusive app mode, we want to always hide the status bubble.
2279 if (chrome::IsRunningInAppMode())
[email protected]34930432009-11-09 00:12:092280 return NULL;
[email protected]8e837ec2013-01-31 01:48:332281
[email protected]9b032bf2009-07-21 17:34:232282 return window_ ? window_->GetStatusBubble() : NULL;
initial.commit09911bf2008-07-26 23:55:292283}
2284
[email protected]36b6dcb2008-11-12 01:19:572285///////////////////////////////////////////////////////////////////////////////
2286// Browser, Session restore functions (private):
initial.commit09911bf2008-07-26 23:55:292287
2288void Browser::SyncHistoryWithTabs(int index) {
[email protected]92371eb2011-04-28 11:50:152289 SessionService* session_service =
2290 SessionServiceFactory::GetForProfileIfExisting(profile());
initial.commit09911bf2008-07-26 23:55:292291 if (session_service) {
[email protected]e0448872013-01-11 19:35:022292 for (int i = index; i < tab_strip_model_->count(); ++i) {
[email protected]0d56fa7212012-11-15 23:34:022293 WebContents* web_contents = tab_strip_model_->GetWebContentsAt(i);
[email protected]50a9a76b2012-09-12 14:19:362294 if (web_contents) {
2295 SessionTabHelper* session_tab_helper =
2296 SessionTabHelper::FromWebContents(web_contents);
initial.commit09911bf2008-07-26 23:55:292297 session_service->SetTabIndexInWindow(
[email protected]50a9a76b2012-09-12 14:19:362298 session_id(), session_tab_helper->session_id(), i);
[email protected]ecc523f62010-09-28 22:45:402299 session_service->SetPinnedState(
2300 session_id(),
[email protected]50a9a76b2012-09-12 14:19:362301 session_tab_helper->session_id(),
[email protected]52877dbc62012-06-29 22:22:032302 tab_strip_model_->IsTabPinned(i));
initial.commit09911bf2008-07-26 23:55:292303 }
2304 }
2305 }
2306}
2307
[email protected]36b6dcb2008-11-12 01:19:572308///////////////////////////////////////////////////////////////////////////////
[email protected]69444cc2009-04-09 20:40:062309// Browser, In-progress download termination handling (private):
2310
[email protected]b6a4ac2b2011-10-17 20:05:482311bool Browser::CanCloseWithInProgressDownloads() {
[email protected]5a17d492011-10-24 19:37:032312 // If we've prompted, we need to hear from the user before we
2313 // can close.
2314 if (cancel_download_confirmation_state_ != NOT_PROMPTED)
2315 return cancel_download_confirmation_state_ != WAITING_FOR_RESPONSE;
2316
2317 int num_downloads_blocking;
[email protected]edfca702013-08-16 08:58:142318 Browser::DownloadClosePreventionType dialog_type =
2319 OkToCloseWithInProgressDownloads(&num_downloads_blocking);
2320 if (dialog_type == DOWNLOAD_CLOSE_OK)
[email protected]446295a2010-04-19 23:43:002321 return true;
2322
[email protected]5a17d492011-10-24 19:37:032323 // Closing this window will kill some downloads; prompt to make sure
2324 // that's ok.
[email protected]69444cc2009-04-09 20:40:062325 cancel_download_confirmation_state_ = WAITING_FOR_RESPONSE;
[email protected]edfca702013-08-16 08:58:142326 window_->ConfirmBrowserCloseWithPendingDownloads(
2327 num_downloads_blocking,
2328 dialog_type,
2329 false,
2330 base::Bind(&Browser::InProgressDownloadResponse,
2331 weak_factory_.GetWeakPtr()));
[email protected]69444cc2009-04-09 20:40:062332
2333 // Return false so the browser does not close. We'll close if the user
2334 // confirms in the dialog.
2335 return false;
2336}
2337
2338///////////////////////////////////////////////////////////////////////////////
[email protected]36b6dcb2008-11-12 01:19:572339// Browser, Assorted utility functions (private):
initial.commit09911bf2008-07-26 23:55:292340
[email protected]63d1f9b2014-07-05 19:09:032341void Browser::SetAsDelegate(WebContents* web_contents, bool set_delegate) {
2342 Browser* delegate = set_delegate ? this : NULL;
[email protected]27e5b3912012-12-16 00:45:372343 // WebContents...
2344 web_contents->SetDelegate(delegate);
2345
2346 // ...and all the helpers.
[email protected]27e5b3912012-12-16 00:45:372347 BookmarkTabHelper::FromWebContents(web_contents)->set_delegate(delegate);
[email protected]d2b16572013-01-03 00:41:582348 WebContentsModalDialogManager::FromWebContents(web_contents)->
[email protected]5c2c7792013-08-20 18:39:162349 SetDelegate(delegate);
[email protected]27e5b3912012-12-16 00:45:372350 CoreTabHelper::FromWebContents(web_contents)->set_delegate(delegate);
[email protected]82808d02014-04-10 00:10:592351 SearchTabHelper::FromWebContents(web_contents)->set_delegate(delegate);
droger38f0d102014-09-16 15:37:412352 translate::ContentTranslateDriver& content_translate_driver =
2353 ChromeTranslateClient::FromWebContents(web_contents)->translate_driver();
2354 if (delegate) {
juncaia64f769c22016-06-04 00:55:322355 zoom::ZoomController::FromWebContents(web_contents)->AddObserver(this);
pkastingcc29d20e2015-02-25 06:05:532356 content_translate_driver.AddObserver(this);
droger38f0d102014-09-16 15:37:412357 } else {
juncaia64f769c22016-06-04 00:55:322358 zoom::ZoomController::FromWebContents(web_contents)->RemoveObserver(this);
pkastingcc29d20e2015-02-25 06:05:532359 content_translate_driver.RemoveObserver(this);
droger38f0d102014-09-16 15:37:412360 }
[email protected]27e5b3912012-12-16 00:45:372361}
2362
[email protected]36b6dcb2008-11-12 01:19:572363void Browser::CloseFrame() {
2364 window_->Close();
initial.commit09911bf2008-07-26 23:55:292365}
2366
[email protected]e89cfcb2012-11-11 14:47:242367void Browser::TabDetachedAtImpl(content::WebContents* contents,
2368 int index,
[email protected]c37e3b62010-01-22 00:15:172369 DetachType type) {
2370 if (type == DETACH_TYPE_DETACH) {
[email protected]56580852010-11-17 16:09:152371 // Save the current location bar state, but only if the tab being detached
2372 // is the selected tab. Because saving state can conditionally revert the
2373 // location bar, saving the current tab's location bar state to a
2374 // non-selected tab can corrupt both tabs.
[email protected]0d56fa7212012-11-15 23:34:022375 if (contents == tab_strip_model_->GetActiveWebContents()) {
[email protected]9800de5e2011-03-11 18:19:492376 LocationBar* location_bar = window()->GetLocationBar();
2377 if (location_bar)
[email protected]e89cfcb2012-11-11 14:47:242378 location_bar->SaveStateToContents(contents);
[email protected]9800de5e2011-03-11 18:19:492379 }
[email protected]c37e3b62010-01-22 00:15:172380
[email protected]2fc15ae2012-05-06 00:01:372381 if (!tab_strip_model_->closing_all())
[email protected]c37e3b62010-01-22 00:15:172382 SyncHistoryWithTabs(0);
2383 }
2384
[email protected]63d1f9b2014-07-05 19:09:032385 SetAsDelegate(contents, false);
[email protected]e89cfcb2012-11-11 14:47:242386 RemoveScheduledUpdatesFor(contents);
[email protected]c37e3b62010-01-22 00:15:172387
[email protected]04e3ead52013-01-14 01:09:202388 if (find_bar_controller_.get() && index == tab_strip_model_->active_index()) {
[email protected]ba3d09a12012-11-07 12:13:522389 find_bar_controller_->ChangeWebContents(NULL);
[email protected]ecc523f62010-09-28 22:45:402390 }
[email protected]c37e3b62010-01-22 00:15:172391
[email protected]e3a5d5b2012-06-22 17:56:462392 // Stop observing search model changes for this tab.
[email protected]e89cfcb2012-11-11 14:47:242393 search_delegate_->OnTabDetached(contents);
[email protected]e3a5d5b2012-06-22 17:56:462394
[email protected]20ca0382013-02-28 19:50:072395 for (size_t i = 0; i < interstitial_observers_.size(); i++) {
2396 if (interstitial_observers_[i]->web_contents() != contents)
2397 continue;
2398
2399 delete interstitial_observers_[i];
2400 interstitial_observers_.erase(interstitial_observers_.begin() + i);
2401 return;
2402 }
[email protected]c37e3b62010-01-22 00:15:172403}
2404
benwellsa2a8a9f72015-04-07 07:09:542405bool Browser::SupportsLocationBar() const {
[email protected]d2202e22014-04-14 20:20:532406 // Tabbed browser always show a location bar.
2407 if (is_type_tabbed())
[email protected]8a585cb2014-03-21 17:13:522408 return true;
[email protected]d2202e22014-04-14 20:20:532409
benwells919e79b2015-03-19 04:58:192410 // Non-app windows that aren't tabbed or system windows should always show a
2411 // location bar, unless they are from a trusted source.
2412 if (!is_app())
2413 return !is_trusted_source();
2414
benwells11bd6ed2015-06-05 04:20:412415 if (hosted_app_controller_)
2416 return hosted_app_controller_->SupportsLocationBar();
benwells919e79b2015-03-19 04:58:192417
benwellsa2a8a9f72015-04-07 07:09:542418 return false;
[email protected]8a585cb2014-03-21 17:13:522419}
2420
benwells502df692014-12-02 20:41:452421bool Browser::ShouldUseWebAppFrame() const {
benwells919e79b2015-03-19 04:58:192422 // Only use the web app frame for apps in ash, and only if the web app frame
2423 // is enabled.
benwellsa2a8a9f72015-04-07 07:09:542424 if (!is_app())
benwells502df692014-12-02 20:41:452425 return false;
benwells502df692014-12-02 20:41:452426
benwells11bd6ed2015-06-05 04:20:412427 if (hosted_app_controller_)
2428 return hosted_app_controller_->should_use_web_app_frame();
benwellsa2a8a9f72015-04-07 07:09:542429
2430 return false;
benwells502df692014-12-02 20:41:452431}
2432
[email protected]92655362012-04-20 21:33:172433bool Browser::SupportsWindowFeatureImpl(WindowFeature feature,
2434 bool check_fullscreen) const {
[email protected]c3e32942012-12-11 10:26:202435 bool hide_ui_for_fullscreen = check_fullscreen && ShouldHideUIForFullscreen();
[email protected]92655362012-04-20 21:33:172436
2437 unsigned int features = FEATURE_INFOBAR | FEATURE_DOWNLOADSHELF;
2438
2439 if (is_type_tabbed())
2440 features |= FEATURE_BOOKMARKBAR;
2441
2442 if (!hide_ui_for_fullscreen) {
2443 if (!is_type_tabbed())
2444 features |= FEATURE_TITLEBAR;
2445
2446 if (is_type_tabbed())
2447 features |= FEATURE_TABSTRIP;
2448
2449 if (is_type_tabbed())
2450 features |= FEATURE_TOOLBAR;
2451
benwellsa2a8a9f72015-04-07 07:09:542452 if (SupportsLocationBar())
[email protected]92655362012-04-20 21:33:172453 features |= FEATURE_LOCATIONBAR;
benwells502df692014-12-02 20:41:452454
2455 if (ShouldUseWebAppFrame())
2456 features |= FEATURE_WEBAPPFRAME;
[email protected]92655362012-04-20 21:33:172457 }
2458 return !!(features & feature);
2459}
2460
[email protected]09b29342011-06-24 19:18:482461void Browser::UpdateBookmarkBarState(BookmarkBarStateChangeReason reason) {
robliaoe879c2bb2015-03-21 00:46:372462 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/467185 is
2463 // fixed.
2464 tracked_objects::ScopedTracker tracking_profile1(
2465 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2466 "467185 Browser::UpdateBookmarkBarState1"));
[email protected]09b29342011-06-24 19:18:482467 BookmarkBar::State state;
[email protected]a7666d82014-04-04 21:34:032468 // The bookmark bar is always hidden for Guest Sessions and in fullscreen
2469 // mode, unless on the new tab page.
2470 if (profile_->IsGuestSession()) {
2471 state = BookmarkBar::HIDDEN;
2472 } else if (browser_defaults::bookmarks_enabled &&
tfarina3bddbe112014-08-28 05:29:322473 profile_->GetPrefs()->GetBoolean(bookmarks::prefs::kShowBookmarkBar) &&
[email protected]c3e32942012-12-11 10:26:202474 !ShouldHideUIForFullscreen()) {
[email protected]04143362012-11-28 14:25:182475 state = BookmarkBar::SHOW;
[email protected]09b29342011-06-24 19:18:482476 } else {
robliaoe879c2bb2015-03-21 00:46:372477 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/467185
2478 // is fixed.
2479 tracked_objects::ScopedTracker tracking_profile2(
2480 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2481 "467185 Browser::UpdateBookmarkBarState2"));
[email protected]0d56fa7212012-11-15 23:34:022482 WebContents* web_contents = tab_strip_model_->GetActiveWebContents();
[email protected]1c5119c2012-09-19 00:08:572483 BookmarkTabHelper* bookmark_tab_helper =
2484 web_contents ? BookmarkTabHelper::FromWebContents(web_contents) : NULL;
2485 if (bookmark_tab_helper && bookmark_tab_helper->ShouldShowBookmarkBar())
[email protected]09b29342011-06-24 19:18:482486 state = BookmarkBar::DETACHED;
2487 else
2488 state = BookmarkBar::HIDDEN;
2489 }
[email protected]2d4e43a02012-06-26 00:11:552490
[email protected]09b29342011-06-24 19:18:482491 if (state == bookmark_bar_state_)
2492 return;
2493
2494 bookmark_bar_state_ = state;
2495
2496 if (!window_)
2497 return; // This is called from the constructor when window_ is NULL.
2498
2499 if (reason == BOOKMARK_BAR_STATE_CHANGE_TAB_SWITCH) {
2500 // Don't notify BrowserWindow on a tab switch as at the time this is invoked
2501 // BrowserWindow hasn't yet switched tabs. The BrowserWindow implementations
2502 // end up querying state once they process the tab switch.
2503 return;
2504 }
2505
robliaoe879c2bb2015-03-21 00:46:372506 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/467185 is
2507 // fixed.
2508 tracked_objects::ScopedTracker tracking_profile3(
2509 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2510 "467185 Browser::UpdateBookmarkBarState3"));
2511
tfarina41f42082014-12-26 14:08:122512 bool should_animate = reason == BOOKMARK_BAR_STATE_CHANGE_PREF_CHANGE;
2513 window_->BookmarkBarStateChanged(should_animate ?
[email protected]09b29342011-06-24 19:18:482514 BookmarkBar::ANIMATE_STATE_CHANGE :
[email protected]e2395da2013-01-23 22:05:352515 BookmarkBar::DONT_ANIMATE_STATE_CHANGE);
[email protected]09b29342011-06-24 19:18:482516}
[email protected]2e2cacc2011-07-12 21:54:262517
[email protected]c3e32942012-12-11 10:26:202518bool Browser::ShouldHideUIForFullscreen() const {
[email protected]6a414ff2013-02-27 08:22:542519 // Windows and GTK remove the top controls in fullscreen, but Mac and Ash
2520 // keep the controls in a slide-down panel.
2521 return window_ && window_->ShouldHideUIForFullscreen();
[email protected]c3e32942012-12-11 10:26:202522}
2523
thestig028978692015-10-08 07:39:402524bool Browser::ShouldStartShutdown() const {
scottmg0d8e4ab2016-01-28 00:34:552525 return BrowserList::GetInstance()->size() <= 1;
thestig028978692015-10-08 07:39:402526}
2527
[email protected]dd6730412013-08-14 15:03:372528bool Browser::MaybeCreateBackgroundContents(
dcheng3ce04b62015-10-26 23:30:552529 int32_t route_id,
2530 int32_t main_frame_route_id,
2531 int32_t main_frame_widget_route_id,
[email protected]dd6730412013-08-14 15:03:372532 WebContents* opener_web_contents,
nasko48321ca32015-07-02 20:44:122533 const std::string& frame_name,
[email protected]dd6730412013-08-14 15:03:372534 const GURL& target_url,
2535 const std::string& partition_id,
2536 content::SessionStorageNamespace* session_storage_namespace) {
[email protected]92655362012-04-20 21:33:172537 GURL opener_url = opener_web_contents->GetURL();
[email protected]06bdd2b2012-11-30 18:47:132538 ExtensionService* extensions_service =
2539 extensions::ExtensionSystem::Get(profile_)->extension_service();
[email protected]92655362012-04-20 21:33:172540
2541 if (!opener_url.is_valid() ||
2542 frame_name.empty() ||
2543 !extensions_service ||
2544 !extensions_service->is_ready())
2545 return false;
2546
2547 // Only hosted apps have web extents, so this ensures that only hosted apps
2548 // can create BackgroundContents. We don't have to check for background
2549 // permission as that is checked in RenderMessageFilter when the CreateWindow
2550 // message is processed.
reillyga3acbc12014-11-11 23:17:122551 const Extension* extension = extensions::ExtensionRegistry::Get(profile_)
2552 ->enabled_extensions()
2553 .GetHostedAppByURL(opener_url);
[email protected]92655362012-04-20 21:33:172554 if (!extension)
2555 return false;
2556
2557 // No BackgroundContents allowed if BackgroundContentsService doesn't exist.
2558 BackgroundContentsService* service =
2559 BackgroundContentsServiceFactory::GetForProfile(profile_);
2560 if (!service)
2561 return false;
2562
2563 // Ensure that we're trying to open this from the extension's process.
2564 SiteInstance* opener_site_instance = opener_web_contents->GetSiteInstance();
[email protected]fafdc842014-01-17 18:09:082565 extensions::ProcessMap* process_map = extensions::ProcessMap::Get(profile_);
[email protected]92655362012-04-20 21:33:172566 if (!opener_site_instance->GetProcess() ||
2567 !process_map->Contains(
2568 extension->id(), opener_site_instance->GetProcess()->GetID())) {
2569 return false;
2570 }
2571
2572 // Only allow a single background contents per app.
[email protected]9367eabc2013-03-01 01:29:292573 bool allow_js_access = extensions::BackgroundInfo::AllowJSAccess(extension);
[email protected]92655362012-04-20 21:33:172574 BackgroundContents* existing =
[email protected]1f136902013-12-24 23:26:452575 service->GetAppBackgroundContents(base::ASCIIToUTF16(extension->id()));
[email protected]92655362012-04-20 21:33:172576 if (existing) {
2577 // For non-scriptable background contents, ignore the request altogether,
2578 // (returning true, so that a regular WebContents isn't created either).
2579 if (!allow_js_access)
2580 return true;
2581 // For scriptable background pages, if one already exists, close it (even
2582 // if it was specified in the manifest).
[email protected]92655362012-04-20 21:33:172583 delete existing;
2584 }
2585
2586 // If script access is not allowed, create the the background contents in a
2587 // new SiteInstance, so that a separate process is used.
2588 scoped_refptr<content::SiteInstance> site_instance =
2589 allow_js_access ?
2590 opener_site_instance :
2591 content::SiteInstance::Create(opener_web_contents->GetBrowserContext());
2592
2593 // Passed all the checks, so this should be created as a BackgroundContents.
dcheng3ce04b62015-10-26 23:30:552594 BackgroundContents* contents = service->CreateBackgroundContents(
2595 site_instance.get(), route_id, main_frame_route_id,
2596 main_frame_widget_route_id, profile_, frame_name,
2597 base::ASCIIToUTF16(extension->id()), partition_id,
2598 session_storage_namespace);
[email protected]92655362012-04-20 21:33:172599
2600 // When a separate process is used, the original renderer cannot access the
2601 // new window later, thus we need to navigate the window now.
2602 if (contents && !allow_js_access) {
2603 contents->web_contents()->GetController().LoadURL(
2604 target_url,
2605 content::Referrer(),
Sylvain Defresnec6ccc77d2014-09-19 10:19:352606 ui::PAGE_TRANSITION_LINK,
[email protected]92655362012-04-20 21:33:172607 std::string()); // No extra headers.
2608 }
2609
2610 return contents != NULL;
[email protected]afcb43562011-09-02 23:33:382611}