1 // Copyright (c) 2011 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.
5 #include "net/socket_stream/socket_stream_job_manager.h"
7 #include "base/memory/singleton.h"
11 SocketStreamJobManager::SocketStreamJobManager() {
14 SocketStreamJobManager::~SocketStreamJobManager() {
18 SocketStreamJobManager* SocketStreamJobManager::GetInstance() {
19 return Singleton<SocketStreamJobManager>::get();
22 SocketStreamJob* SocketStreamJobManager::CreateJob(
23 const GURL& url, SocketStream::Delegate* delegate) const {
24 // If url is invalid, create plain SocketStreamJob, which will close
25 // the socket immediately.
26 if (!url.is_valid()) {
27 SocketStreamJob* job = new SocketStreamJob();
28 job->InitSocketStream(new SocketStream(url, delegate));
32 const std::string& scheme = url.scheme(); // already lowercase
34 base::AutoLock locked(lock_);
35 FactoryMap::const_iterator found = factories_.find(scheme);
36 if (found != factories_.end()) {
37 SocketStreamJob* job = found->second(url, delegate);
41 SocketStreamJob* job = new SocketStreamJob();
42 job->InitSocketStream(new SocketStream(url, delegate));
46 SocketStreamJob::ProtocolFactory*
47 SocketStreamJobManager::RegisterProtocolFactory(
48 const std::string& scheme, SocketStreamJob::ProtocolFactory* factory) {
49 base::AutoLock locked(lock_);
51 SocketStreamJob::ProtocolFactory* old_factory;
52 FactoryMap::iterator found = factories_.find(scheme);
53 if (found != factories_.end()) {
54 old_factory = found->second;
59 factories_[scheme] = factory;
60 } else if (found != factories_.end()) {
61 factories_.erase(found);