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