[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 | |
[email protected] | c49147c3 | 2011-04-26 18:42:04 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 8 | #include <functional> |
[email protected] | b99b13b | 2011-05-04 20:59:04 | [diff] [blame] | 9 | #include <utility> |
[email protected] | c49147c3 | 2011-04-26 18:42:04 | [diff] [blame] | 10 | |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
[email protected] | b97d2a87ce | 2013-03-01 02:57:59 | [diff] [blame] | 12 | #include "chrome/browser/favicon/favicon_tab_helper.h" |
[email protected] | 02b9b2b6f | 2011-05-24 17:27:50 | [diff] [blame] | 13 | #include "chrome/browser/history/history_tab_helper.h" |
[email protected] | 2fc4da9 | 2011-05-27 16:24:31 | [diff] [blame] | 14 | #include "chrome/browser/history/history_types.h" |
[email protected] | db6c4ea | 2013-06-14 17:53:01 | [diff] [blame] | 15 | #include "chrome/browser/prerender/prerender_field_trial.h" |
[email protected] | 4c154ff8 | 2011-02-15 11:23:59 | [diff] [blame] | 16 | #include "chrome/browser/prerender/prerender_final_status.h" |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 17 | #include "chrome/browser/prerender/prerender_handle.h" |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 18 | #include "chrome/browser/prerender/prerender_manager.h" |
[email protected] | 3b023e2 | 2011-06-16 22:53:13 | [diff] [blame] | 19 | #include "chrome/browser/prerender/prerender_render_view_host_observer.h" |
[email protected] | 28a05f3a | 2011-05-20 15:05:08 | [diff] [blame] | 20 | #include "chrome/browser/prerender/prerender_tracker.h" |
[email protected] | cafe4ad | 2011-07-28 18:34:56 | [diff] [blame] | 21 | #include "chrome/browser/profiles/profile.h" |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 22 | #include "chrome/browser/ui/browser.h" |
[email protected] | 27e5b391 | 2012-12-16 00:45:37 | [diff] [blame] | 23 | #include "chrome/browser/ui/browser_tab_contents.h" |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 24 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | a0358d7 | 2012-03-09 14:06:50 | [diff] [blame] | 25 | #include "chrome/common/prerender_messages.h" |
[email protected] | 3936521 | 2011-02-24 01:01:00 | [diff] [blame] | 26 | #include "chrome/common/url_constants.h" |
[email protected] | 458433c | 2012-02-29 23:43:39 | [diff] [blame] | 27 | #include "content/public/browser/browser_child_process_host.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 28 | #include "content/public/browser/notification_service.h" |
[email protected] | d908348 | 2012-01-06 00:38:46 | [diff] [blame] | 29 | #include "content/public/browser/render_process_host.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 30 | #include "content/public/browser/render_view_host.h" |
[email protected] | 853e01b | 2012-09-21 20:14:11 | [diff] [blame] | 31 | #include "content/public/browser/resource_request_details.h" |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 32 | #include "content/public/browser/session_storage_namespace.h" |
[email protected] | d908348 | 2012-01-06 00:38:46 | [diff] [blame] | 33 | #include "content/public/browser/web_contents.h" |
[email protected] | 674bc59 | 2011-12-20 23:00:42 | [diff] [blame] | 34 | #include "content/public/browser/web_contents_delegate.h" |
[email protected] | 8643e6d | 2012-01-18 20:26:10 | [diff] [blame] | 35 | #include "content/public/browser/web_contents_view.h" |
[email protected] | 795c2897 | 2012-12-06 06:13:39 | [diff] [blame] | 36 | #include "content/public/common/favicon_url.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 37 | #include "ui/gfx/rect.h" |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 38 | |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 39 | using content::DownloadItem; |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 40 | using content::OpenURLParams; |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 41 | using content::RenderViewHost; |
[email protected] | 4822759 | 2012-03-19 13:13:44 | [diff] [blame] | 42 | using content::ResourceRedirectDetails; |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 43 | using content::SessionStorageNamespace; |
[email protected] | 768c547 | 2011-12-26 19:06:17 | [diff] [blame] | 44 | using content::WebContents; |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 45 | |
[email protected] | 4c154ff8 | 2011-02-15 11:23:59 | [diff] [blame] | 46 | namespace prerender { |
| 47 | |
[email protected] | 5a8dffa | 2011-01-26 00:40:39 | [diff] [blame] | 48 | class PrerenderContentsFactoryImpl : public PrerenderContents::Factory { |
| 49 | public: |
| 50 | virtual PrerenderContents* CreatePrerenderContents( |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 51 | PrerenderManager* prerender_manager, Profile* profile, |
| 52 | const GURL& url, const content::Referrer& referrer, |
[email protected] | e253ab0 | 2011-07-07 21:58:39 | [diff] [blame] | 53 | Origin origin, uint8 experiment_id) OVERRIDE { |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 54 | return new PrerenderContents(prerender_manager, profile, |
[email protected] | e253ab0 | 2011-07-07 21:58:39 | [diff] [blame] | 55 | url, referrer, origin, experiment_id); |
[email protected] | 5a8dffa | 2011-01-26 00:40:39 | [diff] [blame] | 56 | } |
| 57 | }; |
| 58 | |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 59 | // WebContentsDelegateImpl ----------------------------------------------------- |
[email protected] | d3961e8 | 2011-05-20 06:32:03 | [diff] [blame] | 60 | |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 61 | class PrerenderContents::WebContentsDelegateImpl |
[email protected] | 674bc59 | 2011-12-20 23:00:42 | [diff] [blame] | 62 | : public content::WebContentsDelegate { |
[email protected] | d3961e8 | 2011-05-20 06:32:03 | [diff] [blame] | 63 | public: |
[email protected] | 75d90c1 | 2013-03-20 19:50:16 | [diff] [blame] | 64 | explicit WebContentsDelegateImpl(PrerenderContents* prerender_contents) |
| 65 | : prerender_contents_(prerender_contents) { |
[email protected] | d3961e8 | 2011-05-20 06:32:03 | [diff] [blame] | 66 | } |
[email protected] | 3b023e2 | 2011-06-16 22:53:13 | [diff] [blame] | 67 | |
[email protected] | baece6c3 | 2012-06-25 21:22:41 | [diff] [blame] | 68 | // content::WebContentsDelegate implementation: |
| 69 | virtual WebContents* OpenURLFromTab(WebContents* source, |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 70 | const OpenURLParams& params) OVERRIDE { |
[email protected] | 0905b0166 | 2011-12-05 20:30:22 | [diff] [blame] | 71 | // |OpenURLFromTab| is typically called when a frame performs a navigation |
| 72 | // that requires the browser to perform the transition instead of WebKit. |
| 73 | // Examples include prerendering a site that redirects to an app URL, |
| 74 | // or if --enable-strict-site-isolation is specified and the prerendered |
| 75 | // frame redirects to a different origin. |
[email protected] | baece6c3 | 2012-06-25 21:22:41 | [diff] [blame] | 76 | // TODO(cbentzel): Consider supporting this if it is a common case during |
| 77 | // prerenders. |
[email protected] | 0905b0166 | 2011-12-05 20:30:22 | [diff] [blame] | 78 | prerender_contents_->Destroy(FINAL_STATUS_OPEN_URL); |
| 79 | return NULL; |
| 80 | } |
| 81 | |
[email protected] | f85f503 | 2013-04-03 09:01:54 | [diff] [blame] | 82 | virtual void CanDownload( |
| 83 | RenderViewHost* render_view_host, |
| 84 | int request_id, |
| 85 | const std::string& request_method, |
| 86 | const base::Callback<void(bool)>& callback) OVERRIDE { |
[email protected] | 68649314 | 2011-07-15 21:47:22 | [diff] [blame] | 87 | prerender_contents_->Destroy(FINAL_STATUS_DOWNLOAD); |
| 88 | // Cancel the download. |
[email protected] | f85f503 | 2013-04-03 09:01:54 | [diff] [blame] | 89 | callback.Run(false); |
[email protected] | 68649314 | 2011-07-15 21:47:22 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | 4fdf674 | 2012-01-10 20:14:36 | [diff] [blame] | 92 | virtual bool ShouldCreateWebContents( |
| 93 | WebContents* web_contents, |
| 94 | int route_id, |
| 95 | WindowContainerType window_container_type, |
[email protected] | 03b6d55 | 2012-03-29 04:03:01 | [diff] [blame] | 96 | const string16& frame_name, |
| 97 | const GURL& target_url) OVERRIDE { |
[email protected] | 4fdf674 | 2012-01-10 20:14:36 | [diff] [blame] | 98 | // Since we don't want to permit child windows that would have a |
| 99 | // window.opener property, terminate prerendering. |
| 100 | prerender_contents_->Destroy(FINAL_STATUS_CREATE_NEW_WINDOW); |
| 101 | // Cancel the popup. |
| 102 | return false; |
| 103 | } |
| 104 | |
[email protected] | 9e1ad4b | 2011-08-14 16:49:19 | [diff] [blame] | 105 | virtual bool OnGoToEntryOffset(int offset) OVERRIDE { |
| 106 | // This isn't allowed because the history merge operation |
| 107 | // does not work if there are renderer issued challenges. |
| 108 | // TODO(cbentzel): Cancel in this case? May not need to do |
| 109 | // since render-issued offset navigations are not guaranteed, |
| 110 | // but indicates that the page cares about the history. |
| 111 | return false; |
| 112 | } |
| 113 | |
[email protected] | 2a6bc3e | 2011-12-28 23:51:33 | [diff] [blame] | 114 | virtual void JSOutOfMemory(WebContents* tab) OVERRIDE { |
[email protected] | baece6c3 | 2012-06-25 21:22:41 | [diff] [blame] | 115 | prerender_contents_->Destroy(FINAL_STATUS_JS_OUT_OF_MEMORY); |
[email protected] | 5f5b9e4cf | 2011-09-07 21:26:05 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | virtual bool ShouldSuppressDialogs() OVERRIDE { |
[email protected] | baece6c3 | 2012-06-25 21:22:41 | [diff] [blame] | 119 | // We still want to show the user the message when they navigate to this |
| 120 | // page, so cancel this prerender. |
| 121 | prerender_contents_->Destroy(FINAL_STATUS_JAVASCRIPT_ALERT); |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 122 | // Always suppress JavaScript messages if they're triggered by a page being |
| 123 | // prerendered. |
[email protected] | baece6c3 | 2012-06-25 21:22:41 | [diff] [blame] | 124 | return true; |
| 125 | } |
| 126 | |
| 127 | virtual void RegisterProtocolHandler(WebContents* web_contents, |
| 128 | const std::string& protocol, |
| 129 | const GURL& url, |
| 130 | const string16& title, |
| 131 | bool user_gesture) OVERRIDE { |
| 132 | // TODO(mmenke): Consider supporting this if it is a common case during |
| 133 | // prerenders. |
| 134 | prerender_contents_->Destroy(FINAL_STATUS_REGISTER_PROTOCOL_HANDLER); |
[email protected] | 5f5b9e4cf | 2011-09-07 21:26:05 | [diff] [blame] | 135 | } |
| 136 | |
[email protected] | d3961e8 | 2011-05-20 06:32:03 | [diff] [blame] | 137 | private: |
[email protected] | d3961e8 | 2011-05-20 06:32:03 | [diff] [blame] | 138 | PrerenderContents* prerender_contents_; |
| 139 | }; |
| 140 | |
[email protected] | 49fc07b | 2013-01-03 21:05:22 | [diff] [blame] | 141 | void PrerenderContents::Observer::OnPrerenderStopLoading( |
| 142 | PrerenderContents* contents) { |
| 143 | } |
| 144 | |
[email protected] | 2610170 | 2012-12-15 21:45:18 | [diff] [blame] | 145 | void PrerenderContents::Observer::OnPrerenderCreatedMatchCompleteReplacement( |
| 146 | PrerenderContents* contents, PrerenderContents* replacement) { |
| 147 | } |
| 148 | |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 149 | PrerenderContents::Observer::Observer() { |
| 150 | } |
| 151 | |
| 152 | PrerenderContents::Observer::~Observer() { |
| 153 | } |
| 154 | |
[email protected] | ba17cdb | 2012-11-30 02:12:04 | [diff] [blame] | 155 | PrerenderContents::PendingPrerenderInfo::PendingPrerenderInfo( |
| 156 | base::WeakPtr<PrerenderHandle> weak_prerender_handle, |
| 157 | Origin origin, |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 158 | const GURL& url, |
| 159 | const content::Referrer& referrer, |
[email protected] | 75d90c1 | 2013-03-20 19:50:16 | [diff] [blame] | 160 | const gfx::Size& size) |
| 161 | : weak_prerender_handle(weak_prerender_handle), |
| 162 | origin(origin), |
| 163 | url(url), |
| 164 | referrer(referrer), |
| 165 | size(size) { |
[email protected] | 829c2b8 | 2011-09-26 17:12:18 | [diff] [blame] | 166 | } |
| 167 | |
[email protected] | ba17cdb | 2012-11-30 02:12:04 | [diff] [blame] | 168 | PrerenderContents::PendingPrerenderInfo::~PendingPrerenderInfo() { |
| 169 | } |
| 170 | |
| 171 | void PrerenderContents::AddPendingPrerender( |
| 172 | scoped_ptr<PendingPrerenderInfo> pending_prerender_info) { |
| 173 | pending_prerenders_.push_back(pending_prerender_info.release()); |
[email protected] | 829c2b8 | 2011-09-26 17:12:18 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | 2610170 | 2012-12-15 21:45:18 | [diff] [blame] | 176 | void PrerenderContents::PrepareForUse() { |
| 177 | NotifyPrerenderStop(); |
| 178 | |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 179 | SessionStorageNamespace* session_storage_namespace = NULL; |
[email protected] | ba17cdb | 2012-11-30 02:12:04 | [diff] [blame] | 180 | if (prerender_contents_) { |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 181 | // TODO(ajwong): This does not correctly handle storage for isolated apps. |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 182 | session_storage_namespace = prerender_contents_-> |
| 183 | GetController().GetDefaultSessionStorageNamespace(); |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 184 | } |
[email protected] | ba17cdb | 2012-11-30 02:12:04 | [diff] [blame] | 185 | prerender_manager_->StartPendingPrerenders( |
| 186 | child_id_, &pending_prerenders_, session_storage_namespace); |
| 187 | pending_prerenders_.clear(); |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 188 | } |
| 189 | |
[email protected] | 6ee98ea4 | 2011-11-02 20:28:37 | [diff] [blame] | 190 | PrerenderContents::PrerenderContents( |
| 191 | PrerenderManager* prerender_manager, |
[email protected] | 6ee98ea4 | 2011-11-02 20:28:37 | [diff] [blame] | 192 | Profile* profile, |
| 193 | const GURL& url, |
[email protected] | 5ffbf69 | 2011-12-06 00:17:51 | [diff] [blame] | 194 | const content::Referrer& referrer, |
[email protected] | 6ee98ea4 | 2011-11-02 20:28:37 | [diff] [blame] | 195 | Origin origin, |
| 196 | uint8 experiment_id) |
[email protected] | 115ab7b | 2012-05-16 20:25:59 | [diff] [blame] | 197 | : prerendering_has_started_(false), |
| 198 | prerender_manager_(prerender_manager), |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 199 | prerender_url_(url), |
[email protected] | 608c5713 | 2011-02-16 14:57:33 | [diff] [blame] | 200 | referrer_(referrer), |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 201 | profile_(profile), |
[email protected] | 973407b | 2011-01-26 23:18:23 | [diff] [blame] | 202 | page_id_(0), |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 203 | session_storage_namespace_id_(-1), |
[email protected] | 19440b2 | 2011-01-31 18:52:16 | [diff] [blame] | 204 | has_stopped_loading_(false), |
[email protected] | 6974ae7e | 2012-02-16 01:17:59 | [diff] [blame] | 205 | has_finished_loading_(false), |
[email protected] | a3373cd7 | 2011-03-16 23:24:31 | [diff] [blame] | 206 | final_status_(FINAL_STATUS_MAX), |
[email protected] | 1f259d6 | 2012-01-24 23:27:49 | [diff] [blame] | 207 | match_complete_status_(MATCH_COMPLETE_DEFAULT), |
[email protected] | e348af7 | 2011-05-23 21:02:49 | [diff] [blame] | 208 | prerendering_has_been_cancelled_(false), |
[email protected] | 28a05f3a | 2011-05-20 15:05:08 | [diff] [blame] | 209 | child_id_(-1), |
[email protected] | 86d4ff46 | 2011-05-23 17:39:37 | [diff] [blame] | 210 | route_id_(-1), |
[email protected] | e253ab0 | 2011-07-07 21:58:39 | [diff] [blame] | 211 | origin_(origin), |
[email protected] | a0358d7 | 2012-03-09 14:06:50 | [diff] [blame] | 212 | experiment_id_(experiment_id), |
| 213 | creator_child_id_(-1) { |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 214 | DCHECK(prerender_manager != NULL); |
[email protected] | 8a6e416 | 2011-04-27 19:36:48 | [diff] [blame] | 215 | } |
| 216 | |
[email protected] | 2610170 | 2012-12-15 21:45:18 | [diff] [blame] | 217 | PrerenderContents* PrerenderContents::CreateMatchCompleteReplacement() { |
[email protected] | fd4ea84c | 2012-11-27 20:45:25 | [diff] [blame] | 218 | PrerenderContents* new_contents = prerender_manager_->CreatePrerenderContents( |
| 219 | prerender_url(), referrer(), origin(), experiment_id()); |
| 220 | |
| 221 | new_contents->load_start_time_ = load_start_time_; |
| 222 | new_contents->session_storage_namespace_id_ = session_storage_namespace_id_; |
| 223 | new_contents->set_match_complete_status( |
| 224 | PrerenderContents::MATCH_COMPLETE_REPLACEMENT_PENDING); |
| 225 | |
| 226 | const bool did_init = new_contents->Init(); |
| 227 | DCHECK(did_init); |
| 228 | DCHECK_EQ(alias_urls_.front(), new_contents->alias_urls_.front()); |
| 229 | DCHECK_EQ(1u, new_contents->alias_urls_.size()); |
| 230 | new_contents->alias_urls_ = alias_urls_; |
| 231 | new_contents->set_match_complete_status( |
| 232 | PrerenderContents::MATCH_COMPLETE_REPLACEMENT); |
[email protected] | 2610170 | 2012-12-15 21:45:18 | [diff] [blame] | 233 | NotifyPrerenderCreatedMatchCompleteReplacement(new_contents); |
[email protected] | fd4ea84c | 2012-11-27 20:45:25 | [diff] [blame] | 234 | return new_contents; |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 235 | } |
| 236 | |
[email protected] | 8a6e416 | 2011-04-27 19:36:48 | [diff] [blame] | 237 | bool PrerenderContents::Init() { |
[email protected] | 2fc4da9 | 2011-05-27 16:24:31 | [diff] [blame] | 238 | return AddAliasURL(prerender_url_); |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 239 | } |
| 240 | |
[email protected] | 5a8dffa | 2011-01-26 00:40:39 | [diff] [blame] | 241 | // static |
| 242 | PrerenderContents::Factory* PrerenderContents::CreateFactory() { |
| 243 | return new PrerenderContentsFactoryImpl(); |
| 244 | } |
| 245 | |
[email protected] | 71b5d24 | 2011-04-30 02:27:20 | [diff] [blame] | 246 | void PrerenderContents::StartPrerendering( |
[email protected] | 2736c03 | 2012-05-11 18:06:07 | [diff] [blame] | 247 | int creator_child_id, |
| 248 | const gfx::Size& size, |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 249 | SessionStorageNamespace* session_storage_namespace) { |
[email protected] | 71b5d24 | 2011-04-30 02:27:20 | [diff] [blame] | 250 | DCHECK(profile_ != NULL); |
[email protected] | 2e60e295 | 2012-06-25 17:26:05 | [diff] [blame] | 251 | DCHECK(!size.IsEmpty()); |
[email protected] | 71b5d24 | 2011-04-30 02:27:20 | [diff] [blame] | 252 | DCHECK(!prerendering_has_started_); |
| 253 | DCHECK(prerender_contents_.get() == NULL); |
[email protected] | 2736c03 | 2012-05-11 18:06:07 | [diff] [blame] | 254 | DCHECK_EQ(-1, creator_child_id_); |
| 255 | DCHECK(size_.IsEmpty()); |
| 256 | DCHECK_EQ(1U, alias_urls_.size()); |
[email protected] | 71b5d24 | 2011-04-30 02:27:20 | [diff] [blame] | 257 | |
[email protected] | 2736c03 | 2012-05-11 18:06:07 | [diff] [blame] | 258 | creator_child_id_ = creator_child_id; |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 259 | session_storage_namespace_id_ = session_storage_namespace->id(); |
[email protected] | 2736c03 | 2012-05-11 18:06:07 | [diff] [blame] | 260 | size_ = size; |
[email protected] | 71b5d24 | 2011-04-30 02:27:20 | [diff] [blame] | 261 | |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 262 | DCHECK(load_start_time_.is_null()); |
| 263 | load_start_time_ = base::TimeTicks::Now(); |
| 264 | |
[email protected] | eb9857c | 2012-05-29 22:50:17 | [diff] [blame] | 265 | // Everything after this point sets up the WebContents object and associated |
| 266 | // RenderView for the prerender page. Don't do this for members of the |
| 267 | // control group. |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 268 | if (prerender_manager_->IsControlGroup(experiment_id())) |
[email protected] | eb9857c | 2012-05-29 22:50:17 | [diff] [blame] | 269 | return; |
| 270 | |
[email protected] | db6c4ea | 2013-06-14 17:53:01 | [diff] [blame] | 271 | if (origin_ == ORIGIN_LOCAL_PREDICTOR && |
| 272 | IsLocalPredictorPrerenderAlwaysControlEnabled()) { |
| 273 | return; |
| 274 | } |
| 275 | |
[email protected] | eb9857c | 2012-05-29 22:50:17 | [diff] [blame] | 276 | prerendering_has_started_ = true; |
| 277 | |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 278 | prerender_contents_.reset(CreateWebContents(session_storage_namespace)); |
[email protected] | 27e5b391 | 2012-12-16 00:45:37 | [diff] [blame] | 279 | BrowserTabContents::AttachTabHelpers(prerender_contents_.get()); |
[email protected] | b97d2a87ce | 2013-03-01 02:57:59 | [diff] [blame] | 280 | #if defined(OS_ANDROID) |
| 281 | // Delay icon fetching until the contents are getting swapped in |
| 282 | // to conserve network usage in mobile devices. |
| 283 | FaviconTabHelper::FromWebContents( |
| 284 | prerender_contents_.get())->set_should_fetch_icons(false); |
| 285 | #endif // defined(OS_ANDROID) |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 286 | content::WebContentsObserver::Observe(prerender_contents_.get()); |
[email protected] | 2736c03 | 2012-05-11 18:06:07 | [diff] [blame] | 287 | |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 288 | web_contents_delegate_.reset(new WebContentsDelegateImpl(this)); |
| 289 | prerender_contents_.get()->SetDelegate(web_contents_delegate_.get()); |
[email protected] | 0932b30c | 2012-04-17 13:25:10 | [diff] [blame] | 290 | // Set the size of the prerender WebContents. |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 291 | prerender_contents_->GetView()->SizeContents(size_); |
[email protected] | 208ce7e | 2011-08-27 04:35:13 | [diff] [blame] | 292 | |
[email protected] | 5ee38b88 | 2011-05-05 00:18:57 | [diff] [blame] | 293 | // Register as an observer of the RenderViewHost so we get messages. |
| 294 | render_view_host_observer_.reset( |
[email protected] | cfd820d | 2012-04-29 16:29:07 | [diff] [blame] | 295 | new PrerenderRenderViewHostObserver(this, GetRenderViewHostMutable())); |
[email protected] | 5ee38b88 | 2011-05-05 00:18:57 | [diff] [blame] | 296 | |
[email protected] | cfd820d | 2012-04-29 16:29:07 | [diff] [blame] | 297 | child_id_ = GetRenderViewHost()->GetProcess()->GetID(); |
| 298 | route_id_ = GetRenderViewHost()->GetRoutingID(); |
[email protected] | ac1f70b | 2011-05-03 00:46:05 | [diff] [blame] | 299 | |
[email protected] | 71b5d24 | 2011-04-30 02:27:20 | [diff] [blame] | 300 | // Register this with the ResourceDispatcherHost as a prerender |
| 301 | // RenderViewHost. This must be done before the Navigate message to catch all |
| 302 | // resource requests, but as it is on the same thread as the Navigate message |
| 303 | // (IO) there is no race condition. |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 304 | AddObserver(prerender_manager()->prerender_tracker()); |
| 305 | NotifyPrerenderStart(); |
[email protected] | ac1f70b | 2011-05-03 00:46:05 | [diff] [blame] | 306 | |
| 307 | // Close ourselves when the application is shutting down. |
[email protected] | 966c9a4 | 2012-09-25 14:40:21 | [diff] [blame] | 308 | notification_registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 309 | content::NotificationService::AllSources()); |
[email protected] | ac1f70b | 2011-05-03 00:46:05 | [diff] [blame] | 310 | |
| 311 | // Register for our parent profile to shutdown, so we can shut ourselves down |
| 312 | // as well (should only be called for OTR profiles, as we should receive |
| 313 | // APP_TERMINATING before non-OTR profiles are destroyed). |
| 314 | // TODO(tburkard): figure out if this is needed. |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 315 | notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 316 | content::Source<Profile>(profile_)); |
[email protected] | ac1f70b | 2011-05-03 00:46:05 | [diff] [blame] | 317 | |
[email protected] | 165602b | 2011-05-05 14:46:52 | [diff] [blame] | 318 | // Register to inform new RenderViews that we're prerendering. |
| 319 | notification_registrar_.Add( |
[email protected] | d53a08c | 2012-07-18 20:35:30 | [diff] [blame] | 320 | this, content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 321 | content::Source<WebContents>(prerender_contents_.get())); |
[email protected] | 165602b | 2011-05-05 14:46:52 | [diff] [blame] | 322 | |
[email protected] | ac1f70b | 2011-05-03 00:46:05 | [diff] [blame] | 323 | // Register for redirect notifications sourced from |this|. |
[email protected] | 225e743 | 2011-05-03 02:16:11 | [diff] [blame] | 324 | notification_registrar_.Add( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 325 | this, content::NOTIFICATION_RESOURCE_RECEIVED_REDIRECT, |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 326 | content::Source<WebContents>(prerender_contents_.get())); |
[email protected] | 71b5d24 | 2011-04-30 02:27:20 | [diff] [blame] | 327 | |
[email protected] | bf70edce | 2012-06-20 22:32:22 | [diff] [blame] | 328 | // Transfer over the user agent override. |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 329 | prerender_contents_.get()->SetUserAgentOverride( |
[email protected] | bf70edce | 2012-06-20 22:32:22 | [diff] [blame] | 330 | prerender_manager_->config().user_agent_override); |
| 331 | |
[email protected] | cf00233 | 2012-08-14 19:17:47 | [diff] [blame] | 332 | content::NavigationController::LoadURLParams load_url_params( |
| 333 | prerender_url_); |
| 334 | load_url_params.referrer = referrer_; |
| 335 | load_url_params.transition_type = (origin_ == ORIGIN_OMNIBOX ? |
| 336 | content::PAGE_TRANSITION_TYPED : content::PAGE_TRANSITION_LINK); |
| 337 | load_url_params.override_user_agent = |
| 338 | prerender_manager_->config().is_overriding_user_agent ? |
| 339 | content::NavigationController::UA_OVERRIDE_TRUE : |
| 340 | content::NavigationController::UA_OVERRIDE_FALSE; |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 341 | prerender_contents_.get()->GetController().LoadURLWithParams(load_url_params); |
[email protected] | 71b5d24 | 2011-04-30 02:27:20 | [diff] [blame] | 342 | } |
| 343 | |
[email protected] | 55e98aa | 2011-03-23 17:10:32 | [diff] [blame] | 344 | bool PrerenderContents::GetChildId(int* child_id) const { |
| 345 | CHECK(child_id); |
[email protected] | 28a05f3a | 2011-05-20 15:05:08 | [diff] [blame] | 346 | DCHECK_GE(child_id_, -1); |
| 347 | *child_id = child_id_; |
| 348 | return child_id_ != -1; |
[email protected] | 55e98aa | 2011-03-23 17:10:32 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | bool PrerenderContents::GetRouteId(int* route_id) const { |
| 352 | CHECK(route_id); |
[email protected] | 28a05f3a | 2011-05-20 15:05:08 | [diff] [blame] | 353 | DCHECK_GE(route_id_, -1); |
| 354 | *route_id = route_id_; |
| 355 | return route_id_ != -1; |
[email protected] | 55e98aa | 2011-03-23 17:10:32 | [diff] [blame] | 356 | } |
| 357 | |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 358 | void PrerenderContents::SetFinalStatus(FinalStatus final_status) { |
[email protected] | 19440b2 | 2011-01-31 18:52:16 | [diff] [blame] | 359 | DCHECK(final_status >= FINAL_STATUS_USED && final_status < FINAL_STATUS_MAX); |
[email protected] | 1f259d6 | 2012-01-24 23:27:49 | [diff] [blame] | 360 | DCHECK(final_status_ == FINAL_STATUS_MAX); |
[email protected] | ec17231bb | 2011-02-16 20:06:09 | [diff] [blame] | 361 | |
[email protected] | 19440b2 | 2011-01-31 18:52:16 | [diff] [blame] | 362 | final_status_ = final_status; |
| 363 | } |
| 364 | |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 365 | PrerenderContents::~PrerenderContents() { |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 366 | DCHECK_NE(FINAL_STATUS_MAX, final_status()); |
| 367 | DCHECK( |
| 368 | prerendering_has_been_cancelled() || final_status() == FINAL_STATUS_USED); |
| 369 | DCHECK_NE(ORIGIN_MAX, origin()); |
[email protected] | 36492a3 | 2011-03-30 16:39:10 | [diff] [blame] | 370 | |
[email protected] | 1f259d6 | 2012-01-24 23:27:49 | [diff] [blame] | 371 | prerender_manager_->RecordFinalStatusWithMatchCompleteStatus( |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 372 | origin(), experiment_id(), match_complete_status(), final_status()); |
[email protected] | 19440b2 | 2011-01-31 18:52:16 | [diff] [blame] | 373 | |
[email protected] | f5b9c1b52 | 2013-05-15 09:50:10 | [diff] [blame] | 374 | // Broadcast the removal of aliases. |
| 375 | for (content::RenderProcessHost::iterator host_iterator = |
| 376 | content::RenderProcessHost::AllHostsIterator(); |
| 377 | !host_iterator.IsAtEnd(); |
| 378 | host_iterator.Advance()) { |
| 379 | content::RenderProcessHost* host = host_iterator.GetCurrentValue(); |
| 380 | host->Send(new PrerenderMsg_OnPrerenderRemoveAliases(alias_urls_)); |
| 381 | } |
| 382 | |
[email protected] | 0932b30c | 2012-04-17 13:25:10 | [diff] [blame] | 383 | // 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] | 384 | // destroy it. |
| 385 | if (prerender_contents_.get()) |
| 386 | delete ReleasePrerenderContents(); |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 387 | } |
| 388 | |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 389 | void PrerenderContents::AddObserver(Observer* observer) { |
| 390 | DCHECK_EQ(FINAL_STATUS_MAX, final_status_); |
| 391 | observer_list_.AddObserver(observer); |
| 392 | } |
| 393 | |
[email protected] | 2610170 | 2012-12-15 21:45:18 | [diff] [blame] | 394 | void PrerenderContents::RemoveObserver(Observer* observer) { |
| 395 | observer_list_.RemoveObserver(observer); |
| 396 | } |
| 397 | |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 398 | void PrerenderContents::Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 399 | const content::NotificationSource& source, |
| 400 | const content::NotificationDetails& details) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 401 | switch (type) { |
| 402 | case chrome::NOTIFICATION_PROFILE_DESTROYED: |
[email protected] | 19440b2 | 2011-01-31 18:52:16 | [diff] [blame] | 403 | Destroy(FINAL_STATUS_PROFILE_DESTROYED); |
| 404 | return; |
[email protected] | ec17231bb | 2011-02-16 20:06:09 | [diff] [blame] | 405 | |
[email protected] | 966c9a4 | 2012-09-25 14:40:21 | [diff] [blame] | 406 | case chrome::NOTIFICATION_APP_TERMINATING: |
[email protected] | 19440b2 | 2011-01-31 18:52:16 | [diff] [blame] | 407 | Destroy(FINAL_STATUS_APP_TERMINATING); |
| 408 | return; |
[email protected] | 549e89a | 2011-02-04 18:02:31 | [diff] [blame] | 409 | |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 410 | case content::NOTIFICATION_RESOURCE_RECEIVED_REDIRECT: { |
[email protected] | 88de7c35 | 2011-04-13 12:33:31 | [diff] [blame] | 411 | // RESOURCE_RECEIVED_REDIRECT can come for any resource on a page. |
| 412 | // If it's a redirect on the top-level resource, the name needs |
| 413 | // to be remembered for future matching, and if it redirects to |
| 414 | // an https resource, it needs to be canceled. If a subresource |
| 415 | // is redirected, nothing changes. |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 416 | DCHECK_EQ(content::Source<WebContents>(source).ptr(), |
| 417 | prerender_contents_.get()); |
[email protected] | 88de7c35 | 2011-04-13 12:33:31 | [diff] [blame] | 418 | ResourceRedirectDetails* resource_redirect_details = |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 419 | content::Details<ResourceRedirectDetails>(details).ptr(); |
[email protected] | 88de7c35 | 2011-04-13 12:33:31 | [diff] [blame] | 420 | CHECK(resource_redirect_details); |
[email protected] | 4822759 | 2012-03-19 13:13:44 | [diff] [blame] | 421 | if (resource_redirect_details->resource_type == |
[email protected] | 88de7c35 | 2011-04-13 12:33:31 | [diff] [blame] | 422 | ResourceType::MAIN_FRAME) { |
[email protected] | 4822759 | 2012-03-19 13:13:44 | [diff] [blame] | 423 | if (!AddAliasURL(resource_redirect_details->new_url)) |
[email protected] | 8a6e416 | 2011-04-27 19:36:48 | [diff] [blame] | 424 | return; |
[email protected] | 88de7c35 | 2011-04-13 12:33:31 | [diff] [blame] | 425 | } |
| 426 | break; |
| 427 | } |
| 428 | |
[email protected] | d53a08c | 2012-07-18 20:35:30 | [diff] [blame] | 429 | case content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED: { |
[email protected] | 165602b | 2011-05-05 14:46:52 | [diff] [blame] | 430 | if (prerender_contents_.get()) { |
[email protected] | fbc5e5f9 | 2012-01-02 06:08:32 | [diff] [blame] | 431 | DCHECK_EQ(content::Source<WebContents>(source).ptr(), |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 432 | prerender_contents_.get()); |
[email protected] | 165602b | 2011-05-05 14:46:52 | [diff] [blame] | 433 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 434 | content::Details<RenderViewHost> new_render_view_host(details); |
[email protected] | b54c6c6 | 2011-05-12 15:04:04 | [diff] [blame] | 435 | OnRenderViewHostCreated(new_render_view_host.ptr()); |
| 436 | |
[email protected] | 0932b30c | 2012-04-17 13:25:10 | [diff] [blame] | 437 | // When a new RenderView is created for a prerendering WebContents, |
[email protected] | b54c6c6 | 2011-05-12 15:04:04 | [diff] [blame] | 438 | // tell the new RenderView it's being used for prerendering before any |
| 439 | // navigations occur. Note that this is always triggered before the |
| 440 | // first navigation, so there's no need to send the message just after |
[email protected] | 0932b30c | 2012-04-17 13:25:10 | [diff] [blame] | 441 | // the WebContents is created. |
[email protected] | 165602b | 2011-05-05 14:46:52 | [diff] [blame] | 442 | new_render_view_host->Send( |
[email protected] | a0358d7 | 2012-03-09 14:06:50 | [diff] [blame] | 443 | new PrerenderMsg_SetIsPrerendering( |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 444 | new_render_view_host->GetRoutingID(), |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 445 | true)); |
[email protected] | 329c725 | 2011-06-29 19:43:05 | [diff] [blame] | 446 | |
[email protected] | 5725b2e | 2011-09-01 13:11:27 | [diff] [blame] | 447 | // Make sure the size of the RenderViewHost has been passed to the new |
| 448 | // RenderView. Otherwise, the size may not be sent until the |
| 449 | // RenderViewReady event makes it from the render process to the UI |
| 450 | // thread of the browser process. When the RenderView receives its |
| 451 | // size, is also sets itself to be visible, which would then break the |
| 452 | // visibility API. |
| 453 | new_render_view_host->WasResized(); |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 454 | prerender_contents_->WasHidden(); |
[email protected] | 208ce7e | 2011-08-27 04:35:13 | [diff] [blame] | 455 | } |
[email protected] | 5725b2e | 2011-09-01 13:11:27 | [diff] [blame] | 456 | break; |
[email protected] | 208ce7e | 2011-08-27 04:35:13 | [diff] [blame] | 457 | } |
| 458 | |
[email protected] | 33f7497 | 2010-12-08 16:40:36 | [diff] [blame] | 459 | default: |
| 460 | NOTREACHED() << "Unexpected notification sent."; |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | |
[email protected] | b54c6c6 | 2011-05-12 15:04:04 | [diff] [blame] | 465 | void PrerenderContents::OnRenderViewHostCreated( |
| 466 | RenderViewHost* new_render_view_host) { |
| 467 | } |
| 468 | |
[email protected] | ba17cdb | 2012-11-30 02:12:04 | [diff] [blame] | 469 | size_t PrerenderContents::pending_prerender_count() const { |
| 470 | return pending_prerenders_.size(); |
| 471 | } |
| 472 | |
[email protected] | de1fcdd | 2012-04-06 23:01:07 | [diff] [blame] | 473 | WebContents* PrerenderContents::CreateWebContents( |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 474 | SessionStorageNamespace* session_storage_namespace) { |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 475 | // TODO(ajwong): Remove the temporary map once prerendering is aware of |
| 476 | // multiple session storage namespaces per tab. |
| 477 | content::SessionStorageNamespaceMap session_storage_namespace_map; |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 478 | session_storage_namespace_map[std::string()] = session_storage_namespace; |
| 479 | return WebContents::CreateWithSessionStorage( |
[email protected] | 54944cde | 2012-12-09 09:24:59 | [diff] [blame] | 480 | WebContents::CreateParams(profile_), session_storage_namespace_map); |
[email protected] | de1fcdd | 2012-04-06 23:01:07 | [diff] [blame] | 481 | } |
| 482 | |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 483 | void PrerenderContents::NotifyPrerenderStart() { |
| 484 | DCHECK_EQ(FINAL_STATUS_MAX, final_status_); |
| 485 | FOR_EACH_OBSERVER(Observer, observer_list_, OnPrerenderStart(this)); |
| 486 | } |
| 487 | |
[email protected] | 49fc07b | 2013-01-03 21:05:22 | [diff] [blame] | 488 | void PrerenderContents::NotifyPrerenderStopLoading() { |
| 489 | FOR_EACH_OBSERVER(Observer, observer_list_, OnPrerenderStopLoading(this)); |
| 490 | } |
| 491 | |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 492 | void PrerenderContents::NotifyPrerenderStop() { |
| 493 | DCHECK_NE(FINAL_STATUS_MAX, final_status_); |
| 494 | FOR_EACH_OBSERVER(Observer, observer_list_, OnPrerenderStop(this)); |
| 495 | observer_list_.Clear(); |
| 496 | } |
| 497 | |
[email protected] | 2610170 | 2012-12-15 21:45:18 | [diff] [blame] | 498 | void PrerenderContents::NotifyPrerenderCreatedMatchCompleteReplacement( |
| 499 | PrerenderContents* replacement) { |
| 500 | FOR_EACH_OBSERVER(Observer, observer_list_, |
| 501 | OnPrerenderCreatedMatchCompleteReplacement(this, |
| 502 | replacement)); |
| 503 | } |
| 504 | |
[email protected] | 795c2897 | 2012-12-06 06:13:39 | [diff] [blame] | 505 | void PrerenderContents::DidUpdateFaviconURL( |
[email protected] | 60f36f9 | 2011-04-12 16:18:31 | [diff] [blame] | 506 | int32 page_id, |
[email protected] | 795c2897 | 2012-12-06 06:13:39 | [diff] [blame] | 507 | const std::vector<content::FaviconURL>& urls) { |
[email protected] | b99b13b | 2011-05-04 20:59:04 | [diff] [blame] | 508 | VLOG(1) << "PrerenderContents::OnUpdateFaviconURL" << icon_url_; |
[email protected] | 795c2897 | 2012-12-06 06:13:39 | [diff] [blame] | 509 | for (std::vector<content::FaviconURL>::const_iterator it = urls.begin(); |
[email protected] | 8fdb6f3 | 2011-04-26 15:22:59 | [diff] [blame] | 510 | it != urls.end(); ++it) { |
[email protected] | 795c2897 | 2012-12-06 06:13:39 | [diff] [blame] | 511 | if (it->icon_type == content::FaviconURL::FAVICON) { |
[email protected] | 8fdb6f3 | 2011-04-26 15:22:59 | [diff] [blame] | 512 | icon_url_ = it->icon_url; |
[email protected] | b99b13b | 2011-05-04 20:59:04 | [diff] [blame] | 513 | VLOG(1) << icon_url_; |
[email protected] | 60f36f9 | 2011-04-12 16:18:31 | [diff] [blame] | 514 | return; |
| 515 | } |
| 516 | } |
[email protected] | c775fc8c | 2011-02-08 23:07:15 | [diff] [blame] | 517 | } |
| 518 | |
[email protected] | d085f5c | 2011-02-20 12:17:47 | [diff] [blame] | 519 | bool PrerenderContents::AddAliasURL(const GURL& url) { |
[email protected] | f9034cf | 2011-07-21 12:43:41 | [diff] [blame] | 520 | const bool http = url.SchemeIs(chrome::kHttpScheme); |
| 521 | const bool https = url.SchemeIs(chrome::kHttpsScheme); |
[email protected] | 75d90c1 | 2013-03-20 19:50:16 | [diff] [blame] | 522 | if (!http && !https) { |
[email protected] | 027dc6c4 | 2012-06-14 19:34:20 | [diff] [blame] | 523 | DCHECK_NE(MATCH_COMPLETE_REPLACEMENT_PENDING, match_complete_status_); |
[email protected] | f9034cf | 2011-07-21 12:43:41 | [diff] [blame] | 524 | Destroy(FINAL_STATUS_UNSUPPORTED_SCHEME); |
| 525 | return false; |
| 526 | } |
| 527 | if (https && !prerender_manager_->config().https_allowed) { |
[email protected] | 027dc6c4 | 2012-06-14 19:34:20 | [diff] [blame] | 528 | DCHECK_NE(MATCH_COMPLETE_REPLACEMENT_PENDING, match_complete_status_); |
[email protected] | f9034cf | 2011-07-21 12:43:41 | [diff] [blame] | 529 | Destroy(FINAL_STATUS_HTTPS); |
[email protected] | d085f5c | 2011-02-20 12:17:47 | [diff] [blame] | 530 | return false; |
[email protected] | 8a6e416 | 2011-04-27 19:36:48 | [diff] [blame] | 531 | } |
[email protected] | 027dc6c4 | 2012-06-14 19:34:20 | [diff] [blame] | 532 | if (match_complete_status_ != MATCH_COMPLETE_REPLACEMENT_PENDING && |
[email protected] | 3a6ec76 | 2012-10-17 16:01:01 | [diff] [blame] | 533 | prerender_manager_->HasRecentlyBeenNavigatedTo(origin(), url)) { |
[email protected] | 84b5da9 | 2011-05-19 17:53:56 | [diff] [blame] | 534 | Destroy(FINAL_STATUS_RECENTLY_VISITED); |
| 535 | return false; |
| 536 | } |
[email protected] | f9034cf | 2011-07-21 12:43:41 | [diff] [blame] | 537 | |
[email protected] | f39506a8 | 2011-01-18 23:46:38 | [diff] [blame] | 538 | alias_urls_.push_back(url); |
[email protected] | f5b9c1b52 | 2013-05-15 09:50:10 | [diff] [blame] | 539 | |
| 540 | for (content::RenderProcessHost::iterator host_iterator = |
| 541 | content::RenderProcessHost::AllHostsIterator(); |
| 542 | !host_iterator.IsAtEnd(); |
| 543 | host_iterator.Advance()) { |
| 544 | content::RenderProcessHost* host = host_iterator.GetCurrentValue(); |
| 545 | host->Send(new PrerenderMsg_OnPrerenderAddAlias(url)); |
| 546 | } |
| 547 | |
[email protected] | d085f5c | 2011-02-20 12:17:47 | [diff] [blame] | 548 | return true; |
[email protected] | f39506a8 | 2011-01-18 23:46:38 | [diff] [blame] | 549 | } |
| 550 | |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 551 | bool PrerenderContents::Matches( |
| 552 | const GURL& url, |
| 553 | const SessionStorageNamespace* session_storage_namespace) const { |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 554 | if (session_storage_namespace && |
[email protected] | 75d90c1 | 2013-03-20 19:50:16 | [diff] [blame] | 555 | session_storage_namespace_id_ != session_storage_namespace->id()) { |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 556 | return false; |
[email protected] | 75d90c1 | 2013-03-20 19:50:16 | [diff] [blame] | 557 | } |
[email protected] | 4805504 | 2012-07-14 21:12:11 | [diff] [blame] | 558 | return std::count_if(alias_urls_.begin(), alias_urls_.end(), |
| 559 | std::bind2nd(std::equal_to<GURL>(), url)) != 0; |
[email protected] | f39506a8 | 2011-01-18 23:46:38 | [diff] [blame] | 560 | } |
[email protected] | 973407b | 2011-01-26 23:18:23 | [diff] [blame] | 561 | |
[email protected] | 9cddb1a2 | 2011-11-15 15:04:27 | [diff] [blame] | 562 | void PrerenderContents::RenderViewGone(base::TerminationStatus status) { |
[email protected] | 5ee38b88 | 2011-05-05 00:18:57 | [diff] [blame] | 563 | Destroy(FINAL_STATUS_RENDERER_CRASHED); |
| 564 | } |
| 565 | |
[email protected] | 8979307 | 2012-07-23 22:25:29 | [diff] [blame] | 566 | void PrerenderContents::DidStopLoading( |
| 567 | content::RenderViewHost* render_view_host) { |
[email protected] | 973407b | 2011-01-26 23:18:23 | [diff] [blame] | 568 | has_stopped_loading_ = true; |
[email protected] | 49fc07b | 2013-01-03 21:05:22 | [diff] [blame] | 569 | NotifyPrerenderStopLoading(); |
[email protected] | 973407b | 2011-01-26 23:18:23 | [diff] [blame] | 570 | } |
[email protected] | 19440b2 | 2011-01-31 18:52:16 | [diff] [blame] | 571 | |
[email protected] | 5f5b9e4cf | 2011-09-07 21:26:05 | [diff] [blame] | 572 | void PrerenderContents::DidStartProvisionalLoadForFrame( |
| 573 | int64 frame_id, |
[email protected] | d37c33e | 2012-10-12 13:35:13 | [diff] [blame] | 574 | int64 parent_frame_id, |
[email protected] | 5f5b9e4cf | 2011-09-07 21:26:05 | [diff] [blame] | 575 | bool is_main_frame, |
| 576 | const GURL& validated_url, |
| 577 | bool is_error_page, |
[email protected] | ead9009e | 2013-01-07 22:06:32 | [diff] [blame] | 578 | bool is_iframe_srcdoc, |
[email protected] | 5f5b9e4cf | 2011-09-07 21:26:05 | [diff] [blame] | 579 | RenderViewHost* render_view_host) { |
| 580 | if (is_main_frame) { |
| 581 | if (!AddAliasURL(validated_url)) |
| 582 | return; |
| 583 | |
| 584 | // Usually, this event fires if the user clicks or enters a new URL. |
| 585 | // Neither of these can happen in the case of an invisible prerender. |
| 586 | // So the cause is: Some JavaScript caused a new URL to be loaded. In that |
| 587 | // case, the spinner would start again in the browser, so we must reset |
| 588 | // has_stopped_loading_ so that the spinner won't be stopped. |
| 589 | has_stopped_loading_ = false; |
[email protected] | 6974ae7e | 2012-02-16 01:17:59 | [diff] [blame] | 590 | has_finished_loading_ = false; |
[email protected] | 5f5b9e4cf | 2011-09-07 21:26:05 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
[email protected] | 6974ae7e | 2012-02-16 01:17:59 | [diff] [blame] | 594 | void PrerenderContents::DidFinishLoad(int64 frame_id, |
| 595 | const GURL& validated_url, |
[email protected] | 7bb76189 | 2012-07-20 09:32:47 | [diff] [blame] | 596 | bool is_main_frame, |
| 597 | RenderViewHost* render_view_host) { |
[email protected] | 6974ae7e | 2012-02-16 01:17:59 | [diff] [blame] | 598 | if (is_main_frame) |
| 599 | has_finished_loading_ = true; |
| 600 | } |
| 601 | |
[email protected] | 19440b2 | 2011-01-31 18:52:16 | [diff] [blame] | 602 | void PrerenderContents::Destroy(FinalStatus final_status) { |
[email protected] | 2610170 | 2012-12-15 21:45:18 | [diff] [blame] | 603 | DCHECK_NE(final_status, FINAL_STATUS_USED); |
| 604 | |
[email protected] | e348af7 | 2011-05-23 21:02:49 | [diff] [blame] | 605 | if (prerendering_has_been_cancelled_) |
[email protected] | 5ee38b88 | 2011-05-05 00:18:57 | [diff] [blame] | 606 | return; |
[email protected] | 4c154ff8 | 2011-02-15 11:23:59 | [diff] [blame] | 607 | |
[email protected] | 28a05f3a | 2011-05-20 15:05:08 | [diff] [blame] | 608 | if (child_id_ != -1 && route_id_ != -1) { |
| 609 | // Cancel the prerender in the PrerenderTracker. This is needed |
| 610 | // because destroy may be called directly from the UI thread without calling |
| 611 | // TryCancel(). This is difficult to completely avoid, since prerendering |
| 612 | // can be cancelled before a RenderView is created. |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 613 | bool is_cancelled = prerender_manager()->prerender_tracker()->TryCancel( |
[email protected] | 1459fb6 | 2011-05-25 19:03:27 | [diff] [blame] | 614 | child_id_, route_id_, final_status); |
[email protected] | 28a05f3a | 2011-05-20 15:05:08 | [diff] [blame] | 615 | CHECK(is_cancelled); |
| 616 | |
| 617 | // A different final status may have been set already from another thread. |
| 618 | // If so, use it instead. |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 619 | if (!prerender_manager()->prerender_tracker()-> |
| 620 | GetFinalStatus(child_id_, route_id_, &final_status)) { |
[email protected] | 28a05f3a | 2011-05-20 15:05:08 | [diff] [blame] | 621 | NOTREACHED(); |
| 622 | } |
| 623 | } |
[email protected] | ac4f468 | 2012-12-08 22:39:26 | [diff] [blame] | 624 | SetFinalStatus(final_status); |
[email protected] | e348af7 | 2011-05-23 21:02:49 | [diff] [blame] | 625 | |
[email protected] | 49308e28 | 2011-08-11 00:17:40 | [diff] [blame] | 626 | prerendering_has_been_cancelled_ = true; |
[email protected] | 128c7406 | 2012-11-27 17:55:53 | [diff] [blame] | 627 | prerender_manager_->AddToHistory(this); |
[email protected] | 447f9ab | 2011-11-14 23:56:35 | [diff] [blame] | 628 | prerender_manager_->MoveEntryToPendingDelete(this, final_status); |
[email protected] | 49308e28 | 2011-08-11 00:17:40 | [diff] [blame] | 629 | |
[email protected] | 2610170 | 2012-12-15 21:45:18 | [diff] [blame] | 630 | if (!prerender_manager_->IsControlGroup(experiment_id()) && |
| 631 | (prerendering_has_started() || |
| 632 | match_complete_status() == MATCH_COMPLETE_REPLACEMENT)) { |
| 633 | NotifyPrerenderStop(); |
| 634 | } |
| 635 | |
[email protected] | 5ee38b88 | 2011-05-05 00:18:57 | [diff] [blame] | 636 | // We may destroy the PrerenderContents before we have initialized the |
| 637 | // RenderViewHost. Otherwise set the Observer's PrerenderContents to NULL to |
| 638 | // avoid any more messages being sent. |
[email protected] | 128c7406 | 2012-11-27 17:55:53 | [diff] [blame] | 639 | if (render_view_host_observer_) |
[email protected] | 5ee38b88 | 2011-05-05 00:18:57 | [diff] [blame] | 640 | render_view_host_observer_->set_prerender_contents(NULL); |
[email protected] | ba9f8fb | 2011-02-24 20:19:26 | [diff] [blame] | 641 | } |
| 642 | |
[email protected] | ba9f8fb | 2011-02-24 20:19:26 | [diff] [blame] | 643 | base::ProcessMetrics* PrerenderContents::MaybeGetProcessMetrics() { |
| 644 | if (process_metrics_.get() == NULL) { |
[email protected] | bd6b924 | 2011-04-04 18:38:31 | [diff] [blame] | 645 | // If a PrenderContents hasn't started prerending, don't be fully formed. |
[email protected] | cfd820d | 2012-04-29 16:29:07 | [diff] [blame] | 646 | if (!GetRenderViewHost() || !GetRenderViewHost()->GetProcess()) |
[email protected] | bd6b924 | 2011-04-04 18:38:31 | [diff] [blame] | 647 | return NULL; |
[email protected] | cfd820d | 2012-04-29 16:29:07 | [diff] [blame] | 648 | base::ProcessHandle handle = GetRenderViewHost()->GetProcess()->GetHandle(); |
[email protected] | ba9f8fb | 2011-02-24 20:19:26 | [diff] [blame] | 649 | if (handle == base::kNullProcessHandle) |
| 650 | return NULL; |
| 651 | #if !defined(OS_MACOSX) |
| 652 | process_metrics_.reset(base::ProcessMetrics::CreateProcessMetrics(handle)); |
| 653 | #else |
| 654 | process_metrics_.reset(base::ProcessMetrics::CreateProcessMetrics( |
| 655 | handle, |
[email protected] | 458433c | 2012-02-29 23:43:39 | [diff] [blame] | 656 | content::BrowserChildProcessHost::GetPortProvider())); |
[email protected] | ba9f8fb | 2011-02-24 20:19:26 | [diff] [blame] | 657 | #endif |
| 658 | } |
| 659 | |
| 660 | return process_metrics_.get(); |
| 661 | } |
| 662 | |
| 663 | void PrerenderContents::DestroyWhenUsingTooManyResources() { |
| 664 | base::ProcessMetrics* metrics = MaybeGetProcessMetrics(); |
| 665 | if (metrics == NULL) |
| 666 | return; |
| 667 | |
| 668 | size_t private_bytes, shared_bytes; |
[email protected] | 940da33 | 2011-06-30 18:09:11 | [diff] [blame] | 669 | if (metrics->GetMemoryBytes(&private_bytes, &shared_bytes) && |
| 670 | private_bytes > prerender_manager_->config().max_bytes) { |
[email protected] | 75d90c1 | 2013-03-20 19:50:16 | [diff] [blame] | 671 | Destroy(FINAL_STATUS_MEMORY_LIMIT_EXCEEDED); |
[email protected] | ba9f8fb | 2011-02-24 20:19:26 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 675 | WebContents* PrerenderContents::ReleasePrerenderContents() { |
| 676 | prerender_contents_->SetDelegate(NULL); |
[email protected] | 5ee38b88 | 2011-05-05 00:18:57 | [diff] [blame] | 677 | render_view_host_observer_.reset(); |
[email protected] | d8c66043 | 2011-12-22 20:51:25 | [diff] [blame] | 678 | content::WebContentsObserver::Observe(NULL); |
[email protected] | 71b5d24 | 2011-04-30 02:27:20 | [diff] [blame] | 679 | return prerender_contents_.release(); |
| 680 | } |
| 681 | |
[email protected] | cfd820d | 2012-04-29 16:29:07 | [diff] [blame] | 682 | RenderViewHost* PrerenderContents::GetRenderViewHostMutable() { |
| 683 | return const_cast<RenderViewHost*>(GetRenderViewHost()); |
[email protected] | 5ee38b88 | 2011-05-05 00:18:57 | [diff] [blame] | 684 | } |
| 685 | |
[email protected] | cfd820d | 2012-04-29 16:29:07 | [diff] [blame] | 686 | const RenderViewHost* PrerenderContents::GetRenderViewHost() const { |
[email protected] | 2fc4da9 | 2011-05-27 16:24:31 | [diff] [blame] | 687 | if (!prerender_contents_.get()) |
| 688 | return NULL; |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 689 | return prerender_contents_->GetRenderViewHost(); |
[email protected] | 225e743 | 2011-05-03 02:16:11 | [diff] [blame] | 690 | } |
| 691 | |
[email protected] | 853493a | 2012-09-26 02:49:42 | [diff] [blame] | 692 | void PrerenderContents::DidNavigate( |
| 693 | const history::HistoryAddPageArgs& add_page_args) { |
| 694 | add_page_vector_.push_back(add_page_args); |
| 695 | } |
| 696 | |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 697 | void PrerenderContents::CommitHistory(WebContents* tab) { |
| 698 | HistoryTabHelper* history_tab_helper = HistoryTabHelper::FromWebContents(tab); |
[email protected] | 853493a | 2012-09-26 02:49:42 | [diff] [blame] | 699 | for (size_t i = 0; i < add_page_vector_.size(); ++i) |
[email protected] | 3cbe721 | 2012-09-28 17:02:31 | [diff] [blame] | 700 | history_tab_helper->UpdateHistoryForNavigation(add_page_vector_[i]); |
[email protected] | d3961e8 | 2011-05-20 06:32:03 | [diff] [blame] | 701 | } |
| 702 | |
[email protected] | e260204 | 2011-06-15 19:57:29 | [diff] [blame] | 703 | Value* PrerenderContents::GetAsValue() const { |
| 704 | if (!prerender_contents_.get()) |
| 705 | return NULL; |
| 706 | DictionaryValue* dict_value = new DictionaryValue(); |
| 707 | dict_value->SetString("url", prerender_url_.spec()); |
| 708 | base::TimeTicks current_time = base::TimeTicks::Now(); |
| 709 | base::TimeDelta duration = current_time - load_start_time_; |
| 710 | dict_value->SetInteger("duration", duration.InSeconds()); |
[email protected] | 55615c6 | 2013-05-31 16:45:07 | [diff] [blame] | 711 | dict_value->SetBoolean("is_loaded", prerender_contents_ && |
| 712 | !prerender_contents_->IsLoading()); |
[email protected] | e260204 | 2011-06-15 19:57:29 | [diff] [blame] | 713 | return dict_value; |
| 714 | } |
| 715 | |
[email protected] | 9e1ad4b | 2011-08-14 16:49:19 | [diff] [blame] | 716 | bool PrerenderContents::IsCrossSiteNavigationPending() const { |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 717 | if (!prerender_contents_) |
[email protected] | 9e1ad4b | 2011-08-14 16:49:19 | [diff] [blame] | 718 | return false; |
[email protected] | a562aea2 | 2012-12-11 22:43:12 | [diff] [blame] | 719 | return (prerender_contents_->GetSiteInstance() != |
| 720 | prerender_contents_->GetPendingSiteInstance()); |
[email protected] | 9e1ad4b | 2011-08-14 16:49:19 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | |
[email protected] | 4c154ff8 | 2011-02-15 11:23:59 | [diff] [blame] | 724 | } // namespace prerender |