| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "webkit/fileapi/syncable/local_file_change_tracker.h" | 5 #include "webkit/fileapi/syncable/local_file_change_tracker.h" |
| 6 | 6 |
| 7 #include <queue> | 7 #include <queue> |
| 8 | 8 |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 13 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 14 #include "webkit/fileapi/file_system_context.h" | 14 #include "webkit/fileapi/file_system_context.h" |
| 15 #include "webkit/fileapi/file_system_file_util.h" | 15 #include "webkit/fileapi/file_system_file_util.h" |
| 16 #include "webkit/fileapi/file_system_operation_context.h" | 16 #include "webkit/fileapi/file_system_operation_context.h" |
| 17 #include "webkit/fileapi/file_system_util.h" | 17 #include "webkit/fileapi/file_system_util.h" |
| 18 #include "webkit/fileapi/syncable/local_file_sync_status.h" | 18 #include "webkit/fileapi/syncable/local_file_sync_status.h" |
| 19 #include "webkit/fileapi/syncable/syncable_file_system_util.h" | 19 #include "webkit/fileapi/syncable/syncable_file_system_util.h" |
| 20 | 20 |
| 21 using sync_file_system::FileChange; |
| 22 using sync_file_system::FileChangeList; |
| 23 |
| 21 namespace fileapi { | 24 namespace fileapi { |
| 22 | 25 |
| 23 namespace { | 26 namespace { |
| 24 const base::FilePath::CharType kDatabaseName[] = | 27 const base::FilePath::CharType kDatabaseName[] = |
| 25 FILE_PATH_LITERAL("LocalFileChangeTracker"); | 28 FILE_PATH_LITERAL("LocalFileChangeTracker"); |
| 26 const char kMark[] = "d"; | 29 const char kMark[] = "d"; |
| 27 } // namespace | 30 } // namespace |
| 28 | 31 |
| 29 // A database class that stores local file changes in a local database. This | 32 // A database class that stores local file changes in a local database. This |
| 30 // object must be destructed on file_task_runner. | 33 // object must be destructed on file_task_runner. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 if (ContainsKey(changes_, url)) | 82 if (ContainsKey(changes_, url)) |
| 80 return; | 83 return; |
| 81 // TODO(nhiroki): propagate the error code (see http://crbug.com/152127). | 84 // TODO(nhiroki): propagate the error code (see http://crbug.com/152127). |
| 82 MarkDirtyOnDatabase(url); | 85 MarkDirtyOnDatabase(url); |
| 83 } | 86 } |
| 84 | 87 |
| 85 void LocalFileChangeTracker::OnEndUpdate(const FileSystemURL& url) {} | 88 void LocalFileChangeTracker::OnEndUpdate(const FileSystemURL& url) {} |
| 86 | 89 |
| 87 void LocalFileChangeTracker::OnCreateFile(const FileSystemURL& url) { | 90 void LocalFileChangeTracker::OnCreateFile(const FileSystemURL& url) { |
| 88 RecordChange(url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 91 RecordChange(url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
| 89 SYNC_FILE_TYPE_FILE)); | 92 sync_file_system::SYNC_FILE_TYPE_FILE)); |
| 90 } | 93 } |
| 91 | 94 |
| 92 void LocalFileChangeTracker::OnCreateFileFrom(const FileSystemURL& url, | 95 void LocalFileChangeTracker::OnCreateFileFrom(const FileSystemURL& url, |
| 93 const FileSystemURL& src) { | 96 const FileSystemURL& src) { |
| 94 RecordChange(url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 97 RecordChange(url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
| 95 SYNC_FILE_TYPE_FILE)); | 98 sync_file_system::SYNC_FILE_TYPE_FILE)); |
| 96 } | 99 } |
| 97 | 100 |
| 98 void LocalFileChangeTracker::OnRemoveFile(const FileSystemURL& url) { | 101 void LocalFileChangeTracker::OnRemoveFile(const FileSystemURL& url) { |
| 99 RecordChange(url, FileChange(FileChange::FILE_CHANGE_DELETE, | 102 RecordChange(url, FileChange(FileChange::FILE_CHANGE_DELETE, |
| 100 SYNC_FILE_TYPE_FILE)); | 103 sync_file_system::SYNC_FILE_TYPE_FILE)); |
| 101 } | 104 } |
| 102 | 105 |
| 103 void LocalFileChangeTracker::OnModifyFile(const FileSystemURL& url) { | 106 void LocalFileChangeTracker::OnModifyFile(const FileSystemURL& url) { |
| 104 RecordChange(url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 107 RecordChange(url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
| 105 SYNC_FILE_TYPE_FILE)); | 108 sync_file_system::SYNC_FILE_TYPE_FILE)); |
| 106 } | 109 } |
| 107 | 110 |
| 108 void LocalFileChangeTracker::OnCreateDirectory(const FileSystemURL& url) { | 111 void LocalFileChangeTracker::OnCreateDirectory(const FileSystemURL& url) { |
| 109 RecordChange(url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 112 RecordChange(url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
| 110 SYNC_FILE_TYPE_DIRECTORY)); | 113 sync_file_system::SYNC_FILE_TYPE_DIRECTORY)); |
| 111 } | 114 } |
| 112 | 115 |
| 113 void LocalFileChangeTracker::OnRemoveDirectory(const FileSystemURL& url) { | 116 void LocalFileChangeTracker::OnRemoveDirectory(const FileSystemURL& url) { |
| 114 RecordChange(url, FileChange(FileChange::FILE_CHANGE_DELETE, | 117 RecordChange(url, FileChange(FileChange::FILE_CHANGE_DELETE, |
| 115 SYNC_FILE_TYPE_DIRECTORY)); | 118 sync_file_system::SYNC_FILE_TYPE_DIRECTORY)); |
| 116 } | 119 } |
| 117 | 120 |
| 118 void LocalFileChangeTracker::GetNextChangedURLs( | 121 void LocalFileChangeTracker::GetNextChangedURLs( |
| 119 std::deque<FileSystemURL>* urls, int max_urls) { | 122 std::deque<FileSystemURL>* urls, int max_urls) { |
| 120 DCHECK(urls); | 123 DCHECK(urls); |
| 121 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); | 124 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); |
| 122 urls->clear(); | 125 urls->clear(); |
| 123 // Mildly prioritizes the URLs that older changes and have not been updated | 126 // Mildly prioritizes the URLs that older changes and have not been updated |
| 124 // for a while. | 127 // for a while. |
| 125 for (ChangeSeqMap::iterator iter = change_seqs_.begin(); | 128 for (ChangeSeqMap::iterator iter = change_seqs_.begin(); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 while (!dirty_files.empty()) { | 225 while (!dirty_files.empty()) { |
| 223 const FileSystemURL url = dirty_files.front(); | 226 const FileSystemURL url = dirty_files.front(); |
| 224 dirty_files.pop(); | 227 dirty_files.pop(); |
| 225 DCHECK_EQ(url.type(), kFileSystemTypeSyncable); | 228 DCHECK_EQ(url.type(), kFileSystemTypeSyncable); |
| 226 | 229 |
| 227 switch (file_util->GetFileInfo(context.get(), url, | 230 switch (file_util->GetFileInfo(context.get(), url, |
| 228 &file_info, &platform_path)) { | 231 &file_info, &platform_path)) { |
| 229 case base::PLATFORM_FILE_OK: { | 232 case base::PLATFORM_FILE_OK: { |
| 230 if (!file_info.is_directory) { | 233 if (!file_info.is_directory) { |
| 231 RecordChange(url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 234 RecordChange(url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
| 232 SYNC_FILE_TYPE_FILE)); | 235 sync_file_system::SYNC_FILE_TYPE_FILE)); |
| 233 break; | 236 break; |
| 234 } | 237 } |
| 235 | 238 |
| 236 RecordChange(url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 239 RecordChange(url, FileChange( |
| 237 SYNC_FILE_TYPE_DIRECTORY)); | 240 FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
| 241 sync_file_system::SYNC_FILE_TYPE_DIRECTORY)); |
| 238 | 242 |
| 239 // Push files and directories in this directory into |dirty_files|. | 243 // Push files and directories in this directory into |dirty_files|. |
| 240 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> enumerator( | 244 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> enumerator( |
| 241 file_util->CreateFileEnumerator(context.get(), | 245 file_util->CreateFileEnumerator(context.get(), |
| 242 url, | 246 url, |
| 243 false /* recursive */)); | 247 false /* recursive */)); |
| 244 base::FilePath path_each; | 248 base::FilePath path_each; |
| 245 while (!(path_each = enumerator->Next()).empty()) { | 249 while (!(path_each = enumerator->Next()).empty()) { |
| 246 dirty_files.push(CreateSyncableFileSystemURL( | 250 dirty_files.push(CreateSyncableFileSystemURL( |
| 247 url.origin(), url.filesystem_id(), path_each)); | 251 url.origin(), url.filesystem_id(), path_each)); |
| 248 } | 252 } |
| 249 break; | 253 break; |
| 250 } | 254 } |
| 251 case base::PLATFORM_FILE_ERROR_NOT_FOUND: { | 255 case base::PLATFORM_FILE_ERROR_NOT_FOUND: { |
| 252 // File represented by |url| has already been deleted. Since we cannot | 256 // File represented by |url| has already been deleted. Since we cannot |
| 253 // figure out if this file was directory or not from the URL, file | 257 // figure out if this file was directory or not from the URL, file |
| 254 // type is treated as SYNC_FILE_TYPE_UNKNOWN. | 258 // type is treated as sync_file_system::SYNC_FILE_TYPE_UNKNOWN. |
| 255 // | 259 // |
| 256 // NOTE: Directory to have been reverted (that is, ADD -> DELETE) is | 260 // NOTE: Directory to have been reverted (that is, ADD -> DELETE) is |
| 257 // also treated as FILE_CHANGE_DELETE. | 261 // also treated as FILE_CHANGE_DELETE. |
| 258 RecordChange(url, FileChange(FileChange::FILE_CHANGE_DELETE, | 262 RecordChange(url, FileChange(FileChange::FILE_CHANGE_DELETE, |
| 259 SYNC_FILE_TYPE_UNKNOWN)); | 263 sync_file_system::SYNC_FILE_TYPE_UNKNOWN)); |
| 260 break; | 264 break; |
| 261 } | 265 } |
| 262 case base::PLATFORM_FILE_ERROR_FAILED: | 266 case base::PLATFORM_FILE_ERROR_FAILED: |
| 263 default: | 267 default: |
| 264 // TODO(nhiroki): handle file access error (http://crbug.com/155251). | 268 // TODO(nhiroki): handle file access error (http://crbug.com/155251). |
| 265 LOG(WARNING) << "Failed to access local file."; | 269 LOG(WARNING) << "Failed to access local file."; |
| 266 break; | 270 break; |
| 267 } | 271 } |
| 268 } | 272 } |
| 269 return SYNC_STATUS_OK; | 273 return SYNC_STATUS_OK; |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 db_.reset(); | 414 db_.reset(); |
| 411 return db_status_; | 415 return db_status_; |
| 412 } | 416 } |
| 413 dirty_files->push(url); | 417 dirty_files->push(url); |
| 414 iter->Next(); | 418 iter->Next(); |
| 415 } | 419 } |
| 416 return SYNC_STATUS_OK; | 420 return SYNC_STATUS_OK; |
| 417 } | 421 } |
| 418 | 422 |
| 419 } // namespace fileapi | 423 } // namespace fileapi |
| OLD | NEW |