delay protocol object creation

This commit is contained in:
deepak1556 2016-05-08 16:44:14 +05:30
parent 9c71c9fa6a
commit 70dac71639
4 changed files with 52 additions and 85 deletions

View file

@ -7,7 +7,6 @@
#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/browser.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_buffer_job.h"
#include "atom/browser/net/url_request_fetch_job.h" #include "atom/browser/net/url_request_fetch_job.h"
@ -24,40 +23,11 @@ namespace atom {
namespace api { namespace api {
Protocol::Protocol(v8::Isolate* isolate) Protocol::Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context)
: request_context_getter_(nullptr), : request_context_getter_(browser_context->GetRequestContext()),
job_factory_(nullptr) { job_factory_(browser_context->job_factory()) {
if (Browser::Get()->is_ready()) {
OnWillFinishLaunching();
} else {
Browser::Get()->AddObserver(this);
}
Init(isolate);
}
Protocol::~Protocol() {
Browser::Get()->RemoveObserver(this);
}
void Protocol::OnWillFinishLaunching() {
auto browser_context = static_cast<atom::AtomBrowserContext*>(
atom::AtomBrowserMainParts::Get()->browser_context());
request_context_getter_ = browser_context->GetRequestContext();
job_factory_ = browser_context->job_factory();
CHECK(job_factory_); CHECK(job_factory_);
} Init(isolate);
void Protocol::RegisterStandardSchemes(
const std::vector<std::string>& schemes) {
if (Browser::Get()->is_ready()) {
isolate()->ThrowException(v8::Exception::Error(mate::StringToV8(
isolate(),
"protocol.registerStandardSchemes should be called before"
"app is ready")));
return;
}
for (const auto& scheme : schemes)
url::AddStandardScheme(scheme.c_str(), url::SCHEME_WITHOUT_PORT);
} }
void Protocol::RegisterServiceWorkerSchemes( void Protocol::RegisterServiceWorkerSchemes(
@ -69,10 +39,6 @@ void Protocol::UnregisterProtocol(
const std::string& scheme, mate::Arguments* args) { const std::string& scheme, mate::Arguments* args) {
CompletionCallback callback; CompletionCallback callback;
args->GetNext(&callback); args->GetNext(&callback);
if (!job_factory_) {
OnIOCompleted(callback, PROTOCOL_FAIL);
return;
}
content::BrowserThread::PostTaskAndReplyWithResult( content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::IO, FROM_HERE, content::BrowserThread::IO, FROM_HERE,
base::Bind(&Protocol::UnregisterProtocolInIO, base::Bind(&Protocol::UnregisterProtocolInIO,
@ -91,10 +57,6 @@ Protocol::ProtocolError Protocol::UnregisterProtocolInIO(
void Protocol::IsProtocolHandled(const std::string& scheme, void Protocol::IsProtocolHandled(const std::string& scheme,
const BooleanCallback& callback) { const BooleanCallback& callback) {
if (!job_factory_) {
callback.Run(false);
return;
}
content::BrowserThread::PostTaskAndReplyWithResult( content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::IO, FROM_HERE, content::BrowserThread::IO, FROM_HERE,
base::Bind(&Protocol::IsProtocolHandledInIO, base::Bind(&Protocol::IsProtocolHandledInIO,
@ -110,10 +72,6 @@ void Protocol::UninterceptProtocol(
const std::string& scheme, mate::Arguments* args) { const std::string& scheme, mate::Arguments* args) {
CompletionCallback callback; CompletionCallback callback;
args->GetNext(&callback); args->GetNext(&callback);
if (!job_factory_) {
OnIOCompleted(callback, PROTOCOL_FAIL);
return;
}
content::BrowserThread::PostTaskAndReplyWithResult( content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::IO, FROM_HERE, content::BrowserThread::IO, FROM_HERE,
base::Bind(&Protocol::UninterceptProtocolInIO, base::Bind(&Protocol::UninterceptProtocolInIO,
@ -160,15 +118,15 @@ std::string Protocol::ErrorCodeToString(ProtocolError error) {
} }
// static // static
mate::Handle<Protocol> Protocol::Create(v8::Isolate* isolate) { mate::Handle<Protocol> Protocol::Create(
return mate::CreateHandle(isolate, new Protocol(isolate)); v8::Isolate* isolate, AtomBrowserContext* browser_context) {
return mate::CreateHandle(isolate, new Protocol(isolate, browser_context));
} }
// static // static
void Protocol::BuildPrototype( void Protocol::BuildPrototype(
v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> prototype) { v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> prototype) {
mate::ObjectTemplateBuilder(isolate, prototype) mate::ObjectTemplateBuilder(isolate, prototype)
.SetMethod("registerStandardSchemes", &Protocol::RegisterStandardSchemes)
.SetMethod("registerServiceWorkerSchemes", .SetMethod("registerServiceWorkerSchemes",
&Protocol::RegisterServiceWorkerSchemes) &Protocol::RegisterServiceWorkerSchemes)
.SetMethod("registerStringProtocol", .SetMethod("registerStringProtocol",
@ -198,11 +156,24 @@ void Protocol::BuildPrototype(
namespace { namespace {
void RegisterStandardSchemes(
const std::vector<std::string>& schemes) {
for (const auto& scheme : schemes)
url::AddStandardScheme(scheme.c_str(), url::SCHEME_WITHOUT_PORT);
}
mate::Handle<atom::api::Protocol> CreateProtocol(v8::Isolate* isolate) {
auto browser_context = static_cast<atom::AtomBrowserContext*>(
atom::AtomBrowserMainParts::Get()->browser_context());
return atom::api::Protocol::Create(isolate, browser_context);
}
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused, void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) { v8::Local<v8::Context> context, void* priv) {
v8::Isolate* isolate = context->GetIsolate(); v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports); mate::Dictionary dict(isolate, exports);
dict.Set("protocol", atom::api::Protocol::Create(isolate)); dict.SetMethod("createProtocolObject", base::Bind(&CreateProtocol, isolate));
dict.SetMethod("registerStandardSchemes", &RegisterStandardSchemes);
} }
} // namespace } // namespace

View file

@ -9,7 +9,6 @@
#include <map> #include <map>
#include <vector> #include <vector>
#include "atom/browser/browser_observer.h"
#include "atom/browser/net/atom_url_request_job_factory.h" #include "atom/browser/net/atom_url_request_job_factory.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/containers/scoped_ptr_hash_map.h" #include "base/containers/scoped_ptr_hash_map.h"
@ -31,25 +30,21 @@ class AtomURLRequestJobFactory;
namespace api { namespace api {
class Protocol : public mate::Wrappable<Protocol>, class Protocol : public mate::Wrappable<Protocol> {
public BrowserObserver {
public: public:
using Handler = using Handler =
base::Callback<void(const net::URLRequest*, v8::Local<v8::Value>)>; base::Callback<void(const net::URLRequest*, v8::Local<v8::Value>)>;
using CompletionCallback = base::Callback<void(v8::Local<v8::Value>)>; using CompletionCallback = base::Callback<void(v8::Local<v8::Value>)>;
using BooleanCallback = base::Callback<void(bool)>; using BooleanCallback = base::Callback<void(bool)>;
static mate::Handle<Protocol> Create(v8::Isolate* isolate); static mate::Handle<Protocol> Create(
v8::Isolate* isolate, AtomBrowserContext* browser_context);
static void BuildPrototype(v8::Isolate* isolate, static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> prototype); v8::Local<v8::ObjectTemplate> prototype);
protected: protected:
explicit Protocol(v8::Isolate* isolate); Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context);
~Protocol();
// BrowserObserver:
void OnWillFinishLaunching() override;
private: private:
// Possible errors. // Possible errors.
@ -93,9 +88,6 @@ class Protocol : public mate::Wrappable<Protocol>,
DISALLOW_COPY_AND_ASSIGN(CustomProtocolHandler); DISALLOW_COPY_AND_ASSIGN(CustomProtocolHandler);
}; };
// Register schemes to standard scheme list.
void RegisterStandardSchemes(const std::vector<std::string>& schemes);
// Register schemes that can handle service worker. // Register schemes that can handle service worker.
void RegisterServiceWorkerSchemes(const std::vector<std::string>& schemes); void RegisterServiceWorkerSchemes(const std::vector<std::string>& schemes);
@ -106,10 +98,6 @@ class Protocol : public mate::Wrappable<Protocol>,
mate::Arguments* args) { mate::Arguments* args) {
CompletionCallback callback; CompletionCallback callback;
args->GetNext(&callback); args->GetNext(&callback);
if (!job_factory_) {
OnIOCompleted(callback, PROTOCOL_FAIL);
return;
}
content::BrowserThread::PostTaskAndReplyWithResult( content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::IO, FROM_HERE, content::BrowserThread::IO, FROM_HERE,
base::Bind(&Protocol::RegisterProtocolInIO<RequestJob>, base::Bind(&Protocol::RegisterProtocolInIO<RequestJob>,
@ -147,10 +135,6 @@ class Protocol : public mate::Wrappable<Protocol>,
mate::Arguments* args) { mate::Arguments* args) {
CompletionCallback callback; CompletionCallback callback;
args->GetNext(&callback); args->GetNext(&callback);
if (!job_factory_) {
OnIOCompleted(callback, PROTOCOL_FAIL);
return;
}
content::BrowserThread::PostTaskAndReplyWithResult( content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::IO, FROM_HERE, content::BrowserThread::IO, FROM_HERE,
base::Bind(&Protocol::InterceptProtocolInIO<RequestJob>, base::Bind(&Protocol::InterceptProtocolInIO<RequestJob>,

View file

@ -1,4 +1,6 @@
const protocol = process.atomBinding('protocol').protocol const app = require('electron').app
const {createProtocolObject, registerStandardSchemes} = process.atomBinding('protocol')
let protocol = null
// Warn about removed APIs. // Warn about removed APIs.
var logAndThrow = function (callback, message) { var logAndThrow = function (callback, message) {
@ -10,16 +12,29 @@ var logAndThrow = function (callback, message) {
} }
} }
protocol.registerProtocol = function (scheme, handler, callback) { exports.registerStandardSchemes = function (schemes) {
return logAndThrow(callback, 'registerProtocol API has been replaced by the register[File/Http/Buffer/String]Protocol API family, please switch to the new APIs.') if (app.isReady()) {
throw new Error('protocol.registerStandardSchemes should be called before app is ready')
}
registerStandardSchemes(schemes)
} }
protocol.isHandledProtocol = function (scheme, callback) { app.once('ready', function () {
return logAndThrow(callback, 'isHandledProtocol API has been replaced by isProtocolHandled.') protocol = createProtocolObject()
} // Be compatible with old APIs.
protocol.registerProtocol = function (scheme, handler, callback) {
return logAndThrow(callback, 'registerProtocol API has been replaced by the register[File/Http/Buffer/String]Protocol API family, please switch to the new APIs.')
}
protocol.interceptProtocol = function (scheme, handler, callback) { protocol.isHandledProtocol = function (scheme, callback) {
return logAndThrow(callback, 'interceptProtocol API has been replaced by the intercept[File/Http/Buffer/String]Protocol API family, please switch to the new APIs.') return logAndThrow(callback, 'isHandledProtocol API has been replaced by isProtocolHandled.')
} }
module.exports = protocol protocol.interceptProtocol = function (scheme, handler, callback) {
return logAndThrow(callback, 'interceptProtocol API has been replaced by the intercept[File/Http/Buffer/String]Protocol API family, please switch to the new APIs.')
}
for (let method in protocol) {
exports[method] = protocol[method].bind(protocol)
}
})

View file

@ -1,5 +1,4 @@
const electron = require('electron') const {app, protocol, BrowserWindow} = require('electron')
const app = electron.app
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
const url = require('url') const url = require('url')
@ -70,9 +69,7 @@ app.on('will-quit', function () {
// We can not use protocol or BrowserWindow until app is ready. // We can not use protocol or BrowserWindow until app is ready.
app.once('ready', function () { app.once('ready', function () {
var BrowserWindow, chromeExtensionHandler, i, init, len, protocol, srcDirectory var chromeExtensionHandler, i, init, len, srcDirectory
protocol = electron.protocol
BrowserWindow = electron.BrowserWindow
// Load persisted extensions. // Load persisted extensions.
loadedExtensionsPath = path.join(app.getPath('userData'), 'DevTools Extensions') loadedExtensionsPath = path.join(app.getPath('userData'), 'DevTools Extensions')