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.
5 #include "chrome/browser/extensions/extension_resource_protocols.h"
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"
19 base::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
);
25 class ExtensionResourcesJob
: public net::URLRequestFileJob
{
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) {}
36 virtual void Start() OVERRIDE
;
39 virtual ~ExtensionResourcesJob() {}
41 void ResolvePathDone(const base::FilePath
& resolved_path
);
44 base::WeakPtrFactory
<ExtensionResourcesJob
> weak_ptr_factory_
;
46 base::ThreadChecker thread_checker_
;
48 DISALLOW_COPY_AND_ASSIGN(ExtensionResourcesJob
);
51 void 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()));
60 void ExtensionResourcesJob::ResolvePathDone(
61 const base::FilePath
& resolved_path
) {
62 DCHECK(thread_checker_
.CalledOnValidThread());
63 file_path_
= resolved_path
;
64 net::URLRequestFileJob::Start();
67 class ExtensionResourceProtocolHandler
68 : public net::URLRequestJobFactory::ProtocolHandler
{
70 ExtensionResourceProtocolHandler() {}
71 virtual ~ExtensionResourceProtocolHandler() {}
73 virtual net::URLRequestJob
* MaybeCreateJob(
74 net::URLRequest
* request
,
75 net::NetworkDelegate
* network_delegate
) const OVERRIDE
;
78 DISALLOW_COPY_AND_ASSIGN(ExtensionResourceProtocolHandler
);
81 // Creates URLRequestJobs for chrome-extension-resource:// URLs.
83 ExtensionResourceProtocolHandler::MaybeCreateJob(
84 net::URLRequest
* request
, net::NetworkDelegate
* network_delegate
) const {
85 return new ExtensionResourcesJob(request
, network_delegate
);
90 net::URLRequestJobFactory::ProtocolHandler
*
91 CreateExtensionResourceProtocolHandler() {
92 return new ExtensionResourceProtocolHandler();