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
3 // found in the LICENSE file.
5 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h"
8 #include "base/format_macros.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/stringprintf.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "components/gcm_driver/fake_gcm_client_factory.h"
14 #include "components/gcm_driver/fake_gcm_driver.h"
15 #include "components/gcm_driver/gcm_driver.h"
16 #include "content/public/browser/browser_context.h"
22 class CustomFakeGCMDriver
: public FakeGCMDriver
{
24 explicit CustomFakeGCMDriver(FakeGCMProfileService
* service
);
25 ~CustomFakeGCMDriver() override
;
27 void OnRegisterFinished(const std::string
& app_id
,
28 const std::string
& registration_id
,
29 GCMClient::Result result
);
30 void OnUnregisterFinished(const std::string
& app_id
,
31 GCMClient::Result result
);
32 void OnSendFinished(const std::string
& app_id
,
33 const std::string
& message_id
,
34 GCMClient::Result result
);
37 // FakeGCMDriver overrides:
38 void RegisterImpl(const std::string
& app_id
,
39 const std::vector
<std::string
>& sender_ids
) override
;
40 void UnregisterImpl(const std::string
& app_id
) override
;
41 void SendImpl(const std::string
& app_id
,
42 const std::string
& receiver_id
,
43 const GCMClient::OutgoingMessage
& message
) override
;
46 FakeGCMProfileService
* service_
;
48 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver
);
51 CustomFakeGCMDriver::CustomFakeGCMDriver(FakeGCMProfileService
* service
)
55 CustomFakeGCMDriver::~CustomFakeGCMDriver() {
58 void CustomFakeGCMDriver::RegisterImpl(
59 const std::string
& app_id
,
60 const std::vector
<std::string
>& sender_ids
) {
61 base::MessageLoop::current()->PostTask(
63 base::Bind(&FakeGCMProfileService::RegisterFinished
,
64 base::Unretained(service_
),
69 void CustomFakeGCMDriver::UnregisterImpl(const std::string
& app_id
) {
70 base::MessageLoop::current()->PostTask(
71 FROM_HERE
, base::Bind(
72 &FakeGCMProfileService::UnregisterFinished
,
73 base::Unretained(service_
),
77 void CustomFakeGCMDriver::SendImpl(const std::string
& app_id
,
78 const std::string
& receiver_id
,
79 const GCMClient::OutgoingMessage
& message
) {
80 base::MessageLoop::current()->PostTask(
82 base::Bind(&FakeGCMProfileService::SendFinished
,
83 base::Unretained(service_
),
89 void CustomFakeGCMDriver::OnRegisterFinished(
90 const std::string
& app_id
,
91 const std::string
& registration_id
,
92 GCMClient::Result result
) {
93 RegisterFinished(app_id
, registration_id
, result
);
95 void CustomFakeGCMDriver::OnUnregisterFinished(const std::string
& app_id
,
96 GCMClient::Result result
) {
97 UnregisterFinished(app_id
, result
);
99 void CustomFakeGCMDriver::OnSendFinished(const std::string
& app_id
,
100 const std::string
& message_id
,
101 GCMClient::Result result
) {
102 SendFinished(app_id
, message_id
, result
);
108 KeyedService
* FakeGCMProfileService::Build(content::BrowserContext
* context
) {
109 Profile
* profile
= static_cast<Profile
*>(context
);
110 FakeGCMProfileService
* service
= new FakeGCMProfileService(profile
);
111 service
->SetDriverForTesting(new CustomFakeGCMDriver(service
));
115 FakeGCMProfileService::FakeGCMProfileService(Profile
* profile
)
117 registration_count_(0) {
118 static_cast<PushMessagingServiceImpl
*>(push_messaging_service())
119 ->SetProfileForTesting(profile
);
122 FakeGCMProfileService::~FakeGCMProfileService() {}
124 void FakeGCMProfileService::RegisterFinished(
125 const std::string
& app_id
,
126 const std::vector
<std::string
>& sender_ids
) {
128 last_registered_app_id_
= app_id
;
129 last_registered_sender_ids_
= sender_ids
;
132 // Generate fake registration IDs, encoding the number of sender IDs (used by
133 // GcmApiTest.RegisterValidation), then an incrementing count (even for the
134 // same app_id - there's no caching) so tests can distinguish registrations.
135 std::string registration_id
= base::StringPrintf("%" PRIuS
"-%d",
137 registration_count_
);
138 ++registration_count_
;
140 CustomFakeGCMDriver
* custom_driver
=
141 static_cast<CustomFakeGCMDriver
*>(driver());
142 custom_driver
->OnRegisterFinished(
143 app_id
, registration_id
, GCMClient::SUCCESS
);
146 void FakeGCMProfileService::UnregisterFinished(const std::string
& app_id
) {
147 GCMClient::Result result
= GCMClient::SUCCESS
;
148 if (!unregister_responses_
.empty()) {
149 result
= unregister_responses_
.front();
150 unregister_responses_
.pop_front();
153 CustomFakeGCMDriver
* custom_driver
=
154 static_cast<CustomFakeGCMDriver
*>(driver());
155 custom_driver
->OnUnregisterFinished(app_id
, result
);
157 if (!unregister_callback_
.is_null())
158 unregister_callback_
.Run(app_id
);
161 void FakeGCMProfileService::SendFinished(
162 const std::string
& app_id
,
163 const std::string
& receiver_id
,
164 const GCMClient::OutgoingMessage
& message
) {
166 last_sent_message_
= message
;
167 last_receiver_id_
= receiver_id
;
170 CustomFakeGCMDriver
* custom_driver
=
171 static_cast<CustomFakeGCMDriver
*>(driver());
172 custom_driver
->OnSendFinished(app_id
, message
.id
, GCMClient::SUCCESS
);
175 void FakeGCMProfileService::AddExpectedUnregisterResponse(
176 GCMClient::Result result
) {
177 unregister_responses_
.push_back(result
);
180 void FakeGCMProfileService::SetUnregisterCallback(
181 const UnregisterCallback
& callback
) {
182 unregister_callback_
= callback
;