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