Home | History | Annotate | Download | only in drive
      1 // Copyright 2013 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/write_on_cache_file.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 "chrome/browser/chromeos/drive/file_system_util.h"
     12 #include "content/public/browser/browser_thread.h"
     13 
     14 using content::BrowserThread;
     15 
     16 namespace drive {
     17 
     18 namespace {
     19 
     20 // Runs |close_callback| and |reply|.
     21 void RunCloseCallbackAndReplyTask(const base::Closure& close_callback,
     22                                   const FileOperationCallback& reply,
     23                                   FileError error) {
     24   if (!close_callback.is_null())
     25     close_callback.Run();
     26   DCHECK(!reply.is_null());
     27   reply.Run(error);
     28 }
     29 
     30 // Runs |file_io_task_callback| in blocking pool and runs |close_callback|
     31 // in the UI thread after that.
     32 void WriteOnCacheFileAfterOpenFile(
     33     const base::FilePath& drive_path,
     34     const WriteOnCacheFileCallback& file_io_task_callback,
     35     const FileOperationCallback& reply,
     36     FileError error,
     37     const base::FilePath& local_cache_path,
     38     const base::Closure& close_callback) {
     39   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     40 
     41   BrowserThread::GetBlockingPool()->PostTaskAndReply(
     42       FROM_HERE,
     43       base::Bind(file_io_task_callback, error, local_cache_path),
     44       base::Bind(&RunCloseCallbackAndReplyTask, close_callback, reply, error));
     45 }
     46 
     47 }  // namespace
     48 
     49 void WriteOnCacheFile(FileSystemInterface* file_system,
     50                       const base::FilePath& path,
     51                       const std::string& mime_type,
     52                       const WriteOnCacheFileCallback& callback) {
     53   WriteOnCacheFileAndReply(file_system, path, mime_type, callback,
     54                            base::Bind(&util::EmptyFileOperationCallback));
     55 }
     56 
     57 void WriteOnCacheFileAndReply(FileSystemInterface* file_system,
     58                               const base::FilePath& path,
     59                               const std::string& mime_type,
     60                               const WriteOnCacheFileCallback& callback,
     61                               const FileOperationCallback& reply) {
     62   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     63   DCHECK(file_system);
     64   DCHECK(!callback.is_null());
     65   DCHECK(!reply.is_null());
     66 
     67   file_system->OpenFile(
     68       path,
     69       OPEN_OR_CREATE_FILE,
     70       mime_type,
     71       base::Bind(&WriteOnCacheFileAfterOpenFile, path, callback, reply));
     72 }
     73 
     74 }  // namespace drive
     75