Index: chrome/browser/media/media_device_id_salt.cc |
diff --git a/chrome/browser/media/media_device_id_salt.cc b/chrome/browser/media/media_device_id_salt.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..18288cc3435c30386b7eafbd1c70a013dc0565e3 |
--- /dev/null |
+++ b/chrome/browser/media/media_device_id_salt.cc |
@@ -0,0 +1,87 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+#include "chrome/browser/media/media_device_id_salt.h" |
+ |
+#include "base/logging.h" |
+#include "base/prefs/pref_service.h" |
+#include "base/rand_util.h" |
+#include "chrome/browser/profiles/profile_io_data.h" |
+#include "chrome/common/pref_names.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+using content::BrowserThread; |
+ |
+namespace { |
+ |
+std::string CreateSalt() { |
Jói
2013/10/31 16:14:47
I would be surprised if we don't already have a fu
perkj_chrome
2013/10/31 17:05:44
Yes- but it turned out that crypto::RandBytes in r
Jói
2013/10/31 17:19:34
OK.
|
+ const size_t kSaltLength = 8; |
+ static const char kAlphabet[] = "0123456789" |
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
+ |
+ std::string salt(kSaltLength, ' '); |
+ for (size_t i = 0; i < salt.size(); ++i) { |
+ int random_char = base::RandInt(0, sizeof(kAlphabet) - 1); |
+ salt[i] = kAlphabet[random_char]; |
+ } |
+ return salt; |
+} |
+ |
+} // namespace |
+ |
+MediaDeviceIDSalt::MediaDeviceIDSalt(PrefService* pref_service, |
+ bool incognito) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ |
+ if (!incognito) { |
+ media_device_id_salt_.Init(prefs::kMediaDeviceIdSalt, pref_service, |
+ base::Bind(&MediaDeviceIDSalt::OnValueReset, |
+ base::Unretained(this))); |
+ salt_ = media_device_id_salt_.GetValue(); |
+ } |
+ |
+ if (salt_.empty()) { |
+ salt_ = CreateSalt(); |
+ } |
+ |
+ if (!incognito) { |
+ media_device_id_salt_.SetValue(salt_); |
+ } |
+} |
+ |
+MediaDeviceIDSalt::~MediaDeviceIDSalt() { |
+} |
+ |
+const std::string& MediaDeviceIDSalt::GetSalt() const { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DVLOG(1) << "GetSalt " << salt_; |
+ return salt_; |
+} |
+ |
+void MediaDeviceIDSalt::RegisterProfilePrefs( |
+ user_prefs::PrefRegistrySyncable* registry) { |
+ registry->RegisterStringPref( |
+ prefs::kMediaDeviceIdSalt, |
+ std::string(), |
+ user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
+} |
+ |
+void MediaDeviceIDSalt::Reset(PrefService* pref_service) { |
Jói
2013/10/31 16:14:47
I was a bit surprised this was static, but I see n
perkj_chrome
2013/10/31 17:05:44
Sure- that would simplify things even more.
How wo
Jói
2013/10/31 17:19:34
ProfileIOData::FromResourceContext (and you can do
|
+ pref_service->ClearPref(prefs::kMediaDeviceIdSalt); |
+} |
+ |
+void MediaDeviceIDSalt::OnValueReset(const std::string& name) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ DVLOG(1) << "OnValueReset " << name; |
+ media_device_id_salt_.SetValue(CreateSalt()); |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ base::Bind(&MediaDeviceIDSalt::OnValueResetOnIOThread, |
+ base::Unretained(this), media_device_id_salt_.GetValue())); |
perkj_chrome
2013/10/31 15:31:48
Is Unretained OK here or should MediaDeviceSalt be
Jói
2013/10/31 16:14:47
Good question. I think it may be OK since probably
|
+} |
+ |
+void MediaDeviceIDSalt::OnValueResetOnIOThread(const std::string salt) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DVLOG(1) << "OnValueResetOnIOThread " << salt; |
+ salt_ = salt; |
+} |