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 "ios/web/public/browser_state.h" | 5 #include "ios/web/public/browser_state.h" |
6 | 6 |
| 7 #include "base/location.h" |
7 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
8 #include "ios/web/public/certificate_policy_cache.h" | 9 #include "ios/web/public/certificate_policy_cache.h" |
9 #include "ios/web/public/web_thread.h" | 10 #include "ios/web/public/web_thread.h" |
| 11 #include "ios/web/webui/url_data_manager_ios_backend.h" |
10 | 12 |
11 namespace web { | 13 namespace web { |
12 namespace { | 14 namespace { |
13 // Private key used for safe conversion of base::SupportsUserData to | 15 // Private key used for safe conversion of base::SupportsUserData to |
14 // web::BrowserState in web::BrowserState::FromSupportsUserData. | 16 // web::BrowserState in web::BrowserState::FromSupportsUserData. |
15 const char kBrowserStateIdentifierKey[] = "BrowserStateIdentifierKey"; | 17 const char kBrowserStateIdentifierKey[] = "BrowserStateIdentifierKey"; |
| 18 |
16 // Data key names. | 19 // Data key names. |
17 const char kCertificatePolicyCacheKeyName[] = "cert_policy_cache"; | 20 const char kCertificatePolicyCacheKeyName[] = "cert_policy_cache"; |
18 | 21 |
19 // Wraps a CertificatePolicyCache as a SupportsUserData::Data; this is necessary | 22 // Wraps a CertificatePolicyCache as a SupportsUserData::Data; this is necessary |
20 // since reference counted objects can't be user data. | 23 // since reference counted objects can't be user data. |
21 struct CertificatePolicyCacheHandle : public base::SupportsUserData::Data { | 24 struct CertificatePolicyCacheHandle : public base::SupportsUserData::Data { |
22 explicit CertificatePolicyCacheHandle(CertificatePolicyCache* cache) | 25 explicit CertificatePolicyCacheHandle(CertificatePolicyCache* cache) |
23 : policy_cache(cache) {} | 26 : policy_cache(cache) {} |
24 | 27 |
25 scoped_refptr<CertificatePolicyCache> policy_cache; | 28 scoped_refptr<CertificatePolicyCache> policy_cache; |
(...skipping 11 matching lines...) Expand all Loading... |
37 browser_state->SetUserData(kCertificatePolicyCacheKeyName, | 40 browser_state->SetUserData(kCertificatePolicyCacheKeyName, |
38 cert_cache_service_handle); | 41 cert_cache_service_handle); |
39 } | 42 } |
40 | 43 |
41 CertificatePolicyCacheHandle* handle = | 44 CertificatePolicyCacheHandle* handle = |
42 static_cast<CertificatePolicyCacheHandle*>( | 45 static_cast<CertificatePolicyCacheHandle*>( |
43 browser_state->GetUserData(kCertificatePolicyCacheKeyName)); | 46 browser_state->GetUserData(kCertificatePolicyCacheKeyName)); |
44 return handle->policy_cache; | 47 return handle->policy_cache; |
45 } | 48 } |
46 | 49 |
47 BrowserState::BrowserState() { | 50 BrowserState::BrowserState() : url_data_manager_ios_backend_(nullptr) { |
48 // (Refcounted)?BrowserStateKeyedServiceFactories needs to be able to convert | 51 // (Refcounted)?BrowserStateKeyedServiceFactories needs to be able to convert |
49 // a base::SupportsUserData to a BrowserState. Moreover, since the factories | 52 // a base::SupportsUserData to a BrowserState. Moreover, since the factories |
50 // may be passed a content::BrowserContext instead of a BrowserState, attach | 53 // may be passed a content::BrowserContext instead of a BrowserState, attach |
51 // an empty object to this via a private key. | 54 // an empty object to this via a private key. |
52 SetUserData(kBrowserStateIdentifierKey, new SupportsUserData::Data); | 55 SetUserData(kBrowserStateIdentifierKey, new SupportsUserData::Data); |
53 } | 56 } |
54 | 57 |
55 BrowserState::~BrowserState() { | 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_; |
| 69 } |
| 70 } |
| 71 |
| 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_; |
56 } | 78 } |
57 | 79 |
58 // static | 80 // static |
59 BrowserState* BrowserState::FromSupportsUserData( | 81 BrowserState* BrowserState::FromSupportsUserData( |
60 base::SupportsUserData* supports_user_data) { | 82 base::SupportsUserData* supports_user_data) { |
61 if (!supports_user_data || | 83 if (!supports_user_data || |
62 !supports_user_data->GetUserData(kBrowserStateIdentifierKey)) { | 84 !supports_user_data->GetUserData(kBrowserStateIdentifierKey)) { |
63 return nullptr; | 85 return nullptr; |
64 } | 86 } |
65 return static_cast<BrowserState*>(supports_user_data); | 87 return static_cast<BrowserState*>(supports_user_data); |
66 } | 88 } |
67 } // namespace web | 89 } // namespace web |
OLD | NEW |