Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: chrome/browser/android/offline_pages/prerendering_offliner.cc

Issue 2683493002: Get signals working in the EXTRA_DATA section of MHTML (Closed)
Patch Set: Dimich and DTrainor CR fixes Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 } // namespace
26
21 namespace offline_pages { 27 namespace offline_pages {
22 28
23 PrerenderingOffliner::PrerenderingOffliner( 29 PrerenderingOffliner::PrerenderingOffliner(
24 content::BrowserContext* browser_context, 30 content::BrowserContext* browser_context,
25 const OfflinerPolicy* policy, 31 const OfflinerPolicy* policy,
26 OfflinePageModel* offline_page_model) 32 OfflinePageModel* offline_page_model)
27 : browser_context_(browser_context), 33 : browser_context_(browser_context),
28 policy_(policy), 34 policy_(policy),
29 offline_page_model_(offline_page_model), 35 offline_page_model_(offline_page_model),
30 pending_request_(nullptr), 36 pending_request_(nullptr),
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 save_page_params.client_id = request.client_id(); 82 save_page_params.client_id = request.client_id();
77 save_page_params.proposed_offline_id = request.request_id(); 83 save_page_params.proposed_offline_id = request.request_id();
78 save_page_params.is_background = true; 84 save_page_params.is_background = true;
79 // Pass in the original URL if it is different from the last committed URL 85 // Pass in the original URL if it is different from the last committed URL
80 // when redirects occur. 86 // when redirects occur.
81 if (!request.original_url().is_empty()) 87 if (!request.original_url().is_empty())
82 save_page_params.original_url = request.original_url(); 88 save_page_params.original_url = request.original_url();
83 else if (save_page_params.url != request.url()) 89 else if (save_page_params.url != request.url())
84 save_page_params.original_url = request.url(); 90 save_page_params.original_url = request.url();
85 91
92 if (IsOfflinePagesLoadSignalCollectingEnabled()) {
93 // Stash loading signals for writing when we write out the MHTML.
94 std::string body = SerializeLoadingSignalData();
95 std::string content_type = kContentType;
96 std::string content_location = "cid:signal-data-" +
97 std::to_string(request.request_id()) +
98 "@mhtml.blink";
99
100 content::MHTMLExtraParts* extra_parts =
101 content::MHTMLExtraParts::FromWebContents(web_contents);
102 DCHECK(extra_parts);
103
104 extra_parts->AddExtraMHTMLPart(content_type, content_location, body);
105 }
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698