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/message_loop/message_loop.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "components/gcm_driver/fake_gcm_client_factory.h"
12 #include "components/gcm_driver/fake_gcm_driver.h"
13 #include "components/gcm_driver/gcm_driver.h"
14 #include "content/public/browser/browser_context.h"
20 class CustomFakeGCMDriver : public FakeGCMDriver {
22 explicit CustomFakeGCMDriver(FakeGCMProfileService* service);
23 ~CustomFakeGCMDriver() override;
25 void OnRegisterFinished(const std::string& app_id,
26 const std::string& registration_id,
27 GCMClient::Result result);
28 void OnUnregisterFinished(const std::string& app_id,
29 GCMClient::Result result);
30 void OnSendFinished(const std::string& app_id,
31 const std::string& message_id,
32 GCMClient::Result result);
35 // FakeGCMDriver overrides:
36 void RegisterImpl(const std::string& app_id,
37 const std::vector<std::string>& sender_ids) override;
38 void UnregisterImpl(const std::string& app_id) override;
39 void SendImpl(const std::string& app_id,
40 const std::string& receiver_id,
41 const GCMClient::OutgoingMessage& message) override;
44 FakeGCMProfileService* service_;
46 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver);
49 CustomFakeGCMDriver::CustomFakeGCMDriver(FakeGCMProfileService* service)
53 CustomFakeGCMDriver::~CustomFakeGCMDriver() {
56 void CustomFakeGCMDriver::RegisterImpl(
57 const std::string& app_id,
58 const std::vector<std::string>& sender_ids) {
59 base::MessageLoop::current()->PostTask(
61 base::Bind(&FakeGCMProfileService::RegisterFinished,
62 base::Unretained(service_),
67 void CustomFakeGCMDriver::UnregisterImpl(const std::string& app_id) {
68 base::MessageLoop::current()->PostTask(
69 FROM_HERE, base::Bind(
70 &FakeGCMProfileService::UnregisterFinished,
71 base::Unretained(service_),
75 void CustomFakeGCMDriver::SendImpl(const std::string& app_id,
76 const std::string& receiver_id,
77 const GCMClient::OutgoingMessage& message) {
78 base::MessageLoop::current()->PostTask(
80 base::Bind(&FakeGCMProfileService::SendFinished,
81 base::Unretained(service_),
87 void CustomFakeGCMDriver::OnRegisterFinished(
88 const std::string& app_id,
89 const std::string& registration_id,
90 GCMClient::Result result) {
91 RegisterFinished(app_id, registration_id, result);
93 void CustomFakeGCMDriver::OnUnregisterFinished(const std::string& app_id,
94 GCMClient::Result result) {
95 UnregisterFinished(app_id, result);
97 void CustomFakeGCMDriver::OnSendFinished(const std::string& app_id,
98 const std::string& message_id,
99 GCMClient::Result result) {
100 SendFinished(app_id, message_id, result);
106 KeyedService* FakeGCMProfileService::Build(content::BrowserContext* context) {
107 Profile* profile = static_cast<Profile*>(context);
108 FakeGCMProfileService* service = new FakeGCMProfileService(profile);
109 service->SetDriverForTesting(new CustomFakeGCMDriver(service));
113 FakeGCMProfileService::FakeGCMProfileService(Profile* profile)
115 static_cast<PushMessagingServiceImpl*>(push_messaging_service())
116 ->SetProfileForTesting(profile);
119 FakeGCMProfileService::~FakeGCMProfileService() {}
121 void FakeGCMProfileService::RegisterFinished(
122 const std::string& app_id,
123 const std::vector<std::string>& sender_ids) {
125 last_registered_app_id_ = app_id;
126 last_registered_sender_ids_ = sender_ids;
129 CustomFakeGCMDriver* custom_driver =
130 static_cast<CustomFakeGCMDriver*>(driver());
131 custom_driver->OnRegisterFinished(app_id,
132 base::UintToString(sender_ids.size()),
136 void FakeGCMProfileService::UnregisterFinished(const std::string& app_id) {
137 GCMClient::Result result = GCMClient::SUCCESS;
138 if (!unregister_responses_.empty()) {
139 result = unregister_responses_.front();
140 unregister_responses_.pop_front();
143 CustomFakeGCMDriver* custom_driver =
144 static_cast<CustomFakeGCMDriver*>(driver());
145 custom_driver->OnUnregisterFinished(app_id, result);
148 void FakeGCMProfileService::SendFinished(
149 const std::string& app_id,
150 const std::string& receiver_id,
151 const GCMClient::OutgoingMessage& message) {
153 last_sent_message_ = message;
154 last_receiver_id_ = receiver_id;
157 CustomFakeGCMDriver* custom_driver =
158 static_cast<CustomFakeGCMDriver*>(driver());
159 custom_driver->OnSendFinished(app_id, message.id, GCMClient::SUCCESS);
162 void FakeGCMProfileService::AddExpectedUnregisterResponse(
163 GCMClient::Result result) {
164 unregister_responses_.push_back(result);