OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.h" | 5 #include "components/gcm_driver/instance_id/fake_gcm_driver_for_instance_id.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/rand_util.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "components/gcm_driver/gcm_client.h" |
9 | 12 |
10 namespace instance_id { | 13 namespace instance_id { |
11 | 14 |
12 FakeGCMDriverForInstanceID::FakeGCMDriverForInstanceID() { | 15 FakeGCMDriverForInstanceID::FakeGCMDriverForInstanceID() { |
13 } | 16 } |
14 | 17 |
15 FakeGCMDriverForInstanceID::~FakeGCMDriverForInstanceID() { | 18 FakeGCMDriverForInstanceID::~FakeGCMDriverForInstanceID() { |
16 } | 19 } |
17 | 20 |
18 gcm::InstanceIDStore* FakeGCMDriverForInstanceID::GetInstanceIDStore() { | 21 gcm::InstanceIDHandler* FakeGCMDriverForInstanceID::GetInstanceIDHandler() { |
19 return this; | 22 return this; |
20 } | 23 } |
21 | 24 |
22 void FakeGCMDriverForInstanceID::AddInstanceIDData( | 25 void FakeGCMDriverForInstanceID::AddInstanceIDData( |
23 const std::string& app_id, | 26 const std::string& app_id, |
24 const std::string& instance_id_data) { | 27 const std::string& instance_id, |
25 instance_id_data_[app_id] = instance_id_data; | 28 const std::string& extra_data) { |
| 29 instance_id_data_[app_id] = std::make_pair(instance_id, extra_data); |
26 } | 30 } |
27 | 31 |
28 void FakeGCMDriverForInstanceID::RemoveInstanceIDData( | 32 void FakeGCMDriverForInstanceID::RemoveInstanceIDData( |
29 const std::string& app_id) { | 33 const std::string& app_id) { |
30 instance_id_data_.erase(app_id); | 34 instance_id_data_.erase(app_id); |
31 } | 35 } |
32 | 36 |
33 void FakeGCMDriverForInstanceID::GetInstanceIDData( | 37 void FakeGCMDriverForInstanceID::GetInstanceIDData( |
34 const std::string& app_id, | 38 const std::string& app_id, |
35 const gcm::InstanceIDStore::GetInstanceIDDataCallback& callback) { | 39 const GetInstanceIDDataCallback& callback) { |
36 std::string data; | |
37 auto iter = instance_id_data_.find(app_id); | 40 auto iter = instance_id_data_.find(app_id); |
38 if (iter != instance_id_data_.end()) | 41 std::string instance_id; |
39 data = iter->second; | 42 std::string extra_data; |
| 43 if (iter != instance_id_data_.end()) { |
| 44 instance_id = iter->second.first; |
| 45 extra_data = iter->second.second; |
| 46 } |
40 base::MessageLoop::current()->PostTask( | 47 base::MessageLoop::current()->PostTask( |
41 FROM_HERE, | 48 FROM_HERE, |
42 base::Bind(callback, data)); | 49 base::Bind(callback, instance_id, extra_data)); |
| 50 } |
| 51 |
| 52 void FakeGCMDriverForInstanceID::GetToken( |
| 53 const std::string& app_id, |
| 54 const std::string& authorized_entity, |
| 55 const std::string& scope, |
| 56 const std::map<std::string, std::string>& options, |
| 57 const GetTokenCallback& callback) { |
| 58 std::string token; |
| 59 std::string key = app_id + authorized_entity + scope; |
| 60 auto iter = tokens_.find(key); |
| 61 if (iter != tokens_.end()) { |
| 62 token = iter->second; |
| 63 } else { |
| 64 token = base::Uint64ToString(base::RandUint64()); |
| 65 tokens_[key] = token; |
| 66 } |
| 67 |
| 68 base::MessageLoop::current()->PostTask( |
| 69 FROM_HERE, |
| 70 base::Bind(callback, token, gcm::GCMClient::SUCCESS)); |
| 71 } |
| 72 |
| 73 void FakeGCMDriverForInstanceID::DeleteToken( |
| 74 const std::string& app_id, |
| 75 const std::string& authorized_entity, |
| 76 const std::string& scope, |
| 77 const DeleteTokenCallback& callback) { |
| 78 std::string key = app_id + authorized_entity + scope; |
| 79 tokens_.erase(key); |
| 80 base::MessageLoop::current()->PostTask( |
| 81 FROM_HERE, |
| 82 base::Bind(callback, gcm::GCMClient::SUCCESS)); |
43 } | 83 } |
44 | 84 |
45 } // namespace instance_id | 85 } // namespace instance_id |
OLD | NEW |