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 "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/extensions/api/signed_in_devices/id_mapping_helper.h" | 10 #include "chrome/browser/extensions/api/signed_in_devices/id_mapping_helper.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/sync/glue/device_info.h" | 12 #include "chrome/browser/sync/glue/device_info_tracker.h" |
13 #include "chrome/browser/sync/glue/local_device_info_provider.h" | 13 #include "chrome/browser/sync/glue/local_device_info_provider.h" |
14 #include "chrome/browser/sync/profile_sync_service.h" | 14 #include "chrome/browser/sync/profile_sync_service.h" |
15 #include "chrome/browser/sync/profile_sync_service_factory.h" | 15 #include "chrome/browser/sync/profile_sync_service_factory.h" |
16 #include "chrome/common/extensions/api/signed_in_devices.h" | 16 #include "chrome/common/extensions/api/signed_in_devices.h" |
17 #include "extensions/browser/extension_prefs.h" | 17 #include "extensions/browser/extension_prefs.h" |
18 | 18 |
19 using base::DictionaryValue; | 19 using base::DictionaryValue; |
20 using browser_sync::DeviceInfo; | 20 using browser_sync::DeviceInfo; |
| 21 using browser_sync::DeviceInfoTracker; |
21 using browser_sync::LocalDeviceInfoProvider; | 22 using browser_sync::LocalDeviceInfoProvider; |
22 | 23 |
23 namespace extensions { | 24 namespace extensions { |
24 | 25 |
25 static const char kPrefStringForIdMapping[] = "id_mapping_dictioanry"; | 26 static const char kPrefStringForIdMapping[] = "id_mapping_dictioanry"; |
26 | 27 |
27 // Gets the dictionary that stores the id mapping. The dictionary is stored | 28 // Gets the dictionary that stores the id mapping. The dictionary is stored |
28 // in the |ExtensionPrefs|. | 29 // in the |ExtensionPrefs|. |
29 const base::DictionaryValue* GetIdMappingDictionary( | 30 const base::DictionaryValue* GetIdMappingDictionary( |
30 ExtensionPrefs* extension_prefs, | 31 ExtensionPrefs* extension_prefs, |
(...skipping 10 matching lines...) Expand all Loading... |
41 extension_prefs->UpdateExtensionPref( | 42 extension_prefs->UpdateExtensionPref( |
42 extension_id, | 43 extension_id, |
43 kPrefStringForIdMapping, | 44 kPrefStringForIdMapping, |
44 dictionary.release()); | 45 dictionary.release()); |
45 } | 46 } |
46 | 47 |
47 return out_value; | 48 return out_value; |
48 } | 49 } |
49 | 50 |
50 // Helper routine to get all signed in devices. The helper takes in | 51 // Helper routine to get all signed in devices. The helper takes in |
51 // the pointers for |ProfileSyncService| and |Extensionprefs|. This | 52 // the pointers for |DeviceInfoTracker| and |Extensionprefs|. This |
52 // makes it easier to test by passing mock values for these pointers. | 53 // makes it easier to test by passing mock values for these pointers. |
53 ScopedVector<DeviceInfo> GetAllSignedInDevices( | 54 ScopedVector<DeviceInfo> GetAllSignedInDevices( |
54 const std::string& extension_id, | 55 const std::string& extension_id, |
55 ProfileSyncService* pss, | 56 DeviceInfoTracker* device_tracker, |
56 ExtensionPrefs* extension_prefs) { | 57 ExtensionPrefs* extension_prefs) { |
57 ScopedVector<DeviceInfo> devices = pss->GetAllSignedInDevices(); | 58 DCHECK(device_tracker); |
| 59 ScopedVector<DeviceInfo> devices = device_tracker->GetAllDeviceInfo(); |
58 const base::DictionaryValue* mapping_dictionary = GetIdMappingDictionary( | 60 const base::DictionaryValue* mapping_dictionary = GetIdMappingDictionary( |
59 extension_prefs, | 61 extension_prefs, |
60 extension_id); | 62 extension_id); |
61 | 63 |
62 CHECK(mapping_dictionary); | 64 CHECK(mapping_dictionary); |
63 | 65 |
64 // |mapping_dictionary| is const. So make an editable copy. | 66 // |mapping_dictionary| is const. So make an editable copy. |
65 scoped_ptr<base::DictionaryValue> editable_mapping_dictionary( | 67 scoped_ptr<base::DictionaryValue> editable_mapping_dictionary( |
66 mapping_dictionary->DeepCopy()); | 68 mapping_dictionary->DeepCopy()); |
67 | 69 |
68 CreateMappingForUnmappedDevices(&(devices.get()), | 70 CreateMappingForUnmappedDevices(&(devices.get()), |
69 editable_mapping_dictionary.get()); | 71 editable_mapping_dictionary.get()); |
70 | 72 |
71 // Write into |ExtensionPrefs| which will get persisted in disk. | 73 // Write into |ExtensionPrefs| which will get persisted in disk. |
72 extension_prefs->UpdateExtensionPref(extension_id, | 74 extension_prefs->UpdateExtensionPref(extension_id, |
73 kPrefStringForIdMapping, | 75 kPrefStringForIdMapping, |
74 editable_mapping_dictionary.release()); | 76 editable_mapping_dictionary.release()); |
75 return devices.Pass(); | 77 return devices.Pass(); |
76 } | 78 } |
77 | 79 |
78 ScopedVector<DeviceInfo> GetAllSignedInDevices( | 80 ScopedVector<DeviceInfo> GetAllSignedInDevices( |
79 const std::string& extension_id, | 81 const std::string& extension_id, |
80 Profile* profile) { | 82 Profile* profile) { |
81 // Get the profile sync service and extension prefs pointers | 83 // Get the device tracker and extension prefs pointers |
82 // and call the helper. | 84 // and call the helper. |
83 ProfileSyncService* pss = ProfileSyncServiceFactory::GetForProfile(profile); | 85 DeviceInfoTracker* device_tracker = |
| 86 ProfileSyncServiceFactory::GetForProfile(profile)->GetDeviceInfoTracker(); |
| 87 if (device_tracker == NULL) { |
| 88 // Devices are not sync'ing. |
| 89 return ScopedVector<DeviceInfo>().Pass(); |
| 90 } |
| 91 |
84 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile); | 92 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile); |
85 | 93 |
86 return GetAllSignedInDevices(extension_id, | 94 return GetAllSignedInDevices(extension_id, device_tracker, extension_prefs); |
87 pss, | |
88 extension_prefs); | |
89 } | 95 } |
90 | 96 |
91 scoped_ptr<DeviceInfo> GetLocalDeviceInfo(const std::string& extension_id, | 97 scoped_ptr<DeviceInfo> GetLocalDeviceInfo(const std::string& extension_id, |
92 Profile* profile) { | 98 Profile* profile) { |
93 ProfileSyncService* pss = ProfileSyncServiceFactory::GetForProfile(profile); | 99 ProfileSyncService* pss = ProfileSyncServiceFactory::GetForProfile(profile); |
94 if (!pss) { | 100 if (!pss) { |
95 return scoped_ptr<DeviceInfo>(); | 101 return scoped_ptr<DeviceInfo>(); |
96 } | 102 } |
97 | 103 |
98 LocalDeviceInfoProvider* local_device = pss->GetLocalDeviceInfoProvider(); | 104 LocalDeviceInfoProvider* local_device = pss->GetLocalDeviceInfoProvider(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 ++it) { | 138 ++it) { |
133 result->Append((*it)->ToValue()); | 139 result->Append((*it)->ToValue()); |
134 } | 140 } |
135 | 141 |
136 SetResult(result.release()); | 142 SetResult(result.release()); |
137 return true; | 143 return true; |
138 } | 144 } |
139 | 145 |
140 } // namespace extensions | 146 } // namespace extensions |
141 | 147 |
OLD | NEW |