extension_resource_protocols.cc revision f2477e01787aa58f445919b809d89e252beef54f
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/memory/weak_ptr.h"
9#include "base/path_service.h"
10#include "base/threading/sequenced_worker_pool.h"
11#include "base/threading/thread_checker.h"
12#include "chrome/common/chrome_paths.h"
13#include "content/public/browser/browser_thread.h"
14#include "extensions/common/file_util.h"
15#include "net/url_request/url_request_file_job.h"
16
17namespace {
18
19base::FilePath ResolvePath(const GURL& url) {
20  base::FilePath root_path;
21  PathService::Get(chrome::DIR_RESOURCES_EXTENSION, &root_path);
22  return extensions::file_util::ExtensionResourceURLToFilePath(url, root_path);
23}
24
25class ExtensionResourcesJob : public net::URLRequestFileJob {
26 public:
27  ExtensionResourcesJob(net::URLRequest* request,
28                        net::NetworkDelegate* network_delegate)
29      : net::URLRequestFileJob(
30            request, network_delegate, base::FilePath(),
31            content::BrowserThread::GetBlockingPool()->
32                GetTaskRunnerWithShutdownBehavior(
33                    base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)),
34        weak_ptr_factory_(this) {}
35
36  virtual void Start() OVERRIDE;
37
38 protected:
39  virtual ~ExtensionResourcesJob() {}
40
41  void ResolvePathDone(const base::FilePath& resolved_path);
42
43 private:
44  base::WeakPtrFactory<ExtensionResourcesJob> weak_ptr_factory_;
45
46  base::ThreadChecker thread_checker_;
47
48  DISALLOW_COPY_AND_ASSIGN(ExtensionResourcesJob);
49};
50
51void ExtensionResourcesJob::Start() {
52  DCHECK(thread_checker_.CalledOnValidThread());
53  content::BrowserThread::PostTaskAndReplyWithResult(
54      content::BrowserThread::FILE, FROM_HERE,
55      base::Bind(&ResolvePath, request()->url()),
56      base::Bind(&ExtensionResourcesJob::ResolvePathDone,
57          weak_ptr_factory_.GetWeakPtr()));
58}
59
60void ExtensionResourcesJob::ResolvePathDone(
61    const base::FilePath& resolved_path) {
62  DCHECK(thread_checker_.CalledOnValidThread());
63  file_path_ = resolved_path;
64  net::URLRequestFileJob::Start();
65}
66
67class ExtensionResourceProtocolHandler
68    : public net::URLRequestJobFactory::ProtocolHandler {
69 public:
70  ExtensionResourceProtocolHandler() {}
71  virtual ~ExtensionResourceProtocolHandler() {}
72
73  virtual net::URLRequestJob* MaybeCreateJob(
74      net::URLRequest* request,
75      net::NetworkDelegate* network_delegate) const OVERRIDE;
76
77 private:
78  DISALLOW_COPY_AND_ASSIGN(ExtensionResourceProtocolHandler);
79};
80
81// Creates URLRequestJobs for chrome-extension-resource:// URLs.
82net::URLRequestJob*
83ExtensionResourceProtocolHandler::MaybeCreateJob(
84    net::URLRequest* request, net::NetworkDelegate* network_delegate) const {
85  return new ExtensionResourcesJob(request, network_delegate);
86}
87
88}  // namespace
89
90net::URLRequestJobFactory::ProtocolHandler*
91CreateExtensionResourceProtocolHandler() {
92  return new ExtensionResourceProtocolHandler();
93}
94