sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #import "ios/chrome/browser/ui/browser_view_controller.h" |
| 6 | |
| 7 | #import <AssetsLibrary/AssetsLibrary.h> |
| 8 | #import <MobileCoreServices/MobileCoreServices.h> |
| 9 | #import <PassKit/PassKit.h> |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 10 | #import <QuartzCore/QuartzCore.h> |
| 11 | |
| 12 | #include <stdint.h> |
| 13 | #include <cmath> |
| 14 | #include <memory> |
| 15 | |
| 16 | #include "base/base64.h" |
| 17 | #include "base/command_line.h" |
mathp | 9b4c11d | 2017-07-06 20:24:13 | [diff] [blame] | 18 | #include "base/feature_list.h" |
gambard | 9efce7a | 2017-02-09 18:53:17 | [diff] [blame] | 19 | #include "base/files/file_path.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 20 | #include "base/i18n/rtl.h" |
| 21 | #include "base/ios/block_types.h" |
| 22 | #include "base/ios/ios_util.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 23 | #include "base/logging.h" |
| 24 | #include "base/mac/bind_objc_block.h" |
| 25 | #include "base/mac/bundle_locations.h" |
| 26 | #include "base/mac/foundation_util.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 27 | #include "base/macros.h" |
| 28 | #include "base/memory/ptr_util.h" |
asvitkine | f1899e3 | 2017-01-27 16:30:29 | [diff] [blame] | 29 | #include "base/metrics/histogram_macros.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 30 | #include "base/metrics/user_metrics.h" |
| 31 | #include "base/metrics/user_metrics_action.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 32 | #include "base/strings/sys_string_conversions.h" |
rohitrao | cd324eb7 | 2017-04-04 15:36:39 | [diff] [blame] | 33 | #include "base/strings/utf_string_conversions.h" |
Sylvain Defresne | fd3ecf2 | 2017-07-12 18:47:24 | [diff] [blame] | 34 | #include "base/task_scheduler/post_task.h" |
tzik | 1423603 | 2017-02-15 06:41:01 | [diff] [blame] | 35 | #include "base/threading/sequenced_worker_pool.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 36 | #include "components/bookmarks/browser/base_bookmark_model_observer.h" |
| 37 | #include "components/bookmarks/browser/bookmark_model.h" |
Sylvain Defresne | 7178d4c | 2017-09-14 13:22:37 | [diff] [blame] | 38 | #include "components/favicon/ios/web_favicon_driver.h" |
Tommy Nyquist | c1d6dea1 | 2017-07-26 20:37:23 | [diff] [blame] | 39 | #include "components/feature_engagement/public/event_constants.h" |
Cooper Knaak | e4f495cf | 2017-07-27 23:30:03 | [diff] [blame] | 40 | #include "components/feature_engagement/public/feature_constants.h" |
Tommy Nyquist | c1d6dea1 | 2017-07-26 20:37:23 | [diff] [blame] | 41 | #include "components/feature_engagement/public/tracker.h" |
gambard | bdc07cc | 2017-02-03 16:43:11 | [diff] [blame] | 42 | #include "components/image_fetcher/ios/ios_image_data_fetcher_wrapper.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 43 | #include "components/infobars/core/infobar_manager.h" |
Mark Cogan | ca30df6 | 2017-11-20 14:29:11 | [diff] [blame] | 44 | #import "components/language/ios/browser/ios_language_detection_tab_helper.h" |
mathp | 9b4c11d | 2017-07-06 20:24:13 | [diff] [blame] | 45 | #include "components/payments/core/features.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 46 | #include "components/prefs/pref_service.h" |
olivierrobin | 52b6cd6ec | 2017-03-23 13:55:54 | [diff] [blame] | 47 | #include "components/reading_list/core/reading_list_model.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 48 | #include "components/search_engines/search_engines_pref_names.h" |
| 49 | #include "components/search_engines/template_url_service.h" |
Sylvain Defresne | f2e00d9b | 2017-08-24 10:54:05 | [diff] [blame] | 50 | #include "components/sessions/core/session_types.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 51 | #include "components/sessions/core/tab_restore_service_helper.h" |
edchin | cd32fdf | 2017-10-25 12:45:45 | [diff] [blame] | 52 | #include "components/signin/core/browser/account_reconcilor.h" |
| 53 | #include "components/signin/core/browser/signin_metrics.h" |
| 54 | #import "components/signin/ios/browser/account_consistency_service.h" |
Eugene But | c90499d5 | 2017-09-22 16:02:09 | [diff] [blame] | 55 | #include "components/signin/ios/browser/active_state_manager.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 56 | #include "components/strings/grit/components_strings.h" |
| 57 | #include "components/toolbar/toolbar_model_impl.h" |
| 58 | #include "ios/chrome/app/tests_hook.h" |
| 59 | #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" |
| 60 | #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 61 | #include "ios/chrome/browser/chrome_url_constants.h" |
| 62 | #include "ios/chrome/browser/chrome_url_util.h" |
Eugene But | 49a7c57 | 2017-12-11 20:54:15 | [diff] [blame] | 63 | #import "ios/chrome/browser/download/pass_kit_tab_helper.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 64 | #include "ios/chrome/browser/experimental_flags.h" |
| 65 | #import "ios/chrome/browser/favicon/favicon_loader.h" |
| 66 | #include "ios/chrome/browser/favicon/ios_chrome_favicon_loader_factory.h" |
Tommy Nyquist | c1d6dea1 | 2017-07-26 20:37:23 | [diff] [blame] | 67 | #include "ios/chrome/browser/feature_engagement/tracker_factory.h" |
| 68 | #include "ios/chrome/browser/feature_engagement/tracker_util.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 69 | #import "ios/chrome/browser/find_in_page/find_in_page_controller.h" |
| 70 | #import "ios/chrome/browser/find_in_page/find_in_page_model.h" |
rohitrao | b2bf3cb | 2017-02-10 14:10:36 | [diff] [blame] | 71 | #import "ios/chrome/browser/find_in_page/find_tab_helper.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 72 | #include "ios/chrome/browser/first_run/first_run.h" |
| 73 | #import "ios/chrome/browser/geolocation/omnibox_geolocation_controller.h" |
Mark Cogan | 80aa28d | 2017-11-30 13:11:34 | [diff] [blame] | 74 | #include "ios/chrome/browser/infobars/infobar_container_delegate_ios.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 75 | #include "ios/chrome/browser/infobars/infobar_container_ios.h" |
Mark Cogan | 80aa28d | 2017-11-30 13:11:34 | [diff] [blame] | 76 | #import "ios/chrome/browser/infobars/infobar_container_state_delegate.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 77 | #include "ios/chrome/browser/infobars/infobar_container_view.h" |
Rohit Rao | af46af9 | 2017-08-10 12:52:30 | [diff] [blame] | 78 | #include "ios/chrome/browser/infobars/infobar_manager_impl.h" |
Mark Cogan | ca30df6 | 2017-11-20 14:29:11 | [diff] [blame] | 79 | #import "ios/chrome/browser/language/url_language_histogram_factory.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 80 | #import "ios/chrome/browser/metrics/new_tab_page_uma.h" |
Mark Cogan | fc591c8c | 2018-01-02 16:07:00 | [diff] [blame^] | 81 | #import "ios/chrome/browser/metrics/size_class_recorder.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 82 | #include "ios/chrome/browser/metrics/tab_usage_recorder.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 83 | #import "ios/chrome/browser/passwords/password_controller.h" |
Tomasz Garbus | b844e99 | 2017-09-29 12:44:55 | [diff] [blame] | 84 | #include "ios/chrome/browser/passwords/password_tab_helper.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 85 | #include "ios/chrome/browser/pref_names.h" |
Rohit Rao | 44f20430 | 2017-08-10 14:49:54 | [diff] [blame] | 86 | #import "ios/chrome/browser/prerender/preload_controller_delegate.h" |
| 87 | #import "ios/chrome/browser/prerender/prerender_service.h" |
| 88 | #import "ios/chrome/browser/prerender/prerender_service_factory.h" |
olivierrobin | 013ba67 | 2017-03-01 21:16:24 | [diff] [blame] | 89 | #include "ios/chrome/browser/reading_list/offline_url_utils.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 90 | #include "ios/chrome/browser/reading_list/reading_list_model_factory.h" |
| 91 | #include "ios/chrome/browser/search_engines/template_url_service_factory.h" |
| 92 | #include "ios/chrome/browser/sessions/ios_chrome_tab_restore_service_factory.h" |
Sylvain Defresne | f2e00d9b | 2017-08-24 10:54:05 | [diff] [blame] | 93 | #include "ios/chrome/browser/sessions/session_util.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 94 | #include "ios/chrome/browser/sessions/tab_restore_service_delegate_impl_ios.h" |
| 95 | #include "ios/chrome/browser/sessions/tab_restore_service_delegate_impl_ios_factory.h" |
edchin | cd32fdf | 2017-10-25 12:45:45 | [diff] [blame] | 96 | #import "ios/chrome/browser/signin/account_consistency_service_factory.h" |
| 97 | #include "ios/chrome/browser/signin/account_reconcilor_factory.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 98 | #import "ios/chrome/browser/snapshots/snapshot_cache.h" |
| 99 | #import "ios/chrome/browser/snapshots/snapshot_overlay.h" |
| 100 | #import "ios/chrome/browser/snapshots/snapshot_overlay_provider.h" |
Sylvain Defresne | 17b8aa4 | 2017-12-21 16:17:17 | [diff] [blame] | 101 | #import "ios/chrome/browser/snapshots/snapshot_tab_helper.h" |
Mike Dougherty | 4620cf8e | 2017-10-31 23:37:09 | [diff] [blame] | 102 | #import "ios/chrome/browser/ssl/captive_portal_detector_tab_helper.h" |
| 103 | #import "ios/chrome/browser/ssl/captive_portal_detector_tab_helper_delegate.h" |
pkl | d6e73e5 | 2017-03-08 15:56:51 | [diff] [blame] | 104 | #import "ios/chrome/browser/store_kit/store_kit_tab_helper.h" |
sdefresne | 0452a9d | 2017-02-09 15:33:28 | [diff] [blame] | 105 | #import "ios/chrome/browser/tabs/legacy_tab_helper.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 106 | #import "ios/chrome/browser/tabs/tab.h" |
| 107 | #import "ios/chrome/browser/tabs/tab_dialog_delegate.h" |
olivierrobin | 9ce77b8 | 2017-01-12 17:29:19 | [diff] [blame] | 108 | #import "ios/chrome/browser/tabs/tab_headers_delegate.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 109 | #import "ios/chrome/browser/tabs/tab_model.h" |
| 110 | #import "ios/chrome/browser/tabs/tab_model_observer.h" |
Sylvain Defresne | 72c530e4 | 2017-08-25 15:28:16 | [diff] [blame] | 111 | #import "ios/chrome/browser/tabs/tab_private.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 112 | #import "ios/chrome/browser/tabs/tab_snapshotting_delegate.h" |
Mark Cogan | ca30df6 | 2017-11-20 14:29:11 | [diff] [blame] | 113 | #import "ios/chrome/browser/translate/chrome_ios_translate_client.h" |
| 114 | #import "ios/chrome/browser/translate/language_selection_handler.h" |
Rohit Rao | 01e0e00 | 2017-08-14 20:49:43 | [diff] [blame] | 115 | #import "ios/chrome/browser/ui/activity_services/activity_service_legacy_coordinator.h" |
| 116 | #import "ios/chrome/browser/ui/activity_services/requirements/activity_service_presentation.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 117 | #import "ios/chrome/browser/ui/alert_coordinator/alert_coordinator.h" |
Eugene But | 35ded55 | 2017-09-13 23:31:59 | [diff] [blame] | 118 | #import "ios/chrome/browser/ui/alert_coordinator/repost_form_coordinator.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 119 | #import "ios/chrome/browser/ui/authentication/re_signin_infobar_delegate.h" |
| 120 | #import "ios/chrome/browser/ui/background_generator.h" |
| 121 | #import "ios/chrome/browser/ui/bookmarks/bookmark_interaction_controller.h" |
Gauthier Ambard | 65e949b09 | 2017-11-29 08:46:20 | [diff] [blame] | 122 | #include "ios/chrome/browser/ui/bookmarks/bookmark_model_bridge_observer.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 123 | #import "ios/chrome/browser/ui/browser_container_view.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 124 | #import "ios/chrome/browser/ui/browser_view_controller_dependency_factory.h" |
Cooper Knaak | 33f9f40 | 2017-08-09 18:04:38 | [diff] [blame] | 125 | #import "ios/chrome/browser/ui/bubble/bubble_view_controller_presenter.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 126 | #import "ios/chrome/browser/ui/chrome_web_view_factory.h" |
Mark Cogan | 5e3da15 | 2017-07-11 15:57:30 | [diff] [blame] | 127 | #import "ios/chrome/browser/ui/commands/application_commands.h" |
Mark Cogan | 6c58ea9 | 2017-07-06 13:08:24 | [diff] [blame] | 128 | #import "ios/chrome/browser/ui/commands/browser_commands.h" |
edchin | 9badb06 | 2017-08-16 18:47:54 | [diff] [blame] | 129 | #import "ios/chrome/browser/ui/commands/command_dispatcher.h" |
Mark Cogan | dfcdea7 | 2017-07-18 13:47:38 | [diff] [blame] | 130 | #import "ios/chrome/browser/ui/commands/open_new_tab_command.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 131 | #import "ios/chrome/browser/ui/commands/open_url_command.h" |
| 132 | #import "ios/chrome/browser/ui/commands/reading_list_add_command.h" |
edchin | 95c92707 | 2017-11-04 00:35:07 | [diff] [blame] | 133 | #import "ios/chrome/browser/ui/commands/show_signin_command.h" |
edchin | 7f210cd | 2017-09-28 08:03:53 | [diff] [blame] | 134 | #import "ios/chrome/browser/ui/commands/snackbar_commands.h" |
Jean-François Geyelin | 5d2e184c | 2017-07-28 19:48:00 | [diff] [blame] | 135 | #import "ios/chrome/browser/ui/commands/start_voice_search_command.h" |
Gauthier Ambard | f520c02 | 2017-08-29 07:42:23 | [diff] [blame] | 136 | #import "ios/chrome/browser/ui/content_suggestions/ntp_home_constant.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 137 | #import "ios/chrome/browser/ui/context_menu/context_menu_coordinator.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 138 | #import "ios/chrome/browser/ui/dialogs/dialog_presenter.h" |
| 139 | #import "ios/chrome/browser/ui/dialogs/java_script_dialog_presenter_impl.h" |
Eugene But | 87a0953 | 2017-12-08 20:02:05 | [diff] [blame] | 140 | #import "ios/chrome/browser/ui/download/legacy_download_manager_controller.h" |
Eugene But | 49a7c57 | 2017-12-11 20:54:15 | [diff] [blame] | 141 | #import "ios/chrome/browser/ui/download/pass_kit_coordinator.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 142 | #import "ios/chrome/browser/ui/elements/activity_overlay_coordinator.h" |
| 143 | #import "ios/chrome/browser/ui/external_file_controller.h" |
Louis Romero | d11747a | 2017-10-20 20:10:35 | [diff] [blame] | 144 | #import "ios/chrome/browser/ui/external_search/external_search_coordinator.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 145 | #import "ios/chrome/browser/ui/find_bar/find_bar_controller_ios.h" |
| 146 | #import "ios/chrome/browser/ui/first_run/welcome_to_chrome_view_controller.h" |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 147 | #import "ios/chrome/browser/ui/fullscreen/fullscreen_controller.h" |
| 148 | #import "ios/chrome/browser/ui/fullscreen/fullscreen_controller_factory.h" |
Kurt Horimoto | 80384062 | 2017-10-28 01:20:37 | [diff] [blame] | 149 | #import "ios/chrome/browser/ui/fullscreen/fullscreen_features.h" |
Kurt Horimoto | 06b9425 | 2017-12-08 19:45:59 | [diff] [blame] | 150 | #import "ios/chrome/browser/ui/fullscreen/fullscreen_scroll_end_animator.h" |
| 151 | #import "ios/chrome/browser/ui/fullscreen/fullscreen_ui_element.h" |
| 152 | #import "ios/chrome/browser/ui/fullscreen/fullscreen_ui_updater.h" |
Kurt Horimoto | 62e97c7 | 2017-11-03 19:51:47 | [diff] [blame] | 153 | #import "ios/chrome/browser/ui/fullscreen/legacy_fullscreen_controller.h" |
sczs | dd860eba | 2017-08-10 01:55:38 | [diff] [blame] | 154 | #import "ios/chrome/browser/ui/history_popup/requirements/tab_history_presentation.h" |
sczs | 0a726d2 | 2017-08-21 22:40:13 | [diff] [blame] | 155 | #import "ios/chrome/browser/ui/history_popup/tab_history_legacy_coordinator.h" |
Gauthier Ambard | 92969941 | 2018-01-02 10:05:41 | [diff] [blame] | 156 | #import "ios/chrome/browser/ui/image_util/image_saver.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 157 | #import "ios/chrome/browser/ui/key_commands_provider.h" |
Kurt Horimoto | 1945ef4 | 2017-10-26 03:57:26 | [diff] [blame] | 158 | #import "ios/chrome/browser/ui/location_bar_notification_names.h" |
Rohit Rao | 9a8ad77 | 2017-10-30 22:35:59 | [diff] [blame] | 159 | #import "ios/chrome/browser/ui/main/main_feature_flags.h" |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 160 | #import "ios/chrome/browser/ui/main_content/main_content_ui.h" |
| 161 | #import "ios/chrome/browser/ui/main_content/main_content_ui_broadcasting_util.h" |
| 162 | #import "ios/chrome/browser/ui/main_content/main_content_ui_state.h" |
| 163 | #import "ios/chrome/browser/ui/main_content/web_scroll_view_main_content_ui_forwarder.h" |
Gauthier Ambard | 5bb5f7a | 2017-09-06 12:58:10 | [diff] [blame] | 164 | #import "ios/chrome/browser/ui/ntp/modal_ntp.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 165 | #import "ios/chrome/browser/ui/ntp/new_tab_page_controller.h" |
Gauthier Ambard | d4287fc | 2017-08-29 09:14:42 | [diff] [blame] | 166 | #import "ios/chrome/browser/ui/ntp/recent_tabs/recent_tabs_handset_coordinator.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 167 | #import "ios/chrome/browser/ui/overscroll_actions/overscroll_actions_controller.h" |
Gregory Chatzinoff | df93d69 | 2017-09-09 01:32:27 | [diff] [blame] | 168 | #import "ios/chrome/browser/ui/page_info/page_info_legacy_coordinator.h" |
| 169 | #import "ios/chrome/browser/ui/page_info/requirements/page_info_presentation.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 170 | #import "ios/chrome/browser/ui/page_not_available_controller.h" |
mahmadi | 1acec704 | 2017-04-24 08:29:37 | [diff] [blame] | 171 | #import "ios/chrome/browser/ui/payments/payment_request_manager.h" |
Mark Cogan | ca30df6 | 2017-11-20 14:29:11 | [diff] [blame] | 172 | #import "ios/chrome/browser/ui/presenters/vertical_animation_container.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 173 | #import "ios/chrome/browser/ui/print/print_controller.h" |
Rohit Rao | cda0a99 | 2017-08-16 15:37:11 | [diff] [blame] | 174 | #import "ios/chrome/browser/ui/qr_scanner/qr_scanner_legacy_coordinator.h" |
| 175 | #import "ios/chrome/browser/ui/qr_scanner/requirements/qr_scanner_presenting.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 176 | #import "ios/chrome/browser/ui/reading_list/offline_page_native_content.h" |
gambard | 6299cc1d | 2017-02-21 13:06:03 | [diff] [blame] | 177 | #import "ios/chrome/browser/ui/reading_list/reading_list_coordinator.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 178 | #import "ios/chrome/browser/ui/reading_list/reading_list_menu_notifier.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 179 | #include "ios/chrome/browser/ui/rtl_geometry.h" |
sczs | 6ae47ad | 2017-09-06 17:26:53 | [diff] [blame] | 180 | #import "ios/chrome/browser/ui/sad_tab/sad_tab_legacy_coordinator.h" |
sczs | 4044397 | 2017-09-13 19:02:39 | [diff] [blame] | 181 | #import "ios/chrome/browser/ui/settings/sync_utils/sync_util.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 182 | #import "ios/chrome/browser/ui/side_swipe/side_swipe_controller.h" |
edchin | 9e7a111 | 2017-11-07 18:28:03 | [diff] [blame] | 183 | #import "ios/chrome/browser/ui/signin_interaction/public/signin_presenter.h" |
edchin | 7f210cd | 2017-09-28 08:03:53 | [diff] [blame] | 184 | #import "ios/chrome/browser/ui/snackbar/snackbar_coordinator.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 185 | #import "ios/chrome/browser/ui/stack_view/card_view.h" |
| 186 | #import "ios/chrome/browser/ui/stack_view/page_animation_util.h" |
| 187 | #import "ios/chrome/browser/ui/static_content/static_html_native_content.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 188 | #import "ios/chrome/browser/ui/tab_switcher/tab_switcher_controller.h" |
edchin | f5150c68 | 2017-09-18 02:50:03 | [diff] [blame] | 189 | #import "ios/chrome/browser/ui/tabs/requirements/tab_strip_constants.h" |
| 190 | #import "ios/chrome/browser/ui/tabs/requirements/tab_strip_presentation.h" |
| 191 | #import "ios/chrome/browser/ui/tabs/tab_strip_legacy_coordinator.h" |
Gauthier Ambard | 76d826ac | 2017-11-16 16:27:34 | [diff] [blame] | 192 | #include "ios/chrome/browser/ui/toolbar/legacy_toolbar_coordinator.h" |
Kurt Horimoto | ea429dd | 2017-11-28 02:24:30 | [diff] [blame] | 193 | #import "ios/chrome/browser/ui/toolbar/legacy_toolbar_ui_updater.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 194 | #include "ios/chrome/browser/ui/toolbar/toolbar_model_delegate_ios.h" |
| 195 | #include "ios/chrome/browser/ui/toolbar/toolbar_model_ios.h" |
Gauthier Ambard | 29939db1 | 2017-10-30 16:47:31 | [diff] [blame] | 196 | #import "ios/chrome/browser/ui/toolbar/toolbar_snapshot_providing.h" |
Kurt Horimoto | ea429dd | 2017-11-28 02:24:30 | [diff] [blame] | 197 | #import "ios/chrome/browser/ui/toolbar/toolbar_ui.h" |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 198 | #import "ios/chrome/browser/ui/toolbar/toolbar_ui_broadcasting_util.h" |
sczs | c2b4f15 | 2017-10-11 01:44:24 | [diff] [blame] | 199 | #import "ios/chrome/browser/ui/toolbar/web_toolbar_controller.h" |
Peter Laurens | e0b80f1 | 2017-11-21 07:52:40 | [diff] [blame] | 200 | #import "ios/chrome/browser/ui/tools_menu/public/tools_menu_configuration_provider.h" |
| 201 | #import "ios/chrome/browser/ui/tools_menu/public/tools_menu_presentation_provider.h" |
sczs | bbad163 | 2017-07-29 03:48:00 | [diff] [blame] | 202 | #import "ios/chrome/browser/ui/tools_menu/tools_menu_configuration.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 203 | #import "ios/chrome/browser/ui/tools_menu/tools_menu_view_item.h" |
Mark Cogan | ca30df6 | 2017-11-20 14:29:11 | [diff] [blame] | 204 | #import "ios/chrome/browser/ui/translate/language_selection_coordinator.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 205 | #include "ios/chrome/browser/ui/ui_util.h" |
| 206 | #import "ios/chrome/browser/ui/uikit_ui_util.h" |
Gauthier Ambard | 087e357 | 2017-12-20 12:54:47 | [diff] [blame] | 207 | #import "ios/chrome/browser/ui/util/named_guide.h" |
gambard | 6a13836 | 2017-02-06 17:19:28 | [diff] [blame] | 208 | #import "ios/chrome/browser/ui/util/pasteboard_util.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 209 | #import "ios/chrome/browser/ui/voice/text_to_speech_player.h" |
| 210 | #include "ios/chrome/browser/upgrade/upgrade_center.h" |
eugenebut | 275f589 | 2017-03-09 22:20:51 | [diff] [blame] | 211 | #import "ios/chrome/browser/web/blocked_popup_tab_helper.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 212 | #import "ios/chrome/browser/web/error_page_content.h" |
Danyao Wang | 85389a8 | 2017-10-25 18:56:27 | [diff] [blame] | 213 | #import "ios/chrome/browser/web/load_timing_tab_helper.h" |
Sylvain Defresne | 44835133 | 2017-12-27 10:38:36 | [diff] [blame] | 214 | #import "ios/chrome/browser/web/page_placeholder_tab_helper.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 215 | #import "ios/chrome/browser/web/passkit_dialog_provider.h" |
Sylvain Defresne | cacc3a5 | 2017-09-12 13:51:04 | [diff] [blame] | 216 | #include "ios/chrome/browser/web/print_tab_helper.h" |
eugenebut | cae3d9e6 | 2017-01-27 20:01:05 | [diff] [blame] | 217 | #import "ios/chrome/browser/web/repost_form_tab_helper.h" |
Eugene But | 35ded55 | 2017-09-13 23:31:59 | [diff] [blame] | 218 | #import "ios/chrome/browser/web/repost_form_tab_helper_delegate.h" |
sczs | 6ae47ad | 2017-09-06 17:26:53 | [diff] [blame] | 219 | #import "ios/chrome/browser/web/sad_tab_tab_helper.h" |
Sylvain Defresne | cacc3a5 | 2017-09-12 13:51:04 | [diff] [blame] | 220 | #include "ios/chrome/browser/web/web_state_printer.h" |
sdefresne | 62a00bb | 2017-04-10 15:36:05 | [diff] [blame] | 221 | #import "ios/chrome/browser/web_state_list/web_state_list.h" |
| 222 | #import "ios/chrome/browser/web_state_list/web_state_opener.h" |
Gregory Chatzinoff | 5f9f7f0 | 2017-09-19 02:04:57 | [diff] [blame] | 223 | #import "ios/chrome/browser/webui/net_export_tab_helper.h" |
| 224 | #import "ios/chrome/browser/webui/net_export_tab_helper_delegate.h" |
| 225 | #import "ios/chrome/browser/webui/show_mail_composer_context.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 226 | #include "ios/chrome/grit/ios_chromium_strings.h" |
| 227 | #include "ios/chrome/grit/ios_strings.h" |
| 228 | #import "ios/net/request_tracker.h" |
| 229 | #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 230 | #include "ios/public/provider/chrome/browser/ui/app_rating_prompt.h" |
| 231 | #include "ios/public/provider/chrome/browser/ui/default_ios_web_view_factory.h" |
| 232 | #import "ios/public/provider/chrome/browser/voice/voice_search_bar.h" |
| 233 | #import "ios/public/provider/chrome/browser/voice/voice_search_bar_owner.h" |
| 234 | #include "ios/public/provider/chrome/browser/voice/voice_search_controller.h" |
| 235 | #include "ios/public/provider/chrome/browser/voice/voice_search_controller_delegate.h" |
| 236 | #include "ios/public/provider/chrome/browser/voice/voice_search_provider.h" |
edchin | eeb4d42 | 2017-10-02 17:39:36 | [diff] [blame] | 237 | #import "ios/third_party/material_components_ios/src/components/Snackbar/src/MaterialSnackbar.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 238 | #include "ios/web/public/navigation_item.h" |
| 239 | #import "ios/web/public/navigation_manager.h" |
| 240 | #include "ios/web/public/referrer_util.h" |
| 241 | #include "ios/web/public/ssl_status.h" |
| 242 | #include "ios/web/public/url_scheme_util.h" |
liaoyuke | ea9f3ee6 | 2017-03-07 22:05:39 | [diff] [blame] | 243 | #include "ios/web/public/user_agent.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 244 | #include "ios/web/public/web_client.h" |
| 245 | #import "ios/web/public/web_state/context_menu_params.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 246 | #import "ios/web/public/web_state/ui/crw_native_content_provider.h" |
eugenebut | 4648799 | 2017-03-16 17:21:29 | [diff] [blame] | 247 | #import "ios/web/public/web_state/ui/crw_web_view_proxy.h" |
Sylvain Defresne | e7f2c8a | 2017-10-17 02:39:19 | [diff] [blame] | 248 | #import "ios/web/public/web_state/web_state.h" |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 249 | #import "ios/web/public/web_state/web_state_delegate_bridge.h" |
| 250 | #include "ios/web/public/web_thread.h" |
| 251 | #import "ios/web/web_state/ui/crw_web_controller.h" |
| 252 | #import "net/base/mac/url_conversions.h" |
| 253 | #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 254 | #include "net/ssl/ssl_info.h" |
| 255 | #include "net/url_request/url_request_context_getter.h" |
| 256 | #include "third_party/google_toolbox_for_mac/src/iPhone/GTMUIImage+Resize.h" |
| 257 | #include "ui/base/l10n/l10n_util.h" |
| 258 | #include "ui/base/l10n/l10n_util_mac.h" |
| 259 | #include "ui/base/page_transition_types.h" |
| 260 | #include "url/gurl.h" |
| 261 | |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 262 | #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 263 | #error "This file requires ARC support." |
| 264 | #endif |
| 265 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 266 | using base::UserMetricsAction; |
| 267 | using bookmarks::BookmarkNode; |
| 268 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 269 | class InfoBarContainerDelegateIOS; |
| 270 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 271 | namespace { |
| 272 | |
| 273 | typedef NS_ENUM(NSInteger, ContextMenuHistogram) { |
| 274 | // Note: these values must match the ContextMenuOption enum in histograms.xml. |
| 275 | ACTION_OPEN_IN_NEW_TAB = 0, |
| 276 | ACTION_OPEN_IN_INCOGNITO_TAB = 1, |
| 277 | ACTION_COPY_LINK_ADDRESS = 2, |
| 278 | ACTION_SAVE_IMAGE = 6, |
| 279 | ACTION_OPEN_IMAGE = 7, |
| 280 | ACTION_OPEN_IMAGE_IN_NEW_TAB = 8, |
| 281 | ACTION_SEARCH_BY_IMAGE = 11, |
| 282 | ACTION_OPEN_JAVASCRIPT = 21, |
| 283 | ACTION_READ_LATER = 22, |
| 284 | NUM_ACTIONS = 23, |
| 285 | }; |
| 286 | |
Wei-Yin Chen (陳威尹) | 223326c | 2017-07-21 02:08:28 | [diff] [blame] | 287 | void Record(ContextMenuHistogram action, bool is_image, bool is_link) { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 288 | if (is_image) { |
| 289 | if (is_link) { |
| 290 | UMA_HISTOGRAM_ENUMERATION("ContextMenu.SelectedOption.ImageLink", action, |
| 291 | NUM_ACTIONS); |
| 292 | } else { |
| 293 | UMA_HISTOGRAM_ENUMERATION("ContextMenu.SelectedOption.Image", action, |
| 294 | NUM_ACTIONS); |
| 295 | } |
| 296 | } else { |
| 297 | UMA_HISTOGRAM_ENUMERATION("ContextMenu.SelectedOption.Link", action, |
| 298 | NUM_ACTIONS); |
| 299 | } |
| 300 | } |
| 301 | |
edchin | f5150c68 | 2017-09-18 02:50:03 | [diff] [blame] | 302 | // Returns the status bar background color. |
| 303 | UIColor* StatusBarBackgroundColor() { |
| 304 | return [UIColor colorWithRed:0.149 green:0.149 blue:0.164 alpha:1]; |
| 305 | } |
| 306 | |
| 307 | // Duration of the toolbar animation. |
Kurt Horimoto | 62e97c7 | 2017-11-03 19:51:47 | [diff] [blame] | 308 | const NSTimeInterval kLegacyFullscreenControllerToolbarAnimationDuration = 0.3; |
edchin | f5150c68 | 2017-09-18 02:50:03 | [diff] [blame] | 309 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 310 | const CGFloat kVoiceSearchBarHeight = 59.0; |
| 311 | |
| 312 | // Dimensions to use when downsizing an image for search-by-image. |
| 313 | const CGFloat kSearchByImageMaxImageArea = 90000.0; |
| 314 | const CGFloat kSearchByImageMaxImageWidth = 600.0; |
| 315 | const CGFloat kSearchByImageMaxImageHeight = 400.0; |
| 316 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 317 | enum HeaderBehaviour { |
| 318 | // The header moves completely out of the screen. |
| 319 | Hideable = 0, |
| 320 | // This header stays on screen and doesn't overlap with the content. |
| 321 | Visible, |
| 322 | // This header stay on screen and covers part of the content. |
| 323 | Overlap |
| 324 | }; |
| 325 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 326 | const CGFloat kIPadFindBarOverlap = 11; |
| 327 | |
| 328 | bool IsURLAllowedInIncognito(const GURL& url) { |
dbeam | 25b548f | 2017-05-05 18:05:24 | [diff] [blame] | 329 | // Most URLs are allowed in incognito; the following is an exception. |
| 330 | return !(url.SchemeIs(kChromeUIScheme) && url.host() == kChromeUIHistoryHost); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 331 | } |
| 332 | |
edchin | eeb4d42 | 2017-10-02 17:39:36 | [diff] [blame] | 333 | // Snackbar category for browser view controller. |
| 334 | NSString* const kBrowserViewControllerSnackbarCategory = |
| 335 | @"BrowserViewControllerSnackbarCategory"; |
| 336 | |
rohitrao | 005a643 | 2017-03-16 20:52:42 | [diff] [blame] | 337 | } // namespace |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 338 | |
stkhapugin | 952ecef | 2017-04-11 12:11:45 | [diff] [blame] | 339 | #pragma mark - HeaderDefinition helper |
| 340 | |
| 341 | @interface HeaderDefinition : NSObject |
| 342 | |
| 343 | // The header view. |
| 344 | @property(nonatomic, strong) UIView* view; |
| 345 | // How to place the view, and its behaviour when the headers move. |
| 346 | @property(nonatomic, assign) HeaderBehaviour behaviour; |
| 347 | // Reduces the height of a header to adjust for shadows. |
| 348 | @property(nonatomic, assign) CGFloat heightAdjustement; |
| 349 | // Nudges that particular header up by this number of points. |
| 350 | @property(nonatomic, assign) CGFloat inset; |
| 351 | |
| 352 | - (instancetype)initWithView:(UIView*)view |
| 353 | headerBehaviour:(HeaderBehaviour)behaviour |
| 354 | heightAdjustment:(CGFloat)heightAdjustment |
| 355 | inset:(CGFloat)inset; |
| 356 | |
| 357 | + (instancetype)definitionWithView:(UIView*)view |
| 358 | headerBehaviour:(HeaderBehaviour)behaviour |
| 359 | heightAdjustment:(CGFloat)heightAdjustment |
| 360 | inset:(CGFloat)inset; |
| 361 | |
| 362 | @end |
| 363 | |
| 364 | @implementation HeaderDefinition |
| 365 | @synthesize view = _view; |
| 366 | @synthesize behaviour = _behaviour; |
| 367 | @synthesize heightAdjustement = _heightAdjustement; |
| 368 | @synthesize inset = _inset; |
| 369 | |
| 370 | + (instancetype)definitionWithView:(UIView*)view |
| 371 | headerBehaviour:(HeaderBehaviour)behaviour |
| 372 | heightAdjustment:(CGFloat)heightAdjustment |
| 373 | inset:(CGFloat)inset { |
| 374 | return [[self alloc] initWithView:view |
| 375 | headerBehaviour:behaviour |
| 376 | heightAdjustment:heightAdjustment |
| 377 | inset:inset]; |
| 378 | } |
| 379 | |
| 380 | - (instancetype)initWithView:(UIView*)view |
| 381 | headerBehaviour:(HeaderBehaviour)behaviour |
| 382 | heightAdjustment:(CGFloat)heightAdjustment |
| 383 | inset:(CGFloat)inset { |
| 384 | self = [super init]; |
| 385 | if (self) { |
| 386 | _view = view; |
| 387 | _behaviour = behaviour; |
| 388 | _heightAdjustement = heightAdjustment; |
| 389 | _inset = inset; |
| 390 | } |
| 391 | return self; |
| 392 | } |
| 393 | |
| 394 | @end |
| 395 | |
| 396 | #pragma mark - BVC |
| 397 | |
Rohit Rao | 01e0e00 | 2017-08-14 20:49:43 | [diff] [blame] | 398 | @interface BrowserViewController ()<ActivityServicePresentation, |
Rohit Rao | 01e0e00 | 2017-08-14 20:49:43 | [diff] [blame] | 399 | AppRatingPromptDelegate, |
Gauthier Ambard | 65e949b09 | 2017-11-29 08:46:20 | [diff] [blame] | 400 | BookmarkModelBridgeObserver, |
Mike Dougherty | 4620cf8e | 2017-10-31 23:37:09 | [diff] [blame] | 401 | CaptivePortalDetectorTabHelperDelegate, |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 402 | CRWNativeContentProvider, |
| 403 | CRWWebStateDelegate, |
| 404 | DialogPresenterDelegate, |
Kurt Horimoto | 06b9425 | 2017-12-08 19:45:59 | [diff] [blame] | 405 | FullscreenUIElement, |
Kurt Horimoto | 62e97c7 | 2017-11-03 19:51:47 | [diff] [blame] | 406 | LegacyFullscreenControllerDelegate, |
Mark Cogan | 80aa28d | 2017-11-30 13:11:34 | [diff] [blame] | 407 | InfobarContainerStateDelegate, |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 408 | KeyCommandsPlumbing, |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 409 | MainContentUI, |
edchin | cd32fdf | 2017-10-25 12:45:45 | [diff] [blame] | 410 | ManageAccountsDelegate, |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 411 | MFMailComposeViewControllerDelegate, |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 412 | NetExportTabHelperDelegate, |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 413 | NewTabPageControllerObserver, |
| 414 | OverscrollActionsControllerDelegate, |
Gregory Chatzinoff | df93d69 | 2017-09-09 01:32:27 | [diff] [blame] | 415 | PageInfoPresentation, |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 416 | PassKitDialogProvider, |
Tomasz Garbus | b844e99 | 2017-09-29 12:44:55 | [diff] [blame] | 417 | PasswordControllerDelegate, |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 418 | PreloadControllerDelegate, |
Rohit Rao | cda0a99 | 2017-08-16 15:37:11 | [diff] [blame] | 419 | QRScannerPresenting, |
Eugene But | 35ded55 | 2017-09-13 23:31:59 | [diff] [blame] | 420 | RepostFormTabHelperDelegate, |
Gauthier Ambard | 29939db1 | 2017-10-30 16:47:31 | [diff] [blame] | 421 | SideSwipeControllerDelegate, |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 422 | SigninPresenter, |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 423 | SKStoreProductViewControllerDelegate, |
| 424 | SnapshotOverlayProvider, |
| 425 | StoreKitLauncher, |
| 426 | TabDialogDelegate, |
olivierrobin | 9ce77b8 | 2017-01-12 17:29:19 | [diff] [blame] | 427 | TabHeadersDelegate, |
sczs | dd860eba | 2017-08-10 01:55:38 | [diff] [blame] | 428 | TabHistoryPresentation, |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 429 | TabModelObserver, |
| 430 | TabSnapshottingDelegate, |
edchin | f5150c68 | 2017-09-18 02:50:03 | [diff] [blame] | 431 | TabStripPresentation, |
Peter Laurens | e0b80f1 | 2017-11-21 07:52:40 | [diff] [blame] | 432 | ToolsMenuConfigurationProvider, |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 433 | UIGestureRecognizerDelegate, |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 434 | UpgradeCenterClient, |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 435 | VoiceSearchBarDelegate, |
Sylvain Defresne | cacc3a5 | 2017-09-12 13:51:04 | [diff] [blame] | 436 | VoiceSearchBarOwner, |
| 437 | WebStatePrinter> { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 438 | // The dependency factory passed on initialization. Used to vend objects used |
| 439 | // by the BVC. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 440 | BrowserViewControllerDependencyFactory* _dependencyFactory; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 441 | |
| 442 | // The browser's tab model. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 443 | TabModel* _model; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 444 | |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 445 | // Facade objects used by |_toolbarCoordinator|. |
| 446 | // Must outlive |_toolbarCoordinator|. |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 447 | std::unique_ptr<ToolbarModelDelegateIOS> _toolbarModelDelegate; |
| 448 | std::unique_ptr<ToolbarModelIOS> _toolbarModelIOS; |
| 449 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 450 | // Controller for edge swipe gestures for page and tab navigation. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 451 | SideSwipeController* _sideSwipeController; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 452 | |
| 453 | // Handles displaying the context menu for all form factors. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 454 | ContextMenuCoordinator* _contextMenuCoordinator; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 455 | |
| 456 | // Backing object for property of the same name. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 457 | DialogPresenter* _dialogPresenter; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 458 | |
| 459 | // Handles presentation of JavaScript dialogs. |
| 460 | std::unique_ptr<JavaScriptDialogPresenterImpl> _javaScriptDialogPresenter; |
| 461 | |
justincohen | 75011c3 | 2017-04-28 16:31:39 | [diff] [blame] | 462 | // Handles command dispatching. |
| 463 | CommandDispatcher* _dispatcher; |
| 464 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 465 | // Keyboard commands provider. It offloads most of the keyboard commands |
| 466 | // management off of the BVC. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 467 | KeyCommandsProvider* _keyCommandsProvider; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 468 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 469 | // Used to inject Javascript implementing the PaymentRequest API and to |
| 470 | // display the UI. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 471 | PaymentRequestManager* _paymentRequestManager; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 472 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 473 | // Used to display the Voice Search UI. Nil if not visible. |
| 474 | scoped_refptr<VoiceSearchController> _voiceSearchController; |
| 475 | |
gambard | 6299cc1d | 2017-02-21 13:06:03 | [diff] [blame] | 476 | // Used to display the Reading List. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 477 | ReadingListCoordinator* _readingListCoordinator; |
gambard | 6299cc1d | 2017-02-21 13:06:03 | [diff] [blame] | 478 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 479 | // Used to display the Find In Page UI. Nil if not visible. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 480 | FindBarControllerIOS* _findBarController; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 481 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 482 | // Used to display the Print UI. Nil if not visible. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 483 | PrintController* _printController; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 484 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 485 | // Adapter to let BVC be the delegate for WebState. |
| 486 | std::unique_ptr<web::WebStateDelegateBridge> _webStateDelegate; |
| 487 | |
| 488 | // YES if new tab is animating in. |
| 489 | BOOL _inNewTabAnimation; |
| 490 | |
| 491 | // YES if Voice Search should be started when the new tab animation is |
| 492 | // finished. |
| 493 | BOOL _startVoiceSearchAfterNewTabAnimation; |
| 494 | |
| 495 | // YES if the user interacts with the location bar. |
| 496 | BOOL _locationBarHasFocus; |
| 497 | // YES if a load was cancelled due to typing in the location bar. |
| 498 | BOOL _locationBarEditCancelledLoad; |
| 499 | // YES if waiting for a foreground tab due to expectNewForegroundTab. |
| 500 | BOOL _expectingForegroundTab; |
| 501 | |
Sylvain Defresne | 41170aa | 2017-06-15 10:25:20 | [diff] [blame] | 502 | // Whether or not -shutdown has been called. |
| 503 | BOOL _isShutdown; |
| 504 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 505 | // The ChromeBrowserState associated with this BVC. |
| 506 | ios::ChromeBrowserState* _browserState; // weak |
| 507 | |
| 508 | // Whether or not Incognito* is enabled. |
| 509 | BOOL _isOffTheRecord; |
| 510 | |
| 511 | // The last point within |_contentArea| that's received a touch. |
| 512 | CGPoint _lastTapPoint; |
| 513 | |
| 514 | // The time at which |_lastTapPoint| was most recently set. |
| 515 | CFTimeInterval _lastTapTime; |
| 516 | |
| 517 | // A single infobar container handles all infobars in all tabs. It keeps |
| 518 | // track of infobars for current tab (accessed via infobar helper of |
| 519 | // the current tab). |
| 520 | std::unique_ptr<InfoBarContainerIOS> _infoBarContainer; |
| 521 | |
| 522 | // Bridge class to deliver container change notifications to BVC. |
| 523 | std::unique_ptr<InfoBarContainerDelegateIOS> _infoBarContainerDelegate; |
| 524 | |
| 525 | // Voice search bar at the bottom of the view overlayed on |_contentArea| |
kkhorimoto | c2cdf6f4 | 2017-01-24 21:37:37 | [diff] [blame] | 526 | // when displaying voice search results. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 527 | UIView<VoiceSearchBar>* _voiceSearchBar; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 528 | |
| 529 | // The image fetcher used to save images and perform image-based searches. |
gambard | bdc07cc | 2017-02-03 16:43:11 | [diff] [blame] | 530 | std::unique_ptr<image_fetcher::IOSImageDataFetcherWrapper> _imageFetcher; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 531 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 532 | // Dominant color cache. Key: (NSString*)url, val: (UIColor*)dominantColor. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 533 | NSMutableDictionary* _dominantColorCache; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 534 | |
| 535 | // Bridge to register for bookmark changes. |
Gauthier Ambard | 65e949b09 | 2017-11-29 08:46:20 | [diff] [blame] | 536 | std::unique_ptr<bookmarks::BookmarkModelBridge> _bookmarkModelBridge; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 537 | |
| 538 | // Cached pointer to the bookmarks model. |
| 539 | bookmarks::BookmarkModel* _bookmarkModel; // weak |
| 540 | |
| 541 | // The controller that shows the bookmarking UI after the user taps the star |
| 542 | // button. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 543 | BookmarkInteractionController* _bookmarkInteractionController; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 544 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 545 | // The currently displayed "Rate This App" dialog, if one exists. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 546 | id<AppRatingPrompt> _rateThisAppDialog; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 547 | |
Eugene But | 56efc32 | 2017-08-11 14:03:44 | [diff] [blame] | 548 | // Native controller vended to tab before Tab is added to the tab model. |
Danyao Wang | ac242c7 | 2017-08-29 18:55:28 | [diff] [blame] | 549 | __weak id _temporaryNativeController; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 550 | |
| 551 | // Notifies the toolbar menu of reading list changes. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 552 | ReadingListMenuNotifier* _readingListMenuNotifier; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 553 | |
Jean-François Geyelin | 3d47c21 | 2017-08-03 09:24:09 | [diff] [blame] | 554 | // The view used by the voice search presentation animation. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 555 | __weak UIView* _voiceSearchButton; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 556 | |
Rohit Rao | 01e0e00 | 2017-08-14 20:49:43 | [diff] [blame] | 557 | // Coordinator for the share menu (Activity Services). |
| 558 | ActivityServiceLegacyCoordinator* _activityServiceCoordinator; |
| 559 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 560 | // Coordinator for displaying alerts. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 561 | AlertCoordinator* _alertCoordinator; |
sczs | dd860eba | 2017-08-10 01:55:38 | [diff] [blame] | 562 | |
Rohit Rao | cda0a99 | 2017-08-16 15:37:11 | [diff] [blame] | 563 | // Coordinator for the QR scanner. |
| 564 | QRScannerLegacyCoordinator* _qrScannerCoordinator; |
| 565 | |
sczs | dd860eba | 2017-08-10 01:55:38 | [diff] [blame] | 566 | // Coordinator for Tab History Popup. |
sczs | 0a726d2 | 2017-08-21 22:40:13 | [diff] [blame] | 567 | LegacyTabHistoryCoordinator* _tabHistoryCoordinator; |
sczs | 6ae47ad | 2017-09-06 17:26:53 | [diff] [blame] | 568 | |
| 569 | // Coordinator for displaying Sad Tab. |
| 570 | SadTabLegacyCoordinator* _sadTabCoordinator; |
Gregory Chatzinoff | df93d69 | 2017-09-09 01:32:27 | [diff] [blame] | 571 | |
| 572 | // Coordinator for Page Info UI. |
| 573 | PageInfoLegacyCoordinator* _pageInfoCoordinator; |
Eugene But | 35ded55 | 2017-09-13 23:31:59 | [diff] [blame] | 574 | |
| 575 | // Coordinator for displaying Repost Form dialog. |
| 576 | RepostFormCoordinator* _repostFormCoordinator; |
Justin Cohen | b3170c3 | 2017-09-19 01:55:22 | [diff] [blame] | 577 | |
edchin | 7f210cd | 2017-09-28 08:03:53 | [diff] [blame] | 578 | // Coordinator for displaying snackbars. |
| 579 | SnackbarCoordinator* _snackbarCoordinator; |
| 580 | |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 581 | // Coordinator for the toolbar. |
| 582 | LegacyToolbarCoordinator* _toolbarCoordinator; |
| 583 | |
Kurt Horimoto | ea429dd | 2017-11-28 02:24:30 | [diff] [blame] | 584 | // The toolbar UI updater for the toolbar managed by |_toolbarCoordinator|. |
| 585 | LegacyToolbarUIUpdater* _toolbarUIUpdater; |
| 586 | |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 587 | // The main content UI updater for the content displayed by this BVC. |
| 588 | MainContentUIStateUpdater* _mainContentUIUpdater; |
| 589 | |
| 590 | // The forwarder for web scroll view interation events. |
| 591 | WebScrollViewMainContentUIForwarder* _webMainContentUIForwarder; |
| 592 | |
Kurt Horimoto | 06b9425 | 2017-12-08 19:45:59 | [diff] [blame] | 593 | // The updater that adjusts the toolbar's layout for fullscreen events. |
| 594 | std::unique_ptr<FullscreenUIUpdater> _fullscreenUIUpdater; |
| 595 | |
Louis Romero | d11747a | 2017-10-20 20:10:35 | [diff] [blame] | 596 | // Coordinator for the External Search UI. |
| 597 | ExternalSearchCoordinator* _externalSearchCoordinator; |
| 598 | |
Mark Cogan | ca30df6 | 2017-11-20 14:29:11 | [diff] [blame] | 599 | // Coordinator for the language selection UI. |
| 600 | LanguageSelectionCoordinator* _languageSelectionCoordinator; |
| 601 | |
Eugene But | 49a7c57 | 2017-12-11 20:54:15 | [diff] [blame] | 602 | // Coordinator for the PassKit UI presentation. |
| 603 | PassKitCoordinator* _passKitCoordinator; |
| 604 | |
Justin Cohen | b3170c3 | 2017-09-19 01:55:22 | [diff] [blame] | 605 | // Fake status bar view used to blend the toolbar into the status bar. |
| 606 | UIView* _fakeStatusBarView; |
Sylvain Defresne | f5d2d95 | 2017-11-14 11:15:31 | [diff] [blame] | 607 | |
| 608 | // Stores whether the Tab currently inserted was a pre-rendered Tab. This |
| 609 | // is used to determine whether the pre-rendering animation should be played |
| 610 | // or not. |
| 611 | BOOL _insertedTabWasPrerenderedTab; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | // The browser's side swipe controller. Lazily instantiated on the first call. |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 615 | @property(nonatomic, strong, readonly) SideSwipeController* sideSwipeController; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 616 | // The dialog presenter for this BVC's tab model. |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 617 | @property(nonatomic, strong, readonly) DialogPresenter* dialogPresenter; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 618 | // The object that manages keyboard commands on behalf of the BVC. |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 619 | @property(nonatomic, strong, readonly) KeyCommandsProvider* keyCommandsProvider; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 620 | // Whether the current tab can enable the request desktop menu item. |
| 621 | @property(nonatomic, assign, readonly) BOOL canUseDesktopUserAgent; |
| 622 | // Whether the sharing menu should be enabled. |
| 623 | @property(nonatomic, assign, readonly) BOOL canShowShareMenu; |
| 624 | // Helper method to check web controller canShowFindBar method. |
| 625 | @property(nonatomic, assign, readonly) BOOL canShowFindBar; |
| 626 | // Whether the controller's view is currently available. |
| 627 | // YES from viewWillAppear to viewWillDisappear. |
| 628 | @property(nonatomic, assign, getter=isVisible) BOOL visible; |
| 629 | // Whether the controller's view is currently visible. |
| 630 | // YES from viewDidAppear to viewWillDisappear. |
| 631 | @property(nonatomic, assign) BOOL viewVisible; |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 632 | // Whether the controller should broadcast its UI. |
| 633 | @property(nonatomic, assign, getter=isBroadcasting) BOOL broadcasting; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 634 | // Whether the controller is currently dismissing a presented view controller. |
| 635 | @property(nonatomic, assign, getter=isDismissingModal) BOOL dismissingModal; |
| 636 | // Returns YES if the toolbar has not been scrolled out by fullscreen. |
| 637 | @property(nonatomic, assign, readonly, getter=isToolbarOnScreen) |
| 638 | BOOL toolbarOnScreen; |
| 639 | // Whether a new tab animation is occurring. |
kkhorimoto | a44349c1 | 2017-04-12 23:02:12 | [diff] [blame] | 640 | @property(nonatomic, assign, getter=isInNewTabAnimation) BOOL inNewTabAnimation; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 641 | // Whether BVC prefers to hide the status bar. This value is used to determine |
| 642 | // the response from the |prefersStatusBarHidden| method. |
| 643 | @property(nonatomic, assign) BOOL hideStatusBar; |
| 644 | // Whether the VoiceSearchBar should be displayed. |
| 645 | @property(nonatomic, readonly) BOOL shouldShowVoiceSearchBar; |
| 646 | // Coordinator for displaying a modal overlay with activity indicator to prevent |
| 647 | // the user from interacting with the browser view. |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 648 | @property(nonatomic, strong) |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 649 | ActivityOverlayCoordinator* activityOverlayCoordinator; |
peterlaurens | 90ac0d3 | 2017-06-08 21:13:39 | [diff] [blame] | 650 | // A block to be run when the |tabWasAdded:| method completes the animation |
| 651 | // for the presentation of a new tab. Can be used to record performance metrics. |
| 652 | @property(nonatomic, strong, nullable) |
| 653 | ProceduralBlock foregroundTabWasAddedCompletionBlock; |
Gauthier Ambard | d4287fc | 2017-08-29 09:14:42 | [diff] [blame] | 654 | // Coordinator for Recent Tabs. |
| 655 | @property(nonatomic, strong) |
| 656 | RecentTabsHandsetCoordinator* recentTabsCoordinator; |
edchin | f5150c68 | 2017-09-18 02:50:03 | [diff] [blame] | 657 | // Coordinator for tablet tab strip. |
| 658 | @property(nonatomic, strong) TabStripLegacyCoordinator* tabStripCoordinator; |
| 659 | // A weak reference to the view of the tab strip on tablet. |
| 660 | @property(nonatomic, weak) UIView* tabStripView; |
Gauthier Ambard | 92969941 | 2018-01-02 10:05:41 | [diff] [blame] | 661 | // Helper for saving images. |
| 662 | @property(nonatomic, strong) ImageSaver* imageSaver; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 663 | |
Gauthier Ambard | 92969941 | 2018-01-02 10:05:41 | [diff] [blame] | 664 | // The user agent type used to load the currently visible page. User agent |
| 665 | // type is NONE if there is no visible page or visible page is a native |
| 666 | // page. |
liaoyuke | ea9f3ee6 | 2017-03-07 22:05:39 | [diff] [blame] | 667 | @property(nonatomic, assign, readonly) web::UserAgentType userAgentType; |
| 668 | |
stkhapugin | 952ecef | 2017-04-11 12:11:45 | [diff] [blame] | 669 | // Returns the header views, all the chrome on top of the page, including the |
| 670 | // ones that cannot be scrolled off screen by full screen. |
| 671 | @property(nonatomic, strong, readonly) NSArray<HeaderDefinition*>* headerViews; |
| 672 | |
Cooper Knaak | d0a974cd | 2017-08-10 18:05:47 | [diff] [blame] | 673 | // Used to display the new tab tip in-product help promotion bubble. |nil| if |
| 674 | // the new tab tip bubble has not yet been presented. Once the bubble is |
| 675 | // dismissed, it remains allocated so that |userEngaged| remains accessible. |
Cooper Knaak | 33f9f40 | 2017-08-09 18:04:38 | [diff] [blame] | 676 | @property(nonatomic, strong) |
Cooper Knaak | d0a974cd | 2017-08-10 18:05:47 | [diff] [blame] | 677 | BubbleViewControllerPresenter* tabTipBubblePresenter; |
Helen Yang | 9175bd5 | 2017-08-12 00:28:40 | [diff] [blame] | 678 | // Used to display the new incognito tab tip in-product help promotion bubble. |
| 679 | @property(nonatomic, strong) |
| 680 | BubbleViewControllerPresenter* incognitoTabTipBubblePresenter; |
| 681 | |
Justin Cohen | 9fe9ef67 | 2017-12-01 20:37:43 | [diff] [blame] | 682 | // Vertical offset for fullscreen toolbar. |
| 683 | @property(nonatomic, strong) NSLayoutConstraint* toolbarOffsetConstraint; |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 684 | // Y-dimension offset for placement of the header. |
| 685 | @property(nonatomic, readonly) CGFloat headerOffset; |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 686 | // Height of the header view for the tab model's current tab. |
| 687 | @property(nonatomic, readonly) CGFloat headerHeight; |
| 688 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 689 | // The webState of the active tab. |
| 690 | @property(nonatomic, readonly) web::WebState* currentWebState; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 691 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 692 | // BVC initialization |
| 693 | // ------------------ |
| 694 | // If the BVC is initialized with a valid browser state & tab model immediately, |
| 695 | // the path is straightforward: functionality is enabled, and the UI is built |
| 696 | // when -viewDidLoad is called. |
| 697 | // If the BVC is initialized without a browser state or tab model, the tab model |
| 698 | // and browser state may or may not be provided before -viewDidLoad is called. |
| 699 | // In most cases, they will not, to improve startup performance. |
| 700 | // In order to handle this, initialization of various aspects of BVC have been |
| 701 | // broken out into the following functions, which have expectations (enforced |
| 702 | // with DCHECKs) regarding |_browserState|, |_model|, and [self isViewLoaded]. |
| 703 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 704 | // Updates non-view-related functionality with the given browser state and tab |
| 705 | // model. |
| 706 | // Does not matter whether or not the view has been loaded. |
| 707 | - (void)updateWithTabModel:(TabModel*)model |
| 708 | browserState:(ios::ChromeBrowserState*)browserState; |
| 709 | // On iOS7, iPad should match iOS6 status bar. Install a simple black bar under |
| 710 | // the status bar to mimic this layout. |
| 711 | - (void)installFakeStatusBar; |
| 712 | // Builds the UI parts of tab strip and the toolbar. Does not matter whether |
| 713 | // or not browser state and tab model are valid. |
| 714 | - (void)buildToolbarAndTabStrip; |
Jean-François Geyelin | ed4cde7 | 2017-10-11 11:34:50 | [diff] [blame] | 715 | // Sets up the constraints on the toolbar. |
| 716 | - (void)addConstraintsToToolbar; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 717 | // Updates view-related functionality with the given tab model and browser |
| 718 | // state. The view must have been loaded. Uses |_browserState| and |_model|. |
| 719 | - (void)addUIFunctionalityForModelAndBrowserState; |
Justin Cohen | 4eeada3 | 2017-11-13 18:21:28 | [diff] [blame] | 720 | // Sets the correct frame and hierarchy for subviews and helper views. Only |
| 721 | // insert views on |initialLayout|. |
| 722 | - (void)setUpViewLayout:(BOOL)initialLayout; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 723 | // Makes |tab| the currently visible tab, displaying its view. Calls |
| 724 | // -selectedTabChanged on the toolbar only if |newSelection| is YES. |
| 725 | - (void)displayTab:(Tab*)tab isNewSelection:(BOOL)newSelection; |
| 726 | // Initializes the bookmark interaction controller if not already initialized. |
| 727 | - (void)initializeBookmarkInteractionController; |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 728 | // Installs the BVC as overscroll actions controller of |nativeContent| if |
| 729 | // needed. Sets the style of the overscroll actions toolbar. |
| 730 | - (void)setOverScrollActionControllerToStaticNativeContent: |
| 731 | (StaticHtmlNativeContent*)nativeContent; |
| 732 | |
| 733 | // UI Configuration, update and Layout |
| 734 | // ----------------------------------- |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 735 | // Updates the toolbar display based on the current tab. |
| 736 | - (void)updateToolbar; |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 737 | // Starts or stops broadcasting the toolbar UI and main content UI depending on |
| 738 | // whether the BVC is visible and active. |
| 739 | - (void)updateBroadcastState; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 740 | // Updates |dialogPresenter|'s |active| property to account for the BVC's |
kkhorimoto | a44349c1 | 2017-04-12 23:02:12 | [diff] [blame] | 741 | // |active|, |visible|, and |inNewTabAnimation| properties. |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 742 | - (void)updateDialogPresenterActiveState; |
| 743 | // Dismisses popups and modal dialogs that are displayed above the BVC upon size |
| 744 | // changes (e.g. rotation, resizing,…) or when the accessibility escape gesture |
| 745 | // is performed. |
| 746 | // TODO(crbug.com/522721): Support size changes for all popups and modal |
| 747 | // dialogs. |
| 748 | - (void)dismissPopups; |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 749 | // Returns whether |tab| is scrolled to the top. |
| 750 | - (BOOL)isTabScrolledToTop:(Tab*)tab; |
| 751 | // Returns the footer view if one exists (e.g. the voice search bar). |
| 752 | - (UIView*)footerView; |
| 753 | // Returns the header height needed for |tab|. |
| 754 | - (CGFloat)headerHeightForTab:(Tab*)tab; |
| 755 | // Sets the frame for the headers. |
| 756 | - (void)setFramesForHeaders:(NSArray<HeaderDefinition*>*)headers |
| 757 | atOffset:(CGFloat)headerOffset; |
| 758 | // Adds a CardView on top of the contentArea either taking the size of the full |
| 759 | // screen or just the size of the space under the header. |
| 760 | // Returns the CardView that was added. |
| 761 | - (CardView*)addCardViewInFullscreen:(BOOL)fullScreen; |
Cooper Knaak | d0a974cd | 2017-08-10 18:05:47 | [diff] [blame] | 762 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 763 | // Showing and Dismissing child UI |
| 764 | // ------------------------------- |
| 765 | // Show the bookmarks page. |
| 766 | - (void)showAllBookmarks; |
| 767 | // Shows a panel within the New Tab Page. |
| 768 | - (void)showNTPPanel:(ntp_home::PanelIdentifier)panel; |
| 769 | // Dismisses the "rate this app" dialog. |
| 770 | - (void)dismissRateThisAppDialog; |
| 771 | |
| 772 | // Bubble Views |
| 773 | // ------------ |
Cooper Knaak | d0a974cd | 2017-08-10 18:05:47 | [diff] [blame] | 774 | // Returns a bubble associated with an in-product help promotion if |
| 775 | // it is valid to show the promotion and |nil| otherwise. |feature| is the |
| 776 | // base::Feature object associated with the given promotion. |direction| is the |
| 777 | // direction the bubble's arrow is pointing. |alignment| is the alignment of the |
Gregory Chatzinoff | 541b864 | 2017-10-25 00:25:21 | [diff] [blame] | 778 | // arrow on the button. |text| is the text displayed by the bubble. This method |
| 779 | // requires that |self.browserState| is not NULL. |
Cooper Knaak | d0a974cd | 2017-08-10 18:05:47 | [diff] [blame] | 780 | - (BubbleViewControllerPresenter*) |
| 781 | bubblePresenterForFeature:(const base::Feature&)feature |
| 782 | direction:(BubbleArrowDirection)direction |
| 783 | alignment:(BubbleAlignment)alignment |
| 784 | text:(NSString*)text; |
| 785 | |
Cooper Knaak | 120cee5e | 2017-08-10 20:57:00 | [diff] [blame] | 786 | // Waits to present a bubble associated with the new tab tip in-product help |
| 787 | // promotion until the feature engagement tracker database is fully initialized. |
| 788 | // Does not present the bubble if |tabTipBubblePresenter.userEngaged| is |YES| |
| 789 | // to prevent resetting |tabTipBubblePresenter| and affecting the value of |
Cooper Knaak | e963d670 | 2017-08-11 21:03:11 | [diff] [blame] | 790 | // |userEngaged|. Does not present the bubble if the feature engagement tracker |
Gregory Chatzinoff | 541b864 | 2017-10-25 00:25:21 | [diff] [blame] | 791 | // determines it is not valid to present it. This method requires that |
| 792 | // |self.browserState| is not NULL. |
Cooper Knaak | 120cee5e | 2017-08-10 20:57:00 | [diff] [blame] | 793 | - (void)presentNewTabTipBubbleOnInitialized; |
Cooper Knaak | e963d670 | 2017-08-11 21:03:11 | [diff] [blame] | 794 | // Optionally presents a bubble associated with the new tab tip in-product help |
| 795 | // promotion. If the feature engagement tracker determines it is valid to show |
| 796 | // the new tab tip, then it initializes |tabTipBubblePresenter| and presents |
| 797 | // the bubble. If it is not valid to show the new tab tip, |
Gregory Chatzinoff | 541b864 | 2017-10-25 00:25:21 | [diff] [blame] | 798 | // |tabTipBubblePresenter| is set to |nil| and no bubble is shown. This method |
| 799 | // requires that |self.browserState| is not NULL. |
Cooper Knaak | 120cee5e | 2017-08-10 20:57:00 | [diff] [blame] | 800 | - (void)presentNewTabTipBubble; |
Helen Yang | 9175bd5 | 2017-08-12 00:28:40 | [diff] [blame] | 801 | // Waits to present a bubble associated with the new incognito tab tip |
| 802 | // in-product help promotion until the feature engagement tracker database is |
Gregory Chatzinoff | 541b864 | 2017-10-25 00:25:21 | [diff] [blame] | 803 | // fully initialized. This method requires that |self.browserState| is |
| 804 | // not NULL. |
Helen Yang | 9175bd5 | 2017-08-12 00:28:40 | [diff] [blame] | 805 | - (void)presentNewIncognitoTabTipBubbleOnInitialized; |
| 806 | // Presents a bubble associated with the new incognito tab tip in-product help |
Gregory Chatzinoff | 541b864 | 2017-10-25 00:25:21 | [diff] [blame] | 807 | // promotion. This method requires that |self.browserState| is not NULL. |
Helen Yang | 9175bd5 | 2017-08-12 00:28:40 | [diff] [blame] | 808 | - (void)presentNewIncognitoTabTipBubble; |
Cooper Knaak | 120cee5e | 2017-08-10 20:57:00 | [diff] [blame] | 809 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 810 | // Find Bar UI |
| 811 | // ----------- |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 812 | // Update find bar with model data. If |shouldFocus| is set to YES, the text |
| 813 | // field will become first responder. |
| 814 | - (void)updateFindBar:(BOOL)initialUpdate shouldFocus:(BOOL)shouldFocus; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 815 | // Hide find bar. |
| 816 | - (void)hideFindBarWithAnimation:(BOOL)animate; |
| 817 | // Shows find bar. If |selectText| is YES, all text inside the Find Bar |
| 818 | // textfield will be selected. If |shouldFocus| is set to YES, the textfield is |
| 819 | // set to be first responder. |
| 820 | - (void)showFindBarWithAnimation:(BOOL)animate |
| 821 | selectText:(BOOL)selectText |
| 822 | shouldFocus:(BOOL)shouldFocus; |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 823 | // Redisplays the find bar if necessary furing a view controller size change, |
| 824 | // using the transition coordinator |coordinator|. |
| 825 | - (void)reshowFindBarIfNeededWithCoordinator: |
| 826 | (id<UIViewControllerTransitionCoordinator>)coordinator; |
Gregory Chatzinoff | 7d1144c0 | 2017-08-31 15:00:36 | [diff] [blame] | 827 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 828 | // Alerts |
| 829 | // ------ |
| 830 | // Shows a self-dismissing snackbar displaying |message|. |
| 831 | - (void)showSnackbar:(NSString*)message; |
| 832 | // Shows an alert dialog with |title| and |message|. |
| 833 | - (void)showErrorAlertWithStringTitle:(NSString*)title |
| 834 | message:(NSString*)message; |
| 835 | |
| 836 | // Tap Handling |
| 837 | // ------------ |
| 838 | // Record the last tap point based on the |originPoint| (if any) passed in |
| 839 | // |command|. |
| 840 | - (void)setLastTapPoint:(OpenNewTabCommand*)command; |
| 841 | // Returns the last stored |_lastTapPoint| if it's been set within the past |
| 842 | // second. |
| 843 | - (CGPoint)lastTapPoint; |
| 844 | // Store the tap CGPoint in |_lastTapPoint| and the current timestamp. |
| 845 | - (void)saveContentAreaTapLocation:(UIGestureRecognizer*)gestureRecognizer; |
| 846 | |
| 847 | // Tab creation and selection |
| 848 | // -------------------------- |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 849 | // Called when either a tab finishes loading or when a tab with finished content |
| 850 | // is added directly to the model via pre-rendering. The tab must be non-nil and |
| 851 | // must be a member of the tab model controlled by this BrowserViewController. |
| 852 | - (void)tabLoadComplete:(Tab*)tab withSuccess:(BOOL)success; |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 853 | // Adds a new tab with |url| and |postData| at the end of the model, and make it |
| 854 | // the selected tab and return it. |
| 855 | - (Tab*)addSelectedTabWithURL:(const GURL&)url |
| 856 | postData:(TemplateURLRef::PostContent*)postData |
| 857 | transition:(ui::PageTransition)transition; |
| 858 | // Internal method that all of the similar public and private methods call. |
| 859 | // Adds a new tab with |url| and |postData| (if not null) at |position| in the |
| 860 | // tab model (or at the end if |position is NSNotFound|, with |transition| as |
| 861 | // the page transition type. If |tabAddedCompletion| is nonnull, it's called |
| 862 | // synchronously after the tab is added. |
| 863 | - (Tab*)addSelectedTabWithURL:(const GURL&)url |
| 864 | postData:(TemplateURLRef::PostContent*)postData |
| 865 | atIndex:(NSUInteger)position |
| 866 | transition:(ui::PageTransition)transition |
| 867 | tabAddedCompletion:(ProceduralBlock)tabAddedCompletion; |
| 868 | // Whether the given tab's URL is an application specific URL. |
| 869 | - (BOOL)isTabNativePage:(Tab*)tab; |
| 870 | // Returns the view to use when animating a page in or out, positioning it to |
| 871 | // fill the content area but not actually adding it to the view hierarchy. |
| 872 | - (UIImageView*)pageOpenCloseAnimationView; |
| 873 | // Add all delegates to the provided |tab|. |
| 874 | - (void)installDelegatesForTab:(Tab*)tab; |
| 875 | // Remove delegates from the provided |tab|. |
| 876 | - (void)uninstallDelegatesForTab:(Tab*)tab; |
| 877 | // Called when a tab is selected in the model. Make any required view changes. |
| 878 | // The notification will not be sent when the tab is already the selected tab. |
| 879 | // |notifyToolbar| indicates whether the toolbar is notified that the tab has |
| 880 | // changed. |
| 881 | - (void)tabSelected:(Tab*)tab notifyToolbar:(BOOL)notifyToolbar; |
| 882 | // Returns the native controller being used by |tab|'s web controller. |
| 883 | - (id)nativeControllerForTab:(Tab*)tab; |
| 884 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 885 | // Voice Search |
| 886 | // ------------ |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 887 | // Lazily instantiates |_voiceSearchController|. |
| 888 | - (void)ensureVoiceSearchControllerCreated; |
| 889 | // Lazily instantiates |_voiceSearchBar| and adds it to the view. |
| 890 | - (void)ensureVoiceSearchBarCreated; |
| 891 | // Shows/hides the voice search bar. |
| 892 | - (void)updateVoiceSearchBarVisibilityAnimated:(BOOL)animated; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 893 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 894 | // Reading List |
| 895 | // ------------ |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 896 | // Adds the given url to the reading list. |
| 897 | - (void)addToReadingListURL:(const GURL&)URL title:(NSString*)title; |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 898 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 899 | @end |
| 900 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 901 | @implementation BrowserViewController |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 902 | // Public synthesized propeties. |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 903 | @synthesize contentArea = _contentArea; |
| 904 | @synthesize typingShield = _typingShield; |
| 905 | @synthesize active = _active; |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 906 | // Private synthesized properties |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 907 | @synthesize visible = _visible; |
| 908 | @synthesize viewVisible = _viewVisible; |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 909 | @synthesize broadcasting = _broadcasting; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 910 | @synthesize dismissingModal = _dismissingModal; |
| 911 | @synthesize hideStatusBar = _hideStatusBar; |
| 912 | @synthesize activityOverlayCoordinator = _activityOverlayCoordinator; |
peterlaurens | 90ac0d3 | 2017-06-08 21:13:39 | [diff] [blame] | 913 | @synthesize foregroundTabWasAddedCompletionBlock = |
| 914 | _foregroundTabWasAddedCompletionBlock; |
Gauthier Ambard | d4287fc | 2017-08-29 09:14:42 | [diff] [blame] | 915 | @synthesize recentTabsCoordinator = _recentTabsCoordinator; |
edchin | f5150c68 | 2017-09-18 02:50:03 | [diff] [blame] | 916 | @synthesize tabStripCoordinator = _tabStripCoordinator; |
| 917 | @synthesize tabStripView = _tabStripView; |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 918 | @synthesize tabTipBubblePresenter = _tabTipBubblePresenter; |
| 919 | @synthesize incognitoTabTipBubblePresenter = _incognitoTabTipBubblePresenter; |
Justin Cohen | 9fe9ef67 | 2017-12-01 20:37:43 | [diff] [blame] | 920 | @synthesize toolbarOffsetConstraint = _toolbarOffsetConstraint; |
Gauthier Ambard | 92969941 | 2018-01-02 10:05:41 | [diff] [blame] | 921 | @synthesize imageSaver = _imageSaver; |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 922 | // DialogPresenterDelegate property |
| 923 | @synthesize dialogPresenterDelegateIsPresenting = |
| 924 | _dialogPresenterDelegateIsPresenting; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 925 | |
| 926 | #pragma mark - Object lifecycle |
| 927 | |
Mark Cogan | 5e3da15 | 2017-07-11 15:57:30 | [diff] [blame] | 928 | - (instancetype) |
| 929 | initWithTabModel:(TabModel*)model |
| 930 | browserState:(ios::ChromeBrowserState*)browserState |
| 931 | dependencyFactory:(BrowserViewControllerDependencyFactory*)factory |
| 932 | applicationCommandEndpoint:(id<ApplicationCommands>)applicationCommandEndpoint { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 933 | self = [super initWithNibName:nil bundle:base::mac::FrameworkBundle()]; |
| 934 | if (self) { |
| 935 | DCHECK(factory); |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 936 | |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 937 | _dependencyFactory = factory; |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 938 | _dialogPresenter = [[DialogPresenter alloc] initWithDelegate:self |
| 939 | presentingViewController:self]; |
justincohen | 75011c3 | 2017-04-28 16:31:39 | [diff] [blame] | 940 | _dispatcher = [[CommandDispatcher alloc] init]; |
| 941 | [_dispatcher startDispatchingToTarget:self |
| 942 | forProtocol:@protocol(UrlLoader)]; |
| 943 | [_dispatcher startDispatchingToTarget:self |
| 944 | forProtocol:@protocol(WebToolbarDelegate)]; |
| 945 | [_dispatcher startDispatchingToTarget:self |
Mark Cogan | 6c58ea9 | 2017-07-06 13:08:24 | [diff] [blame] | 946 | forProtocol:@protocol(BrowserCommands)]; |
Mark Cogan | 5e3da15 | 2017-07-11 15:57:30 | [diff] [blame] | 947 | [_dispatcher startDispatchingToTarget:applicationCommandEndpoint |
| 948 | forProtocol:@protocol(ApplicationCommands)]; |
Mark Cogan | 83da264b1 | 2017-07-19 12:21:32 | [diff] [blame] | 949 | // -startDispatchingToTarget:forProtocol: doesn't pick up protocols the |
| 950 | // passed protocol conforms to, so ApplicationSettingsCommands is explicitly |
| 951 | // dispatched to the endpoint as well. Since this is potentially |
| 952 | // fragile, DCHECK that it should still work (if the endpoint is nonnull). |
| 953 | DCHECK(!applicationCommandEndpoint || |
| 954 | [applicationCommandEndpoint |
| 955 | conformsToProtocol:@protocol(ApplicationSettingsCommands)]); |
| 956 | [_dispatcher |
| 957 | startDispatchingToTarget:applicationCommandEndpoint |
| 958 | forProtocol:@protocol(ApplicationSettingsCommands)]; |
justincohen | 75011c3 | 2017-04-28 16:31:39 | [diff] [blame] | 959 | |
edchin | 7f210cd | 2017-09-28 08:03:53 | [diff] [blame] | 960 | _snackbarCoordinator = [[SnackbarCoordinator alloc] init]; |
| 961 | _snackbarCoordinator.dispatcher = _dispatcher; |
| 962 | [_snackbarCoordinator start]; |
| 963 | |
Mark Cogan | ca30df6 | 2017-11-20 14:29:11 | [diff] [blame] | 964 | _languageSelectionCoordinator = |
| 965 | [[LanguageSelectionCoordinator alloc] initWithBaseViewController:self]; |
| 966 | _languageSelectionCoordinator.presenter = |
| 967 | [[VerticalAnimationContainer alloc] init]; |
| 968 | |
Eugene But | 49a7c57 | 2017-12-11 20:54:15 | [diff] [blame] | 969 | _passKitCoordinator = |
| 970 | [[PassKitCoordinator alloc] initWithBaseViewController:self]; |
| 971 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 972 | _javaScriptDialogPresenter.reset( |
| 973 | new JavaScriptDialogPresenterImpl(_dialogPresenter)); |
| 974 | _webStateDelegate.reset(new web::WebStateDelegateBridge(self)); |
| 975 | // TODO(leng): Delay this. |
sczs | 02ad28e | 2017-08-31 11:22:15 | [diff] [blame] | 976 | [[UpgradeCenter sharedInstance] registerClient:self |
| 977 | withDispatcher:self.dispatcher]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 978 | _inNewTabAnimation = NO; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 979 | if (model && browserState) |
| 980 | [self updateWithTabModel:model browserState:browserState]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 981 | } |
| 982 | return self; |
| 983 | } |
| 984 | |
| 985 | - (instancetype)initWithNibName:(NSString*)nibNameOrNil |
| 986 | bundle:(NSBundle*)nibBundleOrNil { |
| 987 | NOTREACHED(); |
| 988 | return nil; |
| 989 | } |
| 990 | |
| 991 | - (instancetype)initWithCoder:(NSCoder*)aDecoder { |
| 992 | NOTREACHED(); |
| 993 | return nil; |
| 994 | } |
| 995 | |
| 996 | - (void)dealloc { |
Sylvain Defresne | 41170aa | 2017-06-15 10:25:20 | [diff] [blame] | 997 | DCHECK(_isShutdown) << "-shutdown must be called before dealloc."; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 998 | } |
| 999 | |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 1000 | #pragma mark - Public Properties |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1001 | |
edchin | 3365c7d | 2017-09-01 22:20:37 | [diff] [blame] | 1002 | - (id<ApplicationCommands, |
| 1003 | BrowserCommands, |
edchin | 3365c7d | 2017-09-01 22:20:37 | [diff] [blame] | 1004 | OmniboxFocuser, |
edchin | 7f210cd | 2017-09-28 08:03:53 | [diff] [blame] | 1005 | SnackbarCommands, |
edchin | 3365c7d | 2017-09-01 22:20:37 | [diff] [blame] | 1006 | UrlLoader, |
| 1007 | WebToolbarDelegate>)dispatcher { |
Mark Cogan | 4c90130 | 2017-09-05 14:47:56 | [diff] [blame] | 1008 | return static_cast<id<ApplicationCommands, BrowserCommands, OmniboxFocuser, |
edchin | 7f210cd | 2017-09-28 08:03:53 | [diff] [blame] | 1009 | SnackbarCommands, UrlLoader, WebToolbarDelegate>>( |
| 1010 | _dispatcher); |
Mark Cogan | 6c58ea9 | 2017-07-06 13:08:24 | [diff] [blame] | 1011 | } |
| 1012 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1013 | - (void)setActive:(BOOL)active { |
| 1014 | if (_active == active) { |
| 1015 | return; |
| 1016 | } |
| 1017 | _active = active; |
| 1018 | |
| 1019 | // If not active, display an activity indicator overlay over the view to |
| 1020 | // prevent interaction with the web page. |
| 1021 | // TODO(crbug.com/637093): This coordinator should be managed by the |
| 1022 | // coordinator used to present BrowserViewController, when implemented. |
| 1023 | if (active) { |
| 1024 | [self.activityOverlayCoordinator stop]; |
| 1025 | self.activityOverlayCoordinator = nil; |
| 1026 | } else if (!self.activityOverlayCoordinator) { |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 1027 | self.activityOverlayCoordinator = |
| 1028 | [[ActivityOverlayCoordinator alloc] initWithBaseViewController:self]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1029 | [self.activityOverlayCoordinator start]; |
| 1030 | } |
| 1031 | |
| 1032 | if (_browserState) { |
Eugene But | c90499d5 | 2017-09-22 16:02:09 | [diff] [blame] | 1033 | ActiveStateManager* active_state_manager = |
| 1034 | ActiveStateManager::FromBrowserState(_browserState); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1035 | active_state_manager->SetActive(active); |
| 1036 | } |
| 1037 | |
| 1038 | [_model setWebUsageEnabled:active]; |
| 1039 | [self updateDialogPresenterActiveState]; |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 1040 | [self updateBroadcastState]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1041 | |
| 1042 | if (active) { |
| 1043 | // Make sure the tab (if any; it's possible to get here without a current |
| 1044 | // tab if the caller is about to create one) ends up on screen completely. |
| 1045 | Tab* currentTab = [_model currentTab]; |
| 1046 | // Force loading the view in case it was not loaded yet. |
Mark Cogan | 059ce7c | 2017-07-18 10:40:44 | [diff] [blame] | 1047 | [self loadViewIfNeeded]; |
Sylvain Defresne | 44835133 | 2017-12-27 10:38:36 | [diff] [blame] | 1048 | if (_expectingForegroundTab) { |
| 1049 | PagePlaceholderTabHelper::FromWebState(currentTab.webState) |
| 1050 | ->AddPlaceholderForNextNavigation(); |
| 1051 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1052 | if (currentTab) |
| 1053 | [self displayTab:currentTab isNewSelection:YES]; |
eugenebut | f8a138e6 | 2017-01-24 22:41:34 | [diff] [blame] | 1054 | } else { |
| 1055 | [_dialogPresenter cancelAllDialogs]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1056 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1057 | [_paymentRequestManager enablePaymentRequest:active]; |
| 1058 | |
| 1059 | [self setNeedsStatusBarAppearanceUpdate]; |
| 1060 | } |
| 1061 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1062 | - (BOOL)isPlayingTTS { |
| 1063 | return _voiceSearchController && _voiceSearchController->IsPlayingAudio(); |
| 1064 | } |
| 1065 | |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 1066 | - (TabModel*)tabModel { |
| 1067 | return _model; |
| 1068 | } |
| 1069 | |
sdefresne | 6165c874 | 2017-01-16 15:42:02 | [diff] [blame] | 1070 | - (ios::ChromeBrowserState*)browserState { |
| 1071 | return _browserState; |
| 1072 | } |
| 1073 | |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 1074 | #pragma mark - Private Properties |
sdefresne | 6165c874 | 2017-01-16 15:42:02 | [diff] [blame] | 1075 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1076 | - (SideSwipeController*)sideSwipeController { |
| 1077 | if (!_sideSwipeController) { |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 1078 | _sideSwipeController = |
| 1079 | [[SideSwipeController alloc] initWithTabModel:_model |
| 1080 | browserState:_browserState]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1081 | [_sideSwipeController setSnapshotDelegate:self]; |
Gauthier Ambard | 29939db1 | 2017-10-30 16:47:31 | [diff] [blame] | 1082 | _sideSwipeController.toolbarInteractionHandler = _toolbarCoordinator; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1083 | [_sideSwipeController setSwipeDelegate:self]; |
edchin | f5150c68 | 2017-09-18 02:50:03 | [diff] [blame] | 1084 | [_sideSwipeController setTabStripDelegate:self.tabStripCoordinator]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1085 | } |
| 1086 | return _sideSwipeController; |
| 1087 | } |
| 1088 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1089 | - (DialogPresenter*)dialogPresenter { |
| 1090 | return _dialogPresenter; |
| 1091 | } |
| 1092 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 1093 | - (KeyCommandsProvider*)keyCommandsProvider { |
| 1094 | if (!_keyCommandsProvider) { |
| 1095 | _keyCommandsProvider = [_dependencyFactory newKeyCommandsProvider]; |
| 1096 | } |
| 1097 | return _keyCommandsProvider; |
| 1098 | } |
| 1099 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1100 | - (BOOL)canUseDesktopUserAgent { |
| 1101 | Tab* tab = [_model currentTab]; |
| 1102 | if ([self isTabNativePage:tab]) |
| 1103 | return NO; |
| 1104 | |
| 1105 | // If |useDesktopUserAgent| is |NO|, allow useDesktopUserAgent. |
liaoyuke | b8453e1 | 2017-02-24 22:08:44 | [diff] [blame] | 1106 | return !tab.usesDesktopUserAgent; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | // Whether the sharing menu should be shown. |
| 1110 | - (BOOL)canShowShareMenu { |
Sylvain Defresne | e7f2c8a | 2017-10-17 02:39:19 | [diff] [blame] | 1111 | const GURL& URL = [_model currentTab].webState->GetLastCommittedURL(); |
kkhorimoto | b110b26 | 2017-06-01 18:38:25 | [diff] [blame] | 1112 | return URL.is_valid() && !web::GetWebClient()->IsAppSpecificURL(URL); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | - (BOOL)canShowFindBar { |
| 1116 | // Make sure web controller can handle find in page. |
| 1117 | Tab* tab = [_model currentTab]; |
rohitrao | 005a643 | 2017-03-16 20:52:42 | [diff] [blame] | 1118 | if (!tab) { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1119 | return NO; |
rohitrao | 005a643 | 2017-03-16 20:52:42 | [diff] [blame] | 1120 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1121 | |
rohitrao | 005a643 | 2017-03-16 20:52:42 | [diff] [blame] | 1122 | auto* helper = FindTabHelper::FromWebState(tab.webState); |
| 1123 | return (helper && helper->CurrentPageSupportsFindInPage() && |
| 1124 | !helper->IsFindUIActive()); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1125 | } |
| 1126 | |
liaoyuke | ea9f3ee6 | 2017-03-07 22:05:39 | [diff] [blame] | 1127 | - (web::UserAgentType)userAgentType { |
| 1128 | web::WebState* webState = [_model currentTab].webState; |
| 1129 | if (!webState) |
| 1130 | return web::UserAgentType::NONE; |
| 1131 | web::NavigationItem* visibleItem = |
| 1132 | webState->GetNavigationManager()->GetVisibleItem(); |
| 1133 | if (!visibleItem) |
| 1134 | return web::UserAgentType::NONE; |
| 1135 | |
| 1136 | return visibleItem->GetUserAgentType(); |
| 1137 | } |
| 1138 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1139 | - (void)setVisible:(BOOL)visible { |
| 1140 | if (_visible == visible) |
| 1141 | return; |
Peter Laurens | e0b80f1 | 2017-11-21 07:52:40 | [diff] [blame] | 1142 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1143 | _visible = visible; |
| 1144 | } |
| 1145 | |
| 1146 | - (void)setViewVisible:(BOOL)viewVisible { |
| 1147 | if (_viewVisible == viewVisible) |
| 1148 | return; |
| 1149 | _viewVisible = viewVisible; |
| 1150 | self.visible = viewVisible; |
| 1151 | [self updateDialogPresenterActiveState]; |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 1152 | [self updateBroadcastState]; |
| 1153 | } |
| 1154 | |
| 1155 | - (void)setBroadcasting:(BOOL)broadcasting { |
| 1156 | if (_broadcasting == broadcasting) |
| 1157 | return; |
| 1158 | _broadcasting = broadcasting; |
| 1159 | if (base::FeatureList::IsEnabled(fullscreen::features::kNewFullscreen)) { |
| 1160 | // TODO(crbug.com/790886): Use the Browser's broadcaster once Browsers are |
| 1161 | // supported. |
Kurt Horimoto | 06b9425 | 2017-12-08 19:45:59 | [diff] [blame] | 1162 | FullscreenController* fullscreenController = |
| 1163 | FullscreenControllerFactory::GetInstance()->GetForBrowserState( |
| 1164 | _browserState); |
| 1165 | ChromeBroadcaster* broadcaster = fullscreenController->broadcaster(); |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 1166 | if (_broadcasting) { |
| 1167 | _toolbarUIUpdater = [[LegacyToolbarUIUpdater alloc] |
| 1168 | initWithToolbarUI:[[ToolbarUIState alloc] init] |
| 1169 | toolbarOwner:self |
| 1170 | webStateList:[_model webStateList]]; |
| 1171 | [_toolbarUIUpdater startUpdating]; |
| 1172 | StartBroadcastingToolbarUI(_toolbarUIUpdater.toolbarUI, broadcaster); |
Kurt Horimoto | 06b9425 | 2017-12-08 19:45:59 | [diff] [blame] | 1173 | |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 1174 | _mainContentUIUpdater = [[MainContentUIStateUpdater alloc] |
| 1175 | initWithState:[[MainContentUIState alloc] init]]; |
| 1176 | _webMainContentUIForwarder = [[WebScrollViewMainContentUIForwarder alloc] |
| 1177 | initWithUpdater:_mainContentUIUpdater |
| 1178 | webStateList:[_model webStateList]]; |
| 1179 | StartBroadcastingMainContentUI(self, broadcaster); |
Kurt Horimoto | 06b9425 | 2017-12-08 19:45:59 | [diff] [blame] | 1180 | |
| 1181 | _fullscreenUIUpdater = base::MakeUnique<FullscreenUIUpdater>(self); |
| 1182 | fullscreenController->AddObserver(_fullscreenUIUpdater.get()); |
| 1183 | |
| 1184 | fullscreenController->SetWebStateList([_model webStateList]); |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 1185 | } else { |
| 1186 | StopBroadcastingToolbarUI(broadcaster); |
| 1187 | StopBroadcastingMainContentUI(broadcaster); |
| 1188 | [_toolbarUIUpdater stopUpdating]; |
| 1189 | _toolbarUIUpdater = nil; |
| 1190 | _mainContentUIUpdater = nil; |
| 1191 | [_webMainContentUIForwarder disconnect]; |
| 1192 | _webMainContentUIForwarder = nil; |
Kurt Horimoto | 06b9425 | 2017-12-08 19:45:59 | [diff] [blame] | 1193 | fullscreenController->RemoveObserver(_fullscreenUIUpdater.get()); |
| 1194 | _fullscreenUIUpdater = nullptr; |
| 1195 | fullscreenController->SetWebStateList(nullptr); |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 1196 | } |
| 1197 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | - (BOOL)isToolbarOnScreen { |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 1201 | return self.headerHeight - [self currentHeaderOffset] > 0; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1202 | } |
| 1203 | |
kkhorimoto | a44349c1 | 2017-04-12 23:02:12 | [diff] [blame] | 1204 | - (void)setInNewTabAnimation:(BOOL)inNewTabAnimation { |
| 1205 | if (_inNewTabAnimation == inNewTabAnimation) |
| 1206 | return; |
| 1207 | _inNewTabAnimation = inNewTabAnimation; |
| 1208 | [self updateDialogPresenterActiveState]; |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 1209 | [self updateBroadcastState]; |
kkhorimoto | a44349c1 | 2017-04-12 23:02:12 | [diff] [blame] | 1210 | } |
| 1211 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1212 | - (BOOL)isInNewTabAnimation { |
| 1213 | return _inNewTabAnimation; |
| 1214 | } |
| 1215 | |
| 1216 | - (BOOL)shouldShowVoiceSearchBar { |
| 1217 | // On iPads, the voice search bar should only be shown for regular horizontal |
| 1218 | // size class configurations. It should always be shown for voice search |
| 1219 | // results Tabs on iPhones, including configurations with regular horizontal |
| 1220 | // size classes (i.e. landscape iPhone 6 Plus). |
| 1221 | BOOL compactWidth = self.traitCollection.horizontalSizeClass == |
| 1222 | UIUserInterfaceSizeClassCompact; |
| 1223 | return self.tabModel.currentTab.isVoiceSearchResultsTab && |
| 1224 | (!IsIPadIdiom() || compactWidth); |
| 1225 | } |
| 1226 | |
| 1227 | - (void)setHideStatusBar:(BOOL)hideStatusBar { |
| 1228 | if (_hideStatusBar == hideStatusBar) |
| 1229 | return; |
| 1230 | _hideStatusBar = hideStatusBar; |
| 1231 | [self setNeedsStatusBarAppearanceUpdate]; |
| 1232 | } |
| 1233 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 1234 | - (NSArray<HeaderDefinition*>*)headerViews { |
| 1235 | NSMutableArray<HeaderDefinition*>* results = [[NSMutableArray alloc] init]; |
| 1236 | if (![self isViewLoaded]) |
| 1237 | return results; |
| 1238 | |
| 1239 | if (!IsIPadIdiom()) { |
| 1240 | if (_toolbarCoordinator.toolbarViewController.view) { |
| 1241 | [results addObject:[HeaderDefinition |
| 1242 | definitionWithView:_toolbarCoordinator |
| 1243 | .toolbarViewController.view |
| 1244 | headerBehaviour:Hideable |
| 1245 | heightAdjustment:0.0 |
| 1246 | inset:0.0]]; |
| 1247 | } |
| 1248 | } else { |
| 1249 | if (self.tabStripView) { |
| 1250 | [results addObject:[HeaderDefinition definitionWithView:self.tabStripView |
| 1251 | headerBehaviour:Hideable |
| 1252 | heightAdjustment:0.0 |
| 1253 | inset:0.0]]; |
| 1254 | } |
| 1255 | if (_toolbarCoordinator.toolbarViewController.view) { |
| 1256 | [results addObject:[HeaderDefinition |
| 1257 | definitionWithView:_toolbarCoordinator |
| 1258 | .toolbarViewController.view |
| 1259 | headerBehaviour:Hideable |
| 1260 | heightAdjustment:0.0 |
| 1261 | inset:0.0]]; |
| 1262 | } |
| 1263 | if ([_findBarController view]) { |
| 1264 | [results addObject:[HeaderDefinition |
| 1265 | definitionWithView:[_findBarController view] |
| 1266 | headerBehaviour:Overlap |
| 1267 | heightAdjustment:0.0 |
| 1268 | inset:kIPadFindBarOverlap]]; |
| 1269 | } |
| 1270 | } |
| 1271 | return [results copy]; |
| 1272 | } |
| 1273 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 1274 | - (CGFloat)headerOffset { |
| 1275 | if (IsIPadIdiom()) |
| 1276 | return StatusBarHeight(); |
| 1277 | return 0.0; |
| 1278 | } |
| 1279 | |
| 1280 | - (CGFloat)headerHeight { |
| 1281 | return [self headerHeightForTab:[_model currentTab]]; |
| 1282 | } |
| 1283 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 1284 | - (web::WebState*)currentWebState { |
| 1285 | return [[_model currentTab] webState]; |
| 1286 | } |
| 1287 | |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 1288 | #pragma mark - Public methods |
| 1289 | |
| 1290 | - (void)setPrimary:(BOOL)primary { |
| 1291 | [_model setPrimary:primary]; |
| 1292 | if (primary) { |
| 1293 | [self updateDialogPresenterActiveState]; |
| 1294 | [self updateBroadcastState]; |
| 1295 | } else { |
| 1296 | self.dialogPresenter.active = false; |
| 1297 | } |
| 1298 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1299 | |
| 1300 | - (void)shieldWasTapped:(id)sender { |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 1301 | [_toolbarCoordinator cancelOmniboxEdit]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1302 | } |
| 1303 | |
Cooper Knaak | d0a974cd | 2017-08-10 18:05:47 | [diff] [blame] | 1304 | - (void)userEnteredTabSwitcher { |
| 1305 | if ([self.tabTipBubblePresenter isUserEngaged]) { |
| 1306 | base::RecordAction(UserMetricsAction("NewTabTipTargetSelected")); |
| 1307 | } |
| 1308 | } |
| 1309 | |
Cooper Knaak | e963d670 | 2017-08-11 21:03:11 | [diff] [blame] | 1310 | - (void)presentBubblesIfEligible { |
| 1311 | [self presentNewTabTipBubbleOnInitialized]; |
Gregory Chatzinoff | 5663519 | 2017-12-06 02:11:26 | [diff] [blame] | 1312 | [self presentNewIncognitoTabTipBubbleOnInitialized]; |
Cooper Knaak | e963d670 | 2017-08-11 21:03:11 | [diff] [blame] | 1313 | } |
| 1314 | |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 1315 | - (void)browserStateDestroyed { |
| 1316 | [self setActive:NO]; |
| 1317 | [_paymentRequestManager close]; |
| 1318 | _paymentRequestManager = nil; |
| 1319 | [_toolbarCoordinator browserStateDestroyed]; |
| 1320 | [_model browserStateDestroyed]; |
| 1321 | |
| 1322 | // Disconnect child coordinators. |
| 1323 | [_activityServiceCoordinator disconnect]; |
| 1324 | [_qrScannerCoordinator disconnect]; |
| 1325 | [_tabHistoryCoordinator disconnect]; |
| 1326 | [_pageInfoCoordinator disconnect]; |
| 1327 | [_externalSearchCoordinator disconnect]; |
| 1328 | [self.tabStripCoordinator stop]; |
| 1329 | self.tabStripCoordinator = nil; |
| 1330 | self.tabStripView = nil; |
| 1331 | |
| 1332 | _browserState = nullptr; |
| 1333 | [_dispatcher stopDispatchingToTarget:self]; |
| 1334 | _dispatcher = nil; |
| 1335 | } |
| 1336 | |
| 1337 | - (Tab*)addSelectedTabWithURL:(const GURL&)url |
| 1338 | transition:(ui::PageTransition)transition { |
| 1339 | return [self addSelectedTabWithURL:url |
| 1340 | atIndex:[_model count] |
| 1341 | transition:transition]; |
| 1342 | } |
| 1343 | |
| 1344 | - (Tab*)addSelectedTabWithURL:(const GURL&)url |
| 1345 | atIndex:(NSUInteger)position |
| 1346 | transition:(ui::PageTransition)transition { |
| 1347 | return [self addSelectedTabWithURL:url |
| 1348 | atIndex:position |
| 1349 | transition:transition |
| 1350 | tabAddedCompletion:nil]; |
| 1351 | } |
| 1352 | |
| 1353 | - (Tab*)addSelectedTabWithURL:(const GURL&)url |
| 1354 | atIndex:(NSUInteger)position |
| 1355 | transition:(ui::PageTransition)transition |
| 1356 | tabAddedCompletion:(ProceduralBlock)tabAddedCompletion { |
| 1357 | return [self addSelectedTabWithURL:url |
| 1358 | postData:NULL |
| 1359 | atIndex:position |
| 1360 | transition:transition |
| 1361 | tabAddedCompletion:tabAddedCompletion]; |
| 1362 | } |
| 1363 | |
| 1364 | - (void)expectNewForegroundTab { |
| 1365 | _expectingForegroundTab = YES; |
| 1366 | } |
| 1367 | |
| 1368 | - (void)startVoiceSearchWithOriginView:(UIView*)originView { |
| 1369 | _voiceSearchButton = originView; |
| 1370 | // Delay Voice Search until new tab animations have finished. |
| 1371 | if (self.inNewTabAnimation) { |
| 1372 | _startVoiceSearchAfterNewTabAnimation = YES; |
| 1373 | return; |
| 1374 | } |
| 1375 | |
| 1376 | // Keyboard shouldn't overlay the ecoutez window, so dismiss find in page and |
| 1377 | // dismiss the keyboard. |
| 1378 | [self closeFindInPage]; |
| 1379 | [[_model currentTab].webController dismissKeyboard]; |
| 1380 | |
| 1381 | // Ensure that voice search objects are created. |
| 1382 | [self ensureVoiceSearchControllerCreated]; |
| 1383 | [self ensureVoiceSearchBarCreated]; |
| 1384 | |
| 1385 | // Present voice search. |
| 1386 | [_voiceSearchBar prepareToPresentVoiceSearch]; |
| 1387 | _voiceSearchController->StartRecognition(self, [_model currentTab]); |
| 1388 | [_toolbarCoordinator cancelOmniboxEdit]; |
| 1389 | } |
| 1390 | |
| 1391 | - (void)clearPresentedStateWithCompletion:(ProceduralBlock)completion |
| 1392 | dismissOmnibox:(BOOL)dismissOmnibox { |
| 1393 | [_activityServiceCoordinator cancelShare]; |
| 1394 | [_bookmarkInteractionController dismissBookmarkModalControllerAnimated:NO]; |
| 1395 | [_bookmarkInteractionController dismissSnackbar]; |
| 1396 | if (dismissOmnibox) { |
| 1397 | [_toolbarCoordinator cancelOmniboxEdit]; |
| 1398 | } |
| 1399 | [_dialogPresenter cancelAllDialogs]; |
| 1400 | [self.dispatcher hidePageInfo]; |
| 1401 | [self.tabTipBubblePresenter dismissAnimated:NO]; |
| 1402 | [self.incognitoTabTipBubblePresenter dismissAnimated:NO]; |
| 1403 | if (_voiceSearchController) |
| 1404 | _voiceSearchController->DismissMicPermissionsHelp(); |
| 1405 | |
| 1406 | Tab* currentTab = [_model currentTab]; |
| 1407 | [currentTab dismissModals]; |
| 1408 | |
| 1409 | if (currentTab) { |
| 1410 | auto* findHelper = FindTabHelper::FromWebState(currentTab.webState); |
| 1411 | if (findHelper) { |
| 1412 | findHelper->StopFinding(^{ |
| 1413 | [self updateFindBar:NO shouldFocus:NO]; |
| 1414 | }); |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | [_paymentRequestManager cancelRequest]; |
| 1419 | [_printController dismissAnimated:YES]; |
| 1420 | _printController = nil; |
| 1421 | [self.dispatcher dismissToolsMenu]; |
| 1422 | [_contextMenuCoordinator stop]; |
| 1423 | [self dismissRateThisAppDialog]; |
| 1424 | |
| 1425 | if (self.presentedViewController) { |
| 1426 | // Dismisses any other modal controllers that may be present, e.g. Recent |
| 1427 | // Tabs. |
| 1428 | // |
| 1429 | // Note that currently, some controllers like the bookmark ones were already |
| 1430 | // dismissed (in this example in -dismissBookmarkModalControllerAnimated:), |
| 1431 | // but are still reported as the presentedViewController. Calling |
| 1432 | // |dismissViewControllerAnimated:completion:| again would dismiss the BVC |
| 1433 | // itself, so instead check the value of |self.dismissingModal| and only |
| 1434 | // call dismiss if one of the above calls has not already triggered a |
| 1435 | // dismissal. |
| 1436 | // |
| 1437 | // To ensure the completion is called, nil is passed to the call to dismiss, |
| 1438 | // and the completion is called explicitly below. |
| 1439 | if (!TabSwitcherPresentsBVCEnabled() || !self.dismissingModal) { |
| 1440 | [self dismissViewControllerAnimated:NO completion:nil]; |
| 1441 | } |
| 1442 | // Dismissed controllers will be so after a delay. Queue the completion |
| 1443 | // callback after that. |
| 1444 | if (completion) { |
| 1445 | dispatch_after( |
| 1446 | dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), |
| 1447 | dispatch_get_main_queue(), ^{ |
| 1448 | completion(); |
| 1449 | }); |
| 1450 | } |
| 1451 | } else if (completion) { |
| 1452 | // If no view controllers are presented, we should be ok with dispatching |
| 1453 | // the completion block directly. |
| 1454 | dispatch_async(dispatch_get_main_queue(), completion); |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | - (UIView<TabStripFoldAnimation>*)tabStripPlaceholderView { |
| 1459 | return [self.tabStripCoordinator placeholderView]; |
| 1460 | } |
| 1461 | |
| 1462 | - (void)shutdown { |
| 1463 | DCHECK(!_isShutdown); |
| 1464 | _isShutdown = YES; |
| 1465 | [self.tabStripCoordinator stop]; |
| 1466 | self.tabStripCoordinator = nil; |
| 1467 | [_toolbarCoordinator stop]; |
| 1468 | _toolbarCoordinator = nil; |
| 1469 | self.tabStripView = nil; |
| 1470 | _infoBarContainer = nil; |
| 1471 | _readingListMenuNotifier = nil; |
| 1472 | _bookmarkModelBridge.reset(); |
| 1473 | [_model removeObserver:self]; |
| 1474 | [[UpgradeCenter sharedInstance] unregisterClient:self]; |
| 1475 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 1476 | [_toolbarCoordinator setToolbarDelegate:nil]; |
| 1477 | if (_voiceSearchController) |
| 1478 | _voiceSearchController->SetDelegate(nil); |
| 1479 | [_rateThisAppDialog setDelegate:nil]; |
| 1480 | [_model closeAllTabs]; |
| 1481 | [_paymentRequestManager setActiveWebState:nullptr]; |
| 1482 | } |
| 1483 | |
| 1484 | #pragma mark - NSObject |
| 1485 | |
| 1486 | - (BOOL)accessibilityPerformEscape { |
| 1487 | [self dismissPopups]; |
| 1488 | return YES; |
| 1489 | } |
| 1490 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 1491 | #pragma mark - UIResponder |
| 1492 | |
| 1493 | - (NSArray*)keyCommands { |
| 1494 | if (![self shouldRegisterKeyboardCommands]) { |
| 1495 | return nil; |
| 1496 | } |
| 1497 | return [self.keyCommandsProvider |
| 1498 | keyCommandsForConsumer:self |
| 1499 | baseViewController:self |
| 1500 | dispatcher:self.dispatcher |
| 1501 | editingText:![self isFirstResponder]]; |
| 1502 | } |
| 1503 | |
| 1504 | #pragma mark - UIResponder helpers |
| 1505 | |
| 1506 | // Whether the BVC should declare keyboard commands. |
| 1507 | - (BOOL)shouldRegisterKeyboardCommands { |
| 1508 | if ([self presentedViewController]) |
| 1509 | return NO; |
| 1510 | |
| 1511 | if (_voiceSearchController && _voiceSearchController->IsVisible()) |
| 1512 | return NO; |
| 1513 | |
| 1514 | // If there is no first responder, try to make the webview the first |
| 1515 | // responder. |
| 1516 | if (!GetFirstResponder()) { |
| 1517 | web::WebState* webState = _model.currentTab.webState; |
| 1518 | if (webState) |
| 1519 | [webState->GetWebViewProxy() becomeFirstResponder]; |
| 1520 | } |
| 1521 | |
| 1522 | return YES; |
| 1523 | } |
| 1524 | |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 1525 | #pragma mark - UIViewController |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1526 | |
| 1527 | // Perform additional set up after loading the view, typically from a nib. |
| 1528 | - (void)viewDidLoad { |
Justin Cohen | 13b7c432 | 2017-09-15 12:40:09 | [diff] [blame] | 1529 | CGRect initialViewsRect = self.view.bounds; |
jif | 50d5ba25 | 2016-12-20 14:00:28 | [diff] [blame] | 1530 | initialViewsRect.origin.y += StatusBarHeight(); |
| 1531 | initialViewsRect.size.height -= StatusBarHeight(); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1532 | UIViewAutoresizing initialViewAutoresizing = |
| 1533 | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; |
| 1534 | |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 1535 | self.contentArea = |
| 1536 | [[BrowserContainerView alloc] initWithFrame:initialViewsRect]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1537 | self.contentArea.autoresizingMask = initialViewAutoresizing; |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 1538 | self.typingShield = [[UIButton alloc] initWithFrame:initialViewsRect]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1539 | self.typingShield.autoresizingMask = initialViewAutoresizing; |
Justin Cohen | 41b1f38 | 2017-12-04 15:22:08 | [diff] [blame] | 1540 | self.typingShield.accessibilityIdentifier = @"Typing Shield"; |
| 1541 | self.typingShield.accessibilityLabel = l10n_util::GetNSString(IDS_CANCEL); |
| 1542 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1543 | [self.typingShield addTarget:self |
| 1544 | action:@selector(shieldWasTapped:) |
| 1545 | forControlEvents:UIControlEventTouchUpInside]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1546 | self.view.autoresizingMask = initialViewAutoresizing; |
| 1547 | self.view.backgroundColor = [UIColor colorWithWhite:0.75 alpha:1.0]; |
| 1548 | [self.view addSubview:self.contentArea]; |
| 1549 | [self.view addSubview:self.typingShield]; |
| 1550 | [super viewDidLoad]; |
| 1551 | |
| 1552 | // Install fake status bar for iPad iOS7 |
| 1553 | [self installFakeStatusBar]; |
| 1554 | [self buildToolbarAndTabStrip]; |
Justin Cohen | 4eeada3 | 2017-11-13 18:21:28 | [diff] [blame] | 1555 | [self setUpViewLayout:YES]; |
Justin Cohen | ba27610e | 2017-11-08 19:34:45 | [diff] [blame] | 1556 | if (IsSafeAreaCompatibleToolbarEnabled()) { |
Jean-François Geyelin | ed4cde7 | 2017-10-11 11:34:50 | [diff] [blame] | 1557 | [self addConstraintsToToolbar]; |
| 1558 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1559 | // If the tab model and browser state are valid, finish initialization. |
| 1560 | if (_model && _browserState) |
| 1561 | [self addUIFunctionalityForModelAndBrowserState]; |
| 1562 | |
| 1563 | // Add a tap gesture recognizer to save the last tap location for the source |
| 1564 | // location of the new tab animation. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 1565 | UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] |
| 1566 | initWithTarget:self |
| 1567 | action:@selector(saveContentAreaTapLocation:)]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1568 | [tapRecognizer setDelegate:self]; |
| 1569 | [tapRecognizer setCancelsTouchesInView:NO]; |
| 1570 | [_contentArea addGestureRecognizer:tapRecognizer]; |
| 1571 | } |
| 1572 | |
Justin Cohen | b3170c3 | 2017-09-19 01:55:22 | [diff] [blame] | 1573 | - (void)viewSafeAreaInsetsDidChange { |
| 1574 | [super viewSafeAreaInsetsDidChange]; |
| 1575 | // Gate this behind iPhone X, since it's currently the only device that |
| 1576 | // needs layout updates here after startup. |
Jean-François Geyelin | ed4cde7 | 2017-10-11 11:34:50 | [diff] [blame] | 1577 | if (IsIPhoneX()) { |
Justin Cohen | 4eeada3 | 2017-11-13 18:21:28 | [diff] [blame] | 1578 | [self setUpViewLayout:NO]; |
Jean-François Geyelin | ed4cde7 | 2017-10-11 11:34:50 | [diff] [blame] | 1579 | } |
Justin Cohen | ba27610e | 2017-11-08 19:34:45 | [diff] [blame] | 1580 | if (IsSafeAreaCompatibleToolbarEnabled()) { |
Gauthier Ambard | 100670f7 | 2017-10-27 09:54:27 | [diff] [blame] | 1581 | // TODO(crbug.com/778236): Check if this call can be removed once the |
| 1582 | // Toolbar is a contained ViewController. |
sczs | 42f7f748 | 2017-11-08 01:13:27 | [diff] [blame] | 1583 | [_toolbarCoordinator.toolbarViewController viewSafeAreaInsetsDidChange]; |
Gauthier Ambard | 100670f7 | 2017-10-27 09:54:27 | [diff] [blame] | 1584 | [_toolbarCoordinator adjustToolbarHeight]; |
Jean-François Geyelin | ed4cde7 | 2017-10-11 11:34:50 | [diff] [blame] | 1585 | } |
Justin Cohen | b3170c3 | 2017-09-19 01:55:22 | [diff] [blame] | 1586 | } |
| 1587 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1588 | - (void)viewDidAppear:(BOOL)animated { |
| 1589 | [super viewDidAppear:animated]; |
| 1590 | self.viewVisible = YES; |
| 1591 | [self updateDialogPresenterActiveState]; |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 1592 | [self updateBroadcastState]; |
Gregory Chatzinoff | 541b864 | 2017-10-25 00:25:21 | [diff] [blame] | 1593 | |
| 1594 | // |viewDidAppear| can be called after |browserState| is destroyed. Since |
| 1595 | // |presentBubblesIfEligible| requires that |self.browserState| is not NULL, |
| 1596 | // check for |self.browserState| before calling the presenting the bubbles. |
| 1597 | if (self.browserState) { |
| 1598 | [self presentBubblesIfEligible]; |
| 1599 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | - (void)viewWillAppear:(BOOL)animated { |
| 1603 | [super viewWillAppear:animated]; |
| 1604 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1605 | self.visible = YES; |
| 1606 | |
| 1607 | // Restore hidden infobars. |
Rohit Rao | 755c37b | 2017-11-10 14:05:52 | [diff] [blame] | 1608 | if (IsIPadIdiom() && _infoBarContainer) { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1609 | _infoBarContainer->RestoreInfobars(); |
| 1610 | } |
| 1611 | |
| 1612 | // If the controller is suspended, or has been paged out due to low memory, |
| 1613 | // updating the view will be handled when it's displayed again. |
| 1614 | if (![_model webUsageEnabled] || !self.contentArea) |
| 1615 | return; |
| 1616 | // Update the displayed tab (if any; the switcher may not have created one |
| 1617 | // yet) in case it changed while showing the switcher. |
| 1618 | Tab* currentTab = [_model currentTab]; |
| 1619 | if (currentTab) |
| 1620 | [self displayTab:currentTab isNewSelection:YES]; |
| 1621 | } |
| 1622 | |
| 1623 | - (void)viewWillDisappear:(BOOL)animated { |
| 1624 | self.viewVisible = NO; |
| 1625 | [self updateDialogPresenterActiveState]; |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 1626 | [self updateBroadcastState]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1627 | [[_model currentTab] wasHidden]; |
| 1628 | [_bookmarkInteractionController dismissSnackbar]; |
Rohit Rao | 755c37b | 2017-11-10 14:05:52 | [diff] [blame] | 1629 | if (IsIPadIdiom() && _infoBarContainer) { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1630 | _infoBarContainer->SuspendInfobars(); |
| 1631 | } |
| 1632 | [super viewWillDisappear:animated]; |
| 1633 | } |
| 1634 | |
| 1635 | - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orient |
| 1636 | duration:(NSTimeInterval)duration { |
| 1637 | [super willRotateToInterfaceOrientation:orient duration:duration]; |
| 1638 | [self dismissPopups]; |
| 1639 | [self reshowFindBarIfNeededWithCoordinator:nil]; |
| 1640 | } |
| 1641 | |
| 1642 | - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)orient { |
| 1643 | [super didRotateFromInterfaceOrientation:orient]; |
| 1644 | |
| 1645 | // This reinitializes the toolbar, including updating the Overlay View, |
| 1646 | // if there is one. |
| 1647 | [self updateToolbar]; |
Mark Cogan | 80aa28d | 2017-11-30 13:11:34 | [diff] [blame] | 1648 | [self infoBarContainerStateDidChangeAnimated:NO]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | - (BOOL)prefersStatusBarHidden { |
| 1652 | return self.hideStatusBar; |
| 1653 | } |
| 1654 | |
| 1655 | // Called when in the foreground and the OS needs more memory. Release as much |
| 1656 | // as possible. |
| 1657 | - (void)didReceiveMemoryWarning { |
| 1658 | // Releases the view if it doesn't have a superview. |
| 1659 | [super didReceiveMemoryWarning]; |
| 1660 | |
| 1661 | // Release any cached data, images, etc that aren't in use. |
| 1662 | // TODO(pinkerton): This feels like it should go in the MemoryPurger class, |
| 1663 | // but since the FaviconCache uses obj-c in the header, it can't be included |
| 1664 | // there. |
| 1665 | if (_browserState) { |
| 1666 | FaviconLoader* loader = |
| 1667 | IOSChromeFaviconLoaderFactory::GetForBrowserStateIfExists( |
| 1668 | _browserState); |
| 1669 | if (loader) |
| 1670 | loader->PurgeCache(); |
| 1671 | } |
| 1672 | |
| 1673 | if (![self isViewLoaded]) { |
| 1674 | // Do not release |_infoBarContainer|, as this must have the same lifecycle |
| 1675 | // as the BrowserViewController. |
| 1676 | self.contentArea = nil; |
| 1677 | self.typingShield = nil; |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 1678 | if (_voiceSearchController) |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1679 | _voiceSearchController->SetDelegate(nil); |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 1680 | _readingListCoordinator = nil; |
Gauthier Ambard | d4287fc | 2017-08-29 09:14:42 | [diff] [blame] | 1681 | self.recentTabsCoordinator = nil; |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 1682 | _toolbarCoordinator = nil; |
Kurt Horimoto | ea429dd | 2017-11-28 02:24:30 | [diff] [blame] | 1683 | [_toolbarUIUpdater stopUpdating]; |
| 1684 | _toolbarUIUpdater = nil; |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 1685 | _toolbarModelDelegate = nil; |
| 1686 | _toolbarModelIOS = nil; |
edchin | f5150c68 | 2017-09-18 02:50:03 | [diff] [blame] | 1687 | [self.tabStripCoordinator stop]; |
| 1688 | self.tabStripCoordinator = nil; |
| 1689 | self.tabStripView = nil; |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 1690 | _sideSwipeController = nil; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | - (void)traitCollectionDidChange:(UITraitCollection*)previousTraitCollection { |
| 1695 | [super traitCollectionDidChange:previousTraitCollection]; |
| 1696 | // TODO(crbug.com/527092): - traitCollectionDidChange: is not always forwarded |
| 1697 | // because in some cases the presented view controller isn't a child of the |
| 1698 | // BVC in the view controller hierarchy (some intervening object isn't a |
| 1699 | // view controller). |
| 1700 | [self.presentedViewController |
| 1701 | traitCollectionDidChange:previousTraitCollection]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1702 | // Update voice search bar visibility. |
| 1703 | [self updateVoiceSearchBarVisibilityAnimated:NO]; |
| 1704 | } |
| 1705 | |
| 1706 | - (void)viewWillTransitionToSize:(CGSize)size |
| 1707 | withTransitionCoordinator: |
| 1708 | (id<UIViewControllerTransitionCoordinator>)coordinator { |
| 1709 | [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; |
| 1710 | [self dismissPopups]; |
| 1711 | [self reshowFindBarIfNeededWithCoordinator:coordinator]; |
| 1712 | } |
| 1713 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1714 | - (void)dismissViewControllerAnimated:(BOOL)flag |
| 1715 | completion:(void (^)())completion { |
Rohit Rao | 685807a5 | 2017-11-10 20:50:11 | [diff] [blame] | 1716 | // It is an error to call this method when no VC is being presented. |
| 1717 | DCHECK(!TabSwitcherPresentsBVCEnabled() || self.presentedViewController); |
| 1718 | |
Rohit Rao | a668c02 | 2017-11-08 00:04:44 | [diff] [blame] | 1719 | // Some calling code invokes |dismissViewControllerAnimated:completion:| |
| 1720 | // multiple times. When the BVC is displayed using VC containment, multiple |
| 1721 | // calls are effectively idempotent because only the first call has any effect |
| 1722 | // and subsequent calls do nothing. However, when the BVC is presented, |
| 1723 | // subsequent calls end up dismissing the BVC itself. This is never what we |
Rohit Rao | 685807a5 | 2017-11-10 20:50:11 | [diff] [blame] | 1724 | // want, so check for this case and return early. It is not enough to check |
| 1725 | // |self.dismissingModal| because some dismissals do not go through |
| 1726 | // -[BrowserViewController dismissViewControllerAnimated:completion:|. |
Rohit Rao | a668c02 | 2017-11-08 00:04:44 | [diff] [blame] | 1727 | // TODO(crbug.com/782338): Fix callers and remove this early return. |
Rohit Rao | 685807a5 | 2017-11-10 20:50:11 | [diff] [blame] | 1728 | if (TabSwitcherPresentsBVCEnabled() && |
| 1729 | (self.dismissingModal || self.presentedViewController.isBeingDismissed)) { |
Rohit Rao | a668c02 | 2017-11-08 00:04:44 | [diff] [blame] | 1730 | return; |
| 1731 | } |
| 1732 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1733 | self.dismissingModal = YES; |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 1734 | __weak BrowserViewController* weakSelf = self; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1735 | [super dismissViewControllerAnimated:flag |
| 1736 | completion:^{ |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 1737 | BrowserViewController* strongSelf = weakSelf; |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 1738 | strongSelf.dismissingModal = NO; |
| 1739 | strongSelf.dialogPresenterDelegateIsPresenting = |
| 1740 | NO; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1741 | if (completion) |
| 1742 | completion(); |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 1743 | [strongSelf.dialogPresenter tryToPresent]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1744 | }]; |
| 1745 | } |
| 1746 | |
| 1747 | - (void)presentViewController:(UIViewController*)viewControllerToPresent |
| 1748 | animated:(BOOL)flag |
| 1749 | completion:(void (^)())completion { |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 1750 | ProceduralBlock finalCompletionHandler = [completion copy]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1751 | // TODO(crbug.com/580098) This is an interim fix for the flicker between the |
| 1752 | // launch screen and the FRE Animation. The fix is, if the FRE is about to be |
| 1753 | // presented, to show a temporary view of the launch screen and then remove it |
| 1754 | // when the controller for the FRE has been presented. This fix should be |
| 1755 | // removed when the FRE startup code is rewritten. |
| 1756 | BOOL firstRunLaunch = (FirstRun::IsChromeFirstRun() || |
| 1757 | experimental_flags::AlwaysDisplayFirstRun()) && |
| 1758 | !tests_hook::DisableFirstRun(); |
| 1759 | // These if statements check that |presentViewController| is being called for |
| 1760 | // the FRE case. |
| 1761 | if (firstRunLaunch && |
| 1762 | [viewControllerToPresent isKindOfClass:[UINavigationController class]]) { |
| 1763 | UINavigationController* navController = |
| 1764 | base::mac::ObjCCastStrict<UINavigationController>( |
| 1765 | viewControllerToPresent); |
| 1766 | if ([navController.topViewController |
| 1767 | isMemberOfClass:[WelcomeToChromeViewController class]]) { |
| 1768 | self.hideStatusBar = YES; |
| 1769 | |
| 1770 | // Load view from Launch Screen and add it to window. |
| 1771 | NSBundle* mainBundle = base::mac::FrameworkBundle(); |
| 1772 | NSArray* topObjects = |
| 1773 | [mainBundle loadNibNamed:@"LaunchScreen" owner:self options:nil]; |
| 1774 | UIViewController* launchScreenController = |
| 1775 | base::mac::ObjCCastStrict<UIViewController>([topObjects lastObject]); |
| 1776 | // |launchScreenView| is loaded as an autoreleased object, and is retained |
| 1777 | // by the |completion| block below. |
| 1778 | UIView* launchScreenView = launchScreenController.view; |
| 1779 | launchScreenView.userInteractionEnabled = NO; |
| 1780 | launchScreenView.frame = self.view.window.bounds; |
| 1781 | [self.view.window addSubview:launchScreenView]; |
| 1782 | |
| 1783 | // Replace the completion handler sent to the superclass with one which |
| 1784 | // removes |launchScreenView| and resets the status bar. If |completion| |
| 1785 | // exists, it is called from within the new completion handler. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 1786 | __weak BrowserViewController* weakSelf = self; |
| 1787 | finalCompletionHandler = ^{ |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1788 | [launchScreenView removeFromSuperview]; |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 1789 | weakSelf.hideStatusBar = NO; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1790 | if (completion) |
| 1791 | completion(); |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 1792 | }; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1793 | } |
| 1794 | } |
| 1795 | |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 1796 | self.dialogPresenterDelegateIsPresenting = YES; |
| 1797 | if ([self.sideSwipeController inSwipe]) { |
| 1798 | [self.sideSwipeController resetContentView]; |
justincohen | 7e61cd9 | 2016-12-24 00:38:17 | [diff] [blame] | 1799 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1800 | |
| 1801 | [super presentViewController:viewControllerToPresent |
| 1802 | animated:flag |
| 1803 | completion:finalCompletionHandler]; |
| 1804 | } |
| 1805 | |
Mark Cogan | 80aa28d | 2017-11-30 13:11:34 | [diff] [blame] | 1806 | - (BOOL)shouldAutorotate { |
Kurt Horimoto | 3a449ce | 2017-12-27 18:22:14 | [diff] [blame] | 1807 | if (self.presentedViewController.beingPresented || |
| 1808 | self.presentedViewController.beingDismissed) { |
| 1809 | // Don't rotate while a presentation or dismissal animation is occurring. |
Mark Cogan | 80aa28d | 2017-11-30 13:11:34 | [diff] [blame] | 1810 | return NO; |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 1811 | } else if (_sideSwipeController && |
| 1812 | ![self.sideSwipeController shouldAutorotate]) { |
Mark Cogan | 80aa28d | 2017-11-30 13:11:34 | [diff] [blame] | 1813 | // Don't auto rotate if side swipe controller view says not to. |
| 1814 | return NO; |
| 1815 | } else { |
| 1816 | return [super shouldAutorotate]; |
| 1817 | } |
| 1818 | } |
| 1819 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 1820 | - (UIStatusBarStyle)preferredStatusBarStyle { |
| 1821 | return (IsIPadIdiom() || _isOffTheRecord) ? UIStatusBarStyleLightContent |
| 1822 | : UIStatusBarStyleDefault; |
| 1823 | } |
| 1824 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 1825 | #pragma mark - ** Private BVC Methods ** |
| 1826 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 1827 | #pragma mark - Private Methods: BVC Initialization |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1828 | |
| 1829 | - (void)updateWithTabModel:(TabModel*)model |
| 1830 | browserState:(ios::ChromeBrowserState*)browserState { |
| 1831 | DCHECK(model); |
| 1832 | DCHECK(browserState); |
| 1833 | DCHECK(!_model); |
| 1834 | DCHECK(!_browserState); |
| 1835 | _browserState = browserState; |
| 1836 | _isOffTheRecord = browserState->IsOffTheRecord() ? YES : NO; |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 1837 | _model = model; |
Mark Cogan | dfcdea7 | 2017-07-18 13:47:38 | [diff] [blame] | 1838 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1839 | [_model addObserver:self]; |
| 1840 | |
| 1841 | if (!_isOffTheRecord) { |
| 1842 | [DefaultIOSWebViewFactory |
| 1843 | registerWebViewFactory:[ChromeWebViewFactory class]]; |
| 1844 | } |
| 1845 | NSUInteger count = [_model count]; |
| 1846 | for (NSUInteger index = 0; index < count; ++index) |
| 1847 | [self installDelegatesForTab:[_model tabAtIndex:index]]; |
| 1848 | |
gambard | bdc07cc | 2017-02-03 16:43:11 | [diff] [blame] | 1849 | _imageFetcher = base::MakeUnique<image_fetcher::IOSImageDataFetcherWrapper>( |
Sylvain Defresne | 4aa6efc | 2017-08-10 16:14:12 | [diff] [blame] | 1850 | _browserState->GetRequestContext()); |
Gauthier Ambard | 92969941 | 2018-01-02 10:05:41 | [diff] [blame] | 1851 | self.imageSaver = [[ImageSaver alloc] initWithBaseViewController:self]; |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 1852 | _dominantColorCache = [[NSMutableDictionary alloc] init]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1853 | |
sdefresne | dc432f4 | 2017-01-17 14:36:59 | [diff] [blame] | 1854 | // Register for bookmark changed notification (BookmarkModel may be null |
| 1855 | // during testing, so explicitly support this). |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1856 | _bookmarkModel = ios::BookmarkModelFactory::GetForBrowserState(_browserState); |
sdefresne | dc432f4 | 2017-01-17 14:36:59 | [diff] [blame] | 1857 | if (_bookmarkModel) { |
Gauthier Ambard | 65e949b09 | 2017-11-29 08:46:20 | [diff] [blame] | 1858 | _bookmarkModelBridge.reset( |
| 1859 | new bookmarks::BookmarkModelBridge(self, _bookmarkModel)); |
sdefresne | dc432f4 | 2017-01-17 14:36:59 | [diff] [blame] | 1860 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1861 | } |
| 1862 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1863 | - (void)installFakeStatusBar { |
Justin Cohen | b3170c3 | 2017-09-19 01:55:22 | [diff] [blame] | 1864 | CGFloat statusBarHeight = StatusBarHeight(); |
| 1865 | CGRect statusBarFrame = |
| 1866 | CGRectMake(0, 0, [[self view] frame].size.width, statusBarHeight); |
| 1867 | _fakeStatusBarView = [[UIView alloc] initWithFrame:statusBarFrame]; |
| 1868 | [_fakeStatusBarView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1869 | if (IsIPadIdiom()) { |
Justin Cohen | b3170c3 | 2017-09-19 01:55:22 | [diff] [blame] | 1870 | [_fakeStatusBarView setBackgroundColor:StatusBarBackgroundColor()]; |
| 1871 | [_fakeStatusBarView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; |
| 1872 | [_fakeStatusBarView layer].zPosition = 99; |
| 1873 | [[self view] addSubview:_fakeStatusBarView]; |
| 1874 | } else { |
| 1875 | // Add a white bar on phone so that the status bar on the NTP is white. |
| 1876 | [_fakeStatusBarView setBackgroundColor:[UIColor whiteColor]]; |
| 1877 | [self.view insertSubview:_fakeStatusBarView atIndex:0]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1878 | } |
| 1879 | } |
| 1880 | |
| 1881 | // Create the UI elements. May or may not have valid browser state & tab model. |
| 1882 | - (void)buildToolbarAndTabStrip { |
| 1883 | DCHECK([self isViewLoaded]); |
| 1884 | DCHECK(!_toolbarModelDelegate); |
| 1885 | |
Rohit Rao | 44f20430 | 2017-08-10 14:49:54 | [diff] [blame] | 1886 | // Initialize the prerender service before creating the toolbar controller. |
| 1887 | PrerenderService* prerenderService = |
| 1888 | PrerenderServiceFactory::GetForBrowserState(self.browserState); |
| 1889 | if (prerenderService) { |
| 1890 | prerenderService->SetDelegate(self); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | // Create the toolbar model and controller. |
rohitrao | 8c4c7fd | 2017-04-03 15:31:20 | [diff] [blame] | 1894 | _toolbarModelDelegate.reset( |
| 1895 | new ToolbarModelDelegateIOS([_model webStateList])); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1896 | _toolbarModelIOS.reset([_dependencyFactory |
| 1897 | newToolbarModelIOSWithDelegate:_toolbarModelDelegate.get()]); |
Peter Laurens | e0b80f1 | 2017-11-21 07:52:40 | [diff] [blame] | 1898 | _toolbarCoordinator = [[LegacyToolbarCoordinator alloc] |
| 1899 | initWithBaseViewController:self |
| 1900 | toolsMenuConfigurationProvider:self |
Kurt Horimoto | 975327e | 2017-12-20 01:38:06 | [diff] [blame] | 1901 | dispatcher:self.dispatcher |
| 1902 | browserState:_browserState]; |
Peter Laurens | e0b80f1 | 2017-11-21 07:52:40 | [diff] [blame] | 1903 | |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 1904 | self.sideSwipeController.toolbarInteractionHandler = _toolbarCoordinator; |
Peter Laurens | e0b80f1 | 2017-11-21 07:52:40 | [diff] [blame] | 1905 | |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 1906 | _toolbarCoordinator.tabModel = _model; |
Gauthier Ambard | 82c8cc5 | 2017-10-26 15:59:05 | [diff] [blame] | 1907 | [_toolbarCoordinator |
sczs | 32cfde8 | 2017-11-14 20:43:22 | [diff] [blame] | 1908 | setToolbarController: |
| 1909 | [_dependencyFactory |
| 1910 | newToolbarControllerWithDelegate:self |
| 1911 | urlLoader:self |
| 1912 | dispatcher:self.dispatcher]]; |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 1913 | [_dispatcher startDispatchingToTarget:_toolbarCoordinator |
justincohen | 75011c3 | 2017-04-28 16:31:39 | [diff] [blame] | 1914 | forProtocol:@protocol(OmniboxFocuser)]; |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 1915 | [_toolbarCoordinator setTabCount:[_model count]]; |
Kurt Horimoto | 06b9425 | 2017-12-08 19:45:59 | [diff] [blame] | 1916 | [_toolbarCoordinator start]; |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 1917 | [self updateBroadcastState]; |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 1918 | if (_voiceSearchController) |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 1919 | _voiceSearchController->SetDelegate( |
Gauthier Ambard | 82c8cc5 | 2017-10-26 15:59:05 | [diff] [blame] | 1920 | [_toolbarCoordinator voiceSearchDelegate]); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1921 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1922 | if (IsIPadIdiom()) { |
edchin | f5150c68 | 2017-09-18 02:50:03 | [diff] [blame] | 1923 | self.tabStripCoordinator = |
| 1924 | [[TabStripLegacyCoordinator alloc] initWithBaseViewController:self]; |
| 1925 | self.tabStripCoordinator.browserState = _browserState; |
| 1926 | self.tabStripCoordinator.dispatcher = _dispatcher; |
| 1927 | self.tabStripCoordinator.tabModel = _model; |
| 1928 | self.tabStripCoordinator.presentationProvider = self; |
| 1929 | self.tabStripCoordinator.animationWaitDuration = |
Kurt Horimoto | 62e97c7 | 2017-11-03 19:51:47 | [diff] [blame] | 1930 | kLegacyFullscreenControllerToolbarAnimationDuration; |
edchin | f5150c68 | 2017-09-18 02:50:03 | [diff] [blame] | 1931 | [self.tabStripCoordinator start]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | // Create infobar container. |
| 1935 | if (!_infoBarContainerDelegate) { |
| 1936 | _infoBarContainerDelegate.reset(new InfoBarContainerDelegateIOS(self)); |
| 1937 | _infoBarContainer.reset( |
| 1938 | new InfoBarContainerIOS(_infoBarContainerDelegate.get())); |
| 1939 | } |
| 1940 | } |
| 1941 | |
Jean-François Geyelin | ed4cde7 | 2017-10-11 11:34:50 | [diff] [blame] | 1942 | - (void)addConstraintsToToolbar { |
Jean-François Geyelin | ce0a474 | 2017-10-25 12:34:11 | [diff] [blame] | 1943 | NSLayoutYAxisAnchor* topAnchor; |
| 1944 | // On iPad, the toolbar is underneath the tab strip. |
| 1945 | // On iPhone, it is underneath the top of the screen. |
| 1946 | if (IsIPadIdiom()) { |
| 1947 | topAnchor = self.tabStripView.bottomAnchor; |
| 1948 | } else { |
| 1949 | topAnchor = [self view].topAnchor; |
| 1950 | } |
| 1951 | |
Gauthier Ambard | 100670f7 | 2017-10-27 09:54:27 | [diff] [blame] | 1952 | [_toolbarCoordinator adjustToolbarHeight]; |
Jean-François Geyelin | ed4cde7 | 2017-10-11 11:34:50 | [diff] [blame] | 1953 | |
Justin Cohen | 9fe9ef67 | 2017-12-01 20:37:43 | [diff] [blame] | 1954 | self.toolbarOffsetConstraint = |
| 1955 | [_toolbarCoordinator.toolbarViewController.view.topAnchor |
| 1956 | constraintEqualToAnchor:topAnchor]; |
Jean-François Geyelin | ed4cde7 | 2017-10-11 11:34:50 | [diff] [blame] | 1957 | [NSLayoutConstraint activateConstraints:@[ |
Justin Cohen | 9fe9ef67 | 2017-12-01 20:37:43 | [diff] [blame] | 1958 | self.toolbarOffsetConstraint, |
sczs | 42f7f748 | 2017-11-08 01:13:27 | [diff] [blame] | 1959 | [_toolbarCoordinator.toolbarViewController.view.leadingAnchor |
Jean-François Geyelin | ed4cde7 | 2017-10-11 11:34:50 | [diff] [blame] | 1960 | constraintEqualToAnchor:[self view].leadingAnchor], |
sczs | 42f7f748 | 2017-11-08 01:13:27 | [diff] [blame] | 1961 | [_toolbarCoordinator.toolbarViewController.view.trailingAnchor |
Jean-François Geyelin | ed4cde7 | 2017-10-11 11:34:50 | [diff] [blame] | 1962 | constraintEqualToAnchor:[self view].trailingAnchor], |
Jean-François Geyelin | ed4cde7 | 2017-10-11 11:34:50 | [diff] [blame] | 1963 | ]]; |
| 1964 | [[self view] layoutIfNeeded]; |
| 1965 | } |
| 1966 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1967 | // Enable functionality that only makes sense if the views are loaded and |
| 1968 | // both browser state and tab model are valid. |
| 1969 | - (void)addUIFunctionalityForModelAndBrowserState { |
| 1970 | DCHECK(_browserState); |
Randall Raymond | 8b66a40 | 2017-06-09 14:19:05 | [diff] [blame] | 1971 | DCHECK(_toolbarModelIOS); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1972 | DCHECK(_model); |
| 1973 | DCHECK([self isViewLoaded]); |
| 1974 | |
| 1975 | [self.sideSwipeController addHorizontalGesturesToView:self.view]; |
| 1976 | |
Rohit Rao | af46af9 | 2017-08-10 12:52:30 | [diff] [blame] | 1977 | infobars::InfoBarManager* infoBarManager = nullptr; |
| 1978 | if (_model.currentTab) { |
| 1979 | DCHECK(_model.currentTab.webState); |
| 1980 | infoBarManager = |
| 1981 | InfoBarManagerImpl::FromWebState(_model.currentTab.webState); |
| 1982 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 1983 | _infoBarContainer->ChangeInfoBarManager(infoBarManager); |
| 1984 | |
sczs | dd860eba | 2017-08-10 01:55:38 | [diff] [blame] | 1985 | // Create child coordinators. |
Rohit Rao | 01e0e00 | 2017-08-14 20:49:43 | [diff] [blame] | 1986 | _activityServiceCoordinator = [[ActivityServiceLegacyCoordinator alloc] |
| 1987 | initWithBaseViewController:self]; |
| 1988 | _activityServiceCoordinator.dispatcher = _dispatcher; |
| 1989 | _activityServiceCoordinator.tabModel = _model; |
| 1990 | _activityServiceCoordinator.browserState = _browserState; |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 1991 | _activityServiceCoordinator.positionProvider = |
Gauthier Ambard | 82c8cc5 | 2017-10-26 15:59:05 | [diff] [blame] | 1992 | [_toolbarCoordinator activityServicePositioner]; |
Rohit Rao | 01e0e00 | 2017-08-14 20:49:43 | [diff] [blame] | 1993 | _activityServiceCoordinator.presentationProvider = self; |
Rohit Rao | 01e0e00 | 2017-08-14 20:49:43 | [diff] [blame] | 1994 | |
Rohit Rao | cda0a99 | 2017-08-16 15:37:11 | [diff] [blame] | 1995 | _qrScannerCoordinator = |
| 1996 | [[QRScannerLegacyCoordinator alloc] initWithBaseViewController:self]; |
| 1997 | _qrScannerCoordinator.dispatcher = _dispatcher; |
Gauthier Ambard | 82c8cc5 | 2017-10-26 15:59:05 | [diff] [blame] | 1998 | _qrScannerCoordinator.loadProvider = |
| 1999 | [_toolbarCoordinator QRScannerResultLoader]; |
Rohit Rao | cda0a99 | 2017-08-16 15:37:11 | [diff] [blame] | 2000 | _qrScannerCoordinator.presentationProvider = self; |
| 2001 | |
Kurt Horimoto | 238ae69 | 2017-12-22 23:55:47 | [diff] [blame] | 2002 | _tabHistoryCoordinator = [[LegacyTabHistoryCoordinator alloc] |
| 2003 | initWithBaseViewController:self |
| 2004 | browserState:_browserState]; |
sczs | dd860eba | 2017-08-10 01:55:38 | [diff] [blame] | 2005 | _tabHistoryCoordinator.dispatcher = _dispatcher; |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 2006 | _tabHistoryCoordinator.positionProvider = |
Gauthier Ambard | 82c8cc5 | 2017-10-26 15:59:05 | [diff] [blame] | 2007 | [_toolbarCoordinator tabHistoryPositioner]; |
sczs | dd860eba | 2017-08-10 01:55:38 | [diff] [blame] | 2008 | _tabHistoryCoordinator.tabModel = _model; |
| 2009 | _tabHistoryCoordinator.presentationProvider = self; |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 2010 | _tabHistoryCoordinator.tabHistoryUIUpdater = |
Gauthier Ambard | 82c8cc5 | 2017-10-26 15:59:05 | [diff] [blame] | 2011 | [_toolbarCoordinator tabHistoryUIUpdater]; |
sczs | dd860eba | 2017-08-10 01:55:38 | [diff] [blame] | 2012 | |
sczs | 6ae47ad | 2017-09-06 17:26:53 | [diff] [blame] | 2013 | _sadTabCoordinator = [[SadTabLegacyCoordinator alloc] init]; |
edchin | 9eaf25f5 | 2017-10-26 02:42:20 | [diff] [blame] | 2014 | _sadTabCoordinator.baseViewController = self; |
| 2015 | _sadTabCoordinator.dispatcher = self.dispatcher; |
sczs | 6ae47ad | 2017-09-06 17:26:53 | [diff] [blame] | 2016 | |
sczs | 281fbdc2 | 2017-12-20 20:59:06 | [diff] [blame] | 2017 | // If there are any existing SadTabHelpers in |_model|, update the helpers |
| 2018 | // delegate with the new |_sadTabCoordinator|. |
| 2019 | for (NSUInteger i = 0; i < _model.count; i++) { |
| 2020 | SadTabTabHelper* sadTabHelper = |
| 2021 | SadTabTabHelper::FromWebState([_model tabAtIndex:i].webState); |
| 2022 | DCHECK(sadTabHelper); |
| 2023 | if (sadTabHelper) { |
| 2024 | sadTabHelper->SetDelegate(_sadTabCoordinator); |
| 2025 | } |
| 2026 | } |
| 2027 | |
Kurt Horimoto | 084d73b | 2017-12-22 23:50:46 | [diff] [blame] | 2028 | _pageInfoCoordinator = [[PageInfoLegacyCoordinator alloc] |
| 2029 | initWithBaseViewController:self |
| 2030 | browserState:_browserState]; |
Gregory Chatzinoff | df93d69 | 2017-09-09 01:32:27 | [diff] [blame] | 2031 | _pageInfoCoordinator.dispatcher = _dispatcher; |
| 2032 | _pageInfoCoordinator.loader = self; |
| 2033 | _pageInfoCoordinator.presentationProvider = self; |
| 2034 | _pageInfoCoordinator.tabModel = _model; |
| 2035 | |
Louis Romero | d11747a | 2017-10-20 20:10:35 | [diff] [blame] | 2036 | _externalSearchCoordinator = [[ExternalSearchCoordinator alloc] init]; |
| 2037 | _externalSearchCoordinator.dispatcher = _dispatcher; |
| 2038 | |
mathp | 9b4c11d | 2017-07-06 20:24:13 | [diff] [blame] | 2039 | if (base::FeatureList::IsEnabled(payments::features::kWebPayments)) { |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 2040 | _paymentRequestManager = [[PaymentRequestManager alloc] |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2041 | initWithBaseViewController:self |
Gregory Chatzinoff | 1c96f80 | 2017-08-18 19:02:20 | [diff] [blame] | 2042 | browserState:_browserState |
| 2043 | dispatcher:self.dispatcher]; |
Randall Raymond | 8b66a40 | 2017-06-09 14:19:05 | [diff] [blame] | 2044 | [_paymentRequestManager setToolbarModel:_toolbarModelIOS.get()]; |
Mohamad Ahmadi | 7d09ec3 | 2017-07-11 22:32:19 | [diff] [blame] | 2045 | [_paymentRequestManager setActiveWebState:[_model currentTab].webState]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | // Set the frame for the various views. View must be loaded. |
Justin Cohen | 4eeada3 | 2017-11-13 18:21:28 | [diff] [blame] | 2050 | - (void)setUpViewLayout:(BOOL)initialLayout { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2051 | DCHECK([self isViewLoaded]); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2052 | CGFloat widthOfView = CGRectGetWidth([self view].bounds); |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 2053 | CGFloat minY = self.headerOffset; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2054 | |
Justin Cohen | b3170c3 | 2017-09-19 01:55:22 | [diff] [blame] | 2055 | // Update the fake toolbar background height. |
| 2056 | CGRect fakeStatusBarFrame = _fakeStatusBarView.frame; |
| 2057 | fakeStatusBarFrame.size.height = StatusBarHeight(); |
| 2058 | _fakeStatusBarView.frame = fakeStatusBarFrame; |
| 2059 | |
edchin | f5150c68 | 2017-09-18 02:50:03 | [diff] [blame] | 2060 | if (self.tabStripView) { |
| 2061 | minY += CGRectGetHeight([self.tabStripView frame]); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | // Position the toolbar next, either at the top of the browser view or |
| 2065 | // directly under the tabstrip. |
Justin Cohen | 4eeada3 | 2017-11-13 18:21:28 | [diff] [blame] | 2066 | if (initialLayout) |
| 2067 | [self addChildViewController:_toolbarCoordinator.toolbarViewController]; |
sczs | 42f7f748 | 2017-11-08 01:13:27 | [diff] [blame] | 2068 | CGRect toolbarFrame = _toolbarCoordinator.toolbarViewController.view.frame; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2069 | toolbarFrame.origin = CGPointMake(0, minY); |
| 2070 | toolbarFrame.size.width = widthOfView; |
Justin Cohen | ba27610e | 2017-11-08 19:34:45 | [diff] [blame] | 2071 | if (!IsSafeAreaCompatibleToolbarEnabled()) { |
sczs | 42f7f748 | 2017-11-08 01:13:27 | [diff] [blame] | 2072 | [_toolbarCoordinator.toolbarViewController.view setFrame:toolbarFrame]; |
Jean-François Geyelin | ed4cde7 | 2017-10-11 11:34:50 | [diff] [blame] | 2073 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2074 | |
| 2075 | // Place the infobar container above the content area. |
| 2076 | InfoBarContainerView* infoBarContainerView = _infoBarContainer->view(); |
Justin Cohen | 4eeada3 | 2017-11-13 18:21:28 | [diff] [blame] | 2077 | if (initialLayout) |
| 2078 | [self.view insertSubview:infoBarContainerView aboveSubview:_contentArea]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2079 | |
Gauthier Ambard | 087e357 | 2017-12-20 12:54:47 | [diff] [blame] | 2080 | // Place the toolbar controller above the infobar container and adds the |
Gauthier Ambard | 470c50f | 2017-12-21 07:55:29 | [diff] [blame] | 2081 | // layout guides. |
Gauthier Ambard | 087e357 | 2017-12-20 12:54:47 | [diff] [blame] | 2082 | if (initialLayout) { |
Justin Cohen | 4eeada3 | 2017-11-13 18:21:28 | [diff] [blame] | 2083 | [[self view] insertSubview:_toolbarCoordinator.toolbarViewController.view |
| 2084 | aboveSubview:infoBarContainerView]; |
Gauthier Ambard | 087e357 | 2017-12-20 12:54:47 | [diff] [blame] | 2085 | AddNamedGuide(kOmniboxGuide, self.view); |
Gauthier Ambard | 470c50f | 2017-12-21 07:55:29 | [diff] [blame] | 2086 | AddNamedGuide(kBackButtonGuide, self.view); |
| 2087 | AddNamedGuide(kForwardButtonGuide, self.view); |
Gauthier Ambard | 087e357 | 2017-12-20 12:54:47 | [diff] [blame] | 2088 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2089 | minY += CGRectGetHeight(toolbarFrame); |
Justin Cohen | 4eeada3 | 2017-11-13 18:21:28 | [diff] [blame] | 2090 | if (initialLayout) |
| 2091 | [_toolbarCoordinator.toolbarViewController |
| 2092 | didMoveToParentViewController:self]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2093 | |
| 2094 | // Account for the toolbar's drop shadow. The toolbar overlaps with the web |
| 2095 | // content slightly. |
sczs | 8c83778 | 2017-10-03 02:57:24 | [diff] [blame] | 2096 | minY -= 0.0; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2097 | |
| 2098 | // Adjust the content area to be under the toolbar, for fullscreen or below |
| 2099 | // the toolbar is not fullscreen. |
| 2100 | CGRect contentFrame = [_contentArea frame]; |
| 2101 | CGFloat marginWithHeader = StatusBarHeight(); |
Justin Cohen | b3170c3 | 2017-09-19 01:55:22 | [diff] [blame] | 2102 | contentFrame.size.height = CGRectGetMaxY(contentFrame) - marginWithHeader; |
| 2103 | contentFrame.origin.y = marginWithHeader; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2104 | [_contentArea setFrame:contentFrame]; |
| 2105 | |
| 2106 | // Adjust the infobar container to be either at the bottom of the screen |
| 2107 | // (iPhone) or on the lower toolbar edge (iPad). |
| 2108 | CGRect infoBarFrame = contentFrame; |
| 2109 | infoBarFrame.origin.y = CGRectGetMaxY(contentFrame); |
| 2110 | infoBarFrame.size.height = 0; |
| 2111 | [infoBarContainerView setFrame:infoBarFrame]; |
| 2112 | |
| 2113 | // Attach the typing shield to the content area but have it hidden. |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 2114 | [self.typingShield setFrame:[_contentArea frame]]; |
Justin Cohen | 41b1f38 | 2017-12-04 15:22:08 | [diff] [blame] | 2115 | if (initialLayout) { |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 2116 | [[self view] insertSubview:self.typingShield aboveSubview:_contentArea]; |
| 2117 | [self.typingShield setHidden:YES]; |
Justin Cohen | 41b1f38 | 2017-12-04 15:22:08 | [diff] [blame] | 2118 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2119 | } |
| 2120 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2121 | - (void)displayTab:(Tab*)tab isNewSelection:(BOOL)newSelection { |
| 2122 | DCHECK(tab); |
Mark Cogan | 059ce7c | 2017-07-18 10:40:44 | [diff] [blame] | 2123 | [self loadViewIfNeeded]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2124 | |
kkhorimoto | a44349c1 | 2017-04-12 23:02:12 | [diff] [blame] | 2125 | if (!self.inNewTabAnimation) { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2126 | // Hide findbar. |updateToolbar| will restore the findbar later. |
| 2127 | [self hideFindBarWithAnimation:NO]; |
| 2128 | |
| 2129 | // Make new content visible, resizing it first as the orientation may |
| 2130 | // have changed from the last time it was displayed. |
| 2131 | [[tab view] setFrame:_contentArea.bounds]; |
| 2132 | [_contentArea displayContentView:[tab view]]; |
| 2133 | } |
| 2134 | [self updateToolbar]; |
| 2135 | |
| 2136 | if (newSelection) |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 2137 | [_toolbarCoordinator selectedTabChanged]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2138 | |
| 2139 | // Notify the Tab that it was displayed. |
| 2140 | [tab wasShown]; |
| 2141 | } |
| 2142 | |
| 2143 | - (void)initializeBookmarkInteractionController { |
| 2144 | if (_bookmarkInteractionController) |
| 2145 | return; |
edchin | bb8ba89 | 2017-09-12 15:44:03 | [diff] [blame] | 2146 | _bookmarkInteractionController = [[BookmarkInteractionController alloc] |
| 2147 | initWithBrowserState:_browserState |
| 2148 | loader:self |
| 2149 | parentController:self |
| 2150 | dispatcher:self.dispatcher]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2151 | } |
| 2152 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 2153 | - (void)setOverScrollActionControllerToStaticNativeContent: |
| 2154 | (StaticHtmlNativeContent*)nativeContent { |
| 2155 | if (!IsIPadIdiom()) { |
| 2156 | OverscrollActionsController* controller = |
| 2157 | [[OverscrollActionsController alloc] |
| 2158 | initWithScrollView:[nativeContent scrollView]]; |
| 2159 | [controller setDelegate:self]; |
| 2160 | OverscrollStyle style = _isOffTheRecord |
| 2161 | ? OverscrollStyle::REGULAR_PAGE_INCOGNITO |
| 2162 | : OverscrollStyle::REGULAR_PAGE_NON_INCOGNITO; |
| 2163 | controller.style = style; |
| 2164 | nativeContent.overscrollActionsController = controller; |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | #pragma mark - Private Methods: UI Configuration, update and Layout |
| 2169 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2170 | // Update the state of back and forward buttons, hiding the forward button if |
| 2171 | // there is nowhere to go. Assumes the model's current tab is up to date. |
| 2172 | - (void)updateToolbar { |
| 2173 | // If the BVC has been partially torn down for low memory, wait for the |
| 2174 | // view rebuild to handle toolbar updates. |
| 2175 | if (!(_toolbarModelIOS && _browserState)) |
| 2176 | return; |
| 2177 | |
| 2178 | Tab* tab = [_model currentTab]; |
| 2179 | if (![tab navigationManager]) |
| 2180 | return; |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 2181 | [_toolbarCoordinator updateToolbarState]; |
| 2182 | [_toolbarCoordinator setShareButtonEnabled:self.canShowShareMenu]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2183 | |
Sylvain Defresne | f5d2d95 | 2017-11-14 11:15:31 | [diff] [blame] | 2184 | if (_insertedTabWasPrerenderedTab && !_toolbarModelIOS->IsLoading()) |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 2185 | [_toolbarCoordinator showPrerenderingAnimation]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2186 | |
rohitrao | 005a643 | 2017-03-16 20:52:42 | [diff] [blame] | 2187 | auto* findHelper = FindTabHelper::FromWebState(tab.webState); |
| 2188 | if (findHelper && findHelper->IsFindUIActive()) { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2189 | [self showFindBarWithAnimation:NO |
| 2190 | selectText:YES |
| 2191 | shouldFocus:[_findBarController isFocused]]; |
rohitrao | b2bf3cb | 2017-02-10 14:10:36 | [diff] [blame] | 2192 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2193 | |
| 2194 | // Hide the toolbar if displaying phone NTP. |
| 2195 | if (!IsIPadIdiom()) { |
kkhorimoto | 7aed9e26 | 2017-03-04 02:28:55 | [diff] [blame] | 2196 | web::NavigationItem* item = [tab navigationManager]->GetVisibleItem(); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2197 | BOOL hideToolbar = NO; |
kkhorimoto | 7aed9e26 | 2017-03-04 02:28:55 | [diff] [blame] | 2198 | if (item) { |
| 2199 | GURL url = item->GetURL(); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2200 | BOOL isNTP = url.GetOrigin() == GURL(kChromeUINewTabURL); |
| 2201 | hideToolbar = isNTP && !_isOffTheRecord && |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 2202 | ![_toolbarCoordinator isOmniboxFirstResponder] && |
| 2203 | ![_toolbarCoordinator showingOmniboxPopup]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2204 | } |
sczs | 42f7f748 | 2017-11-08 01:13:27 | [diff] [blame] | 2205 | [_toolbarCoordinator.toolbarViewController.view setHidden:hideToolbar]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2206 | } |
| 2207 | } |
| 2208 | |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 2209 | - (void)updateBroadcastState { |
| 2210 | self.broadcasting = |
Kurt Horimoto | ea429dd | 2017-11-28 02:24:30 | [diff] [blame] | 2211 | self.active && self.viewVisible && !self.inNewTabAnimation; |
Kurt Horimoto | ea429dd | 2017-11-28 02:24:30 | [diff] [blame] | 2212 | } |
| 2213 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2214 | - (void)updateDialogPresenterActiveState { |
kkhorimoto | a44349c1 | 2017-04-12 23:02:12 | [diff] [blame] | 2215 | self.dialogPresenter.active = |
| 2216 | self.active && self.viewVisible && !self.inNewTabAnimation; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | - (void)dismissPopups { |
Peter Laurens | e0b80f1 | 2017-11-21 07:52:40 | [diff] [blame] | 2220 | [self.dispatcher dismissToolsMenu]; |
Gregory Chatzinoff | df93d69 | 2017-09-09 01:32:27 | [diff] [blame] | 2221 | [self.dispatcher hidePageInfo]; |
Gauthier Ambard | bf38224 | 2017-10-19 14:51:28 | [diff] [blame] | 2222 | [_tabHistoryCoordinator dismissHistoryPopup]; |
Gregory Chatzinoff | f3ff2bc | 2017-12-15 01:19:26 | [diff] [blame] | 2223 | [self.tabTipBubblePresenter dismissAnimated:NO]; |
| 2224 | [self.incognitoTabTipBubblePresenter dismissAnimated:NO]; |
Cooper Knaak | 33f9f40 | 2017-08-09 18:04:38 | [diff] [blame] | 2225 | } |
| 2226 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 2227 | - (BOOL)isTabScrolledToTop:(Tab*)tab { |
| 2228 | CGPoint scrollOffset = |
| 2229 | tab.webState->GetWebViewProxy().scrollViewProxy.contentOffset; |
| 2230 | |
| 2231 | // If there is a native controller, use the native controller's scroll offset. |
| 2232 | id nativeController = [self nativeControllerForTab:tab]; |
| 2233 | if ([nativeController conformsToProtocol:@protocol(CRWNativeContent)] && |
| 2234 | [nativeController respondsToSelector:@selector(scrollOffset)]) { |
| 2235 | scrollOffset = [nativeController scrollOffset]; |
| 2236 | } |
| 2237 | return CGPointEqualToPoint(scrollOffset, CGPointZero); |
| 2238 | } |
| 2239 | |
| 2240 | - (UIView*)footerView { |
| 2241 | return _voiceSearchBar; |
| 2242 | } |
| 2243 | |
| 2244 | - (CGFloat)headerHeightForTab:(Tab*)tab { |
| 2245 | id nativeController = [self nativeControllerForTab:tab]; |
| 2246 | if ([nativeController conformsToProtocol:@protocol(ToolbarOwner)] && |
| 2247 | [nativeController respondsToSelector:@selector(toolbarHeight)] && |
| 2248 | [nativeController toolbarHeight] > 0.0 && !IsIPadIdiom()) { |
| 2249 | // On iPhone, don't add any header height for ToolbarOwner native |
| 2250 | // controllers when they're displaying their own toolbar. |
| 2251 | return 0; |
| 2252 | } |
| 2253 | |
| 2254 | NSArray<HeaderDefinition*>* views = [self headerViews]; |
| 2255 | |
| 2256 | CGFloat height = self.headerOffset; |
| 2257 | for (HeaderDefinition* header in views) { |
| 2258 | if (header.view && header.behaviour == Hideable) { |
| 2259 | height += CGRectGetHeight([header.view frame]) - |
| 2260 | header.heightAdjustement - header.inset; |
| 2261 | } |
| 2262 | } |
| 2263 | |
| 2264 | return height - StatusBarHeight(); |
| 2265 | } |
| 2266 | |
| 2267 | - (void)setFramesForHeaders:(NSArray<HeaderDefinition*>*)headers |
| 2268 | atOffset:(CGFloat)headerOffset { |
| 2269 | CGFloat height = self.headerOffset; |
| 2270 | for (HeaderDefinition* header in headers) { |
| 2271 | CGFloat yOrigin = height - headerOffset - header.inset; |
| 2272 | // Make sure the toolbarView's constraints are also updated. Leaving the |
| 2273 | // -setFrame call to minimize changes in this CL -- otherwise the way |
| 2274 | // toolbar_view manages it's alpha changes would also need to be updated. |
| 2275 | // TODO(crbug.com/778822): This can be cleaned up when the new fullscreen |
| 2276 | // is enabled. |
| 2277 | if (IsSafeAreaCompatibleToolbarEnabled() && |
| 2278 | header.view == _toolbarCoordinator.toolbarViewController.view && |
| 2279 | !IsIPadIdiom()) { |
| 2280 | self.toolbarOffsetConstraint.constant = yOrigin; |
| 2281 | } |
| 2282 | CGRect frame = [header.view frame]; |
| 2283 | frame.origin.y = yOrigin; |
| 2284 | [header.view setFrame:frame]; |
| 2285 | if (header.behaviour != Overlap) |
| 2286 | height += CGRectGetHeight(frame); |
| 2287 | } |
| 2288 | } |
| 2289 | |
| 2290 | - (CardView*)addCardViewInFullscreen:(BOOL)fullScreen { |
| 2291 | CGRect frame = [_contentArea frame]; |
| 2292 | if (!fullScreen) { |
| 2293 | // Changing the origin here is unnecessary, it's set in page_animation_util. |
| 2294 | frame.size.height -= self.headerHeight; |
| 2295 | } |
| 2296 | |
| 2297 | CGFloat shortAxis = frame.size.width; |
| 2298 | CGFloat shortInset = kCardImageInsets.left + kCardImageInsets.right; |
| 2299 | shortAxis -= shortInset + 2 * page_animation_util::kCardMargin; |
| 2300 | CGFloat aspectRatio = frame.size.height / frame.size.width; |
| 2301 | CGFloat longAxis = std::floor(aspectRatio * shortAxis); |
| 2302 | CGFloat longInset = kCardImageInsets.top + kCardImageInsets.bottom; |
| 2303 | CGSize cardSize = CGSizeMake(shortAxis + shortInset, longAxis + longInset); |
| 2304 | CGRect cardFrame = {frame.origin, cardSize}; |
| 2305 | |
| 2306 | CardView* card = |
| 2307 | [[CardView alloc] initWithFrame:cardFrame isIncognito:_isOffTheRecord]; |
| 2308 | card.closeButtonSide = IsPortrait() ? CardCloseButtonSide::TRAILING |
| 2309 | : CardCloseButtonSide::LEADING; |
| 2310 | [_contentArea addSubview:card]; |
| 2311 | return card; |
| 2312 | } |
| 2313 | |
| 2314 | #pragma mark - Private Methods: Showing and Dismissing Child UI |
| 2315 | |
| 2316 | - (void)showAllBookmarks { |
| 2317 | DCHECK(self.visible || self.dismissingModal); |
| 2318 | GURL URL(kChromeUIBookmarksURL); |
| 2319 | Tab* tab = [_model currentTab]; |
| 2320 | web::NavigationManager::WebLoadParams params(URL); |
| 2321 | params.transition_type = ui::PAGE_TRANSITION_AUTO_BOOKMARK; |
| 2322 | [tab navigationManager]->LoadURLWithParams(params); |
| 2323 | } |
| 2324 | |
| 2325 | - (void)showNTPPanel:(ntp_home::PanelIdentifier)panel { |
| 2326 | DCHECK(self.visible || self.dismissingModal); |
| 2327 | GURL url(kChromeUINewTabURL); |
| 2328 | std::string fragment(NewTabPage::FragmentFromIdentifier(panel)); |
| 2329 | if (fragment != "") { |
| 2330 | GURL::Replacements replacement; |
| 2331 | replacement.SetRefStr(fragment); |
| 2332 | url = url.ReplaceComponents(replacement); |
| 2333 | } |
| 2334 | Tab* tab = [_model currentTab]; |
| 2335 | web::NavigationManager::WebLoadParams params(url); |
| 2336 | params.transition_type = ui::PAGE_TRANSITION_AUTO_BOOKMARK; |
| 2337 | [tab navigationManager]->LoadURLWithParams(params); |
| 2338 | } |
| 2339 | |
| 2340 | - (void)dismissRateThisAppDialog { |
| 2341 | if (_rateThisAppDialog) { |
| 2342 | base::RecordAction(base::UserMetricsAction( |
| 2343 | "IOSRateThisAppDialogDismissedProgramatically")); |
| 2344 | [_rateThisAppDialog dismiss]; |
| 2345 | _rateThisAppDialog = nil; |
| 2346 | } |
| 2347 | } |
| 2348 | |
| 2349 | #pragma mark - Private Methods: Bubble views |
| 2350 | |
Cooper Knaak | d0a974cd | 2017-08-10 18:05:47 | [diff] [blame] | 2351 | - (BubbleViewControllerPresenter*) |
| 2352 | bubblePresenterForFeature:(const base::Feature&)feature |
| 2353 | direction:(BubbleArrowDirection)direction |
| 2354 | alignment:(BubbleAlignment)alignment |
| 2355 | text:(NSString*)text { |
Gregory Chatzinoff | 541b864 | 2017-10-25 00:25:21 | [diff] [blame] | 2356 | DCHECK(self.browserState); |
| 2357 | if (!feature_engagement::TrackerFactory::GetForBrowserState(self.browserState) |
Cooper Knaak | 33f9f40 | 2017-08-09 18:04:38 | [diff] [blame] | 2358 | ->ShouldTriggerHelpUI(feature)) { |
Cooper Knaak | d0a974cd | 2017-08-10 18:05:47 | [diff] [blame] | 2359 | return nil; |
Cooper Knaak | 33f9f40 | 2017-08-09 18:04:38 | [diff] [blame] | 2360 | } |
| 2361 | // Capture |weakSelf| instead of the feature engagement tracker object |
| 2362 | // because |weakSelf| will safely become |nil| if it is deallocated, whereas |
| 2363 | // the feature engagement tracker will remain pointing to invalid memory if |
| 2364 | // its owner (the ChromeBrowserState) is deallocated. |
| 2365 | __weak BrowserViewController* weakSelf = self; |
| 2366 | void (^dismissalCallback)(void) = ^() { |
| 2367 | BrowserViewController* strongSelf = weakSelf; |
| 2368 | if (strongSelf) { |
| 2369 | feature_engagement::TrackerFactory::GetForBrowserState( |
| 2370 | strongSelf.browserState) |
| 2371 | ->Dismissed(feature); |
| 2372 | } |
| 2373 | }; |
| 2374 | |
Cooper Knaak | d0a974cd | 2017-08-10 18:05:47 | [diff] [blame] | 2375 | BubbleViewControllerPresenter* bubbleViewControllerPresenter = |
Cooper Knaak | 33f9f40 | 2017-08-09 18:04:38 | [diff] [blame] | 2376 | [[BubbleViewControllerPresenter alloc] initWithText:text |
| 2377 | arrowDirection:direction |
| 2378 | alignment:alignment |
| 2379 | dismissalCallback:dismissalCallback]; |
| 2380 | |
Cooper Knaak | d0a974cd | 2017-08-10 18:05:47 | [diff] [blame] | 2381 | return bubbleViewControllerPresenter; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2382 | } |
| 2383 | |
Cooper Knaak | 120cee5e | 2017-08-10 20:57:00 | [diff] [blame] | 2384 | - (void)presentNewTabTipBubbleOnInitialized { |
Gregory Chatzinoff | 541b864 | 2017-10-25 00:25:21 | [diff] [blame] | 2385 | DCHECK(self.browserState); |
Cooper Knaak | 120cee5e | 2017-08-10 20:57:00 | [diff] [blame] | 2386 | // If the tab tip bubble has already been presented and the user is still |
| 2387 | // considered engaged, it can't be overwritten or set to |nil| or else it will |
| 2388 | // reset the |userEngaged| property. Once the user is not engaged, the bubble |
| 2389 | // can be safely overwritten or set to |nil|. |
| 2390 | if (!self.tabTipBubblePresenter.isUserEngaged) { |
| 2391 | __weak BrowserViewController* weakSelf = self; |
| 2392 | void (^onInitializedBlock)(bool) = ^(bool successfullyLoaded) { |
| 2393 | [weakSelf presentNewTabTipBubble]; |
| 2394 | }; |
| 2395 | |
| 2396 | // Because the new tab tip occurs on startup, the feature engagement |
| 2397 | // tracker's database is not guaranteed to be loaded by this time. For the |
| 2398 | // bubble to appear properly, a callback is used to guarantee the event data |
| 2399 | // is loaded before the check to see if the promotion should be displayed. |
| 2400 | feature_engagement::TrackerFactory::GetForBrowserState(self.browserState) |
| 2401 | ->AddOnInitializedCallback(base::BindBlockArc(onInitializedBlock)); |
| 2402 | } |
| 2403 | } |
| 2404 | |
| 2405 | - (void)presentNewTabTipBubble { |
Gregory Chatzinoff | 541b864 | 2017-10-25 00:25:21 | [diff] [blame] | 2406 | DCHECK(self.browserState); |
Gregory Chatzinoff | f192d5d | 2017-12-09 00:26:07 | [diff] [blame] | 2407 | // If the BVC is not visible, do not present the bubble. |
| 2408 | if (!self.viewVisible) |
| 2409 | return; |
Gregory Chatzinoff | 87695621 | 2017-12-18 22:33:23 | [diff] [blame] | 2410 | // Do not present the bubble if there is no current tab or if the current tab |
| 2411 | // is the NTP. |
Gregory Chatzinoff | f192d5d | 2017-12-09 00:26:07 | [diff] [blame] | 2412 | Tab* currentTab = [self.tabModel currentTab]; |
| 2413 | if (!currentTab) |
| 2414 | return; |
| 2415 | if (currentTab.webState->GetVisibleURL() == kChromeUINewTabURL) |
| 2416 | return; |
Gregory Chatzinoff | e205f4464 | 2017-12-19 17:54:03 | [diff] [blame] | 2417 | |
| 2418 | // Do not present the bubble if the tab is not scrolled to the top. |
| 2419 | if (![self isTabScrolledToTop:currentTab]) |
Gregory Chatzinoff | 87695621 | 2017-12-18 22:33:23 | [diff] [blame] | 2420 | return; |
Gregory Chatzinoff | f192d5d | 2017-12-09 00:26:07 | [diff] [blame] | 2421 | |
Cooper Knaak | 120cee5e | 2017-08-10 20:57:00 | [diff] [blame] | 2422 | NSString* text = |
| 2423 | l10n_util::GetNSStringWithFixup(IDS_IOS_NEW_TAB_IPH_PROMOTION_TEXT); |
| 2424 | CGPoint tabSwitcherAnchor; |
| 2425 | if (IsIPadIdiom()) { |
edchin | f5150c68 | 2017-09-18 02:50:03 | [diff] [blame] | 2426 | DCHECK([self.tabStripCoordinator |
Cooper Knaak | 120cee5e | 2017-08-10 20:57:00 | [diff] [blame] | 2427 | respondsToSelector:@selector(anchorPointForTabSwitcherButton:)]); |
edchin | f5150c68 | 2017-09-18 02:50:03 | [diff] [blame] | 2428 | tabSwitcherAnchor = [self.tabStripCoordinator |
Cooper Knaak | 120cee5e | 2017-08-10 20:57:00 | [diff] [blame] | 2429 | anchorPointForTabSwitcherButton:BubbleArrowDirectionUp]; |
| 2430 | } else { |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 2431 | DCHECK([_toolbarCoordinator |
Cooper Knaak | 120cee5e | 2017-08-10 20:57:00 | [diff] [blame] | 2432 | respondsToSelector:@selector(anchorPointForTabSwitcherButton:)]); |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 2433 | tabSwitcherAnchor = [_toolbarCoordinator |
Cooper Knaak | 120cee5e | 2017-08-10 20:57:00 | [diff] [blame] | 2434 | anchorPointForTabSwitcherButton:BubbleArrowDirectionUp]; |
| 2435 | } |
Gregory Chatzinoff | f3ff2bc | 2017-12-15 01:19:26 | [diff] [blame] | 2436 | |
Cooper Knaak | e963d670 | 2017-08-11 21:03:11 | [diff] [blame] | 2437 | // If the feature engagement tracker does not consider it valid to display |
Gregory Chatzinoff | f3ff2bc | 2017-12-15 01:19:26 | [diff] [blame] | 2438 | // the new tab tip, then end early to prevent the potential reassignment |
| 2439 | // of the existing |tabTipBubblePresenter| to nil. |
| 2440 | BubbleViewControllerPresenter* presenter = |
Cooper Knaak | 120cee5e | 2017-08-10 20:57:00 | [diff] [blame] | 2441 | [self bubblePresenterForFeature:feature_engagement::kIPHNewTabTipFeature |
| 2442 | direction:BubbleArrowDirectionUp |
| 2443 | alignment:BubbleAlignmentTrailing |
| 2444 | text:text]; |
Gregory Chatzinoff | f3ff2bc | 2017-12-15 01:19:26 | [diff] [blame] | 2445 | if (!presenter) |
| 2446 | return; |
| 2447 | |
| 2448 | self.tabTipBubblePresenter = presenter; |
| 2449 | |
Cooper Knaak | 120cee5e | 2017-08-10 20:57:00 | [diff] [blame] | 2450 | [self.tabTipBubblePresenter presentInViewController:self |
| 2451 | view:self.view |
| 2452 | anchorPoint:tabSwitcherAnchor]; |
| 2453 | } |
| 2454 | |
Helen Yang | 9175bd5 | 2017-08-12 00:28:40 | [diff] [blame] | 2455 | - (void)presentNewIncognitoTabTipBubbleOnInitialized { |
Gregory Chatzinoff | 541b864 | 2017-10-25 00:25:21 | [diff] [blame] | 2456 | DCHECK(self.browserState); |
Helen Yang | 9175bd5 | 2017-08-12 00:28:40 | [diff] [blame] | 2457 | // Do not override |incognitoTabtipBubblePresenter| or set it to nil if the |
| 2458 | // user is still considered engaged. |
| 2459 | if (!self.incognitoTabTipBubblePresenter.isUserEngaged) { |
| 2460 | __weak BrowserViewController* weakSelf = self; |
| 2461 | void (^onInitializedBlock)(bool) = ^(bool successfullyLoaded) { |
| 2462 | [weakSelf presentNewIncognitoTabTipBubble]; |
| 2463 | }; |
| 2464 | |
| 2465 | // Use a callback in case the new incognito tab tip should be shown on |
| 2466 | // startup. This ensures that the tracker's database will be fully loaded |
| 2467 | // before checking if the promotion should be displayed. |
| 2468 | feature_engagement::TrackerFactory::GetForBrowserState(self.browserState) |
| 2469 | ->AddOnInitializedCallback(base::BindBlockArc(onInitializedBlock)); |
| 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | - (void)presentNewIncognitoTabTipBubble { |
Gregory Chatzinoff | 541b864 | 2017-10-25 00:25:21 | [diff] [blame] | 2474 | DCHECK(self.browserState); |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 2475 | DCHECK([_toolbarCoordinator |
Helen Yang | 9175bd5 | 2017-08-12 00:28:40 | [diff] [blame] | 2476 | respondsToSelector:@selector(anchorPointForToolsMenuButton:)]); |
Gregory Chatzinoff | f192d5d | 2017-12-09 00:26:07 | [diff] [blame] | 2477 | // If the BVC is not visible, do not present the bubble. |
| 2478 | if (!self.viewVisible) |
| 2479 | return; |
| 2480 | |
Gregory Chatzinoff | 87695621 | 2017-12-18 22:33:23 | [diff] [blame] | 2481 | // Do not present the bubble if there is no current tab. |
| 2482 | Tab* currentTab = [self.tabModel currentTab]; |
| 2483 | if (!currentTab) |
| 2484 | return; |
Gregory Chatzinoff | e205f4464 | 2017-12-19 17:54:03 | [diff] [blame] | 2485 | |
| 2486 | // Do not present the bubble if the tab is not scrolled to the top. |
| 2487 | if (![self isTabScrolledToTop:currentTab]) |
Gregory Chatzinoff | 87695621 | 2017-12-18 22:33:23 | [diff] [blame] | 2488 | return; |
| 2489 | |
Helen Yang | 9175bd5 | 2017-08-12 00:28:40 | [diff] [blame] | 2490 | NSString* text = l10n_util::GetNSStringWithFixup( |
| 2491 | IDS_IOS_NEW_INCOGNITO_TAB_IPH_PROMOTION_TEXT); |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 2492 | CGPoint toolsButtonAnchor = [_toolbarCoordinator |
Helen Yang | 9175bd5 | 2017-08-12 00:28:40 | [diff] [blame] | 2493 | anchorPointForToolsMenuButton:BubbleArrowDirectionUp]; |
Gregory Chatzinoff | f3ff2bc | 2017-12-15 01:19:26 | [diff] [blame] | 2494 | |
| 2495 | // If the feature engagement tracker does not consider it valid to display |
| 2496 | // the incognito tab tip, then end early to prevent the potential reassignment |
| 2497 | // of the existing |incognitoTabTipBubblePresenter| to nil. |
| 2498 | BubbleViewControllerPresenter* presenter = |
Helen Yang | 9175bd5 | 2017-08-12 00:28:40 | [diff] [blame] | 2499 | [self bubblePresenterForFeature:feature_engagement:: |
| 2500 | kIPHNewIncognitoTabTipFeature |
| 2501 | direction:BubbleArrowDirectionUp |
| 2502 | alignment:BubbleAlignmentTrailing |
| 2503 | text:text]; |
Gregory Chatzinoff | f3ff2bc | 2017-12-15 01:19:26 | [diff] [blame] | 2504 | if (!presenter) |
| 2505 | return; |
| 2506 | |
| 2507 | self.incognitoTabTipBubblePresenter = presenter; |
| 2508 | |
Helen Yang | 9175bd5 | 2017-08-12 00:28:40 | [diff] [blame] | 2509 | [self.incognitoTabTipBubblePresenter |
| 2510 | presentInViewController:self |
| 2511 | view:self.view |
| 2512 | anchorPoint:toolsButtonAnchor]; |
Gregory Chatzinoff | f3ff2bc | 2017-12-15 01:19:26 | [diff] [blame] | 2513 | [_toolbarCoordinator triggerToolsMenuButtonAnimation]; |
Helen Yang | 9175bd5 | 2017-08-12 00:28:40 | [diff] [blame] | 2514 | } |
| 2515 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 2516 | #pragma mark - Private Methods: Find Bar UI |
Gregory Chatzinoff | e205f4464 | 2017-12-19 17:54:03 | [diff] [blame] | 2517 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 2518 | - (void)hideFindBarWithAnimation:(BOOL)animate { |
| 2519 | [_findBarController hideFindBarView:animate]; |
Gregory Chatzinoff | e205f4464 | 2017-12-19 17:54:03 | [diff] [blame] | 2520 | } |
| 2521 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 2522 | - (void)showFindBarWithAnimation:(BOOL)animate |
| 2523 | selectText:(BOOL)selectText |
| 2524 | shouldFocus:(BOOL)shouldFocus { |
| 2525 | DCHECK(_findBarController); |
| 2526 | Tab* tab = [_model currentTab]; |
| 2527 | DCHECK(tab); |
| 2528 | CRWWebController* webController = tab.webController; |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 2529 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 2530 | CGRect referenceFrame = CGRectZero; |
| 2531 | if (IsIPadIdiom()) { |
| 2532 | referenceFrame = webController.visibleFrame; |
| 2533 | referenceFrame.origin.y -= kIPadFindBarOverlap; |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 2534 | } else { |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 2535 | referenceFrame = _contentArea.frame; |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 2536 | } |
| 2537 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 2538 | CGRect omniboxFrame = [_toolbarCoordinator visibleOmniboxFrame]; |
| 2539 | [_findBarController addFindBarView:animate |
| 2540 | intoView:self.view |
| 2541 | withFrame:referenceFrame |
| 2542 | alignWithFrame:omniboxFrame |
| 2543 | selectText:selectText]; |
| 2544 | [self updateFindBar:YES shouldFocus:shouldFocus]; |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 2545 | } |
| 2546 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 2547 | - (void)updateFindBar:(BOOL)initialUpdate shouldFocus:(BOOL)shouldFocus { |
| 2548 | // TODO(crbug.com/731045): This early return temporarily replaces a DCHECK. |
| 2549 | // For unknown reasons, this DCHECK sometimes was hit in the wild, resulting |
| 2550 | // in a crash. |
| 2551 | if (![_model currentTab]) { |
| 2552 | return; |
| 2553 | } |
| 2554 | auto* helper = FindTabHelper::FromWebState([_model currentTab].webState); |
| 2555 | if (helper && helper->IsFindUIActive()) { |
| 2556 | if (initialUpdate && !_isOffTheRecord) { |
| 2557 | helper->RestoreSearchTerm(); |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 2558 | } |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 2559 | |
| 2560 | [self setFramesForHeaders:[self headerViews] |
| 2561 | atOffset:[self currentHeaderOffset]]; |
| 2562 | [_findBarController updateView:helper->GetFindResult() |
| 2563 | initialUpdate:initialUpdate |
| 2564 | focusTextfield:shouldFocus]; |
| 2565 | } else { |
| 2566 | [self hideFindBarWithAnimation:YES]; |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 2567 | } |
| 2568 | } |
| 2569 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 2570 | - (void)reshowFindBarIfNeededWithCoordinator: |
| 2571 | (id<UIViewControllerTransitionCoordinator>)coordinator { |
| 2572 | if (![_findBarController isFindInPageShown]) |
| 2573 | return; |
| 2574 | |
| 2575 | // Record focused state. |
| 2576 | BOOL isFocusedBeforeReshow = [_findBarController isFocused]; |
| 2577 | |
| 2578 | [self hideFindBarWithAnimation:NO]; |
| 2579 | |
| 2580 | __weak BrowserViewController* weakSelf = self; |
| 2581 | void (^completion)(id<UIViewControllerTransitionCoordinatorContext>) = |
| 2582 | ^(id<UIViewControllerTransitionCoordinatorContext> context) { |
| 2583 | BrowserViewController* strongSelf = weakSelf; |
| 2584 | if (strongSelf) |
| 2585 | [strongSelf showFindBarWithAnimation:NO |
| 2586 | selectText:NO |
| 2587 | shouldFocus:isFocusedBeforeReshow]; |
| 2588 | }; |
| 2589 | |
| 2590 | BOOL enqueued = |
| 2591 | [coordinator animateAlongsideTransition:nil completion:completion]; |
| 2592 | if (!enqueued) { |
| 2593 | completion(nil); |
| 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | #pragma mark - Private Methods: Alerts |
| 2598 | |
| 2599 | - (void)showErrorAlertWithStringTitle:(NSString*)title |
| 2600 | message:(NSString*)message { |
| 2601 | // Dismiss current alert. |
| 2602 | [_alertCoordinator stop]; |
| 2603 | |
| 2604 | _alertCoordinator = [_dependencyFactory alertCoordinatorWithTitle:title |
| 2605 | message:message |
| 2606 | viewController:self]; |
| 2607 | [_alertCoordinator start]; |
| 2608 | } |
| 2609 | |
| 2610 | - (void)showSnackbar:(NSString*)text { |
| 2611 | MDCSnackbarMessage* message = [MDCSnackbarMessage messageWithText:text]; |
| 2612 | message.accessibilityLabel = text; |
| 2613 | message.duration = 2.0; |
| 2614 | message.category = kBrowserViewControllerSnackbarCategory; |
| 2615 | [self.dispatcher showSnackbarMessage:message]; |
| 2616 | } |
| 2617 | |
| 2618 | #pragma mark - Private Methods: Tap handling |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2619 | |
Mark Cogan | dfcdea7 | 2017-07-18 13:47:38 | [diff] [blame] | 2620 | - (void)setLastTapPoint:(OpenNewTabCommand*)command { |
Mark Cogan | e01ebce | 2017-07-12 19:31:03 | [diff] [blame] | 2621 | if (CGPointEqualToPoint(command.originPoint, CGPointZero)) { |
| 2622 | _lastTapPoint = CGPointZero; |
| 2623 | } else { |
| 2624 | _lastTapPoint = |
| 2625 | [self.view.window convertPoint:command.originPoint toView:self.view]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2626 | } |
Mark Cogan | e01ebce | 2017-07-12 19:31:03 | [diff] [blame] | 2627 | _lastTapTime = CACurrentMediaTime(); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2628 | } |
| 2629 | |
| 2630 | - (CGPoint)lastTapPoint { |
| 2631 | if (CACurrentMediaTime() - _lastTapTime < 1) { |
| 2632 | return _lastTapPoint; |
| 2633 | } |
| 2634 | return CGPointZero; |
| 2635 | } |
| 2636 | |
| 2637 | - (void)saveContentAreaTapLocation:(UIGestureRecognizer*)gestureRecognizer { |
| 2638 | UIView* view = gestureRecognizer.view; |
| 2639 | CGPoint viewCoordinate = [gestureRecognizer locationInView:view]; |
| 2640 | _lastTapPoint = |
| 2641 | [[view superview] convertPoint:viewCoordinate toView:self.view]; |
| 2642 | _lastTapTime = CACurrentMediaTime(); |
| 2643 | } |
| 2644 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 2645 | #pragma mark - Private Methods: Tab creation and selection |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2646 | |
| 2647 | // Called when either a tab finishes loading or when a tab with finished content |
| 2648 | // is added directly to the model via pre-rendering. |
| 2649 | - (void)tabLoadComplete:(Tab*)tab withSuccess:(BOOL)success { |
| 2650 | DCHECK(tab && ([_model indexOfTab:tab] != NSNotFound)); |
| 2651 | |
| 2652 | // Persist the session on a delay. |
| 2653 | [_model saveSessionImmediately:NO]; |
| 2654 | } |
| 2655 | |
| 2656 | - (Tab*)addSelectedTabWithURL:(const GURL&)url |
| 2657 | postData:(TemplateURLRef::PostContent*)postData |
| 2658 | transition:(ui::PageTransition)transition { |
| 2659 | return [self addSelectedTabWithURL:url |
| 2660 | postData:postData |
| 2661 | atIndex:[_model count] |
Olivier Robin | d508a563 | 2017-07-19 16:29:49 | [diff] [blame] | 2662 | transition:transition |
| 2663 | tabAddedCompletion:nil]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2664 | } |
| 2665 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2666 | - (Tab*)addSelectedTabWithURL:(const GURL&)URL |
| 2667 | postData:(TemplateURLRef::PostContent*)postData |
| 2668 | atIndex:(NSUInteger)position |
Olivier Robin | d508a563 | 2017-07-19 16:29:49 | [diff] [blame] | 2669 | transition:(ui::PageTransition)transition |
| 2670 | tabAddedCompletion:(ProceduralBlock)tabAddedCompletion { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2671 | if (position == NSNotFound) |
| 2672 | position = [_model count]; |
| 2673 | DCHECK(position <= [_model count]); |
| 2674 | |
| 2675 | web::NavigationManager::WebLoadParams params(URL); |
| 2676 | params.transition_type = transition; |
| 2677 | if (postData) { |
| 2678 | // Extract the content type and post params from |postData| and add them |
| 2679 | // to the load params. |
| 2680 | NSString* contentType = base::SysUTF8ToNSString(postData->first); |
| 2681 | NSData* data = [NSData dataWithBytes:(void*)postData->second.data() |
| 2682 | length:postData->second.length()]; |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 2683 | params.post_data.reset(data); |
| 2684 | params.extra_headers.reset(@{ @"Content-Type" : contentType }); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2685 | } |
Olivier Robin | d508a563 | 2017-07-19 16:29:49 | [diff] [blame] | 2686 | |
| 2687 | if (tabAddedCompletion) { |
| 2688 | if (self.foregroundTabWasAddedCompletionBlock) { |
| 2689 | ProceduralBlock oldForegroundTabWasAddedCompletionBlock = |
| 2690 | self.foregroundTabWasAddedCompletionBlock; |
| 2691 | self.foregroundTabWasAddedCompletionBlock = ^{ |
| 2692 | oldForegroundTabWasAddedCompletionBlock(); |
| 2693 | tabAddedCompletion(); |
| 2694 | }; |
| 2695 | } else { |
| 2696 | self.foregroundTabWasAddedCompletionBlock = tabAddedCompletion; |
| 2697 | } |
| 2698 | } |
| 2699 | |
sdefresne | a639591 | 2017-03-01 01:14:35 | [diff] [blame] | 2700 | Tab* tab = [_model insertTabWithLoadParams:params |
| 2701 | opener:nil |
| 2702 | openedByDOM:NO |
| 2703 | atIndex:position |
| 2704 | inBackground:NO]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2705 | return tab; |
| 2706 | } |
| 2707 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2708 | - (BOOL)isTabNativePage:(Tab*)tab { |
olivierrobin | 889af53f | 2017-03-01 14:56:32 | [diff] [blame] | 2709 | web::WebState* webState = tab.webState; |
| 2710 | if (!webState) |
| 2711 | return NO; |
liaoyuke | ea9f3ee6 | 2017-03-07 22:05:39 | [diff] [blame] | 2712 | web::NavigationItem* visibleItem = |
| 2713 | webState->GetNavigationManager()->GetVisibleItem(); |
olivierrobin | 889af53f | 2017-03-01 14:56:32 | [diff] [blame] | 2714 | if (!visibleItem) |
| 2715 | return NO; |
| 2716 | return web::GetWebClient()->IsAppSpecificURL(visibleItem->GetURL()); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2717 | } |
| 2718 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2719 | - (UIImageView*)pageOpenCloseAnimationView { |
| 2720 | CGRect frame = [_contentArea bounds]; |
| 2721 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 2722 | frame.size.height = frame.size.height - self.headerHeight; |
| 2723 | frame.origin.y = self.headerHeight; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2724 | |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 2725 | UIImageView* pageView = [[UIImageView alloc] initWithFrame:frame]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2726 | CGPoint center = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame)); |
| 2727 | pageView.center = center; |
| 2728 | |
| 2729 | pageView.backgroundColor = [UIColor whiteColor]; |
| 2730 | return pageView; |
| 2731 | } |
| 2732 | |
| 2733 | - (void)installDelegatesForTab:(Tab*)tab { |
sdefresne | 49cf286 | 2017-03-15 13:46:14 | [diff] [blame] | 2734 | // Unregistration happens when the Tab is removed from the TabModel. |
Sylvain Defresne | f5d2d95 | 2017-11-14 11:15:31 | [diff] [blame] | 2735 | DCHECK_NE(tab.webState->GetDelegate(), _webStateDelegate.get()); |
| 2736 | |
| 2737 | // There should be no pre-rendered Tabs in TabModel. |
| 2738 | PrerenderService* prerenderService = |
| 2739 | PrerenderServiceFactory::GetForBrowserState(_browserState); |
| 2740 | DCHECK(!prerenderService || |
| 2741 | !prerenderService->IsWebStatePrerendered(tab.webState)); |
edchin | cd32fdf | 2017-10-25 12:45:45 | [diff] [blame] | 2742 | |
Sylvain Defresne | 17b8aa4 | 2017-12-21 16:17:17 | [diff] [blame] | 2743 | SnapshotTabHelper::FromWebState(tab.webState)->SetDelegate(tab); |
| 2744 | |
edchin | cd32fdf | 2017-10-25 12:45:45 | [diff] [blame] | 2745 | // TODO(crbug.com/777557): do not pass the dispatcher to PasswordTabHelper. |
| 2746 | if (PasswordTabHelper* passwordTabHelper = |
| 2747 | PasswordTabHelper::FromWebState(tab.webState)) { |
edchin | 6941b849 | 2017-12-01 18:59:59 | [diff] [blame] | 2748 | passwordTabHelper->SetBaseViewController(self); |
edchin | cd32fdf | 2017-10-25 12:45:45 | [diff] [blame] | 2749 | passwordTabHelper->SetDispatcher(self.dispatcher); |
| 2750 | passwordTabHelper->SetPasswordControllerDelegate(self); |
| 2751 | } |
| 2752 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2753 | tab.dialogDelegate = self; |
| 2754 | tab.snapshotOverlayProvider = self; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2755 | tab.passKitDialogProvider = self; |
Kurt Horimoto | a5a922a | 2017-11-07 00:21:09 | [diff] [blame] | 2756 | if (!base::FeatureList::IsEnabled(fullscreen::features::kNewFullscreen)) { |
Kurt Horimoto | 62e97c7 | 2017-11-03 19:51:47 | [diff] [blame] | 2757 | tab.legacyFullscreenControllerDelegate = self; |
Kurt Horimoto | 80384062 | 2017-10-28 01:20:37 | [diff] [blame] | 2758 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2759 | if (!IsIPadIdiom()) { |
| 2760 | tab.overscrollActionsControllerDelegate = self; |
| 2761 | } |
olivierrobin | 9ce77b8 | 2017-01-12 17:29:19 | [diff] [blame] | 2762 | tab.tabHeadersDelegate = self; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2763 | tab.tabSnapshottingDelegate = self; |
| 2764 | // Install the proper CRWWebController delegates. |
| 2765 | tab.webController.nativeProvider = self; |
| 2766 | tab.webController.swipeRecognizerProvider = self.sideSwipeController; |
pkl | d6e73e5 | 2017-03-08 15:56:51 | [diff] [blame] | 2767 | // BrowserViewController presents SKStoreKitViewController on behalf of a |
| 2768 | // tab. |
| 2769 | StoreKitTabHelper* tabHelper = StoreKitTabHelper::FromWebState(tab.webState); |
| 2770 | if (tabHelper) |
| 2771 | tabHelper->SetLauncher(self); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2772 | tab.webState->SetDelegate(_webStateDelegate.get()); |
sczs | 6ae47ad | 2017-09-06 17:26:53 | [diff] [blame] | 2773 | // BrowserViewController owns the coordinator that displays the Sad Tab. |
sczs | dfef35b | 2017-10-27 01:39:29 | [diff] [blame] | 2774 | if (!SadTabTabHelper::FromWebState(tab.webState)) { |
sczs | 6ae47ad | 2017-09-06 17:26:53 | [diff] [blame] | 2775 | SadTabTabHelper::CreateForWebState(tab.webState, _sadTabCoordinator); |
sczs | dfef35b | 2017-10-27 01:39:29 | [diff] [blame] | 2776 | } |
Sylvain Defresne | cacc3a5 | 2017-09-12 13:51:04 | [diff] [blame] | 2777 | PrintTabHelper::CreateForWebState(tab.webState, self); |
Eugene But | 35ded55 | 2017-09-13 23:31:59 | [diff] [blame] | 2778 | RepostFormTabHelper::CreateForWebState(tab.webState, self); |
Gregory Chatzinoff | 5f9f7f0 | 2017-09-19 02:04:57 | [diff] [blame] | 2779 | NetExportTabHelper::CreateForWebState(tab.webState, self); |
Mike Dougherty | 4620cf8e | 2017-10-31 23:37:09 | [diff] [blame] | 2780 | CaptivePortalDetectorTabHelper::CreateForWebState(tab.webState, self); |
Eugene But | 49a7c57 | 2017-12-11 20:54:15 | [diff] [blame] | 2781 | PassKitTabHelper::CreateForWebState(tab.webState, _passKitCoordinator); |
edchin | cd32fdf | 2017-10-25 12:45:45 | [diff] [blame] | 2782 | |
Mark Cogan | ca30df6 | 2017-11-20 14:29:11 | [diff] [blame] | 2783 | // The language detection helper accepts a callback from the translate |
| 2784 | // client, so must be created after it. |
| 2785 | // This will explode if the webState doesn't have a JS injection manager |
| 2786 | // (this only comes up in unit tests), so check for that and bypass the |
| 2787 | // init of the translation helpers if needed. |
| 2788 | // TODO(crbug.com/785238): Remove the need for this check. |
| 2789 | if (tab.webState->GetJSInjectionReceiver()) { |
| 2790 | ChromeIOSTranslateClient::CreateForWebState(tab.webState, |
| 2791 | _languageSelectionCoordinator); |
| 2792 | language::IOSLanguageDetectionTabHelper::CreateForWebState( |
| 2793 | tab.webState, |
| 2794 | ChromeIOSTranslateClient::FromWebState(tab.webState) |
| 2795 | ->GetTranslateDriver() |
| 2796 | ->CreateLanguageDetectionCallback(), |
| 2797 | UrlLanguageHistogramFactory::GetForBrowserState(self.browserState)); |
| 2798 | } |
| 2799 | |
edchin | cd32fdf | 2017-10-25 12:45:45 | [diff] [blame] | 2800 | if (AccountConsistencyService* accountConsistencyService = |
| 2801 | ios::AccountConsistencyServiceFactory::GetForBrowserState( |
| 2802 | self.browserState)) { |
| 2803 | accountConsistencyService->SetWebStateHandler(tab.webState, self); |
Tomasz Garbus | b844e99 | 2017-09-29 12:44:55 | [diff] [blame] | 2804 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2805 | } |
| 2806 | |
sdefresne | 49cf286 | 2017-03-15 13:46:14 | [diff] [blame] | 2807 | - (void)uninstallDelegatesForTab:(Tab*)tab { |
edchin | 5b3d107 | 2017-10-24 13:43:11 | [diff] [blame] | 2808 | DCHECK_EQ(tab.webState->GetDelegate(), _webStateDelegate.get()); |
edchin | cd32fdf | 2017-10-25 12:45:45 | [diff] [blame] | 2809 | |
| 2810 | // TODO(crbug.com/777557): do not pass the dispatcher to PasswordTabHelper. |
| 2811 | if (PasswordTabHelper* passwordTabHelper = |
Sylvain Defresne | 17b8aa4 | 2017-12-21 16:17:17 | [diff] [blame] | 2812 | PasswordTabHelper::FromWebState(tab.webState)) { |
edchin | cd32fdf | 2017-10-25 12:45:45 | [diff] [blame] | 2813 | passwordTabHelper->SetDispatcher(nil); |
Sylvain Defresne | 17b8aa4 | 2017-12-21 16:17:17 | [diff] [blame] | 2814 | } |
edchin | cd32fdf | 2017-10-25 12:45:45 | [diff] [blame] | 2815 | |
sdefresne | 49cf286 | 2017-03-15 13:46:14 | [diff] [blame] | 2816 | tab.dialogDelegate = nil; |
| 2817 | tab.snapshotOverlayProvider = nil; |
| 2818 | tab.passKitDialogProvider = nil; |
Kurt Horimoto | a5a922a | 2017-11-07 00:21:09 | [diff] [blame] | 2819 | if (!base::FeatureList::IsEnabled(fullscreen::features::kNewFullscreen)) { |
Kurt Horimoto | 62e97c7 | 2017-11-03 19:51:47 | [diff] [blame] | 2820 | tab.legacyFullscreenControllerDelegate = nil; |
Kurt Horimoto | 80384062 | 2017-10-28 01:20:37 | [diff] [blame] | 2821 | } |
sdefresne | 49cf286 | 2017-03-15 13:46:14 | [diff] [blame] | 2822 | if (!IsIPadIdiom()) { |
| 2823 | tab.overscrollActionsControllerDelegate = nil; |
| 2824 | } |
| 2825 | tab.tabHeadersDelegate = nil; |
| 2826 | tab.tabSnapshottingDelegate = nil; |
| 2827 | tab.webController.nativeProvider = nil; |
| 2828 | tab.webController.swipeRecognizerProvider = nil; |
| 2829 | StoreKitTabHelper* tabHelper = StoreKitTabHelper::FromWebState(tab.webState); |
| 2830 | if (tabHelper) |
| 2831 | tabHelper->SetLauncher(nil); |
| 2832 | tab.webState->SetDelegate(nullptr); |
edchin | cd32fdf | 2017-10-25 12:45:45 | [diff] [blame] | 2833 | if (AccountConsistencyService* accountConsistencyService = |
| 2834 | ios::AccountConsistencyServiceFactory::GetForBrowserState( |
| 2835 | self.browserState)) { |
| 2836 | accountConsistencyService->RemoveWebStateHandler(tab.webState); |
| 2837 | } |
Sylvain Defresne | 17b8aa4 | 2017-12-21 16:17:17 | [diff] [blame] | 2838 | |
| 2839 | SnapshotTabHelper::FromWebState(tab.webState)->SetDelegate(nil); |
sdefresne | 49cf286 | 2017-03-15 13:46:14 | [diff] [blame] | 2840 | } |
| 2841 | |
Gauthier Ambard | 6439690 | 2017-12-08 10:14:58 | [diff] [blame] | 2842 | - (void)tabSelected:(Tab*)tab notifyToolbar:(BOOL)notifyToolbar { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2843 | DCHECK(tab); |
| 2844 | |
| 2845 | // Ignore changes while the tab stack view is visible (or while suspended). |
| 2846 | // The display will be refreshed when this view becomes active again. |
| 2847 | if (!self.visible || ![_model webUsageEnabled]) |
| 2848 | return; |
| 2849 | |
Gauthier Ambard | 6439690 | 2017-12-08 10:14:58 | [diff] [blame] | 2850 | [self displayTab:tab isNewSelection:notifyToolbar]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2851 | |
kkhorimoto | a44349c1 | 2017-04-12 23:02:12 | [diff] [blame] | 2852 | if (_expectingForegroundTab && !self.inNewTabAnimation) { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2853 | // Now that the new tab has been displayed, return to normal. Rather than |
| 2854 | // keep a reference to the previous tab, just turn off preview mode for all |
| 2855 | // tabs (since doing so is a no-op for the tabs that don't have it set). |
| 2856 | _expectingForegroundTab = NO; |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 2857 | for (Tab* tab in _model) { |
Sylvain Defresne | 44835133 | 2017-12-27 10:38:36 | [diff] [blame] | 2858 | PagePlaceholderTabHelper::FromWebState(tab.webState) |
| 2859 | ->CancelPlaceholderForNextNavigation(); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2860 | } |
| 2861 | } |
| 2862 | } |
| 2863 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 2864 | - (id)nativeControllerForTab:(Tab*)tab { |
| 2865 | id nativeController = tab.webController.nativeController; |
| 2866 | return nativeController ? nativeController : _temporaryNativeController; |
| 2867 | } |
| 2868 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 2869 | #pragma mark - Private Methods: Voice Search |
| 2870 | |
| 2871 | - (void)ensureVoiceSearchControllerCreated { |
| 2872 | if (!_voiceSearchController) { |
| 2873 | VoiceSearchProvider* provider = |
| 2874 | ios::GetChromeBrowserProvider()->GetVoiceSearchProvider(); |
| 2875 | if (provider) { |
| 2876 | _voiceSearchController = |
| 2877 | provider->CreateVoiceSearchController(_browserState); |
| 2878 | _voiceSearchController->SetDelegate( |
| 2879 | [_toolbarCoordinator voiceSearchDelegate]); |
| 2880 | } |
| 2881 | } |
| 2882 | } |
| 2883 | |
| 2884 | - (void)ensureVoiceSearchBarCreated { |
| 2885 | if (_voiceSearchBar) |
| 2886 | return; |
| 2887 | |
| 2888 | CGFloat width = CGRectGetWidth([[self view] bounds]); |
| 2889 | CGFloat y = CGRectGetHeight([[self view] bounds]) - kVoiceSearchBarHeight; |
| 2890 | CGRect frame = CGRectMake(0.0, y, width, kVoiceSearchBarHeight); |
| 2891 | _voiceSearchBar = ios::GetChromeBrowserProvider() |
| 2892 | ->GetVoiceSearchProvider() |
| 2893 | ->BuildVoiceSearchBar(frame, self.dispatcher); |
| 2894 | [_voiceSearchBar setVoiceSearchBarDelegate:self]; |
| 2895 | [_voiceSearchBar setHidden:YES]; |
| 2896 | [_voiceSearchBar setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | |
| 2897 | UIViewAutoresizingFlexibleWidth]; |
| 2898 | [self.view insertSubview:_voiceSearchBar |
| 2899 | belowSubview:_infoBarContainer->view()]; |
| 2900 | } |
| 2901 | |
| 2902 | - (void)updateVoiceSearchBarVisibilityAnimated:(BOOL)animated { |
| 2903 | // Voice search bar exists and is shown/hidden. |
| 2904 | BOOL show = self.shouldShowVoiceSearchBar; |
| 2905 | if (_voiceSearchBar && _voiceSearchBar.hidden != show) |
| 2906 | return; |
| 2907 | |
| 2908 | // Voice search bar doesn't exist and thus is not visible. |
| 2909 | if (!_voiceSearchBar && !show) |
| 2910 | return; |
| 2911 | |
| 2912 | if (animated) |
| 2913 | [_voiceSearchBar animateToBecomeVisible:show]; |
| 2914 | else |
| 2915 | _voiceSearchBar.hidden = !show; |
| 2916 | } |
| 2917 | |
| 2918 | #pragma mark - Private Methods: Reading List |
| 2919 | |
| 2920 | - (void)addToReadingListURL:(const GURL&)URL title:(NSString*)title { |
| 2921 | base::RecordAction(UserMetricsAction("MobileReadingListAdd")); |
| 2922 | |
| 2923 | ReadingListModel* readingModel = |
| 2924 | ReadingListModelFactory::GetForBrowserState(_browserState); |
| 2925 | readingModel->AddEntry(URL, base::SysNSStringToUTF8(title), |
| 2926 | reading_list::ADDED_VIA_CURRENT_APP); |
| 2927 | |
| 2928 | TriggerHapticFeedbackForNotification(UINotificationFeedbackTypeSuccess); |
| 2929 | [self showSnackbar:l10n_util::GetNSString( |
| 2930 | IDS_IOS_READING_LIST_SNACKBAR_MESSAGE)]; |
| 2931 | } |
| 2932 | |
| 2933 | #pragma mark - ** Protocol Implementations and Helpers ** |
| 2934 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2935 | #pragma mark - SnapshotOverlayProvider methods |
| 2936 | |
| 2937 | - (NSArray*)snapshotOverlaysForTab:(Tab*)tab { |
| 2938 | NSMutableArray* overlays = [NSMutableArray array]; |
| 2939 | if (![_model webUsageEnabled]) { |
| 2940 | return overlays; |
| 2941 | } |
| 2942 | UIView* voiceSearchView = [self voiceSearchOverlayViewForTab:tab]; |
| 2943 | if (voiceSearchView) { |
| 2944 | CGFloat voiceSearchYOffset = [self voiceSearchOverlayYOffsetForTab:tab]; |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 2945 | SnapshotOverlay* voiceSearchOverlay = |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2946 | [[SnapshotOverlay alloc] initWithView:voiceSearchView |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 2947 | yOffset:voiceSearchYOffset]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2948 | [overlays addObject:voiceSearchOverlay]; |
| 2949 | } |
| 2950 | UIView* infoBarView = [self infoBarOverlayViewForTab:tab]; |
| 2951 | if (infoBarView) { |
| 2952 | CGFloat infoBarYOffset = [self infoBarOverlayYOffsetForTab:tab]; |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 2953 | SnapshotOverlay* infoBarOverlay = |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2954 | [[SnapshotOverlay alloc] initWithView:infoBarView |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 2955 | yOffset:infoBarYOffset]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2956 | [overlays addObject:infoBarOverlay]; |
| 2957 | } |
| 2958 | return overlays; |
| 2959 | } |
| 2960 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 2961 | #pragma mark - SnapshotOverlayProvider helpers |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2962 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 2963 | // Provides a view that encompasses currently displayed infobar(s) or nil |
| 2964 | // if no infobar is presented. |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2965 | - (UIView*)infoBarOverlayViewForTab:(Tab*)tab { |
| 2966 | if (IsIPadIdiom()) { |
| 2967 | // Not using overlays on iPad because the content is pushed down by |
| 2968 | // infobar and the transition between snapshot and fresh page can |
| 2969 | // cause both snapshot and real infobars to appear at the same time. |
| 2970 | return nil; |
| 2971 | } |
| 2972 | Tab* currentTab = [_model currentTab]; |
Rohit Rao | af46af9 | 2017-08-10 12:52:30 | [diff] [blame] | 2973 | if (currentTab && tab == currentTab) { |
| 2974 | DCHECK(currentTab.webState); |
| 2975 | infobars::InfoBarManager* infoBarManager = |
| 2976 | InfoBarManagerImpl::FromWebState(currentTab.webState); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2977 | if (infoBarManager->infobar_count() > 0) { |
| 2978 | DCHECK(_infoBarContainer); |
| 2979 | return _infoBarContainer->view(); |
| 2980 | } |
| 2981 | } |
| 2982 | return nil; |
| 2983 | } |
| 2984 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 2985 | // Returns a vertical infobar offset relative to the tab content. |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2986 | - (CGFloat)infoBarOverlayYOffsetForTab:(Tab*)tab { |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 2987 | if (tab != [_model currentTab] || !_infoBarContainer) { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 2988 | // There is no UI representation for non-current tabs or there is |
| 2989 | // no _infoBarContainer instantiated yet. |
| 2990 | // Return offset outside of tab. |
| 2991 | return CGRectGetMaxY(self.view.frame); |
| 2992 | } else if (IsIPadIdiom()) { |
| 2993 | // The infobars on iPad are display at the top of a tab. |
| 2994 | return CGRectGetMinY([[_model currentTab].webController visibleFrame]); |
| 2995 | } else { |
| 2996 | // The infobars on iPhone are displayed at the bottom of a tab. |
| 2997 | CGRect visibleFrame = [[_model currentTab].webController visibleFrame]; |
| 2998 | return CGRectGetMaxY(visibleFrame) - |
| 2999 | CGRectGetHeight(_infoBarContainer->view().frame); |
| 3000 | } |
| 3001 | } |
| 3002 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 3003 | // Provides a view that encompasses the voice search bar if it's displayed or |
| 3004 | // nil if the voice search bar isn't displayed. |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3005 | - (UIView*)voiceSearchOverlayViewForTab:(Tab*)tab { |
| 3006 | Tab* currentTab = [_model currentTab]; |
| 3007 | if (tab && tab == currentTab && tab.isVoiceSearchResultsTab && |
| 3008 | _voiceSearchBar && ![_voiceSearchBar isHidden]) { |
| 3009 | return _voiceSearchBar; |
| 3010 | } |
| 3011 | return nil; |
| 3012 | } |
| 3013 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 3014 | // Returns a vertical voice search bar offset relative to the tab content. |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3015 | - (CGFloat)voiceSearchOverlayYOffsetForTab:(Tab*)tab { |
| 3016 | if (tab != [_model currentTab] || [_voiceSearchBar isHidden]) { |
| 3017 | // There is no UI representation for non-current tabs or there is |
| 3018 | // no visible voice search. Return offset outside of tab. |
| 3019 | return CGRectGetMaxY(self.view.frame); |
| 3020 | } else { |
| 3021 | // The voice search bar on iPhone is displayed at the bottom of a tab. |
| 3022 | CGRect visibleFrame = [[_model currentTab].webController visibleFrame]; |
| 3023 | return CGRectGetMaxY(visibleFrame) - kVoiceSearchBarHeight; |
| 3024 | } |
| 3025 | } |
| 3026 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3027 | #pragma mark - PassKitDialogProvider methods |
| 3028 | |
| 3029 | - (void)presentPassKitDialog:(NSData*)data { |
| 3030 | NSError* error = nil; |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 3031 | PKPass* pass = nil; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3032 | if (data) |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 3033 | pass = [[PKPass alloc] initWithData:data error:&error]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3034 | if (error || !data) { |
| 3035 | if ([_model currentTab]) { |
Rohit Rao | af46af9 | 2017-08-10 12:52:30 | [diff] [blame] | 3036 | DCHECK(_model.currentTab.webState); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3037 | infobars::InfoBarManager* infoBarManager = |
Rohit Rao | af46af9 | 2017-08-10 12:52:30 | [diff] [blame] | 3038 | InfoBarManagerImpl::FromWebState(_model.currentTab.webState); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3039 | // TODO(crbug.com/227994): Infobar cleanup (infoBarManager should never be |
| 3040 | // NULL, replace if with DCHECK). |
| 3041 | if (infoBarManager) |
| 3042 | [_dependencyFactory showPassKitErrorInfoBarForManager:infoBarManager]; |
| 3043 | } |
| 3044 | } else { |
| 3045 | PKAddPassesViewController* passKitViewController = |
| 3046 | [_dependencyFactory newPassKitViewControllerForPass:pass]; |
| 3047 | if (passKitViewController) { |
| 3048 | [self presentViewController:passKitViewController |
| 3049 | animated:YES |
| 3050 | completion:^{ |
| 3051 | }]; |
| 3052 | } |
| 3053 | } |
| 3054 | } |
| 3055 | |
Tomasz Garbus | b844e99 | 2017-09-29 12:44:55 | [diff] [blame] | 3056 | #pragma mark - PasswordControllerDelegate methods |
| 3057 | |
| 3058 | - (BOOL)displaySignInNotification:(UIViewController*)viewController |
| 3059 | fromTabId:(NSString*)tabId { |
| 3060 | // Check if the call comes from currently visible tab. |
| 3061 | if ([tabId isEqual:[_model currentTab].tabId]) { |
| 3062 | [self addChildViewController:viewController]; |
| 3063 | [self.view addSubview:viewController.view]; |
| 3064 | [viewController didMoveToParentViewController:self]; |
| 3065 | return YES; |
| 3066 | } else { |
| 3067 | return NO; |
| 3068 | } |
| 3069 | } |
| 3070 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3071 | #pragma mark - CRWWebStateDelegate methods. |
| 3072 | |
eugenebut | 75a06fa7 | 2017-01-09 17:09:55 | [diff] [blame] | 3073 | - (web::WebState*)webState:(web::WebState*)webState |
eugenebut | 275f589 | 2017-03-09 22:20:51 | [diff] [blame] | 3074 | createNewWebStateForURL:(const GURL&)URL |
| 3075 | openerURL:(const GURL&)openerURL |
| 3076 | initiatedByUser:(BOOL)initiatedByUser { |
| 3077 | // Check if requested web state is a popup and block it if necessary. |
| 3078 | if (!initiatedByUser) { |
| 3079 | auto* helper = BlockedPopupTabHelper::FromWebState(webState); |
| 3080 | if (helper->ShouldBlockPopup(openerURL)) { |
kkhorimoto | 069cf2c | 2017-05-09 22:00:10 | [diff] [blame] | 3081 | // It's possible for a page to inject a popup into a window created via |
| 3082 | // window.open before its initial load is committed. Rather than relying |
| 3083 | // on the last committed or pending NavigationItem's referrer policy, just |
| 3084 | // use ReferrerPolicyDefault. |
| 3085 | // TODO(crbug.com/719993): Update this to a more appropriate referrer |
| 3086 | // policy once referrer policies are correctly recorded in |
| 3087 | // NavigationItems. |
| 3088 | web::Referrer referrer(openerURL, web::ReferrerPolicyDefault); |
eugenebut | 275f589 | 2017-03-09 22:20:51 | [diff] [blame] | 3089 | helper->HandlePopup(URL, referrer); |
| 3090 | return nil; |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | // Requested web state should not be blocked from opening. |
| 3095 | Tab* currentTab = LegacyTabHelper::GetTabForWebState(webState); |
Sylvain Defresne | 17b8aa4 | 2017-12-21 16:17:17 | [diff] [blame] | 3096 | SnapshotTabHelper::FromWebState(currentTab.webState) |
| 3097 | ->UpdateSnapshot(/*with_overlays=*/true, /*visible_frame_only=*/true); |
eugenebut | 275f589 | 2017-03-09 22:20:51 | [diff] [blame] | 3098 | |
| 3099 | // Tabs open by DOM are always renderer initiated. |
| 3100 | web::NavigationManager::WebLoadParams params(GURL{}); |
| 3101 | params.transition_type = ui::PAGE_TRANSITION_LINK; |
| 3102 | params.is_renderer_initiated = true; |
| 3103 | Tab* childTab = [[self tabModel] |
| 3104 | insertTabWithLoadParams:params |
| 3105 | opener:currentTab |
| 3106 | openedByDOM:YES |
| 3107 | atIndex:TabModelConstants::kTabPositionAutomatically |
| 3108 | inBackground:NO]; |
| 3109 | return childTab.webState; |
| 3110 | } |
| 3111 | |
eugenebut | b46b212 | 2017-03-14 02:43:26 | [diff] [blame] | 3112 | - (void)closeWebState:(web::WebState*)webState { |
| 3113 | // Only allow a web page to close itself if it was opened by DOM, or if there |
| 3114 | // are no navigation items. |
| 3115 | Tab* tab = LegacyTabHelper::GetTabForWebState(webState); |
kkhorimoto | a8ee9dec | 2017-03-21 01:53:58 | [diff] [blame] | 3116 | DCHECK(webState->HasOpener() || ![tab navigationManager]->GetItemCount()); |
eugenebut | b46b212 | 2017-03-14 02:43:26 | [diff] [blame] | 3117 | |
| 3118 | if (![self tabModel]) |
| 3119 | return; |
| 3120 | |
| 3121 | NSUInteger index = [[self tabModel] indexOfTab:tab]; |
| 3122 | if (index != NSNotFound) |
| 3123 | [[self tabModel] closeTabAtIndex:index]; |
| 3124 | } |
| 3125 | |
eugenebut | 275f589 | 2017-03-09 22:20:51 | [diff] [blame] | 3126 | - (web::WebState*)webState:(web::WebState*)webState |
eugenebut | 75a06fa7 | 2017-01-09 17:09:55 | [diff] [blame] | 3127 | openURLWithParams:(const web::WebState::OpenURLParams&)params { |
| 3128 | switch (params.disposition) { |
| 3129 | case WindowOpenDisposition::NEW_FOREGROUND_TAB: |
| 3130 | case WindowOpenDisposition::NEW_BACKGROUND_TAB: { |
| 3131 | Tab* tab = [[self tabModel] |
sdefresne | a639591 | 2017-03-01 01:14:35 | [diff] [blame] | 3132 | insertTabWithURL:params.url |
| 3133 | referrer:params.referrer |
| 3134 | transition:params.transition |
| 3135 | opener:LegacyTabHelper::GetTabForWebState(webState) |
| 3136 | openedByDOM:NO |
| 3137 | atIndex:TabModelConstants::kTabPositionAutomatically |
| 3138 | inBackground:(params.disposition == |
| 3139 | WindowOpenDisposition::NEW_BACKGROUND_TAB)]; |
eugenebut | 75a06fa7 | 2017-01-09 17:09:55 | [diff] [blame] | 3140 | return tab.webState; |
| 3141 | } |
| 3142 | case WindowOpenDisposition::CURRENT_TAB: { |
| 3143 | web::NavigationManager::WebLoadParams loadParams(params.url); |
| 3144 | loadParams.referrer = params.referrer; |
| 3145 | loadParams.transition_type = params.transition; |
| 3146 | loadParams.is_renderer_initiated = params.is_renderer_initiated; |
| 3147 | webState->GetNavigationManager()->LoadURLWithParams(loadParams); |
| 3148 | return webState; |
| 3149 | } |
eugenebut | d0984e8 | 2017-02-22 23:47:51 | [diff] [blame] | 3150 | case WindowOpenDisposition::NEW_POPUP: { |
| 3151 | Tab* tab = [[self tabModel] |
sdefresne | a639591 | 2017-03-01 01:14:35 | [diff] [blame] | 3152 | insertTabWithURL:params.url |
| 3153 | referrer:params.referrer |
| 3154 | transition:params.transition |
| 3155 | opener:LegacyTabHelper::GetTabForWebState(webState) |
| 3156 | openedByDOM:YES |
| 3157 | atIndex:TabModelConstants::kTabPositionAutomatically |
| 3158 | inBackground:NO]; |
eugenebut | d0984e8 | 2017-02-22 23:47:51 | [diff] [blame] | 3159 | return tab.webState; |
| 3160 | } |
eugenebut | 75a06fa7 | 2017-01-09 17:09:55 | [diff] [blame] | 3161 | default: |
| 3162 | NOTIMPLEMENTED(); |
| 3163 | return nullptr; |
| 3164 | }; |
| 3165 | } |
| 3166 | |
Mike Dougherty | 4e6b3a3 | 2017-08-23 18:49:21 | [diff] [blame] | 3167 | - (void)webState:(web::WebState*)webState |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3168 | handleContextMenu:(const web::ContextMenuParams&)params { |
| 3169 | // Prevent context menu from displaying for a tab which is no longer the |
| 3170 | // current one. |
| 3171 | if (webState != [_model currentTab].webState) { |
Mike Dougherty | 4e6b3a3 | 2017-08-23 18:49:21 | [diff] [blame] | 3172 | return; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | // No custom context menu if no valid url is available in |params|. |
| 3176 | if (!params.link_url.is_valid() && !params.src_url.is_valid()) { |
Mike Dougherty | 4e6b3a3 | 2017-08-23 18:49:21 | [diff] [blame] | 3177 | return; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3178 | } |
| 3179 | |
| 3180 | DCHECK(_browserState); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3181 | |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 3182 | _contextMenuCoordinator = |
| 3183 | [[ContextMenuCoordinator alloc] initWithBaseViewController:self |
| 3184 | params:params]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3185 | |
| 3186 | NSString* title = nil; |
| 3187 | ProceduralBlock action = nil; |
| 3188 | |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 3189 | __weak BrowserViewController* weakSelf = self; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3190 | GURL link = params.link_url; |
| 3191 | bool isLink = link.is_valid(); |
| 3192 | GURL imageUrl = params.src_url; |
| 3193 | bool isImage = imageUrl.is_valid(); |
Sylvain Defresne | e7f2c8a | 2017-10-17 02:39:19 | [diff] [blame] | 3194 | const GURL& lastCommittedURL = webState->GetLastCommittedURL(); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3195 | |
| 3196 | if (isLink) { |
| 3197 | if (link.SchemeIs(url::kJavaScriptScheme)) { |
| 3198 | // Open |
| 3199 | title = l10n_util::GetNSStringWithFixup(IDS_IOS_CONTENT_CONTEXT_OPEN); |
| 3200 | action = ^{ |
| 3201 | Record(ACTION_OPEN_JAVASCRIPT, isImage, isLink); |
| 3202 | [weakSelf openJavascript:base::SysUTF8ToNSString(link.GetContent())]; |
| 3203 | }; |
| 3204 | [_contextMenuCoordinator addItemWithTitle:title action:action]; |
| 3205 | } |
| 3206 | |
| 3207 | if (web::UrlHasWebScheme(link)) { |
Sylvain Defresne | e7f2c8a | 2017-10-17 02:39:19 | [diff] [blame] | 3208 | web::Referrer referrer(lastCommittedURL, params.referrer_policy); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3209 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3210 | // Open in New Tab. |
| 3211 | title = l10n_util::GetNSStringWithFixup( |
| 3212 | IDS_IOS_CONTENT_CONTEXT_OPENLINKNEWTAB); |
| 3213 | action = ^{ |
| 3214 | Record(ACTION_OPEN_IN_NEW_TAB, isImage, isLink); |
Cooper Knaak | 9ae6b4f4a | 2017-07-25 18:56:00 | [diff] [blame] | 3215 | // The "New Tab" item in the context menu opens a new tab in the current |
| 3216 | // browser state. |isOffTheRecord| indicates whether or not the current |
| 3217 | // browser state is incognito. |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3218 | [weakSelf webPageOrderedOpen:link |
| 3219 | referrer:referrer |
Cooper Knaak | 9ae6b4f4a | 2017-07-25 18:56:00 | [diff] [blame] | 3220 | inIncognito:weakSelf.isOffTheRecord |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3221 | inBackground:YES |
| 3222 | appendTo:kCurrentTab]; |
| 3223 | }; |
| 3224 | [_contextMenuCoordinator addItemWithTitle:title action:action]; |
| 3225 | if (!_isOffTheRecord) { |
| 3226 | // Open in Incognito Tab. |
| 3227 | title = l10n_util::GetNSStringWithFixup( |
| 3228 | IDS_IOS_CONTENT_CONTEXT_OPENLINKNEWINCOGNITOTAB); |
| 3229 | action = ^{ |
| 3230 | Record(ACTION_OPEN_IN_INCOGNITO_TAB, isImage, isLink); |
| 3231 | [weakSelf webPageOrderedOpen:link |
| 3232 | referrer:referrer |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3233 | inIncognito:YES |
| 3234 | inBackground:NO |
| 3235 | appendTo:kCurrentTab]; |
| 3236 | }; |
| 3237 | [_contextMenuCoordinator addItemWithTitle:title action:action]; |
| 3238 | } |
olivierrobin | 51d4cf4 | 2017-01-17 13:32:35 | [diff] [blame] | 3239 | } |
gambard | 65d6915 | 2017-03-23 17:44:22 | [diff] [blame] | 3240 | if (link.SchemeIsHTTPOrHTTPS()) { |
olivierrobin | 51d4cf4 | 2017-01-17 13:32:35 | [diff] [blame] | 3241 | NSString* innerText = params.link_text; |
| 3242 | if ([innerText length] > 0) { |
| 3243 | // Add to reading list. |
| 3244 | title = l10n_util::GetNSStringWithFixup( |
| 3245 | IDS_IOS_CONTENT_CONTEXT_ADDTOREADINGLIST); |
| 3246 | action = ^{ |
| 3247 | Record(ACTION_READ_LATER, isImage, isLink); |
| 3248 | [weakSelf addToReadingListURL:link title:innerText]; |
| 3249 | }; |
| 3250 | [_contextMenuCoordinator addItemWithTitle:title action:action]; |
gambard | 5fd40349 | 2017-01-17 09:17:53 | [diff] [blame] | 3251 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3252 | } |
| 3253 | // Copy Link. |
| 3254 | title = l10n_util::GetNSStringWithFixup(IDS_IOS_CONTENT_CONTEXT_COPY); |
| 3255 | action = ^{ |
| 3256 | Record(ACTION_COPY_LINK_ADDRESS, isImage, isLink); |
gambard | 6a13836 | 2017-02-06 17:19:28 | [diff] [blame] | 3257 | StoreURLInPasteboard(link); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3258 | }; |
| 3259 | [_contextMenuCoordinator addItemWithTitle:title action:action]; |
| 3260 | } |
| 3261 | if (isImage) { |
Sylvain Defresne | e7f2c8a | 2017-10-17 02:39:19 | [diff] [blame] | 3262 | web::Referrer referrer(lastCommittedURL, params.referrer_policy); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3263 | // Save Image. |
gambard | 98b4ddf | 2017-04-18 07:14:05 | [diff] [blame] | 3264 | title = l10n_util::GetNSStringWithFixup(IDS_IOS_CONTENT_CONTEXT_SAVEIMAGE); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3265 | action = ^{ |
| 3266 | Record(ACTION_SAVE_IMAGE, isImage, isLink); |
| 3267 | [weakSelf saveImageAtURL:imageUrl referrer:referrer]; |
| 3268 | }; |
| 3269 | [_contextMenuCoordinator addItemWithTitle:title action:action]; |
| 3270 | // Open Image. |
| 3271 | title = l10n_util::GetNSStringWithFixup(IDS_IOS_CONTENT_CONTEXT_OPENIMAGE); |
| 3272 | action = ^{ |
| 3273 | Record(ACTION_OPEN_IMAGE, isImage, isLink); |
| 3274 | [weakSelf loadURL:imageUrl |
| 3275 | referrer:referrer |
| 3276 | transition:ui::PAGE_TRANSITION_LINK |
| 3277 | rendererInitiated:YES]; |
| 3278 | }; |
| 3279 | [_contextMenuCoordinator addItemWithTitle:title action:action]; |
| 3280 | // Open Image In New Tab. |
| 3281 | title = l10n_util::GetNSStringWithFixup( |
| 3282 | IDS_IOS_CONTENT_CONTEXT_OPENIMAGENEWTAB); |
| 3283 | action = ^{ |
| 3284 | Record(ACTION_OPEN_IMAGE_IN_NEW_TAB, isImage, isLink); |
| 3285 | [weakSelf webPageOrderedOpen:imageUrl |
| 3286 | referrer:referrer |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3287 | inBackground:true |
| 3288 | appendTo:kCurrentTab]; |
| 3289 | }; |
| 3290 | [_contextMenuCoordinator addItemWithTitle:title action:action]; |
| 3291 | |
| 3292 | TemplateURLService* service = |
| 3293 | ios::TemplateURLServiceFactory::GetForBrowserState(_browserState); |
jeffschiller | 8aa7a4e | 2017-04-23 02:22:10 | [diff] [blame] | 3294 | const TemplateURL* defaultURL = service->GetDefaultSearchProvider(); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3295 | if (defaultURL && !defaultURL->image_url().empty() && |
| 3296 | defaultURL->image_url_ref().IsValid(service->search_terms_data())) { |
| 3297 | title = l10n_util::GetNSStringF(IDS_IOS_CONTEXT_MENU_SEARCHWEBFORIMAGE, |
| 3298 | defaultURL->short_name()); |
| 3299 | action = ^{ |
| 3300 | Record(ACTION_SEARCH_BY_IMAGE, isImage, isLink); |
| 3301 | [weakSelf searchByImageAtURL:imageUrl referrer:referrer]; |
| 3302 | }; |
| 3303 | [_contextMenuCoordinator addItemWithTitle:title action:action]; |
| 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | [_contextMenuCoordinator start]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3308 | } |
| 3309 | |
eugenebut | b739bdc | 2017-01-25 06:32:48 | [diff] [blame] | 3310 | - (void)webState:(web::WebState*)webState |
| 3311 | runRepostFormDialogWithCompletionHandler:(void (^)(BOOL))handler { |
| 3312 | // Display the action sheet with the arrow pointing at the top center of the |
| 3313 | // web contents. |
sdefresne | 0452a9d | 2017-02-09 15:33:28 | [diff] [blame] | 3314 | Tab* tab = LegacyTabHelper::GetTabForWebState(webState); |
eugenebut | b739bdc | 2017-01-25 06:32:48 | [diff] [blame] | 3315 | UIView* view = webState->GetView(); |
| 3316 | CGPoint dialogLocation = |
| 3317 | CGPointMake(CGRectGetMidX(view.frame), |
sdefresne | 0452a9d | 2017-02-09 15:33:28 | [diff] [blame] | 3318 | CGRectGetMinY(view.frame) + [self headerHeightForTab:tab]); |
vmpstr | 843b41a | 2017-03-01 21:15:03 | [diff] [blame] | 3319 | auto* helper = RepostFormTabHelper::FromWebState(webState); |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 3320 | helper->PresentDialog(dialogLocation, |
| 3321 | base::BindBlockArc(^(bool shouldContinue) { |
eugenebut | cae3d9e6 | 2017-01-27 20:01:05 | [diff] [blame] | 3322 | handler(shouldContinue); |
| 3323 | })); |
eugenebut | b739bdc | 2017-01-25 06:32:48 | [diff] [blame] | 3324 | } |
| 3325 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3326 | - (web::JavaScriptDialogPresenter*)javaScriptDialogPresenterForWebState: |
| 3327 | (web::WebState*)webState { |
| 3328 | return _javaScriptDialogPresenter.get(); |
| 3329 | } |
| 3330 | |
eugenebut | 6323210 | 2017-01-19 16:19:40 | [diff] [blame] | 3331 | - (void)webState:(web::WebState*)webState |
| 3332 | didRequestHTTPAuthForProtectionSpace:(NSURLProtectionSpace*)protectionSpace |
| 3333 | proposedCredential:(NSURLCredential*)proposedCredential |
| 3334 | completionHandler:(void (^)(NSString* username, |
| 3335 | NSString* password))handler { |
| 3336 | [self.dialogPresenter runAuthDialogForProtectionSpace:protectionSpace |
| 3337 | proposedCredential:proposedCredential |
| 3338 | webState:webState |
| 3339 | completionHandler:handler]; |
| 3340 | } |
| 3341 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 3342 | #pragma mark - CRWWebStateDelegate helpers |
| 3343 | |
| 3344 | // Evaluates Javascript asynchronously using the current page context. |
| 3345 | - (void)openJavascript:(NSString*)javascript { |
| 3346 | DCHECK(javascript); |
| 3347 | javascript = [javascript stringByRemovingPercentEncoding]; |
| 3348 | web::WebState* webState = [[_model currentTab] webState]; |
| 3349 | if (webState) { |
| 3350 | webState->ExecuteJavaScript(base::SysNSStringToUTF16(javascript)); |
| 3351 | } |
| 3352 | } |
| 3353 | |
| 3354 | // Performs a search with the image at the given url. The referrer is used to |
| 3355 | // download the image. |
| 3356 | - (void)searchByImageAtURL:(const GURL&)url |
| 3357 | referrer:(const web::Referrer)referrer { |
| 3358 | DCHECK(url.is_valid()); |
| 3359 | __weak BrowserViewController* weakSelf = self; |
| 3360 | const GURL image_source_url = url; |
| 3361 | image_fetcher::IOSImageDataFetcherCallback callback = |
| 3362 | ^(NSData* data, const image_fetcher::RequestMetadata& metadata) { |
| 3363 | DCHECK(data); |
| 3364 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 3365 | [weakSelf searchByImageData:data atURL:image_source_url]; |
| 3366 | }); |
| 3367 | }; |
| 3368 | _imageFetcher->FetchImageDataWebpDecoded( |
| 3369 | url, callback, web::ReferrerHeaderValueForNavigation(url, referrer), |
| 3370 | web::PolicyForNavigation(url, referrer)); |
| 3371 | } |
| 3372 | |
| 3373 | // Performs a search using |data| and |imageURL| as inputs. |
| 3374 | - (void)searchByImageData:(NSData*)data atURL:(const GURL&)imageURL { |
| 3375 | NSData* imageData = data; |
| 3376 | UIImage* image = [UIImage imageWithData:imageData]; |
| 3377 | // Downsize the image if its area exceeds kSearchByImageMaxImageArea AND |
| 3378 | // (either its width exceeds kSearchByImageMaxImageWidth OR its height exceeds |
| 3379 | // kSearchByImageMaxImageHeight). |
| 3380 | if (image && |
| 3381 | image.size.height * image.size.width > kSearchByImageMaxImageArea && |
| 3382 | (image.size.width > kSearchByImageMaxImageWidth || |
| 3383 | image.size.height > kSearchByImageMaxImageHeight)) { |
| 3384 | CGSize newImageSize = |
| 3385 | CGSizeMake(kSearchByImageMaxImageWidth, kSearchByImageMaxImageHeight); |
| 3386 | image = [image gtm_imageByResizingToSize:newImageSize |
| 3387 | preserveAspectRatio:YES |
| 3388 | trimToFit:NO]; |
| 3389 | imageData = UIImageJPEGRepresentation(image, 1.0); |
| 3390 | } |
| 3391 | |
| 3392 | char const* bytes = reinterpret_cast<const char*>([imageData bytes]); |
| 3393 | std::string byteString(bytes, [imageData length]); |
| 3394 | |
| 3395 | TemplateURLService* templateUrlService = |
| 3396 | ios::TemplateURLServiceFactory::GetForBrowserState(_browserState); |
| 3397 | const TemplateURL* defaultURL = |
| 3398 | templateUrlService->GetDefaultSearchProvider(); |
| 3399 | DCHECK(!defaultURL->image_url().empty()); |
| 3400 | DCHECK(defaultURL->image_url_ref().IsValid( |
| 3401 | templateUrlService->search_terms_data())); |
| 3402 | TemplateURLRef::SearchTermsArgs search_args(base::ASCIIToUTF16("")); |
| 3403 | search_args.image_url = imageURL; |
| 3404 | search_args.image_thumbnail_content = byteString; |
| 3405 | |
| 3406 | // Generate the URL and populate |post_content| with the content type and |
| 3407 | // HTTP body for the request. |
| 3408 | TemplateURLRef::PostContent post_content; |
| 3409 | GURL result(defaultURL->image_url_ref().ReplaceSearchTerms( |
| 3410 | search_args, templateUrlService->search_terms_data(), &post_content)); |
| 3411 | [self addSelectedTabWithURL:result |
| 3412 | postData:&post_content |
| 3413 | transition:ui::PAGE_TRANSITION_TYPED]; |
| 3414 | } |
| 3415 | |
| 3416 | // Saves the image at the given URL on the system's album. The referrer is used |
| 3417 | // to download the image. |
| 3418 | - (void)saveImageAtURL:(const GURL&)url |
| 3419 | referrer:(const web::Referrer&)referrer { |
| 3420 | DCHECK(url.is_valid()); |
| 3421 | |
Gauthier Ambard | 92969941 | 2018-01-02 10:05:41 | [diff] [blame] | 3422 | image_fetcher::IOSImageDataFetcherCallback callback = |
| 3423 | ^(NSData* data, const image_fetcher::RequestMetadata& metadata) { |
| 3424 | [self.imageSaver saveImageData:data withMetadata:metadata]; |
| 3425 | }; |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 3426 | _imageFetcher->FetchImageDataWebpDecoded( |
| 3427 | url, callback, web::ReferrerHeaderValueForNavigation(url, referrer), |
| 3428 | web::PolicyForNavigation(url, referrer)); |
| 3429 | } |
| 3430 | |
Kurt Horimoto | 62e97c7 | 2017-11-03 19:51:47 | [diff] [blame] | 3431 | #pragma mark - LegacyFullscreenControllerDelegate methods |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3432 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 3433 | // TODO(crbug.com/798064): Remove these methods and their helpers once the |
| 3434 | // fullscreen migration is complete. |
[email protected] | a08e2bfb | 2017-11-24 19:16:45 | [diff] [blame] | 3435 | - (void)redrawHeader { |
| 3436 | for (HeaderDefinition* header in self.headerViews) { |
| 3437 | [header.view setNeedsLayout]; |
| 3438 | } |
| 3439 | } |
| 3440 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 3441 | - (CGFloat)headerHeightForLegacyFullscreen { |
| 3442 | return self.headerHeight; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | - (BOOL)isTabWithIDCurrent:(NSString*)sessionID { |
sdefresne | b730948 | 2017-01-23 17:14:19 | [diff] [blame] | 3446 | return self.visible && [sessionID isEqualToString:[_model currentTab].tabId]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | - (CGFloat)currentHeaderOffset { |
stkhapugin | 952ecef | 2017-04-11 12:11:45 | [diff] [blame] | 3450 | NSArray<HeaderDefinition*>* headers = [self headerViews]; |
| 3451 | if (!headers.count) |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3452 | return 0.0; |
| 3453 | |
| 3454 | // Prerender tab does not have a toolbar, return |headerHeight| as promised by |
| 3455 | // API documentation. |
Sylvain Defresne | f5d2d95 | 2017-11-14 11:15:31 | [diff] [blame] | 3456 | if (_insertedTabWasPrerenderedTab) |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 3457 | return self.headerHeight; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3458 | |
| 3459 | UIView* topHeader = headers[0].view; |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 3460 | return -(topHeader.frame.origin.y - self.headerOffset); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3461 | } |
| 3462 | |
Kurt Horimoto | 62e97c7 | 2017-11-03 19:51:47 | [diff] [blame] | 3463 | - (void)fullScreenController:(LegacyFullscreenController*)fullScreenController |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3464 | drawHeaderViewFromOffset:(CGFloat)headerOffset |
| 3465 | animate:(BOOL)animate { |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 3466 | if ([self.sideSwipeController inSwipe]) |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3467 | return; |
| 3468 | |
| 3469 | CGRect footerFrame = CGRectZero; |
| 3470 | UIView* footer = nil; |
| 3471 | // Only animate the voice search bar if the tab is a voice search results tab. |
| 3472 | if ([_model currentTab].isVoiceSearchResultsTab) { |
| 3473 | footer = [self footerView]; |
| 3474 | footerFrame = footer.frame; |
| 3475 | footerFrame.origin.y = [self footerYForHeaderOffset:headerOffset]; |
| 3476 | } |
| 3477 | |
stkhapugin | 952ecef | 2017-04-11 12:11:45 | [diff] [blame] | 3478 | NSArray<HeaderDefinition*>* headers = [self headerViews]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3479 | void (^block)(void) = ^{ |
| 3480 | [self setFramesForHeaders:headers atOffset:headerOffset]; |
| 3481 | footer.frame = footerFrame; |
| 3482 | }; |
| 3483 | void (^completion)(BOOL) = ^(BOOL finished) { |
| 3484 | [self fullScreenController:fullScreenController |
| 3485 | headerAnimationCompleted:finished |
| 3486 | offset:headerOffset]; |
| 3487 | }; |
| 3488 | if (animate) { |
Kurt Horimoto | 62e97c7 | 2017-11-03 19:51:47 | [diff] [blame] | 3489 | [UIView |
| 3490 | animateWithDuration:kLegacyFullscreenControllerToolbarAnimationDuration |
| 3491 | delay:0.0 |
| 3492 | options:UIViewAnimationOptionBeginFromCurrentState |
| 3493 | animations:block |
| 3494 | completion:completion]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3495 | } else { |
| 3496 | block(); |
| 3497 | completion(YES); |
| 3498 | } |
| 3499 | } |
| 3500 | |
Kurt Horimoto | 62e97c7 | 2017-11-03 19:51:47 | [diff] [blame] | 3501 | - (void)fullScreenController:(LegacyFullscreenController*)fullScreenController |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3502 | drawHeaderViewFromOffset:(CGFloat)headerOffset |
| 3503 | onWebViewProxy:(id<CRWWebViewProxy>)webViewProxy |
| 3504 | changeTopContentPadding:(BOOL)changeTopContentPadding |
| 3505 | scrollingToOffset:(CGFloat)contentOffset { |
| 3506 | DCHECK(webViewProxy); |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 3507 | if ([self.sideSwipeController inSwipe]) |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3508 | return; |
| 3509 | |
| 3510 | CGRect footerFrame; |
| 3511 | UIView* footer = nil; |
| 3512 | // Only animate the voice search bar if the tab is a voice search results tab. |
| 3513 | if ([_model currentTab].isVoiceSearchResultsTab) { |
| 3514 | footer = [self footerView]; |
| 3515 | footerFrame = footer.frame; |
| 3516 | footerFrame.origin.y = [self footerYForHeaderOffset:headerOffset]; |
| 3517 | } |
| 3518 | |
stkhapugin | 952ecef | 2017-04-11 12:11:45 | [diff] [blame] | 3519 | NSArray<HeaderDefinition*>* headers = [self headerViews]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3520 | void (^block)(void) = ^{ |
| 3521 | [self setFramesForHeaders:headers atOffset:headerOffset]; |
| 3522 | footer.frame = footerFrame; |
| 3523 | webViewProxy.scrollViewProxy.contentOffset = CGPointMake( |
| 3524 | webViewProxy.scrollViewProxy.contentOffset.x, contentOffset); |
| 3525 | if (changeTopContentPadding) |
| 3526 | webViewProxy.topContentPadding = contentOffset; |
| 3527 | }; |
| 3528 | void (^completion)(BOOL) = ^(BOOL finished) { |
| 3529 | [self fullScreenController:fullScreenController |
| 3530 | headerAnimationCompleted:finished |
| 3531 | offset:headerOffset]; |
| 3532 | }; |
| 3533 | |
Kurt Horimoto | 62e97c7 | 2017-11-03 19:51:47 | [diff] [blame] | 3534 | [UIView |
| 3535 | animateWithDuration:kLegacyFullscreenControllerToolbarAnimationDuration |
| 3536 | delay:0.0 |
| 3537 | options:UIViewAnimationOptionBeginFromCurrentState |
| 3538 | animations:block |
| 3539 | completion:completion]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3540 | } |
| 3541 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 3542 | #pragma mark - LegacyFullscreenControllerDelegate helpers |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3543 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 3544 | // Returns the y coordinate for the footer's frame when animating the footer |
| 3545 | // in/out of fullscreen. |
| 3546 | - (CGFloat)footerYForHeaderOffset:(CGFloat)headerOffset { |
| 3547 | UIView* footer = [self footerView]; |
| 3548 | CGFloat headerHeight = [self headerHeight]; |
| 3549 | if (!footer || headerHeight == 0) |
| 3550 | return 0.0; |
| 3551 | |
| 3552 | CGFloat footerHeight = CGRectGetHeight(footer.frame); |
| 3553 | CGFloat offset = headerOffset * footerHeight / headerHeight; |
| 3554 | return std::ceil(CGRectGetHeight(self.view.bounds) - footerHeight + offset); |
| 3555 | } |
| 3556 | |
| 3557 | // Called when the animation for setting the header view's offset is finished. |
| 3558 | // |completed| should indicate if the animation finished completely or was |
| 3559 | // interrupted. |offset| should indicate the header offset after the animation. |
| 3560 | // |dragged| should indicate if the header moved due to the user dragging. |
| 3561 | - (void)fullScreenController:(LegacyFullscreenController*)controller |
| 3562 | headerAnimationCompleted:(BOOL)completed |
| 3563 | offset:(CGFloat)offset { |
| 3564 | if (completed) |
| 3565 | [controller setToolbarInsetsForHeaderOffset:offset]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3566 | } |
| 3567 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3568 | #pragma mark - OverscrollActionsControllerDelegate methods. |
| 3569 | |
| 3570 | - (void)overscrollActionsController:(OverscrollActionsController*)controller |
rohitrao | 922b7111c | 2017-01-03 14:31:05 | [diff] [blame] | 3571 | didTriggerAction:(OverscrollAction)action { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3572 | switch (action) { |
rohitrao | 922b7111c | 2017-01-03 14:31:05 | [diff] [blame] | 3573 | case OverscrollAction::NEW_TAB: |
Mark Cogan | dfcdea7 | 2017-07-18 13:47:38 | [diff] [blame] | 3574 | [self.dispatcher |
| 3575 | openNewTab:[OpenNewTabCommand |
| 3576 | commandWithIncognito:self.isOffTheRecord]]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3577 | break; |
rohitrao | 922b7111c | 2017-01-03 14:31:05 | [diff] [blame] | 3578 | case OverscrollAction::CLOSE_TAB: |
Mark Cogan | 6c58ea9 | 2017-07-06 13:08:24 | [diff] [blame] | 3579 | [self.dispatcher closeCurrentTab]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3580 | break; |
Kurt Horimoto | 4ce1932 | 2017-11-28 19:10:47 | [diff] [blame] | 3581 | case OverscrollAction::REFRESH: |
| 3582 | [self reload]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3583 | break; |
rohitrao | 922b7111c | 2017-01-03 14:31:05 | [diff] [blame] | 3584 | case OverscrollAction::NONE: |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3585 | NOTREACHED(); |
| 3586 | break; |
| 3587 | } |
| 3588 | } |
| 3589 | |
| 3590 | - (BOOL)shouldAllowOverscrollActions { |
| 3591 | return YES; |
| 3592 | } |
| 3593 | |
| 3594 | - (UIView*)headerView { |
sczs | 42f7f748 | 2017-11-08 01:13:27 | [diff] [blame] | 3595 | return _toolbarCoordinator.toolbarViewController.view; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3596 | } |
| 3597 | |
| 3598 | - (UIView*)toolbarSnapshotView { |
sczs | 42f7f748 | 2017-11-08 01:13:27 | [diff] [blame] | 3599 | return [_toolbarCoordinator.toolbarViewController.view |
| 3600 | snapshotViewAfterScreenUpdates:NO]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3601 | } |
| 3602 | |
| 3603 | - (CGFloat)overscrollActionsControllerHeaderInset: |
| 3604 | (OverscrollActionsController*)controller { |
| 3605 | if (controller == [[[self tabModel] currentTab] overscrollActionsController]) |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 3606 | return self.headerHeight; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3607 | else |
| 3608 | return 0; |
| 3609 | } |
| 3610 | |
| 3611 | - (CGFloat)overscrollHeaderHeight { |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 3612 | return self.headerHeight + StatusBarHeight(); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3613 | } |
| 3614 | |
| 3615 | #pragma mark - TabSnapshottingDelegate methods. |
| 3616 | |
Sylvain Defresne | 0a86dd2 | 2017-12-19 14:37:50 | [diff] [blame] | 3617 | - (UIEdgeInsets)snapshotEdgeInsetsForTab:(Tab*)tab { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3618 | CGFloat headerHeight = [self headerHeightForTab:tab]; |
| 3619 | id nativeController = [self nativeControllerForTab:tab]; |
| 3620 | if ([nativeController respondsToSelector:@selector(toolbarHeight)]) |
| 3621 | headerHeight += [nativeController toolbarHeight]; |
Sylvain Defresne | 0a86dd2 | 2017-12-19 14:37:50 | [diff] [blame] | 3622 | return UIEdgeInsetsMake(headerHeight, 0.0, 0.0, 0.0); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3623 | } |
| 3624 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 3625 | #pragma mark - NewTabPageControllerObserver methods. |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3626 | |
| 3627 | - (void)selectedPanelDidChange { |
| 3628 | [self updateToolbar]; |
| 3629 | } |
| 3630 | |
| 3631 | #pragma mark - CRWNativeContentProvider methods |
| 3632 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 3633 | // TODO(crbug.com/725241): This method is deprecated and should be removed by |
| 3634 | // switching to DidFinishnavigation. |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3635 | - (id<CRWNativeContent>)controllerForURL:(const GURL&)url |
| 3636 | withError:(NSError*)error |
| 3637 | isPost:(BOOL)isPost { |
| 3638 | ErrorPageContent* errorPageContent = |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 3639 | [[ErrorPageContent alloc] initWithLoader:self |
| 3640 | browserState:self.browserState |
| 3641 | url:url |
| 3642 | error:error |
| 3643 | isPost:isPost |
| 3644 | isIncognito:_isOffTheRecord]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3645 | [self setOverScrollActionControllerToStaticNativeContent:errorPageContent]; |
| 3646 | return errorPageContent; |
| 3647 | } |
| 3648 | |
| 3649 | - (BOOL)hasControllerForURL:(const GURL&)url { |
Marti Wong | 64481ec | 2017-10-31 03:38:00 | [diff] [blame] | 3650 | base::StringPiece host = url.host_piece(); |
olivierrobin | 5c861c2 | 2017-04-07 15:56:45 | [diff] [blame] | 3651 | if (host == kChromeUIOfflineHost) { |
| 3652 | // Only allow offline URL that are fully specified. |
| 3653 | return reading_list::IsOfflineURLValid( |
| 3654 | url, ReadingListModelFactory::GetForBrowserState(_browserState)); |
| 3655 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3656 | |
Justin Cohen | 8679e85 | 2017-08-14 16:35:25 | [diff] [blame] | 3657 | if (host == kChromeUIBookmarksHost) { |
Marti Wong | 64481ec | 2017-10-31 03:38:00 | [diff] [blame] | 3658 | return IsBookmarksHostEnabled(); |
Justin Cohen | 8679e85 | 2017-08-14 16:35:25 | [diff] [blame] | 3659 | } |
| 3660 | |
| 3661 | return host == kChromeUINewTabHost; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3662 | } |
| 3663 | |
olivierrobin | d43eecb | 2017-01-27 20:35:26 | [diff] [blame] | 3664 | - (id<CRWNativeContent>)controllerForURL:(const GURL&)url |
| 3665 | webState:(web::WebState*)webState { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3666 | DCHECK(url.SchemeIs(kChromeUIScheme)); |
| 3667 | |
| 3668 | id<CRWNativeContent> nativeController = nil; |
Marti Wong | 64481ec | 2017-10-31 03:38:00 | [diff] [blame] | 3669 | base::StringPiece url_host = url.host_piece(); |
Justin Cohen | 4971595 | 2017-08-22 14:12:19 | [diff] [blame] | 3670 | if (url_host == kChromeUINewTabHost || |
Marti Wong | 64481ec | 2017-10-31 03:38:00 | [diff] [blame] | 3671 | (url_host == kChromeUIBookmarksHost && IsBookmarksHostEnabled())) { |
Gauthier Ambard | d889045 | 2017-09-29 12:07:46 | [diff] [blame] | 3672 | CGFloat fakeStatusBarHeight = _fakeStatusBarView.frame.size.height; |
| 3673 | UIEdgeInsets safeAreaInset = UIEdgeInsetsZero; |
| 3674 | if (@available(iOS 11.0, *)) { |
| 3675 | safeAreaInset = self.view.safeAreaInsets; |
| 3676 | } |
| 3677 | safeAreaInset.top = MAX(safeAreaInset.top - fakeStatusBarHeight, 0); |
| 3678 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3679 | NewTabPageController* pageController = |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 3680 | [[NewTabPageController alloc] initWithUrl:url |
| 3681 | loader:self |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 3682 | focuser:_toolbarCoordinator |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 3683 | ntpObserver:self |
| 3684 | browserState:_browserState |
| 3685 | colorCache:_dominantColorCache |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 3686 | toolbarDelegate:_toolbarCoordinator |
justincohen | bc91363 | 2017-04-18 14:41:45 | [diff] [blame] | 3687 | tabModel:_model |
justincohen | 75011c3 | 2017-04-28 16:31:39 | [diff] [blame] | 3688 | parentViewController:self |
Gauthier Ambard | d889045 | 2017-09-29 12:07:46 | [diff] [blame] | 3689 | dispatcher:self.dispatcher |
| 3690 | safeAreaInset:safeAreaInset]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3691 | pageController.swipeRecognizerProvider = self.sideSwipeController; |
| 3692 | |
| 3693 | // Panel is always NTP for iPhone. |
Gauthier Ambard | f520c02 | 2017-08-29 07:42:23 | [diff] [blame] | 3694 | ntp_home::PanelIdentifier panelType = ntp_home::HOME_PANEL; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3695 | |
Marti Wong | 64481ec | 2017-10-31 03:38:00 | [diff] [blame] | 3696 | if (IsBookmarksHostEnabled()) { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3697 | // New Tab Page can have multiple panels. Each panel is addressable |
| 3698 | // by a #fragment, e.g. chrome://newtab/#most_visited takes user to |
| 3699 | // the Most Visited page, chrome://newtab/#bookmarks takes user to |
| 3700 | // the Bookmark Manager, etc. |
| 3701 | // The utility functions NewTabPage::IdentifierFromFragment() and |
| 3702 | // FragmentFromIdentifier() map an identifier to/from a #fragment. |
| 3703 | // If the URL is chrome://bookmarks, pre-select the #bookmarks panel |
| 3704 | // without changing the URL since the URL may be chrome://bookmarks/#123. |
| 3705 | // If the URL is chrome://newtab/, pre-select the panel based on the |
| 3706 | // #fragment. |
| 3707 | panelType = url_host == kChromeUIBookmarksHost |
Gauthier Ambard | f520c02 | 2017-08-29 07:42:23 | [diff] [blame] | 3708 | ? ntp_home::BOOKMARKS_PANEL |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3709 | : NewTabPage::IdentifierFromFragment(url.ref()); |
| 3710 | } |
| 3711 | [pageController selectPanel:panelType]; |
| 3712 | nativeController = pageController; |
olivierrobin | 5c861c2 | 2017-04-07 15:56:45 | [diff] [blame] | 3713 | } else if (url_host == kChromeUIOfflineHost && |
| 3714 | [self hasControllerForURL:url]) { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3715 | StaticHtmlNativeContent* staticNativeController = |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 3716 | [[OfflinePageNativeContent alloc] initWithLoader:self |
| 3717 | browserState:_browserState |
| 3718 | webState:webState |
| 3719 | URL:url]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3720 | [self setOverScrollActionControllerToStaticNativeContent: |
| 3721 | staticNativeController]; |
| 3722 | nativeController = staticNativeController; |
| 3723 | } else if (url_host == kChromeUIExternalFileHost) { |
| 3724 | // Return an instance of the |ExternalFileController| only if the file is |
| 3725 | // still in the sandbox. |
| 3726 | NSString* filePath = [ExternalFileController pathForExternalFileURL:url]; |
| 3727 | if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 3728 | nativeController = |
| 3729 | [[ExternalFileController alloc] initWithURL:url |
| 3730 | browserState:_browserState]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3731 | } |
peterlaurens | 44615d0 | 2017-05-23 20:23:09 | [diff] [blame] | 3732 | } else if (url_host == kChromeUICrashHost) { |
| 3733 | // There is no native controller for kChromeUICrashHost, it is instead |
| 3734 | // handled as any other renderer crash by the SadTabTabHelper. |
| 3735 | // nativeController must be set to nil to prevent defaulting to a |
| 3736 | // PageNotAvailableController. |
| 3737 | nativeController = nil; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3738 | } else { |
| 3739 | DCHECK(![self hasControllerForURL:url]); |
| 3740 | // In any other case the PageNotAvailableController is returned. |
stkhapugin | f58b10d0 | 2017-04-10 13:36:17 | [diff] [blame] | 3741 | nativeController = [[PageNotAvailableController alloc] initWithUrl:url]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3742 | } |
| 3743 | // If a native controller is vended before its tab is added to the tab model, |
| 3744 | // use the temporary key and add it under the new tab's tabId in the |
| 3745 | // TabModelObserver callback. This happens: |
| 3746 | // - when there is no current tab (occurs when vending the NTP controller for |
| 3747 | // the first tab that is opened), |
| 3748 | // - when the current tab's url doesn't match |url| (occurs when a native |
| 3749 | // controller is opened in a new tab) |
| 3750 | // - when the current tab's url matches |url| and there is already a native |
| 3751 | // controller of the appropriate type vended to it (occurs when a native |
| 3752 | // controller is opened in a new tab from a tab with a matching URL, e.g. |
| 3753 | // opening an NTP when an NTP is already displayed in the current tab). |
| 3754 | // For normal page loads, history navigations, tab restorations, and crash |
| 3755 | // recoveries, the tab will already exist in the tab model and the tabId can |
| 3756 | // be used as the native controller key. |
| 3757 | // TODO(crbug.com/498568): To reduce complexity here, refactor the flow so |
| 3758 | // that native controllers vended here always correspond to the current tab. |
| 3759 | Tab* currentTab = [_model currentTab]; |
Sylvain Defresne | e7f2c8a | 2017-10-17 02:39:19 | [diff] [blame] | 3760 | if (!currentTab.webState || |
| 3761 | currentTab.webState->GetLastCommittedURL() != url || |
Eugene But | 56efc32 | 2017-08-11 14:03:44 | [diff] [blame] | 3762 | [currentTab.webController.nativeController |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3763 | isKindOfClass:[nativeController class]]) { |
Eugene But | 56efc32 | 2017-08-11 14:03:44 | [diff] [blame] | 3764 | _temporaryNativeController = nativeController; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3765 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3766 | return nativeController; |
| 3767 | } |
| 3768 | |
edchin | 2c6af4a | 2017-12-05 02:16:49 | [diff] [blame] | 3769 | - (id<CRWNativeContent>)controllerForUnhandledContentAtURL:(const GURL&)URL |
| 3770 | webState: |
| 3771 | (web::WebState*)webState { |
Eugene But | 87a0953 | 2017-12-08 20:02:05 | [diff] [blame] | 3772 | LegacyDownloadManagerController* downloadController = |
| 3773 | [[LegacyDownloadManagerController alloc] initWithWebState:webState |
edchin | 2c6af4a | 2017-12-05 02:16:49 | [diff] [blame] | 3774 | downloadURL:URL |
| 3775 | baseViewController:self]; |
| 3776 | [downloadController start]; |
| 3777 | return downloadController; |
| 3778 | } |
| 3779 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3780 | #pragma mark - DialogPresenterDelegate methods |
| 3781 | |
| 3782 | - (void)dialogPresenter:(DialogPresenter*)presenter |
| 3783 | willShowDialogForWebState:(web::WebState*)webState { |
| 3784 | for (Tab* iteratedTab in self.tabModel) { |
| 3785 | if ([iteratedTab webState] == webState) { |
| 3786 | self.tabModel.currentTab = iteratedTab; |
| 3787 | DCHECK([[iteratedTab view] isDescendantOfView:self.contentArea]); |
| 3788 | break; |
| 3789 | } |
| 3790 | } |
| 3791 | } |
| 3792 | |
Kurt Horimoto | 06b9425 | 2017-12-08 19:45:59 | [diff] [blame] | 3793 | #pragma mark - FullscreenUIElement methods |
| 3794 | |
| 3795 | - (void)updateForFullscreenProgress:(CGFloat)progress { |
| 3796 | [self updateHeadersForFullscreenProgress:progress]; |
| 3797 | [self updateFootersForFullscreenProgress:progress]; |
| 3798 | [self updateContentViewTopPaddingForFullscreenProgress:progress]; |
| 3799 | } |
| 3800 | |
| 3801 | - (void)updateForFullscreenEnabled:(BOOL)enabled { |
| 3802 | if (!enabled) |
| 3803 | [self updateForFullscreenProgress:1.0]; |
| 3804 | } |
| 3805 | |
| 3806 | - (void)finishFullscreenScrollWithAnimator: |
| 3807 | (FullscreenScrollEndAnimator*)animator { |
| 3808 | BOOL showingToolbar = animator.finalProgress > animator.startProgress; |
| 3809 | CGFloat finalProgress = animator.finalProgress; |
| 3810 | // WKWebView does not re-render its content until its model layer's bounds |
| 3811 | // have been updated at the end of the animation. If the animator is going |
| 3812 | // to hide the toolbar, update the content view's top padding early so that |
| 3813 | // content is correctly rendered behind the toolbar that's being animated |
| 3814 | // away. |
| 3815 | if (!showingToolbar) |
| 3816 | [self updateContentViewTopPaddingForFullscreenProgress:finalProgress]; |
| 3817 | [animator addAnimations:^{ |
| 3818 | [self updateHeadersForFullscreenProgress:finalProgress]; |
| 3819 | [self updateFootersForFullscreenProgress:finalProgress]; |
| 3820 | }]; |
| 3821 | // If the toolbar is being animated to become visible, update the content view |
| 3822 | // top padding in the completion block so that fixed-position elements can be |
| 3823 | // properly laid out in the new viewport. |
| 3824 | if (showingToolbar) { |
| 3825 | __weak FullscreenScrollEndAnimator* weakAnimator = animator; |
| 3826 | [animator addCompletion:^(UIViewAnimatingPosition finalPosition) { |
| 3827 | [self updateContentViewTopPaddingForFullscreenProgress: |
| 3828 | [weakAnimator progressForAnimatingPosition:finalPosition]]; |
| 3829 | }]; |
| 3830 | } |
| 3831 | } |
| 3832 | |
| 3833 | #pragma mark - FullscreenUIElement helpers |
| 3834 | |
| 3835 | // Translates the header views up and down according to |progress|, where a |
| 3836 | // progress of 1.0 fully shows the headers and a progress of 0.0 fully hides |
| 3837 | // them. |
| 3838 | - (void)updateHeadersForFullscreenProgress:(CGFloat)progress { |
| 3839 | [self setFramesForHeaders:[self headerViews] |
| 3840 | atOffset:(1.0 - progress) * [self toolbarHeight]]; |
| 3841 | } |
| 3842 | |
| 3843 | // Translates the footer view up and down according to |progress|, where a |
| 3844 | // progress of 1.0 fully shows the footer and a progress of 0.0 fully hides it. |
| 3845 | - (void)updateFootersForFullscreenProgress:(CGFloat)progress { |
| 3846 | if (![_model currentTab].isVoiceSearchResultsTab) |
| 3847 | return; |
| 3848 | |
| 3849 | UIView* footerView = [self footerView]; |
| 3850 | DCHECK(footerView); |
| 3851 | CGRect frame = footerView.frame; |
| 3852 | frame.origin.y = CGRectGetMaxY(footerView.superview.bounds) - |
| 3853 | progress * CGRectGetHeight(frame); |
| 3854 | footerView.frame = frame; |
| 3855 | } |
| 3856 | |
| 3857 | // Updates the top padding of the web view proxy. This either resets the frame |
| 3858 | // of the WKWebView or the contentInsets of the WKWebView's UIScrollView, |
| 3859 | // depending on the the proxy's |shouldUseInsetForTopPadding| property. |
| 3860 | - (void)updateContentViewTopPaddingForFullscreenProgress:(CGFloat)progress { |
| 3861 | if (self.currentWebState) { |
| 3862 | self.currentWebState->GetWebViewProxy().topContentPadding = |
| 3863 | progress * [self toolbarHeight]; |
| 3864 | } |
| 3865 | } |
| 3866 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3867 | #pragma mark - KeyCommandsPlumbing |
| 3868 | |
| 3869 | - (BOOL)isOffTheRecord { |
| 3870 | return _isOffTheRecord; |
| 3871 | } |
| 3872 | |
| 3873 | - (NSUInteger)tabsCount { |
| 3874 | return [_model count]; |
| 3875 | } |
| 3876 | |
lpromero | 47ea886 | 2017-01-13 17:51:06 | [diff] [blame] | 3877 | - (BOOL)canGoBack { |
| 3878 | return [_model currentTab].canGoBack; |
| 3879 | } |
| 3880 | |
| 3881 | - (BOOL)canGoForward { |
| 3882 | return [_model currentTab].canGoForward; |
| 3883 | } |
| 3884 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3885 | - (void)focusTabAtIndex:(NSUInteger)index { |
| 3886 | if ([_model count] > index) { |
| 3887 | [_model setCurrentTab:[_model tabAtIndex:index]]; |
| 3888 | } |
| 3889 | } |
| 3890 | |
| 3891 | - (void)focusNextTab { |
| 3892 | NSInteger currentTabIndex = [_model indexOfTab:[_model currentTab]]; |
| 3893 | NSInteger modelCount = [_model count]; |
| 3894 | if (currentTabIndex < modelCount - 1) { |
| 3895 | Tab* nextTab = [_model tabAtIndex:currentTabIndex + 1]; |
| 3896 | [_model setCurrentTab:nextTab]; |
| 3897 | } else { |
| 3898 | [_model setCurrentTab:[_model tabAtIndex:0]]; |
| 3899 | } |
| 3900 | } |
| 3901 | |
| 3902 | - (void)focusPreviousTab { |
| 3903 | NSInteger currentTabIndex = [_model indexOfTab:[_model currentTab]]; |
| 3904 | if (currentTabIndex > 0) { |
| 3905 | Tab* previousTab = [_model tabAtIndex:currentTabIndex - 1]; |
| 3906 | [_model setCurrentTab:previousTab]; |
| 3907 | } else { |
| 3908 | Tab* lastTab = [_model tabAtIndex:[_model count] - 1]; |
| 3909 | [_model setCurrentTab:lastTab]; |
| 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | - (void)reopenClosedTab { |
| 3914 | sessions::TabRestoreService* const tabRestoreService = |
| 3915 | IOSChromeTabRestoreServiceFactory::GetForBrowserState(_browserState); |
| 3916 | if (!tabRestoreService || tabRestoreService->entries().empty()) |
| 3917 | return; |
| 3918 | |
| 3919 | const std::unique_ptr<sessions::TabRestoreService::Entry>& entry = |
| 3920 | tabRestoreService->entries().front(); |
| 3921 | // Only handle the TAB type. |
| 3922 | if (entry->type != sessions::TabRestoreService::TAB) |
| 3923 | return; |
| 3924 | |
Mark Cogan | dfcdea7 | 2017-07-18 13:47:38 | [diff] [blame] | 3925 | [self.dispatcher openNewTab:[OpenNewTabCommand command]]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3926 | TabRestoreServiceDelegateImplIOS* const delegate = |
| 3927 | TabRestoreServiceDelegateImplIOSFactory::GetForBrowserState( |
| 3928 | _browserState); |
| 3929 | tabRestoreService->RestoreEntryById(delegate, entry->id, |
| 3930 | WindowOpenDisposition::CURRENT_TAB); |
| 3931 | } |
| 3932 | |
Kurt Horimoto | e9b6002c | 2017-12-04 23:19:19 | [diff] [blame] | 3933 | #pragma mark - MainContentUI |
| 3934 | |
| 3935 | - (MainContentUIState*)mainContentUIState { |
| 3936 | return _mainContentUIUpdater.state; |
| 3937 | } |
| 3938 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 3939 | #pragma mark - UrlLoader (Public) |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 3940 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3941 | - (void)loadURL:(const GURL&)url |
| 3942 | referrer:(const web::Referrer&)referrer |
| 3943 | transition:(ui::PageTransition)transition |
| 3944 | rendererInitiated:(BOOL)rendererInitiated { |
| 3945 | [[OmniboxGeolocationController sharedInstance] |
| 3946 | locationBarDidSubmitURL:url |
| 3947 | transition:transition |
| 3948 | browserState:_browserState]; |
| 3949 | |
| 3950 | [_bookmarkInteractionController dismissBookmarkModalControllerAnimated:YES]; |
| 3951 | if (transition & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR) { |
| 3952 | new_tab_page_uma::RecordActionFromOmnibox(_browserState, url, transition); |
| 3953 | } |
| 3954 | |
| 3955 | // NOTE: This check for the Crash Host URL is here to avoid the URL from |
dbeam | 25b548f | 2017-05-05 18:05:24 | [diff] [blame] | 3956 | // ending up in the history causing the app to crash at every subsequent |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3957 | // restart. |
| 3958 | if (url.host() == kChromeUIBrowserCrashHost) { |
| 3959 | [self induceBrowserCrash]; |
| 3960 | // In debug the app can continue working even after the CHECK. Adding a |
| 3961 | // return avoids the crash url to be added to the history. |
| 3962 | return; |
| 3963 | } |
| 3964 | |
Danyao Wang | 85389a8 | 2017-10-25 18:56:27 | [diff] [blame] | 3965 | bool typed_or_generated_transition = |
| 3966 | PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_TYPED) || |
| 3967 | PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_GENERATED); |
| 3968 | |
Rohit Rao | 44f20430 | 2017-08-10 14:49:54 | [diff] [blame] | 3969 | PrerenderService* prerenderService = |
| 3970 | PrerenderServiceFactory::GetForBrowserState(self.browserState); |
| 3971 | if (prerenderService && prerenderService->HasPrerenderForUrl(url)) { |
sdefresne | 2c600c5 | 2017-04-04 16:49:59 | [diff] [blame] | 3972 | std::unique_ptr<web::WebState> newWebState = |
Rohit Rao | 44f20430 | 2017-08-10 14:49:54 | [diff] [blame] | 3973 | prerenderService->ReleasePrerenderContents(); |
sdefresne | 2c600c5 | 2017-04-04 16:49:59 | [diff] [blame] | 3974 | DCHECK(newWebState); |
| 3975 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3976 | Tab* oldTab = [_model currentTab]; |
sdefresne | 2c600c5 | 2017-04-04 16:49:59 | [diff] [blame] | 3977 | Tab* newTab = LegacyTabHelper::GetTabForWebState(newWebState.get()); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3978 | DCHECK(oldTab); |
| 3979 | DCHECK(newTab); |
sdefresne | 2c600c5 | 2017-04-04 16:49:59 | [diff] [blame] | 3980 | |
kkhorimoto | d804c573 | 2017-03-15 23:44:52 | [diff] [blame] | 3981 | bool canPruneItems = |
| 3982 | [newTab navigationManager]->CanPruneAllButLastCommittedItem(); |
sdefresne | 2c600c5 | 2017-04-04 16:49:59 | [diff] [blame] | 3983 | |
Sylvain Defresne | 17b8aa4 | 2017-12-21 16:17:17 | [diff] [blame] | 3984 | if (canPruneItems) { |
kkhorimoto | d804c573 | 2017-03-15 23:44:52 | [diff] [blame] | 3985 | [newTab navigationManager]->CopyStateFromAndPrune( |
| 3986 | [oldTab navigationManager]); |
sdefresne | 2c600c5 | 2017-04-04 16:49:59 | [diff] [blame] | 3987 | |
Sylvain Defresne | f5d2d95 | 2017-11-14 11:15:31 | [diff] [blame] | 3988 | // Set _insertedTabWasPrerenderedTab to YES while the Tab is inserted |
| 3989 | // so that the correct toolbar height is used and animation are played. |
| 3990 | _insertedTabWasPrerenderedTab = YES; |
sdefresne | 2c600c5 | 2017-04-04 16:49:59 | [diff] [blame] | 3991 | [_model webStateList]->ReplaceWebStateAt([_model indexOfTab:oldTab], |
| 3992 | std::move(newWebState)); |
Sylvain Defresne | f5d2d95 | 2017-11-14 11:15:31 | [diff] [blame] | 3993 | _insertedTabWasPrerenderedTab = NO; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 3994 | |
Sylvain Defresne | 17b8aa4 | 2017-12-21 16:17:17 | [diff] [blame] | 3995 | if ([newTab loadFinished]) { |
| 3996 | // If the page has finished loading, take a snapshot. If the page is |
| 3997 | // still loading, do nothing, as the tab helper will automatically take |
| 3998 | // a snapshot once the load completes. |
| 3999 | SnapshotTabHelper::FromWebState(newTab.webState) |
| 4000 | ->UpdateSnapshot( |
| 4001 | /*with_overlays=*/true, /*visible_frame_only=*/true); |
| 4002 | } |
| 4003 | |
Danyao Wang | 85389a8 | 2017-10-25 18:56:27 | [diff] [blame] | 4004 | if (typed_or_generated_transition) { |
| 4005 | LoadTimingTabHelper::FromWebState(newTab.webState) |
| 4006 | ->DidPromotePrerenderTab(); |
| 4007 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4008 | |
sdefresne | 2f7781c | 2017-03-02 19:12:46 | [diff] [blame] | 4009 | [self tabLoadComplete:newTab withSuccess:newTab.loadFinished]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4010 | return; |
| 4011 | } |
| 4012 | } |
| 4013 | |
| 4014 | GURL urlToLoad = url; |
Rohit Rao | 44f20430 | 2017-08-10 14:49:54 | [diff] [blame] | 4015 | if (prerenderService) { |
| 4016 | prerenderService->CancelPrerender(); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4017 | } |
| 4018 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4019 | // Some URLs are not allowed while in incognito. If we are in incognito and |
| 4020 | // load a disallowed URL, instead create a new tab not in the incognito state. |
| 4021 | if (_isOffTheRecord && !IsURLAllowedInIncognito(url)) { |
| 4022 | [self webPageOrderedOpen:url |
| 4023 | referrer:web::Referrer() |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4024 | inIncognito:NO |
| 4025 | inBackground:NO |
| 4026 | appendTo:kCurrentTab]; |
| 4027 | return; |
| 4028 | } |
| 4029 | |
Danyao Wang | 85389a8 | 2017-10-25 18:56:27 | [diff] [blame] | 4030 | if (typed_or_generated_transition) { |
| 4031 | LoadTimingTabHelper::FromWebState([_model currentTab].webState) |
| 4032 | ->DidInitiatePageLoad(); |
| 4033 | } |
| 4034 | |
mrefaat | a84d5a0 | 2017-06-08 17:13:29 | [diff] [blame] | 4035 | // If this is a reload initiated from the omnibox. |
| 4036 | // TODO(crbug.com/730192): Add DCHECK to verify that whenever urlToLood is the |
| 4037 | // same as the old url, the transition type is ui::PAGE_TRANSITION_RELOAD. |
| 4038 | if (PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_RELOAD)) { |
| 4039 | [[_model currentTab] navigationManager]->Reload( |
| 4040 | web::ReloadType::NORMAL, true /* check_for_repost */); |
| 4041 | return; |
| 4042 | } |
| 4043 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4044 | web::NavigationManager::WebLoadParams params(urlToLoad); |
| 4045 | params.referrer = referrer; |
| 4046 | params.transition_type = transition; |
| 4047 | params.is_renderer_initiated = rendererInitiated; |
Kurt Horimoto | 208a1e8 | 2017-10-27 01:41:10 | [diff] [blame] | 4048 | Tab* currentTab = [_model currentTab]; |
| 4049 | DCHECK(currentTab); |
| 4050 | BOOL wasVoiceSearchTab = currentTab.isVoiceSearchResultsTab; |
| 4051 | currentTab.navigationManager->LoadURLWithParams(params); |
| 4052 | // When a Tab becomes a voice search Tab, the voice search bar doesn't need |
| 4053 | // to be animated on screen because the transition animator will handle the |
| 4054 | // animations. When a Tab stops being a voice search Tab, the voice search |
| 4055 | // bar should be animated away. |
| 4056 | if (currentTab.isVoiceSearchResultsTab != wasVoiceSearchTab) |
| 4057 | [self updateVoiceSearchBarVisibilityAnimated:wasVoiceSearchTab]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4058 | } |
| 4059 | |
| 4060 | - (void)loadJavaScriptFromLocationBar:(NSString*)script { |
Rohit Rao | 44f20430 | 2017-08-10 14:49:54 | [diff] [blame] | 4061 | PrerenderService* prerenderService = |
| 4062 | PrerenderServiceFactory::GetForBrowserState(self.browserState); |
| 4063 | if (prerenderService) { |
| 4064 | prerenderService->CancelPrerender(); |
| 4065 | } |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4066 | DCHECK([_model currentTab]); |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 4067 | if (self.currentWebState) |
| 4068 | self.currentWebState->ExecuteUserJavaScript(script); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4069 | } |
| 4070 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4071 | // Load a new URL on a new page/tab. |
| 4072 | - (void)webPageOrderedOpen:(const GURL&)URL |
| 4073 | referrer:(const web::Referrer&)referrer |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4074 | inBackground:(BOOL)inBackground |
| 4075 | appendTo:(OpenPosition)appendTo { |
| 4076 | Tab* adjacentTab = nil; |
| 4077 | if (appendTo == kCurrentTab) |
| 4078 | adjacentTab = [_model currentTab]; |
sdefresne | a639591 | 2017-03-01 01:14:35 | [diff] [blame] | 4079 | [_model insertTabWithURL:URL |
| 4080 | referrer:referrer |
| 4081 | transition:ui::PAGE_TRANSITION_LINK |
| 4082 | opener:adjacentTab |
| 4083 | openedByDOM:NO |
| 4084 | atIndex:TabModelConstants::kTabPositionAutomatically |
| 4085 | inBackground:inBackground]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | - (void)webPageOrderedOpen:(const GURL&)url |
| 4089 | referrer:(const web::Referrer&)referrer |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4090 | inIncognito:(BOOL)inIncognito |
| 4091 | inBackground:(BOOL)inBackground |
| 4092 | appendTo:(OpenPosition)appendTo { |
Cooper Knaak | 9ae6b4f4a | 2017-07-25 18:56:00 | [diff] [blame] | 4093 | // Send either the "New Tab Opened" or "New Incognito Tab" opened to the |
Tommy Nyquist | c1d6dea1 | 2017-07-26 20:37:23 | [diff] [blame] | 4094 | // feature_engagement::Tracker based on |inIncognito|. |
| 4095 | feature_engagement::NotifyNewTabEvent(_model.browserState, inIncognito); |
Cooper Knaak | 9ae6b4f4a | 2017-07-25 18:56:00 | [diff] [blame] | 4096 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4097 | if (inIncognito == _isOffTheRecord) { |
| 4098 | [self webPageOrderedOpen:url |
| 4099 | referrer:referrer |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4100 | inBackground:inBackground |
| 4101 | appendTo:appendTo]; |
| 4102 | return; |
| 4103 | } |
| 4104 | // When sending an open command that switches modes, ensure the tab |
| 4105 | // ends up appended to the end of the model, not just next to what is |
| 4106 | // currently selected in the other mode. This is done with the |append| |
| 4107 | // parameter. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 4108 | OpenUrlCommand* command = [[OpenUrlCommand alloc] |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4109 | initWithURL:url |
| 4110 | referrer:web::Referrer() // Strip referrer when switching modes. |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4111 | inIncognito:inIncognito |
| 4112 | inBackground:inBackground |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 4113 | appendTo:kLastTab]; |
sczs | 02ad28e | 2017-08-31 11:22:15 | [diff] [blame] | 4114 | [self.dispatcher openURL:command]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4115 | } |
| 4116 | |
| 4117 | - (void)loadSessionTab:(const sessions::SessionTab*)sessionTab { |
Sylvain Defresne | f2e00d9b | 2017-08-24 10:54:05 | [diff] [blame] | 4118 | WebStateList* webStateList = [_model webStateList]; |
| 4119 | webStateList->ReplaceWebStateAt( |
| 4120 | webStateList->active_index(), |
| 4121 | session_util::CreateWebStateWithNavigationEntries( |
| 4122 | [_model browserState], sessionTab->current_navigation_index, |
| 4123 | sessionTab->navigations)); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4124 | } |
| 4125 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 4126 | #pragma mark - UrlLoader helpers |
| 4127 | |
| 4128 | // Induce an intentional crash in the browser process. |
| 4129 | - (void)induceBrowserCrash { |
| 4130 | CHECK(false); |
| 4131 | // Call another function, so that the above CHECK can't be tail-call |
| 4132 | // optimized. This ensures that this method's name will show up in the stack |
| 4133 | // for easier identification. |
| 4134 | CHECK(true); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4135 | } |
| 4136 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 4137 | #pragma mark - WebToolbarDelegate (Public) |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4138 | |
Gauthier Ambard | 45963ce2 | 2017-11-17 15:49:11 | [diff] [blame] | 4139 | - (void)locationBarDidBecomeFirstResponder { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4140 | if (_locationBarHasFocus) |
| 4141 | return; // TODO(crbug.com/244366): This should not be necessary. |
| 4142 | _locationBarHasFocus = YES; |
| 4143 | [[NSNotificationCenter defaultCenter] |
Sylvain Defresne | ed8c0db | 2017-08-31 16:29:52 | [diff] [blame] | 4144 | postNotificationName:kLocationBarBecomesFirstResponderNotification |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4145 | object:nil]; |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 4146 | [self.sideSwipeController setEnabled:NO]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4147 | if ([[_model currentTab].webController wantsKeyboardShield]) { |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 4148 | [[self view] insertSubview:self.typingShield aboveSubview:_contentArea]; |
| 4149 | [self.typingShield setAlpha:0.0]; |
| 4150 | [self.typingShield setHidden:NO]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4151 | [UIView animateWithDuration:0.3 |
| 4152 | animations:^{ |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 4153 | [self.typingShield setAlpha:1.0]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4154 | }]; |
| 4155 | } |
| 4156 | [[OmniboxGeolocationController sharedInstance] |
| 4157 | locationBarDidBecomeFirstResponder:_browserState]; |
| 4158 | } |
| 4159 | |
Gauthier Ambard | 45963ce2 | 2017-11-17 15:49:11 | [diff] [blame] | 4160 | - (void)locationBarDidResignFirstResponder { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4161 | if (!_locationBarHasFocus) |
| 4162 | return; // TODO(crbug.com/244366): This should not be necessary. |
| 4163 | _locationBarHasFocus = NO; |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 4164 | [self.sideSwipeController setEnabled:YES]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4165 | [[NSNotificationCenter defaultCenter] |
Sylvain Defresne | ed8c0db | 2017-08-31 16:29:52 | [diff] [blame] | 4166 | postNotificationName:kLocationBarResignsFirstResponderNotification |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4167 | object:nil]; |
| 4168 | [UIView animateWithDuration:0.3 |
| 4169 | animations:^{ |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 4170 | [self.typingShield setAlpha:0.0]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4171 | } |
| 4172 | completion:^(BOOL finished) { |
| 4173 | // This can happen if one quickly resigns the omnibox and then taps |
| 4174 | // on the omnibox again during this animation. If the animation is |
| 4175 | // interrupted and the toolbar controller is first responder, it's safe |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 4176 | // to assume |self.typingShield| shouldn't be hidden here. |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 4177 | if (!finished && [_toolbarCoordinator isOmniboxFirstResponder]) |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4178 | return; |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 4179 | [self.typingShield setHidden:YES]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4180 | }]; |
| 4181 | [[OmniboxGeolocationController sharedInstance] |
| 4182 | locationBarDidResignFirstResponder:_browserState]; |
| 4183 | |
| 4184 | // If a load was cancelled by an omnibox edit, but nothing is loading when |
| 4185 | // editing ends (i.e., editing was cancelled), restart the cancelled load. |
| 4186 | if (_locationBarEditCancelledLoad) { |
| 4187 | _locationBarEditCancelledLoad = NO; |
liaoyuke | 563dc4a | 2017-03-17 18:36:29 | [diff] [blame] | 4188 | |
| 4189 | web::WebState* webState = [_model currentTab].webState; |
| 4190 | if (!_toolbarModelIOS->IsLoading() && webState) |
| 4191 | webState->GetNavigationManager()->Reload(web::ReloadType::NORMAL, |
| 4192 | false /* check_for_repost */); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4193 | } |
| 4194 | } |
| 4195 | |
Gauthier Ambard | 45963ce2 | 2017-11-17 15:49:11 | [diff] [blame] | 4196 | - (void)locationBarBeganEdit { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4197 | // On handsets, if a page is currently loading it should be stopped. |
| 4198 | if (!IsIPadIdiom() && _toolbarModelIOS->IsLoading()) { |
Mark Cogan | b9aac643 | 2017-07-07 13:26:35 | [diff] [blame] | 4199 | [self.dispatcher stopLoading]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4200 | _locationBarEditCancelledLoad = YES; |
| 4201 | } |
| 4202 | } |
| 4203 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4204 | - (ToolbarModelIOS*)toolbarModelIOS { |
| 4205 | return _toolbarModelIOS.get(); |
| 4206 | } |
| 4207 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4208 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 4209 | #pragma mark - ToolsMenuConfigurationProvider |
Peter Laurens | e0b80f1 | 2017-11-21 07:52:40 | [diff] [blame] | 4210 | |
| 4211 | - (void)prepareForToolsMenuPresentationByCoordinator: |
| 4212 | (ToolsMenuCoordinator*)coordinator { |
| 4213 | DCHECK(_browserState); |
| 4214 | DCHECK(self.visible || self.dismissingModal); |
| 4215 | |
| 4216 | // Dismiss the omnibox (if open). |
| 4217 | [_toolbarCoordinator cancelOmniboxEdit]; |
| 4218 | // Dismiss the soft keyboard (if open). |
| 4219 | [[_model currentTab].webController dismissKeyboard]; |
| 4220 | // Dismiss Find in Page focus. |
| 4221 | [self updateFindBar:NO shouldFocus:NO]; |
| 4222 | |
| 4223 | if (self.incognitoTabTipBubblePresenter.isUserEngaged) { |
| 4224 | base::RecordAction(UserMetricsAction("NewIncognitoTabTipTargetSelected")); |
| 4225 | } |
| 4226 | } |
| 4227 | |
| 4228 | - (ToolsMenuConfiguration*)menuConfigurationForToolsMenuCoordinator: |
| 4229 | (ToolsMenuCoordinator*)coordinator { |
| 4230 | ToolsMenuConfiguration* configuration = |
| 4231 | [[ToolsMenuConfiguration alloc] initWithDisplayView:[self view] |
| 4232 | baseViewController:self]; |
| 4233 | configuration.requestStartTime = [NSDate date].timeIntervalSinceReferenceDate; |
| 4234 | |
| 4235 | if ([_model count] == 0) |
| 4236 | [configuration setNoOpenedTabs:YES]; |
| 4237 | |
| 4238 | if (_isOffTheRecord) |
| 4239 | [configuration setInIncognito:YES]; |
| 4240 | |
| 4241 | if (!_readingListMenuNotifier) { |
| 4242 | _readingListMenuNotifier = [[ReadingListMenuNotifier alloc] |
| 4243 | initWithReadingList:ReadingListModelFactory::GetForBrowserState( |
| 4244 | _browserState)]; |
| 4245 | } |
| 4246 | |
| 4247 | feature_engagement::Tracker* engagementTracker = |
| 4248 | feature_engagement::TrackerFactory::GetForBrowserState(_browserState); |
| 4249 | if (engagementTracker->ShouldTriggerHelpUI( |
| 4250 | feature_engagement::kIPHBadgedReadingListFeature)) { |
| 4251 | [configuration setShowReadingListNewBadge:YES]; |
| 4252 | [configuration setEngagementTracker:engagementTracker]; |
| 4253 | } |
| 4254 | [configuration setReadingListMenuNotifier:_readingListMenuNotifier]; |
| 4255 | |
| 4256 | [configuration setUserAgentType:self.userAgentType]; |
| 4257 | |
| 4258 | if (self.incognitoTabTipBubblePresenter.triggerFollowUpAction) { |
| 4259 | [configuration setHighlightNewIncognitoTabCell:YES]; |
| 4260 | [self.incognitoTabTipBubblePresenter setTriggerFollowUpAction:NO]; |
| 4261 | } |
| 4262 | |
| 4263 | return configuration; |
| 4264 | } |
| 4265 | |
| 4266 | - (BOOL)shouldHighlightBookmarkButtonForToolsMenuCoordinator: |
| 4267 | (ToolsMenuCoordinator*)coordinator { |
| 4268 | return [_model currentTab] ? _toolbarModelIOS->IsCurrentTabBookmarked() : NO; |
| 4269 | } |
| 4270 | |
| 4271 | - (BOOL)shouldShowFindBarForToolsMenuCoordinator: |
| 4272 | (ToolsMenuCoordinator*)coordinator { |
| 4273 | return [_model currentTab] ? self.canShowFindBar : NO; |
| 4274 | } |
| 4275 | |
| 4276 | - (BOOL)shouldShowShareMenuForToolsMenuCoordinator: |
| 4277 | (ToolsMenuCoordinator*)coordinator { |
| 4278 | return [_model currentTab] ? self.canShowShareMenu : NO; |
| 4279 | } |
| 4280 | |
| 4281 | - (BOOL)isTabLoadingForToolsMenuCoordinator:(ToolsMenuCoordinator*)coordinator { |
| 4282 | return ([_model currentTab] && !IsIPadIdiom()) ? _toolbarModelIOS->IsLoading() |
| 4283 | : NO; |
| 4284 | } |
| 4285 | |
Mark Cogan | 6ebbde0 | 2017-07-07 12:50:13 | [diff] [blame] | 4286 | #pragma mark - BrowserCommands |
| 4287 | |
| 4288 | - (void)goBack { |
| 4289 | [[_model currentTab] goBack]; |
| 4290 | } |
| 4291 | |
| 4292 | - (void)goForward { |
| 4293 | [[_model currentTab] goForward]; |
| 4294 | } |
| 4295 | |
Mark Cogan | b9aac643 | 2017-07-07 13:26:35 | [diff] [blame] | 4296 | - (void)stopLoading { |
| 4297 | [_model currentTab].webState->Stop(); |
| 4298 | } |
| 4299 | |
| 4300 | - (void)reload { |
| 4301 | web::WebState* webState = [_model currentTab].webState; |
| 4302 | if (webState) { |
| 4303 | // |check_for_repost| is true because the reload is explicitly initiated |
| 4304 | // by the user. |
| 4305 | webState->GetNavigationManager()->Reload(web::ReloadType::NORMAL, |
| 4306 | true /* check_for_repost */); |
| 4307 | } |
| 4308 | } |
| 4309 | |
Mark Cogan | 8e79102 | 2017-07-10 09:55:35 | [diff] [blame] | 4310 | - (void)bookmarkPage { |
| 4311 | [self initializeBookmarkInteractionController]; |
| 4312 | [_bookmarkInteractionController |
| 4313 | presentBookmarkForTab:[_model currentTab] |
sczs | 19e8f3d | 2017-10-03 17:54:06 | [diff] [blame] | 4314 | currentlyBookmarked:_toolbarModelIOS->IsCurrentTabBookmarkedByUser()]; |
Mark Cogan | 8e79102 | 2017-07-10 09:55:35 | [diff] [blame] | 4315 | } |
| 4316 | |
Mark Cogan | dfcdea7 | 2017-07-18 13:47:38 | [diff] [blame] | 4317 | - (void)openNewTab:(OpenNewTabCommand*)command { |
| 4318 | if (self.isOffTheRecord != command.incognito) { |
edchin | 3ab78ff | 2017-11-13 19:13:14 | [diff] [blame] | 4319 | // Must take a snapshot of the tab before we switch the incognito mode |
| 4320 | // because the currentTab will change after the switch. |
| 4321 | Tab* currentTab = [_model currentTab]; |
| 4322 | if (currentTab) { |
Sylvain Defresne | 17b8aa4 | 2017-12-21 16:17:17 | [diff] [blame] | 4323 | SnapshotTabHelper::FromWebState(currentTab.webState) |
| 4324 | ->UpdateSnapshot(/*with_overlays=*/true, /*visible_frame_only=*/true); |
edchin | 3ab78ff | 2017-11-13 19:13:14 | [diff] [blame] | 4325 | } |
Mark Cogan | dfcdea7 | 2017-07-18 13:47:38 | [diff] [blame] | 4326 | // Not for this browser state, send it on its way. |
| 4327 | [self.dispatcher switchModesAndOpenNewTab:command]; |
| 4328 | return; |
| 4329 | } |
| 4330 | |
Cooper Knaak | 9ae6b4f4a | 2017-07-25 18:56:00 | [diff] [blame] | 4331 | // Either send or don't send the "New Tab Opened" or "Incognito Tab Opened" |
Tommy Nyquist | c1d6dea1 | 2017-07-26 20:37:23 | [diff] [blame] | 4332 | // events to the feature_engagement::Tracker based on |command.userInitiated| |
| 4333 | // and |command.incognito|. |
| 4334 | feature_engagement::NotifyNewTabEventForCommand(_browserState, command); |
Cooper Knaak | 9ae6b4f4a | 2017-07-25 18:56:00 | [diff] [blame] | 4335 | |
Mark Cogan | dfcdea7 | 2017-07-18 13:47:38 | [diff] [blame] | 4336 | NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate]; |
| 4337 | BOOL offTheRecord = self.isOffTheRecord; |
Olivier Robin | d508a563 | 2017-07-19 16:29:49 | [diff] [blame] | 4338 | ProceduralBlock oldForegroundTabWasAddedCompletionBlock = |
| 4339 | self.foregroundTabWasAddedCompletionBlock; |
Louis Romero | 960a12f | 2017-11-30 19:08:59 | [diff] [blame] | 4340 | __weak BrowserViewController* weakSelf = self; |
Mark Cogan | dfcdea7 | 2017-07-18 13:47:38 | [diff] [blame] | 4341 | self.foregroundTabWasAddedCompletionBlock = ^{ |
Olivier Robin | d508a563 | 2017-07-19 16:29:49 | [diff] [blame] | 4342 | if (oldForegroundTabWasAddedCompletionBlock) { |
| 4343 | oldForegroundTabWasAddedCompletionBlock(); |
| 4344 | } |
Mark Cogan | dfcdea7 | 2017-07-18 13:47:38 | [diff] [blame] | 4345 | double duration = [NSDate timeIntervalSinceReferenceDate] - startTime; |
| 4346 | base::TimeDelta timeDelta = base::TimeDelta::FromSecondsD(duration); |
| 4347 | if (offTheRecord) { |
| 4348 | UMA_HISTOGRAM_TIMES("Toolbar.Menu.NewIncognitoTabPresentationDuration", |
| 4349 | timeDelta); |
| 4350 | } else { |
| 4351 | UMA_HISTOGRAM_TIMES("Toolbar.Menu.NewTabPresentationDuration", timeDelta); |
| 4352 | } |
Louis Romero | 960a12f | 2017-11-30 19:08:59 | [diff] [blame] | 4353 | if (command.shouldFocusOmnibox) { |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 4354 | [weakSelf.dispatcher focusOmnibox]; |
Louis Romero | 960a12f | 2017-11-30 19:08:59 | [diff] [blame] | 4355 | } |
Mark Cogan | dfcdea7 | 2017-07-18 13:47:38 | [diff] [blame] | 4356 | }; |
| 4357 | |
| 4358 | [self setLastTapPoint:command]; |
Rohit Rao | 2e22b8d | 2017-11-07 19:54:54 | [diff] [blame] | 4359 | // When the tab switcher presentation experiment is enabled, the new tab can |
| 4360 | // be opened before BVC has been made visible onscreen. Test for this case by |
| 4361 | // checking if the parent container VC is currently in the process of being |
| 4362 | // presented. |
| 4363 | DCHECK(self.visible || self.dismissingModal || |
| 4364 | (TabSwitcherPresentsBVCEnabled() && |
| 4365 | self.parentViewController.isBeingPresented)); |
edchin | 3ab78ff | 2017-11-13 19:13:14 | [diff] [blame] | 4366 | |
| 4367 | // In most cases, we want to take a snapshot of the current tab before opening |
| 4368 | // a new tab. However, if the current tab is not fully visible (did not finish |
| 4369 | // |-viewDidAppear:|, then we must not take an empty snapshot, replacing an |
| 4370 | // existing snapshot for the tab. This can happen when a new regular tab is |
| 4371 | // opened from an incognito tab. A different BVC is displayed, which may not |
| 4372 | // have enough time to finish appearing before a snapshot is requested. |
Mark Cogan | dfcdea7 | 2017-07-18 13:47:38 | [diff] [blame] | 4373 | Tab* currentTab = [_model currentTab]; |
edchin | 3ab78ff | 2017-11-13 19:13:14 | [diff] [blame] | 4374 | if (currentTab && self.viewVisible) { |
Sylvain Defresne | 17b8aa4 | 2017-12-21 16:17:17 | [diff] [blame] | 4375 | SnapshotTabHelper::FromWebState(currentTab.webState) |
| 4376 | ->UpdateSnapshot(/*with_overlays=*/true, /*visible_frame_only=*/true); |
Mark Cogan | dfcdea7 | 2017-07-18 13:47:38 | [diff] [blame] | 4377 | } |
| 4378 | [self addSelectedTabWithURL:GURL(kChromeUINewTabURL) |
| 4379 | transition:ui::PAGE_TRANSITION_TYPED]; |
| 4380 | } |
| 4381 | |
Mark Cogan | 12389500 | 2017-07-20 12:54:06 | [diff] [blame] | 4382 | - (void)printTab { |
| 4383 | Tab* currentTab = [_model currentTab]; |
| 4384 | // The UI should prevent users from printing non-printable pages. However, a |
| 4385 | // redirection to an un-printable page can happen before it is reflected in |
| 4386 | // the UI. |
| 4387 | if (![currentTab viewForPrinting]) { |
| 4388 | TriggerHapticFeedbackForNotification(UINotificationFeedbackTypeError); |
edchin | eeb4d42 | 2017-10-02 17:39:36 | [diff] [blame] | 4389 | [self showSnackbar:l10n_util::GetNSString(IDS_IOS_CANNOT_PRINT_PAGE_ERROR)]; |
Mark Cogan | 12389500 | 2017-07-20 12:54:06 | [diff] [blame] | 4390 | return; |
| 4391 | } |
| 4392 | DCHECK(_browserState); |
| 4393 | if (!_printController) { |
| 4394 | _printController = [[PrintController alloc] |
| 4395 | initWithContextGetter:_browserState->GetRequestContext()]; |
| 4396 | } |
| 4397 | [_printController printView:[currentTab viewForPrinting] |
| 4398 | withTitle:[currentTab title] |
| 4399 | viewController:self]; |
| 4400 | } |
| 4401 | |
Mark Cogan | fa25b05 | 2017-07-20 17:31:03 | [diff] [blame] | 4402 | - (void)addToReadingList:(ReadingListAddCommand*)command { |
| 4403 | [self addToReadingListURL:[command URL] title:[command title]]; |
| 4404 | } |
| 4405 | |
sczs | 3a8c860 | 2017-08-01 20:14:08 | [diff] [blame] | 4406 | - (void)showReadingList { |
| 4407 | _readingListCoordinator = [[ReadingListCoordinator alloc] |
| 4408 | initWithBaseViewController:self |
| 4409 | browserState:self.browserState |
| 4410 | loader:self]; |
| 4411 | |
| 4412 | [_readingListCoordinator start]; |
| 4413 | } |
| 4414 | |
Jean-François Geyelin | edef955 | 2017-08-07 09:56:56 | [diff] [blame] | 4415 | - (void)preloadVoiceSearch { |
| 4416 | // Preload VoiceSearchController and views and view controllers needed |
| 4417 | // for voice search. |
| 4418 | [self ensureVoiceSearchControllerCreated]; |
| 4419 | _voiceSearchController->PrepareToAppear(); |
| 4420 | } |
| 4421 | |
edchin | c572072 | 2017-08-14 22:06:31 | [diff] [blame] | 4422 | #if !defined(NDEBUG) |
| 4423 | - (void)viewSource { |
| 4424 | Tab* tab = [_model currentTab]; |
| 4425 | DCHECK(tab); |
| 4426 | CRWWebController* webController = tab.webController; |
| 4427 | NSString* script = @"document.documentElement.outerHTML;"; |
| 4428 | __weak Tab* weakTab = tab; |
| 4429 | __weak BrowserViewController* weakSelf = self; |
| 4430 | web::JavaScriptResultBlock completionHandlerBlock = ^(id result, NSError*) { |
| 4431 | Tab* strongTab = weakTab; |
| 4432 | if (!strongTab) |
| 4433 | return; |
| 4434 | if (![result isKindOfClass:[NSString class]]) |
| 4435 | result = @"Not an HTML page"; |
| 4436 | std::string base64HTML; |
| 4437 | base::Base64Encode(base::SysNSStringToUTF8(result), &base64HTML); |
| 4438 | GURL URL(std::string("data:text/plain;charset=utf-8;base64,") + base64HTML); |
Sylvain Defresne | e7f2c8a | 2017-10-17 02:39:19 | [diff] [blame] | 4439 | web::Referrer referrer(strongTab.webState->GetLastCommittedURL(), |
edchin | c572072 | 2017-08-14 22:06:31 | [diff] [blame] | 4440 | web::ReferrerPolicyDefault); |
| 4441 | |
| 4442 | [[weakSelf tabModel] |
| 4443 | insertTabWithURL:URL |
| 4444 | referrer:referrer |
| 4445 | transition:ui::PAGE_TRANSITION_LINK |
| 4446 | opener:strongTab |
| 4447 | openedByDOM:YES |
| 4448 | atIndex:TabModelConstants::kTabPositionAutomatically |
| 4449 | inBackground:NO]; |
| 4450 | }; |
| 4451 | [webController executeJavaScript:script |
| 4452 | completionHandler:completionHandlerBlock]; |
| 4453 | } |
| 4454 | #endif // !defined(NDEBUG) |
| 4455 | |
edchin | 2134c04 | 2017-08-18 13:57:35 | [diff] [blame] | 4456 | // TODO(crbug.com/634507) Remove base::TimeXXX::ToInternalValue(). |
| 4457 | - (void)showRateThisAppDialog { |
| 4458 | DCHECK(!_rateThisAppDialog); |
| 4459 | |
| 4460 | // Store the current timestamp whenever this dialog is shown. |
| 4461 | _browserState->GetPrefs()->SetInt64(prefs::kRateThisAppDialogLastShownTime, |
| 4462 | base::Time::Now().ToInternalValue()); |
| 4463 | |
Gregory Chatzinoff | f39ec516 | 2017-10-05 20:28:53 | [diff] [blame] | 4464 | // iOS11 no longer supports the itms link to the app store. So, use a deep |
| 4465 | // link for iOS11 and the itms link for prior versions. |
| 4466 | NSURL* storeURL; |
| 4467 | if (base::ios::IsRunningOnIOS11OrLater()) { |
| 4468 | storeURL = |
| 4469 | [NSURL URLWithString:(@"https://itunes.apple.com/us/app/" |
| 4470 | @"google-chrome-the-fast-and-secure-web-browser/" |
| 4471 | @"id535886823?action=write-review")]; |
| 4472 | } else { |
| 4473 | storeURL = [NSURL |
| 4474 | URLWithString:(@"itms-apps://itunes.apple.com/WebObjects/" |
| 4475 | @"MZStore.woa/wa/" |
| 4476 | @"viewContentsUserReviews?type=Purple+Software&id=" |
| 4477 | @"535886823&pt=9008&ct=rating")]; |
| 4478 | } |
edchin | 2134c04 | 2017-08-18 13:57:35 | [diff] [blame] | 4479 | |
| 4480 | base::RecordAction(base::UserMetricsAction("IOSRateThisAppDialogShown")); |
Elodie Banel | aa5ab43 | 2017-09-28 14:42:01 | [diff] [blame] | 4481 | [self clearPresentedStateWithCompletion:nil dismissOmnibox:YES]; |
edchin | 2134c04 | 2017-08-18 13:57:35 | [diff] [blame] | 4482 | |
| 4483 | _rateThisAppDialog = ios::GetChromeBrowserProvider()->CreateAppRatingPrompt(); |
| 4484 | [_rateThisAppDialog setAppStoreURL:storeURL]; |
| 4485 | [_rateThisAppDialog setDelegate:self]; |
| 4486 | [_rateThisAppDialog show]; |
| 4487 | } |
| 4488 | |
Gregory Chatzinoff | 3f40c154 | 2017-08-30 07:50:04 | [diff] [blame] | 4489 | - (void)showFindInPage { |
| 4490 | if (!self.canShowFindBar) |
| 4491 | return; |
| 4492 | |
| 4493 | if (!_findBarController) { |
| 4494 | _findBarController = |
| 4495 | [[FindBarControllerIOS alloc] initWithIncognito:_isOffTheRecord]; |
| 4496 | _findBarController.dispatcher = self.dispatcher; |
| 4497 | } |
| 4498 | |
| 4499 | Tab* tab = [_model currentTab]; |
| 4500 | DCHECK(tab); |
| 4501 | auto* helper = FindTabHelper::FromWebState(tab.webState); |
| 4502 | DCHECK(!helper->IsFindUIActive()); |
| 4503 | helper->SetFindUIActive(true); |
| 4504 | [self showFindBarWithAnimation:YES selectText:YES shouldFocus:YES]; |
| 4505 | } |
| 4506 | |
| 4507 | - (void)closeFindInPage { |
| 4508 | __weak BrowserViewController* weakSelf = self; |
| 4509 | Tab* currentTab = [_model currentTab]; |
| 4510 | if (currentTab) { |
| 4511 | FindTabHelper::FromWebState(currentTab.webState)->StopFinding(^{ |
| 4512 | [weakSelf updateFindBar:NO shouldFocus:NO]; |
| 4513 | }); |
| 4514 | } |
| 4515 | } |
| 4516 | |
| 4517 | - (void)searchFindInPage { |
| 4518 | DCHECK([_model currentTab]); |
| 4519 | auto* helper = FindTabHelper::FromWebState([_model currentTab].webState); |
| 4520 | __weak BrowserViewController* weakSelf = self; |
| 4521 | helper->StartFinding( |
| 4522 | [_findBarController searchTerm], ^(FindInPageModel* model) { |
| 4523 | BrowserViewController* strongSelf = weakSelf; |
| 4524 | if (!strongSelf) { |
| 4525 | return; |
| 4526 | } |
| 4527 | [strongSelf->_findBarController updateResultsCount:model]; |
| 4528 | }); |
| 4529 | |
| 4530 | if (!_isOffTheRecord) |
| 4531 | helper->PersistSearchTerm(); |
| 4532 | } |
| 4533 | |
| 4534 | - (void)findNextStringInPage { |
| 4535 | Tab* currentTab = [_model currentTab]; |
| 4536 | DCHECK(currentTab); |
| 4537 | // TODO(crbug.com/603524): Reshow find bar if necessary. |
| 4538 | FindTabHelper::FromWebState(currentTab.webState) |
| 4539 | ->ContinueFinding(FindTabHelper::FORWARD, ^(FindInPageModel* model) { |
| 4540 | [_findBarController updateResultsCount:model]; |
| 4541 | }); |
| 4542 | } |
| 4543 | |
| 4544 | - (void)findPreviousStringInPage { |
| 4545 | Tab* currentTab = [_model currentTab]; |
| 4546 | DCHECK(currentTab); |
| 4547 | // TODO(crbug.com/603524): Reshow find bar if necessary. |
| 4548 | FindTabHelper::FromWebState(currentTab.webState) |
| 4549 | ->ContinueFinding(FindTabHelper::REVERSE, ^(FindInPageModel* model) { |
| 4550 | [_findBarController updateResultsCount:model]; |
| 4551 | }); |
| 4552 | } |
| 4553 | |
edchin | f84b250 | 2017-08-31 21:30:45 | [diff] [blame] | 4554 | - (void)showHelpPage { |
| 4555 | GURL helpUrl(l10n_util::GetStringUTF16(IDS_IOS_TOOLS_MENU_HELP_URL)); |
| 4556 | [self webPageOrderedOpen:helpUrl |
| 4557 | referrer:web::Referrer() |
| 4558 | inBackground:NO |
| 4559 | appendTo:kCurrentTab]; |
| 4560 | } |
| 4561 | |
edchin | b59b560 | 2017-09-01 15:00:20 | [diff] [blame] | 4562 | - (void)showBookmarksManager { |
Gauthier Ambard | 5bb5f7a | 2017-09-06 12:58:10 | [diff] [blame] | 4563 | if (!PresentNTPPanelModally()) { |
edchin | b59b560 | 2017-09-01 15:00:20 | [diff] [blame] | 4564 | [self showAllBookmarks]; |
| 4565 | } else { |
| 4566 | [self initializeBookmarkInteractionController]; |
| 4567 | [_bookmarkInteractionController presentBookmarks]; |
| 4568 | } |
| 4569 | } |
| 4570 | |
edchin | 8ee0807d | 2017-09-01 23:52:47 | [diff] [blame] | 4571 | - (void)showRecentTabs { |
Gauthier Ambard | 5bb5f7a | 2017-09-06 12:58:10 | [diff] [blame] | 4572 | if (!PresentNTPPanelModally()) { |
edchin | 8ee0807d | 2017-09-01 23:52:47 | [diff] [blame] | 4573 | [self showNTPPanel:ntp_home::RECENT_TABS_PANEL]; |
| 4574 | } else { |
| 4575 | if (!self.recentTabsCoordinator) { |
| 4576 | self.recentTabsCoordinator = [[RecentTabsHandsetCoordinator alloc] |
| 4577 | initWithBaseViewController:self]; |
| 4578 | self.recentTabsCoordinator.loader = self; |
| 4579 | self.recentTabsCoordinator.dispatcher = self.dispatcher; |
| 4580 | self.recentTabsCoordinator.browserState = _browserState; |
| 4581 | } |
| 4582 | [self.recentTabsCoordinator start]; |
| 4583 | } |
| 4584 | } |
| 4585 | |
Mark Cogan | 6de7e9a | 2017-09-06 12:57:21 | [diff] [blame] | 4586 | - (void)requestDesktopSite { |
| 4587 | [[_model currentTab] reloadWithUserAgentType:web::UserAgentType::DESKTOP]; |
| 4588 | } |
| 4589 | |
| 4590 | - (void)requestMobileSite { |
| 4591 | [[_model currentTab] reloadWithUserAgentType:web::UserAgentType::MOBILE]; |
| 4592 | } |
| 4593 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4594 | - (void)closeCurrentTab { |
| 4595 | Tab* currentTab = [_model currentTab]; |
| 4596 | NSUInteger tabIndex = [_model indexOfTab:currentTab]; |
| 4597 | if (tabIndex == NSNotFound) |
| 4598 | return; |
| 4599 | |
jif | 7fed812 | 2017-02-08 13:15:25 | [diff] [blame] | 4600 | // TODO(crbug.com/688003): Evaluate if a screenshot of the tab is needed on |
| 4601 | // iPad. |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4602 | UIImageView* exitingPage = [self pageOpenCloseAnimationView]; |
| 4603 | exitingPage.image = |
Sylvain Defresne | 17b8aa4 | 2017-12-21 16:17:17 | [diff] [blame] | 4604 | SnapshotTabHelper::FromWebState(currentTab.webState) |
| 4605 | ->UpdateSnapshot(/*with_overlays=*/true, /*visible_frame_only=*/true); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4606 | |
| 4607 | // Close the actual tab, and add its image as a subview. |
| 4608 | [_model closeTabAtIndex:tabIndex]; |
| 4609 | |
| 4610 | // Do not animate close in iPad. |
| 4611 | if (!IsIPadIdiom()) { |
| 4612 | [_contentArea addSubview:exitingPage]; |
Sylvain Defresne | ed8c0db | 2017-08-31 16:29:52 | [diff] [blame] | 4613 | page_animation_util::AnimateOutWithCompletion( |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4614 | exitingPage, 0, YES, IsPortrait(), ^{ |
| 4615 | [exitingPage removeFromSuperview]; |
| 4616 | }); |
| 4617 | } |
| 4618 | } |
| 4619 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 4620 | #pragma mark - ToolbarOwner (Public) |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4621 | |
Kurt Horimoto | ea429dd | 2017-11-28 02:24:30 | [diff] [blame] | 4622 | - (CGFloat)toolbarHeight { |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 4623 | return self.headerHeight; |
Kurt Horimoto | ea429dd | 2017-11-28 02:24:30 | [diff] [blame] | 4624 | } |
| 4625 | |
Gauthier Ambard | 04ddb51 | 2017-11-07 09:14:16 | [diff] [blame] | 4626 | - (CGRect)toolbarFrame { |
sczs | 42f7f748 | 2017-11-08 01:13:27 | [diff] [blame] | 4627 | return _toolbarCoordinator.toolbarViewController.view.frame; |
Gauthier Ambard | 04ddb51 | 2017-11-07 09:14:16 | [diff] [blame] | 4628 | } |
| 4629 | |
Gauthier Ambard | 996d9b1 | 2017-11-06 09:39:21 | [diff] [blame] | 4630 | - (id<ToolbarSnapshotProviding>)toolbarSnapshotProvider { |
| 4631 | id<ToolbarSnapshotProviding> toolbarSnapshotProvider = nil; |
sczs | 42f7f748 | 2017-11-08 01:13:27 | [diff] [blame] | 4632 | if (_toolbarCoordinator.toolbarViewController.view.hidden) { |
Gauthier Ambard | 996d9b1 | 2017-11-06 09:39:21 | [diff] [blame] | 4633 | Tab* currentTab = [_model currentTab]; |
| 4634 | if (currentTab.webState && |
| 4635 | UrlHasChromeScheme(currentTab.webState->GetLastCommittedURL())) { |
| 4636 | // Use the native content controller's toolbar when the BVC's is hidden. |
| 4637 | id nativeController = [self nativeControllerForTab:currentTab]; |
| 4638 | if ([nativeController conformsToProtocol:@protocol(ToolbarOwner)]) { |
| 4639 | toolbarSnapshotProvider = [nativeController toolbarSnapshotProvider]; |
| 4640 | } |
| 4641 | } |
| 4642 | } else { |
| 4643 | toolbarSnapshotProvider = _toolbarCoordinator; |
| 4644 | } |
| 4645 | return toolbarSnapshotProvider; |
| 4646 | } |
| 4647 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4648 | #pragma mark - TabModelObserver methods |
| 4649 | |
| 4650 | // Observer method, tab inserted. |
| 4651 | - (void)tabModel:(TabModel*)model |
| 4652 | didInsertTab:(Tab*)tab |
| 4653 | atIndex:(NSUInteger)modelIndex |
| 4654 | inForeground:(BOOL)fg { |
| 4655 | DCHECK(tab); |
| 4656 | [self installDelegatesForTab:tab]; |
| 4657 | |
| 4658 | if (fg) { |
Mohamad Ahmadi | 7d09ec3 | 2017-07-11 22:32:19 | [diff] [blame] | 4659 | [_paymentRequestManager setActiveWebState:tab.webState]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4660 | } |
| 4661 | } |
| 4662 | |
| 4663 | // Observer method, active tab changed. |
| 4664 | - (void)tabModel:(TabModel*)model |
| 4665 | didChangeActiveTab:(Tab*)newTab |
| 4666 | previousTab:(Tab*)previousTab |
| 4667 | atIndex:(NSUInteger)index { |
| 4668 | // TODO(rohitrao): tabSelected expects to always be called with a non-nil tab. |
| 4669 | // Currently this observer method is always called with a non-nil |newTab|, |
| 4670 | // but that may change in the future. Remove this DCHECK when it does. |
| 4671 | DCHECK(newTab); |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 4672 | if (_infoBarContainer) { |
Rohit Rao | af46af9 | 2017-08-10 12:52:30 | [diff] [blame] | 4673 | DCHECK(newTab.webState); |
| 4674 | infobars::InfoBarManager* infoBarManager = |
| 4675 | InfoBarManagerImpl::FromWebState(newTab.webState); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4676 | _infoBarContainer->ChangeInfoBarManager(infoBarManager); |
Mark Cogan | b12391c6 | 2017-12-28 13:35:06 | [diff] [blame] | 4677 | |
| 4678 | // Dismiss the language selector, if any; this is a no-op when there's |
| 4679 | // no language selector presented. |
| 4680 | [_languageSelectionCoordinator dismissLanguageSelector]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4681 | } |
| 4682 | [self updateVoiceSearchBarVisibilityAnimated:NO]; |
| 4683 | |
Mohamad Ahmadi | 7d09ec3 | 2017-07-11 22:32:19 | [diff] [blame] | 4684 | [_paymentRequestManager setActiveWebState:newTab.webState]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4685 | |
Gauthier Ambard | 6439690 | 2017-12-08 10:14:58 | [diff] [blame] | 4686 | [self tabSelected:newTab notifyToolbar:YES]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4687 | } |
| 4688 | |
Mark Cogan | 5b49464 | 2018-01-02 12:55:46 | [diff] [blame] | 4689 | - (void)tabModel:(TabModel*)model willStartLoadingTab:(Tab*)tab { |
| 4690 | // Stop any Find in Page searches and close the find bar when navigating to a |
| 4691 | // new page. |
| 4692 | [self closeFindInPage]; |
| 4693 | } |
| 4694 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4695 | - (void)tabModel:(TabModel*)model didChangeTab:(Tab*)tab { |
| 4696 | DCHECK(tab && ([_model indexOfTab:tab] != NSNotFound)); |
| 4697 | if (tab == [_model currentTab]) { |
| 4698 | [self updateToolbar]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4699 | } |
| 4700 | } |
| 4701 | |
Mark Cogan | 5b49464 | 2018-01-02 12:55:46 | [diff] [blame] | 4702 | - (void)tabModel:(TabModel*)model didStartLoadingTab:(Tab*)tab { |
| 4703 | if (tab == [_model currentTab]) { |
| 4704 | if (![self isTabNativePage:tab]) { |
| 4705 | [_toolbarCoordinator currentPageLoadStarted]; |
| 4706 | } |
| 4707 | [self updateVoiceSearchBarVisibilityAnimated:NO]; |
| 4708 | } |
| 4709 | } |
| 4710 | |
| 4711 | - (void)tabModel:(TabModel*)model |
| 4712 | didFinishLoadingTab:(Tab*)tab |
Mark Cogan | 50a4c06a | 2018-01-02 18:26:20 | [diff] [blame] | 4713 | success:(BOOL)success { |
Mark Cogan | 5b49464 | 2018-01-02 12:55:46 | [diff] [blame] | 4714 | [self tabLoadComplete:tab withSuccess:success]; |
Mark Cogan | fc591c8c | 2018-01-02 16:07:00 | [diff] [blame^] | 4715 | if (IsIPadIdiom()) { |
| 4716 | UIUserInterfaceSizeClass sizeClass = |
| 4717 | self.view.window.traitCollection.horizontalSizeClass; |
| 4718 | [SizeClassRecorder pageLoadedWithHorizontalSizeClass:sizeClass]; |
| 4719 | } |
Mark Cogan | 5b49464 | 2018-01-02 12:55:46 | [diff] [blame] | 4720 | } |
| 4721 | |
| 4722 | - (void)tabModel:(TabModel*)model |
| 4723 | newTabWillOpen:(Tab*)tab |
| 4724 | inBackground:(BOOL)background { |
| 4725 | DCHECK(tab); |
| 4726 | _temporaryNativeController = nil; |
| 4727 | |
| 4728 | // When adding new tabs, check what kind of reminder infobar should |
| 4729 | // be added to the new tab. Try to add only one of them. |
| 4730 | // This check is done when a new tab is added either through the Tools Menu |
| 4731 | // "New Tab" or through "New Tab" in Stack View Controller. This method |
| 4732 | // is called after a new tab has added and finished initial navigation. |
| 4733 | // If this is added earlier, the initial navigation may end up clearing |
| 4734 | // the infobar(s) that are just added. See http://crbug/340250 for details. |
| 4735 | web::WebState* webState = tab.webState; |
| 4736 | DCHECK(webState); |
| 4737 | |
| 4738 | infobars::InfoBarManager* infoBarManager = |
| 4739 | InfoBarManagerImpl::FromWebState(webState); |
| 4740 | [[UpgradeCenter sharedInstance] addInfoBarToManager:infoBarManager |
| 4741 | forTabId:[tab tabId]]; |
| 4742 | if (!ReSignInInfoBarDelegate::Create(_browserState, tab, |
| 4743 | self /* id<SigninPresenter> */)) { |
| 4744 | DisplaySyncErrors(_browserState, tab, self /* id<SyncPresenter> */); |
| 4745 | } |
| 4746 | |
| 4747 | // The rest of this function initiates the new tab animation, which is |
| 4748 | // phone-specific. Call the foreground tab added completion block; for |
| 4749 | // iPhones, this will get executed after the animation has finished. |
| 4750 | if (IsIPadIdiom()) { |
| 4751 | if (self.foregroundTabWasAddedCompletionBlock) { |
| 4752 | // This callback is called before webState is activated (on |
| 4753 | // kTabModelNewTabWillOpenNotification notification). Dispatch the |
| 4754 | // callback asynchronously to be sure the activation is complete. |
| 4755 | dispatch_async(dispatch_get_main_queue(), ^() { |
| 4756 | // Test existence again as the block may have been deleted. |
| 4757 | if (self.foregroundTabWasAddedCompletionBlock) { |
| 4758 | self.foregroundTabWasAddedCompletionBlock(); |
| 4759 | self.foregroundTabWasAddedCompletionBlock = nil; |
| 4760 | } |
| 4761 | }); |
| 4762 | } |
| 4763 | return; |
| 4764 | } |
| 4765 | |
| 4766 | // Do nothing if browsing is currently suspended. The BVC will set everything |
| 4767 | // up correctly when browsing resumes. |
| 4768 | if (!self.visible || ![_model webUsageEnabled]) |
| 4769 | return; |
| 4770 | |
| 4771 | // Block that starts voice search at the end of new Tab animation if |
| 4772 | // necessary. |
| 4773 | ProceduralBlock startVoiceSearchIfNecessaryBlock = ^void() { |
| 4774 | if (_startVoiceSearchAfterNewTabAnimation) { |
| 4775 | _startVoiceSearchAfterNewTabAnimation = NO; |
| 4776 | [self startVoiceSearchWithOriginView:nil]; |
| 4777 | } |
| 4778 | }; |
| 4779 | |
| 4780 | self.inNewTabAnimation = YES; |
| 4781 | if (!background) { |
| 4782 | UIView* animationParentView = _contentArea; |
| 4783 | // Create the new page image, and load with the new tab snapshot except if |
| 4784 | // it is the NTP. |
| 4785 | CGFloat newPageOffset = 0; |
| 4786 | UIView* newPage; |
| 4787 | CGFloat offset = 0; |
| 4788 | if (tab.webState->GetLastCommittedURL() == kChromeUINewTabURL && |
| 4789 | !_isOffTheRecord && !IsIPadIdiom()) { |
| 4790 | offset = 0; |
| 4791 | animationParentView = self.view; |
| 4792 | newPage = tab.view; |
| 4793 | newPage.userInteractionEnabled = NO; |
| 4794 | // Compute a frame for the new page by removing the status bar height from |
| 4795 | // the bounds of |self.view|. |
| 4796 | CGRect viewBounds, remainder; |
| 4797 | CGRectDivide(self.view.bounds, &remainder, &viewBounds, StatusBarHeight(), |
| 4798 | CGRectMinYEdge); |
| 4799 | newPage.frame = viewBounds; |
| 4800 | } else { |
| 4801 | UIImageView* pageScreenshot = [self pageOpenCloseAnimationView]; |
| 4802 | tab.view.frame = _contentArea.bounds; |
| 4803 | pageScreenshot.image = SnapshotTabHelper::FromWebState(tab.webState) |
| 4804 | ->UpdateSnapshot(/*with_overlays=*/true, |
| 4805 | /*visible_frame_only=*/true); |
| 4806 | newPage = pageScreenshot; |
| 4807 | offset = |
| 4808 | pageScreenshot.frame.size.height - pageScreenshot.image.size.height; |
| 4809 | } |
| 4810 | newPageOffset = newPage.frame.origin.y; |
| 4811 | |
| 4812 | [animationParentView addSubview:newPage]; |
| 4813 | CGPoint origin = [self lastTapPoint]; |
| 4814 | page_animation_util::AnimateInPaperWithAnimationAndCompletion( |
| 4815 | newPage, -newPageOffset, offset, origin, _isOffTheRecord, NULL, ^{ |
| 4816 | [tab view].frame = _contentArea.bounds; |
| 4817 | newPage.userInteractionEnabled = YES; |
| 4818 | [newPage removeFromSuperview]; |
| 4819 | self.inNewTabAnimation = NO; |
| 4820 | // Use the model's currentTab here because it is possible that it can |
| 4821 | // be reset to a new value before the new Tab animation finished (e.g. |
| 4822 | // if another Tab shows a dialog via |dialogPresenter|). However, that |
| 4823 | // tab's view hasn't been displayed yet because it was in a new tab |
| 4824 | // animation. |
| 4825 | Tab* currentTab = [_model currentTab]; |
| 4826 | if (currentTab) { |
| 4827 | [self tabSelected:currentTab notifyToolbar:NO]; |
| 4828 | } |
| 4829 | startVoiceSearchIfNecessaryBlock(); |
| 4830 | |
| 4831 | if (self.foregroundTabWasAddedCompletionBlock) { |
| 4832 | self.foregroundTabWasAddedCompletionBlock(); |
| 4833 | self.foregroundTabWasAddedCompletionBlock = nil; |
| 4834 | } |
| 4835 | }); |
| 4836 | } else { |
| 4837 | // SnapshotTabHelper::UpdateSnapshot will force a screen redraw, so take the |
| 4838 | // snapshot before adding the views needed for the background animation. |
| 4839 | Tab* topTab = [_model currentTab]; |
| 4840 | UIImage* image = |
| 4841 | SnapshotTabHelper::FromWebState(topTab.webState) |
| 4842 | ->UpdateSnapshot(/*with_overlays=*/true, |
| 4843 | /*visible_frame_only=*/self.isToolbarOnScreen); |
| 4844 | |
| 4845 | // The size of the |image| above can be wrong if the snapshot fails, grab |
| 4846 | // the correct size here. |
| 4847 | CGRect imageFrame = CGRectZero; |
| 4848 | if (self.isToolbarOnScreen) { |
| 4849 | imageFrame = UIEdgeInsetsInsetRect( |
| 4850 | _contentArea.bounds, [self snapshotEdgeInsetsForTab:topTab]); |
| 4851 | } else { |
| 4852 | imageFrame = [topTab.webState->GetView() bounds]; |
| 4853 | } |
| 4854 | |
| 4855 | // Add three layers in order on top of the contentArea for the animation: |
| 4856 | // 1. The black "background" screen. |
| 4857 | UIView* background = [[UIView alloc] initWithFrame:[_contentArea bounds]]; |
| 4858 | InstallBackgroundInView(background); |
| 4859 | [_contentArea addSubview:background]; |
| 4860 | |
| 4861 | // 2. A CardView displaying the data from the current tab. |
| 4862 | CardView* topCard = [self addCardViewInFullscreen:!self.isToolbarOnScreen]; |
| 4863 | NSString* title = [topTab title]; |
| 4864 | if (![title length]) |
| 4865 | title = [topTab urlDisplayString]; |
| 4866 | [topCard setTitle:title]; |
| 4867 | [topCard setImage:image]; |
| 4868 | [topCard setFavicon:nil]; |
| 4869 | |
| 4870 | favicon::FaviconDriver* faviconDriver = |
| 4871 | favicon::WebFaviconDriver::FromWebState(topTab.webState); |
| 4872 | if (faviconDriver && faviconDriver->FaviconIsValid()) { |
| 4873 | gfx::Image favicon = faviconDriver->GetFavicon(); |
| 4874 | if (!favicon.IsEmpty()) |
| 4875 | [topCard setFavicon:favicon.ToUIImage()]; |
| 4876 | } |
| 4877 | |
| 4878 | // 3. A new, blank CardView to represent the new tab being added. |
| 4879 | // Launch the new background tab animation. |
| 4880 | page_animation_util::AnimateNewBackgroundPageWithCompletion( |
| 4881 | topCard, [_contentArea frame], imageFrame, IsPortrait(), ^{ |
| 4882 | [background removeFromSuperview]; |
| 4883 | [topCard removeFromSuperview]; |
| 4884 | self.inNewTabAnimation = NO; |
| 4885 | // Resnapshot the top card if it has its own toolbar, as the toolbar |
| 4886 | // will be captured in the new tab animation, but isn't desired for |
| 4887 | // the stack view snapshots. |
| 4888 | id nativeController = [self nativeControllerForTab:topTab]; |
| 4889 | if ([nativeController conformsToProtocol:@protocol(ToolbarOwner)]) { |
| 4890 | SnapshotTabHelper::FromWebState(topTab.webState) |
| 4891 | ->UpdateSnapshot(/*with_overlays=*/true, |
| 4892 | /*visible_frame_only=*/true); |
| 4893 | } |
| 4894 | startVoiceSearchIfNecessaryBlock(); |
| 4895 | }); |
| 4896 | // Reset the foreground tab completion block so that it can never be |
| 4897 | // called more than once regardless of foreground/background tab |
| 4898 | // appearances. |
| 4899 | self.foregroundTabWasAddedCompletionBlock = nil; |
| 4900 | } |
| 4901 | } |
| 4902 | |
| 4903 | - (void)tabModel:(TabModel*)model didDeselectTab:(Tab*)tab { |
| 4904 | [tab wasHidden]; |
| 4905 | [self dismissPopups]; |
| 4906 | } |
| 4907 | |
sdefresne | 49cf286 | 2017-03-15 13:46:14 | [diff] [blame] | 4908 | // Observer method, tab replaced. |
| 4909 | - (void)tabModel:(TabModel*)model |
| 4910 | didReplaceTab:(Tab*)oldTab |
| 4911 | withTab:(Tab*)newTab |
| 4912 | atIndex:(NSUInteger)index { |
| 4913 | [self uninstallDelegatesForTab:oldTab]; |
| 4914 | [self installDelegatesForTab:newTab]; |
kkhorimoto | fa0844cc | 2017-03-20 17:01:26 | [diff] [blame] | 4915 | |
michaeldo | 79909fb | 2017-05-09 23:42:50 | [diff] [blame] | 4916 | if (_infoBarContainer) { |
Rohit Rao | af46af9 | 2017-08-10 12:52:30 | [diff] [blame] | 4917 | infobars::InfoBarManager* infoBarManager = nullptr; |
| 4918 | if (newTab) { |
| 4919 | DCHECK(newTab.webState); |
| 4920 | infoBarManager = InfoBarManagerImpl::FromWebState(newTab.webState); |
| 4921 | } |
michaeldo | 79909fb | 2017-05-09 23:42:50 | [diff] [blame] | 4922 | _infoBarContainer->ChangeInfoBarManager(infoBarManager); |
| 4923 | } |
| 4924 | |
Kurt Horimoto | 4d1f696 | 2017-12-19 23:38:09 | [diff] [blame] | 4925 | // Add |newTab|'s view to the hierarchy if it's the current Tab. |
| 4926 | if (self.active && model.currentTab == newTab) |
kkhorimoto | fa0844cc | 2017-03-20 17:01:26 | [diff] [blame] | 4927 | [self displayTab:newTab isNewSelection:NO]; |
Mohamad Ahmadi | bec07eb | 2017-09-12 19:38:46 | [diff] [blame] | 4928 | |
| 4929 | if (newTab) |
| 4930 | [_paymentRequestManager setActiveWebState:newTab.webState]; |
sdefresne | 49cf286 | 2017-03-15 13:46:14 | [diff] [blame] | 4931 | } |
| 4932 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4933 | // A tab has been removed, remove its views from display if necessary. |
| 4934 | - (void)tabModel:(TabModel*)model |
| 4935 | didRemoveTab:(Tab*)tab |
| 4936 | atIndex:(NSUInteger)index { |
sdefresne | 49cf286 | 2017-03-15 13:46:14 | [diff] [blame] | 4937 | [self uninstallDelegatesForTab:tab]; |
| 4938 | |
kkhorimoto | 496fdd7 | 2017-06-12 19:56:31 | [diff] [blame] | 4939 | // Cancel dialogs for |tab|'s WebState. |
| 4940 | [self.dialogPresenter cancelDialogForWebState:tab.webState]; |
| 4941 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4942 | // Ignore changes while the tab stack view is visible (or while suspended). |
| 4943 | // The display will be refreshed when this view becomes active again. |
| 4944 | if (!self.visible || !model.webUsageEnabled) |
| 4945 | return; |
| 4946 | |
| 4947 | // Remove the find bar for now. |
| 4948 | [self hideFindBarWithAnimation:NO]; |
| 4949 | } |
| 4950 | |
| 4951 | - (void)tabModel:(TabModel*)model willRemoveTab:(Tab*)tab { |
| 4952 | if (tab == [model currentTab]) { |
| 4953 | [_contentArea displayContentView:nil]; |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 4954 | [_toolbarCoordinator selectedTabChanged]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4955 | } |
| 4956 | |
Mohamad Ahmadi | 7d09ec3 | 2017-07-11 22:32:19 | [diff] [blame] | 4957 | [_paymentRequestManager stopTrackingWebState:tab.webState]; |
| 4958 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4959 | [[UpgradeCenter sharedInstance] tabWillClose:tab.tabId]; |
| 4960 | if ([model count] == 1) { // About to remove the last tab. |
Mohamad Ahmadi | 7d09ec3 | 2017-07-11 22:32:19 | [diff] [blame] | 4961 | [_paymentRequestManager setActiveWebState:nullptr]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4962 | } |
| 4963 | } |
| 4964 | |
| 4965 | // Called when the number of tabs changes. Update the toolbar accordingly. |
| 4966 | - (void)tabModelDidChangeTabCount:(TabModel*)model { |
| 4967 | DCHECK(model == _model); |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 4968 | [_toolbarCoordinator setTabCount:[_model count]]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4969 | } |
| 4970 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 4971 | #pragma mark - UpgradeCenterClient |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4972 | |
| 4973 | - (void)showUpgrade:(UpgradeCenter*)center { |
| 4974 | // Add an infobar on all the open tabs. |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 4975 | for (Tab* tab in _model) { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4976 | NSString* tabId = tab.tabId; |
Rohit Rao | af46af9 | 2017-08-10 12:52:30 | [diff] [blame] | 4977 | DCHECK(tab.webState); |
| 4978 | infobars::InfoBarManager* infoBarManager = |
| 4979 | InfoBarManagerImpl::FromWebState(tab.webState); |
| 4980 | DCHECK(infoBarManager); |
| 4981 | [center addInfoBarToManager:infoBarManager forTabId:tabId]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4982 | } |
| 4983 | } |
| 4984 | |
Mark Cogan | 80aa28d | 2017-11-30 13:11:34 | [diff] [blame] | 4985 | #pragma mark - InfobarContainerStateDelegate |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4986 | |
Mark Cogan | 80aa28d | 2017-11-30 13:11:34 | [diff] [blame] | 4987 | - (void)infoBarContainerStateDidChangeAnimated:(BOOL)animated { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 4988 | InfoBarContainerView* infoBarContainerView = _infoBarContainer->view(); |
| 4989 | DCHECK(infoBarContainerView); |
| 4990 | CGRect containerFrame = infoBarContainerView.frame; |
| 4991 | CGFloat height = [infoBarContainerView topmostVisibleInfoBarHeight]; |
| 4992 | containerFrame.origin.y = CGRectGetMaxY(_contentArea.frame) - height; |
| 4993 | containerFrame.size.height = height; |
| 4994 | BOOL isViewVisible = self.visible; |
| 4995 | [UIView animateWithDuration:0.1 |
| 4996 | animations:^{ |
| 4997 | [infoBarContainerView setFrame:containerFrame]; |
| 4998 | } |
| 4999 | completion:^(BOOL finished) { |
| 5000 | if (!isViewVisible) |
| 5001 | return; |
| 5002 | UIAccessibilityPostNotification( |
| 5003 | UIAccessibilityLayoutChangedNotification, infoBarContainerView); |
| 5004 | }]; |
| 5005 | } |
| 5006 | |
Mark Cogan | 80aa28d | 2017-11-30 13:11:34 | [diff] [blame] | 5007 | #pragma mark - UIGestureRecognizerDelegate |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5008 | |
| 5009 | // Always return yes, as this tap should work with various recognizers, |
| 5010 | // including UITextTapRecognizer, UILongPressGestureRecognizer, |
| 5011 | // UIScrollViewPanGestureRecognizer and others. |
| 5012 | - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer |
| 5013 | shouldRecognizeSimultaneouslyWithGestureRecognizer: |
| 5014 | (UIGestureRecognizer*)otherGestureRecognizer { |
| 5015 | return YES; |
| 5016 | } |
| 5017 | |
| 5018 | // Tap gestures should only be recognized within |_contentArea|. |
| 5019 | - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gesture { |
| 5020 | CGPoint location = [gesture locationInView:self.view]; |
| 5021 | |
| 5022 | // Only allow touches on descendant views of |_contentArea|. |
| 5023 | UIView* hitView = [self.view hitTest:location withEvent:nil]; |
| 5024 | return (![hitView isDescendantOfView:_contentArea]) ? NO : YES; |
| 5025 | } |
| 5026 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 5027 | #pragma mark - SideSwipeControllerDelegate |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5028 | |
| 5029 | - (void)sideSwipeViewDismissAnimationDidEnd:(UIView*)sideSwipeView { |
| 5030 | DCHECK(!IsIPadIdiom()); |
| 5031 | // Update frame incase orientation changed while |_contentArea| was out of |
| 5032 | // the view hierarchy. |
| 5033 | [_contentArea setFrame:[sideSwipeView frame]]; |
| 5034 | |
Justin Cohen | 16ad60e | 2017-11-10 14:56:26 | [diff] [blame] | 5035 | [self.view insertSubview:_contentArea aboveSubview:_fakeStatusBarView]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5036 | [self updateVoiceSearchBarVisibilityAnimated:NO]; |
| 5037 | [self updateToolbar]; |
| 5038 | |
| 5039 | // Reset horizontal stack view. |
| 5040 | [sideSwipeView removeFromSuperview]; |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 5041 | [self.sideSwipeController setInSwipe:NO]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5042 | [_infoBarContainer->view() setHidden:NO]; |
| 5043 | } |
| 5044 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 5045 | - (UIView*)sideSwipeContentView { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5046 | return _contentArea; |
| 5047 | } |
| 5048 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 5049 | - (void)sideSwipeRedisplayTab:(Tab*)tab { |
| 5050 | [self displayTab:tab isNewSelection:YES]; |
| 5051 | } |
| 5052 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5053 | - (BOOL)preventSideSwipe { |
Peter Laurens | e0b80f1 | 2017-11-21 07:52:40 | [diff] [blame] | 5054 | if ([_toolbarCoordinator isShowingToolsMenu]) |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5055 | return YES; |
| 5056 | |
| 5057 | if (_voiceSearchController && _voiceSearchController->IsVisible()) |
| 5058 | return YES; |
| 5059 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5060 | if (!self.active) |
| 5061 | return YES; |
| 5062 | |
| 5063 | return NO; |
| 5064 | } |
| 5065 | |
| 5066 | - (void)updateAccessoryViewsForSideSwipeWithVisibility:(BOOL)visible { |
| 5067 | if (visible) { |
| 5068 | [self updateVoiceSearchBarVisibilityAnimated:NO]; |
| 5069 | [self updateToolbar]; |
| 5070 | [_infoBarContainer->view() setHidden:NO]; |
| 5071 | } else { |
| 5072 | // Hide UI accessories such as find bar and first visit overlays |
| 5073 | // for welcome page. |
| 5074 | [self hideFindBarWithAnimation:NO]; |
| 5075 | [_infoBarContainer->view() setHidden:YES]; |
| 5076 | [_voiceSearchBar setHidden:YES]; |
| 5077 | } |
| 5078 | } |
| 5079 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 5080 | - (CGFloat)headerHeightForSideSwipe { |
| 5081 | return self.headerHeight; |
| 5082 | } |
| 5083 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5084 | - (BOOL)verifyToolbarViewPlacementInView:(UIView*)views { |
| 5085 | BOOL seenToolbar = NO; |
| 5086 | BOOL seenInfoBarContainer = NO; |
| 5087 | BOOL seenContentArea = NO; |
| 5088 | for (UIView* view in views.subviews) { |
sczs | 42f7f748 | 2017-11-08 01:13:27 | [diff] [blame] | 5089 | if (view == _toolbarCoordinator.toolbarViewController.view) |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5090 | seenToolbar = YES; |
| 5091 | else if (view == _infoBarContainer->view()) |
| 5092 | seenInfoBarContainer = YES; |
| 5093 | else if (view == _contentArea) |
| 5094 | seenContentArea = YES; |
| 5095 | if ((seenToolbar && !seenInfoBarContainer) || |
| 5096 | (seenInfoBarContainer && !seenContentArea)) |
| 5097 | return NO; |
| 5098 | } |
| 5099 | return YES; |
| 5100 | } |
| 5101 | |
| 5102 | #pragma mark - PreloadControllerDelegate methods |
| 5103 | |
rohitrao | eeb5293b | 2017-06-15 14:40:02 | [diff] [blame] | 5104 | - (BOOL)preloadShouldUseDesktopUserAgent { |
liaoyuke | b8453e1 | 2017-02-24 22:08:44 | [diff] [blame] | 5105 | return [_model currentTab].usesDesktopUserAgent; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5106 | } |
| 5107 | |
rohitrao | eeb5293b | 2017-06-15 14:40:02 | [diff] [blame] | 5108 | - (BOOL)preloadHasNativeControllerForURL:(const GURL&)url { |
| 5109 | return [self hasControllerForURL:url]; |
| 5110 | } |
| 5111 | |
Gauthier Ambard | 65e949b09 | 2017-11-29 08:46:20 | [diff] [blame] | 5112 | // TODO(crbug.com/788705): BVC doesn't need to implement |
| 5113 | // BookmarkModelBridgeObserver once the new toolbar is turned on. |
| 5114 | #pragma mark - BookmarkModelBridgeObserver |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5115 | |
| 5116 | // If an added or removed bookmark is the same as the current url, update the |
| 5117 | // toolbar so the star highlight is kept in sync. |
Gauthier Ambard | 65e949b09 | 2017-11-29 08:46:20 | [diff] [blame] | 5118 | - (void)bookmarkNodeChildrenChanged:(const BookmarkNode*)bookmarkNode { |
| 5119 | [self updateToolbar]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5120 | } |
| 5121 | |
| 5122 | // If all bookmarks are removed, update the toolbar so the star highlight is |
| 5123 | // kept in sync. |
Gauthier Ambard | 65e949b09 | 2017-11-29 08:46:20 | [diff] [blame] | 5124 | - (void)bookmarkModelRemovedAllNodes { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5125 | [self updateToolbar]; |
| 5126 | } |
| 5127 | |
Gauthier Ambard | 65e949b09 | 2017-11-29 08:46:20 | [diff] [blame] | 5128 | // In case we are on a bookmarked page before the model is loaded. |
| 5129 | - (void)bookmarkModelLoaded { |
| 5130 | [self updateToolbar]; |
| 5131 | } |
| 5132 | |
| 5133 | - (void)bookmarkNodeChanged:(const BookmarkNode*)bookmarkNode { |
| 5134 | // No-op -- required by BookmarkModelBridgeObserver but not used. |
| 5135 | } |
| 5136 | |
| 5137 | - (void)bookmarkNode:(const BookmarkNode*)bookmarkNode |
| 5138 | movedFromParent:(const BookmarkNode*)oldParent |
| 5139 | toParent:(const BookmarkNode*)newParent { |
| 5140 | // No-op -- required by BookmarkModelBridgeObserver but not used. |
| 5141 | } |
| 5142 | |
| 5143 | - (void)bookmarkNodeDeleted:(const BookmarkNode*)node |
| 5144 | fromFolder:(const BookmarkNode*)folder { |
| 5145 | // No-op -- required by BookmarkModelBridgeObserver but not used. |
| 5146 | } |
| 5147 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 5148 | #pragma mark - NetExportTabHelperDelegate |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5149 | |
Gregory Chatzinoff | 5f9f7f0 | 2017-09-19 02:04:57 | [diff] [blame] | 5150 | - (void)netExportTabHelper:(NetExportTabHelper*)tabHelper |
| 5151 | showMailComposerWithContext:(ShowMailComposerContext*)context { |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5152 | if (![MFMailComposeViewController canSendMail]) { |
| 5153 | NSString* alertTitle = |
Gregory Chatzinoff | 5f9f7f0 | 2017-09-19 02:04:57 | [diff] [blame] | 5154 | l10n_util::GetNSString([context emailNotConfiguredAlertTitleId]); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5155 | NSString* alertMessage = |
Gregory Chatzinoff | 5f9f7f0 | 2017-09-19 02:04:57 | [diff] [blame] | 5156 | l10n_util::GetNSString([context emailNotConfiguredAlertMessageId]); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5157 | [self showErrorAlertWithStringTitle:alertTitle message:alertMessage]; |
| 5158 | return; |
| 5159 | } |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 5160 | MFMailComposeViewController* mailViewController = |
| 5161 | [[MFMailComposeViewController alloc] init]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5162 | [mailViewController setModalPresentationStyle:UIModalPresentationFormSheet]; |
Gregory Chatzinoff | 5f9f7f0 | 2017-09-19 02:04:57 | [diff] [blame] | 5163 | [mailViewController setToRecipients:[context toRecipients]]; |
| 5164 | [mailViewController setSubject:[context subject]]; |
| 5165 | [mailViewController setMessageBody:[context body] isHTML:NO]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5166 | |
Gregory Chatzinoff | 5f9f7f0 | 2017-09-19 02:04:57 | [diff] [blame] | 5167 | const base::FilePath& textFile = [context textFileToAttach]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5168 | if (!textFile.empty()) { |
| 5169 | NSString* filename = base::SysUTF8ToNSString(textFile.value()); |
| 5170 | NSData* data = [NSData dataWithContentsOfFile:filename]; |
| 5171 | if (data) { |
| 5172 | NSString* displayName = |
| 5173 | base::SysUTF8ToNSString(textFile.BaseName().value()); |
| 5174 | [mailViewController addAttachmentData:data |
| 5175 | mimeType:@"text/plain" |
| 5176 | fileName:displayName]; |
| 5177 | } |
| 5178 | } |
| 5179 | |
| 5180 | [mailViewController setMailComposeDelegate:self]; |
| 5181 | [self presentViewController:mailViewController animated:YES completion:nil]; |
| 5182 | } |
| 5183 | |
| 5184 | #pragma mark - MFMailComposeViewControllerDelegate methods |
| 5185 | |
| 5186 | - (void)mailComposeController:(MFMailComposeViewController*)controller |
| 5187 | didFinishWithResult:(MFMailComposeResult)result |
| 5188 | error:(NSError*)error { |
| 5189 | [self dismissViewControllerAnimated:YES completion:nil]; |
| 5190 | } |
| 5191 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 5192 | #pragma mark - SKStoreProductViewControllerDelegate |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5193 | |
| 5194 | - (void)productViewControllerDidFinish: |
| 5195 | (SKStoreProductViewController*)viewController { |
| 5196 | [self dismissViewControllerAnimated:YES completion:nil]; |
| 5197 | } |
| 5198 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 5199 | #pragma mark - StoreKitLauncher methods |
| 5200 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5201 | - (void)openAppStore:(NSString*)appId { |
| 5202 | if (![appId length]) |
| 5203 | return; |
| 5204 | NSDictionary* product = |
| 5205 | @{SKStoreProductParameterITunesItemIdentifier : appId}; |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 5206 | SKStoreProductViewController* storeViewController = |
| 5207 | [[SKStoreProductViewController alloc] init]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5208 | [storeViewController setDelegate:self]; |
| 5209 | [storeViewController loadProductWithParameters:product completionBlock:nil]; |
| 5210 | [self presentViewController:storeViewController animated:YES completion:nil]; |
| 5211 | } |
| 5212 | |
| 5213 | #pragma mark - TabDialogDelegate methods |
| 5214 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5215 | - (void)cancelDialogForTab:(Tab*)tab { |
| 5216 | [self.dialogPresenter cancelDialogForWebState:tab.webState]; |
| 5217 | } |
| 5218 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 5219 | #pragma mark - AppRatingPromptDelegate |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5220 | |
| 5221 | - (void)userTappedRateApp:(UIView*)view { |
| 5222 | base::RecordAction(base::UserMetricsAction("IOSRateThisAppRateChosen")); |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 5223 | _rateThisAppDialog = nil; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5224 | } |
| 5225 | |
| 5226 | - (void)userTappedSendFeedback:(UIView*)view { |
| 5227 | base::RecordAction(base::UserMetricsAction("IOSRateThisAppFeedbackChosen")); |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 5228 | _rateThisAppDialog = nil; |
edchin | 9eaf25f5 | 2017-10-26 02:42:20 | [diff] [blame] | 5229 | [self.dispatcher showReportAnIssueFromViewController:self]; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5230 | } |
| 5231 | |
| 5232 | - (void)userTappedDismiss:(UIView*)view { |
| 5233 | base::RecordAction(base::UserMetricsAction("IOSRateThisAppDismissChosen")); |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 5234 | _rateThisAppDialog = nil; |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5235 | } |
| 5236 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 5237 | #pragma mark - VoiceSearchBarOwner |
| 5238 | |
| 5239 | - (id<VoiceSearchBar>)voiceSearchBar { |
| 5240 | return _voiceSearchBar; |
| 5241 | } |
| 5242 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5243 | #pragma mark - VoiceSearchBarDelegate |
| 5244 | |
| 5245 | - (BOOL)isTTSEnabledForVoiceSearchBar:(id<VoiceSearchBar>)voiceSearchBar { |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 5246 | DCHECK_EQ(_voiceSearchBar, voiceSearchBar); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5247 | [self ensureVoiceSearchControllerCreated]; |
| 5248 | return _voiceSearchController->IsTextToSpeechEnabled() && |
| 5249 | _voiceSearchController->IsTextToSpeechSupported(); |
| 5250 | } |
| 5251 | |
| 5252 | - (void)voiceSearchBarDidUpdateButtonState:(id<VoiceSearchBar>)voiceSearchBar { |
stkhapugin | c9eee7b | 2017-04-10 15:49:27 | [diff] [blame] | 5253 | DCHECK_EQ(_voiceSearchBar, voiceSearchBar); |
Sylvain Defresne | 17b8aa4 | 2017-12-21 16:17:17 | [diff] [blame] | 5254 | SnapshotTabHelper::FromWebState(self.tabModel.currentTab.webState) |
| 5255 | ->UpdateSnapshot(/*with_overlays=*/true, /*visible_frame_only=*/true); |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5256 | } |
| 5257 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 5258 | #pragma mark - VoiceSearchPresenter (Public) |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5259 | |
| 5260 | - (UIView*)voiceSearchButton { |
| 5261 | return _voiceSearchButton; |
| 5262 | } |
| 5263 | |
| 5264 | - (id<LogoAnimationControllerOwner>)logoAnimationControllerOwner { |
| 5265 | return [self currentLogoAnimationControllerOwner]; |
| 5266 | } |
| 5267 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 5268 | #pragma mark - VoiceSearchPresenter helpers |
| 5269 | |
| 5270 | // The LogoAnimationControllerOwner to be used for the next logo transition |
| 5271 | // animation. |
| 5272 | - (id<LogoAnimationControllerOwner>)currentLogoAnimationControllerOwner { |
| 5273 | Protocol* ownerProtocol = @protocol(LogoAnimationControllerOwner); |
| 5274 | if ([_voiceSearchBar conformsToProtocol:ownerProtocol] && |
| 5275 | self.shouldShowVoiceSearchBar) { |
| 5276 | // Use |_voiceSearchBar| for VoiceSearch results tab and dismissal |
| 5277 | // animations. |
| 5278 | return static_cast<id<LogoAnimationControllerOwner>>(_voiceSearchBar); |
| 5279 | } |
| 5280 | id currentNativeController = |
| 5281 | [self nativeControllerForTab:self.tabModel.currentTab]; |
| 5282 | Protocol* possibleOwnerProtocol = |
| 5283 | @protocol(LogoAnimationControllerOwnerOwner); |
| 5284 | if ([currentNativeController conformsToProtocol:possibleOwnerProtocol] && |
| 5285 | [currentNativeController logoAnimationControllerOwner]) { |
| 5286 | // If the current native controller is showing a GLIF view (e.g. the NTP |
| 5287 | // when there is no doodle), use that GLIFControllerOwner. |
| 5288 | return [currentNativeController logoAnimationControllerOwner]; |
| 5289 | } |
| 5290 | return nil; |
| 5291 | } |
| 5292 | |
| 5293 | #pragma mark - ActivityServicePresentation |
Rohit Rao | 01e0e00 | 2017-08-14 20:49:43 | [diff] [blame] | 5294 | |
| 5295 | - (void)presentActivityServiceViewController:(UIViewController*)controller { |
| 5296 | [self presentViewController:controller animated:YES completion:nil]; |
| 5297 | } |
| 5298 | |
| 5299 | - (void)activityServiceDidEndPresenting { |
Mark Cogan | 5bd86ba | 2017-12-28 14:32:38 | [diff] [blame] | 5300 | self.dialogPresenterDelegateIsPresenting = NO; |
Rohit Rao | 01e0e00 | 2017-08-14 20:49:43 | [diff] [blame] | 5301 | [self.dialogPresenter tryToPresent]; |
| 5302 | } |
| 5303 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 5304 | - (void)showActivityServiceErrorAlertWithStringTitle:(NSString*)title |
| 5305 | message:(NSString*)message { |
| 5306 | [self showErrorAlertWithStringTitle:title message:message]; |
| 5307 | } |
| 5308 | |
| 5309 | #pragma mark - QRScannerPresenting |
Rohit Rao | cda0a99 | 2017-08-16 15:37:11 | [diff] [blame] | 5310 | |
| 5311 | - (void)presentQRScannerViewController:(UIViewController*)controller { |
| 5312 | [self presentViewController:controller animated:YES completion:nil]; |
| 5313 | } |
| 5314 | |
| 5315 | - (void)dismissQRScannerViewController:(UIViewController*)controller |
| 5316 | completion:(void (^)(void))completion { |
| 5317 | DCHECK_EQ(controller, self.presentedViewController); |
| 5318 | [self dismissViewControllerAnimated:YES completion:completion]; |
| 5319 | } |
| 5320 | |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 5321 | #pragma mark - TabHeadersDelegate |
| 5322 | |
| 5323 | - (CGFloat)tabHeaderHeightForTab:(Tab*)tab { |
| 5324 | return [self headerHeightForTab:tab]; |
| 5325 | } |
| 5326 | |
| 5327 | #pragma mark - TabHistoryPresentation |
sczs | dd860eba | 2017-08-10 01:55:38 | [diff] [blame] | 5328 | |
sczs | 0a726d2 | 2017-08-21 22:40:13 | [diff] [blame] | 5329 | - (UIView*)viewForTabHistoryPresentation { |
| 5330 | return self.view; |
| 5331 | } |
| 5332 | |
sczs | dd860eba | 2017-08-10 01:55:38 | [diff] [blame] | 5333 | - (void)prepareForTabHistoryPresentation { |
| 5334 | DCHECK(self.visible || self.dismissingModal); |
| 5335 | [[self.tabModel currentTab].webController dismissKeyboard]; |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 5336 | [_toolbarCoordinator cancelOmniboxEdit]; |
sczs | dd860eba | 2017-08-10 01:55:38 | [diff] [blame] | 5337 | } |
| 5338 | |
Mike Dougherty | a1ec2640 | 2017-08-23 19:46:31 | [diff] [blame] | 5339 | #pragma mark - CaptivePortalDetectorTabHelperDelegate |
| 5340 | |
Mike Dougherty | 4620cf8e | 2017-10-31 23:37:09 | [diff] [blame] | 5341 | - (void)captivePortalDetectorTabHelper: |
| 5342 | (CaptivePortalDetectorTabHelper*)tabHelper |
| 5343 | connectWithLandingURL:(const GURL&)landingURL { |
Mike Dougherty | 66e5881 | 2017-11-03 06:54:28 | [diff] [blame] | 5344 | [self addSelectedTabWithURL:landingURL transition:ui::PAGE_TRANSITION_TYPED]; |
Mike Dougherty | a1ec2640 | 2017-08-23 19:46:31 | [diff] [blame] | 5345 | } |
| 5346 | |
Gregory Chatzinoff | df93d69 | 2017-09-09 01:32:27 | [diff] [blame] | 5347 | #pragma mark - PageInfoPresentation |
| 5348 | |
Gregory Chatzinoff | b6a01f7 | 2017-09-20 20:06:39 | [diff] [blame] | 5349 | - (void)presentPageInfoView:(UIView*)pageInfoView { |
| 5350 | [pageInfoView setFrame:self.view.bounds]; |
| 5351 | [self.view addSubview:pageInfoView]; |
Gregory Chatzinoff | df93d69 | 2017-09-09 01:32:27 | [diff] [blame] | 5352 | } |
| 5353 | |
| 5354 | - (void)prepareForPageInfoPresentation { |
| 5355 | // Dismiss the omnibox (if open). |
sczs | f1620e5 | 2017-10-02 22:54:46 | [diff] [blame] | 5356 | [_toolbarCoordinator cancelOmniboxEdit]; |
Gregory Chatzinoff | df93d69 | 2017-09-09 01:32:27 | [diff] [blame] | 5357 | } |
| 5358 | |
Gregory Chatzinoff | b6a01f7 | 2017-09-20 20:06:39 | [diff] [blame] | 5359 | - (CGPoint)convertToPresentationCoordinatesForOrigin:(CGPoint)origin { |
| 5360 | return [self.view convertPoint:origin fromView:nil]; |
| 5361 | } |
| 5362 | |
Sylvain Defresne | cacc3a5 | 2017-09-12 13:51:04 | [diff] [blame] | 5363 | #pragma mark - WebStatePrinter |
| 5364 | |
| 5365 | - (void)printWebState:(web::WebState*)webState { |
| 5366 | if (webState == [_model currentTab].webState) |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 5367 | [self.dispatcher printTab]; |
Sylvain Defresne | cacc3a5 | 2017-09-12 13:51:04 | [diff] [blame] | 5368 | } |
| 5369 | |
Eugene But | 35ded55 | 2017-09-13 23:31:59 | [diff] [blame] | 5370 | #pragma mark - RepostFormTabHelperDelegate |
| 5371 | |
| 5372 | - (void)repostFormTabHelper:(RepostFormTabHelper*)helper |
Sylvain Defresne | e3c69812 | 2017-11-17 11:16:32 | [diff] [blame] | 5373 | presentRepostFormDialogForWebState:(web::WebState*)webState |
| 5374 | dialogAtPoint:(CGPoint)location |
| 5375 | completionHandler:(void (^)(BOOL))completion { |
| 5376 | _repostFormCoordinator = |
| 5377 | [[RepostFormCoordinator alloc] initWithBaseViewController:self |
| 5378 | dialogLocation:location |
| 5379 | webState:webState |
| 5380 | completionHandler:completion]; |
Eugene But | 35ded55 | 2017-09-13 23:31:59 | [diff] [blame] | 5381 | [_repostFormCoordinator start]; |
| 5382 | } |
| 5383 | |
| 5384 | - (void)repostFormTabHelperDismissRepostFormDialog: |
| 5385 | (RepostFormTabHelper*)helper { |
| 5386 | _repostFormCoordinator = nil; |
| 5387 | } |
| 5388 | |
edchin | f5150c68 | 2017-09-18 02:50:03 | [diff] [blame] | 5389 | #pragma mark - TabStripPresentation |
| 5390 | |
| 5391 | - (BOOL)isTabStripFullyVisible { |
| 5392 | return ([self currentHeaderOffset] == 0.0f); |
| 5393 | } |
| 5394 | |
| 5395 | - (void)showTabStripView:(UIView*)tabStripView { |
| 5396 | DCHECK([self isViewLoaded]); |
| 5397 | DCHECK(tabStripView); |
| 5398 | self.tabStripView = tabStripView; |
| 5399 | CGRect tabStripFrame = [self.tabStripView frame]; |
| 5400 | tabStripFrame.origin = CGPointZero; |
| 5401 | // TODO(crbug.com/256655): Move the origin.y below to -setUpViewLayout. |
| 5402 | // because the CGPointZero above will break reset the offset, but it's not |
| 5403 | // clear what removing that will do. |
Mark Cogan | 849244ee | 2017-12-29 15:57:19 | [diff] [blame] | 5404 | tabStripFrame.origin.y = self.headerOffset; |
edchin | f5150c68 | 2017-09-18 02:50:03 | [diff] [blame] | 5405 | tabStripFrame.size.width = CGRectGetWidth([self view].bounds); |
| 5406 | [self.tabStripView setFrame:tabStripFrame]; |
| 5407 | [[self view] addSubview:tabStripView]; |
| 5408 | } |
| 5409 | |
edchin | cd32fdf | 2017-10-25 12:45:45 | [diff] [blame] | 5410 | #pragma mark - ManageAccountsDelegate |
| 5411 | |
| 5412 | - (void)onManageAccounts { |
| 5413 | signin_metrics::LogAccountReconcilorStateOnGaiaResponse( |
| 5414 | ios::AccountReconcilorFactory::GetForBrowserState(self.browserState) |
| 5415 | ->GetState()); |
edchin | 5b8aa05 | 2017-10-30 23:27:28 | [diff] [blame] | 5416 | [self.dispatcher showAccountsSettingsFromViewController:self]; |
edchin | cd32fdf | 2017-10-25 12:45:45 | [diff] [blame] | 5417 | } |
| 5418 | |
| 5419 | - (void)onAddAccount { |
| 5420 | signin_metrics::LogAccountReconcilorStateOnGaiaResponse( |
| 5421 | ios::AccountReconcilorFactory::GetForBrowserState(self.browserState) |
| 5422 | ->GetState()); |
edchin | b082b298 | 2017-11-03 19:41:21 | [diff] [blame] | 5423 | [self.dispatcher showAddAccountFromViewController:self]; |
edchin | cd32fdf | 2017-10-25 12:45:45 | [diff] [blame] | 5424 | } |
| 5425 | |
| 5426 | - (void)onGoIncognito:(const GURL&)url { |
| 5427 | // The user taps on go incognito from the mobile U-turn webpage (the web page |
| 5428 | // that displays all users accounts available in the content area). As the |
| 5429 | // user chooses to go to incognito, the mobile U-turn page is no longer |
| 5430 | // neeeded. The current solution is to go back in history. This has the |
| 5431 | // advantage of keeping the current browsing session and give a good user |
| 5432 | // experience when the user comes back from incognito. |
| 5433 | [self.tabModel.currentTab goBack]; |
| 5434 | |
| 5435 | if (url.is_valid()) { |
| 5436 | OpenUrlCommand* command = [[OpenUrlCommand alloc] |
| 5437 | initWithURL:url |
| 5438 | referrer:web::Referrer() // Strip referrer when switching modes. |
| 5439 | inIncognito:YES |
| 5440 | inBackground:NO |
| 5441 | appendTo:kLastTab]; |
| 5442 | [self.dispatcher openURL:command]; |
| 5443 | } else { |
| 5444 | [self.dispatcher openNewTab:[OpenNewTabCommand command]]; |
| 5445 | } |
| 5446 | } |
| 5447 | |
Mark Cogan | 776e028 | 2018-01-02 09:00:06 | [diff] [blame] | 5448 | #pragma mark - SyncPresenter (Public) |
edchin | 95c92707 | 2017-11-04 00:35:07 | [diff] [blame] | 5449 | |
| 5450 | - (void)showReauthenticateSignin { |
| 5451 | [self.dispatcher |
edchin | 3b46e8d | 2017-11-07 22:48:12 | [diff] [blame] | 5452 | showSignin: |
| 5453 | [[ShowSigninCommand alloc] |
| 5454 | initWithOperation:AUTHENTICATION_OPERATION_REAUTHENTICATE |
| 5455 | accessPoint:signin_metrics::AccessPoint:: |
| 5456 | ACCESS_POINT_UNKNOWN] |
| 5457 | baseViewController:self]; |
edchin | 95c92707 | 2017-11-04 00:35:07 | [diff] [blame] | 5458 | } |
| 5459 | |
| 5460 | - (void)showSyncSettings { |
edchin | a14d718 | 2017-11-06 18:37:50 | [diff] [blame] | 5461 | [self.dispatcher showSyncSettingsFromViewController:self]; |
edchin | 95c92707 | 2017-11-04 00:35:07 | [diff] [blame] | 5462 | } |
| 5463 | |
| 5464 | - (void)showSyncPassphraseSettings { |
edchin | ec72306 | 2017-11-06 20:03:54 | [diff] [blame] | 5465 | [self.dispatcher showSyncPassphraseSettingsFromViewController:self]; |
edchin | 95c92707 | 2017-11-04 00:35:07 | [diff] [blame] | 5466 | } |
| 5467 | |
edchin | 9e7a111 | 2017-11-07 18:28:03 | [diff] [blame] | 5468 | #pragma mark - SigninPresenter |
| 5469 | |
| 5470 | - (void)showSignin:(ShowSigninCommand*)command { |
edchin | 3b46e8d | 2017-11-07 22:48:12 | [diff] [blame] | 5471 | [self.dispatcher showSignin:command baseViewController:self]; |
edchin | 9e7a111 | 2017-11-07 18:28:03 | [diff] [blame] | 5472 | } |
| 5473 | |
sdefresne | e65fd87 | 2016-12-19 13:38:13 | [diff] [blame] | 5474 | @end |