Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / services / gcm / fake_gcm_profile_service.cc
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.
4
5 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h"
6
7 #include "base/bind.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"
15
16 namespace gcm {
17
18 namespace {
19
20 class CustomFakeGCMDriver : public FakeGCMDriver {
21  public:
22   explicit CustomFakeGCMDriver(FakeGCMProfileService* service);
23   ~CustomFakeGCMDriver() override;
24
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);
33
34  protected:
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;
42
43  private:
44   FakeGCMProfileService* service_;
45
46   DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver);
47 };
48
49 CustomFakeGCMDriver::CustomFakeGCMDriver(FakeGCMProfileService* service)
50     : service_(service) {
51 }
52
53 CustomFakeGCMDriver::~CustomFakeGCMDriver() {
54 }
55
56 void CustomFakeGCMDriver::RegisterImpl(
57     const std::string& app_id,
58     const std::vector<std::string>& sender_ids) {
59   base::MessageLoop::current()->PostTask(
60       FROM_HERE,
61       base::Bind(&FakeGCMProfileService::RegisterFinished,
62                  base::Unretained(service_),
63                  app_id,
64                  sender_ids));
65 }
66
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_),
72           app_id));
73 }
74
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(
79       FROM_HERE,
80       base::Bind(&FakeGCMProfileService::SendFinished,
81                  base::Unretained(service_),
82                  app_id,
83                  receiver_id,
84                  message));
85 }
86
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);
92 }
93 void CustomFakeGCMDriver::OnUnregisterFinished(const std::string& app_id,
94                                                GCMClient::Result result) {
95   UnregisterFinished(app_id, result);
96 }
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);
101 }
102
103 }  // namespace
104
105 // static
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));
110   return service;
111 }
112
113 FakeGCMProfileService::FakeGCMProfileService(Profile* profile)
114     : collect_(false) {
115   static_cast<PushMessagingServiceImpl*>(push_messaging_service())
116       ->SetProfileForTesting(profile);
117 }
118
119 FakeGCMProfileService::~FakeGCMProfileService() {}
120
121 void FakeGCMProfileService::RegisterFinished(
122     const std::string& app_id,
123     const std::vector<std::string>& sender_ids) {
124   if (collect_) {
125     last_registered_app_id_ = app_id;
126     last_registered_sender_ids_ = sender_ids;
127   }
128
129   CustomFakeGCMDriver* custom_driver =
130       static_cast<CustomFakeGCMDriver*>(driver());
131   custom_driver->OnRegisterFinished(app_id,
132                            base::UintToString(sender_ids.size()),
133                            GCMClient::SUCCESS);
134 }
135
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();
141   }
142
143   CustomFakeGCMDriver* custom_driver =
144       static_cast<CustomFakeGCMDriver*>(driver());
145   custom_driver->OnUnregisterFinished(app_id, result);
146 }
147
148 void FakeGCMProfileService::SendFinished(
149     const std::string& app_id,
150     const std::string& receiver_id,
151     const GCMClient::OutgoingMessage& message) {
152   if (collect_) {
153     last_sent_message_ = message;
154     last_receiver_id_ = receiver_id;
155   }
156
157   CustomFakeGCMDriver* custom_driver =
158       static_cast<CustomFakeGCMDriver*>(driver());
159   custom_driver->OnSendFinished(app_id, message.id, GCMClient::SUCCESS);
160 }
161
162 void FakeGCMProfileService::AddExpectedUnregisterResponse(
163     GCMClient::Result result) {
164   unregister_responses_.push_back(result);
165 }
166
167 }  // namespace gcm