Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Side by Side Diff: chrome/browser/sync_file_system/drive_file_sync_service.cc

Issue 14075016: Change some snapshot- or temporary-file related changes to use ScopedFile (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
tzik 2013/04/25 05:06:07 Since this function should be called on IO-allowed
kinuko 2013/04/25 06:02:08 Done.
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
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 1445 matching lines...) Expand 10 before | Expand all | Expand 10 after
1618 drive_metadata.set_md5_checksum(std::string()); 1614 drive_metadata.set_md5_checksum(std::string());
1619 metadata_store_->UpdateEntry( 1615 metadata_store_->UpdateEntry(
1620 url, drive_metadata, 1616 url, drive_metadata,
1621 base::Bind(&DriveFileSyncService::CompleteRemoteSync, 1617 base::Bind(&DriveFileSyncService::CompleteRemoteSync,
1622 AsWeakPtr(), base::Passed(&param))); 1618 AsWeakPtr(), base::Passed(&param)));
1623 } 1619 }
1624 } 1620 }
1625 1621
1626 void DriveFileSyncService::DownloadForRemoteSync( 1622 void DriveFileSyncService::DownloadForRemoteSync(
1627 scoped_ptr<ProcessRemoteChangeParam> param) { 1623 scoped_ptr<ProcessRemoteChangeParam> param) {
1628 // TODO(tzik): Use ShareableFileReference here after we get thread-safe 1624 webkit_blob::ScopedFile* temporary_file = &param->temporary_file;
1629 // version of it. crbug.com/162598
1630 base::FilePath* temporary_file_path = &param->temporary_file_path;
1631 content::BrowserThread::PostTaskAndReplyWithResult( 1625 content::BrowserThread::PostTaskAndReplyWithResult(
1632 content::BrowserThread::FILE, FROM_HERE, 1626 content::BrowserThread::FILE, FROM_HERE,
1633 base::Bind(&CreateTemporaryFile, 1627 base::Bind(&CreateTemporaryFile, temporary_file_dir_, temporary_file),
1634 temporary_file_dir_, temporary_file_path),
1635 base::Bind(&DriveFileSyncService::DidGetTemporaryFileForDownload, 1628 base::Bind(&DriveFileSyncService::DidGetTemporaryFileForDownload,
1636 AsWeakPtr(), base::Passed(&param))); 1629 AsWeakPtr(), base::Passed(&param)));
1637 } 1630 }
1638 1631
1639 void DriveFileSyncService::DidGetTemporaryFileForDownload( 1632 void DriveFileSyncService::DidGetTemporaryFileForDownload(
1640 scoped_ptr<ProcessRemoteChangeParam> param, 1633 scoped_ptr<ProcessRemoteChangeParam> param,
1641 bool success) { 1634 bool success) {
1642 if (!success) { 1635 if (!success) {
1643 AbortRemoteSync(param.Pass(), SYNC_FILE_ERROR_FAILED); 1636 AbortRemoteSync(param.Pass(), SYNC_FILE_ERROR_FAILED);
1644 return; 1637 return;
1645 } 1638 }
1646 1639
1647 const base::FilePath& temporary_file_path = param->temporary_file_path; 1640 const base::FilePath& temporary_file_path = param->temporary_file.path();
1648 std::string resource_id = param->remote_change.resource_id; 1641 std::string resource_id = param->remote_change.resource_id;
1642 DCHECK(!temporary_file_path.empty());
1649 1643
1650 // We should not use the md5 in metadata for FETCH type to avoid the download 1644 // We should not use the md5 in metadata for FETCH type to avoid the download
1651 // finishes due to NOT_MODIFIED. 1645 // finishes due to NOT_MODIFIED.
1652 std::string md5_checksum; 1646 std::string md5_checksum;
1653 if (param->remote_change.sync_type != REMOTE_SYNC_TYPE_FETCH) 1647 if (param->remote_change.sync_type != REMOTE_SYNC_TYPE_FETCH)
1654 md5_checksum = param->drive_metadata.md5_checksum(); 1648 md5_checksum = param->drive_metadata.md5_checksum();
1655 1649
1656 sync_client_->DownloadFile( 1650 sync_client_->DownloadFile(
1657 resource_id, md5_checksum, 1651 resource_id, md5_checksum,
1658 temporary_file_path, 1652 temporary_file_path,
(...skipping 12 matching lines...) Expand all
1671 } 1665 }
1672 1666
1673 SyncStatusCode status = GDataErrorCodeToSyncStatusCodeWrapper(error); 1667 SyncStatusCode status = GDataErrorCodeToSyncStatusCodeWrapper(error);
1674 if (status != SYNC_STATUS_OK) { 1668 if (status != SYNC_STATUS_OK) {
1675 AbortRemoteSync(param.Pass(), status); 1669 AbortRemoteSync(param.Pass(), status);
1676 return; 1670 return;
1677 } 1671 }
1678 1672
1679 param->drive_metadata.set_md5_checksum(md5_checksum); 1673 param->drive_metadata.set_md5_checksum(md5_checksum);
1680 const FileChange& change = param->remote_change.change; 1674 const FileChange& change = param->remote_change.change;
1681 const base::FilePath& temporary_file_path = param->temporary_file_path; 1675 const base::FilePath& temporary_file_path = param->temporary_file.path();
1682 const FileSystemURL& url = param->remote_change.url; 1676 const FileSystemURL& url = param->remote_change.url;
1683 remote_change_processor_->ApplyRemoteChange( 1677 remote_change_processor_->ApplyRemoteChange(
1684 change, temporary_file_path, url, 1678 change, temporary_file_path, url,
1685 base::Bind(&DriveFileSyncService::DidApplyRemoteChange, 1679 base::Bind(&DriveFileSyncService::DidApplyRemoteChange,
1686 AsWeakPtr(), base::Passed(&param))); 1680 AsWeakPtr(), base::Passed(&param)));
1687 } 1681 }
1688 1682
1689 void DriveFileSyncService::DidApplyRemoteChange( 1683 void DriveFileSyncService::DidApplyRemoteChange(
1690 scoped_ptr<ProcessRemoteChangeParam> param, 1684 scoped_ptr<ProcessRemoteChangeParam> param,
1691 SyncStatusCode status) { 1685 SyncStatusCode status) {
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
1772 // remote file. 1766 // remote file.
1773 if (param->clear_local_changes) { 1767 if (param->clear_local_changes) {
1774 const FileSystemURL& url = param->remote_change.url; 1768 const FileSystemURL& url = param->remote_change.url;
1775 param->clear_local_changes = false; 1769 param->clear_local_changes = false;
1776 remote_change_processor_->ClearLocalChanges( 1770 remote_change_processor_->ClearLocalChanges(
1777 url, base::Bind(&DriveFileSyncService::FinalizeRemoteSync, 1771 url, base::Bind(&DriveFileSyncService::FinalizeRemoteSync,
1778 AsWeakPtr(), base::Passed(&param), status)); 1772 AsWeakPtr(), base::Passed(&param), status));
1779 return; 1773 return;
1780 } 1774 }
1781 1775
1782 if (!param->temporary_file_path.empty())
1783 DeleteTemporaryFile(param->temporary_file_path);
1784 NotifyTaskDone(status, param->token.Pass()); 1776 NotifyTaskDone(status, param->token.Pass());
1785 if (status == SYNC_STATUS_OK && param->sync_action != SYNC_ACTION_NONE) { 1777 if (status == SYNC_STATUS_OK && param->sync_action != SYNC_ACTION_NONE) {
1786 NotifyObserversFileStatusChanged(param->remote_change.url, 1778 NotifyObserversFileStatusChanged(param->remote_change.url,
1787 SYNC_FILE_STATUS_SYNCED, 1779 SYNC_FILE_STATUS_SYNCED,
1788 param->sync_action, 1780 param->sync_action,
1789 SYNC_DIRECTION_REMOTE_TO_LOCAL); 1781 SYNC_DIRECTION_REMOTE_TO_LOCAL);
1790 } 1782 }
1791 param->callback.Run(status, param->remote_change.url); 1783 param->callback.Run(status, param->remote_change.url);
1792 } 1784 }
1793 1785
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after
2358 pending_batch_sync_origins_.insert(origin); 2350 pending_batch_sync_origins_.insert(origin);
2359 } 2351 }
2360 callback.Run(status, resource_id); 2352 callback.Run(status, resource_id);
2361 } 2353 }
2362 2354
2363 std::string DriveFileSyncService::sync_root_resource_id() { 2355 std::string DriveFileSyncService::sync_root_resource_id() {
2364 return metadata_store_->sync_root_directory(); 2356 return metadata_store_->sync_root_directory();
2365 } 2357 }
2366 2358
2367 } // namespace sync_file_system 2359 } // namespace sync_file_system
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698