summaryrefslogtreecommitdiffstats
path: root/net/proxy/proxy_resolver_factory.cc
blob: b544d892ac7e682c860743ed1a15f7484efe6ee2 (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
// 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 "net/proxy/proxy_resolver_factory.h"

#include "net/base/net_errors.h"
#include "net/proxy/proxy_resolver.h"

namespace net {

class LegacyProxyResolverFactory::Job : public ProxyResolverFactory::Request {
 public:
  Job(LegacyProxyResolverFactory* factory,
      const scoped_refptr<ProxyResolverScriptData>& pac_script,
      scoped_ptr<ProxyResolver> resolver,
      scoped_ptr<ProxyResolver>* resolver_out,
      const net::CompletionCallback& callback);
  ~Job() override;
  int Start();

  void FactoryDestroyed();

 private:
  void OnSetPacScriptDone(int error);

  LegacyProxyResolverFactory* factory_;
  const scoped_refptr<ProxyResolverScriptData> pac_script_;
  scoped_ptr<ProxyResolver> resolver_;
  scoped_ptr<ProxyResolver>* resolver_out_;
  const net::CompletionCallback callback_;
};

LegacyProxyResolverFactory::Job::Job(
    LegacyProxyResolverFactory* factory,
    const scoped_refptr<ProxyResolverScriptData>& pac_script,
    scoped_ptr<ProxyResolver> resolver,
    scoped_ptr<ProxyResolver>* resolver_out,
    const net::CompletionCallback& callback)
    : factory_(factory),
      pac_script_(pac_script),
      resolver_(resolver.Pass()),
      resolver_out_(resolver_out),
      callback_(callback) {
}

LegacyProxyResolverFactory::Job::~Job() {
  if (factory_) {
    resolver_->CancelSetPacScript();
    factory_->RemoveJob(this);
  }
}

int LegacyProxyResolverFactory::Job::Start() {
  int error = resolver_->SetPacScript(
      pac_script_,
      base::Bind(&Job::OnSetPacScriptDone, base::Unretained(this)));
  if (error != ERR_IO_PENDING) {
    factory_ = nullptr;
    if (error == OK)
      *resolver_out_ = resolver_.Pass();
    return error;
  }

  return ERR_IO_PENDING;
}

void LegacyProxyResolverFactory::Job::FactoryDestroyed() {
  factory_ = nullptr;
  resolver_->CancelSetPacScript();
}

void LegacyProxyResolverFactory::Job::OnSetPacScriptDone(int error) {
  factory_->RemoveJob(this);
  if (error == OK)
    *resolver_out_ = resolver_.Pass();

  CompletionCallback callback = callback_;
  factory_ = nullptr;
  if (!callback.is_null())
    callback.Run(error);
}

ProxyResolverFactory::ProxyResolverFactory(bool expects_pac_bytes)
    : expects_pac_bytes_(expects_pac_bytes) {
}

ProxyResolverFactory::~ProxyResolverFactory() {
}

LegacyProxyResolverFactory::LegacyProxyResolverFactory(bool expects_pac_bytes)
    : ProxyResolverFactory(expects_pac_bytes) {
}

LegacyProxyResolverFactory::~LegacyProxyResolverFactory() {
  for (auto job : jobs_) {
    job->FactoryDestroyed();
  }
}

int LegacyProxyResolverFactory::CreateProxyResolver(
    const scoped_refptr<ProxyResolverScriptData>& pac_script,
    scoped_ptr<ProxyResolver>* resolver,
    const net::CompletionCallback& callback,
    scoped_ptr<ProxyResolverFactory::Request>* request) {
  scoped_ptr<Job> job(
      new Job(this, pac_script, CreateProxyResolver(), resolver, callback));
  int error = job->Start();
  if (error != ERR_IO_PENDING)
    return error;

  jobs_.insert(job.get());
  *request = job.Pass();
  return ERR_IO_PENDING;
}

void LegacyProxyResolverFactory::RemoveJob(Job* job) {
  size_t erased = jobs_.erase(job);
  DCHECK_EQ(1u, erased);
}

}  // namespace net