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