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