1 // Copyright 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 #include "chrome/browser/media/media_device_id_salt.h" 5 6 #include "base/base64.h" 7 #include "base/prefs/pref_service.h" 8 #include "base/rand_util.h" 9 #include "chrome/browser/profiles/profile_io_data.h" 10 #include "chrome/common/pref_names.h" 11 #include "content/public/browser/browser_thread.h" 12 13 using content::BrowserThread; 14 15 namespace { 16 CreateSalt()17std::string CreateSalt() { 18 std::string salt; 19 base::Base64Encode(base::RandBytesAsString(16), &salt); 20 DCHECK(!salt.empty()); 21 return salt; 22 } 23 24 } // namespace 25 MediaDeviceIDSalt(PrefService * pref_service,bool incognito)26MediaDeviceIDSalt::MediaDeviceIDSalt(PrefService* pref_service, 27 bool incognito) { 28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 29 30 if (incognito) { 31 incognito_salt_ = CreateSalt(); 32 return; 33 } 34 35 media_device_id_salt_.Init(prefs::kMediaDeviceIdSalt, pref_service); 36 if (media_device_id_salt_.GetValue().empty()) 37 media_device_id_salt_.SetValue(CreateSalt()); 38 39 media_device_id_salt_.MoveToThread( 40 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); 41 } 42 ~MediaDeviceIDSalt()43MediaDeviceIDSalt::~MediaDeviceIDSalt() { 44 } 45 ShutdownOnUIThread()46void MediaDeviceIDSalt::ShutdownOnUIThread() { 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 48 if (incognito_salt_.empty()) 49 media_device_id_salt_.Destroy(); 50 } 51 GetSalt() const52std::string MediaDeviceIDSalt::GetSalt() const { 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 54 if (incognito_salt_.size()) 55 return incognito_salt_; 56 return media_device_id_salt_.GetValue(); 57 } 58 RegisterProfilePrefs(user_prefs::PrefRegistrySyncable * registry)59void MediaDeviceIDSalt::RegisterProfilePrefs( 60 user_prefs::PrefRegistrySyncable* registry) { 61 registry->RegisterStringPref( 62 prefs::kMediaDeviceIdSalt, 63 std::string(), 64 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 65 } 66 Reset(PrefService * pref_service)67void MediaDeviceIDSalt::Reset(PrefService* pref_service) { 68 pref_service->SetString(prefs::kMediaDeviceIdSalt, 69 CreateSalt()); 70 } 71