OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/android/offline_pages/prerendering_offliner.h" | 5 #include "chrome/browser/android/offline_pages/prerendering_offliner.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
9 #include "base/sys_info.h" | 9 #include "base/sys_info.h" |
10 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 10 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
11 #include "chrome/browser/android/offline_pages/offliner_helper.h" | 11 #include "chrome/browser/android/offline_pages/offliner_helper.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "components/offline_pages/core/background/offliner_policy.h" | 13 #include "components/offline_pages/core/background/offliner_policy.h" |
14 #include "components/offline_pages/core/background/save_page_request.h" | 14 #include "components/offline_pages/core/background/save_page_request.h" |
15 #include "components/offline_pages/core/client_namespace_constants.h" | 15 #include "components/offline_pages/core/client_namespace_constants.h" |
16 #include "components/offline_pages/core/downloads/download_ui_adapter.h" | 16 #include "components/offline_pages/core/downloads/download_ui_adapter.h" |
17 #include "components/offline_pages/core/offline_page_feature.h" | |
17 #include "components/offline_pages/core/offline_page_model.h" | 18 #include "components/offline_pages/core/offline_page_model.h" |
18 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
20 #include "content/public/browser/mhtml_extra_parts.h" | |
19 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
20 | 22 |
23 namespace { | |
24 const char kContentType[] = "text/plain"; | |
25 const char kContentTransferEncodingBinary[] = | |
26 "Content-Transfer-Encoding: binary"; | |
27 const char kXHeaderForSignals[] = "Chromium-Loading-Metrics-Data: 1"; | |
Dmitry Titov
2017/04/05 00:18:06
that header doesn't look like x-google-... I htink
Pete Williamson
2017/04/05 18:29:46
After discussion offline, we decided to change thi
| |
28 } // namespace | |
29 | |
21 namespace offline_pages { | 30 namespace offline_pages { |
22 | 31 |
23 PrerenderingOffliner::PrerenderingOffliner( | 32 PrerenderingOffliner::PrerenderingOffliner( |
24 content::BrowserContext* browser_context, | 33 content::BrowserContext* browser_context, |
25 const OfflinerPolicy* policy, | 34 const OfflinerPolicy* policy, |
26 OfflinePageModel* offline_page_model) | 35 OfflinePageModel* offline_page_model) |
27 : browser_context_(browser_context), | 36 : browser_context_(browser_context), |
28 policy_(policy), | 37 policy_(policy), |
29 offline_page_model_(offline_page_model), | 38 offline_page_model_(offline_page_model), |
30 pending_request_(nullptr), | 39 pending_request_(nullptr), |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 save_page_params.client_id = request.client_id(); | 85 save_page_params.client_id = request.client_id(); |
77 save_page_params.proposed_offline_id = request.request_id(); | 86 save_page_params.proposed_offline_id = request.request_id(); |
78 save_page_params.is_background = true; | 87 save_page_params.is_background = true; |
79 // Pass in the original URL if it is different from the last committed URL | 88 // Pass in the original URL if it is different from the last committed URL |
80 // when redirects occur. | 89 // when redirects occur. |
81 if (!request.original_url().is_empty()) | 90 if (!request.original_url().is_empty()) |
82 save_page_params.original_url = request.original_url(); | 91 save_page_params.original_url = request.original_url(); |
83 else if (save_page_params.url != request.url()) | 92 else if (save_page_params.url != request.url()) |
84 save_page_params.original_url = request.url(); | 93 save_page_params.original_url = request.url(); |
85 | 94 |
95 if (IsOfflinePagesLoadSignalCollectingEnabled()) { | |
96 // Stash loading signals for writing when we write out the MHTML. | |
97 std::string headers = | |
98 base::StringPrintf("%s\r\n%s\r\n\r\n", kContentTransferEncodingBinary, | |
99 kXHeaderForSignals); | |
Dmitry Titov
2017/04/05 00:18:06
For the further CL: consider refactoring by adding
Pete Williamson
2017/04/05 18:29:46
Acknowledged.
| |
100 std::string body = headers + SerializeLoadingSignalData(); | |
101 std::string content_type = kContentType; | |
102 std::string content_location = "cid:signal-data-" + | |
103 std::to_string(request.request_id()) + | |
104 "@mhtml.blink"; | |
105 | |
106 content::MHTMLExtraParts* extra_parts = | |
107 content::MHTMLExtraParts::FromWebContents(web_contents); | |
108 DCHECK(extra_parts); | |
109 | |
110 extra_parts->AddExtraMHTMLPart(content_type, content_location, body); | |
111 } | |
112 | |
86 SavePage(save_page_params, std::move(archiver), | 113 SavePage(save_page_params, std::move(archiver), |
87 base::Bind(&PrerenderingOffliner::OnSavePageDone, | 114 base::Bind(&PrerenderingOffliner::OnSavePageDone, |
88 weak_ptr_factory_.GetWeakPtr(), request)); | 115 weak_ptr_factory_.GetWeakPtr(), request)); |
89 } else { | 116 } else { |
90 // Clear pending request and app listener then run completion callback. | 117 // Clear pending request and app listener then run completion callback. |
91 pending_request_.reset(nullptr); | 118 pending_request_.reset(nullptr); |
92 app_listener_.reset(nullptr); | 119 app_listener_.reset(nullptr); |
93 completion_callback_.Run(request, load_status); | 120 completion_callback_.Run(request, load_status); |
94 } | 121 } |
95 } | 122 } |
96 | 123 |
124 std::string PrerenderingOffliner::SerializeLoadingSignalData() { | |
125 // Write the signal data into a single string. | |
126 std::string signal_string; | |
127 const std::vector<std::string>& signals = loader_->GetLoadingSignalData(); | |
128 | |
129 // TODO(petewil): Convert this to JSON, use json_writer.h | |
130 for (std::string signal : signals) { | |
131 signal_string += signal; | |
132 signal_string += "\n"; | |
133 } | |
134 return signal_string; | |
135 } | |
136 | |
97 void PrerenderingOffliner::OnSavePageDone( | 137 void PrerenderingOffliner::OnSavePageDone( |
98 const SavePageRequest& request, | 138 const SavePageRequest& request, |
99 SavePageResult save_result, | 139 SavePageResult save_result, |
100 int64_t offline_id) { | 140 int64_t offline_id) { |
101 // Check if request is still pending receiving a callback. | 141 // Check if request is still pending receiving a callback. |
102 if (!pending_request_) | 142 if (!pending_request_) |
103 return; | 143 return; |
104 | 144 |
105 // Also check that this completed request is same as the pending one | 145 // Also check that this completed request is same as the pending one |
106 // (since SavePage request is not cancel-able currently and could be old). | 146 // (since SavePage request is not cancel-able currently and could be old). |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
283 void PrerenderingOffliner::HandleApplicationStateChangeCancel( | 323 void PrerenderingOffliner::HandleApplicationStateChangeCancel( |
284 const SavePageRequest& request, | 324 const SavePageRequest& request, |
285 int64_t offline_id) { | 325 int64_t offline_id) { |
286 // This shouldn't be immediate, but account for case where request was reset | 326 // This shouldn't be immediate, but account for case where request was reset |
287 // while waiting for callback. | 327 // while waiting for callback. |
288 if (pending_request_ && pending_request_->request_id() != offline_id) | 328 if (pending_request_ && pending_request_->request_id() != offline_id) |
289 return; | 329 return; |
290 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); | 330 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); |
291 } | 331 } |
292 } // namespace offline_pages | 332 } // namespace offline_pages |
OLD | NEW |