delay protocol object creation
This commit is contained in:
parent
9c71c9fa6a
commit
70dac71639
4 changed files with 52 additions and 85 deletions
|
@ -7,7 +7,6 @@
|
|||
#include "atom/browser/atom_browser_client.h"
|
||||
#include "atom/browser/atom_browser_context.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_buffer_job.h"
|
||||
#include "atom/browser/net/url_request_fetch_job.h"
|
||||
|
@ -24,40 +23,11 @@ namespace atom {
|
|||
|
||||
namespace api {
|
||||
|
||||
Protocol::Protocol(v8::Isolate* isolate)
|
||||
: request_context_getter_(nullptr),
|
||||
job_factory_(nullptr) {
|
||||
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();
|
||||
Protocol::Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context)
|
||||
: request_context_getter_(browser_context->GetRequestContext()),
|
||||
job_factory_(browser_context->job_factory()) {
|
||||
CHECK(job_factory_);
|
||||
}
|
||||
|
||||
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);
|
||||
Init(isolate);
|
||||
}
|
||||
|
||||
void Protocol::RegisterServiceWorkerSchemes(
|
||||
|
@ -69,10 +39,6 @@ void Protocol::UnregisterProtocol(
|
|||
const std::string& scheme, mate::Arguments* args) {
|
||||
CompletionCallback callback;
|
||||
args->GetNext(&callback);
|
||||
if (!job_factory_) {
|
||||
OnIOCompleted(callback, PROTOCOL_FAIL);
|
||||
return;
|
||||
}
|
||||
content::BrowserThread::PostTaskAndReplyWithResult(
|
||||
content::BrowserThread::IO, FROM_HERE,
|
||||
base::Bind(&Protocol::UnregisterProtocolInIO,
|
||||
|
@ -91,10 +57,6 @@ Protocol::ProtocolError Protocol::UnregisterProtocolInIO(
|
|||
|
||||
void Protocol::IsProtocolHandled(const std::string& scheme,
|
||||
const BooleanCallback& callback) {
|
||||
if (!job_factory_) {
|
||||
callback.Run(false);
|
||||
return;
|
||||
}
|
||||
content::BrowserThread::PostTaskAndReplyWithResult(
|
||||
content::BrowserThread::IO, FROM_HERE,
|
||||
base::Bind(&Protocol::IsProtocolHandledInIO,
|
||||
|
@ -110,10 +72,6 @@ void Protocol::UninterceptProtocol(
|
|||
const std::string& scheme, mate::Arguments* args) {
|
||||
CompletionCallback callback;
|
||||
args->GetNext(&callback);
|
||||
if (!job_factory_) {
|
||||
OnIOCompleted(callback, PROTOCOL_FAIL);
|
||||
return;
|
||||
}
|
||||
content::BrowserThread::PostTaskAndReplyWithResult(
|
||||
content::BrowserThread::IO, FROM_HERE,
|
||||
base::Bind(&Protocol::UninterceptProtocolInIO,
|
||||
|
@ -160,15 +118,15 @@ std::string Protocol::ErrorCodeToString(ProtocolError error) {
|
|||
}
|
||||
|
||||
// static
|
||||
mate::Handle<Protocol> Protocol::Create(v8::Isolate* isolate) {
|
||||
return mate::CreateHandle(isolate, new Protocol(isolate));
|
||||
mate::Handle<Protocol> Protocol::Create(
|
||||
v8::Isolate* isolate, AtomBrowserContext* browser_context) {
|
||||
return mate::CreateHandle(isolate, new Protocol(isolate, browser_context));
|
||||
}
|
||||
|
||||
// static
|
||||
void Protocol::BuildPrototype(
|
||||
v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> prototype) {
|
||||
mate::ObjectTemplateBuilder(isolate, prototype)
|
||||
.SetMethod("registerStandardSchemes", &Protocol::RegisterStandardSchemes)
|
||||
.SetMethod("registerServiceWorkerSchemes",
|
||||
&Protocol::RegisterServiceWorkerSchemes)
|
||||
.SetMethod("registerStringProtocol",
|
||||
|
@ -198,11 +156,24 @@ void Protocol::BuildPrototype(
|
|||
|
||||
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,
|
||||
v8::Local<v8::Context> context, void* priv) {
|
||||
v8::Isolate* isolate = context->GetIsolate();
|
||||
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
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include "atom/browser/browser_observer.h"
|
||||
#include "atom/browser/net/atom_url_request_job_factory.h"
|
||||
#include "base/callback.h"
|
||||
#include "base/containers/scoped_ptr_hash_map.h"
|
||||
|
@ -31,25 +30,21 @@ class AtomURLRequestJobFactory;
|
|||
|
||||
namespace api {
|
||||
|
||||
class Protocol : public mate::Wrappable<Protocol>,
|
||||
public BrowserObserver {
|
||||
class Protocol : public mate::Wrappable<Protocol> {
|
||||
public:
|
||||
using Handler =
|
||||
base::Callback<void(const net::URLRequest*, v8::Local<v8::Value>)>;
|
||||
using CompletionCallback = base::Callback<void(v8::Local<v8::Value>)>;
|
||||
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,
|
||||
v8::Local<v8::ObjectTemplate> prototype);
|
||||
|
||||
protected:
|
||||
explicit Protocol(v8::Isolate* isolate);
|
||||
~Protocol();
|
||||
|
||||
// BrowserObserver:
|
||||
void OnWillFinishLaunching() override;
|
||||
Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context);
|
||||
|
||||
private:
|
||||
// Possible errors.
|
||||
|
@ -93,9 +88,6 @@ class Protocol : public mate::Wrappable<Protocol>,
|
|||
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.
|
||||
void RegisterServiceWorkerSchemes(const std::vector<std::string>& schemes);
|
||||
|
||||
|
@ -106,10 +98,6 @@ class Protocol : public mate::Wrappable<Protocol>,
|
|||
mate::Arguments* args) {
|
||||
CompletionCallback callback;
|
||||
args->GetNext(&callback);
|
||||
if (!job_factory_) {
|
||||
OnIOCompleted(callback, PROTOCOL_FAIL);
|
||||
return;
|
||||
}
|
||||
content::BrowserThread::PostTaskAndReplyWithResult(
|
||||
content::BrowserThread::IO, FROM_HERE,
|
||||
base::Bind(&Protocol::RegisterProtocolInIO<RequestJob>,
|
||||
|
@ -147,10 +135,6 @@ class Protocol : public mate::Wrappable<Protocol>,
|
|||
mate::Arguments* args) {
|
||||
CompletionCallback callback;
|
||||
args->GetNext(&callback);
|
||||
if (!job_factory_) {
|
||||
OnIOCompleted(callback, PROTOCOL_FAIL);
|
||||
return;
|
||||
}
|
||||
content::BrowserThread::PostTaskAndReplyWithResult(
|
||||
content::BrowserThread::IO, FROM_HERE,
|
||||
base::Bind(&Protocol::InterceptProtocolInIO<RequestJob>,
|
||||
|
|
|
@ -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.
|
||||
var logAndThrow = function (callback, message) {
|
||||
|
@ -10,16 +12,29 @@ var logAndThrow = function (callback, message) {
|
|||
}
|
||||
}
|
||||
|
||||
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.')
|
||||
exports.registerStandardSchemes = function (schemes) {
|
||||
if (app.isReady()) {
|
||||
throw new Error('protocol.registerStandardSchemes should be called before app is ready')
|
||||
}
|
||||
registerStandardSchemes(schemes)
|
||||
}
|
||||
|
||||
protocol.isHandledProtocol = function (scheme, callback) {
|
||||
return logAndThrow(callback, 'isHandledProtocol API has been replaced by isProtocolHandled.')
|
||||
}
|
||||
app.once('ready', function () {
|
||||
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) {
|
||||
return logAndThrow(callback, 'interceptProtocol API has been replaced by the intercept[File/Http/Buffer/String]Protocol API family, please switch to the new APIs.')
|
||||
}
|
||||
protocol.isHandledProtocol = function (scheme, callback) {
|
||||
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)
|
||||
}
|
||||
})
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
const electron = require('electron')
|
||||
const app = electron.app
|
||||
const {app, protocol, BrowserWindow} = require('electron')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const url = require('url')
|
||||
|
@ -70,9 +69,7 @@ app.on('will-quit', function () {
|
|||
|
||||
// We can not use protocol or BrowserWindow until app is ready.
|
||||
app.once('ready', function () {
|
||||
var BrowserWindow, chromeExtensionHandler, i, init, len, protocol, srcDirectory
|
||||
protocol = electron.protocol
|
||||
BrowserWindow = electron.BrowserWindow
|
||||
var chromeExtensionHandler, i, init, len, srcDirectory
|
||||
|
||||
// Load persisted extensions.
|
||||
loadedExtensionsPath = path.join(app.getPath('userData'), 'DevTools Extensions')
|
||||
|
|
Loading…
Reference in a new issue