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_model.h" | 17 #include "components/offline_pages/core/offline_page_model.h" |
18 #include "content/public/browser/browser_context.h" | 18 #include "content/public/browser/browser_context.h" |
| 19 #include "content/public/browser/mhtml_extra_parts.h" |
19 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
20 | 21 |
| 22 namespace { |
| 23 const char kContentType[] = "text/plain"; |
| 24 } // namespace |
| 25 |
21 namespace offline_pages { | 26 namespace offline_pages { |
22 | 27 |
23 PrerenderingOffliner::PrerenderingOffliner( | 28 PrerenderingOffliner::PrerenderingOffliner( |
24 content::BrowserContext* browser_context, | 29 content::BrowserContext* browser_context, |
25 const OfflinerPolicy* policy, | 30 const OfflinerPolicy* policy, |
26 OfflinePageModel* offline_page_model) | 31 OfflinePageModel* offline_page_model) |
27 : browser_context_(browser_context), | 32 : browser_context_(browser_context), |
28 policy_(policy), | 33 policy_(policy), |
29 offline_page_model_(offline_page_model), | 34 offline_page_model_(offline_page_model), |
30 pending_request_(nullptr), | 35 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(); | 81 save_page_params.client_id = request.client_id(); |
77 save_page_params.proposed_offline_id = request.request_id(); | 82 save_page_params.proposed_offline_id = request.request_id(); |
78 save_page_params.is_background = true; | 83 save_page_params.is_background = true; |
79 // Pass in the original URL if it is different from the last committed URL | 84 // Pass in the original URL if it is different from the last committed URL |
80 // when redirects occur. | 85 // when redirects occur. |
81 if (!request.original_url().is_empty()) | 86 if (!request.original_url().is_empty()) |
82 save_page_params.original_url = request.original_url(); | 87 save_page_params.original_url = request.original_url(); |
83 else if (save_page_params.url != request.url()) | 88 else if (save_page_params.url != request.url()) |
84 save_page_params.original_url = request.url(); | 89 save_page_params.original_url = request.url(); |
85 | 90 |
| 91 #ifndef NDEBUG |
| 92 // Stash loading signals for writing when we write out the MHTML. |
| 93 std::string body = SerializeLoadingSignalData(); |
| 94 std::string content_type = kContentType; |
| 95 std::string content_location = "cid:signal-data-" + |
| 96 std::to_string(request.request_id()) + |
| 97 "@mhtml.blink"; |
| 98 |
| 99 content::MHTMLExtraParts* extra_parts = |
| 100 content::MHTMLExtraParts::FromWebContents(web_contents); |
| 101 DCHECK(extra_parts); |
| 102 |
| 103 extra_parts->AddExtraMHTMLPart(content_type, content_location, body); |
| 104 #endif |
| 105 |
86 SavePage(save_page_params, std::move(archiver), | 106 SavePage(save_page_params, std::move(archiver), |
87 base::Bind(&PrerenderingOffliner::OnSavePageDone, | 107 base::Bind(&PrerenderingOffliner::OnSavePageDone, |
88 weak_ptr_factory_.GetWeakPtr(), request)); | 108 weak_ptr_factory_.GetWeakPtr(), request)); |
89 } else { | 109 } else { |
90 // Clear pending request and app listener then run completion callback. | 110 // Clear pending request and app listener then run completion callback. |
91 pending_request_.reset(nullptr); | 111 pending_request_.reset(nullptr); |
92 app_listener_.reset(nullptr); | 112 app_listener_.reset(nullptr); |
93 completion_callback_.Run(request, load_status); | 113 completion_callback_.Run(request, load_status); |
94 } | 114 } |
95 } | 115 } |
96 | 116 |
| 117 std::string PrerenderingOffliner::SerializeLoadingSignalData() { |
| 118 // Write the signal data into a single string. |
| 119 std::string signal_string; |
| 120 const std::vector<std::string>& signals = loader_->GetLoadingSignalData(); |
| 121 |
| 122 // TODO(petewil): Convert this to JSON, use json_writer.h |
| 123 for (std::string signal : signals) { |
| 124 signal_string += signal; |
| 125 signal_string += "\r\n"; |
| 126 } |
| 127 return signal_string; |
| 128 } |
| 129 |
97 void PrerenderingOffliner::OnSavePageDone( | 130 void PrerenderingOffliner::OnSavePageDone( |
98 const SavePageRequest& request, | 131 const SavePageRequest& request, |
99 SavePageResult save_result, | 132 SavePageResult save_result, |
100 int64_t offline_id) { | 133 int64_t offline_id) { |
101 // Check if request is still pending receiving a callback. | 134 // Check if request is still pending receiving a callback. |
102 if (!pending_request_) | 135 if (!pending_request_) |
103 return; | 136 return; |
104 | 137 |
105 // Also check that this completed request is same as the pending one | 138 // 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). | 139 // (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( | 316 void PrerenderingOffliner::HandleApplicationStateChangeCancel( |
284 const SavePageRequest& request, | 317 const SavePageRequest& request, |
285 int64_t offline_id) { | 318 int64_t offline_id) { |
286 // This shouldn't be immediate, but account for case where request was reset | 319 // This shouldn't be immediate, but account for case where request was reset |
287 // while waiting for callback. | 320 // while waiting for callback. |
288 if (pending_request_ && pending_request_->request_id() != offline_id) | 321 if (pending_request_ && pending_request_->request_id() != offline_id) |
289 return; | 322 return; |
290 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); | 323 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); |
291 } | 324 } |
292 } // namespace offline_pages | 325 } // namespace offline_pages |
OLD | NEW |