Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(626)

Side by Side Diff: chrome/browser/policy/cloud/remote_commands_invalidator.cc

Issue 1094493003: Initial RemoteCommandsInvalidator (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: fixes addressing #5 Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.h"
6
7 #include <string>
8
9 #include "base/bind.h"
bartfab (slow) 2015/05/15 16:00:30 Nit: Not used.
binjin 2015/05/15 16:12:41 Done.
10 #include "base/location.h"
bartfab (slow) 2015/05/15 16:00:30 Nit: Not used.
binjin 2015/05/15 16:12:41 Done.
11 #include "base/logging.h"
12 #include "components/invalidation/invalidation.h"
13 #include "components/invalidation/invalidation_service.h"
14 #include "components/invalidation/invalidation_util.h"
15 #include "components/invalidation/invalidator_state.h"
16 #include "components/invalidation/object_id_invalidation_map.h"
17 #include "components/invalidation/single_object_invalidation_set.h"
18
19 namespace policy {
20
21 RemoteCommandsInvalidator::RemoteCommandsInvalidator() {
22 }
23
24 RemoteCommandsInvalidator::~RemoteCommandsInvalidator() {
25 DCHECK_EQ(SHUT_DOWN, state_);
26 }
27
28 void RemoteCommandsInvalidator::Initialize(
29 invalidation::InvalidationService* invalidation_service) {
30 DCHECK_EQ(SHUT_DOWN, state_);
31 DCHECK(thread_checker_.CalledOnValidThread());
32
33 DCHECK(invalidation_service);
34 invalidation_service_ = invalidation_service;
35
36 state_ = STOPPED;
37 OnInitialize();
38 }
39
40 void RemoteCommandsInvalidator::Shutdown() {
41 DCHECK_NE(SHUT_DOWN, state_);
42 DCHECK(thread_checker_.CalledOnValidThread());
43
44 Stop();
45
46 state_ = SHUT_DOWN;
47 OnShutdown();
48 }
49
50 void RemoteCommandsInvalidator::Start() {
51 DCHECK_EQ(STOPPED, state_);
52 DCHECK(thread_checker_.CalledOnValidThread());
53 state_ = STARTED;
54
55 OnStart();
56 }
57
58 void RemoteCommandsInvalidator::Stop() {
59 DCHECK_NE(SHUT_DOWN, state_);
60 DCHECK(thread_checker_.CalledOnValidThread());
61
62 if (state_ == STARTED) {
63 Unregister();
64 state_ = STOPPED;
65
66 OnStop();
67 }
68 }
69
70 void RemoteCommandsInvalidator::OnInvalidatorStateChange(
71 syncer::InvalidatorState state) {
72 DCHECK_EQ(STARTED, state_);
73 DCHECK(thread_checker_.CalledOnValidThread());
74
75 invalidation_service_enabled_ = state == syncer::INVALIDATIONS_ENABLED;
76 UpdateInvalidationsEnabled();
77 }
78
79 void RemoteCommandsInvalidator::OnIncomingInvalidation(
80 const syncer::ObjectIdInvalidationMap& invalidation_map) {
81 DCHECK_EQ(STARTED, state_);
82 DCHECK(thread_checker_.CalledOnValidThread());
83
84 if (!invalidation_service_enabled_)
85 LOG(WARNING) << "Unexpected invalidation received.";
86
87 const syncer::SingleObjectInvalidationSet& list =
88 invalidation_map.ForObject(object_id_);
89 if (list.IsEmpty()) {
90 NOTREACHED();
91 return;
92 }
93
94 // Acknowledge all invalidations.
95 for (const auto& it : list)
96 it.Acknowledge();
97
98 DoRemoteCommandsFetch();
99 }
100
101 std::string RemoteCommandsInvalidator::GetOwnerName() const {
102 return "RemoteCommands";
103 }
104
105 void RemoteCommandsInvalidator::ReloadPolicyData(
106 const enterprise_management::PolicyData* policy) {
107 DCHECK(thread_checker_.CalledOnValidThread());
108
109 if (state_ != STARTED)
110 return;
111
112 // Create the ObjectId based on the policy data.
113 // If the policy does not specify an the ObjectId, then unregister.
114 if (!policy || !policy->has_command_invalidation_source() ||
115 !policy->has_command_invalidation_name()) {
116 Unregister();
117 return;
118 }
119 const invalidation::ObjectId object_id(policy->command_invalidation_source(),
120 policy->command_invalidation_name());
121
122 // If the policy object id in the policy data is different from the currently
123 // registered object id, update the object registration.
124 if (!is_registered_ || !(object_id == object_id_))
125 Register(object_id);
126 }
127
128 void RemoteCommandsInvalidator::Register(
129 const invalidation::ObjectId& object_id) {
130 // Register this handler with the invalidation service if needed.
131 if (!is_registered_) {
132 OnInvalidatorStateChange(invalidation_service_->GetInvalidatorState());
133 invalidation_service_->RegisterInvalidationHandler(this);
134 is_registered_ = true;
135 }
136
137 object_id_ = object_id;
138 UpdateInvalidationsEnabled();
139
140 // Update registration with the invalidation service.
141 syncer::ObjectIdSet ids;
142 ids.insert(object_id);
143 invalidation_service_->UpdateRegisteredInvalidationIds(this, ids);
144 }
145
146 void RemoteCommandsInvalidator::Unregister() {
147 if (is_registered_) {
148 invalidation_service_->UpdateRegisteredInvalidationIds(
149 this, syncer::ObjectIdSet());
150 invalidation_service_->UnregisterInvalidationHandler(this);
151 is_registered_ = false;
152 UpdateInvalidationsEnabled();
153 }
154 }
155
156 void RemoteCommandsInvalidator::UpdateInvalidationsEnabled() {
157 invalidations_enabled_ = invalidation_service_enabled_ && is_registered_;
158 }
159
160 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698