Home | History | Annotate | Download | only in signedin_devices
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/extensions/api/signedin_devices/signedin_devices_api.h"
      6 
      7 #include "base/memory/scoped_ptr.h"
      8 #include "base/memory/scoped_vector.h"
      9 #include "base/values.h"
     10 #include "chrome/browser/extensions/api/signedin_devices/id_mapping_helper.h"
     11 #include "chrome/browser/extensions/extension_prefs.h"
     12 #include "chrome/browser/profiles/profile.h"
     13 #include "chrome/browser/sync/glue/device_info.h"
     14 #include "chrome/browser/sync/profile_sync_service.h"
     15 #include "chrome/browser/sync/profile_sync_service_factory.h"
     16 
     17 using base::DictionaryValue;
     18 using browser_sync::DeviceInfo;
     19 
     20 namespace extensions {
     21 
     22 static const char kPrefStringForIdMapping[] = "id_mapping_dictioanry";
     23 
     24 // Gets the dictionary that stores the id mapping. The dictionary is stored
     25 // in the |ExtensionPrefs|.
     26 const base::DictionaryValue* GetIdMappingDictionary(
     27     ExtensionPrefs* extension_prefs,
     28     const std::string& extension_id) {
     29   const base::DictionaryValue* out_value = NULL;
     30   if (!extension_prefs->ReadPrefAsDictionary(
     31           extension_id,
     32           kPrefStringForIdMapping,
     33           &out_value) || out_value == NULL) {
     34     // Looks like this is the first call to get the dictionary. Let us create
     35     // a dictionary and set it in to |extension_prefs|.
     36     scoped_ptr<DictionaryValue> dictionary(new DictionaryValue());
     37     out_value = dictionary.get();
     38     extension_prefs->UpdateExtensionPref(
     39         extension_id,
     40         kPrefStringForIdMapping,
     41         dictionary.release());
     42   }
     43 
     44   return out_value;
     45 }
     46 
     47 // Helper routine to get all signed in devices. The helper takes in
     48 // the pointers for |ProfileSyncService| and |Extensionprefs|. This
     49 // makes it easier to test by passing mock values for these pointers.
     50 ScopedVector<DeviceInfo> GetAllSignedInDevices(
     51     const std::string& extension_id,
     52     ProfileSyncService* pss,
     53     ExtensionPrefs* extension_prefs) {
     54   ScopedVector<DeviceInfo> devices = pss->GetAllSignedInDevices();
     55   const DictionaryValue* mapping_dictionary = GetIdMappingDictionary(
     56       extension_prefs,
     57       extension_id);
     58 
     59   CHECK(mapping_dictionary);
     60 
     61   // |mapping_dictionary| is const. So make an editable copy.
     62   scoped_ptr<DictionaryValue> editable_mapping_dictionary(
     63       mapping_dictionary->DeepCopy());
     64 
     65   CreateMappingForUnmappedDevices(&(devices.get()),
     66                                   editable_mapping_dictionary.get());
     67 
     68   // Write into |ExtensionPrefs| which will get persisted in disk.
     69   extension_prefs->UpdateExtensionPref(extension_id,
     70                                        kPrefStringForIdMapping,
     71                                        editable_mapping_dictionary.release());
     72   return devices.Pass();
     73 }
     74 
     75 ScopedVector<DeviceInfo> GetAllSignedInDevices(
     76     const std::string& extension_id,
     77     Profile* profile) {
     78   // Get the profile sync service and extension prefs pointers
     79   // and call the helper.
     80   ProfileSyncService* pss = ProfileSyncServiceFactory::GetForProfile(profile);
     81   ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile);
     82 
     83   return GetAllSignedInDevices(extension_id,
     84                                pss,
     85                                extension_prefs);
     86 }
     87 
     88 }  // namespace extensions
     89 
     90