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