| OLD | NEW |
| (Empty) |
| 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 <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/format_macros.h" | |
| 11 #include "base/location.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/single_thread_task_runner.h" | |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "base/strings/stringprintf.h" | |
| 16 #include "base/threading/thread_task_runner_handle.h" | |
| 17 #include "chrome/browser/profiles/profile.h" | |
| 18 #include "components/gcm_driver/fake_gcm_client_factory.h" | |
| 19 #include "components/gcm_driver/fake_gcm_driver.h" | |
| 20 #include "components/gcm_driver/gcm_driver.h" | |
| 21 #include "content/public/browser/browser_context.h" | |
| 22 | |
| 23 namespace gcm { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 class CustomFakeGCMDriver : public FakeGCMDriver { | |
| 28 public: | |
| 29 explicit CustomFakeGCMDriver(FakeGCMProfileService* service); | |
| 30 ~CustomFakeGCMDriver() override; | |
| 31 | |
| 32 void OnRegisterFinished(const std::string& app_id, | |
| 33 const std::string& registration_id, | |
| 34 GCMClient::Result result); | |
| 35 void OnUnregisterFinished(const std::string& app_id, | |
| 36 GCMClient::Result result); | |
| 37 void OnSendFinished(const std::string& app_id, | |
| 38 const std::string& message_id, | |
| 39 GCMClient::Result result); | |
| 40 | |
| 41 void OnDispatchMessage(const std::string& app_id, | |
| 42 const IncomingMessage& message); | |
| 43 | |
| 44 protected: | |
| 45 // FakeGCMDriver overrides: | |
| 46 void RegisterImpl(const std::string& app_id, | |
| 47 const std::vector<std::string>& sender_ids) override; | |
| 48 void UnregisterImpl(const std::string& app_id) override; | |
| 49 void UnregisterWithSenderIdImpl(const std::string& app_id, | |
| 50 const std::string& sender_id) override; | |
| 51 void SendImpl(const std::string& app_id, | |
| 52 const std::string& receiver_id, | |
| 53 const OutgoingMessage& message) override; | |
| 54 | |
| 55 private: | |
| 56 FakeGCMProfileService* service_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver); | |
| 59 }; | |
| 60 | |
| 61 CustomFakeGCMDriver::CustomFakeGCMDriver(FakeGCMProfileService* service) | |
| 62 : FakeGCMDriver(base::ThreadTaskRunnerHandle::Get()), | |
| 63 service_(service) { | |
| 64 } | |
| 65 | |
| 66 CustomFakeGCMDriver::~CustomFakeGCMDriver() { | |
| 67 } | |
| 68 | |
| 69 void CustomFakeGCMDriver::RegisterImpl( | |
| 70 const std::string& app_id, | |
| 71 const std::vector<std::string>& sender_ids) { | |
| 72 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 73 FROM_HERE, base::Bind(&FakeGCMProfileService::RegisterFinished, | |
| 74 base::Unretained(service_), app_id, sender_ids)); | |
| 75 } | |
| 76 | |
| 77 void CustomFakeGCMDriver::UnregisterImpl(const std::string& app_id) { | |
| 78 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 79 FROM_HERE, base::Bind(&FakeGCMProfileService::UnregisterFinished, | |
| 80 base::Unretained(service_), app_id)); | |
| 81 } | |
| 82 | |
| 83 void CustomFakeGCMDriver::UnregisterWithSenderIdImpl( | |
| 84 const std::string& app_id, | |
| 85 const std::string& sender_id) {} | |
| 86 | |
| 87 void CustomFakeGCMDriver::SendImpl(const std::string& app_id, | |
| 88 const std::string& receiver_id, | |
| 89 const OutgoingMessage& message) { | |
| 90 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 91 FROM_HERE, | |
| 92 base::Bind(&FakeGCMProfileService::SendFinished, | |
| 93 base::Unretained(service_), app_id, receiver_id, message)); | |
| 94 } | |
| 95 | |
| 96 void CustomFakeGCMDriver::OnRegisterFinished( | |
| 97 const std::string& app_id, | |
| 98 const std::string& registration_id, | |
| 99 GCMClient::Result result) { | |
| 100 RegisterFinished(app_id, registration_id, result); | |
| 101 } | |
| 102 | |
| 103 void CustomFakeGCMDriver::OnUnregisterFinished(const std::string& app_id, | |
| 104 GCMClient::Result result) { | |
| 105 UnregisterFinished(app_id, result); | |
| 106 } | |
| 107 | |
| 108 void CustomFakeGCMDriver::OnSendFinished(const std::string& app_id, | |
| 109 const std::string& message_id, | |
| 110 GCMClient::Result result) { | |
| 111 SendFinished(app_id, message_id, result); | |
| 112 } | |
| 113 | |
| 114 void CustomFakeGCMDriver::OnDispatchMessage(const std::string& app_id, | |
| 115 const IncomingMessage& message) { | |
| 116 DispatchMessage(app_id, message); | |
| 117 } | |
| 118 | |
| 119 } // namespace | |
| 120 | |
| 121 // static | |
| 122 std::unique_ptr<KeyedService> FakeGCMProfileService::Build( | |
| 123 content::BrowserContext* context) { | |
| 124 Profile* profile = static_cast<Profile*>(context); | |
| 125 std::unique_ptr<FakeGCMProfileService> service( | |
| 126 new FakeGCMProfileService(profile)); | |
| 127 service->SetDriverForTesting(new CustomFakeGCMDriver(service.get())); | |
| 128 return std::move(service); | |
| 129 } | |
| 130 | |
| 131 FakeGCMProfileService::FakeGCMProfileService(Profile* profile) | |
| 132 : collect_(false), | |
| 133 registration_count_(0) { | |
| 134 } | |
| 135 | |
| 136 FakeGCMProfileService::~FakeGCMProfileService() {} | |
| 137 | |
| 138 void FakeGCMProfileService::RegisterFinished( | |
| 139 const std::string& app_id, | |
| 140 const std::vector<std::string>& sender_ids) { | |
| 141 if (collect_) { | |
| 142 last_registered_app_id_ = app_id; | |
| 143 last_registered_sender_ids_ = sender_ids; | |
| 144 } | |
| 145 | |
| 146 // Generate fake registration IDs, encoding the number of sender IDs (used by | |
| 147 // GcmApiTest.RegisterValidation), then an incrementing count (even for the | |
| 148 // same app_id - there's no caching) so tests can distinguish registrations. | |
| 149 std::string registration_id = base::StringPrintf("%" PRIuS "-%d", | |
| 150 sender_ids.size(), | |
| 151 registration_count_); | |
| 152 ++registration_count_; | |
| 153 | |
| 154 CustomFakeGCMDriver* custom_driver = | |
| 155 static_cast<CustomFakeGCMDriver*>(driver()); | |
| 156 custom_driver->OnRegisterFinished( | |
| 157 app_id, registration_id, GCMClient::SUCCESS); | |
| 158 } | |
| 159 | |
| 160 void FakeGCMProfileService::UnregisterFinished(const std::string& app_id) { | |
| 161 GCMClient::Result result = GCMClient::SUCCESS; | |
| 162 if (!unregister_responses_.empty()) { | |
| 163 result = unregister_responses_.front(); | |
| 164 unregister_responses_.pop_front(); | |
| 165 } | |
| 166 | |
| 167 CustomFakeGCMDriver* custom_driver = | |
| 168 static_cast<CustomFakeGCMDriver*>(driver()); | |
| 169 custom_driver->OnUnregisterFinished(app_id, result); | |
| 170 | |
| 171 if (!unregister_callback_.is_null()) | |
| 172 unregister_callback_.Run(app_id); | |
| 173 } | |
| 174 | |
| 175 void FakeGCMProfileService::SendFinished(const std::string& app_id, | |
| 176 const std::string& receiver_id, | |
| 177 const OutgoingMessage& message) { | |
| 178 if (collect_) { | |
| 179 last_sent_message_ = message; | |
| 180 last_receiver_id_ = receiver_id; | |
| 181 } | |
| 182 | |
| 183 CustomFakeGCMDriver* custom_driver = | |
| 184 static_cast<CustomFakeGCMDriver*>(driver()); | |
| 185 custom_driver->OnSendFinished(app_id, message.id, GCMClient::SUCCESS); | |
| 186 } | |
| 187 | |
| 188 void FakeGCMProfileService::AddExpectedUnregisterResponse( | |
| 189 GCMClient::Result result) { | |
| 190 unregister_responses_.push_back(result); | |
| 191 } | |
| 192 | |
| 193 void FakeGCMProfileService::SetUnregisterCallback( | |
| 194 const UnregisterCallback& callback) { | |
| 195 unregister_callback_ = callback; | |
| 196 } | |
| 197 | |
| 198 void FakeGCMProfileService::DispatchMessage(const std::string& app_id, | |
| 199 const IncomingMessage& message) { | |
| 200 CustomFakeGCMDriver* custom_driver = | |
| 201 static_cast<CustomFakeGCMDriver*>(driver()); | |
| 202 custom_driver->OnDispatchMessage(app_id, message); | |
| 203 } | |
| 204 | |
| 205 } // namespace gcm | |
| OLD | NEW |