Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / ios / web / browser_state.cc
blobd7321aea9390be2261ef9eef6aff0cd4cb2de0cf
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/location.h"
8 #include "base/memory/ref_counted.h"
9 #include "ios/web/public/certificate_policy_cache.h"
10 #include "ios/web/public/web_thread.h"
11 #include "ios/web/webui/url_data_manager_ios_backend.h"
13 namespace web {
14 namespace {
15 // Private key used for safe conversion of base::SupportsUserData to
16 // web::BrowserState in web::BrowserState::FromSupportsUserData.
17 const char kBrowserStateIdentifierKey[] = "BrowserStateIdentifierKey";
19 // Data key names.
20 const char kCertificatePolicyCacheKeyName[] = "cert_policy_cache";
22 // Wraps a CertificatePolicyCache as a SupportsUserData::Data; this is necessary
23 // since reference counted objects can't be user data.
24 struct CertificatePolicyCacheHandle : public base::SupportsUserData::Data {
25 explicit CertificatePolicyCacheHandle(CertificatePolicyCache* cache)
26 : policy_cache(cache) {}
28 scoped_refptr<CertificatePolicyCache> policy_cache;
32 // static
33 scoped_refptr<CertificatePolicyCache> BrowserState::GetCertificatePolicyCache(
34 BrowserState* browser_state) {
35 DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
36 if (!browser_state->GetUserData(kCertificatePolicyCacheKeyName)) {
37 CertificatePolicyCacheHandle* cert_cache_service_handle =
38 new CertificatePolicyCacheHandle(new CertificatePolicyCache());
40 browser_state->SetUserData(kCertificatePolicyCacheKeyName,
41 cert_cache_service_handle);
44 CertificatePolicyCacheHandle* handle =
45 static_cast<CertificatePolicyCacheHandle*>(
46 browser_state->GetUserData(kCertificatePolicyCacheKeyName));
47 return handle->policy_cache;
50 BrowserState::BrowserState() : url_data_manager_ios_backend_(nullptr) {
51 // (Refcounted)?BrowserStateKeyedServiceFactories needs to be able to convert
52 // a base::SupportsUserData to a BrowserState. Moreover, since the factories
53 // may be passed a content::BrowserContext instead of a BrowserState, attach
54 // an empty object to this via a private key.
55 SetUserData(kBrowserStateIdentifierKey, new SupportsUserData::Data);
58 BrowserState::~BrowserState() {
59 // Delete the URLDataManagerIOSBackend instance on the IO thread if it has
60 // been created. Note that while this check can theoretically race with a
61 // call to |GetURLDataManagerIOSBackendOnIOThread()|, if any clients of this
62 // BrowserState are still accessing it on the IO thread at this point,
63 // they're going to have a bad time anyway.
64 if (url_data_manager_ios_backend_) {
65 bool posted = web::WebThread::DeleteSoon(web::WebThread::IO, FROM_HERE,
66 url_data_manager_ios_backend_);
67 if (!posted)
68 delete url_data_manager_ios_backend_;
72 URLDataManagerIOSBackend*
73 BrowserState::GetURLDataManagerIOSBackendOnIOThread() {
74 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::IO);
75 if (!url_data_manager_ios_backend_)
76 url_data_manager_ios_backend_ = new URLDataManagerIOSBackend();
77 return url_data_manager_ios_backend_;
80 // static
81 BrowserState* BrowserState::FromSupportsUserData(
82 base::SupportsUserData* supports_user_data) {
83 if (!supports_user_data ||
84 !supports_user_data->GetUserData(kBrowserStateIdentifierKey)) {
85 return nullptr;
87 return static_cast<BrowserState*>(supports_user_data);
89 } // namespace web