1// Copyright (c) 2012 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 "storage/browser/fileapi/file_system_url_request_job_factory.h"
6
7#include <string>
8
9#include "base/basictypes.h"
10#include "base/compiler_specific.h"
11#include "net/url_request/url_request.h"
12#include "storage/browser/fileapi/file_system_dir_url_request_job.h"
13#include "storage/browser/fileapi/file_system_url_request_job.h"
14
15namespace storage {
16
17namespace {
18
19class FileSystemProtocolHandler
20    : public net::URLRequestJobFactory::ProtocolHandler {
21 public:
22  FileSystemProtocolHandler(const std::string& storage_domain,
23                            FileSystemContext* context);
24  virtual ~FileSystemProtocolHandler();
25
26  virtual net::URLRequestJob* MaybeCreateJob(
27      net::URLRequest* request,
28      net::NetworkDelegate* network_delegate) const OVERRIDE;
29
30 private:
31  const std::string storage_domain_;
32
33  // No scoped_refptr because |file_system_context_| is owned by the
34  // ProfileIOData, which also owns this ProtocolHandler.
35  FileSystemContext* const file_system_context_;
36
37  DISALLOW_COPY_AND_ASSIGN(FileSystemProtocolHandler);
38};
39
40FileSystemProtocolHandler::FileSystemProtocolHandler(
41    const std::string& storage_domain,
42    FileSystemContext* context)
43    : storage_domain_(storage_domain),
44      file_system_context_(context) {
45  DCHECK(file_system_context_);
46}
47
48FileSystemProtocolHandler::~FileSystemProtocolHandler() {}
49
50net::URLRequestJob* FileSystemProtocolHandler::MaybeCreateJob(
51    net::URLRequest* request, net::NetworkDelegate* network_delegate) const {
52  const std::string path = request->url().path();
53
54  // If the path ends with a /, we know it's a directory. If the path refers
55  // to a directory and gets dispatched to FileSystemURLRequestJob, that class
56  // redirects back here, by adding a / to the URL.
57  if (!path.empty() && path[path.size() - 1] == '/') {
58    return new FileSystemDirURLRequestJob(
59        request, network_delegate, storage_domain_, file_system_context_);
60  }
61  return new FileSystemURLRequestJob(
62      request, network_delegate, storage_domain_, file_system_context_);
63}
64
65}  // anonymous namespace
66
67net::URLRequestJobFactory::ProtocolHandler* CreateFileSystemProtocolHandler(
68    const std::string& storage_domain, FileSystemContext* context) {
69  DCHECK(context);
70  return new FileSystemProtocolHandler(storage_domain, context);
71}
72
73}  // namespace storage
74