[email protected] | ce2fcd920 | 2012-01-06 18:42:50 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/prerender/prerender_contents.h" |
| 6 | |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 9 | #include <functional> |
[email protected] | b99b13b | 2011-05-04 20:59:04 | [diff] [blame] | 10 | #include <utility> |
[email protected] | c49147c3 | 2011-04-26 18:42:04 | [diff] [blame] | 11 | |
[email protected] | 48ed261 | 2014-01-21 00:30:57 | [diff] [blame] | 12 | #include "base/bind.h" |
tripta.g | 3e097a9 | 2017-06-22 07:56:26 | [diff] [blame] | 13 | #include "base/stl_util.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 15 | #include "base/task/post_task.h" |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 16 | #include "build/build_config.h" |
[email protected] | 25ff086 | 2013-07-12 00:59:03 | [diff] [blame] | 17 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 02b9b2b6f | 2011-05-24 17:27:50 | [diff] [blame] | 18 | #include "chrome/browser/history/history_tab_helper.h" |
[email protected] | db6c4ea | 2013-06-14 17:53:01 | [diff] [blame] | 19 | #include "chrome/browser/prerender/prerender_field_trial.h" |
[email protected] | 4c154ff8 | 2011-02-15 11:23:59 | [diff] [blame] | 20 | #include "chrome/browser/prerender/prerender_final_status.h" |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 21 | #include "chrome/browser/prerender/prerender_handle.h" |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 22 | #include "chrome/browser/prerender/prerender_manager.h" |
[email protected] | a303db7 | 2014-01-06 05:08:19 | [diff] [blame] | 23 | #include "chrome/browser/prerender/prerender_manager_factory.h" |
John Abd-El-Malek | d237798 | 2018-01-08 22:23:12 | [diff] [blame] | 24 | #include "chrome/browser/prerender/prerender_util.h" |
[email protected] | cafe4ad | 2011-07-28 18:34:56 | [diff] [blame] | 25 | #include "chrome/browser/profiles/profile.h" |
avi | 24d693f | 2016-08-06 18:03:52 | [diff] [blame] | 26 | #include "chrome/browser/task_manager/web_contents_tags.h" |
[email protected] | e0bc75d5 | 2014-01-30 23:42:59 | [diff] [blame] | 27 | #include "chrome/browser/ui/tab_helpers.h" |
hashimoto | bf20fc6 | 2014-08-27 03:33:57 | [diff] [blame] | 28 | #include "chrome/browser/ui/web_contents_sizer.h" |
[email protected] | a0358d7 | 2012-03-09 14:06:50 | [diff] [blame] | 29 | #include "chrome/common/prerender_messages.h" |
droger | 67b240c6 | 2016-08-03 11:50:27 | [diff] [blame] | 30 | #include "chrome/common/prerender_types.h" |
John Abd-El-Malek | d237798 | 2018-01-08 22:23:12 | [diff] [blame] | 31 | #include "chrome/common/prerender_util.h" |
sdefresne | bc766ef | 2014-09-25 09:28:13 | [diff] [blame] | 32 | #include "components/history/core/browser/history_types.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 33 | #include "content/public/browser/browser_task_traits.h" |
[email protected] | 48ed261 | 2014-01-21 00:30:57 | [diff] [blame] | 34 | #include "content/public/browser/browser_thread.h" |
jam | e562635 | 2017-01-26 20:15:23 | [diff] [blame] | 35 | #include "content/public/browser/navigation_handle.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 36 | #include "content/public/browser/notification_service.h" |
[email protected] | b849847b | 2013-12-10 21:57:58 | [diff] [blame] | 37 | #include "content/public/browser/render_frame_host.h" |
[email protected] | d908348 | 2012-01-06 00:38:46 | [diff] [blame] | 38 | #include "content/public/browser/render_process_host.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 39 | #include "content/public/browser/render_view_host.h" |
avi | f9ab5d94 | 2015-10-15 14:05:44 | [diff] [blame] | 40 | #include "content/public/browser/render_widget_host.h" |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 41 | #include "content/public/browser/session_storage_namespace.h" |
[email protected] | d908348 | 2012-01-06 00:38:46 | [diff] [blame] | 42 | #include "content/public/browser/web_contents.h" |
[email protected] | 674bc59 | 2011-12-20 23:00:42 | [diff] [blame] | 43 | #include "content/public/browser/web_contents_delegate.h" |
[email protected] | bde57b8 | 2013-11-01 23:10:44 | [diff] [blame] | 44 | #include "content/public/common/frame_navigate_params.h" |
jam | e562635 | 2017-01-26 20:15:23 | [diff] [blame] | 45 | #include "net/http/http_response_headers.h" |
erikchen | 00e7992 | 2018-03-16 20:52:57 | [diff] [blame] | 46 | #include "services/resource_coordinator/public/cpp/memory_instrumentation/memory_instrumentation.h" |
ben | 03ac5d01 | 2017-04-28 05:12:59 | [diff] [blame] | 47 | #include "services/service_manager/public/cpp/binder_registry.h" |
Sylvain Defresne | c6ccc77d | 2014-09-19 10:19:35 | [diff] [blame] | 48 | #include "ui/base/page_transition_types.h" |
yusufo | 7c8d964 | 2016-08-22 17:41:07 | [diff] [blame] | 49 | #include "ui/gfx/geometry/size.h" |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 50 | |
[email protected] | 28c5d0b7 | 2014-05-13 08:19:59 | [diff] [blame] | 51 | using content::BrowserThread; |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 52 | using content::OpenURLParams; |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 53 | using content::RenderViewHost; |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 54 | using content::SessionStorageNamespace; |
[email protected] | 768c547 | 2011-12-26 19:06:17 | [diff] [blame] | 55 | using content::WebContents; |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 56 | |
[email protected] | 4c154ff8 | 2011-02-15 11:23:59 | [diff] [blame] | 57 | namespace prerender { |
| 58 | |
[email protected] | 5a8dffa | 2011-01-26 00:40:39 | [diff] [blame] | 59 | class PrerenderContentsFactoryImpl : public PrerenderContents::Factory { |
| 60 | public: |
dcheng | 2c9c80c5 | 2014-10-22 21:21:05 | [diff] [blame] | 61 | PrerenderContents* CreatePrerenderContents( |
| 62 | PrerenderManager* prerender_manager, |
| 63 | Profile* profile, |
| 64 | const GURL& url, |
| 65 | const content::Referrer& referrer, |
Ryan Sturm | d154e1e | 2019-06-01 01:18:59 | [diff] [blame] | 66 | const base::Optional<url::Origin>& initiator_origin, |
davidben | 569bc23 | 2015-04-21 21:25:49 | [diff] [blame] | 67 | Origin origin) override { |
| 68 | return new PrerenderContents(prerender_manager, profile, url, referrer, |
Ryan Sturm | d154e1e | 2019-06-01 01:18:59 | [diff] [blame] | 69 | initiator_origin, origin); |
[email protected] | 5a8dffa | 2011-01-26 00:40:39 | [diff] [blame] | 70 | } |
| 71 | }; |
| 72 | |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 73 | // WebContentsDelegateImpl ----------------------------------------------------- |
[email protected] | d3961e8 | 2011-05-20 06:32:03 | [diff] [blame] | 74 | |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 75 | class PrerenderContents::WebContentsDelegateImpl |
[email protected] | 674bc59 | 2011-12-20 23:00:42 | [diff] [blame] | 76 | : public content::WebContentsDelegate { |
[email protected] | d3961e8 | 2011-05-20 06:32:03 | [diff] [blame] | 77 | public: |
[email protected] | 75d90c1 | 2013-03-20 19:50:16 | [diff] [blame] | 78 | explicit WebContentsDelegateImpl(PrerenderContents* prerender_contents) |
| 79 | : prerender_contents_(prerender_contents) { |
[email protected] | d3961e8 | 2011-05-20 06:32:03 | [diff] [blame] | 80 | } |
[email protected] | 3b023e2 | 2011-06-16 22:53:13 | [diff] [blame] | 81 | |
[email protected] | baece6c3 | 2012-06-25 21:22:41 | [diff] [blame] | 82 | // content::WebContentsDelegate implementation: |
dcheng | 2c9c80c5 | 2014-10-22 21:21:05 | [diff] [blame] | 83 | WebContents* OpenURLFromTab(WebContents* source, |
| 84 | const OpenURLParams& params) override { |
[email protected] | 0905b0166 | 2011-12-05 20:30:22 | [diff] [blame] | 85 | // |OpenURLFromTab| is typically called when a frame performs a navigation |
| 86 | // that requires the browser to perform the transition instead of WebKit. |
creis | 2946027 | 2015-12-16 04:38:22 | [diff] [blame] | 87 | // Examples include client redirects to hosted app URLs. |
nick | a23a06d | 2015-06-09 18:05:44 | [diff] [blame] | 88 | // TODO(cbentzel): Consider supporting this for CURRENT_TAB dispositions, if |
| 89 | // it is a common case during prerenders. |
[email protected] | 0905b0166 | 2011-12-05 20:30:22 | [diff] [blame] | 90 | prerender_contents_->Destroy(FINAL_STATUS_OPEN_URL); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
lukasza | 1d02573 | 2016-09-28 21:36:08 | [diff] [blame] | 94 | bool ShouldTransferNavigation(bool is_main_frame_navigation) override { |
creis | 2946027 | 2015-12-16 04:38:22 | [diff] [blame] | 95 | // Cancel the prerender if the navigation attempts to transfer to a |
| 96 | // different process. Examples include server redirects to privileged pages |
| 97 | // or cross-site subframe navigations in --site-per-process. |
| 98 | prerender_contents_->Destroy(FINAL_STATUS_OPEN_URL); |
| 99 | return false; |
| 100 | } |
| 101 | |
dcheng | 2c9c80c5 | 2014-10-22 21:21:05 | [diff] [blame] | 102 | void CloseContents(content::WebContents* contents) override { |
[email protected] | eebe7da5 | 2014-04-30 18:00:21 | [diff] [blame] | 103 | prerender_contents_->Destroy(FINAL_STATUS_CLOSED); |
| 104 | } |
| 105 | |
rdsmith | daa9227 | 2015-05-21 20:33:41 | [diff] [blame] | 106 | void CanDownload(const GURL& url, |
dcheng | 2c9c80c5 | 2014-10-22 21:21:05 | [diff] [blame] | 107 | const std::string& request_method, |
Avi Drissman | 346e0ea | 2019-04-02 00:08:46 | [diff] [blame] | 108 | base::OnceCallback<void(bool)> callback) override { |
[email protected] | 68649314 | 2011-07-15 21:47:22 | [diff] [blame] | 109 | prerender_contents_->Destroy(FINAL_STATUS_DOWNLOAD); |
| 110 | // Cancel the download. |
Avi Drissman | 346e0ea | 2019-04-02 00:08:46 | [diff] [blame] | 111 | std::move(callback).Run(false); |
[email protected] | 68649314 | 2011-07-15 21:47:22 | [diff] [blame] | 112 | } |
| 113 | |
dcheng | 2c9c80c5 | 2014-10-22 21:21:05 | [diff] [blame] | 114 | bool ShouldCreateWebContents( |
nick | f5e618c | 2016-11-28 22:38:37 | [diff] [blame] | 115 | content::WebContents* web_contents, |
lukasza | 6f8ac62 | 2017-06-06 03:10:20 | [diff] [blame] | 116 | content::RenderFrameHost* opener, |
nick | f5e618c | 2016-11-28 22:38:37 | [diff] [blame] | 117 | content::SiteInstance* source_site_instance, |
dcheng | 3ce04b6 | 2015-10-26 23:30:55 | [diff] [blame] | 118 | int32_t route_id, |
| 119 | int32_t main_frame_route_id, |
| 120 | int32_t main_frame_widget_route_id, |
scottmg | de42fb9 | 2017-02-10 17:56:03 | [diff] [blame] | 121 | content::mojom::WindowContainerType window_container_type, |
nick | f5e618c | 2016-11-28 22:38:37 | [diff] [blame] | 122 | const GURL& opener_url, |
nasko | 48321ca3 | 2015-07-02 20:44:12 | [diff] [blame] | 123 | const std::string& frame_name, |
dcheng | 2c9c80c5 | 2014-10-22 21:21:05 | [diff] [blame] | 124 | const GURL& target_url, |
| 125 | const std::string& partition_id, |
| 126 | SessionStorageNamespace* session_storage_namespace) override { |
[email protected] | f1cd336 | 2014-01-07 20:43:01 | [diff] [blame] | 127 | // Since we don't want to permit child windows that would have a |
| 128 | // window.opener property, terminate prerendering. |
| 129 | prerender_contents_->Destroy(FINAL_STATUS_CREATE_NEW_WINDOW); |
| 130 | // Cancel the popup. |
| 131 | return false; |
| 132 | } |
| 133 | |
dcheng | 2c9c80c5 | 2014-10-22 21:21:05 | [diff] [blame] | 134 | bool OnGoToEntryOffset(int offset) override { |
[email protected] | 9e1ad4b | 2011-08-14 16:49:19 | [diff] [blame] | 135 | // This isn't allowed because the history merge operation |
| 136 | // does not work if there are renderer issued challenges. |
| 137 | // TODO(cbentzel): Cancel in this case? May not need to do |
| 138 | // since render-issued offset navigations are not guaranteed, |
| 139 | // but indicates that the page cares about the history. |
| 140 | return false; |
| 141 | } |
| 142 | |
mathiash | 72a5e46 | 2014-11-19 08:18:50 | [diff] [blame] | 143 | bool ShouldSuppressDialogs(WebContents* source) override { |
[email protected] | baece6c3 | 2012-06-25 21:22:41 | [diff] [blame] | 144 | // We still want to show the user the message when they navigate to this |
| 145 | // page, so cancel this prerender. |
| 146 | prerender_contents_->Destroy(FINAL_STATUS_JAVASCRIPT_ALERT); |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 147 | // Always suppress JavaScript messages if they're triggered by a page being |
| 148 | // prerendered. |
[email protected] | baece6c3 | 2012-06-25 21:22:41 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | |
dcheng | 2c9c80c5 | 2014-10-22 21:21:05 | [diff] [blame] | 152 | void RegisterProtocolHandler(WebContents* web_contents, |
| 153 | const std::string& protocol, |
| 154 | const GURL& url, |
| 155 | bool user_gesture) override { |
[email protected] | baece6c3 | 2012-06-25 21:22:41 | [diff] [blame] | 156 | // TODO(mmenke): Consider supporting this if it is a common case during |
| 157 | // prerenders. |
| 158 | prerender_contents_->Destroy(FINAL_STATUS_REGISTER_PROTOCOL_HANDLER); |
[email protected] | 5f5b9e4cf | 2011-09-07 21:26:05 | [diff] [blame] | 159 | } |
| 160 | |
Lucas Furukawa Gadani | 4909f3c | 2019-06-18 22:36:52 | [diff] [blame] | 161 | gfx::Size GetSizeForNewRenderView(WebContents* web_contents) override { |
[email protected] | b3b0e82 | 2014-01-22 21:20:12 | [diff] [blame] | 162 | // Have to set the size of the RenderView on initialization to be sure it is |
| 163 | // set before the RenderView is hidden on all platforms (esp. Android). |
yusufo | 7c8d964 | 2016-08-22 17:41:07 | [diff] [blame] | 164 | return prerender_contents_->bounds_.size(); |
[email protected] | b3b0e82 | 2014-01-22 21:20:12 | [diff] [blame] | 165 | } |
| 166 | |
[email protected] | d3961e8 | 2011-05-20 06:32:03 | [diff] [blame] | 167 | private: |
[email protected] | d3961e8 | 2011-05-20 06:32:03 | [diff] [blame] | 168 | PrerenderContents* prerender_contents_; |
| 169 | }; |
| 170 | |
mattcary | fb7f2b6 | 2016-10-17 08:34:35 | [diff] [blame] | 171 | PrerenderContents::Observer::~Observer() {} |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 172 | |
Ryan Sturm | d154e1e | 2019-06-01 01:18:59 | [diff] [blame] | 173 | PrerenderContents::PrerenderContents( |
| 174 | PrerenderManager* prerender_manager, |
| 175 | Profile* profile, |
| 176 | const GURL& url, |
| 177 | const content::Referrer& referrer, |
| 178 | const base::Optional<url::Origin>& initiator_origin, |
| 179 | Origin origin) |
Egor Pasko | f0c1180 | 2019-03-22 13:52:31 | [diff] [blame] | 180 | : prerender_mode_(DEPRECATED_FULL_PRERENDER), |
mattcary | d4a8c77f | 2016-08-23 17:32:45 | [diff] [blame] | 181 | prerendering_has_started_(false), |
nigeltao | 9881067 | 2017-01-20 01:10:46 | [diff] [blame] | 182 | prerender_canceler_binding_(this), |
[email protected] | 115ab7b | 2012-05-16 20:25:59 | [diff] [blame] | 183 | prerender_manager_(prerender_manager), |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 184 | prerender_url_(url), |
[email protected] | 608c5713 | 2011-02-16 14:57:33 | [diff] [blame] | 185 | referrer_(referrer), |
Ryan Sturm | d154e1e | 2019-06-01 01:18:59 | [diff] [blame] | 186 | initiator_origin_(initiator_origin), |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 187 | profile_(profile), |
[email protected] | 6974ae7e | 2012-02-16 01:17:59 | [diff] [blame] | 188 | has_finished_loading_(false), |
Ryan Sturm | 74e5c53 | 2019-04-05 23:29:47 | [diff] [blame] | 189 | final_status_(FINAL_STATUS_UNKNOWN), |
[email protected] | e348af7 | 2011-05-23 21:02:49 | [diff] [blame] | 190 | prerendering_has_been_cancelled_(false), |
erikchen | 00e7992 | 2018-03-16 20:52:57 | [diff] [blame] | 191 | process_pid_(base::kNullProcessId), |
[email protected] | 28a05f3a | 2011-05-20 15:05:08 | [diff] [blame] | 192 | child_id_(-1), |
[email protected] | 86d4ff46 | 2011-05-23 17:39:37 | [diff] [blame] | 193 | route_id_(-1), |
[email protected] | e253ab0 | 2011-07-07 21:58:39 | [diff] [blame] | 194 | origin_(origin), |
nigeltao | 9881067 | 2017-01-20 01:10:46 | [diff] [blame] | 195 | network_bytes_(0), |
| 196 | weak_factory_(this) { |
Ryan Sturm | d154e1e | 2019-06-01 01:18:59 | [diff] [blame] | 197 | switch (origin) { |
| 198 | case ORIGIN_OMNIBOX: |
| 199 | case ORIGIN_EXTERNAL_REQUEST: |
| 200 | case ORIGIN_EXTERNAL_REQUEST_FORCED_PRERENDER: |
Sofiya Semenova | ece7b54 | 2019-06-19 17:26:48 | [diff] [blame] | 201 | case ORIGIN_NAVIGATION_PREDICTOR: |
Ryan Sturm | d154e1e | 2019-06-01 01:18:59 | [diff] [blame] | 202 | DCHECK(!initiator_origin_.has_value()); |
| 203 | break; |
| 204 | |
| 205 | case ORIGIN_GWS_PRERENDER: |
| 206 | case ORIGIN_LINK_REL_PRERENDER_SAMEDOMAIN: |
| 207 | case ORIGIN_LINK_REL_PRERENDER_CROSSDOMAIN: |
| 208 | case ORIGIN_LINK_REL_NEXT: |
| 209 | DCHECK(initiator_origin_.has_value()); |
| 210 | break; |
| 211 | case ORIGIN_NONE: |
| 212 | case ORIGIN_MAX: |
| 213 | NOTREACHED(); |
| 214 | } |
| 215 | |
thestig | c98161a | 2016-07-20 01:42:49 | [diff] [blame] | 216 | DCHECK(prerender_manager); |
Ben Goodger | 9e24929e | 2017-07-21 03:13:40 | [diff] [blame] | 217 | registry_.AddInterface(base::Bind( |
| 218 | &PrerenderContents::OnPrerenderCancelerRequest, base::Unretained(this))); |
[email protected] | 8a6e416 | 2011-04-27 19:36:48 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | bool PrerenderContents::Init() { |
[email protected] | 2fc4da9 | 2011-05-27 16:24:31 | [diff] [blame] | 222 | return AddAliasURL(prerender_url_); |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 223 | } |
| 224 | |
mattcary | d4a8c77f | 2016-08-23 17:32:45 | [diff] [blame] | 225 | void PrerenderContents::SetPrerenderMode(PrerenderMode mode) { |
| 226 | DCHECK(!prerendering_has_started_); |
| 227 | prerender_mode_ = mode; |
| 228 | } |
| 229 | |
[email protected] | 5a8dffa | 2011-01-26 00:40:39 | [diff] [blame] | 230 | // static |
| 231 | PrerenderContents::Factory* PrerenderContents::CreateFactory() { |
| 232 | return new PrerenderContentsFactoryImpl(); |
| 233 | } |
| 234 | |
[email protected] | a303db7 | 2014-01-06 05:08:19 | [diff] [blame] | 235 | // static |
| 236 | PrerenderContents* PrerenderContents::FromWebContents( |
| 237 | content::WebContents* web_contents) { |
| 238 | if (!web_contents) |
| 239 | return NULL; |
droger | c154315 | 2016-09-20 13:03:37 | [diff] [blame] | 240 | PrerenderManager* prerender_manager = |
| 241 | PrerenderManagerFactory::GetForBrowserContext( |
| 242 | web_contents->GetBrowserContext()); |
[email protected] | a303db7 | 2014-01-06 05:08:19 | [diff] [blame] | 243 | if (!prerender_manager) |
| 244 | return NULL; |
| 245 | return prerender_manager->GetPrerenderContents(web_contents); |
| 246 | } |
| 247 | |
[email protected] | 71b5d24 | 2011-04-30 02:27:20 | [diff] [blame] | 248 | void PrerenderContents::StartPrerendering( |
yusufo | 7c8d964 | 2016-08-22 17:41:07 | [diff] [blame] | 249 | const gfx::Rect& bounds, |
davidben | 879199c | 2015-03-06 00:55:04 | [diff] [blame] | 250 | SessionStorageNamespace* session_storage_namespace) { |
thestig | c98161a | 2016-07-20 01:42:49 | [diff] [blame] | 251 | DCHECK(profile_); |
yusufo | 7c8d964 | 2016-08-22 17:41:07 | [diff] [blame] | 252 | DCHECK(!bounds.IsEmpty()); |
[email protected] | 71b5d24 | 2011-04-30 02:27:20 | [diff] [blame] | 253 | DCHECK(!prerendering_has_started_); |
melandory | 1346cde | 2016-06-11 00:42:12 | [diff] [blame] | 254 | DCHECK(!prerender_contents_); |
[email protected] | 2736c03 | 2012-05-11 18:06:07 | [diff] [blame] | 255 | DCHECK_EQ(1U, alias_urls_.size()); |
[email protected] | 71b5d24 | 2011-04-30 02:27:20 | [diff] [blame] | 256 | |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 257 | session_storage_namespace_id_ = session_storage_namespace->id(); |
yusufo | 7c8d964 | 2016-08-22 17:41:07 | [diff] [blame] | 258 | bounds_ = bounds; |
[email protected] | 71b5d24 | 2011-04-30 02:27:20 | [diff] [blame] | 259 | |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 260 | DCHECK(load_start_time_.is_null()); |
| 261 | load_start_time_ = base::TimeTicks::Now(); |
| 262 | |
[email protected] | eb9857c | 2012-05-29 22:50:17 | [diff] [blame] | 263 | prerendering_has_started_ = true; |
| 264 | |
erikchen | ade1fef | 2018-05-02 22:07:57 | [diff] [blame] | 265 | prerender_contents_ = CreateWebContents(session_storage_namespace); |
Bryan McQuade | d2b93c0 | 2017-07-14 22:26:31 | [diff] [blame] | 266 | TabHelpers::AttachTabHelpers(prerender_contents_.get()); |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 267 | content::WebContentsObserver::Observe(prerender_contents_.get()); |
[email protected] | 2736c03 | 2012-05-11 18:06:07 | [diff] [blame] | 268 | |
afakhry | 674c7b1 | 2015-06-23 22:56:12 | [diff] [blame] | 269 | // Tag the prerender contents with the task manager specific prerender tag, so |
| 270 | // that it shows up in the task manager. |
avi | 24d693f | 2016-08-06 18:03:52 | [diff] [blame] | 271 | task_manager::WebContentsTags::CreateForPrerenderContents( |
afakhry | 674c7b1 | 2015-06-23 22:56:12 | [diff] [blame] | 272 | prerender_contents_.get()); |
| 273 | |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 274 | web_contents_delegate_.reset(new WebContentsDelegateImpl(this)); |
| 275 | prerender_contents_.get()->SetDelegate(web_contents_delegate_.get()); |
[email protected] | 0932b30c | 2012-04-17 13:25:10 | [diff] [blame] | 276 | // Set the size of the prerender WebContents. |
yusufo | 7c8d964 | 2016-08-22 17:41:07 | [diff] [blame] | 277 | ResizeWebContents(prerender_contents_.get(), bounds_); |
[email protected] | 208ce7e | 2011-08-27 04:35:13 | [diff] [blame] | 278 | |
davidben | 879199c | 2015-03-06 00:55:04 | [diff] [blame] | 279 | // TODO(davidben): This logic assumes each prerender has at most one |
| 280 | // route. https://crbug.com/440544 |
[email protected] | cfd820d | 2012-04-29 16:29:07 | [diff] [blame] | 281 | child_id_ = GetRenderViewHost()->GetProcess()->GetID(); |
| 282 | route_id_ = GetRenderViewHost()->GetRoutingID(); |
[email protected] | ac1f70b | 2011-05-03 00:46:05 | [diff] [blame] | 283 | |
davidben | 879199c | 2015-03-06 00:55:04 | [diff] [blame] | 284 | // TODO(davidben): This logic assumes each prerender has at most one |
| 285 | // process. https://crbug.com/440544 |
[email protected] | 28c5d0b7 | 2014-05-13 08:19:59 | [diff] [blame] | 286 | prerender_manager()->AddPrerenderProcessHost( |
| 287 | GetRenderViewHost()->GetProcess()); |
| 288 | |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 289 | NotifyPrerenderStart(); |
[email protected] | ac1f70b | 2011-05-03 00:46:05 | [diff] [blame] | 290 | |
| 291 | // Close ourselves when the application is shutting down. |
[email protected] | 966c9a4 | 2012-09-25 14:40:21 | [diff] [blame] | 292 | notification_registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 293 | content::NotificationService::AllSources()); |
[email protected] | ac1f70b | 2011-05-03 00:46:05 | [diff] [blame] | 294 | |
[email protected] | 165602b | 2011-05-05 14:46:52 | [diff] [blame] | 295 | // Register to inform new RenderViews that we're prerendering. |
| 296 | notification_registrar_.Add( |
[email protected] | d53a08c | 2012-07-18 20:35:30 | [diff] [blame] | 297 | this, content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 298 | content::Source<WebContents>(prerender_contents_.get())); |
[email protected] | 165602b | 2011-05-05 14:46:52 | [diff] [blame] | 299 | |
[email protected] | bf70edce | 2012-06-20 22:32:22 | [diff] [blame] | 300 | // Transfer over the user agent override. |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 301 | prerender_contents_.get()->SetUserAgentOverride( |
Changwan Ryu | c1134a8 | 2018-03-07 02:10:14 | [diff] [blame] | 302 | prerender_manager_->config().user_agent_override, false); |
[email protected] | bf70edce | 2012-06-20 22:32:22 | [diff] [blame] | 303 | |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 304 | content::NavigationController::LoadURLParams load_url_params( |
| 305 | prerender_url_); |
| 306 | load_url_params.referrer = referrer_; |
Ryan Sturm | d154e1e | 2019-06-01 01:18:59 | [diff] [blame] | 307 | load_url_params.initiator_origin = initiator_origin_; |
Sylvain Defresne | c6ccc77d | 2014-09-19 10:19:35 | [diff] [blame] | 308 | load_url_params.transition_type = ui::PAGE_TRANSITION_LINK; |
[email protected] | 8b18b4a | 2014-02-28 05:53:49 | [diff] [blame] | 309 | if (origin_ == ORIGIN_OMNIBOX) { |
Sylvain Defresne | c6ccc77d | 2014-09-19 10:19:35 | [diff] [blame] | 310 | load_url_params.transition_type = ui::PageTransitionFromInt( |
| 311 | ui::PAGE_TRANSITION_TYPED | |
| 312 | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); |
Sofiya Semenova | ece7b54 | 2019-06-19 17:26:48 | [diff] [blame] | 313 | } else if (origin_ == ORIGIN_NAVIGATION_PREDICTOR) { |
| 314 | load_url_params.transition_type = |
| 315 | ui::PageTransitionFromInt(ui::PAGE_TRANSITION_GENERATED); |
[email protected] | 8b18b4a | 2014-02-28 05:53:49 | [diff] [blame] | 316 | } |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 317 | load_url_params.override_user_agent = |
| 318 | prerender_manager_->config().is_overriding_user_agent ? |
| 319 | content::NavigationController::UA_OVERRIDE_TRUE : |
| 320 | content::NavigationController::UA_OVERRIDE_FALSE; |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 321 | prerender_contents_.get()->GetController().LoadURLWithParams(load_url_params); |
[email protected] | 71b5d24 | 2011-04-30 02:27:20 | [diff] [blame] | 322 | } |
| 323 | |
[email protected] | 55e98aa | 2011-03-23 17:10:32 | [diff] [blame] | 324 | bool PrerenderContents::GetChildId(int* child_id) const { |
| 325 | CHECK(child_id); |
[email protected] | 28a05f3a | 2011-05-20 15:05:08 | [diff] [blame] | 326 | DCHECK_GE(child_id_, -1); |
| 327 | *child_id = child_id_; |
| 328 | return child_id_ != -1; |
[email protected] | 55e98aa | 2011-03-23 17:10:32 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | bool PrerenderContents::GetRouteId(int* route_id) const { |
| 332 | CHECK(route_id); |
[email protected] | 28a05f3a | 2011-05-20 15:05:08 | [diff] [blame] | 333 | DCHECK_GE(route_id_, -1); |
| 334 | *route_id = route_id_; |
| 335 | return route_id_ != -1; |
[email protected] | 55e98aa | 2011-03-23 17:10:32 | [diff] [blame] | 336 | } |
| 337 | |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 338 | void PrerenderContents::SetFinalStatus(FinalStatus final_status) { |
[email protected] | 099d672 | 2014-02-04 01:13:03 | [diff] [blame] | 339 | DCHECK_GE(final_status, FINAL_STATUS_USED); |
| 340 | DCHECK_LT(final_status, FINAL_STATUS_MAX); |
| 341 | |
Ryan Sturm | 74e5c53 | 2019-04-05 23:29:47 | [diff] [blame] | 342 | DCHECK_EQ(FINAL_STATUS_UNKNOWN, final_status_); |
[email protected] | ec17231bb | 2011-02-16 20:06:09 | [diff] [blame] | 343 | |
[email protected] | 19440b2 | 2011-01-31 18:52:16 | [diff] [blame] | 344 | final_status_ = final_status; |
| 345 | } |
| 346 | |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 347 | PrerenderContents::~PrerenderContents() { |
Ryan Sturm | 74e5c53 | 2019-04-05 23:29:47 | [diff] [blame] | 348 | DCHECK_NE(FINAL_STATUS_UNKNOWN, final_status()); |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 349 | DCHECK( |
| 350 | prerendering_has_been_cancelled() || final_status() == FINAL_STATUS_USED); |
| 351 | DCHECK_NE(ORIGIN_MAX, origin()); |
davidben | f47d191 | 2015-03-14 00:24:24 | [diff] [blame] | 352 | |
pasko | 6ee988e | 2016-05-12 20:23:22 | [diff] [blame] | 353 | prerender_manager_->RecordFinalStatus(origin(), final_status()); |
Egor Pasko | b2d8479 | 2017-09-06 01:43:40 | [diff] [blame] | 354 | prerender_manager_->RecordNetworkBytesConsumed(origin(), network_bytes_); |
[email protected] | 3adcb41 | 2014-02-14 14:59:01 | [diff] [blame] | 355 | |
Egor Pasko | f0c1180 | 2019-03-22 13:52:31 | [diff] [blame] | 356 | if (prerender_mode_ == DEPRECATED_FULL_PRERENDER) { |
Alexander Yashkin | 6b058fe | 2019-01-16 11:26:59 | [diff] [blame] | 357 | // Broadcast the removal of aliases. |
| 358 | for (content::RenderProcessHost::iterator host_iterator = |
| 359 | content::RenderProcessHost::AllHostsIterator(); |
| 360 | !host_iterator.IsAtEnd(); host_iterator.Advance()) { |
| 361 | content::RenderProcessHost* host = host_iterator.GetCurrentValue(); |
| 362 | IPC::ChannelProxy* channel = host->GetChannel(); |
| 363 | // |channel| might be NULL in tests. |
| 364 | if (host->IsInitializedAndNotDead() && channel) { |
| 365 | chrome::mojom::PrerenderDispatcherAssociatedPtr prerender_dispatcher; |
| 366 | channel->GetRemoteAssociatedInterface(&prerender_dispatcher); |
| 367 | prerender_dispatcher->PrerenderRemoveAliases(alias_urls_); |
| 368 | } |
cm.sanchi | c54c999 | 2018-01-15 05:27:24 | [diff] [blame] | 369 | } |
[email protected] | f5b9c1b52 | 2013-05-15 09:50:10 | [diff] [blame] | 370 | } |
| 371 | |
thestig | c98161a | 2016-07-20 01:42:49 | [diff] [blame] | 372 | if (!prerender_contents_) |
| 373 | return; |
| 374 | |
[email protected] | 0932b30c | 2012-04-17 13:25:10 | [diff] [blame] | 375 | // If we still have a WebContents, clean up anything we need to and then |
[email protected] | d3c79f39 | 2011-05-20 20:04:56 | [diff] [blame] | 376 | // destroy it. |
thestig | c98161a | 2016-07-20 01:42:49 | [diff] [blame] | 377 | std::unique_ptr<WebContents> contents = ReleasePrerenderContents(); |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 378 | } |
| 379 | |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 380 | void PrerenderContents::AddObserver(Observer* observer) { |
Ryan Sturm | 74e5c53 | 2019-04-05 23:29:47 | [diff] [blame] | 381 | DCHECK_EQ(FINAL_STATUS_UNKNOWN, final_status_); |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 382 | observer_list_.AddObserver(observer); |
| 383 | } |
| 384 | |
[email protected] | 2610170 | 2012-12-15 21:45:18 | [diff] [blame] | 385 | void PrerenderContents::RemoveObserver(Observer* observer) { |
| 386 | observer_list_.RemoveObserver(observer); |
| 387 | } |
| 388 | |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 389 | void PrerenderContents::Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 390 | const content::NotificationSource& source, |
| 391 | const content::NotificationDetails& details) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 392 | switch (type) { |
[email protected] | b9b5c5c | 2014-02-25 05:44:35 | [diff] [blame] | 393 | // TODO(davidben): Try to remove this in favor of relying on |
| 394 | // FINAL_STATUS_PROFILE_DESTROYED. |
[email protected] | 966c9a4 | 2012-09-25 14:40:21 | [diff] [blame] | 395 | case chrome::NOTIFICATION_APP_TERMINATING: |
[email protected] | 19440b2 | 2011-01-31 18:52:16 | [diff] [blame] | 396 | Destroy(FINAL_STATUS_APP_TERMINATING); |
| 397 | return; |
[email protected] | 549e89a | 2011-02-04 18:02:31 | [diff] [blame] | 398 | |
[email protected] | d53a08c | 2012-07-18 20:35:30 | [diff] [blame] | 399 | case content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED: { |
[email protected] | 165602b | 2011-05-05 14:46:52 | [diff] [blame] | 400 | if (prerender_contents_.get()) { |
[email protected] | fbc5e5f9 | 2012-01-02 06:08:32 | [diff] [blame] | 401 | DCHECK_EQ(content::Source<WebContents>(source).ptr(), |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 402 | prerender_contents_.get()); |
[email protected] | 165602b | 2011-05-05 14:46:52 | [diff] [blame] | 403 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 404 | content::Details<RenderViewHost> new_render_view_host(details); |
[email protected] | b54c6c6 | 2011-05-12 15:04:04 | [diff] [blame] | 405 | OnRenderViewHostCreated(new_render_view_host.ptr()); |
| 406 | |
[email protected] | 5725b2e | 2011-09-01 13:11:27 | [diff] [blame] | 407 | // Make sure the size of the RenderViewHost has been passed to the new |
| 408 | // RenderView. Otherwise, the size may not be sent until the |
| 409 | // RenderViewReady event makes it from the render process to the UI |
| 410 | // thread of the browser process. When the RenderView receives its |
| 411 | // size, is also sets itself to be visible, which would then break the |
| 412 | // visibility API. |
Fady Samuel | 0b91182 | 2018-04-25 13:22:16 | [diff] [blame] | 413 | new_render_view_host->GetWidget()->SynchronizeVisualProperties(); |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 414 | prerender_contents_->WasHidden(); |
[email protected] | 208ce7e | 2011-08-27 04:35:13 | [diff] [blame] | 415 | } |
[email protected] | 5725b2e | 2011-09-01 13:11:27 | [diff] [blame] | 416 | break; |
[email protected] | 208ce7e | 2011-08-27 04:35:13 | [diff] [blame] | 417 | } |
| 418 | |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 419 | default: |
| 420 | NOTREACHED() << "Unexpected notification sent."; |
| 421 | break; |
| 422 | } |
| 423 | } |
| 424 | |
[email protected] | b54c6c6 | 2011-05-12 15:04:04 | [diff] [blame] | 425 | void PrerenderContents::OnRenderViewHostCreated( |
| 426 | RenderViewHost* new_render_view_host) { |
| 427 | } |
| 428 | |
erikchen | ade1fef | 2018-05-02 22:07:57 | [diff] [blame] | 429 | std::unique_ptr<WebContents> PrerenderContents::CreateWebContents( |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 430 | SessionStorageNamespace* session_storage_namespace) { |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 431 | // TODO(ajwong): Remove the temporary map once prerendering is aware of |
| 432 | // multiple session storage namespaces per tab. |
| 433 | content::SessionStorageNamespaceMap session_storage_namespace_map; |
| 434 | session_storage_namespace_map[std::string()] = session_storage_namespace; |
Bryan McQuade | d2b93c0 | 2017-07-14 22:26:31 | [diff] [blame] | 435 | return WebContents::CreateWithSessionStorage( |
| 436 | WebContents::CreateParams(profile_), session_storage_namespace_map); |
[email protected] | de1fcdd | 2012-04-06 23:01:07 | [diff] [blame] | 437 | } |
| 438 | |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 439 | void PrerenderContents::NotifyPrerenderStart() { |
Ryan Sturm | 74e5c53 | 2019-04-05 23:29:47 | [diff] [blame] | 440 | DCHECK_EQ(FINAL_STATUS_UNKNOWN, final_status_); |
ericwilligers | 58b0e16 | 2016-10-21 07:15:56 | [diff] [blame] | 441 | for (Observer& observer : observer_list_) |
| 442 | observer.OnPrerenderStart(this); |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 443 | } |
| 444 | |
[email protected] | 49fc07b | 2013-01-03 21:05:22 | [diff] [blame] | 445 | void PrerenderContents::NotifyPrerenderStopLoading() { |
ericwilligers | 58b0e16 | 2016-10-21 07:15:56 | [diff] [blame] | 446 | for (Observer& observer : observer_list_) |
| 447 | observer.OnPrerenderStopLoading(this); |
[email protected] | 49fc07b | 2013-01-03 21:05:22 | [diff] [blame] | 448 | } |
| 449 | |
[email protected] | 59000ecd | 2014-02-12 00:32:04 | [diff] [blame] | 450 | void PrerenderContents::NotifyPrerenderDomContentLoaded() { |
ericwilligers | 58b0e16 | 2016-10-21 07:15:56 | [diff] [blame] | 451 | for (Observer& observer : observer_list_) |
| 452 | observer.OnPrerenderDomContentLoaded(this); |
[email protected] | 59000ecd | 2014-02-12 00:32:04 | [diff] [blame] | 453 | } |
| 454 | |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 455 | void PrerenderContents::NotifyPrerenderStop() { |
Ryan Sturm | 74e5c53 | 2019-04-05 23:29:47 | [diff] [blame] | 456 | DCHECK_NE(FINAL_STATUS_UNKNOWN, final_status_); |
ericwilligers | 58b0e16 | 2016-10-21 07:15:56 | [diff] [blame] | 457 | for (Observer& observer : observer_list_) |
| 458 | observer.OnPrerenderStop(this); |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 459 | observer_list_.Clear(); |
| 460 | } |
| 461 | |
[email protected] | bde57b8 | 2013-11-01 23:10:44 | [diff] [blame] | 462 | bool PrerenderContents::CheckURL(const GURL& url) { |
[email protected] | 08a19e13 | 2014-01-10 18:41:45 | [diff] [blame] | 463 | if (!url.SchemeIsHTTPOrHTTPS()) { |
[email protected] | f9034cf | 2011-07-21 12:43:41 | [diff] [blame] | 464 | Destroy(FINAL_STATUS_UNSUPPORTED_SCHEME); |
| 465 | return false; |
| 466 | } |
Egor Pasko | 3dd9c71 | 2017-10-12 13:31:22 | [diff] [blame] | 467 | if (prerender_manager_->HasRecentlyBeenNavigatedTo(origin(), url)) { |
[email protected] | 84b5da9 | 2011-05-19 17:53:56 | [diff] [blame] | 468 | Destroy(FINAL_STATUS_RECENTLY_VISITED); |
| 469 | return false; |
| 470 | } |
[email protected] | bde57b8 | 2013-11-01 23:10:44 | [diff] [blame] | 471 | return true; |
| 472 | } |
| 473 | |
| 474 | bool PrerenderContents::AddAliasURL(const GURL& url) { |
| 475 | if (!CheckURL(url)) |
| 476 | return false; |
[email protected] | f9034cf | 2011-07-21 12:43:41 | [diff] [blame] | 477 | |
[email protected] | f39506a8 | 2011-01-18 23:46:38 | [diff] [blame] | 478 | alias_urls_.push_back(url); |
[email protected] | f5b9c1b52 | 2013-05-15 09:50:10 | [diff] [blame] | 479 | |
Egor Pasko | f0c1180 | 2019-03-22 13:52:31 | [diff] [blame] | 480 | if (prerender_mode_ == DEPRECATED_FULL_PRERENDER) { |
Alexander Yashkin | 6b058fe | 2019-01-16 11:26:59 | [diff] [blame] | 481 | for (content::RenderProcessHost::iterator host_iterator = |
| 482 | content::RenderProcessHost::AllHostsIterator(); |
| 483 | !host_iterator.IsAtEnd(); host_iterator.Advance()) { |
| 484 | content::RenderProcessHost* host = host_iterator.GetCurrentValue(); |
| 485 | IPC::ChannelProxy* channel = host->GetChannel(); |
| 486 | // |channel| might be NULL in tests. |
| 487 | if (host->IsInitializedAndNotDead() && channel) { |
| 488 | chrome::mojom::PrerenderDispatcherAssociatedPtr prerender_dispatcher; |
| 489 | channel->GetRemoteAssociatedInterface(&prerender_dispatcher); |
| 490 | prerender_dispatcher->PrerenderAddAlias(url); |
| 491 | } |
cm.sanchi | c54c999 | 2018-01-15 05:27:24 | [diff] [blame] | 492 | } |
[email protected] | f5b9c1b52 | 2013-05-15 09:50:10 | [diff] [blame] | 493 | } |
| 494 | |
[email protected] | d085f5c | 2011-02-20 12:17:47 | [diff] [blame] | 495 | return true; |
[email protected] | f39506a8 | 2011-01-18 23:46:38 | [diff] [blame] | 496 | } |
| 497 | |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 498 | bool PrerenderContents::Matches( |
| 499 | const GURL& url, |
Lucas Furukawa Gadani | d726e1e | 2019-05-08 16:20:03 | [diff] [blame] | 500 | SessionStorageNamespace* session_storage_namespace) const { |
davidben | 1f9befb | 2015-01-14 18:14:05 | [diff] [blame] | 501 | // TODO(davidben): Remove any consumers that pass in a NULL |
| 502 | // session_storage_namespace and only test with matches. |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 503 | if (session_storage_namespace && |
[email protected] | 75d90c1 | 2013-03-20 19:50:16 | [diff] [blame] | 504 | session_storage_namespace_id_ != session_storage_namespace->id()) { |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 505 | return false; |
[email protected] | 75d90c1 | 2013-03-20 19:50:16 | [diff] [blame] | 506 | } |
Jan Wilken Dörrie | a8cb5630 | 2019-06-06 18:59:36 | [diff] [blame] | 507 | return base::Contains(alias_urls_, url); |
[email protected] | f39506a8 | 2011-01-18 23:46:38 | [diff] [blame] | 508 | } |
[email protected] | 973407b | 2011-01-26 23:18:23 | [diff] [blame] | 509 | |
[email protected] | 58d5cfe | 2013-07-10 02:40:52 | [diff] [blame] | 510 | void PrerenderContents::RenderProcessGone(base::TerminationStatus status) { |
pasko | 339bce6e | 2016-10-20 12:14:17 | [diff] [blame] | 511 | if (status == base::TERMINATION_STATUS_STILL_RUNNING) { |
| 512 | // The renderer process is being killed because of the browser/test |
| 513 | // shutdown, before the termination notification is received. |
| 514 | Destroy(FINAL_STATUS_APP_TERMINATING); |
| 515 | } |
[email protected] | 5ee38b88 | 2011-05-05 00:18:57 | [diff] [blame] | 516 | Destroy(FINAL_STATUS_RENDERER_CRASHED); |
| 517 | } |
| 518 | |
Ben Goodger | 9e24929e | 2017-07-21 03:13:40 | [diff] [blame] | 519 | void PrerenderContents::OnInterfaceRequestFromFrame( |
| 520 | content::RenderFrameHost* render_frame_host, |
| 521 | const std::string& interface_name, |
| 522 | mojo::ScopedMessagePipeHandle* interface_pipe) { |
| 523 | registry_.TryBindInterface(interface_name, interface_pipe); |
| 524 | } |
| 525 | |
[email protected] | b849847b | 2013-12-10 21:57:58 | [diff] [blame] | 526 | void PrerenderContents::RenderFrameCreated( |
| 527 | content::RenderFrameHost* render_frame_host) { |
[email protected] | b849847b | 2013-12-10 21:57:58 | [diff] [blame] | 528 | // When a new RenderFrame is created for a prerendering WebContents, tell the |
| 529 | // new RenderFrame it's being used for prerendering before any navigations |
| 530 | // occur. Note that this is always triggered before the first navigation, so |
| 531 | // there's no need to send the message just after the WebContents is created. |
| 532 | render_frame_host->Send(new PrerenderMsg_SetIsPrerendering( |
John Abd-El-Malek | d237798 | 2018-01-08 22:23:12 | [diff] [blame] | 533 | render_frame_host->GetRoutingID(), prerender_mode_, |
| 534 | PrerenderHistograms::GetHistogramPrefix(origin_))); |
[email protected] | b849847b | 2013-12-10 21:57:58 | [diff] [blame] | 535 | } |
| 536 | |
fdegans | 6ce28f5 | 2015-03-19 12:52:22 | [diff] [blame] | 537 | void PrerenderContents::DidStopLoading() { |
[email protected] | 49fc07b | 2013-01-03 21:05:22 | [diff] [blame] | 538 | NotifyPrerenderStopLoading(); |
[email protected] | 973407b | 2011-01-26 23:18:23 | [diff] [blame] | 539 | } |
[email protected] | 19440b2 | 2011-01-31 18:52:16 | [diff] [blame] | 540 | |
[email protected] | 59000ecd | 2014-02-12 00:32:04 | [diff] [blame] | 541 | void PrerenderContents::DocumentLoadedInFrame( |
[email protected] | 3619ca8 | 2014-07-09 15:45:14 | [diff] [blame] | 542 | content::RenderFrameHost* render_frame_host) { |
| 543 | if (!render_frame_host->GetParent()) |
[email protected] | 59000ecd | 2014-02-12 00:32:04 | [diff] [blame] | 544 | NotifyPrerenderDomContentLoaded(); |
| 545 | } |
| 546 | |
jam | e562635 | 2017-01-26 20:15:23 | [diff] [blame] | 547 | void PrerenderContents::DidStartNavigation( |
| 548 | content::NavigationHandle* navigation_handle) { |
eugenebut | a11672fb | 2017-03-07 17:13:51 | [diff] [blame] | 549 | if (!navigation_handle->IsInMainFrame() || |
| 550 | navigation_handle->IsSameDocument()) { |
jam | e562635 | 2017-01-26 20:15:23 | [diff] [blame] | 551 | return; |
eugenebut | a11672fb | 2017-03-07 17:13:51 | [diff] [blame] | 552 | } |
[email protected] | 5f5b9e4cf | 2011-09-07 21:26:05 | [diff] [blame] | 553 | |
jam | e562635 | 2017-01-26 20:15:23 | [diff] [blame] | 554 | if (!CheckURL(navigation_handle->GetURL())) |
| 555 | return; |
| 556 | |
| 557 | // Usually, this event fires if the user clicks or enters a new URL. |
| 558 | // Neither of these can happen in the case of an invisible prerender. |
| 559 | // So the cause is: Some JavaScript caused a new URL to be loaded. In that |
| 560 | // case, the spinner would start again in the browser, so we must reset |
Egor Pasko | 7d7e5cd | 2018-05-28 14:54:28 | [diff] [blame] | 561 | // has_finished_loading_ so that the spinner won't be stopped. |
jam | e562635 | 2017-01-26 20:15:23 | [diff] [blame] | 562 | has_finished_loading_ = false; |
[email protected] | 5f5b9e4cf | 2011-09-07 21:26:05 | [diff] [blame] | 563 | } |
| 564 | |
John Abd-El-Malek | cb599d07 | 2017-11-22 21:36:00 | [diff] [blame] | 565 | void PrerenderContents::DidRedirectNavigation( |
| 566 | content::NavigationHandle* navigation_handle) { |
| 567 | if (!navigation_handle->IsInMainFrame()) |
| 568 | return; |
| 569 | |
| 570 | // If it's a redirect on the top-level resource, the name needs to be |
| 571 | // remembered for future matching, and if it redirects to an https resource, |
| 572 | // it needs to be canceled. If a subresource is redirected, nothing changes. |
| 573 | CheckURL(navigation_handle->GetURL()); |
| 574 | } |
| 575 | |
[email protected] | 3619ca8 | 2014-07-09 15:45:14 | [diff] [blame] | 576 | void PrerenderContents::DidFinishLoad( |
[email protected] | 860234a | 2014-07-01 00:35:31 | [diff] [blame] | 577 | content::RenderFrameHost* render_frame_host, |
[email protected] | 3619ca8 | 2014-07-09 15:45:14 | [diff] [blame] | 578 | const GURL& validated_url) { |
| 579 | if (!render_frame_host->GetParent()) |
[email protected] | 6974ae7e | 2012-02-16 01:17:59 | [diff] [blame] | 580 | has_finished_loading_ = true; |
| 581 | } |
| 582 | |
jam | e562635 | 2017-01-26 20:15:23 | [diff] [blame] | 583 | void PrerenderContents::DidFinishNavigation( |
| 584 | content::NavigationHandle* navigation_handle) { |
| 585 | if (!navigation_handle->IsInMainFrame() || |
| 586 | !navigation_handle->HasCommitted() || |
| 587 | navigation_handle->IsErrorPage()) { |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | if (navigation_handle->GetResponseHeaders() && |
| 592 | navigation_handle->GetResponseHeaders()->response_code() >= 400) { |
| 593 | // Maintain same behavior as old navigation API when the URL is unreachable |
| 594 | // and leads to an error page. While there will be a subsequent navigation |
| 595 | // that has navigation_handle->IsErrorPage(), it'll be too late to wait for |
| 596 | // it as the renderer side will consider this prerender complete. This |
| 597 | // object would therefore have been destructed already and so instead look |
| 598 | // for the error response code now. |
| 599 | // Also maintain same final status code that previous navigation API |
| 600 | // returned, which was reached because the URL for the error page was |
| 601 | // kUnreachableWebDataURL and that was interpreted as unsupported scheme. |
| 602 | Destroy(FINAL_STATUS_UNSUPPORTED_SCHEME); |
| 603 | return; |
| 604 | } |
| 605 | |
[email protected] | ab5871b | 2013-11-12 20:56:55 | [diff] [blame] | 606 | // If the prerender made a second navigation entry, abort the prerender. This |
| 607 | // avoids having to correctly implement a complex history merging case (this |
| 608 | // interacts with location.replace) and correctly synchronize with the |
| 609 | // renderer. The final status may be monitored to see we need to revisit this |
| 610 | // decision. This does not affect client redirects as those do not push new |
| 611 | // history entries. (Calls to location.replace, navigations before onload, and |
| 612 | // <meta http-equiv=refresh> with timeouts under 1 second do not create |
| 613 | // entries in Blink.) |
| 614 | if (prerender_contents_->GetController().GetEntryCount() > 1) { |
| 615 | Destroy(FINAL_STATUS_NEW_NAVIGATION_ENTRY); |
| 616 | return; |
| 617 | } |
| 618 | |
jam | e562635 | 2017-01-26 20:15:23 | [diff] [blame] | 619 | // Add each redirect as an alias. |navigation_handle->GetURL()| is included in |
| 620 | // |navigation_handle->GetRedirectChain()|. |
[email protected] | bde57b8 | 2013-11-01 23:10:44 | [diff] [blame] | 621 | // |
| 622 | // TODO(davidben): We do not correctly patch up history for renderer-initated |
| 623 | // navigations which add history entries. http://crbug.com/305660. |
jam | e562635 | 2017-01-26 20:15:23 | [diff] [blame] | 624 | for (const auto& redirect : navigation_handle->GetRedirectChain()) { |
| 625 | if (!AddAliasURL(redirect)) |
[email protected] | bde57b8 | 2013-11-01 23:10:44 | [diff] [blame] | 626 | return; |
| 627 | } |
| 628 | } |
| 629 | |
[email protected] | 19440b2 | 2011-01-31 18:52:16 | [diff] [blame] | 630 | void PrerenderContents::Destroy(FinalStatus final_status) { |
[email protected] | 2610170 | 2012-12-15 21:45:18 | [diff] [blame] | 631 | DCHECK_NE(final_status, FINAL_STATUS_USED); |
| 632 | |
[email protected] | e348af7 | 2011-05-23 21:02:49 | [diff] [blame] | 633 | if (prerendering_has_been_cancelled_) |
[email protected] | 5ee38b88 | 2011-05-05 00:18:57 | [diff] [blame] | 634 | return; |
[email protected] | 4c154ff8 | 2011-02-15 11:23:59 | [diff] [blame] | 635 | |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 636 | SetFinalStatus(final_status); |
[email protected] | e348af7 | 2011-05-23 21:02:49 | [diff] [blame] | 637 | |
[email protected] | 49308e28 | 2011-08-11 00:17:40 | [diff] [blame] | 638 | prerendering_has_been_cancelled_ = true; |
[email protected] | 128c7406 | 2012-11-27 17:55:53 | [diff] [blame] | 639 | prerender_manager_->AddToHistory(this); |
Egor Pasko | 6fed382 | 2018-05-30 10:33:14 | [diff] [blame] | 640 | prerender_manager_->SetPrefetchFinalStatusForUrl(prerender_url_, |
| 641 | final_status); |
[email protected] | 447f9ab | 2011-11-14 23:56:35 | [diff] [blame] | 642 | prerender_manager_->MoveEntryToPendingDelete(this, final_status); |
[email protected] | 49308e28 | 2011-08-11 00:17:40 | [diff] [blame] | 643 | |
droger | 43d1d1a | 2016-11-25 16:01:09 | [diff] [blame] | 644 | if (prerendering_has_started()) |
[email protected] | 2610170 | 2012-12-15 21:45:18 | [diff] [blame] | 645 | NotifyPrerenderStop(); |
[email protected] | ba9f8fb | 2011-02-24 20:19:26 | [diff] [blame] | 646 | } |
| 647 | |
erikchen | 00e7992 | 2018-03-16 20:52:57 | [diff] [blame] | 648 | void PrerenderContents::DestroyWhenUsingTooManyResources() { |
| 649 | if (process_pid_ == base::kNullProcessId) { |
Lucas Furukawa Gadani | 95fc361 | 2019-04-05 22:33:49 | [diff] [blame] | 650 | RenderViewHost* rvh = GetRenderViewHost(); |
thestig | 0df2bae8 | 2016-07-26 17:59:36 | [diff] [blame] | 651 | if (!rvh) |
erikchen | 00e7992 | 2018-03-16 20:52:57 | [diff] [blame] | 652 | return; |
thestig | 0df2bae8 | 2016-07-26 17:59:36 | [diff] [blame] | 653 | |
Lucas Furukawa Gadani | 79951a2 | 2018-12-12 18:16:02 | [diff] [blame] | 654 | content::RenderProcessHost* rph = rvh->GetProcess(); |
thestig | 0df2bae8 | 2016-07-26 17:59:36 | [diff] [blame] | 655 | if (!rph) |
erikchen | 00e7992 | 2018-03-16 20:52:57 | [diff] [blame] | 656 | return; |
thestig | 0df2bae8 | 2016-07-26 17:59:36 | [diff] [blame] | 657 | |
Sigurdur Asgeirsson | eb95666d | 2018-04-16 16:16:03 | [diff] [blame] | 658 | base::ProcessHandle handle = rph->GetProcess().Handle(); |
[email protected] | ba9f8fb | 2011-02-24 20:19:26 | [diff] [blame] | 659 | if (handle == base::kNullProcessHandle) |
erikchen | 00e7992 | 2018-03-16 20:52:57 | [diff] [blame] | 660 | return; |
thestig | 0df2bae8 | 2016-07-26 17:59:36 | [diff] [blame] | 661 | |
Sigurdur Asgeirsson | eb95666d | 2018-04-16 16:16:03 | [diff] [blame] | 662 | process_pid_ = rph->GetProcess().Pid(); |
[email protected] | ba9f8fb | 2011-02-24 20:19:26 | [diff] [blame] | 663 | } |
| 664 | |
erikchen | 00e7992 | 2018-03-16 20:52:57 | [diff] [blame] | 665 | if (process_pid_ == base::kNullProcessId) |
[email protected] | ba9f8fb | 2011-02-24 20:19:26 | [diff] [blame] | 666 | return; |
| 667 | |
erikchen | 00e7992 | 2018-03-16 20:52:57 | [diff] [blame] | 668 | // Using AdaptCallbackForRepeating allows for an easier transition to |
| 669 | // OnceCallbacks for https://crbug.com/714018. |
| 670 | memory_instrumentation::MemoryInstrumentation::GetInstance() |
erikchen | 0c634107 | 2018-06-15 22:05:06 | [diff] [blame] | 671 | ->RequestPrivateMemoryFootprint( |
| 672 | process_pid_, base::AdaptCallbackForRepeating(base::BindOnce( |
| 673 | &PrerenderContents::DidGetMemoryUsage, |
| 674 | weak_factory_.GetWeakPtr()))); |
erikchen | 00e7992 | 2018-03-16 20:52:57 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | void PrerenderContents::DidGetMemoryUsage( |
| 678 | bool success, |
| 679 | std::unique_ptr<memory_instrumentation::GlobalMemoryDump> global_dump) { |
| 680 | if (!success) |
| 681 | return; |
| 682 | |
| 683 | for (const memory_instrumentation::GlobalMemoryDump::ProcessDump& dump : |
| 684 | global_dump->process_dumps()) { |
| 685 | if (dump.pid() != process_pid_) |
| 686 | continue; |
| 687 | |
| 688 | // If |final_status_| == |FINAL_STATUS_USED|, then destruction will be |
| 689 | // handled by the entity that set final_status_. |
| 690 | if (dump.os_dump().private_footprint_kb * 1024 > |
| 691 | prerender_manager_->config().max_bytes && |
| 692 | final_status_ != FINAL_STATUS_USED) { |
| 693 | Destroy(FINAL_STATUS_MEMORY_LIMIT_EXCEEDED); |
| 694 | } |
| 695 | return; |
[email protected] | ba9f8fb | 2011-02-24 20:19:26 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
thestig | c98161a | 2016-07-20 01:42:49 | [diff] [blame] | 699 | std::unique_ptr<WebContents> PrerenderContents::ReleasePrerenderContents() { |
| 700 | prerender_contents_->SetDelegate(nullptr); |
| 701 | content::WebContentsObserver::Observe(nullptr); |
afakhry | 674c7b1 | 2015-06-23 22:56:12 | [diff] [blame] | 702 | |
| 703 | // Clear the task manager tag we added earlier to our |
| 704 | // WebContents since it's no longer a prerender contents. |
avi | 24d693f | 2016-08-06 18:03:52 | [diff] [blame] | 705 | task_manager::WebContentsTags::ClearTag(prerender_contents_.get()); |
afakhry | 674c7b1 | 2015-06-23 22:56:12 | [diff] [blame] | 706 | |
thestig | c98161a | 2016-07-20 01:42:49 | [diff] [blame] | 707 | return std::move(prerender_contents_); |
[email protected] | 71b5d24 | 2011-04-30 02:27:20 | [diff] [blame] | 708 | } |
| 709 | |
Lucas Furukawa Gadani | 95fc361 | 2019-04-05 22:33:49 | [diff] [blame] | 710 | RenderViewHost* PrerenderContents::GetRenderViewHost() { |
thestig | c98161a | 2016-07-20 01:42:49 | [diff] [blame] | 711 | return prerender_contents_ ? prerender_contents_->GetRenderViewHost() |
| 712 | : nullptr; |
[email protected] | 225e743 | 2011-05-03 02:16:11 | [diff] [blame] | 713 | } |
| 714 | |
[email protected] | 853493a | 2012-09-26 02:49:42 | [diff] [blame] | 715 | void PrerenderContents::DidNavigate( |
| 716 | const history::HistoryAddPageArgs& add_page_args) { |
| 717 | add_page_vector_.push_back(add_page_args); |
| 718 | } |
| 719 | |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 720 | void PrerenderContents::CommitHistory(WebContents* tab) { |
| 721 | HistoryTabHelper* history_tab_helper = HistoryTabHelper::FromWebContents(tab); |
[email protected] | 853493a | 2012-09-26 02:49:42 | [diff] [blame] | 722 | for (size_t i = 0; i < add_page_vector_.size(); ++i) |
[email protected] | 3cbe721 | 2012-09-28 17:02:31 | [diff] [blame] | 723 | history_tab_helper->UpdateHistoryForNavigation(add_page_vector_[i]); |
[email protected] | d3961e8 | 2011-05-20 06:32:03 | [diff] [blame] | 724 | } |
| 725 | |
thestig | c98161a | 2016-07-20 01:42:49 | [diff] [blame] | 726 | std::unique_ptr<base::DictionaryValue> PrerenderContents::GetAsValue() const { |
| 727 | if (!prerender_contents_) |
| 728 | return nullptr; |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 729 | auto dict_value = std::make_unique<base::DictionaryValue>(); |
[email protected] | e260204 | 2011-06-15 19:57:29 | [diff] [blame] | 730 | dict_value->SetString("url", prerender_url_.spec()); |
| 731 | base::TimeTicks current_time = base::TimeTicks::Now(); |
| 732 | base::TimeDelta duration = current_time - load_start_time_; |
| 733 | dict_value->SetInteger("duration", duration.InSeconds()); |
[email protected] | 55615c6 | 2013-05-31 16:45:07 | [diff] [blame] | 734 | dict_value->SetBoolean("is_loaded", prerender_contents_ && |
| 735 | !prerender_contents_->IsLoading()); |
[email protected] | e260204 | 2011-06-15 19:57:29 | [diff] [blame] | 736 | return dict_value; |
| 737 | } |
| 738 | |
[email protected] | 099d672 | 2014-02-04 01:13:03 | [diff] [blame] | 739 | void PrerenderContents::PrepareForUse() { |
| 740 | SetFinalStatus(FINAL_STATUS_USED); |
| 741 | |
| 742 | if (prerender_contents_.get()) { |
John Abd-El-Malek | d237798 | 2018-01-08 22:23:12 | [diff] [blame] | 743 | prerender_contents_->SendToAllFrames(new PrerenderMsg_SetIsPrerendering( |
| 744 | MSG_ROUTING_NONE, NO_PRERENDER, std::string())); |
[email protected] | 099d672 | 2014-02-04 01:13:03 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | NotifyPrerenderStop(); |
[email protected] | 099d672 | 2014-02-04 01:13:03 | [diff] [blame] | 748 | } |
| 749 | |
nigeltao | 9881067 | 2017-01-20 01:10:46 | [diff] [blame] | 750 | void PrerenderContents::CancelPrerenderForPrinting() { |
[email protected] | 8d158e09 | 2013-10-18 12:13:35 | [diff] [blame] | 751 | Destroy(FINAL_STATUS_WINDOW_PRINT); |
| 752 | } |
[email protected] | 9e1ad4b | 2011-08-14 16:49:19 | [diff] [blame] | 753 | |
John Abd-El-Malek | d237798 | 2018-01-08 22:23:12 | [diff] [blame] | 754 | void PrerenderContents::CancelPrerenderForUnsupportedMethod() { |
| 755 | Destroy(FINAL_STATUS_INVALID_HTTP_METHOD); |
| 756 | } |
| 757 | |
| 758 | void PrerenderContents::CancelPrerenderForUnsupportedScheme(const GURL& url) { |
| 759 | Destroy(FINAL_STATUS_UNSUPPORTED_SCHEME); |
| 760 | ReportUnsupportedPrerenderScheme(url); |
| 761 | } |
| 762 | |
| 763 | void PrerenderContents::CancelPrerenderForSyncDeferredRedirect() { |
| 764 | Destroy(FINAL_STATUS_BAD_DEFERRED_REDIRECT); |
| 765 | } |
| 766 | |
nigeltao | 9881067 | 2017-01-20 01:10:46 | [diff] [blame] | 767 | void PrerenderContents::OnPrerenderCancelerRequest( |
| 768 | chrome::mojom::PrerenderCancelerRequest request) { |
| 769 | if (!prerender_canceler_binding_.is_bound()) |
| 770 | prerender_canceler_binding_.Bind(std::move(request)); |
| 771 | } |
| 772 | |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 773 | void PrerenderContents::AddNetworkBytes(int64_t bytes) { |
davidben | f47d191 | 2015-03-14 00:24:24 | [diff] [blame] | 774 | network_bytes_ += bytes; |
dimich | e932ed6 | 2017-02-24 21:38:39 | [diff] [blame] | 775 | for (Observer& observer : observer_list_) |
| 776 | observer.OnPrerenderNetworkBytesChanged(this); |
davidben | f47d191 | 2015-03-14 00:24:24 | [diff] [blame] | 777 | } |
[email protected] | 3adcb41 | 2014-02-14 14:59:01 | [diff] [blame] | 778 | |
[email protected] | 4c154ff8 | 2011-02-15 11:23:59 | [diff] [blame] | 779 | } // namespace prerender |