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

Side by Side Diff: chrome/browser/services/gcm/gcm_client_mock.cc

Issue 165993005: [GCM] Make sure GCM checkout logic is invoked when the profile is signed out (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix test failures on trybots Created 6 years, 10 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chrome/browser/services/gcm/gcm_client_mock.h" 5 #include "chrome/browser/services/gcm/gcm_client_mock.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/sys_byteorder.h" 10 #include "base/sys_byteorder.h"
11 #include "base/time/time.h" 11 #include "base/time/time.h"
12 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
13 13
14 namespace gcm { 14 namespace gcm {
15 15
16 GCMClientMock::GCMClientMock(Status status, ErrorSimulation error_simulation) 16 GCMClientMock::GCMClientMock(LoadingDelay loading_delay,
17 ErrorSimulation error_simulation)
17 : delegate_(NULL), 18 : delegate_(NULL),
18 status_(status), 19 status_(UNINITIALIZED),
19 error_simulation_(error_simulation) { 20 loading_delay_(loading_delay),
21 error_simulation_(error_simulation),
22 weak_ptr_factory_(this) {
20 } 23 }
21 24
22 GCMClientMock::~GCMClientMock() { 25 GCMClientMock::~GCMClientMock() {
23 } 26 }
24 27
25 void GCMClientMock::Initialize( 28 void GCMClientMock::Initialize(
26 const checkin_proto::ChromeBuildProto& chrome_build_proto, 29 const checkin_proto::ChromeBuildProto& chrome_build_proto,
27 const base::FilePath& store_path, 30 const base::FilePath& store_path,
28 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner, 31 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
29 const scoped_refptr<net::URLRequestContextGetter>& 32 const scoped_refptr<net::URLRequestContextGetter>&
30 url_request_context_getter, 33 url_request_context_getter,
31 Delegate* delegate) { 34 Delegate* delegate) {
32 delegate_ = delegate; 35 delegate_ = delegate;
33 } 36 }
34 37
38 void GCMClientMock::Load() {
39 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
40 DCHECK_NE(LOADED, status_);
41
42 if (loading_delay_ == DELAY_LOADING)
43 return;
44
45 status_ = LOADED;
46 base::MessageLoop::current()->PostTask(
47 FROM_HERE,
48 base::Bind(&GCMClientMock::CheckinFinished,
49 weak_ptr_factory_.GetWeakPtr()));
50 }
51
35 void GCMClientMock::CheckOut() { 52 void GCMClientMock::CheckOut() {
36 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 53 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
54 status_ = CHECKED_OUT;
37 } 55 }
38 56
39 void GCMClientMock::Register(const std::string& app_id, 57 void GCMClientMock::Register(const std::string& app_id,
40 const std::string& cert, 58 const std::string& cert,
41 const std::vector<std::string>& sender_ids) { 59 const std::vector<std::string>& sender_ids) {
42 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 60 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
43 61
44 std::string registration_id; 62 std::string registration_id;
45 if (error_simulation_ == ALWAYS_SUCCEED) 63 if (error_simulation_ == ALWAYS_SUCCEED)
46 registration_id = GetRegistrationIdFromSenderIds(sender_ids); 64 registration_id = GetRegistrationIdFromSenderIds(sender_ids);
47 65
48 base::MessageLoop::current()->PostTask( 66 base::MessageLoop::current()->PostTask(
49 FROM_HERE, 67 FROM_HERE,
50 base::Bind(&GCMClientMock::RegisterFinished, 68 base::Bind(&GCMClientMock::RegisterFinished,
51 base::Unretained(this), 69 weak_ptr_factory_.GetWeakPtr(),
52 app_id, 70 app_id,
53 registration_id)); 71 registration_id));
54 } 72 }
55 73
56 void GCMClientMock::Unregister(const std::string& app_id) { 74 void GCMClientMock::Unregister(const std::string& app_id) {
57 } 75 }
58 76
59 void GCMClientMock::Send(const std::string& app_id, 77 void GCMClientMock::Send(const std::string& app_id,
60 const std::string& receiver_id, 78 const std::string& receiver_id,
61 const OutgoingMessage& message) { 79 const OutgoingMessage& message) {
62 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 80 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
63 81
64 base::MessageLoop::current()->PostTask( 82 base::MessageLoop::current()->PostTask(
65 FROM_HERE, 83 FROM_HERE,
66 base::Bind(&GCMClientMock::SendFinished, 84 base::Bind(&GCMClientMock::SendFinished,
67 base::Unretained(this), 85 weak_ptr_factory_.GetWeakPtr(),
68 app_id, 86 app_id,
69 message.id)); 87 message.id));
70 } 88 }
71 89
72 bool GCMClientMock::IsReady() const { 90 void GCMClientMock::PerformDelayedLoading() {
73 return status_ == READY; 91 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
92 DCHECK_EQ(loading_delay_, DELAY_LOADING);
93
94 loading_delay_ = NO_DELAY_LOADING;
95 content::BrowserThread::PostTask(
96 content::BrowserThread::IO,
97 FROM_HERE,
98 base::Bind(&GCMClientMock::Load, weak_ptr_factory_.GetWeakPtr()));
74 } 99 }
75 100
76 void GCMClientMock::ReceiveMessage(const std::string& app_id, 101 void GCMClientMock::ReceiveMessage(const std::string& app_id,
77 const IncomingMessage& message) { 102 const IncomingMessage& message) {
78 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 103 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
79 104
80 content::BrowserThread::PostTask( 105 content::BrowserThread::PostTask(
81 content::BrowserThread::IO, 106 content::BrowserThread::IO,
82 FROM_HERE, 107 FROM_HERE,
83 base::Bind(&GCMClientMock::MessageReceived, 108 base::Bind(&GCMClientMock::MessageReceived,
84 base::Unretained(this), 109 weak_ptr_factory_.GetWeakPtr(),
85 app_id, 110 app_id,
86 message)); 111 message));
87 } 112 }
88 113
89 void GCMClientMock::DeleteMessages(const std::string& app_id) { 114 void GCMClientMock::DeleteMessages(const std::string& app_id) {
90 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 115 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
91 116
92 content::BrowserThread::PostTask( 117 content::BrowserThread::PostTask(
93 content::BrowserThread::IO, 118 content::BrowserThread::IO,
94 FROM_HERE, 119 FROM_HERE,
95 base::Bind(&GCMClientMock::MessagesDeleted, 120 base::Bind(&GCMClientMock::MessagesDeleted,
96 base::Unretained(this), 121 weak_ptr_factory_.GetWeakPtr(),
97 app_id)); 122 app_id));
98 } 123 }
99 124
100 void GCMClientMock::SetReady() {
101 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
102 DCHECK_EQ(status_, NOT_READY);
103
104 status_ = READY;
105 content::BrowserThread::PostTask(
106 content::BrowserThread::IO,
107 FROM_HERE,
108 base::Bind(&GCMClientMock::SetReadyOnIO,
109 base::Unretained(this)));
110 }
111
112 // static 125 // static
113 std::string GCMClientMock::GetRegistrationIdFromSenderIds( 126 std::string GCMClientMock::GetRegistrationIdFromSenderIds(
114 const std::vector<std::string>& sender_ids) { 127 const std::vector<std::string>& sender_ids) {
115 // GCMProfileService normalizes the sender IDs by making them sorted. 128 // GCMProfileService normalizes the sender IDs by making them sorted.
116 std::vector<std::string> normalized_sender_ids = sender_ids; 129 std::vector<std::string> normalized_sender_ids = sender_ids;
117 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end()); 130 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end());
118 131
119 // Simulate the registration_id by concaternating all sender IDs. 132 // Simulate the registration_id by concaternating all sender IDs.
120 // Set registration_id to empty to denote an error if sender_ids contains a 133 // Set registration_id to empty to denote an error if sender_ids contains a
121 // hint. 134 // hint.
122 std::string registration_id; 135 std::string registration_id;
123 if (sender_ids.size() != 1 || 136 if (sender_ids.size() != 1 ||
124 sender_ids[0].find("error") == std::string::npos) { 137 sender_ids[0].find("error") == std::string::npos) {
125 for (size_t i = 0; i < normalized_sender_ids.size(); ++i) { 138 for (size_t i = 0; i < normalized_sender_ids.size(); ++i) {
126 if (i > 0) 139 if (i > 0)
127 registration_id += ","; 140 registration_id += ",";
128 registration_id += normalized_sender_ids[i]; 141 registration_id += normalized_sender_ids[i];
129 } 142 }
130 } 143 }
131 return registration_id; 144 return registration_id;
132 } 145 }
133 146
147 void GCMClientMock::CheckinFinished() {
148 delegate_->OnGCMReady();
149 }
150
134 void GCMClientMock::RegisterFinished(const std::string& app_id, 151 void GCMClientMock::RegisterFinished(const std::string& app_id,
135 const std::string& registrion_id) { 152 const std::string& registrion_id) {
136 delegate_->OnRegisterFinished( 153 delegate_->OnRegisterFinished(
137 app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS); 154 app_id, registrion_id, registrion_id.empty() ? SERVER_ERROR : SUCCESS);
138 } 155 }
139 156
140 void GCMClientMock::SendFinished(const std::string& app_id, 157 void GCMClientMock::SendFinished(const std::string& app_id,
141 const std::string& message_id) { 158 const std::string& message_id) {
142 delegate_->OnSendFinished(app_id, message_id, SUCCESS); 159 delegate_->OnSendFinished(app_id, message_id, SUCCESS);
143 160
144 // Simulate send error if message id contains a hint. 161 // Simulate send error if message id contains a hint.
145 if (message_id.find("error") != std::string::npos) { 162 if (message_id.find("error") != std::string::npos) {
146 base::MessageLoop::current()->PostDelayedTask( 163 base::MessageLoop::current()->PostDelayedTask(
147 FROM_HERE, 164 FROM_HERE,
148 base::Bind(&GCMClientMock::MessageSendError, 165 base::Bind(&GCMClientMock::MessageSendError,
149 base::Unretained(this), 166 weak_ptr_factory_.GetWeakPtr(),
150 app_id, 167 app_id,
151 message_id), 168 message_id),
152 base::TimeDelta::FromMilliseconds(200)); 169 base::TimeDelta::FromMilliseconds(200));
153 } 170 }
154 } 171 }
155 172
156 void GCMClientMock::MessageReceived(const std::string& app_id, 173 void GCMClientMock::MessageReceived(const std::string& app_id,
157 const IncomingMessage& message) { 174 const IncomingMessage& message) {
158 if (delegate_) 175 if (delegate_)
159 delegate_->OnMessageReceived(app_id, message); 176 delegate_->OnMessageReceived(app_id, message);
160 } 177 }
161 178
162 void GCMClientMock::MessagesDeleted(const std::string& app_id) { 179 void GCMClientMock::MessagesDeleted(const std::string& app_id) {
163 if (delegate_) 180 if (delegate_)
164 delegate_->OnMessagesDeleted(app_id); 181 delegate_->OnMessagesDeleted(app_id);
165 } 182 }
166 183
167 void GCMClientMock::MessageSendError(const std::string& app_id, 184 void GCMClientMock::MessageSendError(const std::string& app_id,
168 const std::string& message_id) { 185 const std::string& message_id) {
169 if (delegate_) 186 if (delegate_)
170 delegate_->OnMessageSendError(app_id, message_id, NETWORK_ERROR); 187 delegate_->OnMessageSendError(app_id, message_id, NETWORK_ERROR);
171 } 188 }
172 189
173 void GCMClientMock::SetReadyOnIO() {
174 delegate_->OnGCMReady();
175 }
176
177 } // namespace gcm 190 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698