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.
4
5#ifndef CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_GETTER_H_
6#define CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_GETTER_H_
7
8#include <string>
9
10#include "base/memory/scoped_ptr.h"
11#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
12#include "net/url_request/url_request_context.h"
13#include "net/url_request/url_request_context_getter.h"
14#include "net/url_request/url_request_job_factory.h"
15
16class ChromeURLRequestContextFactory;
17class IOThread;
18class Profile;
19class ProfileIOData;
20struct StoragePartitionDescriptor;
21
22// A net::URLRequestContextGetter subclass used by the browser. This returns a
23// subclass of net::URLRequestContext which can be used to store extra
24// information about requests.
25//
26// Most methods are expected to be called on the UI thread, except for
27// the destructor and GetURLRequestContext().
28class ChromeURLRequestContextGetter : public net::URLRequestContextGetter {
29 public:
30  // Constructs a ChromeURLRequestContextGetter that will use |factory| to
31  // create the URLRequestContext.
32  explicit ChromeURLRequestContextGetter(
33      ChromeURLRequestContextFactory* factory);
34
35  // Note that GetURLRequestContext() can only be called from the IO
36  // thread (it will assert otherwise).
37  // GetIOMessageLoopProxy however can be called from any thread.
38  //
39  // net::URLRequestContextGetter implementation.
40  virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE;
41  virtual scoped_refptr<base::SingleThreadTaskRunner>
42      GetNetworkTaskRunner() const OVERRIDE;
43
44  // Create an instance for use with an 'original' (non-OTR) profile. This is
45  // expected to get called on the UI thread.
46  static ChromeURLRequestContextGetter* Create(
47      Profile* profile,
48      const ProfileIOData* profile_io_data,
49      content::ProtocolHandlerMap* protocol_handlers,
50      content::URLRequestInterceptorScopedVector request_interceptors);
51
52  // Create an instance for an original profile for media. This is expected to
53  // get called on UI thread. This method takes a profile and reuses the
54  // 'original' net::URLRequestContext for common files.
55  static ChromeURLRequestContextGetter* CreateForMedia(
56      Profile* profile, const ProfileIOData* profile_io_data);
57
58  // Create an instance for an original profile for extensions. This is expected
59  // to get called on UI thread.
60  static ChromeURLRequestContextGetter* CreateForExtensions(
61      Profile* profile, const ProfileIOData* profile_io_data);
62
63  // Create an instance for an original profile for an app with isolated
64  // storage. This is expected to get called on UI thread.
65  static ChromeURLRequestContextGetter* CreateForIsolatedApp(
66      Profile* profile,
67      const ProfileIOData* profile_io_data,
68      const StoragePartitionDescriptor& partition_descriptor,
69      scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
70          protocol_handler_interceptor,
71      content::ProtocolHandlerMap* protocol_handlers,
72      content::URLRequestInterceptorScopedVector request_interceptors);
73
74  // Create an instance for an original profile for media with isolated
75  // storage. This is expected to get called on UI thread.
76  static ChromeURLRequestContextGetter* CreateForIsolatedMedia(
77      Profile* profile,
78      ChromeURLRequestContextGetter* app_context,
79      const ProfileIOData* profile_io_data,
80      const StoragePartitionDescriptor& partition_descriptor);
81
82  // Discard reference to URLRequestContext.
83  // Access only from the IO thread.
84  void Invalidate();
85
86 private:
87  virtual ~ChromeURLRequestContextGetter();
88
89  // Deferred logic for creating a URLRequestContext.
90  // Access only from the IO thread.
91  scoped_ptr<ChromeURLRequestContextFactory> factory_;
92
93  // NULL before initialization and after invalidation.
94  // Otherwise, it is the URLRequestContext instance that
95  // was lazily created by GetURLRequestContext().
96  // Access only from the IO thread.
97  net::URLRequestContext* url_request_context_;
98
99  DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContextGetter);
100};
101
102#endif  // CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_GETTER_H_
103