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 | 4 |
5 #include "components/user_prefs/user_prefs.h" | 5 #include "components/user_prefs/user_prefs.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ptr_util.h" |
8 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
9 #include "components/prefs/pref_service.h" | 10 #include "components/prefs/pref_service.h" |
10 | 11 |
11 namespace user_prefs { | 12 namespace user_prefs { |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 void* UserDataKey() { | 16 void* UserDataKey() { |
16 // We just need a unique constant. Use the address of a static that | 17 // We just need a unique constant. Use the address of a static that |
17 // COMDAT folding won't touch in an optimizing linker. | 18 // COMDAT folding won't touch in an optimizing linker. |
18 static int data_key = 0; | 19 static int data_key = 0; |
19 return reinterpret_cast<void*>(&data_key); | 20 return reinterpret_cast<void*>(&data_key); |
20 } | 21 } |
21 | 22 |
22 } // namespace | 23 } // namespace |
23 | 24 |
24 // static | 25 // static |
25 PrefService* UserPrefs::Get(base::SupportsUserData* context) { | 26 PrefService* UserPrefs::Get(base::SupportsUserData* context) { |
26 DCHECK(context); | 27 DCHECK(context); |
27 DCHECK(context->GetUserData(UserDataKey())); | 28 DCHECK(context->GetUserData(UserDataKey())); |
28 return static_cast<UserPrefs*>( | 29 return static_cast<UserPrefs*>( |
29 context->GetUserData(UserDataKey()))->prefs_; | 30 context->GetUserData(UserDataKey()))->prefs_; |
30 } | 31 } |
31 | 32 |
32 // static | 33 // static |
33 void UserPrefs::Set(base::SupportsUserData* context, PrefService* prefs) { | 34 void UserPrefs::Set(base::SupportsUserData* context, PrefService* prefs) { |
34 DCHECK(context); | 35 DCHECK(context); |
35 DCHECK(prefs); | 36 DCHECK(prefs); |
36 DCHECK(!context->GetUserData(UserDataKey())); | 37 DCHECK(!context->GetUserData(UserDataKey())); |
37 context->SetUserData(UserDataKey(), new UserPrefs(prefs)); | 38 context->SetUserData(UserDataKey(), base::WrapUnique(new UserPrefs(prefs))); |
38 } | 39 } |
39 | 40 |
40 UserPrefs::UserPrefs(PrefService* prefs) : prefs_(prefs) { | 41 UserPrefs::UserPrefs(PrefService* prefs) : prefs_(prefs) { |
41 } | 42 } |
42 | 43 |
43 UserPrefs::~UserPrefs() { | 44 UserPrefs::~UserPrefs() { |
44 } | 45 } |
45 | 46 |
46 } // namespace user_prefs | 47 } // namespace user_prefs |
OLD | NEW |