Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / services / gcm / fake_gcm_profile_service.cc
blobe1926a3b4539a3b99c5364fbd9227cc330957bdd
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"
7 #include "base/bind.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"
18 namespace gcm {
20 namespace {
22 class CustomFakeGCMDriver : public FakeGCMDriver {
23 public:
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);
36 protected:
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;
45 private:
46 FakeGCMProfileService* service_;
48 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver);
51 CustomFakeGCMDriver::CustomFakeGCMDriver(FakeGCMProfileService* service)
52 : service_(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(
62 FROM_HERE,
63 base::Bind(&FakeGCMProfileService::RegisterFinished,
64 base::Unretained(service_),
65 app_id,
66 sender_ids));
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_),
74 app_id));
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(
81 FROM_HERE,
82 base::Bind(&FakeGCMProfileService::SendFinished,
83 base::Unretained(service_),
84 app_id,
85 receiver_id,
86 message));
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);
105 } // namespace
107 // static
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));
112 return service;
115 FakeGCMProfileService::FakeGCMProfileService(Profile* profile)
116 : collect_(false),
117 registration_count_(0) {
120 FakeGCMProfileService::~FakeGCMProfileService() {}
122 void FakeGCMProfileService::RegisterFinished(
123 const std::string& app_id,
124 const std::vector<std::string>& sender_ids) {
125 if (collect_) {
126 last_registered_app_id_ = app_id;
127 last_registered_sender_ids_ = sender_ids;
130 // Generate fake registration IDs, encoding the number of sender IDs (used by
131 // GcmApiTest.RegisterValidation), then an incrementing count (even for the
132 // same app_id - there's no caching) so tests can distinguish registrations.
133 std::string registration_id = base::StringPrintf("%" PRIuS "-%d",
134 sender_ids.size(),
135 registration_count_);
136 ++registration_count_;
138 CustomFakeGCMDriver* custom_driver =
139 static_cast<CustomFakeGCMDriver*>(driver());
140 custom_driver->OnRegisterFinished(
141 app_id, registration_id, GCMClient::SUCCESS);
144 void FakeGCMProfileService::UnregisterFinished(const std::string& app_id) {
145 GCMClient::Result result = GCMClient::SUCCESS;
146 if (!unregister_responses_.empty()) {
147 result = unregister_responses_.front();
148 unregister_responses_.pop_front();
151 CustomFakeGCMDriver* custom_driver =
152 static_cast<CustomFakeGCMDriver*>(driver());
153 custom_driver->OnUnregisterFinished(app_id, result);
155 if (!unregister_callback_.is_null())
156 unregister_callback_.Run(app_id);
159 void FakeGCMProfileService::SendFinished(
160 const std::string& app_id,
161 const std::string& receiver_id,
162 const GCMClient::OutgoingMessage& message) {
163 if (collect_) {
164 last_sent_message_ = message;
165 last_receiver_id_ = receiver_id;
168 CustomFakeGCMDriver* custom_driver =
169 static_cast<CustomFakeGCMDriver*>(driver());
170 custom_driver->OnSendFinished(app_id, message.id, GCMClient::SUCCESS);
173 void FakeGCMProfileService::AddExpectedUnregisterResponse(
174 GCMClient::Result result) {
175 unregister_responses_.push_back(result);
178 void FakeGCMProfileService::SetUnregisterCallback(
179 const UnregisterCallback& callback) {
180 unregister_callback_ = callback;
183 } // namespace gcm