summaryrefslogtreecommitdiffstats
path: root/ios/chrome/browser/net/ios_chrome_url_request_context_getter.cc
blob: 0c53bc14d8e68ac5812376cb023aaae05f7cfd8d (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
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
// Copyright 2015 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.

#include "ios/chrome/browser/net/ios_chrome_url_request_context_getter.h"

#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state_io_data.h"
#include "ios/chrome/browser/ios_chrome_io_thread.h"
#include "ios/web/public/web_thread.h"
#include "net/cookies/cookie_store.h"

class IOSChromeURLRequestContextFactory {
 public:
  IOSChromeURLRequestContextFactory() {}
  virtual ~IOSChromeURLRequestContextFactory() {}

  // Called to create a new instance (will only be called once).
  virtual net::URLRequestContext* Create() = 0;

 protected:
  DISALLOW_COPY_AND_ASSIGN(IOSChromeURLRequestContextFactory);
};

namespace {

// ----------------------------------------------------------------------------
// Helper factories
// ----------------------------------------------------------------------------

// Factory that creates the main URLRequestContext.
class FactoryForMain : public IOSChromeURLRequestContextFactory {
 public:
  FactoryForMain(const ChromeBrowserStateIOData* io_data,
                 ProtocolHandlerMap* protocol_handlers)
      : io_data_(io_data) {
    std::swap(protocol_handlers_, *protocol_handlers);
  }

  net::URLRequestContext* Create() override {
    io_data_->Init(&protocol_handlers_);
    return io_data_->GetMainRequestContext();
  }

 private:
  const ChromeBrowserStateIOData* const io_data_;
  ProtocolHandlerMap protocol_handlers_;
};

// Factory that creates the URLRequestContext for a given isolated app.
class FactoryForIsolatedApp : public IOSChromeURLRequestContextFactory {
 public:
  FactoryForIsolatedApp(const ChromeBrowserStateIOData* io_data,
                        const base::FilePath& partition_path,
                        net::URLRequestContextGetter* main_context)
      : io_data_(io_data),
        partition_path_(partition_path),
        main_request_context_getter_(main_context) {}

  net::URLRequestContext* Create() override {
    // We will copy most of the state from the main request context.
    //
    // Note that this factory is one-shot.  After Create() is called once, the
    // factory is actually destroyed. Thus it is safe to destructively pass
    // state onwards.
    return io_data_->GetIsolatedAppRequestContext(
        main_request_context_getter_->GetURLRequestContext(), partition_path_);
  }

 private:
  const ChromeBrowserStateIOData* const io_data_;
  const base::FilePath partition_path_;
  scoped_refptr<net::URLRequestContextGetter> main_request_context_getter_;
};

}  // namespace

// ----------------------------------------------------------------------------
// IOSChromeURLRequestContextGetter
// ----------------------------------------------------------------------------

IOSChromeURLRequestContextGetter::IOSChromeURLRequestContextGetter(
    scoped_ptr<IOSChromeURLRequestContextFactory> factory)
    : factory_(std::move(factory)), url_request_context_(nullptr) {
  DCHECK(factory_);
}

IOSChromeURLRequestContextGetter::~IOSChromeURLRequestContextGetter() {
  // NotifyContextShuttingDown() must have been called.
  DCHECK(!factory_.get());
  DCHECK(!url_request_context_);
}

// Lazily create a URLRequestContext using our factory.
net::URLRequestContext*
IOSChromeURLRequestContextGetter::GetURLRequestContext() {
  DCHECK_CURRENTLY_ON(web::WebThread::IO);

  if (factory_.get()) {
    DCHECK(!url_request_context_);
    url_request_context_ = factory_->Create();
    factory_.reset();
  }

  return url_request_context_;
}

void IOSChromeURLRequestContextGetter::NotifyContextShuttingDown() {
  DCHECK_CURRENTLY_ON(web::WebThread::IO);

  factory_.reset();
  url_request_context_ = nullptr;
  URLRequestContextGetter::NotifyContextShuttingDown();
}

scoped_refptr<base::SingleThreadTaskRunner>
IOSChromeURLRequestContextGetter::GetNetworkTaskRunner() const {
  return web::WebThread::GetTaskRunnerForThread(web::WebThread::IO);
}

// static
IOSChromeURLRequestContextGetter* IOSChromeURLRequestContextGetter::Create(
    const ChromeBrowserStateIOData* io_data,
    ProtocolHandlerMap* protocol_handlers) {
  return new IOSChromeURLRequestContextGetter(
      make_scoped_ptr(new FactoryForMain(io_data, protocol_handlers)));
}

// static
IOSChromeURLRequestContextGetter*
IOSChromeURLRequestContextGetter::CreateForIsolatedApp(
    net::URLRequestContextGetter* main_context,
    const ChromeBrowserStateIOData* io_data,
    const base::FilePath& partition_path) {
  return new IOSChromeURLRequestContextGetter(make_scoped_ptr(
      new FactoryForIsolatedApp(io_data, partition_path, main_context)));
}