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/browser/download/mhtml_generation_manager.h" |
18 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
19 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
20 | 21 |
| 22 namespace { |
| 23 const char kApplicationJson[] = "application/json"; |
| 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 std::string signal_string = SerializeLoadingSignalData(); |
| 92 |
| 93 // Attach a signal data section to the web contents. |
| 94 content::MHTMLGenerationManager::MHTMLExtraSection signal_section; |
| 95 signal_section.body = SerializeLoadingSignalData(); |
| 96 signal_section.content_type = kApplicationJson; |
| 97 signal_section.content_location = "cid:signal-data-" + |
| 98 std::to_string(request.request_id()) + |
| 99 "@mhtml.blink"; |
| 100 content::MHTMLGenerationManager::MHTMLExtraData::AddToWebContents( |
| 101 web_contents, signal_section); |
| 102 |
86 SavePage(save_page_params, std::move(archiver), | 103 SavePage(save_page_params, std::move(archiver), |
87 base::Bind(&PrerenderingOffliner::OnSavePageDone, | 104 base::Bind(&PrerenderingOffliner::OnSavePageDone, |
88 weak_ptr_factory_.GetWeakPtr(), request)); | 105 weak_ptr_factory_.GetWeakPtr(), request)); |
89 } else { | 106 } else { |
90 // Clear pending request and app listener then run completion callback. | 107 // Clear pending request and app listener then run completion callback. |
91 pending_request_.reset(nullptr); | 108 pending_request_.reset(nullptr); |
92 app_listener_.reset(nullptr); | 109 app_listener_.reset(nullptr); |
93 completion_callback_.Run(request, load_status); | 110 completion_callback_.Run(request, load_status); |
94 } | 111 } |
95 } | 112 } |
96 | 113 |
| 114 std::string PrerenderingOffliner::SerializeLoadingSignalData() { |
| 115 // Write the signal data into a single string. |
| 116 std::string signal_string; |
| 117 const std::vector<std::string>& signals = loader_->GetLoadingSignalData(); |
| 118 |
| 119 // TODO(petewil): Convert this to JSON, use json_writer.h |
| 120 for (std::string signal : signals) { |
| 121 signal_string += signal; |
| 122 signal_string += "\r\n"; |
| 123 } |
| 124 return signal_string; |
| 125 } |
| 126 |
97 void PrerenderingOffliner::OnSavePageDone( | 127 void PrerenderingOffliner::OnSavePageDone( |
98 const SavePageRequest& request, | 128 const SavePageRequest& request, |
99 SavePageResult save_result, | 129 SavePageResult save_result, |
100 int64_t offline_id) { | 130 int64_t offline_id) { |
101 // Check if request is still pending receiving a callback. | 131 // Check if request is still pending receiving a callback. |
102 if (!pending_request_) | 132 if (!pending_request_) |
103 return; | 133 return; |
104 | 134 |
105 // Also check that this completed request is same as the pending one | 135 // 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). | 136 // (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( | 313 void PrerenderingOffliner::HandleApplicationStateChangeCancel( |
284 const SavePageRequest& request, | 314 const SavePageRequest& request, |
285 int64_t offline_id) { | 315 int64_t offline_id) { |
286 // This shouldn't be immediate, but account for case where request was reset | 316 // This shouldn't be immediate, but account for case where request was reset |
287 // while waiting for callback. | 317 // while waiting for callback. |
288 if (pending_request_ && pending_request_->request_id() != offline_id) | 318 if (pending_request_ && pending_request_->request_id() != offline_id) |
289 return; | 319 return; |
290 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); | 320 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); |
291 } | 321 } |
292 } // namespace offline_pages | 322 } // namespace offline_pages |
OLD | NEW |