Home | History | Annotate | Download | only in gcm
      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   virtual ~CustomFakeGCMDriver();
     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   virtual void RegisterImpl(
     37       const std::string& app_id,
     38       const std::vector<std::string>& sender_ids) OVERRIDE;
     39   virtual void UnregisterImpl(const std::string& app_id) OVERRIDE;
     40   virtual void SendImpl(const std::string& app_id,
     41                         const std::string& receiver_id,
     42                         const GCMClient::OutgoingMessage& message) OVERRIDE;
     43 
     44  private:
     45   FakeGCMProfileService* service_;
     46 
     47   DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver);
     48 };
     49 
     50 CustomFakeGCMDriver::CustomFakeGCMDriver(FakeGCMProfileService* service)
     51     : service_(service) {
     52 }
     53 
     54 CustomFakeGCMDriver::~CustomFakeGCMDriver() {
     55 }
     56 
     57 void CustomFakeGCMDriver::RegisterImpl(
     58     const std::string& app_id,
     59     const std::vector<std::string>& sender_ids) {
     60   base::MessageLoop::current()->PostTask(
     61       FROM_HERE,
     62       base::Bind(&FakeGCMProfileService::RegisterFinished,
     63                  base::Unretained(service_),
     64                  app_id,
     65                  sender_ids));
     66 }
     67 
     68 void CustomFakeGCMDriver::UnregisterImpl(const std::string& app_id) {
     69   base::MessageLoop::current()->PostTask(
     70       FROM_HERE, base::Bind(
     71           &FakeGCMProfileService::UnregisterFinished,
     72           base::Unretained(service_),
     73           app_id));
     74 }
     75 
     76 void CustomFakeGCMDriver::SendImpl(const std::string& app_id,
     77                                    const std::string& receiver_id,
     78                                    const GCMClient::OutgoingMessage& message) {
     79   base::MessageLoop::current()->PostTask(
     80       FROM_HERE,
     81       base::Bind(&FakeGCMProfileService::SendFinished,
     82                  base::Unretained(service_),
     83                  app_id,
     84                  receiver_id,
     85                  message));
     86 }
     87 
     88 void CustomFakeGCMDriver::OnRegisterFinished(
     89     const std::string& app_id,
     90     const std::string& registration_id,
     91     GCMClient::Result result) {
     92   RegisterFinished(app_id, registration_id, result);
     93 }
     94 void CustomFakeGCMDriver::OnUnregisterFinished(const std::string& app_id,
     95                                                GCMClient::Result result) {
     96   UnregisterFinished(app_id, result);
     97 }
     98 void CustomFakeGCMDriver::OnSendFinished(const std::string& app_id,
     99                                          const std::string& message_id,
    100                                          GCMClient::Result result) {
    101   SendFinished(app_id, message_id, result);
    102 }
    103 
    104 }  // namespace
    105 
    106 // static
    107 KeyedService* FakeGCMProfileService::Build(content::BrowserContext* context) {
    108   Profile* profile = static_cast<Profile*>(context);
    109   FakeGCMProfileService* service = new FakeGCMProfileService(profile);
    110   service->SetDriverForTesting(new CustomFakeGCMDriver(service));
    111   return service;
    112 }
    113 
    114 FakeGCMProfileService::FakeGCMProfileService(Profile* profile)
    115     : collect_(false) {}
    116 
    117 FakeGCMProfileService::~FakeGCMProfileService() {}
    118 
    119 void FakeGCMProfileService::RegisterFinished(
    120     const std::string& app_id,
    121     const std::vector<std::string>& sender_ids) {
    122   if (collect_) {
    123     last_registered_app_id_ = app_id;
    124     last_registered_sender_ids_ = sender_ids;
    125   }
    126 
    127   CustomFakeGCMDriver* custom_driver =
    128       static_cast<CustomFakeGCMDriver*>(driver());
    129   custom_driver->OnRegisterFinished(app_id,
    130                            base::UintToString(sender_ids.size()),
    131                            GCMClient::SUCCESS);
    132 }
    133 
    134 void FakeGCMProfileService::UnregisterFinished(const std::string& app_id) {
    135   GCMClient::Result result = GCMClient::SUCCESS;
    136   if (!unregister_responses_.empty()) {
    137     result = unregister_responses_.front();
    138     unregister_responses_.pop_front();
    139   }
    140 
    141   CustomFakeGCMDriver* custom_driver =
    142       static_cast<CustomFakeGCMDriver*>(driver());
    143   custom_driver->OnUnregisterFinished(app_id, result);
    144 }
    145 
    146 void FakeGCMProfileService::SendFinished(
    147     const std::string& app_id,
    148     const std::string& receiver_id,
    149     const GCMClient::OutgoingMessage& message) {
    150   if (collect_) {
    151     last_sent_message_ = message;
    152     last_receiver_id_ = receiver_id;
    153   }
    154 
    155   CustomFakeGCMDriver* custom_driver =
    156       static_cast<CustomFakeGCMDriver*>(driver());
    157   custom_driver->OnSendFinished(app_id, message.id, GCMClient::SUCCESS);
    158 }
    159 
    160 void FakeGCMProfileService::AddExpectedUnregisterResponse(
    161     GCMClient::Result result) {
    162   unregister_responses_.push_back(result);
    163 }
    164 
    165 }  // namespace gcm
    166