Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: switch to mojo proxy resolver (3-1-x) #15813

Merged
merged 6 commits into from Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 13 additions & 13 deletions atom/browser/api/atom_api_cookies.cc
Expand Up @@ -5,7 +5,7 @@
#include "atom/browser/api/atom_api_cookies.h"

#include "atom/browser/atom_browser_context.h"
#include "atom/browser/request_context_delegate.h"
#include "atom/browser/cookie_change_notifier.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "atom/common/native_mate_converters/value_converter.h"
Expand Down Expand Up @@ -56,20 +56,21 @@ struct Converter<net::CanonicalCookie> {
};

template <>
struct Converter<net::CookieChangeCause> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const net::CookieChangeCause& val) {
struct Converter<network::mojom::CookieChangeCause> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
const network::mojom::CookieChangeCause& val) {
switch (val) {
case net::CookieChangeCause::INSERTED:
case net::CookieChangeCause::EXPLICIT:
case network::mojom::CookieChangeCause::INSERTED:
case network::mojom::CookieChangeCause::EXPLICIT:
return mate::StringToV8(isolate, "explicit");
case net::CookieChangeCause::OVERWRITE:
case network::mojom::CookieChangeCause::OVERWRITE:
return mate::StringToV8(isolate, "overwrite");
case net::CookieChangeCause::EXPIRED:
case network::mojom::CookieChangeCause::EXPIRED:
return mate::StringToV8(isolate, "expired");
case net::CookieChangeCause::EVICTED:
case network::mojom::CookieChangeCause::EVICTED:
return mate::StringToV8(isolate, "evicted");
case net::CookieChangeCause::EXPIRED_OVERWRITE:
case network::mojom::CookieChangeCause::EXPIRED_OVERWRITE:
return mate::StringToV8(isolate, "expired-overwrite");
default:
return mate::StringToV8(isolate, "unknown");
Expand Down Expand Up @@ -255,9 +256,8 @@ Cookies::Cookies(v8::Isolate* isolate, AtomBrowserContext* browser_context)
: browser_context_(browser_context) {
Init(isolate);
cookie_change_subscription_ =
browser_context->GetRequestContextDelegate()
->RegisterCookieChangeCallback(
base::Bind(&Cookies::OnCookieChanged, base::Unretained(this)));
browser_context_->cookie_change_notifier()->RegisterCookieChangeCallback(
base::Bind(&Cookies::OnCookieChanged, base::Unretained(this)));
}

Cookies::~Cookies() {}
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/api/atom_api_cookies.h
Expand Up @@ -55,7 +55,7 @@ class Cookies : public mate::TrackableObject<Cookies> {
void Set(const base::DictionaryValue& details, const SetCallback& callback);
void FlushStore(const base::Closure& callback);

// AtomBrowserContext::RegisterCookieChangeCallback subscription:
// CookieChangeNotifier subscription:
void OnCookieChanged(const CookieDetails*);

private:
Expand Down
123 changes: 85 additions & 38 deletions atom/browser/api/atom_api_net_log.cc
Expand Up @@ -3,31 +3,37 @@
// found in the LICENSE file.

#include "atom/browser/api/atom_api_net_log.h"
#include "atom/browser/atom_browser_client.h"

#include <utility>

#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "base/callback.h"
#include "content/public/common/content_switches.h"
#include "base/command_line.h"
#include "components/net_log/chrome_net_log.h"
#include "content/public/browser/storage_partition.h"
#include "native_mate/dictionary.h"
#include "native_mate/handle.h"
#include "net/url_request/url_request_context_getter.h"

#include "atom/common/node_includes.h"

namespace atom {

namespace api {

NetLog::NetLog(v8::Isolate* isolate) {
NetLog::NetLog(v8::Isolate* isolate, AtomBrowserContext* browser_context)
: browser_context_(browser_context) {
Init(isolate);

net_log_ = atom::AtomBrowserClient::Get()->GetNetLog();
net_log_writer_ =
atom::AtomBrowserMainParts::Get()->net_log()->net_export_file_writer();
net_log_writer_->AddObserver(this);
}

NetLog::~NetLog() {}

// static
v8::Local<v8::Value> NetLog::Create(v8::Isolate* isolate) {
return mate::CreateHandle(isolate, new NetLog(isolate)).ToV8();
NetLog::~NetLog() {
net_log_writer_->RemoveObserver(this);
}

void NetLog::StartLogging(mate::Arguments* args) {
Expand All @@ -37,22 +43,82 @@ void NetLog::StartLogging(mate::Arguments* args) {
return;
}

net_log_->StartDynamicLogging(log_path);
net_log::NetExportFileWriter::URLRequestContextGetterList context_getters = {
browser_context_->GetRequestContext()};

// TODO(deepak1556): Provide more flexibility to this module
// by allowing customizations on the capturing options.
net_log_writer_->StartNetLog(
log_path, net::NetLogCaptureMode::Default(),
net_log::NetExportFileWriter::kNoLimit /* file size limit */,
base::CommandLine::ForCurrentProcess()->GetCommandLineString(),
std::string(), context_getters);
}

std::string NetLog::GetLoggingState() const {
if (!net_log_state_)
return std::string();
const base::Value* current_log_state =
net_log_state_->FindKeyOfType("state", base::Value::Type::STRING);
if (!current_log_state)
return std::string();
return current_log_state->GetString();
}

bool NetLog::IsCurrentlyLogging() {
return net_log_->IsDynamicLogging();
bool NetLog::IsCurrentlyLogging() const {
const std::string log_state = GetLoggingState();
return (log_state == "STARTING_LOG") || (log_state == "LOGGING");
}

base::FilePath::StringType NetLog::GetCurrentlyLoggingPath() {
return net_log_->GetDynamicLoggingPath().value();
std::string NetLog::GetCurrentlyLoggingPath() const {
// Net log exporter has a default path which will be used
// when no log path is provided, but since we don't allow
// net log capture without user provided file path, this
// check is completely safe.
if (IsCurrentlyLogging()) {
const base::Value* current_log_path =
net_log_state_->FindKeyOfType("file", base::Value::Type::STRING);
if (current_log_path)
return current_log_path->GetString();
}

return std::string();
}

void NetLog::StopLogging(mate::Arguments* args) {
base::OnceClosure callback;
args->GetNext(&callback);
net_log::NetExportFileWriter::FilePathCallback callback;
if (!args->GetNext(&callback)) {
args->ThrowError("Invalid callback function");
return;
}

if (IsCurrentlyLogging()) {
stop_callback_queue_.emplace_back(callback);
net_log_writer_->StopNetLog(nullptr, nullptr);
} else {
callback.Run(base::FilePath());
}
}

net_log_->StopDynamicLogging(std::move(callback));
void NetLog::OnNewState(const base::DictionaryValue& state) {
net_log_state_ = state.CreateDeepCopy();

if (stop_callback_queue_.empty())
return;

if (GetLoggingState() == "NOT_LOGGING") {
for (auto& callback : stop_callback_queue_) {
if (!callback.is_null())
net_log_writer_->GetFilePathToCompletedLog(callback);
}
stop_callback_queue_.clear();
}
}

// static
mate::Handle<NetLog> NetLog::Create(v8::Isolate* isolate,
AtomBrowserContext* browser_context) {
return mate::CreateHandle(isolate, new NetLog(isolate, browser_context));
}

// static
Expand All @@ -63,28 +129,9 @@ void NetLog::BuildPrototype(v8::Isolate* isolate,
.SetProperty("currentlyLogging", &NetLog::IsCurrentlyLogging)
.SetProperty("currentlyLoggingPath", &NetLog::GetCurrentlyLoggingPath)
.SetMethod("startLogging", &NetLog::StartLogging)
.SetMethod("_stopLogging", &NetLog::StopLogging);
.SetMethod("stopLogging", &NetLog::StopLogging);
}

} // namespace api

} // namespace atom

namespace {

using atom::api::NetLog;

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("netLog", NetLog::Create(isolate));
dict.Set("NetLog", NetLog::GetConstructor(isolate)->GetFunction());
}

} // namespace

NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_net_log, Initialize)
34 changes: 26 additions & 8 deletions atom/browser/api/atom_api_net_log.h
Expand Up @@ -5,32 +5,50 @@
#ifndef ATOM_BROWSER_API_ATOM_API_NET_LOG_H_
#define ATOM_BROWSER_API_ATOM_API_NET_LOG_H_

#include <list>
#include <memory>
#include <string>
#include "brightray/browser/net_log.h"
#include "native_mate/wrappable.h"

#include "atom/browser/api/trackable_object.h"
#include "base/callback.h"
#include "base/values.h"
#include "components/net_log/net_export_file_writer.h"
#include "native_mate/handle.h"

namespace atom {

class AtomBrowserContext;

namespace api {

class NetLog : public mate::Wrappable<NetLog> {
class NetLog : public mate::TrackableObject<NetLog>,
public net_log::NetExportFileWriter::StateObserver {
public:
static v8::Local<v8::Value> Create(v8::Isolate* isolate);
static mate::Handle<NetLog> Create(v8::Isolate* isolate,
AtomBrowserContext* browser_context);

static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

void StartLogging(mate::Arguments* args);
bool IsCurrentlyLogging();
base::FilePath::StringType GetCurrentlyLoggingPath();
std::string GetLoggingState() const;
bool IsCurrentlyLogging() const;
std::string GetCurrentlyLoggingPath() const;
void StopLogging(mate::Arguments* args);

protected:
explicit NetLog(v8::Isolate* isolate);
explicit NetLog(v8::Isolate* isolate, AtomBrowserContext* browser_context);
~NetLog() override;

// net_log::NetExportFileWriter::StateObserver implementation
void OnNewState(const base::DictionaryValue& state) override;

private:
brightray::NetLog* net_log_;
AtomBrowserContext* browser_context_;
net_log::NetExportFileWriter* net_log_writer_;
std::list<net_log::NetExportFileWriter::FilePathCallback>
stop_callback_queue_;
std::unique_ptr<base::DictionaryValue> net_log_state_;

DISALLOW_COPY_AND_ASSIGN(NetLog);
};
Expand Down
22 changes: 11 additions & 11 deletions atom/browser/api/atom_api_protocol.cc
Expand Up @@ -83,7 +83,8 @@ void Protocol::UnregisterProtocol(const std::string& scheme,
mate::Arguments* args) {
CompletionCallback callback;
args->GetNext(&callback);
auto* getter = browser_context_->GetRequestContext();
auto* getter = static_cast<URLRequestContextGetter*>(
browser_context_->GetRequestContext());
content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::IO, FROM_HERE,
base::BindOnce(&Protocol::UnregisterProtocolInIO,
Expand All @@ -93,10 +94,9 @@ void Protocol::UnregisterProtocol(const std::string& scheme,

// static
Protocol::ProtocolError Protocol::UnregisterProtocolInIO(
scoped_refptr<brightray::URLRequestContextGetter> request_context_getter,
scoped_refptr<URLRequestContextGetter> request_context_getter,
const std::string& scheme) {
auto* job_factory = static_cast<AtomURLRequestJobFactory*>(
request_context_getter->job_factory());
auto* job_factory = request_context_getter->job_factory();
if (!job_factory->HasProtocolHandler(scheme))
return PROTOCOL_NOT_REGISTERED;
job_factory->SetProtocolHandler(scheme, nullptr);
Expand All @@ -105,7 +105,8 @@ Protocol::ProtocolError Protocol::UnregisterProtocolInIO(

void Protocol::IsProtocolHandled(const std::string& scheme,
const BooleanCallback& callback) {
auto* getter = browser_context_->GetRequestContext();
auto* getter = static_cast<URLRequestContextGetter*>(
browser_context_->GetRequestContext());
content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&Protocol::IsProtocolHandledInIO, base::RetainedRef(getter),
Expand All @@ -115,7 +116,7 @@ void Protocol::IsProtocolHandled(const std::string& scheme,

// static
bool Protocol::IsProtocolHandledInIO(
scoped_refptr<brightray::URLRequestContextGetter> request_context_getter,
scoped_refptr<URLRequestContextGetter> request_context_getter,
const std::string& scheme) {
return request_context_getter->job_factory()->IsHandledProtocol(scheme);
}
Expand All @@ -124,7 +125,8 @@ void Protocol::UninterceptProtocol(const std::string& scheme,
mate::Arguments* args) {
CompletionCallback callback;
args->GetNext(&callback);
auto* getter = browser_context_->GetRequestContext();
auto* getter = static_cast<URLRequestContextGetter*>(
browser_context_->GetRequestContext());
content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::IO, FROM_HERE,
base::BindOnce(&Protocol::UninterceptProtocolInIO,
Expand All @@ -134,11 +136,9 @@ void Protocol::UninterceptProtocol(const std::string& scheme,

// static
Protocol::ProtocolError Protocol::UninterceptProtocolInIO(
scoped_refptr<brightray::URLRequestContextGetter> request_context_getter,
scoped_refptr<URLRequestContextGetter> request_context_getter,
const std::string& scheme) {
return static_cast<AtomURLRequestJobFactory*>(
request_context_getter->job_factory())
->UninterceptProtocol(scheme)
return request_context_getter->job_factory()->UninterceptProtocol(scheme)
? PROTOCOL_OK
: PROTOCOL_NOT_INTERCEPTED;
}
Expand Down