blob: 1fae818e5b440898e27cb79cade8f52d420cf66e [file] [log] [blame]
sdefresnef40c65a2014-11-13 12:25:021// Copyright 2013 The Chromium Authors. All rights reserved.
sdefresnea010b6d2014-11-12 09:32:202// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef IOS_WEB_PUBLIC_BROWSER_STATE_H_
6#define IOS_WEB_PUBLIC_BROWSER_STATE_H_
7
John Abd-El-Malek07a93f12018-02-08 19:28:258#include <memory>
9
sdefresnea010b6d2014-11-12 09:32:2010#include "base/supports_user_data.h"
Matt Menke1f2fb3732018-03-09 19:14:4711#include "services/network/public/mojom/network_service.mojom.h"
12#include "services/network/public/mojom/url_loader_factory.mojom.h"
Colin Blundelld4c96c752017-07-11 07:24:1213#include "services/service_manager/embedder/embedded_service_info.h"
sdefresnea010b6d2014-11-12 09:32:2014
sdefresne6f95f8f2014-11-21 11:21:1015namespace base {
16class FilePath;
17}
18
sdefresnea010b6d2014-11-12 09:32:2019namespace net {
20class URLRequestContextGetter;
21}
22
John Abd-El-Malek07a93f12018-02-08 19:28:2523namespace network {
24namespace mojom {
25class URLLoaderFactory;
26}
27} // namespace network
28
Colin Blundelld4c96c752017-07-11 07:24:1229namespace service_manager {
30class Connector;
31}
32
sdefresnea010b6d2014-11-12 09:32:2033namespace web {
stuartmorgand7f6a672015-03-31 22:01:1334class CertificatePolicyCache;
Colin Blundelld4c96c752017-07-11 07:24:1235class ServiceManagerConnection;
stuartmorganf0e19f912015-04-29 06:01:0136class URLDataManagerIOS;
37class URLDataManagerIOSBackend;
38class URLRequestChromeJob;
sdefresnea010b6d2014-11-12 09:32:2039
40// This class holds the context needed for a browsing session.
41// It lives on the UI thread. All these methods must only be called on the UI
42// thread.
43class BrowserState : public base::SupportsUserData {
44 public:
sdefresnedb52bdf2014-11-13 09:20:3245 ~BrowserState() override;
sdefresnea010b6d2014-11-12 09:32:2046
stuartmorgand7f6a672015-03-31 22:01:1347 // static
48 static scoped_refptr<CertificatePolicyCache> GetCertificatePolicyCache(
49 BrowserState* browser_state);
50
sdefresne6f95f8f2014-11-21 11:21:1051 // Returns whether this BrowserState is incognito. Default is false.
sdefresnea010b6d2014-11-12 09:32:2052 virtual bool IsOffTheRecord() const = 0;
53
sdefresne6f95f8f2014-11-21 11:21:1054 // Returns the path where the BrowserState data is stored.
sdefresne2bbdeed2015-03-02 14:24:2655 // Unlike Profile::GetPath(), incognito BrowserState do not share their path
56 // with their original BrowserState.
57 virtual base::FilePath GetStatePath() const = 0;
sdefresne6f95f8f2014-11-21 11:21:1058
sdefresnea010b6d2014-11-12 09:32:2059 // Returns the request context information associated with this
60 // BrowserState.
61 virtual net::URLRequestContextGetter* GetRequestContext() = 0;
62
John Abd-El-Malek07a93f12018-02-08 19:28:2563 // Returns a URLLoaderFactory that is backed by GetRequestContext.
64 network::mojom::URLLoaderFactory* GetURLLoaderFactory();
65
sdefresneb30864ac2014-11-21 12:40:5266 // Safely cast a base::SupportsUserData to a BrowserState. Returns nullptr
67 // if |supports_user_data| is not a BrowserState.
68 static BrowserState* FromSupportsUserData(
69 base::SupportsUserData* supports_user_data);
70
Colin Blundelld4c96c752017-07-11 07:24:1271 // Returns a Service User ID associated with this BrowserState. This ID is
72 // not persistent across runs. See
Ken Rockot543f5e32018-02-04 02:13:5073 // services/service_manager/public/mojom/connector.mojom. By default,
Colin Blundelld4c96c752017-07-11 07:24:1274 // this user id is randomly generated when Initialize() is called.
75 static const std::string& GetServiceUserIdFor(BrowserState* browser_state);
76
77 // Returns a Connector associated with this BrowserState, which can be used
78 // to connect to service instances bound as this user.
79 static service_manager::Connector* GetConnectorFor(
80 BrowserState* browser_state);
81
82 // Returns a ServiceManagerConnection associated with this BrowserState,
83 // which can be used to connect to service instances bound as this user.
84 static ServiceManagerConnection* GetServiceManagerConnectionFor(
85 BrowserState* browser_state);
86
87 using StaticServiceMap =
88 std::map<std::string, service_manager::EmbeddedServiceInfo>;
89
90 // Registers per-browser-state services to be loaded by the Service Manager.
91 virtual void RegisterServices(StaticServiceMap* services) {}
92
sdefresnea010b6d2014-11-12 09:32:2093 protected:
94 BrowserState();
stuartmorganf0e19f912015-04-29 06:01:0195
Colin Blundelld4c96c752017-07-11 07:24:1296 // Makes the Service Manager aware of this BrowserState, and assigns a user
97 // ID number to it. Must be called for each BrowserState created. |path|
98 // should be the same path that would be returned by GetStatePath().
99 static void Initialize(BrowserState* browser_state,
100 const base::FilePath& path);
101
stuartmorganf0e19f912015-04-29 06:01:01102 private:
Matt Menke1f2fb3732018-03-09 19:14:47103 class NetworkContextOwner;
stuartmorganf0e19f912015-04-29 06:01:01104 friend class URLDataManagerIOS;
105 friend class URLRequestChromeJob;
106
107 // Returns the URLDataManagerIOSBackend instance associated with this
108 // BrowserState, creating it if necessary. Should only be called on the IO
109 // thread.
110 // Not intended for usage outside of //web.
111 URLDataManagerIOSBackend* GetURLDataManagerIOSBackendOnIOThread();
112
Matt Menke1f2fb3732018-03-09 19:14:47113 network::mojom::URLLoaderFactoryPtr url_loader_factory_;
114 network::mojom::NetworkContextPtr network_context_;
115
116 // Owns the network::NetworkContext that backs |url_loader_factory_|. Created
117 // on the UI thread, destroyed on the IO thread.
118 std::unique_ptr<NetworkContextOwner> network_context_owner_;
John Abd-El-Malek07a93f12018-02-08 19:28:25119
stuartmorganf0e19f912015-04-29 06:01:01120 // The URLDataManagerIOSBackend instance associated with this BrowserState.
121 // Created and destroyed on the IO thread, and should be accessed only from
122 // the IO thread.
123 URLDataManagerIOSBackend* url_data_manager_ios_backend_;
sdefresnea010b6d2014-11-12 09:32:20124};
125
126} // namespace web
127
128#endif // IOS_WEB_PUBLIC_BROWSER_STATE_H_