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/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
;
26 namespace file_system
{
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
,
39 scoped_ptr
<base::ScopedClosureRunner
> file_closer
;
40 FileError error
= cache
->OpenForWrite(local_id
, &file_closer
);
41 if (error
!= FILE_ERROR_OK
)
44 base::PlatformFileError result
= base::PLATFORM_FILE_ERROR_FAILED
;
45 base::PlatformFile file
= base::CreatePlatformFile(
47 base::PLATFORM_FILE_OPEN
| base::PLATFORM_FILE_WRITE
,
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
;
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
),
75 download_operation_(new DownloadOperation(blocking_task_runner
,
80 temporary_file_directory
)),
81 weak_ptr_factory_(this) {
84 TruncateOperation::~TruncateOperation() {
87 void TruncateOperation::Truncate(const base::FilePath
& file_path
,
89 const FileOperationCallback
& callback
) {
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
91 DCHECK(!callback
.is_null());
94 base::MessageLoopProxy::current()->PostTask(
96 base::Bind(callback
, FILE_ERROR_INVALID_OPERATION
));
100 // TODO(kinaba): http://crbug.com/132780.
101 // Optimize the cases for small |length|, at least for |length| == 0.
102 download_operation_
->EnsureFileDownloadedByPath(
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(
113 const FileOperationCallback
& callback
,
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
) {
125 DCHECK(entry
->has_file_specific_info());
127 if (entry
->file_specific_info().is_hosted_document()) {
128 callback
.Run(FILE_ERROR_INVALID_OPERATION
);
132 base::PostTaskAndReplyWithResult(
133 blocking_task_runner_
.get(),
135 base::Bind(&TruncateOnBlockingPool
,
136 metadata_
, cache_
, entry
->local_id(), local_file_path
, length
),
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
,
146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
147 DCHECK(!callback
.is_null());
149 observer_
->OnCacheFileUploadNeededByOperation(local_id
);
154 } // namespace file_system