OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/extensions/api/signed_in_devices/signed_in_devices_api.
h" | 5 #include "chrome/browser/extensions/api/signed_in_devices/signed_in_devices_api.
h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/memory/scoped_vector.h" | |
11 #include "base/values.h" | 10 #include "base/values.h" |
12 #include "chrome/browser/extensions/api/signed_in_devices/id_mapping_helper.h" | 11 #include "chrome/browser/extensions/api/signed_in_devices/id_mapping_helper.h" |
13 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/sync/profile_sync_service_factory.h" | 13 #include "chrome/browser/sync/profile_sync_service_factory.h" |
15 #include "chrome/common/extensions/api/signed_in_devices.h" | 14 #include "chrome/common/extensions/api/signed_in_devices.h" |
16 #include "components/browser_sync/browser/profile_sync_service.h" | 15 #include "components/browser_sync/browser/profile_sync_service.h" |
17 #include "components/sync/device_info/device_info_tracker.h" | 16 #include "components/sync/device_info/device_info_tracker.h" |
18 #include "components/sync/device_info/local_device_info_provider.h" | 17 #include "components/sync/device_info/local_device_info_provider.h" |
19 #include "extensions/browser/extension_prefs.h" | 18 #include "extensions/browser/extension_prefs.h" |
20 | 19 |
(...skipping 26 matching lines...) Expand all Loading... |
47 kPrefStringForIdMapping, | 46 kPrefStringForIdMapping, |
48 dictionary.release()); | 47 dictionary.release()); |
49 } | 48 } |
50 | 49 |
51 return out_value; | 50 return out_value; |
52 } | 51 } |
53 | 52 |
54 // Helper routine to get all signed in devices. The helper takes in | 53 // Helper routine to get all signed in devices. The helper takes in |
55 // the pointers for |DeviceInfoTracker| and |Extensionprefs|. This | 54 // the pointers for |DeviceInfoTracker| and |Extensionprefs|. This |
56 // makes it easier to test by passing mock values for these pointers. | 55 // makes it easier to test by passing mock values for these pointers. |
57 ScopedVector<DeviceInfo> GetAllSignedInDevices( | 56 std::vector<std::unique_ptr<DeviceInfo>> GetAllSignedInDevices( |
58 const std::string& extension_id, | 57 const std::string& extension_id, |
59 DeviceInfoTracker* device_tracker, | 58 DeviceInfoTracker* device_tracker, |
60 ExtensionPrefs* extension_prefs) { | 59 ExtensionPrefs* extension_prefs) { |
61 DCHECK(device_tracker); | 60 DCHECK(device_tracker); |
62 ScopedVector<DeviceInfo> devices = device_tracker->GetAllDeviceInfo(); | 61 std::vector<std::unique_ptr<DeviceInfo>> devices = |
| 62 device_tracker->GetAllDeviceInfo(); |
63 const base::DictionaryValue* mapping_dictionary = GetIdMappingDictionary( | 63 const base::DictionaryValue* mapping_dictionary = GetIdMappingDictionary( |
64 extension_prefs, | 64 extension_prefs, |
65 extension_id); | 65 extension_id); |
66 | 66 |
67 CHECK(mapping_dictionary); | 67 CHECK(mapping_dictionary); |
68 | 68 |
69 // |mapping_dictionary| is const. So make an editable copy. | 69 // |mapping_dictionary| is const. So make an editable copy. |
70 std::unique_ptr<base::DictionaryValue> editable_mapping_dictionary( | 70 std::unique_ptr<base::DictionaryValue> editable_mapping_dictionary( |
71 mapping_dictionary->DeepCopy()); | 71 mapping_dictionary->DeepCopy()); |
72 | 72 |
73 CreateMappingForUnmappedDevices(&(devices.get()), | 73 CreateMappingForUnmappedDevices(devices, editable_mapping_dictionary.get()); |
74 editable_mapping_dictionary.get()); | |
75 | 74 |
76 // Write into |ExtensionPrefs| which will get persisted in disk. | 75 // Write into |ExtensionPrefs| which will get persisted in disk. |
77 extension_prefs->UpdateExtensionPref(extension_id, | 76 extension_prefs->UpdateExtensionPref(extension_id, |
78 kPrefStringForIdMapping, | 77 kPrefStringForIdMapping, |
79 editable_mapping_dictionary.release()); | 78 editable_mapping_dictionary.release()); |
80 return devices; | 79 return devices; |
81 } | 80 } |
82 | 81 |
83 ScopedVector<DeviceInfo> GetAllSignedInDevices( | 82 std::vector<std::unique_ptr<DeviceInfo>> GetAllSignedInDevices( |
84 const std::string& extension_id, | 83 const std::string& extension_id, |
85 Profile* profile) { | 84 Profile* profile) { |
86 // Get the device tracker and extension prefs pointers | 85 // Get the device tracker and extension prefs pointers |
87 // and call the helper. | 86 // and call the helper. |
88 DeviceInfoTracker* device_tracker = | 87 DeviceInfoTracker* device_tracker = |
89 ProfileSyncServiceFactory::GetForProfile(profile)->GetDeviceInfoTracker(); | 88 ProfileSyncServiceFactory::GetForProfile(profile)->GetDeviceInfoTracker(); |
90 DCHECK(device_tracker); | 89 DCHECK(device_tracker); |
91 if (!device_tracker->IsSyncing()) { | 90 if (!device_tracker->IsSyncing()) { |
92 // Devices are not sync'ing. | 91 // Devices are not sync'ing. |
93 return ScopedVector<DeviceInfo>(); | 92 return std::vector<std::unique_ptr<DeviceInfo>>(); |
94 } | 93 } |
95 | 94 |
96 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile); | 95 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile); |
97 | 96 |
98 return GetAllSignedInDevices(extension_id, device_tracker, extension_prefs); | 97 return GetAllSignedInDevices(extension_id, device_tracker, extension_prefs); |
99 } | 98 } |
100 | 99 |
101 std::unique_ptr<DeviceInfo> GetLocalDeviceInfo(const std::string& extension_id, | 100 std::unique_ptr<DeviceInfo> GetLocalDeviceInfo(const std::string& extension_id, |
102 Profile* profile) { | 101 Profile* profile) { |
103 ProfileSyncService* pss = ProfileSyncServiceFactory::GetForProfile(profile); | 102 ProfileSyncService* pss = ProfileSyncServiceFactory::GetForProfile(profile); |
(...skipping 20 matching lines...) Expand all Loading... |
124 std::unique_ptr<DeviceInfo> device = | 123 std::unique_ptr<DeviceInfo> device = |
125 GetLocalDeviceInfo(extension_id(), GetProfile()); | 124 GetLocalDeviceInfo(extension_id(), GetProfile()); |
126 std::unique_ptr<base::ListValue> result(new base::ListValue()); | 125 std::unique_ptr<base::ListValue> result(new base::ListValue()); |
127 if (device.get()) { | 126 if (device.get()) { |
128 result->Append(device->ToValue()); | 127 result->Append(device->ToValue()); |
129 } | 128 } |
130 SetResult(std::move(result)); | 129 SetResult(std::move(result)); |
131 return true; | 130 return true; |
132 } | 131 } |
133 | 132 |
134 ScopedVector<DeviceInfo> devices = | 133 std::vector<std::unique_ptr<DeviceInfo>> devices = |
135 GetAllSignedInDevices(extension_id(), GetProfile()); | 134 GetAllSignedInDevices(extension_id(), GetProfile()); |
136 | 135 |
137 std::unique_ptr<base::ListValue> result(new base::ListValue()); | 136 std::unique_ptr<base::ListValue> result(new base::ListValue()); |
138 | 137 |
139 for (ScopedVector<DeviceInfo>::const_iterator it = devices.begin(); | 138 for (const std::unique_ptr<DeviceInfo>& device : devices) |
140 it != devices.end(); | 139 result->Append(device->ToValue()); |
141 ++it) { | |
142 result->Append((*it)->ToValue()); | |
143 } | |
144 | 140 |
145 SetResult(std::move(result)); | 141 SetResult(std::move(result)); |
146 return true; | 142 return true; |
147 } | 143 } |
148 | 144 |
149 } // namespace extensions | 145 } // namespace extensions |
150 | 146 |
OLD | NEW |