OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/drive/file_system/open_file_operation.h" | 5 #include "chrome/browser/chromeos/drive/file_system/open_file_operation.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
11 #include "base/task_runner_util.h" | 11 #include "base/task_runner_util.h" |
12 #include "chrome/browser/chromeos/drive/drive.pb.h" | 12 #include "chrome/browser/chromeos/drive/drive.pb.h" |
13 #include "chrome/browser/chromeos/drive/file_cache.h" | 13 #include "chrome/browser/chromeos/drive/file_cache.h" |
14 #include "chrome/browser/chromeos/drive/file_errors.h" | 14 #include "chrome/browser/chromeos/drive/file_errors.h" |
15 #include "chrome/browser/chromeos/drive/file_system/create_file_operation.h" | 15 #include "chrome/browser/chromeos/drive/file_system/create_file_operation.h" |
16 #include "chrome/browser/chromeos/drive/file_system/download_operation.h" | 16 #include "chrome/browser/chromeos/drive/file_system/download_operation.h" |
| 17 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" |
17 #include "chrome/browser/chromeos/drive/file_system_interface.h" | 18 #include "chrome/browser/chromeos/drive/file_system_interface.h" |
18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
19 | 20 |
20 using content::BrowserThread; | 21 using content::BrowserThread; |
21 | 22 |
22 namespace drive { | 23 namespace drive { |
23 namespace file_system { | 24 namespace file_system { |
24 namespace { | 25 namespace { |
25 | 26 |
26 FileError UpdateFileLocalState(internal::FileCache* cache, | 27 FileError UpdateFileLocalState(internal::FileCache* cache, |
27 const std::string& resource_id, | 28 const std::string& resource_id, |
28 const std::string& md5, | 29 const std::string& md5, |
29 base::FilePath* local_file_path) { | 30 base::FilePath* local_file_path) { |
30 FileError error = cache->MarkDirty(resource_id, md5); | 31 FileError error = cache->MarkDirty(resource_id, md5); |
31 if (error != FILE_ERROR_OK) | 32 if (error != FILE_ERROR_OK) |
32 return error; | 33 return error; |
33 | 34 |
34 return cache->GetFile(resource_id, md5, local_file_path); | 35 return cache->GetFile(resource_id, md5, local_file_path); |
35 } | 36 } |
36 | 37 |
37 } // namespace | 38 } // namespace |
38 | 39 |
39 OpenFileOperation::OpenFileOperation( | 40 OpenFileOperation::OpenFileOperation( |
40 base::SequencedTaskRunner* blocking_task_runner, | 41 base::SequencedTaskRunner* blocking_task_runner, |
41 OperationObserver* observer, | 42 OperationObserver* observer, |
42 JobScheduler* scheduler, | 43 JobScheduler* scheduler, |
43 internal::ResourceMetadata* metadata, | 44 internal::ResourceMetadata* metadata, |
44 internal::FileCache* cache, | 45 internal::FileCache* cache, |
45 const base::FilePath& temporary_file_directory, | 46 const base::FilePath& temporary_file_directory) |
46 std::map<base::FilePath, int>* open_files) | |
47 : blocking_task_runner_(blocking_task_runner), | 47 : blocking_task_runner_(blocking_task_runner), |
| 48 observer_(observer), |
48 cache_(cache), | 49 cache_(cache), |
49 create_file_operation_(new CreateFileOperation( | 50 create_file_operation_(new CreateFileOperation( |
50 blocking_task_runner, observer, scheduler, metadata, cache)), | 51 blocking_task_runner, observer, scheduler, metadata, cache)), |
51 download_operation_(new DownloadOperation( | 52 download_operation_(new DownloadOperation( |
52 blocking_task_runner, observer, scheduler, | 53 blocking_task_runner, observer, scheduler, |
53 metadata, cache, temporary_file_directory)), | 54 metadata, cache, temporary_file_directory)), |
54 open_files_(open_files), | |
55 weak_ptr_factory_(this) { | 55 weak_ptr_factory_(this) { |
56 DCHECK(open_files); | |
57 } | 56 } |
58 | 57 |
59 OpenFileOperation::~OpenFileOperation() { | 58 OpenFileOperation::~OpenFileOperation() { |
60 } | 59 } |
61 | 60 |
62 void OpenFileOperation::OpenFile(const base::FilePath& file_path, | 61 void OpenFileOperation::OpenFile(const base::FilePath& file_path, |
63 OpenMode open_mode, | 62 OpenMode open_mode, |
64 const OpenFileCallback& callback) { | 63 const OpenFileCallback& callback) { |
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
66 DCHECK(!callback.is_null()); | 65 DCHECK(!callback.is_null()); |
(...skipping 23 matching lines...) Expand all Loading... |
90 } | 89 } |
91 | 90 |
92 void OpenFileOperation::OpenFileAfterCreateFile( | 91 void OpenFileOperation::OpenFileAfterCreateFile( |
93 const base::FilePath& file_path, | 92 const base::FilePath& file_path, |
94 const OpenFileCallback& callback, | 93 const OpenFileCallback& callback, |
95 FileError error) { | 94 FileError error) { |
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
97 DCHECK(!callback.is_null()); | 96 DCHECK(!callback.is_null()); |
98 | 97 |
99 if (error != FILE_ERROR_OK) { | 98 if (error != FILE_ERROR_OK) { |
100 callback.Run(error, base::FilePath()); | 99 callback.Run(error, base::FilePath(), base::Closure()); |
101 return; | 100 return; |
102 } | 101 } |
103 | 102 |
104 download_operation_->EnsureFileDownloadedByPath( | 103 download_operation_->EnsureFileDownloadedByPath( |
105 file_path, | 104 file_path, |
106 ClientContext(USER_INITIATED), | 105 ClientContext(USER_INITIATED), |
107 GetFileContentInitializedCallback(), | 106 GetFileContentInitializedCallback(), |
108 google_apis::GetContentCallback(), | 107 google_apis::GetContentCallback(), |
109 base::Bind( | 108 base::Bind( |
110 &OpenFileOperation::OpenFileAfterFileDownloaded, | 109 &OpenFileOperation::OpenFileAfterFileDownloaded, |
111 weak_ptr_factory_.GetWeakPtr(), file_path, callback)); | 110 weak_ptr_factory_.GetWeakPtr(), callback)); |
112 } | 111 } |
113 | 112 |
114 void OpenFileOperation::OpenFileAfterFileDownloaded( | 113 void OpenFileOperation::OpenFileAfterFileDownloaded( |
115 const base::FilePath& file_path, | |
116 const OpenFileCallback& callback, | 114 const OpenFileCallback& callback, |
117 FileError error, | 115 FileError error, |
118 const base::FilePath& local_file_path, | 116 const base::FilePath& local_file_path, |
119 scoped_ptr<ResourceEntry> entry) { | 117 scoped_ptr<ResourceEntry> entry) { |
120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
121 DCHECK(!callback.is_null()); | 119 DCHECK(!callback.is_null()); |
122 | 120 |
123 if (error == FILE_ERROR_OK) { | 121 if (error == FILE_ERROR_OK) { |
124 DCHECK(entry); | 122 DCHECK(entry); |
125 DCHECK(entry->has_file_specific_info()); | 123 DCHECK(entry->has_file_specific_info()); |
126 if (entry->file_specific_info().is_hosted_document()) | 124 if (entry->file_specific_info().is_hosted_document()) |
127 // No support for opening a hosted document. | 125 // No support for opening a hosted document. |
128 error = FILE_ERROR_INVALID_OPERATION; | 126 error = FILE_ERROR_INVALID_OPERATION; |
129 } | 127 } |
130 | 128 |
131 if (error != FILE_ERROR_OK) { | 129 if (error != FILE_ERROR_OK) { |
132 callback.Run(error, base::FilePath()); | 130 callback.Run(error, base::FilePath(), base::Closure()); |
133 return; | 131 return; |
134 } | 132 } |
135 | 133 |
136 // Note: after marking the file dirty, the local file path may be changed. | 134 // Note: after marking the file dirty, the local file path may be changed. |
137 // So, it is necessary to take the path again. | 135 // So, it is necessary to take the path again. |
138 base::FilePath* new_local_file_path = new base::FilePath; | 136 base::FilePath* new_local_file_path = new base::FilePath; |
139 base::PostTaskAndReplyWithResult( | 137 base::PostTaskAndReplyWithResult( |
140 blocking_task_runner_.get(), | 138 blocking_task_runner_.get(), |
141 FROM_HERE, | 139 FROM_HERE, |
142 base::Bind(&UpdateFileLocalState, | 140 base::Bind(&UpdateFileLocalState, |
143 cache_, | 141 cache_, |
144 entry->resource_id(), | 142 entry->resource_id(), |
145 entry->file_specific_info().md5(), | 143 entry->file_specific_info().md5(), |
146 new_local_file_path), | 144 new_local_file_path), |
147 base::Bind(&OpenFileOperation::OpenFileAfterUpdateLocalState, | 145 base::Bind(&OpenFileOperation::OpenFileAfterUpdateLocalState, |
148 weak_ptr_factory_.GetWeakPtr(), | 146 weak_ptr_factory_.GetWeakPtr(), |
149 file_path, | 147 entry->resource_id(), |
150 callback, | 148 callback, |
151 base::Owned(new_local_file_path))); | 149 base::Owned(new_local_file_path))); |
152 } | 150 } |
153 | 151 |
154 void OpenFileOperation::OpenFileAfterUpdateLocalState( | 152 void OpenFileOperation::OpenFileAfterUpdateLocalState( |
155 const base::FilePath& file_path, | 153 const std::string& resource_id, |
156 const OpenFileCallback& callback, | 154 const OpenFileCallback& callback, |
157 const base::FilePath* local_file_path, | 155 const base::FilePath* local_file_path, |
158 FileError error) { | 156 FileError error) { |
159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
160 DCHECK(!callback.is_null()); | 158 DCHECK(!callback.is_null()); |
161 | 159 |
162 if (error == FILE_ERROR_OK) | 160 if (error != FILE_ERROR_OK) { |
163 ++(*open_files_)[file_path]; | 161 callback.Run(error, base::FilePath(), base::Closure()); |
164 callback.Run(error, *local_file_path); | 162 return; |
| 163 } |
| 164 |
| 165 ++open_files_[resource_id]; |
| 166 callback.Run(error, *local_file_path, |
| 167 base::Bind(&OpenFileOperation::CloseFile, |
| 168 weak_ptr_factory_.GetWeakPtr(), resource_id)); |
| 169 } |
| 170 |
| 171 void OpenFileOperation::CloseFile(const std::string& resource_id) { |
| 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 173 DCHECK_GT(open_files_[resource_id], 0); |
| 174 |
| 175 if (--open_files_[resource_id] == 0) { |
| 176 // All clients closes this file, so notify to upload the file. |
| 177 open_files_.erase(resource_id); |
| 178 observer_->OnCacheFileUploadNeededByOperation(resource_id); |
| 179 } |
165 } | 180 } |
166 | 181 |
167 } // namespace file_system | 182 } // namespace file_system |
168 } // namespace drive | 183 } // namespace drive |
OLD | NEW |