| 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" |
| 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::url_response_disk_cache::URLResponseDiskCache* |
| 34 url_response_disk_cache, |
| 35 const FetchCallback& loader_callback) |
| 33 : Fetcher(loader_callback), | 36 : Fetcher(loader_callback), |
| 34 disable_cache_(false), | 37 disable_cache_(false), |
| 35 url_(url), | 38 url_(url), |
| 39 url_response_disk_cache_(url_response_disk_cache), |
| 36 weak_ptr_factory_(this) { | 40 weak_ptr_factory_(this) { |
| 37 StartNetworkRequest(url, network_service); | 41 StartNetworkRequest(url, network_service); |
| 38 } | 42 } |
| 39 | 43 |
| 40 NetworkFetcher::~NetworkFetcher() { | 44 NetworkFetcher::~NetworkFetcher() { |
| 41 } | 45 } |
| 42 | 46 |
| 43 const GURL& NetworkFetcher::GetURL() const { | 47 const GURL& NetworkFetcher::GetURL() const { |
| 44 return url_; | 48 return url_; |
| 45 } | 49 } |
| (...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 | 145 // 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 | 146 // application is downloaded. Deleting it would be racy. This is only |
| 143 // happening when --predictable-app-filenames is used. | 147 // happening when --predictable-app-filenames is used. |
| 144 bool result = base::CreateDirectoryAndGetError(app_dir, nullptr); | 148 bool result = base::CreateDirectoryAndGetError(app_dir, nullptr); |
| 145 DCHECK(result); | 149 DCHECK(result); |
| 146 std::string unique_name = base::StringPrintf("%s.mojo", app_id.c_str()); | 150 std::string unique_name = base::StringPrintf("%s.mojo", app_id.c_str()); |
| 147 *new_path = app_dir.Append(unique_name); | 151 *new_path = app_dir.Append(unique_name); |
| 148 return base::Move(old_path, *new_path); | 152 return base::Move(old_path, *new_path); |
| 149 } | 153 } |
| 150 | 154 |
| 151 void NetworkFetcher::CopyCompleted( | 155 void NetworkFetcher::OnFileRetrievedFromCache( |
| 152 base::Callback<void(const base::FilePath&, bool)> callback, | 156 base::Callback<void(const base::FilePath&, bool)> callback, |
| 153 bool success) { | 157 mojo::Array<uint8_t> path_as_array, |
| 158 mojo::Array<uint8_t> cache_dir) { |
| 159 bool success = !path_as_array.is_null(); |
| 154 if (success) { | 160 if (success) { |
| 161 path_ = base::FilePath(std::string( |
| 162 reinterpret_cast<char*>(&path_as_array.front()), path_as_array.size())); |
| 155 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 163 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 156 switches::kPredictableAppFilenames)) { | 164 switches::kPredictableAppFilenames)) { |
| 157 // The copy completed, now move to $TMP/$APP_ID.mojo before the dlopen. | 165 // The copy completed, now move to $TMP/$APP_ID.mojo before the dlopen. |
| 158 success = false; | |
| 159 base::FilePath new_path; | 166 base::FilePath new_path; |
| 160 if (RenameToAppId(url_, path_, &new_path)) { | 167 if (RenameToAppId(url_, path_, &new_path)) { |
| 161 if (base::PathExists(new_path)) { | 168 if (base::PathExists(new_path)) { |
| 162 path_ = new_path; | 169 path_ = new_path; |
| 163 success = true; | 170 success = true; |
| 164 } | 171 } |
| 165 } | 172 } |
| 166 } | 173 } |
| 167 } | 174 } |
| 168 | 175 |
| 169 if (success) | 176 if (success) |
| 170 RecordCacheToURLMapping(path_, url_); | 177 RecordCacheToURLMapping(path_, url_); |
| 171 | 178 |
| 172 base::MessageLoop::current()->PostTask(FROM_HERE, | 179 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 173 base::Bind(callback, path_, success)); | 180 base::Bind(callback, path_, success)); |
| 174 } | 181 } |
| 175 | 182 |
| 176 void NetworkFetcher::AsPath( | 183 void NetworkFetcher::AsPath( |
| 177 base::TaskRunner* task_runner, | 184 base::TaskRunner* task_runner, |
| 178 base::Callback<void(const base::FilePath&, bool)> callback) { | 185 base::Callback<void(const base::FilePath&, bool)> callback) { |
| 179 if (!path_.empty() || !response_) { | 186 // This should only called once, when we have a response. |
| 180 base::MessageLoop::current()->PostTask( | 187 DCHECK(response_.get()); |
| 181 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); | |
| 182 return; | |
| 183 } | |
| 184 | 188 |
| 185 base::CreateTemporaryFile(&path_); | 189 url_response_disk_cache_->GetFile( |
| 186 mojo::common::CopyToFile( | 190 response_.Pass(), base::Bind(&NetworkFetcher::OnFileRetrievedFromCache, |
| 187 response_->body.Pass(), path_, task_runner, | 191 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 188 base::Bind(&NetworkFetcher::CopyCompleted, weak_ptr_factory_.GetWeakPtr(), | |
| 189 callback)); | |
| 190 } | 192 } |
| 191 | 193 |
| 192 std::string NetworkFetcher::MimeType() { | 194 std::string NetworkFetcher::MimeType() { |
| 193 return response_->mime_type; | 195 return response_->mime_type; |
| 194 } | 196 } |
| 195 | 197 |
| 196 bool NetworkFetcher::HasMojoMagic() { | 198 bool NetworkFetcher::HasMojoMagic() { |
| 197 std::string magic; | 199 std::string magic; |
| 198 return BlockingPeekNBytes(response_->body.get(), &magic, strlen(kMojoMagic), | 200 return BlockingPeekNBytes(response_->body.get(), &magic, strlen(kMojoMagic), |
| 199 kPeekTimeout) && | 201 kPeekTimeout) && |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 << "while fetching " << response->url; | 240 << "while fetching " << response->url; |
| 239 loader_callback_.Run(nullptr); | 241 loader_callback_.Run(nullptr); |
| 240 return; | 242 return; |
| 241 } | 243 } |
| 242 | 244 |
| 243 response_ = response.Pass(); | 245 response_ = response.Pass(); |
| 244 loader_callback_.Run(owner.Pass()); | 246 loader_callback_.Run(owner.Pass()); |
| 245 } | 247 } |
| 246 | 248 |
| 247 } // namespace shell | 249 } // namespace shell |
| OLD | NEW |