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.
5 #include "chrome/browser/chromeos/drive/write_on_cache_file.h"
8 #include "base/callback.h"
9 #include "base/threading/sequenced_worker_pool.h"
10 #include "components/drive/file_system_core_util.h"
11 #include "components/drive/file_system_interface.h"
12 #include "content/public/browser/browser_thread.h"
14 using content::BrowserThread
;
20 // Runs |close_callback| and |reply|.
21 void RunCloseCallbackAndReplyTask(const base::Closure
& close_callback
,
22 const FileOperationCallback
& reply
,
24 if (!close_callback
.is_null())
26 DCHECK(!reply
.is_null());
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
,
37 const base::FilePath
& local_cache_path
,
38 const base::Closure
& close_callback
) {
39 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
41 BrowserThread::GetBlockingPool()->PostTaskAndReply(
43 base::Bind(file_io_task_callback
, error
, local_cache_path
),
44 base::Bind(&RunCloseCallbackAndReplyTask
, close_callback
, reply
, error
));
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
));
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_CURRENTLY_ON(BrowserThread::UI
);
64 DCHECK(!callback
.is_null());
65 DCHECK(!reply
.is_null());
67 file_system
->OpenFile(
71 base::Bind(&WriteOnCacheFileAfterOpenFile
, path
, callback
, reply
));