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.
5 #include "ios/web/public/browser_state.h"
7 #include "base/memory/ref_counted.h"
8 #include "ios/web/public/certificate_policy_cache.h"
9 #include "ios/web/public/web_thread.h"
13 // Private key used for safe conversion of base::SupportsUserData to
14 // web::BrowserState in web::BrowserState::FromSupportsUserData.
15 const char kBrowserStateIdentifierKey
[] = "BrowserStateIdentifierKey";
17 const char kCertificatePolicyCacheKeyName
[] = "cert_policy_cache";
19 // Wraps a CertificatePolicyCache as a SupportsUserData::Data; this is necessary
20 // since reference counted objects can't be user data.
21 struct CertificatePolicyCacheHandle
: public base::SupportsUserData::Data
{
22 explicit CertificatePolicyCacheHandle(CertificatePolicyCache
* cache
)
23 : policy_cache(cache
) {}
25 scoped_refptr
<CertificatePolicyCache
> policy_cache
;
30 scoped_refptr
<CertificatePolicyCache
> BrowserState::GetCertificatePolicyCache(
31 BrowserState
* browser_state
) {
32 DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI
);
33 if (!browser_state
->GetUserData(kCertificatePolicyCacheKeyName
)) {
34 CertificatePolicyCacheHandle
* cert_cache_service_handle
=
35 new CertificatePolicyCacheHandle(new CertificatePolicyCache());
37 browser_state
->SetUserData(kCertificatePolicyCacheKeyName
,
38 cert_cache_service_handle
);
41 CertificatePolicyCacheHandle
* handle
=
42 static_cast<CertificatePolicyCacheHandle
*>(
43 browser_state
->GetUserData(kCertificatePolicyCacheKeyName
));
44 return handle
->policy_cache
;
47 BrowserState::BrowserState() {
48 // (Refcounted)?BrowserStateKeyedServiceFactories needs to be able to convert
49 // a base::SupportsUserData to a BrowserState. Moreover, since the factories
50 // may be passed a content::BrowserContext instead of a BrowserState, attach
51 // an empty object to this via a private key.
52 SetUserData(kBrowserStateIdentifierKey
, new SupportsUserData::Data
);
55 BrowserState::~BrowserState() {
59 BrowserState
* BrowserState::FromSupportsUserData(
60 base::SupportsUserData
* supports_user_data
) {
61 if (!supports_user_data
||
62 !supports_user_data
->GetUserData(kBrowserStateIdentifierKey
)) {
65 return static_cast<BrowserState
*>(supports_user_data
);