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