| 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) && |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 |