diff options
author | sammc <sammc@chromium.org> | 2015-02-25 01:45:24 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-02-25 09:46:45 +0000 |
commit | 352f74947a93a5633ea7614d8af5df1afe13ca77 (patch) | |
tree | f9497087782607f2e53496719e0b7850e24c16bd /net | |
parent | be80bf74e784f8ad15402c7aecf593b384c5c33b (diff) | |
download | chromium_src-352f74947a93a5633ea7614d8af5df1afe13ca77.zip chromium_src-352f74947a93a5633ea7614d8af5df1afe13ca77.tar.gz chromium_src-352f74947a93a5633ea7614d8af5df1afe13ca77.tar.bz2 |
Implement utility-side proxy resolver factory Mojo service.
BUG=11746
Review URL: https://codereview.chromium.org/918933002
Cr-Commit-Position: refs/heads/master@{#318010}
Diffstat (limited to 'net')
-rw-r--r-- | net/BUILD.gn | 3 | ||||
-rw-r--r-- | net/net.gyp | 3 | ||||
-rw-r--r-- | net/net.gypi | 1 | ||||
-rw-r--r-- | net/proxy/mojo_proxy_resolver_factory_impl.cc | 87 | ||||
-rw-r--r-- | net/proxy/mojo_proxy_resolver_factory_impl.h | 41 | ||||
-rw-r--r-- | net/proxy/mojo_proxy_resolver_factory_impl_unittest.cc | 123 |
6 files changed, 258 insertions, 0 deletions
diff --git a/net/BUILD.gn b/net/BUILD.gn index 351de60..2c731e9 100644 --- a/net/BUILD.gn +++ b/net/BUILD.gn @@ -824,6 +824,8 @@ if (use_v8_in_net && !is_android) { sources = [ "dns/host_resolver_mojo.cc", "dns/host_resolver_mojo.h", + "proxy/mojo_proxy_resolver_factory_impl.cc", + "proxy/mojo_proxy_resolver_factory_impl.h", "proxy/mojo_proxy_resolver_impl.cc", "proxy/mojo_proxy_resolver_impl.h", ] @@ -1397,6 +1399,7 @@ if (!is_android && !is_win && !is_mac) { sources -= [ "dns/host_resolver_mojo_unittest.cc", "dns/mojo_host_resolver_impl_unittest.cc", + "proxy/mojo_proxy_resolver_factory_impl_unittest.cc", "proxy/mojo_proxy_resolver_impl_unittest.cc", ] } diff --git a/net/net.gyp b/net/net.gyp index 20d3630..5ff2c51 100644 --- a/net/net.gyp +++ b/net/net.gyp @@ -718,6 +718,7 @@ 'sources!': [ 'dns/host_resolver_mojo_unittest.cc', 'dns/mojo_host_resolver_impl_unittest.cc', + 'proxy/mojo_proxy_resolver_factory_impl_unittest.cc', 'proxy/mojo_proxy_resolver_impl_unittest.cc', ], }, @@ -1285,6 +1286,8 @@ 'sources': [ 'dns/host_resolver_mojo.cc', 'dns/host_resolver_mojo.h', + 'proxy/mojo_proxy_resolver_factory_impl.cc', + 'proxy/mojo_proxy_resolver_factory_impl.h', 'proxy/mojo_proxy_resolver_impl.cc', 'proxy/mojo_proxy_resolver_impl.h', ], diff --git a/net/net.gypi b/net/net.gypi index 8d1fde9..4daff8e 100644 --- a/net/net.gypi +++ b/net/net.gypi @@ -1437,6 +1437,7 @@ 'proxy/dhcp_proxy_script_adapter_fetcher_win_unittest.cc', 'proxy/dhcp_proxy_script_fetcher_factory_unittest.cc', 'proxy/dhcp_proxy_script_fetcher_win_unittest.cc', + 'proxy/mojo_proxy_resolver_factory_impl_unittest.cc', 'proxy/mojo_proxy_resolver_impl_unittest.cc', 'proxy/multi_threaded_proxy_resolver_unittest.cc', 'proxy/network_delegate_error_observer_unittest.cc', diff --git a/net/proxy/mojo_proxy_resolver_factory_impl.cc b/net/proxy/mojo_proxy_resolver_factory_impl.cc new file mode 100644 index 0000000..52082bc --- /dev/null +++ b/net/proxy/mojo_proxy_resolver_factory_impl.cc @@ -0,0 +1,87 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/proxy/mojo_proxy_resolver_factory_impl.h" + +#include "net/dns/host_resolver_mojo.h" +#include "net/proxy/mojo_proxy_resolver_impl.h" +#include "net/proxy/proxy_resolver_v8.h" +#include "net/proxy/proxy_resolver_v8_tracing.h" +#include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h" + +namespace net { +namespace { + +scoped_ptr<ProxyResolver> CreateDefaultProxyResolver( + HostResolver* host_resolver) { + ProxyResolverV8::EnsureIsolateCreated(); + return make_scoped_ptr( + new ProxyResolverV8Tracing(host_resolver, nullptr, nullptr)); +} + +// A class to manage the lifetime of a MojoProxyResolverImpl and a +// HostResolverMojo. An instance will remain while the message pipes for both +// mojo connections remain open. +class MojoProxyResolverHolder : public mojo::ErrorHandler { + public: + MojoProxyResolverHolder( + const MojoProxyResolverFactoryImpl::Factory& proxy_resolver_factory, + mojo::InterfaceRequest<interfaces::ProxyResolver> request, + interfaces::HostResolverPtr host_resolver); + + private: + // mojo::ErrorHandler override. + void OnConnectionError() override; + + HostResolverMojo host_resolver_; + MojoProxyResolverImpl proxy_resolver_; + mojo::Binding<interfaces::ProxyResolver> binding_; + + DISALLOW_COPY_AND_ASSIGN(MojoProxyResolverHolder); +}; + +MojoProxyResolverHolder::MojoProxyResolverHolder( + const MojoProxyResolverFactoryImpl::Factory& proxy_resolver_factory, + mojo::InterfaceRequest<interfaces::ProxyResolver> request, + interfaces::HostResolverPtr host_resolver) + : host_resolver_(host_resolver.Pass(), + base::Bind(&MojoProxyResolverHolder::OnConnectionError, + base::Unretained(this))), + proxy_resolver_(proxy_resolver_factory.Run(&host_resolver_)), + binding_(&proxy_resolver_, request.Pass()) { + binding_.set_error_handler(this); +} + +void MojoProxyResolverHolder::OnConnectionError() { + delete this; +} + +} // namespace + +MojoProxyResolverFactoryImpl::MojoProxyResolverFactoryImpl( + const MojoProxyResolverFactoryImpl::Factory& proxy_resolver_factory, + mojo::InterfaceRequest<interfaces::ProxyResolverFactory> request) + : proxy_resolver_impl_factory_(proxy_resolver_factory), + binding_(this, request.Pass()) { +} + +MojoProxyResolverFactoryImpl::MojoProxyResolverFactoryImpl( + mojo::InterfaceRequest<interfaces::ProxyResolverFactory> request) + : MojoProxyResolverFactoryImpl(base::Bind(&CreateDefaultProxyResolver), + request.Pass()) { +} + +MojoProxyResolverFactoryImpl::~MojoProxyResolverFactoryImpl() = default; + +void MojoProxyResolverFactoryImpl::CreateResolver( + mojo::InterfaceRequest<interfaces::ProxyResolver> request, + interfaces::HostResolverPtr host_resolver) { + // The MojoProxyResolverHolder will delete itself when either |request| or + // |host_resolver| encounters a connection error, that is, when either the + // ProxyResolver client or the HostResolver implementation is deleted. + new MojoProxyResolverHolder(proxy_resolver_impl_factory_, request.Pass(), + host_resolver.Pass()); +} + +} // namespace net diff --git a/net/proxy/mojo_proxy_resolver_factory_impl.h b/net/proxy/mojo_proxy_resolver_factory_impl.h new file mode 100644 index 0000000..5b1bb1a --- /dev/null +++ b/net/proxy/mojo_proxy_resolver_factory_impl.h @@ -0,0 +1,41 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef NET_PROXY_MOJO_PROXY_RESOLVER_FACTORY_IMPL_H_ +#define NET_PROXY_MOJO_PROXY_RESOLVER_FACTORY_IMPL_H_ + +#include "base/callback.h" +#include "net/interfaces/proxy_resolver_service.mojom.h" +#include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h" + +namespace net { +class HostResolver; +class ProxyResolver; + +class MojoProxyResolverFactoryImpl : public interfaces::ProxyResolverFactory { + public: + using Factory = base::Callback<scoped_ptr<ProxyResolver>(HostResolver*)>; + + explicit MojoProxyResolverFactoryImpl( + mojo::InterfaceRequest<interfaces::ProxyResolverFactory> request); + MojoProxyResolverFactoryImpl( + const Factory& proxy_resolver_factory, + mojo::InterfaceRequest<interfaces::ProxyResolverFactory> request); + + ~MojoProxyResolverFactoryImpl() override; + + private: + // interfaces::ProxyResolverFactory override. + void CreateResolver(mojo::InterfaceRequest<interfaces::ProxyResolver> request, + interfaces::HostResolverPtr host_resolver) override; + + const Factory proxy_resolver_impl_factory_; + mojo::StrongBinding<interfaces::ProxyResolverFactory> binding_; + + DISALLOW_COPY_AND_ASSIGN(MojoProxyResolverFactoryImpl); +}; + +} // namespace net + +#endif // NET_PROXY_MOJO_PROXY_RESOLVER_FACTORY_IMPL_H_ diff --git a/net/proxy/mojo_proxy_resolver_factory_impl_unittest.cc b/net/proxy/mojo_proxy_resolver_factory_impl_unittest.cc new file mode 100644 index 0000000..860476a --- /dev/null +++ b/net/proxy/mojo_proxy_resolver_factory_impl_unittest.cc @@ -0,0 +1,123 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/proxy/mojo_proxy_resolver_factory_impl.h" + +#include "net/proxy/mock_proxy_resolver.h" +#include "net/test/event_waiter.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" +#include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h" + +namespace net { +namespace { + +class FakeProxyResolver : public MockAsyncProxyResolverExpectsBytes { + public: + explicit FakeProxyResolver(const base::Closure& on_destruction) + : on_destruction_(on_destruction) {} + + ~FakeProxyResolver() override { on_destruction_.Run(); } + + private: + const base::Closure on_destruction_; +}; + +} // namespace + +class MojoProxyResolverFactoryImplTest : public testing::Test, + public mojo::ErrorHandler { + protected: + enum Event { + NONE, + RESOLVER_CREATED, + CONNECTION_ERROR, + RESOLVER_DESTROYED, + }; + + void SetUp() override { + new MojoProxyResolverFactoryImpl( + base::Bind(&MojoProxyResolverFactoryImplTest::CreateFakeProxyResolver, + base::Unretained(this)), + mojo::GetProxy(&factory_)); + } + + void OnConnectionError() override { waiter_.NotifyEvent(CONNECTION_ERROR); } + + scoped_ptr<ProxyResolver> CreateFakeProxyResolver( + HostResolver* host_resolver) { + EXPECT_TRUE(host_resolver); + instances_created_++; + waiter_.NotifyEvent(RESOLVER_CREATED); + return make_scoped_ptr(new FakeProxyResolver(base::Bind( + &MojoProxyResolverFactoryImplTest::OnFakeProxyInstanceDestroyed, + base::Unretained(this)))); + } + + void OnFakeProxyInstanceDestroyed() { + instances_destroyed_++; + waiter_.NotifyEvent(RESOLVER_DESTROYED); + } + + interfaces::ProxyResolverFactoryPtr factory_; + + int instances_created_ = 0; + int instances_destroyed_ = 0; + + EventWaiter<Event> waiter_; +}; + +TEST_F(MojoProxyResolverFactoryImplTest, DisconnectHostResolver) { + interfaces::ProxyResolverPtr proxy_resolver; + interfaces::HostResolverPtr host_resolver; + mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request = + mojo::GetProxy(&host_resolver); + factory_->CreateResolver(mojo::GetProxy(&proxy_resolver), + host_resolver.Pass()); + proxy_resolver.set_error_handler(this); + waiter_.WaitForEvent(RESOLVER_CREATED); + EXPECT_EQ(1, instances_created_); + EXPECT_EQ(0, instances_destroyed_); + host_resolver_request = mojo::InterfaceRequest<interfaces::HostResolver>(); + waiter_.WaitForEvent(CONNECTION_ERROR); + EXPECT_EQ(1, instances_created_); + EXPECT_EQ(1, instances_destroyed_); +} + +TEST_F(MojoProxyResolverFactoryImplTest, DisconnectProxyResolverClient) { + interfaces::ProxyResolverPtr proxy_resolver; + interfaces::HostResolverPtr host_resolver; + mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request = + mojo::GetProxy(&host_resolver); + mojo::Binding<interfaces::HostResolver> binding(nullptr, &host_resolver); + binding.set_error_handler(this); + factory_->CreateResolver(mojo::GetProxy(&proxy_resolver), + host_resolver.Pass()); + waiter_.WaitForEvent(RESOLVER_CREATED); + EXPECT_EQ(1, instances_created_); + EXPECT_EQ(0, instances_destroyed_); + proxy_resolver.reset(); + waiter_.WaitForEvent(CONNECTION_ERROR); + EXPECT_EQ(1, instances_created_); + EXPECT_EQ(1, instances_destroyed_); +} + +TEST_F(MojoProxyResolverFactoryImplTest, DisconnectBoth) { + interfaces::ProxyResolverPtr proxy_resolver; + interfaces::HostResolverPtr host_resolver; + mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request = + mojo::GetProxy(&host_resolver); + factory_->CreateResolver(mojo::GetProxy(&proxy_resolver), + host_resolver.Pass()); + waiter_.WaitForEvent(RESOLVER_CREATED); + EXPECT_EQ(1, instances_created_); + EXPECT_EQ(0, instances_destroyed_); + proxy_resolver.reset(); + host_resolver_request = mojo::InterfaceRequest<interfaces::HostResolver>(); + waiter_.WaitForEvent(RESOLVER_DESTROYED); + EXPECT_EQ(1, instances_created_); + EXPECT_EQ(1, instances_destroyed_); +} + +} // namespace net |