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

Side by Side Diff: chrome/browser/chromeos/imageburner/burn_manager.cc

Issue 9568001: Use URLFetcher to download ChromeOS recovery image config file (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: _ Created 8 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/chromeos/imageburner/burn_manager.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/chromeos/imageburner/burn_manager.h" 5 #include "chrome/browser/chromeos/imageburner/burn_manager.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
11 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/download/download_util.h" 12 #include "chrome/browser/download/download_util.h"
12 #include "chrome/browser/tab_contents/tab_util.h"
13 #include "chrome/common/chrome_paths.h" 13 #include "chrome/common/chrome_paths.h"
14 #include "content/browser/download/download_types.h" 14 #include "content/browser/download/download_types.h"
15 #include "content/browser/renderer_host/render_view_host.h"
16 #include "content/public/browser/browser_context.h" 15 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/render_process_host.h" 17 #include "content/public/browser/download_manager.h"
19 #include "content/public/browser/web_contents.h" 18 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/url_fetcher.h"
20 #include "net/base/file_stream.h"
21 #include "net/url_request/url_request_status.h"
20 22
21 using content::BrowserThread; 23 using content::BrowserThread;
22 using content::DownloadItem;
23 using content::DownloadManager; 24 using content::DownloadManager;
24 using content::WebContents; 25 using content::WebContents;
25 26
26 namespace chromeos { 27 namespace chromeos {
27 namespace imageburner { 28 namespace imageburner {
28 29
29 namespace { 30 namespace {
30 31
31 const char kConfigFileUrl[] = 32 const char kConfigFileUrl[] =
32 "https://dl.google.com/dl/edgedl/chromeos/recovery/recovery.conf"; 33 "https://dl.google.com/dl/edgedl/chromeos/recovery/recovery.conf";
33 const char kTempImageFolderName[] = "chromeos_image"; 34 const char kTempImageFolderName[] = "chromeos_image";
34 const char kConfigFileName[] = "recovery.conf";
35 35
36 BurnManager* g_burn_manager = NULL; 36 BurnManager* g_burn_manager = NULL;
37 37
38 // Cretes a directory and calls |callback| with the result on UI thread. 38 // Cretes a directory and calls |callback| with the result on UI thread.
39 void CreateDirectory(const FilePath& path, 39 void CreateDirectory(const FilePath& path,
40 base::Callback<void(bool success)> callback) { 40 base::Callback<void(bool success)> callback) {
41 const bool success = file_util::CreateDirectory(path); 41 const bool success = file_util::CreateDirectory(path);
42 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 42 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
43 base::Bind(callback, success)); 43 base::Bind(callback, success));
44 } 44 }
45 45
46 // Reads file content and calls |callback| with the result on UI thread.
47 void ReadFile(const FilePath& path,
48 base::Callback<void(bool success,
49 const std::string& file_content)> callback) {
50 std::string file_content;
51 const bool success = file_util::ReadFileToString(path, &file_content);
52 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
53 base::Bind(callback, success, file_content));
54 }
55
56 // Creates a FileStream and calls |callback| with the result on UI thread. 46 // Creates a FileStream and calls |callback| with the result on UI thread.
57 void CreateFileStream( 47 void CreateFileStream(
58 const FilePath& file_path, 48 const FilePath& file_path,
59 base::Callback<void(net::FileStream* file_stream)> callback) { 49 base::Callback<void(net::FileStream* file_stream)> callback) {
60 scoped_ptr<net::FileStream> file_stream(new net::FileStream(NULL)); 50 scoped_ptr<net::FileStream> file_stream(new net::FileStream(NULL));
61 // TODO(tbarzic): Save temp image file to temp folder instead of Downloads 51 // TODO(tbarzic): Save temp image file to temp folder instead of Downloads
62 // once extracting image directly to removalbe device is implemented 52 // once extracting image directly to removalbe device is implemented
63 if (file_stream->OpenSync(file_path, base::PLATFORM_FILE_OPEN_ALWAYS | 53 if (file_stream->OpenSync(file_path, base::PLATFORM_FILE_OPEN_ALWAYS |
64 base::PLATFORM_FILE_WRITE)) 54 base::PLATFORM_FILE_WRITE))
65 file_stream.reset(); 55 file_stream.reset();
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 } 212 }
223 213
224 //////////////////////////////////////////////////////////////////////////////// 214 ////////////////////////////////////////////////////////////////////////////////
225 // 215 //
226 // BurnManager 216 // BurnManager
227 // 217 //
228 //////////////////////////////////////////////////////////////////////////////// 218 ////////////////////////////////////////////////////////////////////////////////
229 219
230 BurnManager::BurnManager() 220 BurnManager::BurnManager()
231 : weak_ptr_factory_(this), 221 : weak_ptr_factory_(this),
232 download_manager_(NULL),
233 download_item_observer_added_(false),
234 active_download_item_(NULL),
235 config_file_url_(kConfigFileUrl), 222 config_file_url_(kConfigFileUrl),
236 config_file_requested_(false),
237 config_file_fetched_(false),
238 state_machine_(new StateMachine()), 223 state_machine_(new StateMachine()),
239 downloader_(NULL) { 224 downloader_(NULL) {
240 } 225 }
241 226
242 BurnManager::~BurnManager() { 227 BurnManager::~BurnManager() {
243 if (!image_dir_.empty()) { 228 if (!image_dir_.empty()) {
244 file_util::Delete(image_dir_, true); 229 file_util::Delete(image_dir_, true);
245 } 230 }
246 if (active_download_item_)
247 active_download_item_->RemoveObserver(this);
248 if (download_manager_)
249 download_manager_->RemoveObserver(this);
250 } 231 }
251 232
252 // static 233 // static
253 void BurnManager::Initialize() { 234 void BurnManager::Initialize() {
254 if (g_burn_manager) { 235 if (g_burn_manager) {
255 LOG(WARNING) << "BurnManager was already initialized"; 236 LOG(WARNING) << "BurnManager was already initialized";
256 return; 237 return;
257 } 238 }
258 g_burn_manager = new BurnManager(); 239 g_burn_manager = new BurnManager();
259 VLOG(1) << "BurnManager initialized"; 240 VLOG(1) << "BurnManager initialized";
260 } 241 }
261 242
262 // static 243 // static
263 void BurnManager::Shutdown() { 244 void BurnManager::Shutdown() {
264 if (!g_burn_manager) { 245 if (!g_burn_manager) {
265 LOG(WARNING) << "BurnManager::Shutdown() called with NULL manager"; 246 LOG(WARNING) << "BurnManager::Shutdown() called with NULL manager";
266 return; 247 return;
267 } 248 }
268 delete g_burn_manager; 249 delete g_burn_manager;
269 g_burn_manager = NULL; 250 g_burn_manager = NULL;
270 VLOG(1) << "BurnManager Shutdown completed"; 251 VLOG(1) << "BurnManager Shutdown completed";
271 } 252 }
272 253
273 // static 254 // static
274 BurnManager* BurnManager::GetInstance() { 255 BurnManager* BurnManager::GetInstance() {
275 return g_burn_manager; 256 return g_burn_manager;
276 } 257 }
277 258
278 void BurnManager::OnDownloadUpdated(DownloadItem* download) {
279 if (download->IsCancelled()) {
280 ConfigFileFetched(false, "");
281 DCHECK(!download_item_observer_added_);
282 DCHECK(active_download_item_ == NULL);
283 } else if (download->IsComplete()) {
284 OnConfigFileDownloaded();
285 }
286 }
287
288 void BurnManager::OnConfigFileDownloaded() {
289 BrowserThread::PostBlockingPoolTask(
290 FROM_HERE,
291 base::Bind(ReadFile,
292 config_file_path_,
293 base::Bind(&BurnManager::ConfigFileFetched,
294 weak_ptr_factory_.GetWeakPtr())));
295 }
296
297 void BurnManager::ModelChanged(DownloadManager* manager) {
298 DCHECK_EQ(download_manager_, manager);
299
300 std::vector<DownloadItem*> downloads;
301 download_manager_->GetTemporaryDownloads(GetImageDir(), &downloads);
302 if (download_item_observer_added_)
303 return;
304 for (std::vector<DownloadItem*>::const_iterator it = downloads.begin();
305 it != downloads.end();
306 ++it) {
307 if ((*it)->GetURL() == config_file_url_) {
308 download_item_observer_added_ = true;
309 (*it)->AddObserver(this);
310 active_download_item_ = *it;
311 break;
312 }
313 }
314 }
315
316 void BurnManager::OnBurnDownloadStarted(bool success) {
317 if (!success)
318 ConfigFileFetched(false, "");
319 }
320
321 void BurnManager::CreateImageDir(Delegate* delegate) { 259 void BurnManager::CreateImageDir(Delegate* delegate) {
322 if (image_dir_.empty()) { 260 if (image_dir_.empty()) {
323 CHECK(PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &image_dir_)); 261 CHECK(PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &image_dir_));
324 image_dir_ = image_dir_.Append(kTempImageFolderName); 262 image_dir_ = image_dir_.Append(kTempImageFolderName);
325 BrowserThread::PostBlockingPoolTask( 263 BrowserThread::PostBlockingPoolTask(
326 FROM_HERE, 264 FROM_HERE,
327 base::Bind(CreateDirectory, 265 base::Bind(CreateDirectory,
328 image_dir_, 266 image_dir_,
329 base::Bind(&BurnManager::OnImageDirCreated, 267 base::Bind(&BurnManager::OnImageDirCreated,
330 weak_ptr_factory_.GetWeakPtr(), 268 weak_ptr_factory_.GetWeakPtr(),
331 delegate))); 269 delegate)));
332 } else { 270 } else {
333 const bool success = true; 271 const bool success = true;
334 OnImageDirCreated(delegate, success); 272 OnImageDirCreated(delegate, success);
335 } 273 }
336 } 274 }
337 275
338 void BurnManager::OnImageDirCreated(Delegate* delegate, bool success) { 276 void BurnManager::OnImageDirCreated(Delegate* delegate, bool success) {
339 delegate->OnImageDirCreated(success); 277 delegate->OnImageDirCreated(success);
340 } 278 }
341 279
342 const FilePath& BurnManager::GetImageDir() { 280 const FilePath& BurnManager::GetImageDir() {
343 return image_dir_; 281 return image_dir_;
344 } 282 }
345 283
346 void BurnManager::FetchConfigFile(WebContents* web_contents, 284 void BurnManager::FetchConfigFile(Delegate* delegate) {
347 Delegate* delegate) { 285 if (config_file_fetched()) {
348 if (config_file_fetched_) {
349 delegate->OnConfigFileFetched(config_file_, true); 286 delegate->OnConfigFileFetched(config_file_, true);
350 return; 287 return;
351 } 288 }
352 downloaders_.push_back(delegate->AsWeakPtr()); 289 downloaders_.push_back(delegate->AsWeakPtr());
353 290
354 if (config_file_requested_) 291 if (config_fetcher_.get())
355 return; 292 return;
356 config_file_requested_ = true;
357 293
358 config_file_path_ = GetImageDir().Append(kConfigFileName); 294 config_fetcher_.reset(content::URLFetcher::Create(
359 download_manager_ = web_contents->GetBrowserContext()->GetDownloadManager(); 295 config_file_url_, content::URLFetcher::GET, this));
360 download_manager_->AddObserver(this); 296 config_fetcher_->StartWithRequestContextGetter(
361 downloader()->AddListener(this, config_file_url_); 297 g_browser_process->system_request_context());
362 downloader()->DownloadFile(config_file_url_, config_file_path_, web_contents); 298 }
299
300 void BurnManager::OnURLFetchComplete(const content::URLFetcher* source) {
301 if (source == config_fetcher_.get()) {
302 std::string data;
303 const bool success =
304 source->GetStatus().status() == net::URLRequestStatus::SUCCESS;
305 if (success)
306 config_fetcher_->GetResponseAsString(&data);
307 config_fetcher_.reset();
308 ConfigFileFetched(success, data);
309 }
363 } 310 }
364 311
365 void BurnManager::ConfigFileFetched(bool fetched, const std::string& content) { 312 void BurnManager::ConfigFileFetched(bool fetched, const std::string& content) {
366 if (config_file_fetched_) 313 if (config_file_fetched())
367 return; 314 return;
368 315
369 if (active_download_item_) {
370 active_download_item_->RemoveObserver(this);
371 active_download_item_ = NULL;
372 }
373 download_item_observer_added_ = false;
374 if (download_manager_)
375 download_manager_->RemoveObserver(this);
376
377 config_file_fetched_ = fetched;
378
379 if (fetched) { 316 if (fetched) {
380 config_file_.reset(content); 317 config_file_.reset(content);
381 } else { 318 } else {
382 config_file_.clear(); 319 config_file_.clear();
383 } 320 }
384 321
385 for (size_t i = 0; i < downloaders_.size(); ++i) { 322 for (size_t i = 0; i < downloaders_.size(); ++i) {
386 if (downloaders_[i]) { 323 if (downloaders_[i]) {
387 downloaders_[i]->OnConfigFileFetched(config_file_, fetched); 324 downloaders_[i]->OnConfigFileFetched(config_file_, fetched);
388 } 325 }
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 current_listener != listener_range.second; 388 current_listener != listener_range.second;
452 ++current_listener) { 389 ++current_listener) {
453 if (current_listener->second) 390 if (current_listener->second)
454 current_listener->second->OnBurnDownloadStarted(success); 391 current_listener->second->OnBurnDownloadStarted(success);
455 } 392 }
456 listeners_.erase(listener_range.first, listener_range.second); 393 listeners_.erase(listener_range.first, listener_range.second);
457 } 394 }
458 395
459 } // namespace imageburner 396 } // namespace imageburner
460 } // namespace chromeos 397 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/imageburner/burn_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698