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 #ifndef WEBKIT_FILEAPI_SYNCABLE_LOCAL_FILE_SYNC_CONTEXT_H_ | 5 #ifndef WEBKIT_FILEAPI_SYNCABLE_LOCAL_FILE_SYNC_CONTEXT_H_ |
6 #define WEBKIT_FILEAPI_SYNCABLE_LOCAL_FILE_SYNC_CONTEXT_H_ | 6 #define WEBKIT_FILEAPI_SYNCABLE_LOCAL_FILE_SYNC_CONTEXT_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/callback_forward.h" | 13 #include "base/callback.h" |
14 #include "base/file_path.h" | 14 #include "base/file_path.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
19 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
20 #include "webkit/fileapi/syncable/file_change.h" | 20 #include "webkit/fileapi/syncable/file_change.h" |
21 #include "webkit/fileapi/syncable/local_file_sync_status.h" | 21 #include "webkit/fileapi/syncable/local_file_sync_status.h" |
| 22 #include "webkit/fileapi/syncable/sync_callbacks.h" |
22 #include "webkit/fileapi/syncable/sync_status_code.h" | 23 #include "webkit/fileapi/syncable/sync_status_code.h" |
23 #include "webkit/storage/webkit_storage_export.h" | 24 #include "webkit/storage/webkit_storage_export.h" |
24 | 25 |
25 namespace base { | 26 namespace base { |
26 class SingleThreadTaskRunner; | 27 class SingleThreadTaskRunner; |
27 } | 28 } |
28 | 29 |
29 namespace fileapi { | 30 namespace fileapi { |
30 | 31 |
31 class FileChange; | 32 class FileChange; |
32 class FileSystemContext; | 33 class FileSystemContext; |
33 class LocalFileChangeTracker; | 34 class LocalFileChangeTracker; |
34 class SyncableFileOperationRunner; | 35 class SyncableFileOperationRunner; |
35 | 36 |
36 // This class works as a bridge between LocalFileSyncService (which is a | 37 // This class works as a bridge between LocalFileSyncService (which is a |
37 // per-profile object) and FileSystemContext's (which is a per-storage-partition | 38 // per-profile object) and FileSystemContext's (which is a per-storage-partition |
38 // object and may exist multiple in a profile). | 39 // object and may exist multiple in a profile). |
39 // An instance of this class is shared by FileSystemContexts and outlives | 40 // An instance of this class is shared by FileSystemContexts and outlives |
40 // LocalFileSyncService. | 41 // LocalFileSyncService. |
41 class WEBKIT_STORAGE_EXPORT LocalFileSyncContext | 42 class WEBKIT_STORAGE_EXPORT LocalFileSyncContext |
42 : public base::RefCountedThreadSafe<LocalFileSyncContext> { | 43 : public base::RefCountedThreadSafe<LocalFileSyncContext>, |
| 44 public LocalFileSyncStatus::Observer { |
43 public: | 45 public: |
44 typedef base::Callback<void(SyncStatusCode status)> StatusCallback; | 46 typedef base::Callback<void( |
| 47 SyncStatusCode status, |
| 48 const FileChangeList& change)> ChangeListCallback; |
45 | 49 |
46 LocalFileSyncContext(base::SingleThreadTaskRunner* ui_task_runner, | 50 LocalFileSyncContext(base::SingleThreadTaskRunner* ui_task_runner, |
47 base::SingleThreadTaskRunner* io_task_runner); | 51 base::SingleThreadTaskRunner* io_task_runner); |
48 | 52 |
49 // Initializes |file_system_context| for syncable file operations and | 53 // Initializes |file_system_context| for syncable file operations and |
50 // registers the it into the internal map. | 54 // registers the it into the internal map. |
51 // Calling this multiple times for the same file_system_context is valid. | 55 // Calling this multiple times for the same file_system_context is valid. |
52 // This method must be called on UI thread. | 56 // This method must be called on UI thread. |
53 void MaybeInitializeFileSystemContext(const GURL& source_url, | 57 void MaybeInitializeFileSystemContext(const GURL& source_url, |
54 FileSystemContext* file_system_context, | 58 FileSystemContext* file_system_context, |
55 const StatusCallback& callback); | 59 const StatusCallback& callback); |
56 | 60 |
57 // Called when the corresponding LocalFileSyncService exits. | 61 // Called when the corresponding LocalFileSyncService exits. |
58 // This method must be called on UI thread. | 62 // This method must be called on UI thread. |
59 void ShutdownOnUIThread(); | 63 void ShutdownOnUIThread(); |
60 | 64 |
| 65 // Prepares for sync |url| by disabling writes on |url|. |
| 66 // If the target |url| is being written and cannot start sync it |
| 67 // returns SYNC_STATUS_WRITING status code via |callback|. |
| 68 // Otherwise it disables writes, marks the |url| syncing and returns |
| 69 // the current change set made on |url|. |
| 70 // This method must be called on UI thread. |
| 71 void PrepareForSync(const FileSystemURL& url, |
| 72 const ChangeListCallback& callback); |
| 73 |
| 74 // Registers |url| to wait until sync is enabled for |url|. |
| 75 // |on_syncable_callback| is to be called when |url| becomes syncable |
| 76 // (i.e. when we have no pending writes and the file is successfully locked |
| 77 // for sync). |
| 78 // |
| 79 // Calling this method again while this already has another URL waiting |
| 80 // for sync will overwrite the previously registered URL. |
| 81 // |
| 82 // This method must be called on UI thread. |
| 83 void RegisterURLForWaitingSync(const FileSystemURL& url, |
| 84 const base::Closure& on_syncable_callback); |
| 85 |
61 // OperationRunner is accessible only on IO thread. | 86 // OperationRunner is accessible only on IO thread. |
62 base::WeakPtr<SyncableFileOperationRunner> operation_runner() const; | 87 base::WeakPtr<SyncableFileOperationRunner> operation_runner() const; |
63 | 88 |
64 // SyncContext is accessible only on IO thread. | 89 // SyncContext is accessible only on IO thread. |
65 LocalFileSyncStatus* sync_status() const; | 90 LocalFileSyncStatus* sync_status() const; |
66 | 91 |
| 92 protected: |
| 93 // LocalFileSyncStatus::Observer overrides. They are called on IO thread. |
| 94 virtual void OnSyncEnabled(const FileSystemURL& url) OVERRIDE; |
| 95 virtual void OnWriteEnabled(const FileSystemURL& url) OVERRIDE; |
| 96 |
67 private: | 97 private: |
68 typedef std::deque<StatusCallback> StatusCallbackQueue; | 98 typedef std::deque<StatusCallback> StatusCallbackQueue; |
69 friend class base::RefCountedThreadSafe<LocalFileSyncContext>; | 99 friend class base::RefCountedThreadSafe<LocalFileSyncContext>; |
70 friend class CannedSyncableFileSystem; | 100 friend class CannedSyncableFileSystem; |
71 | 101 |
72 virtual ~LocalFileSyncContext(); | 102 virtual ~LocalFileSyncContext(); |
73 | 103 |
74 void ShutdownOnIOThread(); | 104 void ShutdownOnIOThread(); |
75 | 105 |
76 // Helper routines for MaybeInitializeFileSystemContext. | 106 // Helper routines for MaybeInitializeFileSystemContext. |
77 void InitializeFileSystemContextOnIOThread( | 107 void InitializeFileSystemContextOnIOThread( |
78 const GURL& source_url, | 108 const GURL& source_url, |
79 FileSystemContext* file_system_context); | 109 FileSystemContext* file_system_context); |
80 SyncStatusCode InitializeChangeTrackerOnFileThread( | 110 SyncStatusCode InitializeChangeTrackerOnFileThread( |
81 scoped_ptr<LocalFileChangeTracker>* tracker_ptr, | 111 scoped_ptr<LocalFileChangeTracker>* tracker_ptr, |
82 FileSystemContext* file_system_context); | 112 FileSystemContext* file_system_context); |
83 void DidInitializeChangeTracker( | 113 void DidInitializeChangeTracker( |
84 scoped_ptr<LocalFileChangeTracker>* tracker_ptr, | 114 scoped_ptr<LocalFileChangeTracker>* tracker_ptr, |
85 const GURL& source_url, | 115 const GURL& source_url, |
86 FileSystemContext* file_system_context, | 116 FileSystemContext* file_system_context, |
87 SyncStatusCode status); | 117 SyncStatusCode status); |
88 void DidInitialize( | 118 void DidInitialize( |
89 const GURL& source_url, | 119 const GURL& source_url, |
90 FileSystemContext* file_system_context, | 120 FileSystemContext* file_system_context, |
91 SyncStatusCode status); | 121 SyncStatusCode status); |
92 | 122 |
| 123 // Helper routines for PrepareForSync. |
| 124 void DidDisabledWritesForPrepareForSync( |
| 125 const FileSystemURL& url, |
| 126 const ChangeListCallback& callback); |
| 127 |
93 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 128 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
94 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 129 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
95 | 130 |
| 131 // Indicates if the sync service is shutdown on UI thread. |
| 132 bool shutdown_on_ui_; |
| 133 |
96 // OperationRunner. This must be accessed only on IO thread. | 134 // OperationRunner. This must be accessed only on IO thread. |
97 scoped_ptr<SyncableFileOperationRunner> operation_runner_; | 135 scoped_ptr<SyncableFileOperationRunner> operation_runner_; |
98 | 136 |
99 // Keeps track of writing/syncing status. | 137 // Keeps track of writing/syncing status. |
100 // This must be accessed only on IO thread. | 138 // This must be accessed only on IO thread. |
101 scoped_ptr<LocalFileSyncStatus> sync_status_; | 139 scoped_ptr<LocalFileSyncStatus> sync_status_; |
102 | 140 |
103 // Pointers to file system contexts that have been initialized for | 141 // Pointers to file system contexts that have been initialized for |
104 // synchronization (i.e. that own this instance). | 142 // synchronization (i.e. that own this instance). |
105 // This must be accessed only on UI thread. | 143 // This must be accessed only on UI thread. |
106 std::set<FileSystemContext*> file_system_contexts_; | 144 std::set<FileSystemContext*> file_system_contexts_; |
107 | 145 |
108 // Accessed only on UI thread. | 146 // Accessed only on UI thread. |
109 std::map<FileSystemContext*, StatusCallbackQueue> | 147 std::map<FileSystemContext*, StatusCallbackQueue> |
110 pending_initialize_callbacks_; | 148 pending_initialize_callbacks_; |
111 | 149 |
112 // Origin to context map. (Assuming that as far as we're in the same | 150 // Origin to context map. (Assuming that as far as we're in the same |
113 // profile single origin wouldn't belong to multiple FileSystemContexts.) | 151 // profile single origin wouldn't belong to multiple FileSystemContexts.) |
114 // Accessed only on UI thread. | 152 // Accessed only on UI thread. |
115 std::map<GURL, FileSystemContext*> origin_to_contexts_; | 153 std::map<GURL, FileSystemContext*> origin_to_contexts_; |
116 | 154 |
| 155 // A URL and associated callback waiting for sync is enabled. |
| 156 // Accessed only on IO thread. |
| 157 FileSystemURL url_waiting_sync_on_io_; |
| 158 base::Closure url_syncable_callback_; |
| 159 |
117 DISALLOW_COPY_AND_ASSIGN(LocalFileSyncContext); | 160 DISALLOW_COPY_AND_ASSIGN(LocalFileSyncContext); |
118 }; | 161 }; |
119 | 162 |
120 } // namespace fileapi | 163 } // namespace fileapi |
121 | 164 |
122 #endif // WEBKIT_FILEAPI_SYNCABLE_LOCAL_FILE_SYNC_CONTEXT_H_ | 165 #endif // WEBKIT_FILEAPI_SYNCABLE_LOCAL_FILE_SYNC_CONTEXT_H_ |
OLD | NEW |