blob: 613d33ca3a9827bf6f58d71f98811cfe4bdb8ecf [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;
Maks Orlovichc71746a62018-04-27 21:52:1735class NetworkContextOwner;
Colin Blundelld4c96c752017-07-11 07:24:1236class ServiceManagerConnection;
stuartmorganf0e19f912015-04-29 06:01:0137class URLDataManagerIOS;
38class URLDataManagerIOSBackend;
39class URLRequestChromeJob;
sdefresnea010b6d2014-11-12 09:32:2040
41// This class holds the context needed for a browsing session.
42// It lives on the UI thread. All these methods must only be called on the UI
43// thread.
44class BrowserState : public base::SupportsUserData {
45 public:
sdefresnedb52bdf2014-11-13 09:20:3246 ~BrowserState() override;
sdefresnea010b6d2014-11-12 09:32:2047
stuartmorgand7f6a672015-03-31 22:01:1348 // static
49 static scoped_refptr<CertificatePolicyCache> GetCertificatePolicyCache(
50 BrowserState* browser_state);
51
sdefresne6f95f8f2014-11-21 11:21:1052 // Returns whether this BrowserState is incognito. Default is false.
sdefresnea010b6d2014-11-12 09:32:2053 virtual bool IsOffTheRecord() const = 0;
54
sdefresne6f95f8f2014-11-21 11:21:1055 // Returns the path where the BrowserState data is stored.
sdefresne2bbdeed2015-03-02 14:24:2656 // Unlike Profile::GetPath(), incognito BrowserState do not share their path
57 // with their original BrowserState.
58 virtual base::FilePath GetStatePath() const = 0;
sdefresne6f95f8f2014-11-21 11:21:1059
sdefresnea010b6d2014-11-12 09:32:2060 // Returns the request context information associated with this
61 // BrowserState.
62 virtual net::URLRequestContextGetter* GetRequestContext() = 0;
63
John Abd-El-Malek07a93f12018-02-08 19:28:2564 // Returns a URLLoaderFactory that is backed by GetRequestContext.
65 network::mojom::URLLoaderFactory* GetURLLoaderFactory();
66
sdefresneb30864ac2014-11-21 12:40:5267 // Safely cast a base::SupportsUserData to a BrowserState. Returns nullptr
68 // if |supports_user_data| is not a BrowserState.
69 static BrowserState* FromSupportsUserData(
70 base::SupportsUserData* supports_user_data);
71
Colin Blundelld4c96c752017-07-11 07:24:1272 // Returns a Service User ID associated with this BrowserState. This ID is
73 // not persistent across runs. See
Ken Rockot543f5e32018-02-04 02:13:5074 // services/service_manager/public/mojom/connector.mojom. By default,
Colin Blundelld4c96c752017-07-11 07:24:1275 // this user id is randomly generated when Initialize() is called.
76 static const std::string& GetServiceUserIdFor(BrowserState* browser_state);
77
78 // Returns a Connector associated with this BrowserState, which can be used
79 // to connect to service instances bound as this user.
80 static service_manager::Connector* GetConnectorFor(
81 BrowserState* browser_state);
82
83 // Returns a ServiceManagerConnection associated with this BrowserState,
84 // which can be used to connect to service instances bound as this user.
85 static ServiceManagerConnection* GetServiceManagerConnectionFor(
86 BrowserState* browser_state);
87
88 using StaticServiceMap =
89 std::map<std::string, service_manager::EmbeddedServiceInfo>;
90
91 // Registers per-browser-state services to be loaded by the Service Manager.
92 virtual void RegisterServices(StaticServiceMap* services) {}
93
sdefresnea010b6d2014-11-12 09:32:2094 protected:
95 BrowserState();
stuartmorganf0e19f912015-04-29 06:01:0196
Colin Blundelld4c96c752017-07-11 07:24:1297 // Makes the Service Manager aware of this BrowserState, and assigns a user
98 // ID number to it. Must be called for each BrowserState created. |path|
99 // should be the same path that would be returned by GetStatePath().
100 static void Initialize(BrowserState* browser_state,
101 const base::FilePath& path);
102
stuartmorganf0e19f912015-04-29 06:01:01103 private:
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_