#include "atom/browser/api/atom_api_protocol.h"
#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/net/url_request_async_asar_job.h"
#include "atom/browser/net/url_request_buffer_job.h"
namespace api {
Protocol::Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context)
- : request_context_getter_(browser_context->GetRequestContext()),
- job_factory_(browser_context->job_factory()) {
- CHECK(job_factory_);
+ : browser_context_(browser_context),
+ request_context_getter_(browser_context->GetRequestContext()),
+ weak_factory_(this) {
+ CHECK(job_factory());
Init(isolate);
}
base::Bind(&Protocol::UnregisterProtocolInIO,
base::Unretained(this), scheme),
base::Bind(&Protocol::OnIOCompleted,
- base::Unretained(this), callback));
+ GetWeakPtr(), callback));
}
Protocol::ProtocolError Protocol::UnregisterProtocolInIO(
const std::string& scheme) {
- if (!job_factory_->HasProtocolHandler(scheme))
+ if (!job_factory()->HasProtocolHandler(scheme))
return PROTOCOL_NOT_REGISTERED;
- job_factory_->SetProtocolHandler(scheme, nullptr);
+ job_factory()->SetProtocolHandler(scheme, nullptr);
return PROTOCOL_OK;
}
void Protocol::IsProtocolHandled(const std::string& scheme,
- const BooleanCallback& callback) {
+ const BooleanCallback& callback) {
content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&Protocol::IsProtocolHandledInIO,
}
bool Protocol::IsProtocolHandledInIO(const std::string& scheme) {
- return job_factory_->IsHandledProtocol(scheme);
+ return job_factory()->IsHandledProtocol(scheme);
}
void Protocol::UninterceptProtocol(
base::Bind(&Protocol::UninterceptProtocolInIO,
base::Unretained(this), scheme),
base::Bind(&Protocol::OnIOCompleted,
- base::Unretained(this), callback));
+ GetWeakPtr(), callback));
}
Protocol::ProtocolError Protocol::UninterceptProtocolInIO(
const std::string& scheme) {
if (!original_protocols_.contains(scheme))
return PROTOCOL_NOT_INTERCEPTED;
- job_factory_->ReplaceProtocol(scheme,
- original_protocols_.take_and_erase(scheme));
+ job_factory()->ReplaceProtocol(scheme,
+ original_protocols_.take_and_erase(scheme));
return PROTOCOL_OK;
}
#include <vector>
#include "atom/browser/api/trackable_object.h"
+#include "atom/browser/atom_browser_context.h"
#include "atom/browser/net/atom_url_request_job_factory.h"
#include "base/callback.h"
#include "base/containers/scoped_ptr_hash_map.h"
+#include "base/memory/weak_ptr.h"
#include "content/public/browser/browser_thread.h"
#include "native_mate/arguments.h"
#include "native_mate/dictionary.h"
namespace atom {
-class AtomBrowserContext;
-class AtomURLRequestJobFactory;
-
namespace api {
class Protocol : public mate::TrackableObject<Protocol> {
protected:
Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context);
+ AtomURLRequestJobFactory* job_factory() const {
+ return browser_context_->job_factory();
+ }
+
+ base::WeakPtr<Protocol> GetWeakPtr() {
+ return weak_factory_.GetWeakPtr();
+ }
+
private:
// Possible errors.
enum ProtocolError {
base::Bind(&Protocol::RegisterProtocolInIO<RequestJob>,
base::Unretained(this), scheme, handler),
base::Bind(&Protocol::OnIOCompleted,
- base::Unretained(this), callback));
+ GetWeakPtr(), callback));
}
template<typename RequestJob>
ProtocolError RegisterProtocolInIO(const std::string& scheme,
const Handler& handler) {
- if (job_factory_->IsHandledProtocol(scheme))
+ if (job_factory()->IsHandledProtocol(scheme))
return PROTOCOL_REGISTERED;
std::unique_ptr<CustomProtocolHandler<RequestJob>> protocol_handler(
new CustomProtocolHandler<RequestJob>(
isolate(), request_context_getter_, handler));
- if (job_factory_->SetProtocolHandler(scheme, std::move(protocol_handler)))
+ if (job_factory()->SetProtocolHandler(scheme, std::move(protocol_handler)))
return PROTOCOL_OK;
else
return PROTOCOL_FAIL;
base::Bind(&Protocol::InterceptProtocolInIO<RequestJob>,
base::Unretained(this), scheme, handler),
base::Bind(&Protocol::OnIOCompleted,
- base::Unretained(this), callback));
+ GetWeakPtr(), callback));
}
template<typename RequestJob>
ProtocolError InterceptProtocolInIO(const std::string& scheme,
const Handler& handler) {
- if (!job_factory_->IsHandledProtocol(scheme))
+ if (!job_factory()->IsHandledProtocol(scheme))
return PROTOCOL_NOT_REGISTERED;
// It is possible a protocol is handled but can not be intercepted.
- if (!job_factory_->HasProtocolHandler(scheme))
+ if (!job_factory()->HasProtocolHandler(scheme))
return PROTOCOL_FAIL;
if (ContainsKey(original_protocols_, scheme))
return PROTOCOL_INTERCEPTED;
isolate(), request_context_getter_, handler));
original_protocols_.set(
scheme,
- job_factory_->ReplaceProtocol(scheme, std::move(protocol_handler)));
+ job_factory()->ReplaceProtocol(scheme, std::move(protocol_handler)));
return PROTOCOL_OK;
}
// Convert error code to string.
std::string ErrorCodeToString(ProtocolError error);
+ scoped_refptr<AtomBrowserContext> browser_context_;
net::URLRequestContextGetter* request_context_getter_;
// Map that stores the original protocols of schemes.
std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler>>;
OriginalProtocolsMap original_protocols_;
- AtomURLRequestJobFactory* job_factory_; // weak ref
+ base::WeakPtrFactory<Protocol> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(Protocol);
};