Make api::Protocol thread safe
This commit is contained in:
parent
659384383e
commit
fe0e17d1c3
4 changed files with 94 additions and 62 deletions
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include "atom/browser/net/atom_url_request_job_factory.h"
|
||||
|
||||
#include "base/memory/ptr_util.h"
|
||||
#include "base/stl_util.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "net/base/load_flags.h"
|
||||
|
@ -41,14 +42,24 @@ bool AtomURLRequestJobFactory::SetProtocolHandler(
|
|||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<ProtocolHandler> AtomURLRequestJobFactory::ReplaceProtocol(
|
||||
bool AtomURLRequestJobFactory::InterceptProtocol(
|
||||
const std::string& scheme,
|
||||
std::unique_ptr<ProtocolHandler> protocol_handler) {
|
||||
if (!ContainsKey(protocol_handler_map_, scheme))
|
||||
return nullptr;
|
||||
if (!ContainsKey(protocol_handler_map_, scheme) ||
|
||||
ContainsKey(original_protocols_, scheme))
|
||||
return false;
|
||||
ProtocolHandler* original_protocol_handler = protocol_handler_map_[scheme];
|
||||
protocol_handler_map_[scheme] = protocol_handler.release();
|
||||
return make_scoped_ptr(original_protocol_handler);
|
||||
original_protocols_.set(scheme, base::WrapUnique(original_protocol_handler));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AtomURLRequestJobFactory::UninterceptProtocol(const std::string& scheme) {
|
||||
if (!original_protocols_.contains(scheme))
|
||||
return false;
|
||||
protocol_handler_map_[scheme] =
|
||||
original_protocols_.take_and_erase(scheme).release();
|
||||
return true;
|
||||
}
|
||||
|
||||
ProtocolHandler* AtomURLRequestJobFactory::GetProtocolHandler(
|
||||
|
|
|
@ -7,11 +7,11 @@
|
|||
#define ATOM_BROWSER_NET_ATOM_URL_REQUEST_JOB_FACTORY_H_
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
#include "base/synchronization/lock.h"
|
||||
#include "base/containers/scoped_ptr_hash_map.h"
|
||||
#include "net/url_request/url_request_job_factory.h"
|
||||
|
||||
namespace atom {
|
||||
|
@ -27,11 +27,11 @@ class AtomURLRequestJobFactory : public net::URLRequestJobFactory {
|
|||
bool SetProtocolHandler(const std::string& scheme,
|
||||
std::unique_ptr<ProtocolHandler> protocol_handler);
|
||||
|
||||
// Intercepts the ProtocolHandler for a scheme. Returns the original protocol
|
||||
// handler on success, otherwise returns NULL.
|
||||
std::unique_ptr<ProtocolHandler> ReplaceProtocol(
|
||||
// Intercepts the ProtocolHandler for a scheme.
|
||||
bool InterceptProtocol(
|
||||
const std::string& scheme,
|
||||
std::unique_ptr<ProtocolHandler> protocol_handler);
|
||||
bool UninterceptProtocol(const std::string& scheme);
|
||||
|
||||
// Returns the protocol handler registered with scheme.
|
||||
ProtocolHandler* GetProtocolHandler(const std::string& scheme) const;
|
||||
|
@ -60,6 +60,12 @@ class AtomURLRequestJobFactory : public net::URLRequestJobFactory {
|
|||
|
||||
ProtocolHandlerMap protocol_handler_map_;
|
||||
|
||||
// Map that stores the original protocols of schemes.
|
||||
using OriginalProtocolsMap = base::ScopedPtrHashMap<
|
||||
std::string, std::unique_ptr<ProtocolHandler>>;
|
||||
// Can only be accessed in IO thread.
|
||||
OriginalProtocolsMap original_protocols_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(AtomURLRequestJobFactory);
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue