Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / services / gcm / fake_gcm_profile_service.cc
blobe0530d56da478b32b3a1ae4816992c3d9638d7ea
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/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "components/gcm_driver/fake_gcm_client_factory.h"
16 #include "components/gcm_driver/fake_gcm_driver.h"
17 #include "components/gcm_driver/gcm_driver.h"
18 #include "content/public/browser/browser_context.h"
20 namespace gcm {
22 namespace {
24 class CustomFakeGCMDriver : public FakeGCMDriver {
25 public:
26 explicit CustomFakeGCMDriver(FakeGCMProfileService* service);
27 ~CustomFakeGCMDriver() override;
29 void OnRegisterFinished(const std::string& app_id,
30 const std::string& registration_id,
31 GCMClient::Result result);
32 void OnUnregisterFinished(const std::string& app_id,
33 GCMClient::Result result);
34 void OnSendFinished(const std::string& app_id,
35 const std::string& message_id,
36 GCMClient::Result result);
38 protected:
39 // FakeGCMDriver overrides:
40 void RegisterImpl(const std::string& app_id,
41 const std::vector<std::string>& sender_ids) override;
42 void UnregisterImpl(const std::string& app_id) override;
43 void SendImpl(const std::string& app_id,
44 const std::string& receiver_id,
45 const OutgoingMessage& message) override;
47 private:
48 FakeGCMProfileService* service_;
50 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver);
53 CustomFakeGCMDriver::CustomFakeGCMDriver(FakeGCMProfileService* service)
54 : FakeGCMDriver(base::ThreadTaskRunnerHandle::Get()),
55 service_(service) {
58 CustomFakeGCMDriver::~CustomFakeGCMDriver() {
61 void CustomFakeGCMDriver::RegisterImpl(
62 const std::string& app_id,
63 const std::vector<std::string>& sender_ids) {
64 base::ThreadTaskRunnerHandle::Get()->PostTask(
65 FROM_HERE, base::Bind(&FakeGCMProfileService::RegisterFinished,
66 base::Unretained(service_), app_id, sender_ids));
69 void CustomFakeGCMDriver::UnregisterImpl(const std::string& app_id) {
70 base::ThreadTaskRunnerHandle::Get()->PostTask(
71 FROM_HERE, base::Bind(&FakeGCMProfileService::UnregisterFinished,
72 base::Unretained(service_), app_id));
75 void CustomFakeGCMDriver::SendImpl(const std::string& app_id,
76 const std::string& receiver_id,
77 const OutgoingMessage& message) {
78 base::ThreadTaskRunnerHandle::Get()->PostTask(
79 FROM_HERE,
80 base::Bind(&FakeGCMProfileService::SendFinished,
81 base::Unretained(service_), app_id, receiver_id, message));
84 void CustomFakeGCMDriver::OnRegisterFinished(
85 const std::string& app_id,
86 const std::string& registration_id,
87 GCMClient::Result result) {
88 RegisterFinished(app_id, registration_id, result);
90 void CustomFakeGCMDriver::OnUnregisterFinished(const std::string& app_id,
91 GCMClient::Result result) {
92 UnregisterFinished(app_id, result);
94 void CustomFakeGCMDriver::OnSendFinished(const std::string& app_id,
95 const std::string& message_id,
96 GCMClient::Result result) {
97 SendFinished(app_id, message_id, result);
100 } // namespace
102 // static
103 scoped_ptr<KeyedService> FakeGCMProfileService::Build(
104 content::BrowserContext* context) {
105 Profile* profile = static_cast<Profile*>(context);
106 scoped_ptr<FakeGCMProfileService> service(new FakeGCMProfileService(profile));
107 service->SetDriverForTesting(new CustomFakeGCMDriver(service.get()));
108 return service.Pass();
111 FakeGCMProfileService::FakeGCMProfileService(Profile* profile)
112 : collect_(false),
113 registration_count_(0) {
116 FakeGCMProfileService::~FakeGCMProfileService() {}
118 void FakeGCMProfileService::RegisterFinished(
119 const std::string& app_id,
120 const std::vector<std::string>& sender_ids) {
121 if (collect_) {
122 last_registered_app_id_ = app_id;
123 last_registered_sender_ids_ = sender_ids;
126 // Generate fake registration IDs, encoding the number of sender IDs (used by
127 // GcmApiTest.RegisterValidation), then an incrementing count (even for the
128 // same app_id - there's no caching) so tests can distinguish registrations.
129 std::string registration_id = base::StringPrintf("%" PRIuS "-%d",
130 sender_ids.size(),
131 registration_count_);
132 ++registration_count_;
134 CustomFakeGCMDriver* custom_driver =
135 static_cast<CustomFakeGCMDriver*>(driver());
136 custom_driver->OnRegisterFinished(
137 app_id, registration_id, GCMClient::SUCCESS);
140 void FakeGCMProfileService::UnregisterFinished(const std::string& app_id) {
141 GCMClient::Result result = GCMClient::SUCCESS;
142 if (!unregister_responses_.empty()) {
143 result = unregister_responses_.front();
144 unregister_responses_.pop_front();
147 CustomFakeGCMDriver* custom_driver =
148 static_cast<CustomFakeGCMDriver*>(driver());
149 custom_driver->OnUnregisterFinished(app_id, result);
151 if (!unregister_callback_.is_null())
152 unregister_callback_.Run(app_id);
155 void FakeGCMProfileService::SendFinished(const std::string& app_id,
156 const std::string& receiver_id,
157 const OutgoingMessage& message) {
158 if (collect_) {
159 last_sent_message_ = message;
160 last_receiver_id_ = receiver_id;
163 CustomFakeGCMDriver* custom_driver =
164 static_cast<CustomFakeGCMDriver*>(driver());
165 custom_driver->OnSendFinished(app_id, message.id, GCMClient::SUCCESS);
168 void FakeGCMProfileService::AddExpectedUnregisterResponse(
169 GCMClient::Result result) {
170 unregister_responses_.push_back(result);
173 void FakeGCMProfileService::SetUnregisterCallback(
174 const UnregisterCallback& callback) {
175 unregister_callback_ = callback;
178 } // namespace gcm