OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 #include "chrome/browser/policy/cloud/remote_commands_invalidator_base.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/location.h" | |
11 #include "base/logging.h" | |
12 #include "base/sequenced_task_runner.h" | |
13 #include "components/invalidation/invalidation.h" | |
14 #include "components/invalidation/invalidation_service.h" | |
15 #include "components/invalidation/invalidation_util.h" | |
16 #include "components/invalidation/invalidator_state.h" | |
17 #include "components/invalidation/object_id_invalidation_map.h" | |
18 #include "components/invalidation/single_object_invalidation_set.h" | |
19 | |
20 namespace policy { | |
21 | |
22 RemoteCommandsInvalidatorBase::RemoteCommandsInvalidatorBase( | |
23 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | |
24 : task_runner_(task_runner), weak_factory_(this) { | |
25 DCHECK(task_runner); | |
26 } | |
27 | |
28 RemoteCommandsInvalidatorBase::~RemoteCommandsInvalidatorBase() { | |
29 DCHECK_EQ(SHUT_DOWN, state_); | |
30 } | |
31 | |
32 void RemoteCommandsInvalidatorBase::Initialize( | |
33 invalidation::InvalidationService* invalidation_service) { | |
34 DCHECK_EQ(SHUT_DOWN, state_); | |
35 DCHECK(thread_checker_.CalledOnValidThread()); | |
36 | |
37 DCHECK(invalidation_service); | |
38 invalidation_service_ = invalidation_service; | |
39 | |
40 state_ = STOPPED; | |
41 OnInitialize(); | |
42 } | |
43 | |
44 void RemoteCommandsInvalidatorBase::Shutdown() { | |
45 DCHECK_NE(SHUT_DOWN, state_); | |
46 DCHECK(thread_checker_.CalledOnValidThread()); | |
47 | |
48 Stop(); | |
49 | |
50 state_ = SHUT_DOWN; | |
51 weak_factory_.InvalidateWeakPtrs(); | |
52 OnShutdown(); | |
53 } | |
54 | |
55 void RemoteCommandsInvalidatorBase::Start() { | |
56 DCHECK_EQ(STOPPED, state_); | |
57 DCHECK(thread_checker_.CalledOnValidThread()); | |
58 state_ = STARTED; | |
59 | |
60 OnStart(); | |
61 } | |
62 | |
63 void RemoteCommandsInvalidatorBase::Stop() { | |
64 DCHECK_NE(SHUT_DOWN, state_); | |
65 DCHECK(thread_checker_.CalledOnValidThread()); | |
66 | |
67 if (state_ == STARTED) { | |
68 Unregister(); | |
69 state_ = STOPPED; | |
70 | |
71 OnStop(); | |
72 } | |
73 } | |
74 | |
75 void RemoteCommandsInvalidatorBase::OnInvalidatorStateChange( | |
76 syncer::InvalidatorState state) { | |
77 DCHECK_EQ(STARTED, state_); | |
78 DCHECK(thread_checker_.CalledOnValidThread()); | |
79 | |
80 invalidation_service_enabled_ = state == syncer::INVALIDATIONS_ENABLED; | |
81 UpdateInvalidationsEnabled(); | |
82 } | |
83 | |
84 void RemoteCommandsInvalidatorBase::OnIncomingInvalidation( | |
85 const syncer::ObjectIdInvalidationMap& invalidation_map) { | |
86 DCHECK_EQ(STARTED, state_); | |
87 DCHECK(thread_checker_.CalledOnValidThread()); | |
88 | |
89 if (!invalidation_service_enabled_) | |
90 LOG(WARNING) << "Unexpected invalidation received."; | |
91 | |
92 const syncer::SingleObjectInvalidationSet& list = | |
93 invalidation_map.ForObject(object_id_); | |
94 if (list.IsEmpty()) { | |
95 NOTREACHED(); | |
96 return; | |
97 } | |
98 | |
99 // Acknowledge all invalidations. | |
100 for (const auto& it : list) | |
101 it.Acknowledge(); | |
102 | |
103 task_runner_->PostTask( | |
bartfab (slow)
2015/05/15 15:37:12
As discussed offline, you should be able to do thi
binjin
2015/05/15 15:46:46
Done.
| |
104 FROM_HERE, | |
105 base::Bind(&RemoteCommandsInvalidatorBase::DoRemoteCommandsFetch, | |
106 weak_factory_.GetWeakPtr())); | |
107 } | |
108 | |
109 std::string RemoteCommandsInvalidatorBase::GetOwnerName() const { | |
110 return "RemoteCommands"; | |
111 } | |
112 | |
113 void RemoteCommandsInvalidatorBase::ReloadPolicyData( | |
114 const enterprise_management::PolicyData* policy) { | |
115 DCHECK(thread_checker_.CalledOnValidThread()); | |
116 | |
117 if (state_ != STARTED) | |
118 return; | |
119 | |
120 // Create the ObjectId based on the policy data. | |
121 // If the policy does not specify an the ObjectId, then unregister. | |
122 if (!policy || !policy->has_command_invalidation_source() || | |
123 !policy->has_command_invalidation_name()) { | |
124 Unregister(); | |
125 return; | |
126 } | |
127 const invalidation::ObjectId object_id(policy->command_invalidation_source(), | |
128 policy->command_invalidation_name()); | |
129 | |
130 // If the policy object id in the policy data is different from the currently | |
131 // registered object id, update the object registration. | |
132 if (!is_registered_ || !(object_id == object_id_)) | |
133 Register(object_id); | |
134 } | |
135 | |
136 void RemoteCommandsInvalidatorBase::Register( | |
137 const invalidation::ObjectId& object_id) { | |
138 // Register this handler with the invalidation service if needed. | |
139 if (!is_registered_) { | |
140 OnInvalidatorStateChange(invalidation_service_->GetInvalidatorState()); | |
141 invalidation_service_->RegisterInvalidationHandler(this); | |
142 is_registered_ = true; | |
143 } | |
144 | |
145 object_id_ = object_id; | |
146 UpdateInvalidationsEnabled(); | |
147 | |
148 // Update registration with the invalidation service. | |
149 syncer::ObjectIdSet ids; | |
150 ids.insert(object_id); | |
151 invalidation_service_->UpdateRegisteredInvalidationIds(this, ids); | |
152 } | |
153 | |
154 void RemoteCommandsInvalidatorBase::Unregister() { | |
155 if (is_registered_) { | |
156 invalidation_service_->UpdateRegisteredInvalidationIds( | |
157 this, syncer::ObjectIdSet()); | |
158 invalidation_service_->UnregisterInvalidationHandler(this); | |
159 is_registered_ = false; | |
160 UpdateInvalidationsEnabled(); | |
161 } | |
162 } | |
163 | |
164 void RemoteCommandsInvalidatorBase::UpdateInvalidationsEnabled() { | |
165 invalidations_enabled_ = invalidation_service_enabled_ && is_registered_; | |
166 } | |
167 | |
168 } // namespace policy | |
OLD | NEW |