| 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 "chrome/browser/sync_file_system/drive_file_sync_service.h" | 5 #include "chrome/browser/sync_file_system/drive_file_sync_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "chrome/browser/sync_file_system/conflict_resolution_policy.h" | 24 #include "chrome/browser/sync_file_system/conflict_resolution_policy.h" |
| 25 #include "chrome/browser/sync_file_system/drive_file_sync_client.h" | 25 #include "chrome/browser/sync_file_system/drive_file_sync_client.h" |
| 26 #include "chrome/browser/sync_file_system/drive_file_sync_util.h" | 26 #include "chrome/browser/sync_file_system/drive_file_sync_util.h" |
| 27 #include "chrome/browser/sync_file_system/drive_metadata_store.h" | 27 #include "chrome/browser/sync_file_system/drive_metadata_store.h" |
| 28 #include "chrome/browser/sync_file_system/file_status_observer.h" | 28 #include "chrome/browser/sync_file_system/file_status_observer.h" |
| 29 #include "chrome/browser/sync_file_system/remote_change_processor.h" | 29 #include "chrome/browser/sync_file_system/remote_change_processor.h" |
| 30 #include "chrome/browser/sync_file_system/sync_file_system.pb.h" | 30 #include "chrome/browser/sync_file_system/sync_file_system.pb.h" |
| 31 #include "chrome/common/extensions/extension.h" | 31 #include "chrome/common/extensions/extension.h" |
| 32 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
| 33 #include "extensions/common/constants.h" | 33 #include "extensions/common/constants.h" |
| 34 #include "webkit/blob/scoped_file.h" |
| 34 #include "webkit/fileapi/file_system_url.h" | 35 #include "webkit/fileapi/file_system_url.h" |
| 35 #include "webkit/fileapi/syncable/sync_file_metadata.h" | 36 #include "webkit/fileapi/syncable/sync_file_metadata.h" |
| 36 #include "webkit/fileapi/syncable/sync_file_type.h" | 37 #include "webkit/fileapi/syncable/sync_file_type.h" |
| 37 #include "webkit/fileapi/syncable/syncable_file_system_util.h" | 38 #include "webkit/fileapi/syncable/syncable_file_system_util.h" |
| 38 | 39 |
| 39 using fileapi::FileSystemURL; | 40 using fileapi::FileSystemURL; |
| 40 | 41 |
| 41 namespace sync_file_system { | 42 namespace sync_file_system { |
| 42 | 43 |
| 43 namespace { | 44 namespace { |
| 44 | 45 |
| 45 const base::FilePath::CharType kTempDirName[] = FILE_PATH_LITERAL("tmp"); | 46 const base::FilePath::CharType kTempDirName[] = FILE_PATH_LITERAL("tmp"); |
| 46 const base::FilePath::CharType kSyncFileSystemDir[] = | 47 const base::FilePath::CharType kSyncFileSystemDir[] = |
| 47 FILE_PATH_LITERAL("Sync FileSystem"); | 48 FILE_PATH_LITERAL("Sync FileSystem"); |
| 48 | 49 |
| 49 // Incremental sync polling interval. | 50 // Incremental sync polling interval. |
| 50 // TODO(calvinlo): Improve polling algorithm dependent on whether push | 51 // TODO(calvinlo): Improve polling algorithm dependent on whether push |
| 51 // notifications are on or off. | 52 // notifications are on or off. |
| 52 const int64 kMinimumPollingDelaySeconds = 5; | 53 const int64 kMinimumPollingDelaySeconds = 5; |
| 53 const int64 kMaximumPollingDelaySeconds = 10 * 60; // 10 min | 54 const int64 kMaximumPollingDelaySeconds = 10 * 60; // 10 min |
| 54 const int64 kPollingDelaySecondsWithNotification = 4 * 60 * 60; // 4 hr | 55 const int64 kPollingDelaySecondsWithNotification = 4 * 60 * 60; // 4 hr |
| 55 const double kDelayMultiplier = 1.6; | 56 const double kDelayMultiplier = 1.6; |
| 56 | 57 |
| 57 bool CreateTemporaryFile(const base::FilePath& dir_path, | 58 bool CreateTemporaryFile(const base::FilePath& dir_path, |
| 58 base::FilePath* temp_file) { | 59 webkit_blob::ScopedFile* temp_file) { |
| 59 return file_util::CreateDirectory(dir_path) && | 60 base::FilePath temp_file_path; |
| 60 file_util::CreateTemporaryFileInDir(dir_path, temp_file); | 61 const bool success = file_util::CreateDirectory(dir_path) && |
| 61 } | 62 file_util::CreateTemporaryFileInDir(dir_path, &temp_file_path); |
| 62 | 63 if (!success) |
| 63 void DeleteTemporaryFile(const base::FilePath& file_path) { | 64 return success; |
| 64 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)) { | 65 *temp_file = webkit_blob::ScopedFile( |
| 65 content::BrowserThread::PostTask( | 66 temp_file_path, |
| 66 content::BrowserThread::FILE, FROM_HERE, | 67 webkit_blob::ScopedFile::DELETE_ON_SCOPE_OUT, |
| 67 base::Bind(&DeleteTemporaryFile, file_path)); | 68 base::MessageLoopProxy::current()); |
| 68 return; | 69 return success; |
| 69 } | |
| 70 | |
| 71 if (!file_util::Delete(file_path, true)) | |
| 72 LOG(ERROR) << "Leaked temporary file for Sync FileSystem: " | |
| 73 << file_path.value(); | |
| 74 } | 70 } |
| 75 | 71 |
| 76 void EmptyStatusCallback(SyncStatusCode status) {} | 72 void EmptyStatusCallback(SyncStatusCode status) {} |
| 77 | 73 |
| 78 void DidHandleUnregisteredOrigin(const GURL& origin, SyncStatusCode status) { | 74 void DidHandleUnregisteredOrigin(const GURL& origin, SyncStatusCode status) { |
| 79 // TODO(calvinlo): Disable syncing if status not ok (http://crbug.com/171611). | 75 // TODO(calvinlo): Disable syncing if status not ok (http://crbug.com/171611). |
| 80 DCHECK_EQ(SYNC_STATUS_OK, status); | 76 DCHECK_EQ(SYNC_STATUS_OK, status); |
| 81 if (status != SYNC_STATUS_OK) { | 77 if (status != SYNC_STATUS_OK) { |
| 82 LOG(WARNING) << "Remove origin failed for: " << origin.spec() | 78 LOG(WARNING) << "Remove origin failed for: " << origin.spec() |
| 83 << " status=" << status; | 79 << " status=" << status; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 }; | 148 }; |
| 153 | 149 |
| 154 struct DriveFileSyncService::ProcessRemoteChangeParam { | 150 struct DriveFileSyncService::ProcessRemoteChangeParam { |
| 155 scoped_ptr<TaskToken> token; | 151 scoped_ptr<TaskToken> token; |
| 156 RemoteChange remote_change; | 152 RemoteChange remote_change; |
| 157 SyncFileCallback callback; | 153 SyncFileCallback callback; |
| 158 | 154 |
| 159 DriveMetadata drive_metadata; | 155 DriveMetadata drive_metadata; |
| 160 SyncFileMetadata local_metadata; | 156 SyncFileMetadata local_metadata; |
| 161 bool metadata_updated; | 157 bool metadata_updated; |
| 162 base::FilePath temporary_file_path; | 158 webkit_blob::ScopedFile temporary_file; |
| 163 std::string md5_checksum; | 159 std::string md5_checksum; |
| 164 SyncAction sync_action; | 160 SyncAction sync_action; |
| 165 bool clear_local_changes; | 161 bool clear_local_changes; |
| 166 | 162 |
| 167 ProcessRemoteChangeParam(scoped_ptr<TaskToken> token, | 163 ProcessRemoteChangeParam(scoped_ptr<TaskToken> token, |
| 168 const RemoteChange& remote_change, | 164 const RemoteChange& remote_change, |
| 169 const SyncFileCallback& callback) | 165 const SyncFileCallback& callback) |
| 170 : token(token.Pass()), | 166 : token(token.Pass()), |
| 171 remote_change(remote_change), | 167 remote_change(remote_change), |
| 172 callback(callback), | 168 callback(callback), |
| (...skipping 1410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1583 drive_metadata.set_md5_checksum(std::string()); | 1579 drive_metadata.set_md5_checksum(std::string()); |
| 1584 metadata_store_->UpdateEntry( | 1580 metadata_store_->UpdateEntry( |
| 1585 url, drive_metadata, | 1581 url, drive_metadata, |
| 1586 base::Bind(&DriveFileSyncService::CompleteRemoteSync, | 1582 base::Bind(&DriveFileSyncService::CompleteRemoteSync, |
| 1587 AsWeakPtr(), base::Passed(¶m))); | 1583 AsWeakPtr(), base::Passed(¶m))); |
| 1588 } | 1584 } |
| 1589 } | 1585 } |
| 1590 | 1586 |
| 1591 void DriveFileSyncService::DownloadForRemoteSync( | 1587 void DriveFileSyncService::DownloadForRemoteSync( |
| 1592 scoped_ptr<ProcessRemoteChangeParam> param) { | 1588 scoped_ptr<ProcessRemoteChangeParam> param) { |
| 1593 // TODO(tzik): Use ShareableFileReference here after we get thread-safe | 1589 webkit_blob::ScopedFile* temporary_file = ¶m->temporary_file; |
| 1594 // version of it. crbug.com/162598 | |
| 1595 base::FilePath* temporary_file_path = ¶m->temporary_file_path; | |
| 1596 content::BrowserThread::PostTaskAndReplyWithResult( | 1590 content::BrowserThread::PostTaskAndReplyWithResult( |
| 1597 content::BrowserThread::FILE, FROM_HERE, | 1591 content::BrowserThread::FILE, FROM_HERE, |
| 1598 base::Bind(&CreateTemporaryFile, | 1592 base::Bind(&CreateTemporaryFile, temporary_file_dir_, temporary_file), |
| 1599 temporary_file_dir_, temporary_file_path), | |
| 1600 base::Bind(&DriveFileSyncService::DidGetTemporaryFileForDownload, | 1593 base::Bind(&DriveFileSyncService::DidGetTemporaryFileForDownload, |
| 1601 AsWeakPtr(), base::Passed(¶m))); | 1594 AsWeakPtr(), base::Passed(¶m))); |
| 1602 } | 1595 } |
| 1603 | 1596 |
| 1604 void DriveFileSyncService::DidGetTemporaryFileForDownload( | 1597 void DriveFileSyncService::DidGetTemporaryFileForDownload( |
| 1605 scoped_ptr<ProcessRemoteChangeParam> param, | 1598 scoped_ptr<ProcessRemoteChangeParam> param, |
| 1606 bool success) { | 1599 bool success) { |
| 1607 if (!success) { | 1600 if (!success) { |
| 1608 AbortRemoteSync(param.Pass(), SYNC_FILE_ERROR_FAILED); | 1601 AbortRemoteSync(param.Pass(), SYNC_FILE_ERROR_FAILED); |
| 1609 return; | 1602 return; |
| 1610 } | 1603 } |
| 1611 | 1604 |
| 1612 const base::FilePath& temporary_file_path = param->temporary_file_path; | 1605 const base::FilePath& temporary_file_path = param->temporary_file.path(); |
| 1613 std::string resource_id = param->remote_change.resource_id; | 1606 std::string resource_id = param->remote_change.resource_id; |
| 1607 DCHECK(!temporary_file_path.empty()); |
| 1614 | 1608 |
| 1615 // We should not use the md5 in metadata for FETCH type to avoid the download | 1609 // We should not use the md5 in metadata for FETCH type to avoid the download |
| 1616 // finishes due to NOT_MODIFIED. | 1610 // finishes due to NOT_MODIFIED. |
| 1617 std::string md5_checksum; | 1611 std::string md5_checksum; |
| 1618 if (param->remote_change.sync_type != REMOTE_SYNC_TYPE_FETCH) | 1612 if (param->remote_change.sync_type != REMOTE_SYNC_TYPE_FETCH) |
| 1619 md5_checksum = param->drive_metadata.md5_checksum(); | 1613 md5_checksum = param->drive_metadata.md5_checksum(); |
| 1620 | 1614 |
| 1621 sync_client_->DownloadFile( | 1615 sync_client_->DownloadFile( |
| 1622 resource_id, md5_checksum, | 1616 resource_id, md5_checksum, |
| 1623 temporary_file_path, | 1617 temporary_file_path, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1638 } | 1632 } |
| 1639 | 1633 |
| 1640 SyncStatusCode status = GDataErrorCodeToSyncStatusCodeWrapper(error); | 1634 SyncStatusCode status = GDataErrorCodeToSyncStatusCodeWrapper(error); |
| 1641 if (status != SYNC_STATUS_OK) { | 1635 if (status != SYNC_STATUS_OK) { |
| 1642 AbortRemoteSync(param.Pass(), status); | 1636 AbortRemoteSync(param.Pass(), status); |
| 1643 return; | 1637 return; |
| 1644 } | 1638 } |
| 1645 | 1639 |
| 1646 param->drive_metadata.set_md5_checksum(md5_checksum); | 1640 param->drive_metadata.set_md5_checksum(md5_checksum); |
| 1647 const FileChange& change = param->remote_change.change; | 1641 const FileChange& change = param->remote_change.change; |
| 1648 const base::FilePath& temporary_file_path = param->temporary_file_path; | 1642 const base::FilePath& temporary_file_path = param->temporary_file.path(); |
| 1649 const FileSystemURL& url = param->remote_change.url; | 1643 const FileSystemURL& url = param->remote_change.url; |
| 1650 remote_change_processor_->ApplyRemoteChange( | 1644 remote_change_processor_->ApplyRemoteChange( |
| 1651 change, temporary_file_path, url, | 1645 change, temporary_file_path, url, |
| 1652 base::Bind(&DriveFileSyncService::DidApplyRemoteChange, | 1646 base::Bind(&DriveFileSyncService::DidApplyRemoteChange, |
| 1653 AsWeakPtr(), base::Passed(¶m))); | 1647 AsWeakPtr(), base::Passed(¶m))); |
| 1654 } | 1648 } |
| 1655 | 1649 |
| 1656 void DriveFileSyncService::DidApplyRemoteChange( | 1650 void DriveFileSyncService::DidApplyRemoteChange( |
| 1657 scoped_ptr<ProcessRemoteChangeParam> param, | 1651 scoped_ptr<ProcessRemoteChangeParam> param, |
| 1658 SyncStatusCode status) { | 1652 SyncStatusCode status) { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1739 // remote file. | 1733 // remote file. |
| 1740 if (param->clear_local_changes) { | 1734 if (param->clear_local_changes) { |
| 1741 const FileSystemURL& url = param->remote_change.url; | 1735 const FileSystemURL& url = param->remote_change.url; |
| 1742 param->clear_local_changes = false; | 1736 param->clear_local_changes = false; |
| 1743 remote_change_processor_->ClearLocalChanges( | 1737 remote_change_processor_->ClearLocalChanges( |
| 1744 url, base::Bind(&DriveFileSyncService::FinalizeRemoteSync, | 1738 url, base::Bind(&DriveFileSyncService::FinalizeRemoteSync, |
| 1745 AsWeakPtr(), base::Passed(¶m), status)); | 1739 AsWeakPtr(), base::Passed(¶m), status)); |
| 1746 return; | 1740 return; |
| 1747 } | 1741 } |
| 1748 | 1742 |
| 1749 if (!param->temporary_file_path.empty()) | |
| 1750 DeleteTemporaryFile(param->temporary_file_path); | |
| 1751 NotifyTaskDone(status, param->token.Pass()); | 1743 NotifyTaskDone(status, param->token.Pass()); |
| 1752 if (status == SYNC_STATUS_OK && param->sync_action != SYNC_ACTION_NONE) { | 1744 if (status == SYNC_STATUS_OK && param->sync_action != SYNC_ACTION_NONE) { |
| 1753 NotifyObserversFileStatusChanged(param->remote_change.url, | 1745 NotifyObserversFileStatusChanged(param->remote_change.url, |
| 1754 SYNC_FILE_STATUS_SYNCED, | 1746 SYNC_FILE_STATUS_SYNCED, |
| 1755 param->sync_action, | 1747 param->sync_action, |
| 1756 SYNC_DIRECTION_REMOTE_TO_LOCAL); | 1748 SYNC_DIRECTION_REMOTE_TO_LOCAL); |
| 1757 } | 1749 } |
| 1758 param->callback.Run(status, param->remote_change.url); | 1750 param->callback.Run(status, param->remote_change.url); |
| 1759 } | 1751 } |
| 1760 | 1752 |
| (...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2336 pending_batch_sync_origins_.insert(origin); | 2328 pending_batch_sync_origins_.insert(origin); |
| 2337 } | 2329 } |
| 2338 callback.Run(status, resource_id); | 2330 callback.Run(status, resource_id); |
| 2339 } | 2331 } |
| 2340 | 2332 |
| 2341 std::string DriveFileSyncService::sync_root_resource_id() { | 2333 std::string DriveFileSyncService::sync_root_resource_id() { |
| 2342 return metadata_store_->sync_root_directory(); | 2334 return metadata_store_->sync_root_directory(); |
| 2343 } | 2335 } |
| 2344 | 2336 |
| 2345 } // namespace sync_file_system | 2337 } // namespace sync_file_system |
| OLD | NEW |