15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_job_manager.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/singleton.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "build/build_config.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/load_flags.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_errors.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/network_delegate.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_context.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_error_job.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_http_job.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_job_factory.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The built-in set of protocol factories
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct SchemeToFactory {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const char* scheme;
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  URLRequest::ProtocolFactory* factory;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const SchemeToFactory kBuiltinFactories[] = {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  { "http", URLRequestHttpJob::Factory },
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  { "https", URLRequestHttpJob::Factory },
35a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
36a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#if !defined(OS_IOS)
37a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  { "ws", URLRequestHttpJob::Factory },
38a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  { "wss", URLRequestHttpJob::Factory },
39a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#endif  // !defined(OS_IOS)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequestJobManager* URLRequestJobManager::GetInstance() {
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return Singleton<URLRequestJobManager>::get();
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequestJob* URLRequestJobManager::CreateJob(
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLRequest* request, NetworkDelegate* network_delegate) const {
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(IsAllowedThread());
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If we are given an invalid URL, then don't even try to inspect the scheme.
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!request->url().is_valid())
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return new URLRequestErrorJob(request, network_delegate, ERR_INVALID_URL);
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We do this here to avoid asking interceptors about unsupported schemes.
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const URLRequestJobFactory* job_factory = NULL;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_factory = request->context()->job_factory();
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& scheme = request->url().scheme();  // already lowercase
60f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (!job_factory->IsHandledProtocol(scheme)) {
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return new URLRequestErrorJob(
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        request, network_delegate, ERR_UNKNOWN_URL_SCHEME);
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // THREAD-SAFETY NOTICE:
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   We do not need to acquire the lock here since we are only reading our
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   data structures.  They should only be modified on the current thread.
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // See if the request should be intercepted.
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(pauljensen): Remove this when AppCacheInterceptor is a
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // ProtocolHandler, see crbug.com/161547.
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!(request->load_flags() & LOAD_DISABLE_INTERCEPT)) {
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    InterceptorList::const_iterator i;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (i = interceptors_.begin(); i != interceptors_.end(); ++i) {
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      URLRequestJob* job = (*i)->MaybeIntercept(request, network_delegate);
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (job)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return job;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
83f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  URLRequestJob* job = job_factory->MaybeCreateJobWithProtocolHandler(
84f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      scheme, request, network_delegate);
85f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (job)
86f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return job;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // See if the request should be handled by a built-in protocol factory.
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) {
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (scheme == kBuiltinFactories[i].scheme) {
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      URLRequestJob* job = (kBuiltinFactories[i].factory)(
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          request, network_delegate, scheme);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DCHECK(job);  // The built-in factories are not expected to fail!
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return job;
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If we reached here, then it means that a registered protocol factory
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // wasn't interested in handling the URL.  That is fairly unexpected, and we
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // don't have a specific error to report here :-(
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LOG(WARNING) << "Failed to map: " << request->url().spec();
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return new URLRequestErrorJob(request, network_delegate, ERR_FAILED);
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequestJob* URLRequestJobManager::MaybeInterceptRedirect(
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLRequest* request,
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NetworkDelegate* network_delegate,
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& location) const {
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(IsAllowedThread());
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!request->url().is_valid() ||
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      request->load_flags() & LOAD_DISABLE_INTERCEPT ||
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      request->status().status() == URLRequestStatus::CANCELED) {
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const URLRequestJobFactory* job_factory = NULL;
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_factory = request->context()->job_factory();
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& scheme = request->url().scheme();  // already lowercase
120f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (!job_factory->IsHandledProtocol(scheme))
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InterceptorList::const_iterator i;
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (i = interceptors_.begin(); i != interceptors_.end(); ++i) {
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    URLRequestJob* job = (*i)->MaybeInterceptRedirect(request,
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                      network_delegate,
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                      location);
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (job)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return job;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequestJob* URLRequestJobManager::MaybeInterceptResponse(
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLRequest* request, NetworkDelegate* network_delegate) const {
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(IsAllowedThread());
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!request->url().is_valid() ||
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      request->load_flags() & LOAD_DISABLE_INTERCEPT ||
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      request->status().status() == URLRequestStatus::CANCELED) {
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const URLRequestJobFactory* job_factory = NULL;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_factory = request->context()->job_factory();
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& scheme = request->url().scheme();  // already lowercase
147f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (!job_factory->IsHandledProtocol(scheme))
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InterceptorList::const_iterator i;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (i = interceptors_.begin(); i != interceptors_.end(); ++i) {
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    URLRequestJob* job = (*i)->MaybeInterceptResponse(request,
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                      network_delegate);
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (job)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return job;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
160f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// static
161f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)bool URLRequestJobManager::SupportsScheme(const std::string& scheme) {
162f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  for (size_t i = 0; i < arraysize(kBuiltinFactories); ++i) {
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (LowerCaseEqualsASCII(scheme, kBuiltinFactories[i].scheme))
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return true;
165f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequestJobManager::RegisterRequestInterceptor(
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLRequest::Interceptor* interceptor) {
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(IsAllowedThread());
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::AutoLock locked(lock_);
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(std::find(interceptors_.begin(), interceptors_.end(), interceptor) ==
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         interceptors_.end());
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  interceptors_.push_back(interceptor);
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequestJobManager::UnregisterRequestInterceptor(
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLRequest::Interceptor* interceptor) {
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(IsAllowedThread());
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::AutoLock locked(lock_);
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InterceptorList::iterator i =
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::find(interceptors_.begin(), interceptors_.end(), interceptor);
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(i != interceptors_.end());
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  interceptors_.erase(i);
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequestJobManager::URLRequestJobManager()
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : allowed_thread_(0),
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      allowed_thread_initialized_(false) {
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequestJobManager::~URLRequestJobManager() {}
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
201