extension_resource_protocols.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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 "chrome/browser/extensions/extension_resource_protocols.h"
6
7#include "base/files/file_path.h"
8#include "base/path_service.h"
9#include "chrome/common/chrome_paths.h"
10#include "chrome/common/extensions/extension_file_util.h"
11#include "content/public/browser/browser_thread.h"
12#include "net/url_request/url_request_file_job.h"
13
14namespace {
15
16class ExtensionResourcesJob : public net::URLRequestFileJob {
17 public:
18  ExtensionResourcesJob(net::URLRequest* request,
19                        net::NetworkDelegate* network_delegate)
20    : net::URLRequestFileJob(request, network_delegate, base::FilePath()),
21      thread_id_(content::BrowserThread::UI) {
22  }
23
24  virtual void Start() OVERRIDE;
25
26 protected:
27  virtual ~ExtensionResourcesJob() {}
28
29  void ResolvePath();
30  void ResolvePathDone();
31
32 private:
33  content::BrowserThread::ID thread_id_;
34};
35
36void ExtensionResourcesJob::Start() {
37  bool result =
38      content::BrowserThread::GetCurrentThreadIdentifier(&thread_id_);
39  CHECK(result) << "Can not get thread id.";
40  content::BrowserThread::PostTask(
41      content::BrowserThread::FILE, FROM_HERE,
42      base::Bind(&ExtensionResourcesJob::ResolvePath, this));
43}
44
45void ExtensionResourcesJob::ResolvePath() {
46  base::FilePath root_path;
47  PathService::Get(chrome::DIR_RESOURCES_EXTENSION, &root_path);
48  file_path_ = extension_file_util::ExtensionResourceURLToFilePath(
49      request()->url(), root_path);
50  content::BrowserThread::PostTask(
51      thread_id_, FROM_HERE,
52      base::Bind(&ExtensionResourcesJob::ResolvePathDone, this));
53}
54
55void ExtensionResourcesJob::ResolvePathDone() {
56  net::URLRequestFileJob::Start();
57}
58
59class ExtensionResourceProtocolHandler
60    : public net::URLRequestJobFactory::ProtocolHandler {
61 public:
62  ExtensionResourceProtocolHandler() {}
63  virtual ~ExtensionResourceProtocolHandler() {}
64
65  virtual net::URLRequestJob* MaybeCreateJob(
66      net::URLRequest* request,
67      net::NetworkDelegate* network_delegate) const OVERRIDE;
68
69 private:
70  DISALLOW_COPY_AND_ASSIGN(ExtensionResourceProtocolHandler);
71};
72
73// Creates URLRequestJobs for chrome-extension-resource:// URLs.
74net::URLRequestJob*
75ExtensionResourceProtocolHandler::MaybeCreateJob(
76    net::URLRequest* request, net::NetworkDelegate* network_delegate) const {
77  return new ExtensionResourcesJob(request, network_delegate);
78}
79
80}  // namespace
81
82net::URLRequestJobFactory::ProtocolHandler*
83CreateExtensionResourceProtocolHandler() {
84  return new ExtensionResourceProtocolHandler();
85}
86