summaryrefslogtreecommitdiffstats
path: root/mojo/services/network/network_context.h
blob: e01f0befec6bd07d4c551febce52acaf1d3b9c7b (plain)
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
// Copyright 2014 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 MOJO_SERVICES_NETWORK_NETWORK_CONTEXT_H_
#define MOJO_SERVICES_NETWORK_NETWORK_CONTEXT_H_

#include <stddef.h>

#include <set>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/sequenced_task_runner.h"

namespace base {
class FilePath;
}

namespace net {
class URLRequestContext;
}

namespace mojo {
class URLLoader;
class URLLoaderImpl;
class NetworkServiceDelegate;

class NetworkContext {
 public:
  explicit NetworkContext(
      scoped_ptr<net::URLRequestContext> url_request_context);
  NetworkContext(
      const base::FilePath& base_path,
      const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
      NetworkServiceDelegate* delegate);
  ~NetworkContext();

  net::URLRequestContext* url_request_context() {
    return url_request_context_.get();
  }

  // These are called by individual url loaders as they are being created and
  // destroyed.
  void RegisterURLLoader(URLLoaderImpl* url_loader);
  void DeregisterURLLoader(URLLoaderImpl* url_loader);

 private:
  friend class UrlLoaderImplTest;
  size_t GetURLLoaderCountForTesting();

  static scoped_ptr<net::URLRequestContext> MakeURLRequestContext(
      const base::FilePath& base_path,
      const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
      NetworkServiceDelegate* delegate);

  class MojoNetLog;
  scoped_ptr<class MojoNetLog> net_log_;

  scoped_ptr<net::URLRequestContext> url_request_context_;
  // URLLoaderImpls register themselves with the NetworkContext so that they can
  // be cleaned up when the NetworkContext goes away. This is needed as
  // net::URLRequests held by URLLoaderImpls have to be gone when
  // net::URLRequestContext (held by NetworkContext) is destroyed.
  std::set<URLLoaderImpl*> url_loaders_;

  // Set when entering the destructor, in order to avoid manipulations of the
  // |url_loaders_| (as a url_loader might delete itself in Cleanup()).
  bool in_shutdown_;

  DISALLOW_COPY_AND_ASSIGN(NetworkContext);
};

}  // namespace mojo

#endif  // MOJO_SERVICES_NETWORK_NETWORK_CONTEXT_H_