Home | History | Annotate | Download | only in drive
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/chromeos/drive/file_write_helper.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/callback.h"
      9 #include "base/threading/sequenced_worker_pool.h"
     10 #include "chrome/browser/chromeos/drive/file_system_interface.h"
     11 #include "content/public/browser/browser_thread.h"
     12 
     13 using content::BrowserThread;
     14 
     15 namespace drive {
     16 
     17 FileWriteHelper::FileWriteHelper(FileSystemInterface* file_system)
     18     : file_system_(file_system),
     19       weak_ptr_factory_(this) {
     20   // Must be created in DriveIntegrationService.
     21   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     22 }
     23 
     24 FileWriteHelper::~FileWriteHelper() {
     25   // Must be destroyed in DriveIntegrationService.
     26   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     27 }
     28 
     29 void FileWriteHelper::PrepareWritableFileAndRun(
     30     const base::FilePath& file_path,
     31     const OpenFileCallback& callback) {
     32   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     33   DCHECK(!callback.is_null());
     34 
     35   file_system_->OpenFile(
     36       file_path, OPEN_OR_CREATE_FILE,
     37       base::Bind(&FileWriteHelper::PrepareWritableFileAndRunAfterOpenFile,
     38                  weak_ptr_factory_.GetWeakPtr(), file_path, callback));
     39 }
     40 
     41 void FileWriteHelper::PrepareWritableFileAndRunAfterOpenFile(
     42     const base::FilePath& file_path,
     43     const OpenFileCallback& callback,
     44     FileError error,
     45     const base::FilePath& local_cache_path,
     46     const base::Closure& close_callback) {
     47   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     48   DCHECK(!callback.is_null());
     49 
     50   if (error != FILE_ERROR_OK) {
     51     content::BrowserThread::GetBlockingPool()->PostTask(
     52         FROM_HERE,
     53         base::Bind(callback, error, base::FilePath()));
     54     return;
     55   }
     56 
     57   content::BrowserThread::GetBlockingPool()->PostTaskAndReply(
     58       FROM_HERE,
     59       base::Bind(callback, FILE_ERROR_OK, local_cache_path),
     60       close_callback);
     61 }
     62 
     63 }  // namespace drive
     64