OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/keyed_service/ios/browser_state_helper.h" | 5 #include "components/keyed_service/ios/browser_state_helper.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/supports_user_data.h" | 8 #include "base/supports_user_data.h" |
9 #include "ios/web/public/browser_state.h" | 9 #include "ios/web/public/browser_state.h" |
10 | 10 |
11 // iOS code is still using BrowserContextKeyedServiceFactory and until the | 11 // iOS code is still using BrowserContextKeyedServiceFactory and until the |
12 // upstreaming is complete (http://crbug.com/419366) there is need to have | 12 // conversion is complete (http://crbug.com/478763) there is need to have |
13 // mixed dependency between BCKSF and BSKSF. | 13 // mixed dependency between BCKSF and BSKSF. |
14 // | 14 // |
15 // The implementation has BrowserStateKeyedServiceFactory supporting a | 15 // The implementation has BrowserStateKeyedServiceFactory supporting a |
16 // BrowserContextDependencyManager as DependencyManager. Thus the context | 16 // BrowserContextDependencyManager as DependencyManager. Thus the context |
17 // parameter passed to the BrowserStateKeyedServiceFactory can either be | 17 // parameter passed to the BrowserStateKeyedServiceFactory can either be |
18 // content::BrowserContext if the method is invoked by DependencyManager | 18 // content::BrowserContext if the method is invoked by DependencyManager |
19 // or web::BrowserState if the method is invoked via the type-safe public | 19 // or web::BrowserState if the method is invoked via the type-safe public |
20 // API. | 20 // API. |
21 // | 21 // |
22 // The public API of BrowserStateKeyedServiceFactory is type-safe (all | 22 // The public API of BrowserStateKeyedServiceFactory is type-safe (all |
23 // public method receive web::BrowserState for context object), so only | 23 // public method receive web::BrowserState for context object), so only |
24 // methods that take a base::SupportsUserData need to discriminate | 24 // methods that take a base::SupportsUserData need to discriminate |
25 // between the two objects. | 25 // between the two objects. |
26 // | 26 // |
27 // If the base::SupportsUserData is a web::BrowserState then the public | 27 // If the base::SupportsUserData is a web::BrowserState then the public |
28 // method web::BrowserState::FromSupportsUserData can do the conversion | 28 // method web::BrowserState::FromSupportsUserData can do the conversion |
29 // safely. If this method fails then context is content::BrowserContext | 29 // safely. If this method fails then context is content::BrowserContext |
30 // and the methods defined below allow the embedder to provides helper | 30 // and the methods defined below allow the embedder to provides helper |
31 // to find the associated web::BrowserState (there is a 1:1 mapping). | 31 // to find the associated web::BrowserState (there is a 1:1 mapping). |
32 | 32 |
33 namespace { | 33 namespace { |
34 BrowserStateFromContextFn g_browser_state_from_context = nullptr; | 34 UnderlyingContextFromContextFn g_underlying_context_from_context = nullptr; |
| 35 OriginalContextFromContextFn g_original_context_from_context = nullptr; |
35 } // namespace | 36 } // namespace |
36 | 37 |
37 void SetBrowserStateFromContextHelper(BrowserStateFromContextFn helper) { | 38 void SetUnderlyingContextFromContextHelper( |
38 g_browser_state_from_context = helper; | 39 UnderlyingContextFromContextFn helper) { |
| 40 DCHECK_NE(UnderlyingContextFromContext, helper); |
| 41 g_underlying_context_from_context = helper; |
39 } | 42 } |
40 | 43 |
41 web::BrowserState* BrowserStateFromContext(base::SupportsUserData* context) { | 44 web::BrowserState* UnderlyingContextFromContext( |
42 web::BrowserState* state = nullptr; | 45 base::SupportsUserData* context) { |
| 46 web::BrowserState* underlying_context = nullptr; |
43 if (context) { | 47 if (context) { |
44 state = web::BrowserState::FromSupportsUserData(context); | 48 underlying_context = web::BrowserState::FromSupportsUserData(context); |
45 if (!state && g_browser_state_from_context) | 49 if (!underlying_context && g_underlying_context_from_context) |
46 state = g_browser_state_from_context(context); | 50 underlying_context = g_underlying_context_from_context(context); |
47 DCHECK(state) << "cannot convert context to web::BrowserState"; | 51 DCHECK(underlying_context) << "cannot resolve underlying context"; |
48 } | 52 } |
49 return state; | 53 return underlying_context; |
50 } | 54 } |
| 55 |
| 56 void SetOriginalContextFromContextHelper(OriginalContextFromContextFn helper) { |
| 57 g_original_context_from_context = helper; |
| 58 } |
| 59 |
| 60 base::SupportsUserData* OriginalContextFromContext( |
| 61 base::SupportsUserData* context) { |
| 62 base::SupportsUserData* original_context = context; |
| 63 if (context) { |
| 64 web::BrowserState* browser_state = |
| 65 web::BrowserState::FromSupportsUserData(context); |
| 66 if (browser_state && g_original_context_from_context) |
| 67 original_context = g_original_context_from_context(browser_state); |
| 68 } |
| 69 return original_context; |
| 70 } |
OLD | NEW |