OLD | NEW |
---|---|
(Empty) | |
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/logging.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 | |
17 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.
| |
18 const size_t kSaltLength = 8; | |
19 static const char kAlphabet[] = "0123456789" | |
20 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
21 | |
22 std::string salt(kSaltLength, ' '); | |
23 for (size_t i = 0; i < salt.size(); ++i) { | |
24 int random_char = base::RandInt(0, sizeof(kAlphabet) - 1); | |
25 salt[i] = kAlphabet[random_char]; | |
26 } | |
27 return salt; | |
28 } | |
29 | |
30 } // namespace | |
31 | |
32 MediaDeviceIDSalt::MediaDeviceIDSalt(PrefService* pref_service, | |
33 bool incognito) { | |
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
35 | |
36 if (!incognito) { | |
37 media_device_id_salt_.Init(prefs::kMediaDeviceIdSalt, pref_service, | |
38 base::Bind(&MediaDeviceIDSalt::OnValueReset, | |
39 base::Unretained(this))); | |
40 salt_ = media_device_id_salt_.GetValue(); | |
41 } | |
42 | |
43 if (salt_.empty()) { | |
44 salt_ = CreateSalt(); | |
45 } | |
46 | |
47 if (!incognito) { | |
48 media_device_id_salt_.SetValue(salt_); | |
49 } | |
50 } | |
51 | |
52 MediaDeviceIDSalt::~MediaDeviceIDSalt() { | |
53 } | |
54 | |
55 const std::string& MediaDeviceIDSalt::GetSalt() const { | |
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
57 DVLOG(1) << "GetSalt " << salt_; | |
58 return salt_; | |
59 } | |
60 | |
61 void MediaDeviceIDSalt::RegisterProfilePrefs( | |
62 user_prefs::PrefRegistrySyncable* registry) { | |
63 registry->RegisterStringPref( | |
64 prefs::kMediaDeviceIdSalt, | |
65 std::string(), | |
66 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
67 } | |
68 | |
69 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
| |
70 pref_service->ClearPref(prefs::kMediaDeviceIdSalt); | |
71 } | |
72 | |
73 void MediaDeviceIDSalt::OnValueReset(const std::string& name) { | |
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
75 DVLOG(1) << "OnValueReset " << name; | |
76 media_device_id_salt_.SetValue(CreateSalt()); | |
77 BrowserThread::PostTask( | |
78 BrowserThread::IO, FROM_HERE, | |
79 base::Bind(&MediaDeviceIDSalt::OnValueResetOnIOThread, | |
80 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
| |
81 } | |
82 | |
83 void MediaDeviceIDSalt::OnValueResetOnIOThread(const std::string salt) { | |
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
85 DVLOG(1) << "OnValueResetOnIOThread " << salt; | |
86 salt_ = salt; | |
87 } | |
OLD | NEW |