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 CHROME_BROWSER_SYNC_GLUE_UI_MODEL_WORKER_H_ | 5 #ifndef CHROME_BROWSER_SYNC_GLUE_UI_MODEL_WORKER_H_ |
6 #define CHROME_BROWSER_SYNC_GLUE_UI_MODEL_WORKER_H_ | 6 #define CHROME_BROWSER_SYNC_GLUE_UI_MODEL_WORKER_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/synchronization/condition_variable.h" | 10 #include "base/synchronization/condition_variable.h" |
11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
12 #include "sync/internal_api/public/engine/model_safe_worker.h" | 12 #include "sync/internal_api/public/engine/model_safe_worker.h" |
13 #include "sync/internal_api/public/util/unrecoverable_error_info.h" | 13 #include "sync/internal_api/public/util/unrecoverable_error_info.h" |
14 | 14 |
15 namespace browser_sync { | 15 namespace browser_sync { |
16 | 16 |
17 // A syncer::ModelSafeWorker for UI models (e.g. bookmarks) that | 17 // A syncer::ModelSafeWorker for UI models (e.g. bookmarks) that |
18 // accepts work requests from the syncapi that need to be fulfilled | 18 // accepts work requests from the syncapi that need to be fulfilled |
19 // from the MessageLoop home to the native model. | 19 // from the MessageLoop home to the native model. |
20 // | 20 // |
21 // Lifetime note: Instances of this class will generally be owned by the | 21 // Lifetime note: Instances of this class will generally be owned by the |
22 // SyncerThread. When the SyncerThread _object_ is destroyed, the | 22 // SyncerThread. When the SyncerThread _object_ is destroyed, the |
23 // UIModelWorker will be destroyed. The SyncerThread object is destroyed | 23 // UIModelWorker will be destroyed. The SyncerThread object is destroyed |
24 // after the actual syncer pthread has exited. | 24 // after the actual syncer pthread has exited. |
25 class UIModelWorker : public syncer::ModelSafeWorker { | 25 class UIModelWorker : public syncer::ModelSafeWorker { |
26 public: | 26 public: |
27 UIModelWorker(); | 27 explicit UIModelWorker(syncer::WorkerLoopDestructionObserver* observer); |
28 | 28 |
29 // Called by the UI thread on shutdown of the sync service. Blocks until | 29 // Called by the UI thread on shutdown of the sync service. Blocks until |
30 // the UIModelWorker has safely met termination conditions, namely that | 30 // the UIModelWorker has safely met termination conditions, namely that |
31 // no task scheduled by CallDoWorkFromModelSafeThreadAndWait remains un- | 31 // no task scheduled by CallDoWorkFromModelSafeThreadAndWait remains un- |
32 // processed and that syncapi will not schedule any further work for us to do. | 32 // processed and that syncapi will not schedule any further work for us to do. |
33 void Stop(); | 33 void Stop(); |
34 | 34 |
35 // syncer::ModelSafeWorker implementation. Called on syncapi SyncerThread. | 35 // syncer::ModelSafeWorker implementation. Called on syncapi SyncerThread. |
36 virtual syncer::SyncerError DoWorkAndWaitUntilDone( | 36 virtual void RegisterForLoopDestruction() OVERRIDE; |
37 const syncer::WorkCallback& work) OVERRIDE; | |
38 virtual syncer::ModelSafeGroup GetModelSafeGroup() OVERRIDE; | 37 virtual syncer::ModelSafeGroup GetModelSafeGroup() OVERRIDE; |
39 | 38 |
40 // Upon receiving this idempotent call, the syncer::ModelSafeWorker can | 39 // Upon receiving this idempotent call, the syncer::ModelSafeWorker can |
41 // assume no work will ever be scheduled again from now on. If it has any work | 40 // assume no work will ever be scheduled again from now on. If it has any work |
42 // that it has not yet completed, it must make sure to run it as soon as | 41 // that it has not yet completed, it must make sure to run it as soon as |
43 // possible as the Syncer is trying to shut down. Called from the CoreThread. | 42 // possible as the Syncer is trying to shut down. Called from the CoreThread. |
44 void OnSyncerShutdownComplete(); | 43 void OnSyncerShutdownComplete(); |
45 | 44 |
46 // Callback from |pending_work_| to notify us that it has been run. | 45 // Callback from |pending_work_| to notify us that it has been run. |
47 // Called on ui loop. | 46 // Called on ui loop. |
48 void OnTaskCompleted() { pending_work_.Reset(); } | 47 void OnTaskCompleted() { pending_work_.Reset(); } |
49 | 48 |
| 49 protected: |
| 50 virtual syncer::SyncerError DoWorkAndWaitUntilDoneImpl( |
| 51 const syncer::WorkCallback& work) OVERRIDE; |
| 52 |
50 private: | 53 private: |
51 // The life-cycle of a UIModelWorker in three states. | 54 // The life-cycle of a UIModelWorker in three states. |
52 enum State { | 55 enum State { |
53 // We hit the ground running in this state and remain until | 56 // We hit the ground running in this state and remain until |
54 // the UI loop calls Stop(). | 57 // the UI loop calls Stop(). |
55 WORKING, | 58 WORKING, |
56 // Stop() sequence has been initiated, but we have not received word that | 59 // Stop() sequence has been initiated, but we have not received word that |
57 // the SyncerThread has terminated and doesn't need us anymore. Since the | 60 // the SyncerThread has terminated and doesn't need us anymore. Since the |
58 // UI MessageLoop is not running at this point, we manually process any | 61 // UI MessageLoop is not running at this point, we manually process any |
59 // last pending_task_ that the Syncer throws at us, effectively dedicating | 62 // last pending_task_ that the Syncer throws at us, effectively dedicating |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 // SyncerThread has terminated. We only care about (1) when we are in Stop(), | 96 // SyncerThread has terminated. We only care about (1) when we are in Stop(), |
94 // because we have to manually Run() the task. | 97 // because we have to manually Run() the task. |
95 base::ConditionVariable syncapi_event_; | 98 base::ConditionVariable syncapi_event_; |
96 | 99 |
97 DISALLOW_COPY_AND_ASSIGN(UIModelWorker); | 100 DISALLOW_COPY_AND_ASSIGN(UIModelWorker); |
98 }; | 101 }; |
99 | 102 |
100 } // namespace browser_sync | 103 } // namespace browser_sync |
101 | 104 |
102 #endif // CHROME_BROWSER_SYNC_GLUE_UI_MODEL_WORKER_H_ | 105 #endif // CHROME_BROWSER_SYNC_GLUE_UI_MODEL_WORKER_H_ |
OLD | NEW |