| 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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/debug/trace_event.h" | 12 #include "base/debug/trace_event.h" |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/message_loop_proxy.h" | 16 #include "base/message_loop_proxy.h" |
| 17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 18 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
| 20 #include "chrome/browser/extensions/extension_system.h" | 20 #include "chrome/browser/extensions/extension_system.h" |
| 21 #include "chrome/browser/google_apis/drive_notification_manager.h" | 21 #include "chrome/browser/google_apis/drive_notification_manager.h" |
| 22 #include "chrome/browser/google_apis/drive_notification_manager_factory.h" | 22 #include "chrome/browser/google_apis/drive_notification_manager_factory.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 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/remote_sync_operation_resolver.h" |
| 30 #include "chrome/browser/sync_file_system/sync_file_system.pb.h" | 31 #include "chrome/browser/sync_file_system/sync_file_system.pb.h" |
| 31 #include "chrome/common/extensions/extension.h" | 32 #include "chrome/common/extensions/extension.h" |
| 32 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
| 33 #include "extensions/common/constants.h" | 34 #include "extensions/common/constants.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; |
| (...skipping 1407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1447 bool missing_db_entry = (status != SYNC_STATUS_OK); | 1448 bool missing_db_entry = (status != SYNC_STATUS_OK); |
| 1448 if (missing_db_entry) { | 1449 if (missing_db_entry) { |
| 1449 param->drive_metadata.set_resource_id(param->remote_change.resource_id); | 1450 param->drive_metadata.set_resource_id(param->remote_change.resource_id); |
| 1450 param->drive_metadata.set_md5_checksum(std::string()); | 1451 param->drive_metadata.set_md5_checksum(std::string()); |
| 1451 param->drive_metadata.set_conflicted(false); | 1452 param->drive_metadata.set_conflicted(false); |
| 1452 param->drive_metadata.set_to_be_fetched(false); | 1453 param->drive_metadata.set_to_be_fetched(false); |
| 1453 } | 1454 } |
| 1454 bool missing_local_file = (metadata.file_type == SYNC_FILE_TYPE_UNKNOWN); | 1455 bool missing_local_file = (metadata.file_type == SYNC_FILE_TYPE_UNKNOWN); |
| 1455 | 1456 |
| 1456 RemoteSyncOperationType operation = | 1457 RemoteSyncOperationType operation = |
| 1457 ResolveRemoteSyncOperationType(remote_file_change, param.get(), | 1458 RemoteSyncOperationResolver::Resolve(remote_file_change, |
| 1458 local_changes, missing_local_file); | 1459 local_changes, |
| 1460 param->local_metadata.file_type, |
| 1461 param->drive_metadata.conflicted()); |
| 1459 | 1462 |
| 1460 DVLOG(1) << "ProcessRemoteChange for " << url.DebugString() | 1463 DVLOG(1) << "ProcessRemoteChange for " << url.DebugString() |
| 1461 << (param->drive_metadata.conflicted() ? " (conflicted)" : " ") | 1464 << (param->drive_metadata.conflicted() ? " (conflicted)" : " ") |
| 1462 << (missing_local_file ? " (missing local file)" : " ") | 1465 << (missing_local_file ? " (missing local file)" : " ") |
| 1463 << " remote_change: " << remote_file_change.DebugString() | 1466 << " remote_change: " << remote_file_change.DebugString() |
| 1464 << " ==> operation: " << operation; | 1467 << " ==> operation: " << operation; |
| 1468 DCHECK_NE(REMOTE_SYNC_OPERATION_FAIL, operation); |
| 1465 | 1469 |
| 1466 switch (operation) { | 1470 switch (operation) { |
| 1467 case REMOTE_SYNC_OPERATION_ADD_FILE: | 1471 case REMOTE_SYNC_OPERATION_ADD_FILE: |
| 1468 param->sync_action = SYNC_ACTION_ADDED; | 1472 param->sync_action = SYNC_ACTION_ADDED; |
| 1469 DownloadForRemoteSync(param.Pass()); | 1473 DownloadForRemoteSync(param.Pass()); |
| 1470 return; | 1474 return; |
| 1475 case REMOTE_SYNC_OPERATION_ADD_DIRECTORY: |
| 1476 // TODO(nhiroki): support directory operations (http://crbug.com/161442). |
| 1477 NOTIMPLEMENTED(); |
| 1478 AbortRemoteSync(param.Pass(), SYNC_STATUS_FAILED); |
| 1479 return; |
| 1471 case REMOTE_SYNC_OPERATION_UPDATE_FILE: | 1480 case REMOTE_SYNC_OPERATION_UPDATE_FILE: |
| 1472 param->sync_action = SYNC_ACTION_UPDATED; | 1481 param->sync_action = SYNC_ACTION_UPDATED; |
| 1473 DownloadForRemoteSync(param.Pass()); | 1482 DownloadForRemoteSync(param.Pass()); |
| 1474 return; | 1483 return; |
| 1475 case REMOTE_SYNC_OPERATION_DELETE_FILE: { | 1484 case REMOTE_SYNC_OPERATION_DELETE_FILE: { |
| 1476 param->sync_action = SYNC_ACTION_DELETED; | 1485 param->sync_action = SYNC_ACTION_DELETED; |
| 1477 const FileChange& file_change = remote_file_change; | 1486 const FileChange& file_change = remote_file_change; |
| 1478 remote_change_processor_->ApplyRemoteChange( | 1487 remote_change_processor_->ApplyRemoteChange( |
| 1479 file_change, base::FilePath(), url, | 1488 file_change, base::FilePath(), url, |
| 1480 base::Bind(&DriveFileSyncService::DidApplyRemoteChange, AsWeakPtr(), | 1489 base::Bind(&DriveFileSyncService::DidApplyRemoteChange, AsWeakPtr(), |
| 1481 base::Passed(¶m))); | 1490 base::Passed(¶m))); |
| 1482 return; | 1491 return; |
| 1483 } | 1492 } |
| 1493 case REMOTE_SYNC_OPERATION_DELETE_DIRECTORY: |
| 1494 // TODO(nhiroki): support directory operations (http://crbug.com/161442). |
| 1495 NOTIMPLEMENTED(); |
| 1496 AbortRemoteSync(param.Pass(), SYNC_STATUS_FAILED); |
| 1497 return; |
| 1484 case REMOTE_SYNC_OPERATION_NONE: | 1498 case REMOTE_SYNC_OPERATION_NONE: |
| 1485 param->sync_action = SYNC_ACTION_NONE; | 1499 param->sync_action = SYNC_ACTION_NONE; |
| 1486 CompleteRemoteSync(param.Pass(), SYNC_STATUS_OK); | 1500 CompleteRemoteSync(param.Pass(), SYNC_STATUS_OK); |
| 1487 return; | 1501 return; |
| 1488 case REMOTE_SYNC_OPERATION_CONFLICT: | 1502 case REMOTE_SYNC_OPERATION_CONFLICT: |
| 1489 HandleConflictForRemoteSync(param.Pass(), base::Time(), SYNC_STATUS_OK); | 1503 HandleConflictForRemoteSync(param.Pass(), base::Time(), SYNC_STATUS_OK); |
| 1490 return; | 1504 return; |
| 1491 case REMOTE_SYNC_OPERATION_RESOLVE_TO_LOCAL: | 1505 case REMOTE_SYNC_OPERATION_RESOLVE_TO_LOCAL: |
| 1492 ResolveConflictToLocalForRemoteSync(param.Pass()); | 1506 ResolveConflictToLocalForRemoteSync(param.Pass()); |
| 1493 return; | 1507 return; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1509 DeleteMetadataForRemoteSync(param.Pass()); | 1523 DeleteMetadataForRemoteSync(param.Pass()); |
| 1510 return; | 1524 return; |
| 1511 case REMOTE_SYNC_OPERATION_FAIL: | 1525 case REMOTE_SYNC_OPERATION_FAIL: |
| 1512 AbortRemoteSync(param.Pass(), SYNC_STATUS_FAILED); | 1526 AbortRemoteSync(param.Pass(), SYNC_STATUS_FAILED); |
| 1513 return; | 1527 return; |
| 1514 } | 1528 } |
| 1515 NOTREACHED(); | 1529 NOTREACHED(); |
| 1516 AbortRemoteSync(param.Pass(), SYNC_STATUS_FAILED); | 1530 AbortRemoteSync(param.Pass(), SYNC_STATUS_FAILED); |
| 1517 } | 1531 } |
| 1518 | 1532 |
| 1519 DriveFileSyncService::RemoteSyncOperationType | |
| 1520 DriveFileSyncService::ResolveRemoteSyncOperationType( | |
| 1521 const FileChange& remote_file_change, | |
| 1522 ProcessRemoteChangeParam* param, | |
| 1523 const FileChangeList& local_changes, | |
| 1524 bool missing_local_file) { | |
| 1525 if (param->drive_metadata.conflicted()) { | |
| 1526 switch (remote_file_change.change()) { | |
| 1527 case FileChange::FILE_CHANGE_ADD_OR_UPDATE: | |
| 1528 return missing_local_file ? REMOTE_SYNC_OPERATION_RESOLVE_TO_REMOTE | |
| 1529 : REMOTE_SYNC_OPERATION_CONFLICT; | |
| 1530 case FileChange::FILE_CHANGE_DELETE: | |
| 1531 return missing_local_file ? REMOTE_SYNC_OPERATION_DELETE_METADATA | |
| 1532 : REMOTE_SYNC_OPERATION_RESOLVE_TO_LOCAL; | |
| 1533 } | |
| 1534 } | |
| 1535 | |
| 1536 if (remote_file_change.IsAddOrUpdate()) { | |
| 1537 if (local_changes.empty()) | |
| 1538 return missing_local_file ? REMOTE_SYNC_OPERATION_ADD_FILE | |
| 1539 : REMOTE_SYNC_OPERATION_UPDATE_FILE; | |
| 1540 switch (local_changes.list().back().change()) { | |
| 1541 case FileChange::FILE_CHANGE_ADD_OR_UPDATE: | |
| 1542 return REMOTE_SYNC_OPERATION_CONFLICT; | |
| 1543 case FileChange::FILE_CHANGE_DELETE: | |
| 1544 return REMOTE_SYNC_OPERATION_ADD_FILE; | |
| 1545 } | |
| 1546 } | |
| 1547 | |
| 1548 DCHECK(remote_file_change.IsDelete()); | |
| 1549 if (local_changes.empty()) | |
| 1550 return missing_local_file ? REMOTE_SYNC_OPERATION_DELETE_METADATA | |
| 1551 : REMOTE_SYNC_OPERATION_DELETE_FILE; | |
| 1552 switch (local_changes.list().back().change()) { | |
| 1553 case FileChange::FILE_CHANGE_ADD_OR_UPDATE: | |
| 1554 return REMOTE_SYNC_OPERATION_NONE; | |
| 1555 case FileChange::FILE_CHANGE_DELETE: | |
| 1556 return REMOTE_SYNC_OPERATION_DELETE_METADATA; | |
| 1557 } | |
| 1558 NOTREACHED(); | |
| 1559 return REMOTE_SYNC_OPERATION_FAIL; | |
| 1560 } | |
| 1561 | |
| 1562 void DriveFileSyncService::DidResolveConflictToLocalChange( | 1533 void DriveFileSyncService::DidResolveConflictToLocalChange( |
| 1563 scoped_ptr<ProcessRemoteChangeParam> param, | 1534 scoped_ptr<ProcessRemoteChangeParam> param, |
| 1564 SyncStatusCode status) { | 1535 SyncStatusCode status) { |
| 1565 if (status != SYNC_STATUS_OK) { | 1536 if (status != SYNC_STATUS_OK) { |
| 1566 DCHECK_NE(SYNC_STATUS_HAS_CONFLICT, status); | 1537 DCHECK_NE(SYNC_STATUS_HAS_CONFLICT, status); |
| 1567 AbortRemoteSync(param.Pass(), status); | 1538 AbortRemoteSync(param.Pass(), status); |
| 1568 return; | 1539 return; |
| 1569 } | 1540 } |
| 1570 | 1541 |
| 1571 const FileSystemURL& url = param->remote_change.url; | 1542 const FileSystemURL& url = param->remote_change.url; |
| (...skipping 764 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2336 pending_batch_sync_origins_.insert(origin); | 2307 pending_batch_sync_origins_.insert(origin); |
| 2337 } | 2308 } |
| 2338 callback.Run(status, resource_id); | 2309 callback.Run(status, resource_id); |
| 2339 } | 2310 } |
| 2340 | 2311 |
| 2341 std::string DriveFileSyncService::sync_root_resource_id() { | 2312 std::string DriveFileSyncService::sync_root_resource_id() { |
| 2342 return metadata_store_->sync_root_directory(); | 2313 return metadata_store_->sync_root_directory(); |
| 2343 } | 2314 } |
| 2344 | 2315 |
| 2345 } // namespace sync_file_system | 2316 } // namespace sync_file_system |
| OLD | NEW |