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