OLD | NEW |
---|---|
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" |
(...skipping 11 matching lines...) Expand all Loading... | |
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(bool disable_cache, |
30 const GURL& url, | 30 const GURL& url, |
31 mojo::NetworkService* network_service, | 31 mojo::NetworkService* network_service, |
32 mojo::service_cache::ServiceCache* service_cache, | |
32 const FetchCallback& loader_callback) | 33 const FetchCallback& loader_callback) |
33 : Fetcher(loader_callback), | 34 : Fetcher(loader_callback), |
34 disable_cache_(false), | 35 disable_cache_(false), |
35 url_(url), | 36 url_(url), |
37 service_cache_(service_cache), | |
36 weak_ptr_factory_(this) { | 38 weak_ptr_factory_(this) { |
37 StartNetworkRequest(url, network_service); | 39 StartNetworkRequest(url, network_service); |
38 } | 40 } |
39 | 41 |
40 NetworkFetcher::~NetworkFetcher() { | 42 NetworkFetcher::~NetworkFetcher() { |
41 } | 43 } |
42 | 44 |
43 const GURL& NetworkFetcher::GetURL() const { | 45 const GURL& NetworkFetcher::GetURL() const { |
44 return url_; | 46 return url_; |
45 } | 47 } |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 // The directory is leaked, because it can be reused at any time if the same | 143 // 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 | 144 // application is downloaded. Deleting it would be racy. This is only |
143 // happening when --predictable-app-filenames is used. | 145 // happening when --predictable-app-filenames is used. |
144 bool result = base::CreateDirectoryAndGetError(app_dir, nullptr); | 146 bool result = base::CreateDirectoryAndGetError(app_dir, nullptr); |
145 DCHECK(result); | 147 DCHECK(result); |
146 std::string unique_name = base::StringPrintf("%s.mojo", app_id.c_str()); | 148 std::string unique_name = base::StringPrintf("%s.mojo", app_id.c_str()); |
147 *new_path = app_dir.Append(unique_name); | 149 *new_path = app_dir.Append(unique_name); |
148 return base::Move(old_path, *new_path); | 150 return base::Move(old_path, *new_path); |
149 } | 151 } |
150 | 152 |
151 void NetworkFetcher::CopyCompleted( | 153 void NetworkFetcher::CacheFileRetrieved( |
152 base::Callback<void(const base::FilePath&, bool)> callback, | 154 base::Callback<void(const base::FilePath&, bool)> callback, |
153 bool success) { | 155 mojo::Array<uint8_t> path_as_array, |
156 mojo::Array<uint8_t> cache_dir) { | |
157 bool success = !path_as_array.is_null(); | |
154 if (success) { | 158 if (success) { |
159 path_ = base::FilePath(std::string( | |
160 reinterpret_cast<char*>(&path_as_array.front()), path_as_array.size())); | |
155 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 161 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
156 switches::kPredictableAppFilenames)) { | 162 switches::kPredictableAppFilenames)) { |
157 // The copy completed, now move to $TMP/$APP_ID.mojo before the dlopen. | 163 // The copy completed, now move to $TMP/$APP_ID.mojo before the dlopen. |
158 success = false; | |
159 base::FilePath new_path; | 164 base::FilePath new_path; |
160 if (RenameToAppId(url_, path_, &new_path)) { | 165 if (RenameToAppId(url_, path_, &new_path)) { |
161 if (base::PathExists(new_path)) { | 166 if (base::PathExists(new_path)) { |
162 path_ = new_path; | 167 path_ = new_path; |
163 success = true; | 168 success = true; |
164 } | 169 } |
165 } | 170 } |
166 } | 171 } |
167 } | 172 } |
168 | 173 |
169 if (success) | 174 if (success) |
170 RecordCacheToURLMapping(path_, url_); | 175 RecordCacheToURLMapping(path_, url_); |
171 | 176 |
172 base::MessageLoop::current()->PostTask(FROM_HERE, | 177 base::MessageLoop::current()->PostTask(FROM_HERE, |
173 base::Bind(callback, path_, success)); | 178 base::Bind(callback, path_, success)); |
174 } | 179 } |
175 | 180 |
176 void NetworkFetcher::AsPath( | 181 void NetworkFetcher::AsPath( |
177 base::TaskRunner* task_runner, | 182 base::TaskRunner* task_runner, |
178 base::Callback<void(const base::FilePath&, bool)> callback) { | 183 base::Callback<void(const base::FilePath&, bool)> callback) { |
184 // TODO(qsr) Test is not enough anymore -> we do not have anything while | |
185 // waiting for the service cache. This is fine for now, as AsPath is never | |
186 // called more than once. | |
179 if (!path_.empty() || !response_) { | 187 if (!path_.empty() || !response_) { |
180 base::MessageLoop::current()->PostTask( | 188 base::MessageLoop::current()->PostTask( |
181 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); | 189 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); |
182 return; | 190 return; |
183 } | 191 } |
184 | 192 |
185 base::CreateTemporaryFile(&path_); | 193 service_cache_->GetFile(response_.Pass(), |
186 mojo::common::CopyToFile( | 194 base::Bind(&NetworkFetcher::CacheFileRetrieved, |
187 response_->body.Pass(), path_, task_runner, | 195 weak_ptr_factory_.GetWeakPtr(), callback)); |
188 base::Bind(&NetworkFetcher::CopyCompleted, weak_ptr_factory_.GetWeakPtr(), | |
189 callback)); | |
190 } | 196 } |
191 | 197 |
192 std::string NetworkFetcher::MimeType() { | 198 std::string NetworkFetcher::MimeType() { |
193 return response_->mime_type; | 199 return response_->mime_type; |
194 } | 200 } |
195 | 201 |
196 bool NetworkFetcher::HasMojoMagic() { | 202 bool NetworkFetcher::HasMojoMagic() { |
197 std::string magic; | 203 std::string magic; |
198 return BlockingPeekNBytes(response_->body.get(), &magic, strlen(kMojoMagic), | 204 return BlockingPeekNBytes(response_->body.get(), &magic, strlen(kMojoMagic), |
199 kPeekTimeout) && | 205 kPeekTimeout) && |
200 magic == kMojoMagic; | 206 magic == kMojoMagic; |
201 } | 207 } |
202 | 208 |
203 bool NetworkFetcher::PeekFirstLine(std::string* line) { | 209 bool NetworkFetcher::PeekFirstLine(std::string* line) { |
204 return BlockingPeekLine(response_->body.get(), line, kMaxShebangLength, | 210 return BlockingPeekLine(response_->body.get(), line, kMaxShebangLength, |
205 kPeekTimeout); | 211 kPeekTimeout); |
206 } | 212 } |
207 | 213 |
208 void NetworkFetcher::StartNetworkRequest( | 214 void NetworkFetcher::StartNetworkRequest( |
209 const GURL& url, | 215 const GURL& url, |
210 mojo::NetworkService* network_service) { | 216 mojo::NetworkService* network_service) { |
211 TRACE_EVENT_ASYNC_BEGIN1("mojo_shell", "NetworkFetcher::NetworkRequest", this, | 217 TRACE_EVENT_ASYNC_BEGIN1("mojo_shell", "NetworkFetcher::NetworkRequest", this, |
212 "url", url.spec()); | 218 "url", url.spec()); |
213 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 219 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
DaveMoore
2015/05/06 22:31:21
I thought the point of etags was that you sent the
qsr
2015/05/07 08:40:54
The network service is doing all of this for us. A
| |
214 request->url = mojo::String::From(url); | 220 request->url = mojo::String::From(url); |
215 request->auto_follow_redirects = false; | 221 request->auto_follow_redirects = false; |
216 request->bypass_cache = disable_cache_; | 222 request->bypass_cache = disable_cache_; |
217 | 223 |
218 network_service->CreateURLLoader(mojo::GetProxy(&url_loader_)); | 224 network_service->CreateURLLoader(mojo::GetProxy(&url_loader_)); |
219 url_loader_->Start(request.Pass(), | 225 url_loader_->Start(request.Pass(), |
220 base::Bind(&NetworkFetcher::OnLoadComplete, | 226 base::Bind(&NetworkFetcher::OnLoadComplete, |
221 weak_ptr_factory_.GetWeakPtr())); | 227 weak_ptr_factory_.GetWeakPtr())); |
222 } | 228 } |
223 | 229 |
(...skipping 14 matching lines...) Expand all Loading... | |
238 << "while fetching " << response->url; | 244 << "while fetching " << response->url; |
239 loader_callback_.Run(nullptr); | 245 loader_callback_.Run(nullptr); |
240 return; | 246 return; |
241 } | 247 } |
242 | 248 |
243 response_ = response.Pass(); | 249 response_ = response.Pass(); |
244 loader_callback_.Run(owner.Pass()); | 250 loader_callback_.Run(owner.Pass()); |
245 } | 251 } |
246 | 252 |
247 } // namespace shell | 253 } // namespace shell |
OLD | NEW |