1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_
#define CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_
#include <string>
#include "base/basictypes.h"
#include "base/callback_forward.h"
#include "base/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/synchronization/lock.h"
#include "chrome/browser/api/prefs/pref_member.h"
#include "chrome/browser/io_thread.h"
#include "chrome/browser/net/chrome_url_request_context.h"
#include "content/public/browser/resource_context.h"
#include "net/cookies/cookie_monster.h"
#include "net/http/http_network_session.h"
#include "net/url_request/url_request_job_factory.h"
class CookieSettings;
class DesktopNotificationService;
class ExtensionInfoMap;
class HostContentSettingsMap;
class Profile;
class ProtocolHandlerRegistry;
class TransportSecurityPersister;
namespace chrome_browser_net {
class LoadTimeStats;
class HttpServerPropertiesManager;
class ResourcePrefetchPredictorObserver;
}
namespace net {
class CookieStore;
class FraudulentCertificateReporter;
class HttpTransactionFactory;
class ServerBoundCertService;
class ProxyConfigService;
class ProxyService;
class SSLConfigService;
class TransportSecurityState;
} // namespace net
namespace policy {
class URLBlacklistManager;
} // namespace policy
// Conceptually speaking, the ProfileIOData represents data that lives on the IO
// thread that is owned by a Profile, such as, but not limited to, network
// objects like CookieMonster, HttpTransactionFactory, etc. Profile owns
// ProfileIOData, but will make sure to delete it on the IO thread (except
// possibly in unit tests where there is no IO thread).
class ProfileIOData {
public:
virtual ~ProfileIOData();
static ProfileIOData* FromResourceContext(content::ResourceContext* rc);
// Returns true if |scheme| is handled in Chrome, or by default handlers in
// net::URLRequest.
static bool IsHandledProtocol(const std::string& scheme);
// Returns true if |url| is handled in Chrome, or by default handlers in
// net::URLRequest.
static bool IsHandledURL(const GURL& url);
// Called by Profile.
content::ResourceContext* GetResourceContext() const;
ChromeURLDataManagerBackend* GetChromeURLDataManagerBackend() const;
// These should only be called at most once each. Ownership is reversed when
// they get called, from ProfileIOData owning ChromeURLRequestContext to vice
// versa.
ChromeURLRequestContext* GetMainRequestContext() const;
ChromeURLRequestContext* GetMediaRequestContext() const;
ChromeURLRequestContext* GetExtensionsRequestContext() const;
ChromeURLRequestContext* GetIsolatedAppRequestContext(
ChromeURLRequestContext* main_context,
const std::string& app_id,
scoped_ptr<net::URLRequestJobFactory::Interceptor>
protocol_handler_interceptor) const;
ChromeURLRequestContext* GetIsolatedMediaRequestContext(
ChromeURLRequestContext* app_context,
const std::string& app_id) const;
// These are useful when the Chrome layer is called from the content layer
// with a content::ResourceContext, and they want access to Chrome data for
// that profile.
ExtensionInfoMap* GetExtensionInfoMap() const;
CookieSettings* GetCookieSettings() const;
#if defined(ENABLE_NOTIFICATIONS)
DesktopNotificationService* GetNotificationService() const;
#endif
IntegerPrefMember* session_startup_pref() const {
return &session_startup_pref_;
}
ChromeURLRequestContext* extensions_request_context() const {
return extensions_request_context_.get();
}
BooleanPrefMember* safe_browsing_enabled() const {
return &safe_browsing_enabled_;
}
BooleanPrefMember* printing_enabled() const {
return &printing_enabled_;
}
net::TransportSecurityState* transport_security_state() const {
return transport_security_state_.get();
}
chrome_browser_net::HttpServerPropertiesManager*
http_server_properties_manager() const;
bool is_incognito() const {
return is_incognito_;
}
chrome_browser_net::ResourcePrefetchPredictorObserver*
resource_prefetch_predictor_observer() const {
return resource_prefetch_predictor_observer_.get();
}
// Initialize the member needed to track the metrics enabled state. This is
// only to be called on the UI thread.
void InitializeMetricsEnabledStateOnUIThread();
// Returns whether or not metrics reporting is enabled in the browser instance
// on which this profile resides. This is safe for use from the IO thread, and
// should only be called from there.
bool GetMetricsEnabledStateOnIOThread() const;
protected:
// A URLRequestContext for media that owns its HTTP factory, to ensure
// it is deleted.
class MediaRequestContext : public ChromeURLRequestContext {
public:
explicit MediaRequestContext(
chrome_browser_net::LoadTimeStats* load_time_stats);
void SetHttpTransactionFactory(
scoped_ptr<net::HttpTransactionFactory> http_factory);
private:
virtual ~MediaRequestContext();
scoped_ptr<net::HttpTransactionFactory> http_factory_;
};
// A URLRequestContext for apps that owns its cookie store and HTTP factory,
// to ensure they are deleted.
class AppRequestContext : public ChromeURLRequestContext {
public:
explicit AppRequestContext(
chrome_browser_net::LoadTimeStats* load_time_stats);
void SetCookieStore(net::CookieStore* cookie_store);
void SetHttpTransactionFactory(
scoped_ptr<net::HttpTransactionFactory> http_factory);
void SetJobFactory(scoped_ptr<net::URLRequestJobFactory> job_factory);
private:
virtual ~AppRequestContext();
scoped_refptr<net::CookieStore> cookie_store_;
scoped_ptr<net::HttpTransactionFactory> http_factory_;
scoped_ptr<net::URLRequestJobFactory> job_factory_;
};
// Created on the UI thread, read on the IO thread during ProfileIOData lazy
// initialization.
struct ProfileParams {
ProfileParams();
~ProfileParams();
FilePath path;
std::string accept_language;
std::string accept_charset;
std::string referrer_charset;
IOThread* io_thread;
scoped_refptr<CookieSettings> cookie_settings;
scoped_refptr<net::SSLConfigService> ssl_config_service;
scoped_refptr<net::CookieMonster::Delegate> cookie_monster_delegate;
scoped_refptr<ExtensionInfoMap> extension_info_map;
scoped_ptr<chrome_browser_net::ResourcePrefetchPredictorObserver>
resource_prefetch_predictor_observer_;
#if defined(ENABLE_NOTIFICATIONS)
DesktopNotificationService* notification_service;
#endif
// This pointer exists only as a means of conveying a url interceptor
// pointer from the protocol handler registry on the UI thread to the
// the URLRequestJobFactory on the IO thread. The consumer MUST take
// ownership of the object by calling release() on this pointer.
scoped_ptr<net::URLRequestJobFactory::Interceptor>
protocol_handler_interceptor;
// We need to initialize the ProxyConfigService from the UI thread
// because on linux it relies on initializing things through gconf,
// and needs to be on the main thread.
scoped_ptr<net::ProxyConfigService> proxy_config_service;
// The profile this struct was populated from. It's passed as a void* to
// ensure it's not accidently used on the IO thread. Before using it on the
// UI thread, call ProfileManager::IsValidProfile to ensure it's alive.
void* profile;
};
explicit ProfileIOData(bool is_incognito);
static std::string GetSSLSessionCacheShard();
void InitializeOnUIThread(Profile* profile);
void ApplyProfileParamsToContext(ChromeURLRequestContext* context) const;
void SetUpJobFactoryDefaults(
net::URLRequestJobFactory* job_factory,
scoped_ptr<net::URLRequestJobFactory::Interceptor>
protocol_handler_interceptor,
net::NetworkDelegate* network_delegate,
net::FtpTransactionFactory* ftp_transaction_factory,
net::FtpAuthCache* ftp_auth_cache) const;
// Lazy initializes the ProfileIOData object the first time a request context
// is requested. The lazy logic is implemented here. The actual initialization
// is done in LazyInitializeInternal(), implemented by subtypes. Static helper
// functions have been provided to assist in common operations.
void LazyInitialize() const;
// Called when the profile is destroyed.
void ShutdownOnUIThread();
ChromeURLDataManagerBackend* chrome_url_data_manager_backend() const {
return chrome_url_data_manager_backend_.get();
}
// A ServerBoundCertService object is created by a derived class of
// ProfileIOData, and the derived class calls this method to set the
// server_bound_cert_service_ member and transfers ownership to the base
// class.
void set_server_bound_cert_service(
net::ServerBoundCertService* server_bound_cert_service) const;
net::NetworkDelegate* network_delegate() const {
return network_delegate_.get();
}
net::FraudulentCertificateReporter* fraudulent_certificate_reporter() const {
return fraudulent_certificate_reporter_.get();
}
net::ProxyService* proxy_service() const {
return proxy_service_.get();
}
void set_http_server_properties_manager(
chrome_browser_net::HttpServerPropertiesManager* manager) const;
ChromeURLRequestContext* main_request_context() const {
return main_request_context_.get();
}
chrome_browser_net::LoadTimeStats* load_time_stats() const {
return load_time_stats_;
}
// Destroys the ResourceContext first, to cancel any URLRequests that are
// using it still, before we destroy the member variables that those
// URLRequests may be accessing.
void DestroyResourceContext();
// Fills in fields of params using values from main_request_context_ and the
// IOThread associated with profile_params.
void PopulateNetworkSessionParams(
const ProfileParams* profile_params,
net::HttpNetworkSession::Params* params) const;
private:
class ResourceContext : public content::ResourceContext {
public:
explicit ResourceContext(ProfileIOData* io_data);
virtual ~ResourceContext();
// ResourceContext implementation:
virtual net::HostResolver* GetHostResolver() OVERRIDE;
virtual net::URLRequestContext* GetRequestContext() OVERRIDE;
private:
friend class ProfileIOData;
void EnsureInitialized();
ProfileIOData* const io_data_;
net::HostResolver* host_resolver_;
net::URLRequestContext* request_context_;
};
typedef base::hash_map<std::string, ChromeURLRequestContext*>
URLRequestContextMap;
// --------------------------------------------
// Virtual interface for subtypes to implement:
// --------------------------------------------
// Does the actual initialization of the ProfileIOData subtype. Subtypes
// should use the static helper functions above to implement this.
virtual void LazyInitializeInternal(ProfileParams* profile_params) const = 0;
// Does an on-demand initialization of a RequestContext for the given
// isolated app.
virtual ChromeURLRequestContext* InitializeAppRequestContext(
ChromeURLRequestContext* main_context,
const std::string& app_id,
scoped_ptr<net::URLRequestJobFactory::Interceptor>
protocol_handler_interceptor) const = 0;
// Does an on-demand initialization of a media RequestContext for the given
// isolated app.
virtual ChromeURLRequestContext* InitializeMediaRequestContext(
ChromeURLRequestContext* original_context,
const std::string& app_id) const = 0;
// These functions are used to transfer ownership of the lazily initialized
// context from ProfileIOData to the URLRequestContextGetter.
virtual ChromeURLRequestContext*
AcquireMediaRequestContext() const = 0;
virtual ChromeURLRequestContext*
AcquireIsolatedAppRequestContext(
ChromeURLRequestContext* main_context,
const std::string& app_id,
scoped_ptr<net::URLRequestJobFactory::Interceptor>
protocol_handler_interceptor) const = 0;
virtual ChromeURLRequestContext*
AcquireIsolatedMediaRequestContext(
ChromeURLRequestContext* app_context,
const std::string& app_id) const = 0;
// Returns the LoadTimeStats object to be used for this profile.
virtual chrome_browser_net::LoadTimeStats* GetLoadTimeStats(
IOThread::Globals* io_thread_globals) const = 0;
// The order *DOES* matter for the majority of these member variables, so
// don't move them around unless you know what you're doing!
// General rules:
// * ResourceContext references the URLRequestContexts, so
// URLRequestContexts must outlive ResourceContext, hence ResourceContext
// should be destroyed first.
// * URLRequestContexts reference a whole bunch of members, so
// URLRequestContext needs to be destroyed before them.
// * Therefore, ResourceContext should be listed last, and then the
// URLRequestContexts, and then the URLRequestContext members.
// * Note that URLRequestContext members have a directed dependency graph
// too, so they must themselves be ordered correctly.
// Tracks whether or not we've been lazily initialized.
mutable bool initialized_;
// Data from the UI thread from the Profile, used to initialize ProfileIOData.
// Deleted after lazy initialization.
mutable scoped_ptr<ProfileParams> profile_params_;
// Member variables which are pointed to by the various context objects.
mutable BooleanPrefMember enable_referrers_;
mutable BooleanPrefMember enable_do_not_track_;
mutable BooleanPrefMember safe_browsing_enabled_;
mutable BooleanPrefMember printing_enabled_;
// TODO(marja): Remove session_startup_pref_ if no longer needed.
mutable IntegerPrefMember session_startup_pref_;
// The state of metrics reporting in the browser that this profile runs on.
// Unfortunately, since ChromeOS has a separate representation of this state,
// we need to make one available based on the platform.
#if defined(OS_CHROMEOS)
bool enable_metrics_;
#else
BooleanPrefMember enable_metrics_;
#endif
// Pointed to by NetworkDelegate.
mutable scoped_ptr<policy::URLBlacklistManager> url_blacklist_manager_;
// Pointed to by URLRequestContext.
mutable scoped_refptr<ExtensionInfoMap> extension_info_map_;
mutable scoped_ptr<ChromeURLDataManagerBackend>
chrome_url_data_manager_backend_;
mutable scoped_ptr<net::ServerBoundCertService> server_bound_cert_service_;
mutable scoped_ptr<net::NetworkDelegate> network_delegate_;
mutable scoped_ptr<net::FraudulentCertificateReporter>
fraudulent_certificate_reporter_;
mutable scoped_ptr<net::ProxyService> proxy_service_;
mutable scoped_ptr<net::TransportSecurityState> transport_security_state_;
mutable scoped_ptr<chrome_browser_net::HttpServerPropertiesManager>
http_server_properties_manager_;
#if defined(ENABLE_NOTIFICATIONS)
mutable DesktopNotificationService* notification_service_;
#endif
mutable scoped_ptr<TransportSecurityPersister>
transport_security_persister_;
// These are only valid in between LazyInitialize() and their accessor being
// called.
mutable scoped_ptr<ChromeURLRequestContext> main_request_context_;
mutable scoped_ptr<ChromeURLRequestContext> extensions_request_context_;
// One URLRequestContext per isolated app for main and media requests.
mutable URLRequestContextMap app_request_context_map_;
mutable URLRequestContextMap isolated_media_request_context_map_;
mutable scoped_ptr<ResourceContext> resource_context_;
mutable scoped_refptr<CookieSettings> cookie_settings_;
mutable scoped_ptr<chrome_browser_net::ResourcePrefetchPredictorObserver>
resource_prefetch_predictor_observer_;
mutable chrome_browser_net::LoadTimeStats* load_time_stats_;
// TODO(jhawkins): Remove once crbug.com/102004 is fixed.
bool initialized_on_UI_thread_;
bool is_incognito_;
DISALLOW_COPY_AND_ASSIGN(ProfileIOData);
};
#endif // CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_
|