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