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

Side by Side Diff: shell/application_manager/network_fetcher.cc

Issue 1088533003: Adding URLResponse Disk Cache to mojo. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Follow review Created 5 years, 7 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
« no previous file with comments | « shell/application_manager/network_fetcher.h ('k') | shell/background_application_loader.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "shell/application_manager/network_fetcher.h" 5 #include "shell/application_manager/network_fetcher.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/files/file.h" 9 #include "base/files/file.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/process/process.h" 13 #include "base/process/process.h"
14 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
17 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
18 #include "base/trace_event/trace_event.h" 18 #include "base/trace_event/trace_event.h"
19 #include "crypto/secure_hash.h" 19 #include "crypto/secure_hash.h"
20 #include "crypto/sha2.h" 20 #include "crypto/sha2.h"
21 #include "mojo/common/common_type_converters.h" 21 #include "mojo/common/common_type_converters.h"
22 #include "mojo/common/data_pipe_utils.h" 22 #include "mojo/common/data_pipe_utils.h"
23 #include "mojo/services/network/public/interfaces/network_service.mojom.h" 23 #include "mojo/services/network/public/interfaces/network_service.mojom.h"
24 #include "shell/application_manager/data_pipe_peek.h" 24 #include "shell/application_manager/data_pipe_peek.h"
25 #include "shell/switches.h" 25 #include "shell/switches.h"
26 26
27 namespace shell { 27 namespace shell {
28 28
29 NetworkFetcher::NetworkFetcher(bool disable_cache, 29 NetworkFetcher::NetworkFetcher(
30 const GURL& url, 30 bool disable_cache,
31 mojo::NetworkService* network_service, 31 const GURL& url,
32 const FetchCallback& loader_callback) 32 mojo::NetworkService* network_service,
33 mojo::URLResponseDiskCache* url_response_disk_cache,
34 const FetchCallback& loader_callback)
33 : Fetcher(loader_callback), 35 : Fetcher(loader_callback),
34 disable_cache_(false), 36 disable_cache_(false),
35 url_(url), 37 url_(url),
38 url_response_disk_cache_(url_response_disk_cache),
36 weak_ptr_factory_(this) { 39 weak_ptr_factory_(this) {
37 StartNetworkRequest(url, network_service); 40 StartNetworkRequest(url, network_service);
38 } 41 }
39 42
40 NetworkFetcher::~NetworkFetcher() { 43 NetworkFetcher::~NetworkFetcher() {
41 } 44 }
42 45
43 const GURL& NetworkFetcher::GetURL() const { 46 const GURL& NetworkFetcher::GetURL() const {
44 return url_; 47 return url_;
45 } 48 }
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 // The directory is leaked, because it can be reused at any time if the same 144 // The directory is leaked, because it can be reused at any time if the same
142 // application is downloaded. Deleting it would be racy. This is only 145 // application is downloaded. Deleting it would be racy. This is only
143 // happening when --predictable-app-filenames is used. 146 // happening when --predictable-app-filenames is used.
144 bool result = base::CreateDirectoryAndGetError(app_dir, nullptr); 147 bool result = base::CreateDirectoryAndGetError(app_dir, nullptr);
145 DCHECK(result); 148 DCHECK(result);
146 std::string unique_name = base::StringPrintf("%s.mojo", app_id.c_str()); 149 std::string unique_name = base::StringPrintf("%s.mojo", app_id.c_str());
147 *new_path = app_dir.Append(unique_name); 150 *new_path = app_dir.Append(unique_name);
148 return base::Move(old_path, *new_path); 151 return base::Move(old_path, *new_path);
149 } 152 }
150 153
151 void NetworkFetcher::CopyCompleted( 154 void NetworkFetcher::OnFileRetrievedFromCache(
152 base::Callback<void(const base::FilePath&, bool)> callback, 155 base::Callback<void(const base::FilePath&, bool)> callback,
153 bool success) { 156 mojo::Array<uint8_t> path_as_array,
157 mojo::Array<uint8_t> cache_dir) {
158 bool success = !path_as_array.is_null();
154 if (success) { 159 if (success) {
160 path_ = base::FilePath(std::string(
161 reinterpret_cast<char*>(&path_as_array.front()), path_as_array.size()));
155 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 162 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
156 switches::kPredictableAppFilenames)) { 163 switches::kPredictableAppFilenames)) {
157 // The copy completed, now move to $TMP/$APP_ID.mojo before the dlopen. 164 // The copy completed, now move to $TMP/$APP_ID.mojo before the dlopen.
158 success = false;
159 base::FilePath new_path; 165 base::FilePath new_path;
160 if (RenameToAppId(url_, path_, &new_path)) { 166 if (RenameToAppId(url_, path_, &new_path)) {
161 if (base::PathExists(new_path)) { 167 if (base::PathExists(new_path)) {
162 path_ = new_path; 168 path_ = new_path;
163 success = true; 169 success = true;
164 } 170 }
165 } 171 }
166 } 172 }
167 } 173 }
168 174
169 if (success) 175 if (success)
170 RecordCacheToURLMapping(path_, url_); 176 RecordCacheToURLMapping(path_, url_);
171 177
172 base::MessageLoop::current()->PostTask(FROM_HERE, 178 base::MessageLoop::current()->PostTask(FROM_HERE,
173 base::Bind(callback, path_, success)); 179 base::Bind(callback, path_, success));
174 } 180 }
175 181
176 void NetworkFetcher::AsPath( 182 void NetworkFetcher::AsPath(
177 base::TaskRunner* task_runner, 183 base::TaskRunner* task_runner,
178 base::Callback<void(const base::FilePath&, bool)> callback) { 184 base::Callback<void(const base::FilePath&, bool)> callback) {
179 if (!path_.empty() || !response_) { 185 // This should only called once, when we have a response.
180 base::MessageLoop::current()->PostTask( 186 DCHECK(response_.get());
181 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_)));
182 return;
183 }
184 187
185 base::CreateTemporaryFile(&path_); 188 url_response_disk_cache_->GetFile(
186 mojo::common::CopyToFile( 189 response_.Pass(), base::Bind(&NetworkFetcher::OnFileRetrievedFromCache,
187 response_->body.Pass(), path_, task_runner, 190 weak_ptr_factory_.GetWeakPtr(), callback));
188 base::Bind(&NetworkFetcher::CopyCompleted, weak_ptr_factory_.GetWeakPtr(),
189 callback));
190 } 191 }
191 192
192 std::string NetworkFetcher::MimeType() { 193 std::string NetworkFetcher::MimeType() {
193 return response_->mime_type; 194 return response_->mime_type;
194 } 195 }
195 196
196 bool NetworkFetcher::HasMojoMagic() { 197 bool NetworkFetcher::HasMojoMagic() {
197 std::string magic; 198 std::string magic;
198 return BlockingPeekNBytes(response_->body.get(), &magic, strlen(kMojoMagic), 199 return BlockingPeekNBytes(response_->body.get(), &magic, strlen(kMojoMagic),
199 kPeekTimeout) && 200 kPeekTimeout) &&
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 << "while fetching " << response->url; 239 << "while fetching " << response->url;
239 loader_callback_.Run(nullptr); 240 loader_callback_.Run(nullptr);
240 return; 241 return;
241 } 242 }
242 243
243 response_ = response.Pass(); 244 response_ = response.Pass();
244 loader_callback_.Run(owner.Pass()); 245 loader_callback_.Run(owner.Pass());
245 } 246 }
246 247
247 } // namespace shell 248 } // namespace shell
OLDNEW
« no previous file with comments | « shell/application_manager/network_fetcher.h ('k') | shell/background_application_loader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698