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/file_system/truncate_operation.h"
8 #include "base/files/file_path.h"
9 #include "base/files/scoped_platform_file_closer.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/platform_file.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/task_runner_util.h"
15 #include "chrome/browser/chromeos/drive/drive.pb.h"
16 #include "chrome/browser/chromeos/drive/file_cache.h"
17 #include "chrome/browser/chromeos/drive/file_errors.h"
18 #include "chrome/browser/chromeos/drive/file_system/download_operation.h"
19 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h"
20 #include "content/public/browser/browser_thread.h"
22 using content::BrowserThread;
25 namespace file_system {
28 // Truncates the local file at |local_cache_path| to the |length| bytes,
29 // then marks the resource is dirty on |cache|.
30 FileError TruncateOnBlockingPool(internal::ResourceMetadata* metadata,
31 internal::FileCache* cache,
32 const std::string& resource_id,
33 const base::FilePath& local_cache_path,
35 std::string* local_id) {
40 FileError error = metadata->GetIdByResourceId(resource_id, local_id);
41 if (error != FILE_ERROR_OK)
44 error = cache->MarkDirty(*local_id);
45 if (error != FILE_ERROR_OK)
48 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED;
49 base::PlatformFile file = base::CreatePlatformFile(
51 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
54 if (result != base::PLATFORM_FILE_OK)
55 return FILE_ERROR_FAILED;
57 DCHECK_NE(base::kInvalidPlatformFileValue, file);
58 base::ScopedPlatformFileCloser file_closer(&file);
60 if (!base::TruncatePlatformFile(file, length))
61 return FILE_ERROR_FAILED;
68 TruncateOperation::TruncateOperation(
69 base::SequencedTaskRunner* blocking_task_runner,
70 OperationObserver* observer,
71 JobScheduler* scheduler,
72 internal::ResourceMetadata* metadata,
73 internal::FileCache* cache,
74 const base::FilePath& temporary_file_directory)
75 : blocking_task_runner_(blocking_task_runner),
79 download_operation_(new DownloadOperation(blocking_task_runner,
84 temporary_file_directory)),
85 weak_ptr_factory_(this) {
88 TruncateOperation::~TruncateOperation() {
91 void TruncateOperation::Truncate(const base::FilePath& file_path,
93 const FileOperationCallback& callback) {
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
95 DCHECK(!callback.is_null());
98 base::MessageLoopProxy::current()->PostTask(
100 base::Bind(callback, FILE_ERROR_INVALID_OPERATION));
104 // TODO(kinaba): http://crbug.com/132780.
105 // Optimize the cases for small |length|, at least for |length| == 0.
106 download_operation_->EnsureFileDownloadedByPath(
108 ClientContext(USER_INITIATED),
109 GetFileContentInitializedCallback(),
110 google_apis::GetContentCallback(),
111 base::Bind(&TruncateOperation::TruncateAfterEnsureFileDownloadedByPath,
112 weak_ptr_factory_.GetWeakPtr(), length, callback));
115 void TruncateOperation::TruncateAfterEnsureFileDownloadedByPath(
117 const FileOperationCallback& callback,
119 const base::FilePath& local_file_path,
120 scoped_ptr<ResourceEntry> entry) {
121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
122 DCHECK(!callback.is_null());
124 if (error != FILE_ERROR_OK) {
129 DCHECK(entry->has_file_specific_info());
131 if (entry->file_specific_info().is_hosted_document()) {
132 callback.Run(FILE_ERROR_INVALID_OPERATION);
136 std::string* local_id = new std::string;
137 base::PostTaskAndReplyWithResult(
138 blocking_task_runner_.get(),
140 base::Bind(&TruncateOnBlockingPool,
141 metadata_, cache_, entry->resource_id(), local_file_path,
144 &TruncateOperation::TruncateAfterTruncateOnBlockingPool,
145 weak_ptr_factory_.GetWeakPtr(), base::Owned(local_id), callback));
148 void TruncateOperation::TruncateAfterTruncateOnBlockingPool(
149 const std::string* local_id,
150 const FileOperationCallback& callback,
152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
153 DCHECK(!callback.is_null());
155 observer_->OnCacheFileUploadNeededByOperation(*local_id);
160 } // namespace file_system