- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / file_system / truncate_operation.cc
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/file_system/truncate_operation.h"
6
7 #include "base/bind.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"
21
22 using content::BrowserThread;
23
24 namespace drive {
25 namespace file_system {
26 namespace {
27
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,
34                                  int64 length,
35                                  std::string* local_id) {
36   DCHECK(metadata);
37   DCHECK(cache);
38   DCHECK(local_id);
39
40   FileError error = metadata->GetIdByResourceId(resource_id, local_id);
41   if (error != FILE_ERROR_OK)
42     return error;
43
44   error = cache->MarkDirty(*local_id);
45   if (error != FILE_ERROR_OK)
46     return error;
47
48   base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED;
49   base::PlatformFile file = base::CreatePlatformFile(
50       local_cache_path,
51       base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
52       NULL,
53       &result);
54   if (result != base::PLATFORM_FILE_OK)
55     return FILE_ERROR_FAILED;
56
57   DCHECK_NE(base::kInvalidPlatformFileValue, file);
58   base::ScopedPlatformFileCloser file_closer(&file);
59
60   if (!base::TruncatePlatformFile(file, length))
61     return FILE_ERROR_FAILED;
62
63   return FILE_ERROR_OK;
64 }
65
66 }  // namespace
67
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),
76       observer_(observer),
77       metadata_(metadata),
78       cache_(cache),
79       download_operation_(new DownloadOperation(blocking_task_runner,
80                                                 observer,
81                                                 scheduler,
82                                                 metadata,
83                                                 cache,
84                                                 temporary_file_directory)),
85       weak_ptr_factory_(this) {
86 }
87
88 TruncateOperation::~TruncateOperation() {
89 }
90
91 void TruncateOperation::Truncate(const base::FilePath& file_path,
92                                  int64 length,
93                                  const FileOperationCallback& callback) {
94   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
95   DCHECK(!callback.is_null());
96
97   if (length < 0) {
98     base::MessageLoopProxy::current()->PostTask(
99         FROM_HERE,
100         base::Bind(callback, FILE_ERROR_INVALID_OPERATION));
101     return;
102   }
103
104   // TODO(kinaba): http://crbug.com/132780.
105   // Optimize the cases for small |length|, at least for |length| == 0.
106   download_operation_->EnsureFileDownloadedByPath(
107       file_path,
108       ClientContext(USER_INITIATED),
109       GetFileContentInitializedCallback(),
110       google_apis::GetContentCallback(),
111       base::Bind(&TruncateOperation::TruncateAfterEnsureFileDownloadedByPath,
112                  weak_ptr_factory_.GetWeakPtr(), length, callback));
113 }
114
115 void TruncateOperation::TruncateAfterEnsureFileDownloadedByPath(
116     int64 length,
117     const FileOperationCallback& callback,
118     FileError error,
119     const base::FilePath& local_file_path,
120     scoped_ptr<ResourceEntry> entry) {
121   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
122   DCHECK(!callback.is_null());
123
124   if (error != FILE_ERROR_OK) {
125     callback.Run(error);
126     return;
127   }
128   DCHECK(entry);
129   DCHECK(entry->has_file_specific_info());
130
131   if (entry->file_specific_info().is_hosted_document()) {
132     callback.Run(FILE_ERROR_INVALID_OPERATION);
133     return;
134   }
135
136   std::string* local_id = new std::string;
137   base::PostTaskAndReplyWithResult(
138       blocking_task_runner_.get(),
139       FROM_HERE,
140       base::Bind(&TruncateOnBlockingPool,
141                  metadata_, cache_, entry->resource_id(), local_file_path,
142                  length, local_id),
143       base::Bind(
144           &TruncateOperation::TruncateAfterTruncateOnBlockingPool,
145           weak_ptr_factory_.GetWeakPtr(), base::Owned(local_id), callback));
146 }
147
148 void TruncateOperation::TruncateAfterTruncateOnBlockingPool(
149     const std::string* local_id,
150     const FileOperationCallback& callback,
151     FileError error) {
152   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
153   DCHECK(!callback.is_null());
154
155   observer_->OnCacheFileUploadNeededByOperation(*local_id);
156
157   callback.Run(error);
158 }
159
160 }  // namespace file_system
161 }  // namespace drive