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 void Start() override
;
38 bool IsRedirectResponse(GURL
* location
, int* http_status_code
) override
;
41 ~ExtensionResourcesJob() override
{}
43 void ResolvePathDone(const base::FilePath
& resolved_path
);
46 base::ThreadChecker thread_checker_
;
48 base::WeakPtrFactory
<ExtensionResourcesJob
> weak_ptr_factory_
;
50 DISALLOW_COPY_AND_ASSIGN(ExtensionResourcesJob
);
53 void ExtensionResourcesJob::Start() {
54 DCHECK(thread_checker_
.CalledOnValidThread());
55 content::BrowserThread::PostTaskAndReplyWithResult(
56 content::BrowserThread::FILE, FROM_HERE
,
57 base::Bind(&ResolvePath
, request()->url()),
58 base::Bind(&ExtensionResourcesJob::ResolvePathDone
,
59 weak_ptr_factory_
.GetWeakPtr()));
62 bool ExtensionResourcesJob::IsRedirectResponse(GURL
* location
,
63 int* http_status_code
) {
67 void ExtensionResourcesJob::ResolvePathDone(
68 const base::FilePath
& resolved_path
) {
69 DCHECK(thread_checker_
.CalledOnValidThread());
70 file_path_
= resolved_path
;
71 net::URLRequestFileJob::Start();
74 class ExtensionResourceProtocolHandler
75 : public net::URLRequestJobFactory::ProtocolHandler
{
77 ExtensionResourceProtocolHandler() {}
78 ~ExtensionResourceProtocolHandler() override
{}
80 net::URLRequestJob
* MaybeCreateJob(
81 net::URLRequest
* request
,
82 net::NetworkDelegate
* network_delegate
) const override
;
85 DISALLOW_COPY_AND_ASSIGN(ExtensionResourceProtocolHandler
);
88 // Creates URLRequestJobs for chrome-extension-resource:// URLs.
90 ExtensionResourceProtocolHandler::MaybeCreateJob(
91 net::URLRequest
* request
, net::NetworkDelegate
* network_delegate
) const {
92 return new ExtensionResourcesJob(request
, network_delegate
);
97 net::URLRequestJobFactory::ProtocolHandler
*
98 CreateExtensionResourceProtocolHandler() {
99 return new ExtensionResourceProtocolHandler();