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

Side by Side Diff: components/gcm_driver/instance_id/instance_id_impl.cc

Issue 2789123003: [GCM] Run InstanceIDImpl callbacks asynchronously (Closed)
Patch Set: Created 3 years, 8 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
« no previous file with comments | « components/gcm_driver/instance_id/instance_id_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/gcm_driver/instance_id/instance_id_impl.h" 5 #include "components/gcm_driver/instance_id/instance_id_impl.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <memory> 10 #include <memory>
11 11
12 #include "base/base64.h" 12 #include "base/base64.h"
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/message_loop/message_loop.h" 16 #include "base/message_loop/message_loop.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "base/threading/thread_task_runner_handle.h"
18 #include "components/gcm_driver/gcm_driver.h" 19 #include "components/gcm_driver/gcm_driver.h"
19 #include "crypto/random.h" 20 #include "crypto/random.h"
20 21
21 namespace instance_id { 22 namespace instance_id {
22 23
23 namespace { 24 namespace {
24 25
25 InstanceID::Result GCMClientResultToInstanceIDResult( 26 InstanceID::Result GCMClientResultToInstanceIDResult(
26 gcm::GCMClient::Result result) { 27 gcm::GCMClient::Result result) {
27 switch (result) { 28 switch (result) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 : InstanceID(app_id, gcm_driver), weak_ptr_factory_(this) { 61 : InstanceID(app_id, gcm_driver), weak_ptr_factory_(this) {
61 Handler()->GetInstanceIDData( 62 Handler()->GetInstanceIDData(
62 app_id, base::Bind(&InstanceIDImpl::GetInstanceIDDataCompleted, 63 app_id, base::Bind(&InstanceIDImpl::GetInstanceIDDataCompleted,
63 weak_ptr_factory_.GetWeakPtr())); 64 weak_ptr_factory_.GetWeakPtr()));
64 } 65 }
65 66
66 InstanceIDImpl::~InstanceIDImpl() { 67 InstanceIDImpl::~InstanceIDImpl() {
67 } 68 }
68 69
69 void InstanceIDImpl::GetID(const GetIDCallback& callback) { 70 void InstanceIDImpl::GetID(const GetIDCallback& callback) {
70 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { 71 RunWhenReady(base::Bind(&InstanceIDImpl::DoGetID,
71 delayed_task_controller_.AddTask( 72 weak_ptr_factory_.GetWeakPtr(), callback));
72 base::Bind(&InstanceIDImpl::DoGetID,
73 weak_ptr_factory_.GetWeakPtr(),
74 callback));
75 return;
76 }
77
78 DoGetID(callback);
79 } 73 }
80 74
81 void InstanceIDImpl::DoGetID(const GetIDCallback& callback) { 75 void InstanceIDImpl::DoGetID(const GetIDCallback& callback) {
82 EnsureIDGenerated(); 76 EnsureIDGenerated();
83 callback.Run(id_); 77 callback.Run(id_);
84 } 78 }
85 79
86 void InstanceIDImpl::GetCreationTime(const GetCreationTimeCallback& callback) { 80 void InstanceIDImpl::GetCreationTime(const GetCreationTimeCallback& callback) {
87 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { 81 RunWhenReady(base::Bind(&InstanceIDImpl::DoGetCreationTime,
88 delayed_task_controller_.AddTask( 82 weak_ptr_factory_.GetWeakPtr(), callback));
89 base::Bind(&InstanceIDImpl::DoGetCreationTime,
90 weak_ptr_factory_.GetWeakPtr(),
91 callback));
92 return;
93 }
94
95 DoGetCreationTime(callback);
96 } 83 }
97 84
98 void InstanceIDImpl::DoGetCreationTime( 85 void InstanceIDImpl::DoGetCreationTime(
99 const GetCreationTimeCallback& callback) { 86 const GetCreationTimeCallback& callback) {
100 callback.Run(creation_time_); 87 callback.Run(creation_time_);
101 } 88 }
102 89
103 void InstanceIDImpl::GetToken( 90 void InstanceIDImpl::GetToken(
104 const std::string& authorized_entity, 91 const std::string& authorized_entity,
105 const std::string& scope, 92 const std::string& scope,
106 const std::map<std::string, std::string>& options, 93 const std::map<std::string, std::string>& options,
107 const GetTokenCallback& callback) { 94 const GetTokenCallback& callback) {
108 DCHECK(!authorized_entity.empty()); 95 DCHECK(!authorized_entity.empty());
109 DCHECK(!scope.empty()); 96 DCHECK(!scope.empty());
110 97
111 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { 98 RunWhenReady(base::Bind(&InstanceIDImpl::DoGetToken,
112 delayed_task_controller_.AddTask( 99 weak_ptr_factory_.GetWeakPtr(), authorized_entity,
113 base::Bind(&InstanceIDImpl::DoGetToken, 100 scope, options, callback));
114 weak_ptr_factory_.GetWeakPtr(),
115 authorized_entity,
116 scope,
117 options,
118 callback));
119 return;
120 }
121
122 DoGetToken(authorized_entity, scope, options, callback);
123 } 101 }
124 102
125 void InstanceIDImpl::DoGetToken( 103 void InstanceIDImpl::DoGetToken(
126 const std::string& authorized_entity, 104 const std::string& authorized_entity,
127 const std::string& scope, 105 const std::string& scope,
128 const std::map<std::string, std::string>& options, 106 const std::map<std::string, std::string>& options,
129 const GetTokenCallback& callback) { 107 const GetTokenCallback& callback) {
130 EnsureIDGenerated(); 108 EnsureIDGenerated();
131 109
132 Handler()->GetToken(app_id(), authorized_entity, scope, options, 110 Handler()->GetToken(app_id(), authorized_entity, scope, options,
133 base::Bind(&InstanceIDImpl::OnGetTokenCompleted, 111 base::Bind(&InstanceIDImpl::OnGetTokenCompleted,
134 weak_ptr_factory_.GetWeakPtr(), callback)); 112 weak_ptr_factory_.GetWeakPtr(), callback));
135 } 113 }
136 114
137 void InstanceIDImpl::ValidateToken(const std::string& authorized_entity, 115 void InstanceIDImpl::ValidateToken(const std::string& authorized_entity,
138 const std::string& scope, 116 const std::string& scope,
139 const std::string& token, 117 const std::string& token,
140 const ValidateTokenCallback& callback) { 118 const ValidateTokenCallback& callback) {
141 DCHECK(!authorized_entity.empty()); 119 DCHECK(!authorized_entity.empty());
142 DCHECK(!scope.empty()); 120 DCHECK(!scope.empty());
143 DCHECK(!token.empty()); 121 DCHECK(!token.empty());
144 122
145 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { 123 RunWhenReady(base::Bind(&InstanceIDImpl::DoValidateToken,
146 delayed_task_controller_.AddTask(base::Bind( 124 weak_ptr_factory_.GetWeakPtr(), authorized_entity,
147 &InstanceIDImpl::DoValidateToken, weak_ptr_factory_.GetWeakPtr(), 125 scope, token, callback));
148 authorized_entity, scope, token, callback));
149 return;
150 }
151
152 DoValidateToken(authorized_entity, scope, token, callback);
153 } 126 }
154 127
155 void InstanceIDImpl::DoValidateToken(const std::string& authorized_entity, 128 void InstanceIDImpl::DoValidateToken(const std::string& authorized_entity,
156 const std::string& scope, 129 const std::string& scope,
157 const std::string& token, 130 const std::string& token,
158 const ValidateTokenCallback& callback) { 131 const ValidateTokenCallback& callback) {
159 if (id_.empty()) { 132 if (id_.empty()) {
160 callback.Run(false /* is_valid */); 133 callback.Run(false /* is_valid */);
161 return; 134 return;
162 } 135 }
163 136
164 Handler()->ValidateToken(app_id(), authorized_entity, scope, token, callback); 137 Handler()->ValidateToken(app_id(), authorized_entity, scope, token, callback);
165 } 138 }
166 139
167 void InstanceIDImpl::DeleteTokenImpl(const std::string& authorized_entity, 140 void InstanceIDImpl::DeleteTokenImpl(const std::string& authorized_entity,
168 const std::string& scope, 141 const std::string& scope,
169 const DeleteTokenCallback& callback) { 142 const DeleteTokenCallback& callback) {
170 DCHECK(!authorized_entity.empty()); 143 DCHECK(!authorized_entity.empty());
171 DCHECK(!scope.empty()); 144 DCHECK(!scope.empty());
172 145
173 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { 146 RunWhenReady(base::Bind(&InstanceIDImpl::DoDeleteToken,
174 delayed_task_controller_.AddTask( 147 weak_ptr_factory_.GetWeakPtr(), authorized_entity,
175 base::Bind(&InstanceIDImpl::DoDeleteToken, 148 scope, callback));
176 weak_ptr_factory_.GetWeakPtr(),
177 authorized_entity,
178 scope,
179 callback));
180 return;
181 }
182
183 DoDeleteToken(authorized_entity, scope, callback);
184 } 149 }
185 150
186 void InstanceIDImpl::DoDeleteToken( 151 void InstanceIDImpl::DoDeleteToken(
187 const std::string& authorized_entity, 152 const std::string& authorized_entity,
188 const std::string& scope, 153 const std::string& scope,
189 const DeleteTokenCallback& callback) { 154 const DeleteTokenCallback& callback) {
190 // Nothing to delete if the ID has not been generated. 155 // Nothing to delete if the ID has not been generated.
191 if (id_.empty()) { 156 if (id_.empty()) {
192 callback.Run(InstanceID::INVALID_PARAMETER); 157 callback.Run(InstanceID::INVALID_PARAMETER);
193 return; 158 return;
194 } 159 }
195 160
196 Handler()->DeleteToken(app_id(), authorized_entity, scope, 161 Handler()->DeleteToken(app_id(), authorized_entity, scope,
197 base::Bind(&InstanceIDImpl::OnDeleteTokenCompleted, 162 base::Bind(&InstanceIDImpl::OnDeleteTokenCompleted,
198 weak_ptr_factory_.GetWeakPtr(), callback)); 163 weak_ptr_factory_.GetWeakPtr(), callback));
199 } 164 }
200 165
201 void InstanceIDImpl::DeleteIDImpl(const DeleteIDCallback& callback) { 166 void InstanceIDImpl::DeleteIDImpl(const DeleteIDCallback& callback) {
202 if (!delayed_task_controller_.CanRunTaskWithoutDelay()) { 167 RunWhenReady(base::Bind(&InstanceIDImpl::DoDeleteID,
203 delayed_task_controller_.AddTask( 168 weak_ptr_factory_.GetWeakPtr(), callback));
204 base::Bind(&InstanceIDImpl::DoDeleteID,
205 weak_ptr_factory_.GetWeakPtr(),
206 callback));
207 return;
208 }
209
210 DoDeleteID(callback);
211 } 169 }
212 170
213 void InstanceIDImpl::DoDeleteID(const DeleteIDCallback& callback) { 171 void InstanceIDImpl::DoDeleteID(const DeleteIDCallback& callback) {
214 // Nothing to do if ID has not been generated. 172 // Nothing to do if ID has not been generated.
215 if (id_.empty()) { 173 if (id_.empty()) {
216 callback.Run(InstanceID::SUCCESS); 174 callback.Run(InstanceID::SUCCESS);
217 return; 175 return;
218 } 176 }
219 177
220 Handler()->DeleteAllTokensForApp( 178 Handler()->DeleteAllTokensForApp(
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 app_id(), id_, base::Int64ToString(creation_time_.ToInternalValue())); 256 app_id(), id_, base::Int64ToString(creation_time_.ToInternalValue()));
299 } 257 }
300 258
301 gcm::InstanceIDHandler* InstanceIDImpl::Handler() { 259 gcm::InstanceIDHandler* InstanceIDImpl::Handler() {
302 gcm::InstanceIDHandler* handler = 260 gcm::InstanceIDHandler* handler =
303 gcm_driver()->GetInstanceIDHandlerInternal(); 261 gcm_driver()->GetInstanceIDHandlerInternal();
304 DCHECK(handler); 262 DCHECK(handler);
305 return handler; 263 return handler;
306 } 264 }
307 265
266 void InstanceIDImpl::RunWhenReady(base::Closure task) {
267 if (!delayed_task_controller_.CanRunTaskWithoutDelay())
268 delayed_task_controller_.AddTask(task);
269 else
270 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task);
Peter Beverloo 2017/04/03 17:45:01 It's a bit unfortunate that this makes things asyn
johnme 2017/04/03 17:50:43 Acknowledged :)
271 }
272
308 } // namespace instance_id 273 } // namespace instance_id
OLDNEW
« no previous file with comments | « components/gcm_driver/instance_id/instance_id_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698