OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" | 5 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h" |
11 #include "content/public/browser/browser_context.h" | 12 #include "content/public/browser/browser_context.h" |
12 | 13 |
13 namespace gcm { | 14 namespace gcm { |
14 | 15 |
15 // static | 16 namespace { |
16 KeyedService* FakeGCMProfileService::Build(content::BrowserContext* context) { | 17 |
17 Profile* profile = static_cast<Profile*>(context); | 18 class FakeGCMDriver : public GCMDriver { |
18 return new FakeGCMProfileService(profile); | 19 public: |
| 20 explicit FakeGCMDriver(FakeGCMProfileService* service); |
| 21 virtual ~FakeGCMDriver(); |
| 22 |
| 23 // GCMDriver overrides. |
| 24 virtual void Shutdown() OVERRIDE; |
| 25 virtual void AddAppHandler(const std::string& app_id, |
| 26 GCMAppHandler* handler) OVERRIDE; |
| 27 virtual void RemoveAppHandler(const std::string& app_id) OVERRIDE; |
| 28 virtual void Register(const std::string& app_id, |
| 29 const std::vector<std::string>& sender_ids, |
| 30 const RegisterCallback& callback) OVERRIDE; |
| 31 virtual void Unregister(const std::string& app_id, |
| 32 const UnregisterCallback& callback) OVERRIDE; |
| 33 virtual void Send(const std::string& app_id, |
| 34 const std::string& receiver_id, |
| 35 const GCMClient::OutgoingMessage& message, |
| 36 const SendCallback& callback) OVERRIDE; |
| 37 |
| 38 private: |
| 39 FakeGCMProfileService* service_; |
| 40 |
| 41 DISALLOW_COPY_AND_ASSIGN(FakeGCMDriver); |
| 42 }; |
| 43 |
| 44 FakeGCMDriver::FakeGCMDriver(FakeGCMProfileService* service) |
| 45 : service_(service) { |
19 } | 46 } |
20 | 47 |
21 FakeGCMProfileService::FakeGCMProfileService(Profile* profile) | 48 FakeGCMDriver::~FakeGCMDriver() { |
22 : GCMProfileService(profile), | 49 } |
23 collect_(false) {} | |
24 | 50 |
25 FakeGCMProfileService::~FakeGCMProfileService() {} | 51 void FakeGCMDriver::Shutdown() { |
| 52 } |
26 | 53 |
27 void FakeGCMProfileService::Register(const std::string& app_id, | 54 void FakeGCMDriver::AddAppHandler(const std::string& app_id, |
28 const std::vector<std::string>& sender_ids, | 55 GCMAppHandler* handler) { |
29 const RegisterCallback& callback) { | 56 } |
| 57 |
| 58 void FakeGCMDriver::RemoveAppHandler(const std::string& app_id) { |
| 59 } |
| 60 |
| 61 void FakeGCMDriver::Register(const std::string& app_id, |
| 62 const std::vector<std::string>& sender_ids, |
| 63 const RegisterCallback& callback) { |
30 base::MessageLoop::current()->PostTask( | 64 base::MessageLoop::current()->PostTask( |
31 FROM_HERE, | 65 FROM_HERE, |
32 base::Bind(&FakeGCMProfileService::RegisterFinished, | 66 base::Bind(&FakeGCMProfileService::RegisterFinished, |
33 base::Unretained(this), | 67 base::Unretained(service_), |
34 app_id, | 68 app_id, |
35 sender_ids, | 69 sender_ids, |
36 callback)); | 70 callback)); |
37 } | 71 } |
38 | 72 |
| 73 void FakeGCMDriver::Unregister(const std::string& app_id, |
| 74 const UnregisterCallback& callback) { |
| 75 base::MessageLoop::current()->PostTask( |
| 76 FROM_HERE, base::Bind( |
| 77 &FakeGCMProfileService::UnregisterFinished, |
| 78 base::Unretained(service_), |
| 79 app_id, |
| 80 callback)); |
| 81 } |
| 82 |
| 83 void FakeGCMDriver::Send(const std::string& app_id, |
| 84 const std::string& receiver_id, |
| 85 const GCMClient::OutgoingMessage& message, |
| 86 const SendCallback& callback) { |
| 87 base::MessageLoop::current()->PostTask( |
| 88 FROM_HERE, |
| 89 base::Bind(&FakeGCMProfileService::SendFinished, |
| 90 base::Unretained(service_), |
| 91 app_id, |
| 92 receiver_id, |
| 93 message, |
| 94 callback)); |
| 95 } |
| 96 |
| 97 } // namespace |
| 98 |
| 99 // static |
| 100 KeyedService* FakeGCMProfileService::Build(content::BrowserContext* context) { |
| 101 Profile* profile = static_cast<Profile*>(context); |
| 102 FakeGCMProfileService* service = new FakeGCMProfileService(profile); |
| 103 service->SetDriverForTesting(new FakeGCMDriver(service)); |
| 104 return service; |
| 105 } |
| 106 |
| 107 FakeGCMProfileService::FakeGCMProfileService(Profile* profile) |
| 108 : collect_(false) {} |
| 109 |
| 110 FakeGCMProfileService::~FakeGCMProfileService() {} |
| 111 |
39 void FakeGCMProfileService::RegisterFinished( | 112 void FakeGCMProfileService::RegisterFinished( |
40 const std::string& app_id, | 113 const std::string& app_id, |
41 const std::vector<std::string>& sender_ids, | 114 const std::vector<std::string>& sender_ids, |
42 const RegisterCallback& callback) { | 115 const GCMDriver::RegisterCallback& callback) { |
43 if (collect_) { | 116 if (collect_) { |
44 last_registered_app_id_ = app_id; | 117 last_registered_app_id_ = app_id; |
45 last_registered_sender_ids_ = sender_ids; | 118 last_registered_sender_ids_ = sender_ids; |
46 } | 119 } |
47 | 120 |
48 callback.Run(base::UintToString(sender_ids.size()), GCMClient::SUCCESS); | 121 callback.Run(base::UintToString(sender_ids.size()), GCMClient::SUCCESS); |
49 } | 122 } |
50 | 123 |
51 void FakeGCMProfileService::Unregister(const std::string& app_id, | 124 void FakeGCMProfileService::UnregisterFinished( |
52 const UnregisterCallback& callback) { | 125 const std::string& app_id, |
53 base::MessageLoop::current()->PostTask( | 126 const GCMDriver::UnregisterCallback& callback) { |
54 FROM_HERE, base::Bind(callback, GetNextExpectedUnregisterResponse())); | 127 GCMClient::Result result = GCMClient::SUCCESS; |
55 } | 128 if (!unregister_responses_.empty()) { |
| 129 result = unregister_responses_.front(); |
| 130 unregister_responses_.pop_front(); |
| 131 } |
56 | 132 |
57 void FakeGCMProfileService::Send(const std::string& app_id, | 133 callback.Run(result); |
58 const std::string& receiver_id, | |
59 const GCMClient::OutgoingMessage& message, | |
60 const SendCallback& callback) { | |
61 base::MessageLoop::current()->PostTask( | |
62 FROM_HERE, | |
63 base::Bind(&FakeGCMProfileService::SendFinished, | |
64 base::Unretained(this), | |
65 app_id, | |
66 receiver_id, | |
67 message, | |
68 callback)); | |
69 } | 134 } |
70 | 135 |
71 void FakeGCMProfileService::SendFinished( | 136 void FakeGCMProfileService::SendFinished( |
72 const std::string& app_id, | 137 const std::string& app_id, |
73 const std::string& receiver_id, | 138 const std::string& receiver_id, |
74 const GCMClient::OutgoingMessage& message, | 139 const GCMClient::OutgoingMessage& message, |
75 const SendCallback& callback) { | 140 const GCMDriver::SendCallback& callback) { |
76 if (collect_) { | 141 if (collect_) { |
77 last_sent_message_ = message; | 142 last_sent_message_ = message; |
78 last_receiver_id_ = receiver_id; | 143 last_receiver_id_ = receiver_id; |
79 } | 144 } |
80 | 145 |
81 callback.Run(message.id, GCMClient::SUCCESS); | 146 callback.Run(message.id, GCMClient::SUCCESS); |
82 } | 147 } |
83 | 148 |
84 void FakeGCMProfileService::AddExpectedUnregisterResponse( | 149 void FakeGCMProfileService::AddExpectedUnregisterResponse( |
85 GCMClient::Result result) { | 150 GCMClient::Result result) { |
86 unregister_responses_.push_back(result); | 151 unregister_responses_.push_back(result); |
87 } | 152 } |
88 | 153 |
89 GCMClient::Result FakeGCMProfileService::GetNextExpectedUnregisterResponse() { | |
90 if (unregister_responses_.empty()) | |
91 return GCMClient::SUCCESS; | |
92 GCMClient::Result response = *unregister_responses_.begin(); | |
93 unregister_responses_.erase(unregister_responses_.begin()); | |
94 return response; | |
95 } | |
96 | |
97 } // namespace gcm | 154 } // namespace gcm |
OLD | NEW |