Implement protocol.registerFileProtocol

This commit is contained in:
Cheng Zhao 2015-08-12 15:18:31 +08:00
parent 337460cdc2
commit d2681d2ba1
8 changed files with 383 additions and 88 deletions

View file

@ -8,10 +8,12 @@
#include <memory>
#include <string>
#include "atom/browser/net/js_asker.h"
#include "atom/common/asar/archive.h"
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "net/http/http_byte_range.h"
#include "net/url_request/url_request_job.h"
namespace base {
@ -34,11 +36,20 @@ net::URLRequestJob* CreateJobFromPath(
class URLRequestAsarJob : public net::URLRequestJob {
public:
URLRequestAsarJob(net::URLRequest* request,
net::NetworkDelegate* network_delegate,
std::shared_ptr<Archive> archive,
const base::FilePath& file_path,
const Archive::FileInfo& file_info,
const scoped_refptr<base::TaskRunner>& file_task_runner);
net::NetworkDelegate* network_delegate);
void Initialize(const scoped_refptr<base::TaskRunner> file_task_runner,
const base::FilePath& file_path);
protected:
virtual ~URLRequestAsarJob();
void InitializeAsarJob(const scoped_refptr<base::TaskRunner> file_task_runner,
std::shared_ptr<Archive> archive,
const base::FilePath& file_path,
const Archive::FileInfo& file_info);
void InitializeFileJob(const scoped_refptr<base::TaskRunner> file_task_runner,
const base::FilePath& file_path);
// net::URLRequestJob:
void Start() override;
@ -46,12 +57,39 @@ class URLRequestAsarJob : public net::URLRequestJob {
bool ReadRawData(net::IOBuffer* buf,
int buf_size,
int* bytes_read) override;
bool IsRedirectResponse(GURL* location, int* http_status_code) override;
net::Filter* SetupFilter() const override;
bool GetMimeType(std::string* mime_type) const override;
protected:
virtual ~URLRequestAsarJob();
void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers) override;
private:
// Meta information about the file. It's used as a member in the
// URLRequestFileJob and also passed between threads because disk access is
// necessary to obtain it.
struct FileMetaInfo {
FileMetaInfo();
// Size of the file.
int64 file_size;
// Mime type associated with the file.
std::string mime_type;
// Result returned from GetMimeTypeFromFile(), i.e. flag showing whether
// obtaining of the mime type was successful.
bool mime_type_result;
// Flag showing whether the file exists.
bool file_exists;
// Flag showing whether the file name actually refers to a directory.
bool is_directory;
};
// Fetches file info on a background thread.
static void FetchMetaInfo(const base::FilePath& file_path,
FileMetaInfo* meta_info);
// Callback after fetching file info on a background thread.
void DidFetchMetaInfo(const FileMetaInfo* meta_info);
// Callback after opening file on a background thread.
void DidOpen(int result);
@ -62,14 +100,24 @@ class URLRequestAsarJob : public net::URLRequestJob {
// Callback after data is asynchronously read from the file into |buf|.
void DidRead(scoped_refptr<net::IOBuffer> buf, int result);
// The type of this job.
enum JobType {
TYPE_ERROR,
TYPE_ASAR,
TYPE_FILE,
};
JobType type_;
std::shared_ptr<Archive> archive_;
base::FilePath file_path_;
Archive::FileInfo file_info_;
scoped_ptr<net::FileStream> stream_;
int64 remaining_bytes_;
FileMetaInfo meta_info_;
scoped_refptr<base::TaskRunner> file_task_runner_;
const scoped_refptr<base::TaskRunner> file_task_runner_;
net::HttpByteRange byte_range_;
int64 remaining_bytes_;
base::WeakPtrFactory<URLRequestAsarJob> weak_ptr_factory_;