summaryrefslogtreecommitdiffstats
path: root/net/socket_stream/socket_stream_job_manager.cc
blob: 7f66a4a4fd32a69127667086b8c1c94e6ee41ec4 (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
// Copyright (c) 2011 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/socket_stream/socket_stream_job_manager.h"

#include "base/memory/singleton.h"

namespace net {

SocketStreamJobManager::SocketStreamJobManager() {
}

SocketStreamJobManager::~SocketStreamJobManager() {
}

// static
SocketStreamJobManager* SocketStreamJobManager::GetInstance() {
  return Singleton<SocketStreamJobManager>::get();
}

SocketStreamJob* SocketStreamJobManager::CreateJob(
    const GURL& url, SocketStream::Delegate* delegate) const {
  // If url is invalid, create plain SocketStreamJob, which will close
  // the socket immediately.
  if (!url.is_valid()) {
    SocketStreamJob* job = new SocketStreamJob();
    job->InitSocketStream(new SocketStream(url, delegate));
    return job;
  }

  const std::string& scheme = url.scheme();  // already lowercase

  base::AutoLock locked(lock_);
  FactoryMap::const_iterator found = factories_.find(scheme);
  if (found != factories_.end()) {
    SocketStreamJob* job = found->second(url, delegate);
    if (job)
      return job;
  }
  SocketStreamJob* job = new SocketStreamJob();
  job->InitSocketStream(new SocketStream(url, delegate));
  return job;
}

SocketStreamJob::ProtocolFactory*
SocketStreamJobManager::RegisterProtocolFactory(
    const std::string& scheme, SocketStreamJob::ProtocolFactory* factory) {
  base::AutoLock locked(lock_);

  SocketStreamJob::ProtocolFactory* old_factory;
  FactoryMap::iterator found = factories_.find(scheme);
  if (found != factories_.end()) {
    old_factory = found->second;
  } else {
    old_factory = NULL;
  }
  if (factory) {
    factories_[scheme] = factory;
  } else if (found != factories_.end()) {
    factories_.erase(found);
  }
  return old_factory;
}

}  // namespace net