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
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_REMOTE_SYNC_DELEGATE_H_
6#define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_REMOTE_SYNC_DELEGATE_H_
7
8#include <string>
9
10#include "base/callback.h"
11#include "base/memory/weak_ptr.h"
12#include "chrome/browser/sync_file_system/drive_backend/api_util.h"
13#include "chrome/browser/sync_file_system/drive_backend/drive_file_sync_service.h"
14#include "chrome/browser/sync_file_system/drive_backend/drive_metadata_store.h"
15#include "chrome/browser/sync_file_system/file_change.h"
16#include "chrome/browser/sync_file_system/sync_action.h"
17#include "chrome/browser/sync_file_system/sync_callbacks.h"
18#include "chrome/browser/sync_file_system/sync_file_metadata.h"
19#include "chrome/browser/sync_file_system/sync_file_system.pb.h"
20#include "webkit/browser/fileapi/file_system_url.h"
21#include "webkit/common/blob/scoped_file.h"
22
23namespace sync_file_system {
24
25class DriveMetadataStore;
26class RemoteChangeProcessor;
27
28namespace drive_backend {
29
30class APIUtil;
31
32// This class handles RemoteFileSyncService::ProcessRemoteChange for drive
33// backend, and its instance represents single ProcessRemoteChange operation.
34// The caller is responsible to own the instance, and can cancel operation by
35// deleting the instance.
36class RemoteSyncDelegate : public base::SupportsWeakPtr<RemoteSyncDelegate> {
37 public:
38  typedef RemoteChangeHandler::RemoteChange RemoteChange;
39
40  RemoteSyncDelegate(
41      DriveFileSyncService* sync_service,
42      const RemoteChange& remote_change);
43  virtual ~RemoteSyncDelegate();
44
45  void Run(const SyncStatusCallback& callback);
46
47  const fileapi::FileSystemURL& url() const { return remote_change_.url; }
48
49 private:
50  void DidPrepareForProcessRemoteChange(const SyncStatusCallback& callback,
51                                        SyncStatusCode status,
52                                        const SyncFileMetadata& metadata,
53                                        const FileChangeList& local_changes);
54  void ApplyRemoteChange(const SyncStatusCallback& callback);
55  void DidApplyRemoteChange(const SyncStatusCallback& callback,
56                            SyncStatusCode status);
57  void DeleteMetadata(const SyncStatusCallback& callback);
58  void DownloadFile(const SyncStatusCallback& callback);
59  void DidDownloadFile(const SyncStatusCallback& callback,
60                       google_apis::GDataErrorCode error,
61                       const std::string& md5_checksum,
62                       int64 file_size,
63                       const base::Time& updated_time,
64                       scoped_ptr<webkit_blob::ScopedFile> downloaded_file);
65  void HandleConflict(const SyncStatusCallback& callback,
66                      SyncFileType remote_file_type);
67  void HandleLocalWin(const SyncStatusCallback& callback);
68  void HandleRemoteWin(const SyncStatusCallback& callback,
69                       SyncFileType remote_file_type);
70  void HandleManualResolutionCase(const SyncStatusCallback& callback);
71  void DidGetEntryForConflictResolution(
72      const SyncStatusCallback& callback,
73      google_apis::GDataErrorCode error,
74      scoped_ptr<google_apis::ResourceEntry> entry);
75  void ResolveToLocal(const SyncStatusCallback& callback);
76  void DidResolveToLocal(const SyncStatusCallback& callback,
77                         SyncStatusCode status);
78  void ResolveToRemote(const SyncStatusCallback& callback);
79  void DidResolveToRemote(const SyncStatusCallback& callback,
80                          SyncStatusCode status);
81  void StartOver(const SyncStatusCallback& callback,
82                 SyncStatusCode status);
83
84  void CompleteSync(const SyncStatusCallback& callback,
85                    SyncStatusCode status);
86  void AbortSync(const SyncStatusCallback& callback,
87                 SyncStatusCode status);
88  void DidFinish(const SyncStatusCallback& callback,
89                 SyncStatusCode status);
90
91  SyncStatusCode GDataErrorCodeToSyncStatusCodeWrapper(
92      google_apis::GDataErrorCode error);
93
94  DriveMetadataStore* metadata_store();
95  APIUtilInterface* api_util();
96  RemoteChangeHandler* remote_change_handler();
97  RemoteChangeProcessor* remote_change_processor();
98  ConflictResolutionResolver* conflict_resolution_resolver();
99
100  const FileChange& remote_file_change() const { return remote_change_.change; }
101
102  DriveFileSyncService* sync_service_;  // Not owned.
103
104  RemoteChange remote_change_;
105  DriveMetadata drive_metadata_;
106  SyncFileMetadata local_metadata_;
107  webkit_blob::ScopedFile temporary_file_;
108  std::string md5_checksum_;
109  SyncAction sync_action_;
110  bool metadata_updated_;
111  bool clear_local_changes_;
112
113  std::string origin_resource_id_;
114
115  DISALLOW_COPY_AND_ASSIGN(RemoteSyncDelegate);
116};
117
118}  // namespace drive_backend
119
120}  // namespace sync_file_system
121
122#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_REMOTE_SYNC_DELEGATE_H_
123