Implement protocol.registerBufferProtocol

This commit is contained in:
Cheng Zhao 2015-08-12 20:37:52 +08:00
parent ebb1ddc0df
commit 1f2d7d1cd8
4 changed files with 44 additions and 17 deletions

View file

@ -7,8 +7,9 @@
#include "atom/browser/atom_browser_client.h" #include "atom/browser/atom_browser_client.h"
#include "atom/browser/atom_browser_context.h" #include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h" #include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/net/url_request_string_job.h"
#include "atom/browser/net/url_request_async_asar_job.h" #include "atom/browser/net/url_request_async_asar_job.h"
#include "atom/browser/net/url_request_buffer_job.h"
#include "atom/browser/net/url_request_string_job.h"
#include "atom/common/native_mate_converters/callback.h" #include "atom/common/native_mate_converters/callback.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
@ -48,6 +49,8 @@ mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder(
.SetMethod("registerStandardSchemes", &Protocol::RegisterStandardSchemes) .SetMethod("registerStandardSchemes", &Protocol::RegisterStandardSchemes)
.SetMethod("registerStringProtocol", .SetMethod("registerStringProtocol",
&Protocol::RegisterProtocol<URLRequestStringJob>) &Protocol::RegisterProtocol<URLRequestStringJob>)
.SetMethod("registerBufferProtocol",
&Protocol::RegisterProtocol<URLRequestBufferJob>)
.SetMethod("registerFileProtocol", .SetMethod("registerFileProtocol",
&Protocol::RegisterProtocol<UrlRequestAsyncAsarJob>); &Protocol::RegisterProtocol<UrlRequestAsyncAsarJob>);
} }

View file

@ -72,8 +72,8 @@ app.once 'ready', ->
directory = getPathForHost parsed.hostname directory = getPathForHost parsed.hostname
return callback() unless directory? return callback() unless directory?
callback path.join(directory, parsed.path) callback path.join(directory, parsed.path)
protocol.registerFileProtocol 'chrome-extension', chromeExtensionHandler, -> protocol.registerFileProtocol 'chrome-extension', chromeExtensionHandler, (error) ->
console.error 'Unable to register chrome-extension protocol' console.error 'Unable to register chrome-extension protocol' if error
BrowserWindow::_loadDevToolsExtensions = (extensionInfoArray) -> BrowserWindow::_loadDevToolsExtensions = (extensionInfoArray) ->
@devToolsWebContents?.executeJavaScript "DevToolsAPI.addExtensions(#{JSON.stringify(extensionInfoArray)});" @devToolsWebContents?.executeJavaScript "DevToolsAPI.addExtensions(#{JSON.stringify(extensionInfoArray)});"

View file

@ -13,13 +13,34 @@ namespace atom {
URLRequestBufferJob::URLRequestBufferJob( URLRequestBufferJob::URLRequestBufferJob(
net::URLRequest* request, net::URLRequest* request,
net::NetworkDelegate* network_delegate, net::NetworkDelegate* network_delegate,
const std::string& mime_type, v8::Isolate* isolate,
const std::string& charset, const JavaScriptHandler& handler)
scoped_refptr<base::RefCountedBytes> data) : JsAsker<net::URLRequestSimpleJob>(request, network_delegate, isolate,
: net::URLRequestSimpleJob(request, network_delegate), handler) {
mime_type_(mime_type), }
charset_(charset),
buffer_data_(data) { void URLRequestBufferJob::StartAsync(scoped_ptr<base::Value> options) {
const base::BinaryValue* binary = nullptr;
if (options->IsType(base::Value::TYPE_DICTIONARY)) {
base::DictionaryValue* dict =
static_cast<base::DictionaryValue*>(options.get());
dict->GetString("mimeType", &mime_type_);
dict->GetString("charset", &charset_);
dict->GetBinary("data", &binary);
} else if (options->IsType(base::Value::TYPE_BINARY)) {
options->GetAsBinary(&binary);
}
if (!binary) {
NotifyStartError(net::URLRequestStatus(
net::URLRequestStatus::FAILED, net::ERR_NOT_IMPLEMENTED));
return;
}
data_ = new base::RefCountedBytes(
reinterpret_cast<const unsigned char*>(binary->GetBuffer()),
binary->GetSize());
net::URLRequestSimpleJob::Start();
} }
int URLRequestBufferJob::GetRefCountedData( int URLRequestBufferJob::GetRefCountedData(
@ -29,7 +50,7 @@ int URLRequestBufferJob::GetRefCountedData(
const net::CompletionCallback& callback) const { const net::CompletionCallback& callback) const {
*mime_type = mime_type_; *mime_type = mime_type_;
*charset = charset_; *charset = charset_;
*data = buffer_data_; *data = data_;
return net::OK; return net::OK;
} }

View file

@ -7,19 +7,22 @@
#include <string> #include <string>
#include "atom/browser/net/js_asker.h"
#include "atom/common/node_includes.h"
#include "base/memory/ref_counted_memory.h" #include "base/memory/ref_counted_memory.h"
#include "net/url_request/url_request_simple_job.h" #include "net/url_request/url_request_simple_job.h"
#include "atom/common/node_includes.h"
namespace atom { namespace atom {
class URLRequestBufferJob : public net::URLRequestSimpleJob { class URLRequestBufferJob : public JsAsker<net::URLRequestSimpleJob> {
public: public:
URLRequestBufferJob(net::URLRequest* request, URLRequestBufferJob(net::URLRequest* request,
net::NetworkDelegate* network_delegate, net::NetworkDelegate* network_delegate,
const std::string& mime_type, v8::Isolate* isolate,
const std::string& charset, const JavaScriptHandler& handler);
scoped_refptr<base::RefCountedBytes> data);
// JsAsker:
void StartAsync(scoped_ptr<base::Value> options) override;
// URLRequestSimpleJob: // URLRequestSimpleJob:
int GetRefCountedData(std::string* mime_type, int GetRefCountedData(std::string* mime_type,
@ -30,7 +33,7 @@ class URLRequestBufferJob : public net::URLRequestSimpleJob {
private: private:
std::string mime_type_; std::string mime_type_;
std::string charset_; std::string charset_;
scoped_refptr<base::RefCountedBytes> buffer_data_; scoped_refptr<base::RefCountedBytes> data_;
DISALLOW_COPY_AND_ASSIGN(URLRequestBufferJob); DISALLOW_COPY_AND_ASSIGN(URLRequestBufferJob);
}; };