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.
4
5#include "chrome/browser/net/file_system_url_request_job_factory.h"
6
7#include "chrome/browser/net/chrome_url_request_context.h"
8#include "chrome/common/url_constants.h"
9#include "content/browser/browser_thread.h"
10#include "net/url_request/url_request.h"
11#include "webkit/fileapi/file_system_url_request_job.h"
12#include "webkit/fileapi/file_system_dir_url_request_job.h"
13
14namespace {
15
16net::URLRequestJob* FileSystemURLRequestJobFactory(net::URLRequest* request,
17                                                   const std::string& scheme) {
18  fileapi::FileSystemContext* file_system_context =
19      static_cast<ChromeURLRequestContext*>(request->context())
20          ->file_system_context();
21  const std::string path = request->url().path();
22
23  // If the path ends with a /, we know it's a directory. If the path refers
24  // to a directory and gets dispatched to FileSystemURLRequestJob, that class
25  // redirects back here, by adding a / to the URL.
26  if (!path.empty() && path[path.size() - 1] == '/') {
27    return new fileapi::FileSystemDirURLRequestJob(request, file_system_context,
28        BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
29  }
30  return new fileapi::FileSystemURLRequestJob(request, file_system_context,
31      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
32}
33
34}  // anonymous namespace
35
36void RegisterFileSystemURLRequestJobFactory() {
37  net::URLRequest::RegisterProtocolFactory(chrome::kFileSystemScheme,
38                                           &FileSystemURLRequestJobFactory);
39}
40