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
3 // found in the LICENSE file.
5 #include "components/keyed_service/ios/browser_state_helper.h"
7 #include "base/logging.h"
8 #include "base/supports_user_data.h"
9 #include "ios/web/public/browser_state.h"
11 // iOS code is still using BrowserContextKeyedServiceFactory and until the
12 // upstreaming is complete (http://crbug.com/419366) there is need to have
13 // mixed dependency between BCKSF and BSKSF.
15 // The implementation has BrowserStateKeyedServiceFactory supporting a
16 // BrowserContextDependencyManager as DependencyManager. Thus the context
17 // parameter passed to the BrowserStateKeyedServiceFactory can either be
18 // content::BrowserContext if the method is invoked by DependencyManager
19 // or web::BrowserState if the method is invoked via the type-safe public
22 // The public API of BrowserStateKeyedServiceFactory is type-safe (all
23 // public method receive web::BrowserState for context object), so only
24 // methods that take a base::SupportsUserData need to discriminate
25 // between the two objects.
27 // If the base::SupportsUserData is a web::BrowserState then the public
28 // method web::BrowserState::FromSupportsUserData can do the conversion
29 // safely. If this method fails then context is content::BrowserContext
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).
34 BrowserStateFromContextFn gBrowserStateFromContext
= nullptr;
37 void SetBrowserStateFromContextHelper(BrowserStateFromContextFn helper
) {
38 gBrowserStateFromContext
= helper
;
41 web::BrowserState
* BrowserStateFromContext(base::SupportsUserData
* context
) {
42 web::BrowserState
* state
= nullptr;
44 state
= web::BrowserState::FromSupportsUserData(context
);
45 if (!state
&& gBrowserStateFromContext
)
46 state
= gBrowserStateFromContext(context
);
47 DCHECK(state
) << "cannot convert context to web::BrowserState";