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 17 namespace { 18 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); 23 } 24 25 class 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 virtual bool IsRedirectResponse(GURL* location, 39 int* http_status_code) override; 40 41 protected: 42 virtual ~ExtensionResourcesJob() {} 43 44 void ResolvePathDone(const base::FilePath& resolved_path); 45 46 private: 47 base::WeakPtrFactory<ExtensionResourcesJob> weak_ptr_factory_; 48 49 base::ThreadChecker thread_checker_; 50 51 DISALLOW_COPY_AND_ASSIGN(ExtensionResourcesJob); 52 }; 53 54 void ExtensionResourcesJob::Start() { 55 DCHECK(thread_checker_.CalledOnValidThread()); 56 content::BrowserThread::PostTaskAndReplyWithResult( 57 content::BrowserThread::FILE, FROM_HERE, 58 base::Bind(&ResolvePath, request()->url()), 59 base::Bind(&ExtensionResourcesJob::ResolvePathDone, 60 weak_ptr_factory_.GetWeakPtr())); 61 } 62 63 bool ExtensionResourcesJob::IsRedirectResponse(GURL* location, 64 int* http_status_code) { 65 return false; 66 } 67 68 void ExtensionResourcesJob::ResolvePathDone( 69 const base::FilePath& resolved_path) { 70 DCHECK(thread_checker_.CalledOnValidThread()); 71 file_path_ = resolved_path; 72 net::URLRequestFileJob::Start(); 73 } 74 75 class ExtensionResourceProtocolHandler 76 : public net::URLRequestJobFactory::ProtocolHandler { 77 public: 78 ExtensionResourceProtocolHandler() {} 79 virtual ~ExtensionResourceProtocolHandler() {} 80 81 virtual net::URLRequestJob* MaybeCreateJob( 82 net::URLRequest* request, 83 net::NetworkDelegate* network_delegate) const OVERRIDE; 84 85 private: 86 DISALLOW_COPY_AND_ASSIGN(ExtensionResourceProtocolHandler); 87 }; 88 89 // Creates URLRequestJobs for chrome-extension-resource:// URLs. 90 net::URLRequestJob* 91 ExtensionResourceProtocolHandler::MaybeCreateJob( 92 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { 93 return new ExtensionResourcesJob(request, network_delegate); 94 } 95 96 } // namespace 97 98 net::URLRequestJobFactory::ProtocolHandler* 99 CreateExtensionResourceProtocolHandler() { 100 return new ExtensionResourceProtocolHandler(); 101 } 102