| 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_data.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 #ifndef NDEBUG |
| 92 // Stash loading signals for writing when we write out the MHTML. |
| 93 std::string body = SerializeLoadingSignalData(); |
| 94 std::string content_type = kApplicationJson; |
| 95 std::string content_location = "cid:signal-data-" + |
| 96 std::to_string(request.request_id()) + |
| 97 "@mhtml.blink"; |
| 98 |
| 99 content::MHTMLExtraData* extra_data = |
| 100 content::MHTMLExtraData::FromWebContents(web_contents); |
| 101 DCHECK(extra_data); |
| 102 |
| 103 extra_data->StashMHTMLPartForAdditionToPage(content_type, content_location, |
| 104 body); |
| 105 #endif |
| 106 |
| 86 SavePage(save_page_params, std::move(archiver), | 107 SavePage(save_page_params, std::move(archiver), |
| 87 base::Bind(&PrerenderingOffliner::OnSavePageDone, | 108 base::Bind(&PrerenderingOffliner::OnSavePageDone, |
| 88 weak_ptr_factory_.GetWeakPtr(), request)); | 109 weak_ptr_factory_.GetWeakPtr(), request)); |
| 89 } else { | 110 } else { |
| 90 // Clear pending request and app listener then run completion callback. | 111 // Clear pending request and app listener then run completion callback. |
| 91 pending_request_.reset(nullptr); | 112 pending_request_.reset(nullptr); |
| 92 app_listener_.reset(nullptr); | 113 app_listener_.reset(nullptr); |
| 93 completion_callback_.Run(request, load_status); | 114 completion_callback_.Run(request, load_status); |
| 94 } | 115 } |
| 95 } | 116 } |
| 96 | 117 |
| 118 std::string PrerenderingOffliner::SerializeLoadingSignalData() { |
| 119 // Write the signal data into a single string. |
| 120 std::string signal_string; |
| 121 const std::vector<std::string>& signals = loader_->GetLoadingSignalData(); |
| 122 |
| 123 // TODO(petewil): Convert this to JSON, use json_writer.h |
| 124 for (std::string signal : signals) { |
| 125 signal_string += signal; |
| 126 signal_string += "\r\n"; |
| 127 } |
| 128 return signal_string; |
| 129 } |
| 130 |
| 97 void PrerenderingOffliner::OnSavePageDone( | 131 void PrerenderingOffliner::OnSavePageDone( |
| 98 const SavePageRequest& request, | 132 const SavePageRequest& request, |
| 99 SavePageResult save_result, | 133 SavePageResult save_result, |
| 100 int64_t offline_id) { | 134 int64_t offline_id) { |
| 101 // Check if request is still pending receiving a callback. | 135 // Check if request is still pending receiving a callback. |
| 102 if (!pending_request_) | 136 if (!pending_request_) |
| 103 return; | 137 return; |
| 104 | 138 |
| 105 // Also check that this completed request is same as the pending one | 139 // 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). | 140 // (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( | 317 void PrerenderingOffliner::HandleApplicationStateChangeCancel( |
| 284 const SavePageRequest& request, | 318 const SavePageRequest& request, |
| 285 int64_t offline_id) { | 319 int64_t offline_id) { |
| 286 // This shouldn't be immediate, but account for case where request was reset | 320 // This shouldn't be immediate, but account for case where request was reset |
| 287 // while waiting for callback. | 321 // while waiting for callback. |
| 288 if (pending_request_ && pending_request_->request_id() != offline_id) | 322 if (pending_request_ && pending_request_->request_id() != offline_id) |
| 289 return; | 323 return; |
| 290 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); | 324 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); |
| 291 } | 325 } |
| 292 } // namespace offline_pages | 326 } // namespace offline_pages |
| OLD | NEW |