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/id_mapping_helper.h"
      6 
      7 #include "base/rand_util.h"
      8 #include "base/strings/string_number_conversions.h"
      9 #include "base/values.h"
     10 
     11 #include "chrome/browser/sync/glue/device_info.h"
     12 
     13 using base::DictionaryValue;
     14 using base::Value;
     15 using browser_sync::DeviceInfo;
     16 
     17 namespace extensions {
     18 
     19 std::string GetPublicIdFromGUID(
     20     const base::DictionaryValue& id_mapping,
     21     const std::string& guid) {
     22   for (DictionaryValue::Iterator it(id_mapping);
     23        !it.IsAtEnd();
     24        it.Advance()) {
     25     const Value& value = it.value();
     26     std::string guid_in_value;
     27     if (!value.GetAsString(&guid_in_value)) {
     28       LOG(ERROR) << "Badly formatted dictionary";
     29       continue;
     30     }
     31     if (guid_in_value == guid) {
     32       return it.key();
     33     }
     34   }
     35 
     36   return std::string();
     37 }
     38 
     39 std::string GetGUIDFromPublicId(
     40     const base::DictionaryValue& id_mapping,
     41     const std::string& id) {
     42   std::string guid;
     43   id_mapping.GetString(id, &guid);
     44   return guid;
     45 }
     46 
     47 // Finds out a random unused id. First finds a random id.
     48 // If the id is in use, increments the id until it finds an unused id.
     49 std::string GetRandomId(
     50   const DictionaryValue& mapping,
     51   int device_count) {
     52   // Set the max value for rand to be twice the device count.
     53   int max = device_count * 2;
     54   int rand_value = base::RandInt(0, max);
     55   std::string string_value;
     56   const Value *out_value;
     57 
     58   do {
     59     string_value = base::IntToString(rand_value);
     60     rand_value++;
     61   } while (mapping.Get(string_value, &out_value));
     62 
     63   return string_value;
     64 }
     65 
     66 void CreateMappingForUnmappedDevices(
     67     std::vector<browser_sync::DeviceInfo*>* device_info,
     68     base::DictionaryValue* value) {
     69   for (unsigned int i = 0; i < device_info->size(); ++i) {
     70     DeviceInfo* device = (*device_info)[i];
     71     std::string local_id = GetPublicIdFromGUID(*value,
     72                                                device->guid());
     73 
     74     // If the device does not have a local id, set one.
     75     if (local_id.empty()) {
     76       local_id = GetRandomId(*value, device_info->size());
     77       value->SetString(local_id, device->guid());
     78     }
     79     device->set_public_id(local_id);
     80   }
     81 }
     82 
     83 }  // namespace  extensions
     84