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
3 // found in the LICENSE file.
5 #include "sync/internal_api/public/engine/model_safe_worker.h"
8 #include "base/json/json_writer.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/values.h"
14 base::DictionaryValue* ModelSafeRoutingInfoToValue(
15 const ModelSafeRoutingInfo& routing_info) {
16 base::DictionaryValue* dict = new base::DictionaryValue();
17 for (ModelSafeRoutingInfo::const_iterator it = routing_info.begin();
18 it != routing_info.end(); ++it) {
19 dict->SetString(ModelTypeToString(it->first),
20 ModelSafeGroupToString(it->second));
25 std::string ModelSafeRoutingInfoToString(
26 const ModelSafeRoutingInfo& routing_info) {
27 scoped_ptr<base::DictionaryValue> dict(
28 ModelSafeRoutingInfoToValue(routing_info));
30 base::JSONWriter::Write(dict.get(), &json);
34 ModelTypeSet GetRoutingInfoTypes(const ModelSafeRoutingInfo& routing_info) {
36 for (ModelSafeRoutingInfo::const_iterator it = routing_info.begin();
37 it != routing_info.end(); ++it) {
43 ModelSafeGroup GetGroupForModelType(const ModelType type,
44 const ModelSafeRoutingInfo& routes) {
45 ModelSafeRoutingInfo::const_iterator it = routes.find(type);
46 if (it == routes.end()) {
47 if (type != UNSPECIFIED && type != TOP_LEVEL_FOLDER)
48 DVLOG(1) << "Entry does not belong to active ModelSafeGroup!";
54 std::string ModelSafeGroupToString(ModelSafeGroup group) {
63 return "GROUP_HISTORY";
65 return "GROUP_PASSIVE";
67 return "GROUP_PASSWORD";
74 ModelSafeWorker::ModelSafeWorker(WorkerLoopDestructionObserver* observer)
76 work_done_or_stopped_(false, false),
81 ModelSafeWorker::~ModelSafeWorker() {}
83 void ModelSafeWorker::RequestStop() {
84 base::AutoLock al(stopped_lock_);
86 // Set stop flag but don't signal work_done_or_stopped_ to unblock sync loop
87 // because the worker may be working and depending on sync command object
88 // living on sync thread. his prevents any *further* tasks from being posted
89 // to worker threads (see DoWorkAndWaitUntilDone below), but note that one
90 // may already be posted.
94 SyncerError ModelSafeWorker::DoWorkAndWaitUntilDone(const WorkCallback& work) {
96 base::AutoLock al(stopped_lock_);
98 return CANNOT_DO_WORK;
100 CHECK(!work_done_or_stopped_.IsSignaled());
103 return DoWorkAndWaitUntilDoneImpl(work);
106 bool ModelSafeWorker::IsStopped() {
107 base::AutoLock al(stopped_lock_);
111 void ModelSafeWorker::WillDestroyCurrentMessageLoop() {
113 base::AutoLock al(stopped_lock_);
116 // Must signal to unblock syncer if it's waiting for a posted task to
117 // finish. At this point, all pending tasks posted to the loop have been
118 // destroyed (see MessageLoop::~MessageLoop). So syncer will be blocked
119 // indefinitely without signaling here.
120 work_done_or_stopped_.Signal();
122 DVLOG(1) << ModelSafeGroupToString(GetModelSafeGroup())
123 << " worker stops on destruction of its working thread.";
127 base::AutoLock l(working_loop_lock_);
128 working_loop_ = NULL;
132 observer_->OnWorkerLoopDestroyed(GetModelSafeGroup());
135 void ModelSafeWorker::SetWorkingLoopToCurrent() {
136 base::Callback<void(ModelSafeGroup)> unregister_done_callback;
139 base::AutoLock l(working_loop_lock_);
140 DCHECK(!working_loop_);
142 if (unregister_done_callback_.is_null()) {
143 // Expected case - UnregisterForLoopDestruction hasn't been called yet.
144 base::MessageLoop::current()->AddDestructionObserver(this);
145 working_loop_ = base::MessageLoop::current();
147 // Rare case which is possible when the model type thread remains
148 // blocked for the entire session and UnregisterForLoopDestruction ends
149 // up being called before this method. This method is posted unlike
150 // UnregisterForLoopDestruction - that's why they can end up being called
152 // In this case we skip the destruction observer registration
153 // and just invoke the callback stored at UnregisterForLoopDestruction.
155 unregister_done_callback = unregister_done_callback_;
156 unregister_done_callback_.Reset();
160 if (!unregister_done_callback.is_null()) {
161 unregister_done_callback.Run(GetModelSafeGroup());
165 void ModelSafeWorker::UnregisterForLoopDestruction(
166 base::Callback<void(ModelSafeGroup)> unregister_done_callback) {
167 base::AutoLock l(working_loop_lock_);
168 if (working_loop_ != NULL) {
169 // Normal case - observer registration has been already done.
170 // Delegate to the sync thread to do the actual unregistration in
171 // UnregisterForLoopDestructionAsync.
172 DCHECK_NE(base::MessageLoop::current(), working_loop_);
173 working_loop_->PostTask(
175 base::Bind(&ModelSafeWorker::UnregisterForLoopDestructionAsync,
177 unregister_done_callback));
179 // The working loop is still unknown, probably because the model type
180 // thread is blocked. Store the callback to be called from
181 // SetWorkingLoopToCurrent.
182 unregister_done_callback_ = unregister_done_callback;
186 void ModelSafeWorker::UnregisterForLoopDestructionAsync(
187 base::Callback<void(ModelSafeGroup)> unregister_done_callback) {
189 base::AutoLock l(working_loop_lock_);
192 DCHECK_EQ(base::MessageLoop::current(), working_loop_);
196 base::MessageLoop::current()->RemoveDestructionObserver(this);
197 unregister_done_callback.Run(GetModelSafeGroup());
200 } // namespace syncer