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/sync_file_system_service.h" | 5 #include "chrome/browser/sync_file_system/sync_file_system_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 // Run the given join_callback when all the callbacks created by this runner | 32 // Run the given join_callback when all the callbacks created by this runner |
33 // are run, or may dispatch it earlier if we get an error in any of the sub | 33 // are run, or may dispatch it earlier if we get an error in any of the sub |
34 // callbacks. | 34 // callbacks. |
35 class SharedCallbackRunner | 35 class SharedCallbackRunner |
36 : public base::RefCounted<SharedCallbackRunner>, | 36 : public base::RefCounted<SharedCallbackRunner>, |
37 public base::NonThreadSafe { | 37 public base::NonThreadSafe { |
38 public: | 38 public: |
39 SharedCallbackRunner(const SyncStatusCallback& join_callback) | 39 explicit SharedCallbackRunner(const SyncStatusCallback& join_callback) |
40 : join_callback_(join_callback), | 40 : join_callback_(join_callback), |
41 num_shared_callbacks_(0) {} | 41 num_shared_callbacks_(0) {} |
42 | 42 |
| 43 SyncStatusCallback CreateCallback() { |
| 44 ++num_shared_callbacks_; |
| 45 return base::Bind(&SharedCallbackRunner::Done, this); |
| 46 } |
| 47 |
43 template <typename R> | 48 template <typename R> |
44 base::Callback<void(SyncStatusCode, const R& in)> | 49 base::Callback<void(SyncStatusCode, const R& in)> |
45 CreateAssignAndRunCallback(R* out) { | 50 CreateAssignAndRunCallback(R* out) { |
46 ++num_shared_callbacks_; | 51 ++num_shared_callbacks_; |
47 return base::Bind(&SharedCallbackRunner::AssignAndRun<R>, this, out); | 52 return base::Bind(&SharedCallbackRunner::AssignAndRun<R>, this, out); |
48 } | 53 } |
49 | 54 |
50 private: | 55 private: |
51 virtual ~SharedCallbackRunner() {} | 56 virtual ~SharedCallbackRunner() {} |
52 friend class base::RefCounted<SharedCallbackRunner>; | 57 friend class base::RefCounted<SharedCallbackRunner>; |
53 | 58 |
54 template <typename R> | 59 template <typename R> |
55 void AssignAndRun(R* out, SyncStatusCode status, const R& in) { | 60 void AssignAndRun(R* out, SyncStatusCode status, const R& in) { |
56 DCHECK(out); | 61 DCHECK(out); |
57 DCHECK_GT(num_shared_callbacks_, 0); | 62 DCHECK_GT(num_shared_callbacks_, 0); |
58 if (join_callback_.is_null()) | 63 if (join_callback_.is_null()) |
59 return; | 64 return; |
60 *out = in; | 65 *out = in; |
| 66 Done(status); |
| 67 } |
| 68 |
| 69 void Done(SyncStatusCode status) { |
61 if (status != fileapi::SYNC_STATUS_OK) { | 70 if (status != fileapi::SYNC_STATUS_OK) { |
62 join_callback_.Run(status); | 71 join_callback_.Run(status); |
63 join_callback_.Reset(); | 72 join_callback_.Reset(); |
64 return; | 73 return; |
65 } | 74 } |
66 if (--num_shared_callbacks_ > 0) | 75 if (--num_shared_callbacks_ > 0) |
67 return; | 76 return; |
68 join_callback_.Run(status); | 77 join_callback_.Run(status); |
69 join_callback_.Reset(); | 78 join_callback_.Reset(); |
70 } | 79 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 const SyncStatusCallback& callback) { | 130 const SyncStatusCallback& callback) { |
122 DCHECK(local_file_service_); | 131 DCHECK(local_file_service_); |
123 DCHECK(app_origin == app_origin.GetOrigin()); | 132 DCHECK(app_origin == app_origin.GetOrigin()); |
124 | 133 |
125 bool inserted = initialized_app_origins_.insert(app_origin).second; | 134 bool inserted = initialized_app_origins_.insert(app_origin).second; |
126 if (!inserted) { | 135 if (!inserted) { |
127 callback.Run(fileapi::SYNC_STATUS_OK); | 136 callback.Run(fileapi::SYNC_STATUS_OK); |
128 return; | 137 return; |
129 } | 138 } |
130 | 139 |
| 140 scoped_refptr<SharedCallbackRunner> callback_runner( |
| 141 new SharedCallbackRunner(callback)); |
131 local_file_service_->MaybeInitializeFileSystemContext( | 142 local_file_service_->MaybeInitializeFileSystemContext( |
132 app_origin, service_name, file_system_context, callback); | 143 app_origin, service_name, file_system_context, |
| 144 callback_runner->CreateCallback()); |
133 | 145 |
134 if (remote_file_service_) { | 146 if (remote_file_service_) { |
135 // TODO(tzik): Handle errors in the completion callback. | |
136 remote_file_service_->RegisterOriginForTrackingChanges( | 147 remote_file_service_->RegisterOriginForTrackingChanges( |
137 app_origin, fileapi::SyncStatusCallback()); | 148 app_origin, callback_runner->CreateCallback()); |
138 } | 149 } |
139 } | 150 } |
140 | 151 |
141 void SyncFileSystemService::GetConflictFiles( | 152 void SyncFileSystemService::GetConflictFiles( |
142 const GURL& app_origin, | 153 const GURL& app_origin, |
143 const std::string& service_name, | 154 const std::string& service_name, |
144 const fileapi::SyncFileSetCallback& callback) { | 155 const fileapi::SyncFileSetCallback& callback) { |
145 DCHECK(remote_file_service_); | 156 DCHECK(remote_file_service_); |
146 DCHECK(app_origin == app_origin.GetOrigin()); | 157 DCHECK(app_origin == app_origin.GetOrigin()); |
147 | 158 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 | 285 |
275 if (mock_remote_file_service_) | 286 if (mock_remote_file_service_) |
276 remote_file_service = mock_remote_file_service_.Pass(); | 287 remote_file_service = mock_remote_file_service_.Pass(); |
277 | 288 |
278 service->Initialize(local_file_service.Pass(), | 289 service->Initialize(local_file_service.Pass(), |
279 remote_file_service.Pass()); | 290 remote_file_service.Pass()); |
280 return service; | 291 return service; |
281 } | 292 } |
282 | 293 |
283 } // namespace sync_file_system | 294 } // namespace sync_file_system |
OLD | NEW |