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 "content/public/browser/browser_context.h"
     12 
     13 namespace gcm {
     14 
     15 // static
     16 BrowserContextKeyedService* FakeGCMProfileService::Build(
     17     content::BrowserContext* context) {
     18   Profile* profile = static_cast<Profile*>(context);
     19 
     20   return new FakeGCMProfileService(profile);
     21 }
     22 
     23 // static
     24 void FakeGCMProfileService::EnableGCMForTesting() {
     25   GCMProfileService::enable_gcm_for_testing_ = true;
     26 }
     27 
     28 FakeGCMProfileService::FakeGCMProfileService(Profile* profile)
     29     : GCMProfileService(profile),
     30       collect_(false) {}
     31 
     32 FakeGCMProfileService::~FakeGCMProfileService() {}
     33 
     34 void FakeGCMProfileService::Register(const std::string& app_id,
     35                                      const std::vector<std::string>& sender_ids,
     36                                      const std::string& cert,
     37                                      RegisterCallback callback) {
     38   base::MessageLoop::current()->PostTask(
     39       FROM_HERE,
     40       base::Bind(&FakeGCMProfileService::RegisterFinished,
     41                  base::Unretained(this),
     42                  app_id,
     43                  sender_ids,
     44                  cert,
     45                  callback));
     46 }
     47 
     48 void FakeGCMProfileService::RegisterFinished(
     49     const std::string& app_id,
     50     const std::vector<std::string>& sender_ids,
     51     const std::string& cert,
     52     RegisterCallback callback) {
     53   if (collect_) {
     54     last_registered_app_id_ = app_id;
     55     last_registered_sender_ids_ = sender_ids;
     56     last_registered_cert_ = cert;
     57   }
     58 
     59   callback.Run(base::UintToString(sender_ids.size()), GCMClient::SUCCESS);
     60 }
     61 
     62 void FakeGCMProfileService::Send(const std::string& app_id,
     63                                  const std::string& receiver_id,
     64                                  const GCMClient::OutgoingMessage& message,
     65                                  SendCallback callback) {
     66   base::MessageLoop::current()->PostTask(
     67       FROM_HERE,
     68       base::Bind(&FakeGCMProfileService::SendFinished,
     69                  base::Unretained(this),
     70                  app_id,
     71                  receiver_id,
     72                  message,
     73                  callback));
     74 }
     75 
     76 void FakeGCMProfileService::SendFinished(
     77     const std::string& app_id,
     78     const std::string& receiver_id,
     79     const GCMClient::OutgoingMessage& message,
     80     SendCallback callback) {
     81   if (collect_) {
     82     last_sent_message_ = message;
     83     last_receiver_id_ = receiver_id;
     84   }
     85 
     86   callback.Run(message.id, GCMClient::SUCCESS);
     87 }
     88 
     89 }  // namespace gcm
     90