OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifdef CHROME_PERSONALIZATION |
| 6 |
| 7 #ifndef CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_WORKER_H_ |
| 8 #define CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_WORKER_H_ |
| 9 |
| 10 #include "base/lock.h" |
| 11 #include "base/task.h" |
| 12 #include "base/waitable_event.h" |
| 13 #include "chrome/browser/sync/engine/syncapi.h" |
| 14 |
| 15 class MessageLoop; |
| 16 |
| 17 namespace browser_sync { |
| 18 |
| 19 // A ModelSafeWorker for bookmarks that accepts work requests from the syncapi |
| 20 // that need to be fulfilled from the MessageLoop home to the BookmarkModel |
| 21 // (this is typically the "main" UI thread). |
| 22 // |
| 23 // Lifetime note: Instances of this class will generally be owned by the |
| 24 // SyncerThread. When the SyncerThread _object_ is destroyed, the |
| 25 // BookmarkModelWorker will be destroyed. The SyncerThread object is destroyed |
| 26 // after the actual syncer pthread has exited. |
| 27 class BookmarkModelWorker |
| 28 : public sync_api::ModelSafeWorkerInterface { |
| 29 public: |
| 30 explicit BookmarkModelWorker(MessageLoop* bookmark_model_loop) |
| 31 : state_(WORKING), |
| 32 pending_work_(NULL), |
| 33 syncapi_has_shutdown_(false), |
| 34 bookmark_model_loop_(bookmark_model_loop), |
| 35 syncapi_event_(false, false) { |
| 36 } |
| 37 virtual ~BookmarkModelWorker(); |
| 38 |
| 39 // A simple task to signal a waitable event after calling DoWork on a visitor. |
| 40 class CallDoWorkAndSignalTask : public Task { |
| 41 public: |
| 42 CallDoWorkAndSignalTask(ModelSafeWorkerInterface::Visitor* visitor, |
| 43 base::WaitableEvent* work_done, |
| 44 BookmarkModelWorker* scheduler) |
| 45 : visitor_(visitor), work_done_(work_done), scheduler_(scheduler) { |
| 46 } |
| 47 virtual ~CallDoWorkAndSignalTask() { } |
| 48 |
| 49 // Task implementation. |
| 50 virtual void Run(); |
| 51 |
| 52 private: |
| 53 // Task data - a visitor that knows how to DoWork, and a waitable event |
| 54 // to signal after the work has been done. |
| 55 ModelSafeWorkerInterface::Visitor* visitor_; |
| 56 base::WaitableEvent* work_done_; |
| 57 |
| 58 // The BookmarkModelWorker responsible for scheduling us. |
| 59 BookmarkModelWorker* const scheduler_; |
| 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(CallDoWorkAndSignalTask); |
| 62 }; |
| 63 |
| 64 // Called by the UI thread on shutdown of the sync service. Blocks until |
| 65 // the BookmarkModelWorker has safely met termination conditions, namely that |
| 66 // no task scheduled by CallDoWorkFromModelSafeThreadAndWait remains un- |
| 67 // processed and that syncapi will not schedule any further work for us to do. |
| 68 void Stop(); |
| 69 |
| 70 // ModelSafeWorkerInterface implementation. Called on syncapi SyncerThread. |
| 71 virtual void CallDoWorkFromModelSafeThreadAndWait( |
| 72 ModelSafeWorkerInterface::Visitor* visitor); |
| 73 |
| 74 // Upon receiving this idempotent call, the ModelSafeWorkerInterface can |
| 75 // assume no work will ever be scheduled again from now on. If it has any work |
| 76 // that it has not yet completed, it must make sure to run it as soon as |
| 77 // possible as the Syncer is trying to shut down. Called from the CoreThread. |
| 78 void OnSyncerShutdownComplete(); |
| 79 |
| 80 // Callback from |pending_work_| to notify us that it has been run. |
| 81 // Called on |bookmark_model_loop_|. |
| 82 void OnTaskCompleted() { pending_work_ = NULL; } |
| 83 |
| 84 private: |
| 85 // The life-cycle of a BookmarkModelWorker in three states. |
| 86 enum State { |
| 87 // We hit the ground running in this state and remain until |
| 88 // the UI loop calls Stop(). |
| 89 WORKING, |
| 90 // Stop() sequence has been initiated, but we have not received word that |
| 91 // the SyncerThread has terminated and doesn't need us anymore. Since the |
| 92 // UI MessageLoop is not running at this point, we manually process any |
| 93 // last pending_task_ that the Syncer throws at us, effectively dedicating |
| 94 // the UI thread to terminating the Syncer. |
| 95 RUNNING_MANUAL_SHUTDOWN_PUMP, |
| 96 // We have come to a complete stop, no scheduled work remains, and no work |
| 97 // will be scheduled from now until our destruction. |
| 98 STOPPED, |
| 99 }; |
| 100 |
| 101 // This is set by the UI thread, but is not explicitly thread safe, so only |
| 102 // read this value from other threads when you know it is absolutely safe (e.g |
| 103 // there is _no_ way we can be in CallDoWork with state_ = STOPPED, so it is |
| 104 // safe to read / compare in this case). |
| 105 State state_; |
| 106 |
| 107 // We keep a reference to any task we have scheduled so we can gracefully |
| 108 // force them to run if the syncer is trying to shutdown. |
| 109 Task* pending_work_; |
| 110 Lock pending_work_lock_; |
| 111 |
| 112 // Set by the SyncCoreThread when Syncapi shutdown has completed and the |
| 113 // SyncerThread has terminated, so no more work will be scheduled. Read by |
| 114 // the UI thread in Stop(). |
| 115 bool syncapi_has_shutdown_; |
| 116 |
| 117 // The BookmarkModel's home-sweet-home MessageLoop. |
| 118 MessageLoop* const bookmark_model_loop_; |
| 119 |
| 120 // Used as a barrier at shutdown to ensure the SyncerThread terminates before |
| 121 // we allow the UI thread to return from Stop(). This gets signalled whenever |
| 122 // one of two events occur: a new pending_work_ task was scheduled, or the |
| 123 // SyncerThread has terminated. We only care about (1) when we are in Stop(), |
| 124 // because we have to manually Run() the task. |
| 125 base::WaitableEvent syncapi_event_; |
| 126 |
| 127 DISALLOW_COPY_AND_ASSIGN(BookmarkModelWorker); |
| 128 }; |
| 129 |
| 130 } // namespace browser_sync |
| 131 |
| 132 #endif // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_WORKER_H_ |
| 133 |
| 134 #endif // CHROME_PERSONALIZATION |
OLD | NEW |