Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / file_system / truncate_operation.cc
bloba5273f2d36e50e2e55acd0038dc156efe7435a9c
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"
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/files/file_path.h"
10 #include "base/files/scoped_platform_file_closer.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/platform_file.h"
14 #include "base/sequenced_task_runner.h"
15 #include "base/task_runner_util.h"
16 #include "chrome/browser/chromeos/drive/drive.pb.h"
17 #include "chrome/browser/chromeos/drive/file_cache.h"
18 #include "chrome/browser/chromeos/drive/file_errors.h"
19 #include "chrome/browser/chromeos/drive/file_system/download_operation.h"
20 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h"
21 #include "content/public/browser/browser_thread.h"
23 using content::BrowserThread;
25 namespace drive {
26 namespace file_system {
27 namespace {
29 // Truncates the local file at |local_cache_path| to the |length| bytes,
30 // then marks the resource is dirty on |cache|.
31 FileError TruncateOnBlockingPool(internal::ResourceMetadata* metadata,
32 internal::FileCache* cache,
33 const std::string& local_id,
34 const base::FilePath& local_cache_path,
35 int64 length) {
36 DCHECK(metadata);
37 DCHECK(cache);
39 scoped_ptr<base::ScopedClosureRunner> file_closer;
40 FileError error = cache->OpenForWrite(local_id, &file_closer);
41 if (error != FILE_ERROR_OK)
42 return error;
44 base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED;
45 base::PlatformFile file = base::CreatePlatformFile(
46 local_cache_path,
47 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
48 NULL,
49 &result);
50 if (result != base::PLATFORM_FILE_OK)
51 return FILE_ERROR_FAILED;
53 DCHECK_NE(base::kInvalidPlatformFileValue, file);
54 base::ScopedPlatformFileCloser platform_file_closer(&file);
56 if (!base::TruncatePlatformFile(file, length))
57 return FILE_ERROR_FAILED;
59 return FILE_ERROR_OK;
62 } // namespace
64 TruncateOperation::TruncateOperation(
65 base::SequencedTaskRunner* blocking_task_runner,
66 OperationObserver* observer,
67 JobScheduler* scheduler,
68 internal::ResourceMetadata* metadata,
69 internal::FileCache* cache,
70 const base::FilePath& temporary_file_directory)
71 : blocking_task_runner_(blocking_task_runner),
72 observer_(observer),
73 metadata_(metadata),
74 cache_(cache),
75 download_operation_(new DownloadOperation(blocking_task_runner,
76 observer,
77 scheduler,
78 metadata,
79 cache,
80 temporary_file_directory)),
81 weak_ptr_factory_(this) {
84 TruncateOperation::~TruncateOperation() {
87 void TruncateOperation::Truncate(const base::FilePath& file_path,
88 int64 length,
89 const FileOperationCallback& callback) {
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
91 DCHECK(!callback.is_null());
93 if (length < 0) {
94 base::MessageLoopProxy::current()->PostTask(
95 FROM_HERE,
96 base::Bind(callback, FILE_ERROR_INVALID_OPERATION));
97 return;
100 // TODO(kinaba): http://crbug.com/132780.
101 // Optimize the cases for small |length|, at least for |length| == 0.
102 download_operation_->EnsureFileDownloadedByPath(
103 file_path,
104 ClientContext(USER_INITIATED),
105 GetFileContentInitializedCallback(),
106 google_apis::GetContentCallback(),
107 base::Bind(&TruncateOperation::TruncateAfterEnsureFileDownloadedByPath,
108 weak_ptr_factory_.GetWeakPtr(), length, callback));
111 void TruncateOperation::TruncateAfterEnsureFileDownloadedByPath(
112 int64 length,
113 const FileOperationCallback& callback,
114 FileError error,
115 const base::FilePath& local_file_path,
116 scoped_ptr<ResourceEntry> entry) {
117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
118 DCHECK(!callback.is_null());
120 if (error != FILE_ERROR_OK) {
121 callback.Run(error);
122 return;
124 DCHECK(entry);
125 DCHECK(entry->has_file_specific_info());
127 if (entry->file_specific_info().is_hosted_document()) {
128 callback.Run(FILE_ERROR_INVALID_OPERATION);
129 return;
132 base::PostTaskAndReplyWithResult(
133 blocking_task_runner_.get(),
134 FROM_HERE,
135 base::Bind(&TruncateOnBlockingPool,
136 metadata_, cache_, entry->local_id(), local_file_path, length),
137 base::Bind(
138 &TruncateOperation::TruncateAfterTruncateOnBlockingPool,
139 weak_ptr_factory_.GetWeakPtr(), entry->local_id(), callback));
142 void TruncateOperation::TruncateAfterTruncateOnBlockingPool(
143 const std::string& local_id,
144 const FileOperationCallback& callback,
145 FileError error) {
146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
147 DCHECK(!callback.is_null());
149 observer_->OnCacheFileUploadNeededByOperation(local_id);
151 callback.Run(error);
154 } // namespace file_system
155 } // namespace drive