BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / ios / web / browser_state.mm
blob510140d2f898b86b8be01ab9ef38d4d572da88d1
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/active_state_manager_impl.h"
10 #import "ios/web/browsing_data_partition_impl.h"
11 #include "ios/web/public/certificate_policy_cache.h"
12 #include "ios/web/public/web_thread.h"
13 #include "ios/web/webui/url_data_manager_ios_backend.h"
15 namespace web {
16 namespace {
17 // Private key used for safe conversion of base::SupportsUserData to
18 // web::BrowserState in web::BrowserState::FromSupportsUserData.
19 const char kBrowserStateIdentifierKey[] = "BrowserStateIdentifierKey";
21 // Data key names.
22 const char kCertificatePolicyCacheKeyName[] = "cert_policy_cache";
23 const char kActiveStateManagerKeyName[] = "active_state_manager";
24 const char kBrowsingDataPartitionKeyName[] = "browsing_data_partition";
26 // Wraps a CertificatePolicyCache as a SupportsUserData::Data; this is necessary
27 // since reference counted objects can't be user data.
28 struct CertificatePolicyCacheHandle : public base::SupportsUserData::Data {
29   explicit CertificatePolicyCacheHandle(CertificatePolicyCache* cache)
30       : policy_cache(cache) {}
32   scoped_refptr<CertificatePolicyCache> policy_cache;
36 // static
37 scoped_refptr<CertificatePolicyCache> BrowserState::GetCertificatePolicyCache(
38     BrowserState* browser_state) {
39   DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
40   if (!browser_state->GetUserData(kCertificatePolicyCacheKeyName)) {
41     CertificatePolicyCacheHandle* cert_cache_service_handle =
42         new CertificatePolicyCacheHandle(new CertificatePolicyCache());
44     browser_state->SetUserData(kCertificatePolicyCacheKeyName,
45                                cert_cache_service_handle);
46   }
48   CertificatePolicyCacheHandle* handle =
49       static_cast<CertificatePolicyCacheHandle*>(
50           browser_state->GetUserData(kCertificatePolicyCacheKeyName));
51   return handle->policy_cache;
54 // static
55 ActiveStateManager* BrowserState::GetActiveStateManager(
56     BrowserState* browser_state) {
57   DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
58   DCHECK(browser_state);
60   ActiveStateManagerImpl* active_state_manager =
61       static_cast<ActiveStateManagerImpl*>(
62           browser_state->GetUserData(kActiveStateManagerKeyName));
63   if (!active_state_manager) {
64     active_state_manager = new ActiveStateManagerImpl(browser_state);
65     browser_state->SetUserData(kActiveStateManagerKeyName,
66                                active_state_manager);
67   }
68   return active_state_manager;
71 // static
72 BrowsingDataPartition* BrowserState::GetBrowsingDataPartition(
73     BrowserState* browser_state) {
74   DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
75   DCHECK(browser_state);
77   BrowsingDataPartitionImpl* browsing_data_partition =
78       static_cast<BrowsingDataPartitionImpl*>(
79           browser_state->GetUserData(kBrowsingDataPartitionKeyName));
80   if (!browsing_data_partition) {
81     browsing_data_partition = new BrowsingDataPartitionImpl(browser_state);
82     browser_state->SetUserData(kBrowsingDataPartitionKeyName,
83                                browsing_data_partition);
84   }
85   return browsing_data_partition;
88 BrowserState::BrowserState() : url_data_manager_ios_backend_(nullptr) {
89   // (Refcounted)?BrowserStateKeyedServiceFactories needs to be able to convert
90   // a base::SupportsUserData to a BrowserState. Moreover, since the factories
91   // may be passed a content::BrowserContext instead of a BrowserState, attach
92   // an empty object to this via a private key.
93   SetUserData(kBrowserStateIdentifierKey, new SupportsUserData::Data);
96 BrowserState::~BrowserState() {
97   // Delete the URLDataManagerIOSBackend instance on the IO thread if it has
98   // been created. Note that while this check can theoretically race with a
99   // call to |GetURLDataManagerIOSBackendOnIOThread()|, if any clients of this
100   // BrowserState are still accessing it on the IO thread at this point,
101   // they're going to have a bad time anyway.
102   if (url_data_manager_ios_backend_) {
103     bool posted = web::WebThread::DeleteSoon(web::WebThread::IO, FROM_HERE,
104                                              url_data_manager_ios_backend_);
105     if (!posted)
106       delete url_data_manager_ios_backend_;
107   }
110 URLDataManagerIOSBackend*
111 BrowserState::GetURLDataManagerIOSBackendOnIOThread() {
112   DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::IO);
113   if (!url_data_manager_ios_backend_)
114     url_data_manager_ios_backend_ = new URLDataManagerIOSBackend();
115   return url_data_manager_ios_backend_;
118 // static
119 BrowserState* BrowserState::FromSupportsUserData(
120     base::SupportsUserData* supports_user_data) {
121   if (!supports_user_data ||
122       !supports_user_data->GetUserData(kBrowserStateIdentifierKey)) {
123     return nullptr;
124   }
125   return static_cast<BrowserState*>(supports_user_data);
127 }  // namespace web