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 UIModelWorker(syncer::WorkerObserver* 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 DoWorkAndWaitUntilDone( |
| 51 const syncer::WorkCallback& work, |
| 52 base::WaitableEvent* work_done) OVERRIDE; |
| 53 |
50 private: | 54 private: |
51 // The life-cycle of a UIModelWorker in three states. | 55 // The life-cycle of a UIModelWorker in three states. |
52 enum State { | 56 enum State { |
53 // We hit the ground running in this state and remain until | 57 // We hit the ground running in this state and remain until |
54 // the UI loop calls Stop(). | 58 // the UI loop calls Stop(). |
55 WORKING, | 59 WORKING, |
56 // Stop() sequence has been initiated, but we have not received word that | 60 // 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 | 61 // 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 | 62 // UI MessageLoop is not running at this point, we manually process any |
59 // last pending_task_ that the Syncer throws at us, effectively dedicating | 63 // last pending_task_ that the Syncer throws at us, effectively dedicating |
60 // the UI thread to terminating the Syncer. | 64 // the UI thread to terminating the Syncer. |
61 RUNNING_MANUAL_SHUTDOWN_PUMP, | 65 RUNNING_MANUAL_SHUTDOWN_PUMP, |
62 // We have come to a complete stop, no scheduled work remains, and no work | 66 // We have come to a complete stop, no scheduled work remains, and no work |
63 // will be scheduled from now until our destruction. | 67 // will be scheduled from now until our destruction. |
64 STOPPED, | 68 STOPPED, |
65 }; | 69 }; |
66 | 70 |
67 virtual ~UIModelWorker(); | 71 virtual ~UIModelWorker(); |
68 | 72 |
| 73 void CallDoWorkAndSignalCallback(const syncer::WorkCallback& work, |
| 74 base::WaitableEvent* work_done, |
| 75 syncer::SyncerError* error_info); |
| 76 |
69 // This is set by the UI thread, but is not explicitly thread safe, so only | 77 // This is set by the UI thread, but is not explicitly thread safe, so only |
70 // read this value from other threads when you know it is absolutely safe. | 78 // read this value from other threads when you know it is absolutely safe. |
71 State state_; | 79 State state_; |
72 | 80 |
73 // We keep a reference to any task we have scheduled so we can gracefully | 81 // We keep a reference to any task we have scheduled so we can gracefully |
74 // force them to run if the syncer is trying to shutdown. | 82 // force them to run if the syncer is trying to shutdown. |
75 base::Closure pending_work_; | 83 base::Closure pending_work_; |
76 | 84 |
77 // Set by the SyncCoreThread when Syncapi shutdown has completed and the | 85 // Set by the SyncCoreThread when Syncapi shutdown has completed and the |
78 // SyncerThread has terminated, so no more work will be scheduled. Read by | 86 // SyncerThread has terminated, so no more work will be scheduled. Read by |
(...skipping 14 matching lines...) Expand all Loading... |
93 // SyncerThread has terminated. We only care about (1) when we are in Stop(), | 101 // SyncerThread has terminated. We only care about (1) when we are in Stop(), |
94 // because we have to manually Run() the task. | 102 // because we have to manually Run() the task. |
95 base::ConditionVariable syncapi_event_; | 103 base::ConditionVariable syncapi_event_; |
96 | 104 |
97 DISALLOW_COPY_AND_ASSIGN(UIModelWorker); | 105 DISALLOW_COPY_AND_ASSIGN(UIModelWorker); |
98 }; | 106 }; |
99 | 107 |
100 } // namespace browser_sync | 108 } // namespace browser_sync |
101 | 109 |
102 #endif // CHROME_BROWSER_SYNC_GLUE_UI_MODEL_WORKER_H_ | 110 #endif // CHROME_BROWSER_SYNC_GLUE_UI_MODEL_WORKER_H_ |
OLD | NEW |