| OLD | NEW | 
|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "net/proxy/proxy_resolver_mojo.h" | 5 #include "net/proxy/proxy_resolver_mojo.h" | 
| 6 | 6 | 
| 7 #include <list> | 7 #include <list> | 
| 8 #include <map> | 8 #include <map> | 
| 9 #include <queue> | 9 #include <queue> | 
| 10 #include <string> | 10 #include <string> | 
| 11 | 11 | 
| 12 #include "base/bind.h" | 12 #include "base/bind.h" | 
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" | 
|  | 14 #include "base/memory/scoped_vector.h" | 
| 14 #include "base/run_loop.h" | 15 #include "base/run_loop.h" | 
| 15 #include "base/stl_util.h" | 16 #include "base/stl_util.h" | 
| 16 #include "mojo/common/common_type_converters.h" | 17 #include "mojo/common/common_type_converters.h" | 
| 17 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" | 
| 18 #include "net/base/net_log.h" | 19 #include "net/base/net_log.h" | 
| 19 #include "net/base/test_completion_callback.h" | 20 #include "net/base/test_completion_callback.h" | 
| 20 #include "net/dns/mock_host_resolver.h" | 21 #include "net/dns/mock_host_resolver.h" | 
| 21 #include "net/proxy/mojo_proxy_resolver_factory.h" | 22 #include "net/proxy/mojo_proxy_resolver_factory.h" | 
| 22 #include "net/proxy/mojo_proxy_type_converters.h" | 23 #include "net/proxy/mojo_proxy_type_converters.h" | 
| 23 #include "net/proxy/proxy_info.h" | 24 #include "net/proxy/proxy_info.h" | 
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 59 | 60 | 
| 60 struct GetProxyForUrlAction { | 61 struct GetProxyForUrlAction { | 
| 61   enum Action { | 62   enum Action { | 
| 62     COMPLETE, | 63     COMPLETE, | 
| 63     // Drop the request by closing the reply channel. | 64     // Drop the request by closing the reply channel. | 
| 64     DROP, | 65     DROP, | 
| 65     // Disconnect the service. | 66     // Disconnect the service. | 
| 66     DISCONNECT, | 67     DISCONNECT, | 
| 67     // Wait for the client pipe to be disconnected. | 68     // Wait for the client pipe to be disconnected. | 
| 68     WAIT_FOR_CLIENT_DISCONNECT, | 69     WAIT_FOR_CLIENT_DISCONNECT, | 
|  | 70     // Send a LoadStateChanged message and keep the client pipe open. | 
|  | 71     SEND_LOAD_STATE_AND_BLOCK, | 
| 69   }; | 72   }; | 
| 70 | 73 | 
| 71   GetProxyForUrlAction() {} | 74   GetProxyForUrlAction() {} | 
| 72   GetProxyForUrlAction(const GetProxyForUrlAction& old) { | 75   GetProxyForUrlAction(const GetProxyForUrlAction& old) { | 
| 73     action = old.action; | 76     action = old.action; | 
| 74     error = old.error; | 77     error = old.error; | 
| 75     expected_url = old.expected_url; | 78     expected_url = old.expected_url; | 
| 76     proxy_servers = old.proxy_servers.Clone(); | 79     proxy_servers = old.proxy_servers.Clone(); | 
| 77   } | 80   } | 
| 78 | 81 | 
| (...skipping 27 matching lines...) Expand all  Loading... | 
| 106     return result; | 109     return result; | 
| 107   } | 110   } | 
| 108 | 111 | 
| 109   static GetProxyForUrlAction WaitForClientDisconnect(const GURL& url) { | 112   static GetProxyForUrlAction WaitForClientDisconnect(const GURL& url) { | 
| 110     GetProxyForUrlAction result; | 113     GetProxyForUrlAction result; | 
| 111     result.expected_url = url; | 114     result.expected_url = url; | 
| 112     result.action = WAIT_FOR_CLIENT_DISCONNECT; | 115     result.action = WAIT_FOR_CLIENT_DISCONNECT; | 
| 113     return result; | 116     return result; | 
| 114   } | 117   } | 
| 115 | 118 | 
|  | 119   static GetProxyForUrlAction SendLoadStateChanged(const GURL& url) { | 
|  | 120     GetProxyForUrlAction result; | 
|  | 121     result.expected_url = url; | 
|  | 122     result.action = SEND_LOAD_STATE_AND_BLOCK; | 
|  | 123     return result; | 
|  | 124   } | 
|  | 125 | 
| 116   Action action = COMPLETE; | 126   Action action = COMPLETE; | 
| 117   Error error = OK; | 127   Error error = OK; | 
| 118   mojo::Array<interfaces::ProxyServerPtr> proxy_servers; | 128   mojo::Array<interfaces::ProxyServerPtr> proxy_servers; | 
| 119   GURL expected_url; | 129   GURL expected_url; | 
| 120 }; | 130 }; | 
| 121 | 131 | 
| 122 class MockMojoProxyResolver : public interfaces::ProxyResolver { | 132 class MockMojoProxyResolver : public interfaces::ProxyResolver { | 
| 123  public: | 133  public: | 
| 124   explicit MockMojoProxyResolver( | 134   explicit MockMojoProxyResolver( | 
| 125       mojo::InterfaceRequest<interfaces::ProxyResolver> req); | 135       mojo::InterfaceRequest<interfaces::ProxyResolver> req); | 
| 126   ~MockMojoProxyResolver() override; | 136   ~MockMojoProxyResolver() override; | 
| 127 | 137 | 
| 128   void AddPacScriptAction(SetPacScriptAction action); | 138   void AddPacScriptAction(SetPacScriptAction action); | 
| 129   // Returned script data is UTF8. | 139   // Returned script data is UTF8. | 
| 130   std::string pac_script_data() { return pac_script_data_; } | 140   std::string pac_script_data() { return pac_script_data_; } | 
| 131 | 141 | 
| 132   void AddGetProxyAction(GetProxyForUrlAction action); | 142   void AddGetProxyAction(GetProxyForUrlAction action); | 
| 133 | 143 | 
| 134   void WaitForNextRequest(); | 144   void WaitForNextRequest(); | 
| 135 | 145 | 
|  | 146   void ClearBlockedClients(); | 
|  | 147 | 
| 136  private: | 148  private: | 
| 137   // Overridden from interfaces::ProxyResolver: | 149   // Overridden from interfaces::ProxyResolver: | 
| 138   void SetPacScript(const mojo::String& data, | 150   void SetPacScript(const mojo::String& data, | 
| 139                     const mojo::Callback<void(int32_t)>& callback) override; | 151                     const mojo::Callback<void(int32_t)>& callback) override; | 
| 140   void GetProxyForUrl( | 152   void GetProxyForUrl( | 
| 141       const mojo::String& url, | 153       const mojo::String& url, | 
| 142       interfaces::ProxyResolverRequestClientPtr client) override; | 154       interfaces::ProxyResolverRequestClientPtr client) override; | 
| 143 | 155 | 
| 144   void WakeWaiter(); | 156   void WakeWaiter(); | 
| 145 | 157 | 
| 146   std::string pac_script_data_; | 158   std::string pac_script_data_; | 
| 147   std::queue<SetPacScriptAction> pac_script_actions_; | 159   std::queue<SetPacScriptAction> pac_script_actions_; | 
| 148 | 160 | 
| 149   std::queue<GetProxyForUrlAction> get_proxy_actions_; | 161   std::queue<GetProxyForUrlAction> get_proxy_actions_; | 
| 150 | 162 | 
| 151   base::Closure quit_closure_; | 163   base::Closure quit_closure_; | 
| 152 | 164 | 
|  | 165   ScopedVector<interfaces::ProxyResolverRequestClientPtr> blocked_clients_; | 
| 153   mojo::Binding<interfaces::ProxyResolver> binding_; | 166   mojo::Binding<interfaces::ProxyResolver> binding_; | 
| 154 }; | 167 }; | 
| 155 | 168 | 
| 156 MockMojoProxyResolver::MockMojoProxyResolver( | 169 MockMojoProxyResolver::MockMojoProxyResolver( | 
| 157     mojo::InterfaceRequest<interfaces::ProxyResolver> req) | 170     mojo::InterfaceRequest<interfaces::ProxyResolver> req) | 
| 158     : binding_(this, req.Pass()) { | 171     : binding_(this, req.Pass()) { | 
| 159 } | 172 } | 
| 160 | 173 | 
| 161 MockMojoProxyResolver::~MockMojoProxyResolver() { | 174 MockMojoProxyResolver::~MockMojoProxyResolver() { | 
| 162   EXPECT_TRUE(pac_script_actions_.empty()) | 175   EXPECT_TRUE(pac_script_actions_.empty()) | 
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 198     case SetPacScriptAction::COMPLETE: | 211     case SetPacScriptAction::COMPLETE: | 
| 199       callback.Run(action.error); | 212       callback.Run(action.error); | 
| 200       break; | 213       break; | 
| 201     case SetPacScriptAction::DISCONNECT: | 214     case SetPacScriptAction::DISCONNECT: | 
| 202       binding_.Close(); | 215       binding_.Close(); | 
| 203       break; | 216       break; | 
| 204   } | 217   } | 
| 205   WakeWaiter(); | 218   WakeWaiter(); | 
| 206 } | 219 } | 
| 207 | 220 | 
|  | 221 void MockMojoProxyResolver::ClearBlockedClients() { | 
|  | 222   blocked_clients_.clear(); | 
|  | 223 } | 
|  | 224 | 
| 208 void MockMojoProxyResolver::GetProxyForUrl( | 225 void MockMojoProxyResolver::GetProxyForUrl( | 
| 209     const mojo::String& url, | 226     const mojo::String& url, | 
| 210     interfaces::ProxyResolverRequestClientPtr client) { | 227     interfaces::ProxyResolverRequestClientPtr client) { | 
| 211   ASSERT_FALSE(get_proxy_actions_.empty()); | 228   ASSERT_FALSE(get_proxy_actions_.empty()); | 
| 212   GetProxyForUrlAction action = get_proxy_actions_.front(); | 229   GetProxyForUrlAction action = get_proxy_actions_.front(); | 
| 213   get_proxy_actions_.pop(); | 230   get_proxy_actions_.pop(); | 
| 214 | 231 | 
| 215   EXPECT_EQ(action.expected_url.spec(), url.To<std::string>()); | 232   EXPECT_EQ(action.expected_url.spec(), url.To<std::string>()); | 
| 216   switch (action.action) { | 233   switch (action.action) { | 
| 217     case GetProxyForUrlAction::COMPLETE: | 234     case GetProxyForUrlAction::COMPLETE: | 
| 218       client->ReportResult(action.error, action.proxy_servers.Pass()); | 235       client->ReportResult(action.error, action.proxy_servers.Pass()); | 
| 219       break; | 236       break; | 
| 220     case GetProxyForUrlAction::DROP: | 237     case GetProxyForUrlAction::DROP: | 
| 221       client.reset(); | 238       client.reset(); | 
| 222       break; | 239       break; | 
| 223     case GetProxyForUrlAction::DISCONNECT: | 240     case GetProxyForUrlAction::DISCONNECT: | 
| 224       binding_.Close(); | 241       binding_.Close(); | 
| 225       break; | 242       break; | 
| 226     case GetProxyForUrlAction::WAIT_FOR_CLIENT_DISCONNECT: | 243     case GetProxyForUrlAction::WAIT_FOR_CLIENT_DISCONNECT: | 
| 227       ASSERT_FALSE(client.WaitForIncomingMethodCall()); | 244       ASSERT_FALSE(client.WaitForIncomingMethodCall()); | 
| 228       break; | 245       break; | 
|  | 246     case GetProxyForUrlAction::SEND_LOAD_STATE_AND_BLOCK: | 
|  | 247       client->LoadStateChanged(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT); | 
|  | 248       blocked_clients_.push_back( | 
|  | 249           new interfaces::ProxyResolverRequestClientPtr(client.Pass())); | 
|  | 250       break; | 
| 229   } | 251   } | 
| 230   WakeWaiter(); | 252   WakeWaiter(); | 
| 231 } | 253 } | 
| 232 | 254 | 
| 233 class TestMojoProxyResolverFactory : public MojoProxyResolverFactory { | 255 class TestMojoProxyResolverFactory : public MojoProxyResolverFactory { | 
| 234  public: | 256  public: | 
| 235   TestMojoProxyResolverFactory(); | 257   TestMojoProxyResolverFactory(); | 
| 236   ~TestMojoProxyResolverFactory(); | 258   ~TestMojoProxyResolverFactory(); | 
| 237 | 259 | 
| 238   // Overridden from MojoProxyResolverFactory: | 260   // Overridden from MojoProxyResolverFactory: | 
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 296 class Request { | 318 class Request { | 
| 297  public: | 319  public: | 
| 298   Request(ProxyResolverMojo* resolver, const GURL& url); | 320   Request(ProxyResolverMojo* resolver, const GURL& url); | 
| 299 | 321 | 
| 300   int Resolve(); | 322   int Resolve(); | 
| 301   void Cancel(); | 323   void Cancel(); | 
| 302   int WaitForResult(); | 324   int WaitForResult(); | 
| 303 | 325 | 
| 304   int error() const { return error_; } | 326   int error() const { return error_; } | 
| 305   const ProxyInfo& results() const { return results_; } | 327   const ProxyInfo& results() const { return results_; } | 
|  | 328   LoadState load_state() { return resolver_->GetLoadState(handle_); } | 
| 306 | 329 | 
| 307  private: | 330  private: | 
| 308   ProxyResolverMojo* resolver_; | 331   ProxyResolverMojo* resolver_; | 
| 309   const GURL url_; | 332   const GURL url_; | 
| 310   ProxyInfo results_; | 333   ProxyInfo results_; | 
| 311   ProxyResolver::RequestHandle handle_; | 334   ProxyResolver::RequestHandle handle_; | 
| 312   int error_; | 335   int error_; | 
| 313   TestCompletionCallback callback_; | 336   TestCompletionCallback callback_; | 
| 314 }; | 337 }; | 
| 315 | 338 | 
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 515   EXPECT_EQ(OK, request->WaitForResult()); | 538   EXPECT_EQ(OK, request->WaitForResult()); | 
| 516 | 539 | 
| 517   EXPECT_EQ("DIRECT", request->results().ToPacString()); | 540   EXPECT_EQ("DIRECT", request->results().ToPacString()); | 
| 518 } | 541 } | 
| 519 | 542 | 
| 520 TEST_F(ProxyResolverMojoTest, GetProxyForURL_WithoutSetPacScript) { | 543 TEST_F(ProxyResolverMojoTest, GetProxyForURL_WithoutSetPacScript) { | 
| 521   scoped_ptr<Request> request(MakeRequest(GURL(kExampleUrl))); | 544   scoped_ptr<Request> request(MakeRequest(GURL(kExampleUrl))); | 
| 522   EXPECT_EQ(ERR_PAC_SCRIPT_TERMINATED, request->Resolve()); | 545   EXPECT_EQ(ERR_PAC_SCRIPT_TERMINATED, request->Resolve()); | 
| 523 } | 546 } | 
| 524 | 547 | 
|  | 548 TEST_F(ProxyResolverMojoTest, GetProxyForURL_LoadState) { | 
|  | 549   mojo_proxy_resolver_factory_.AddFutureGetProxyAction( | 
|  | 550       0, GetProxyForUrlAction::SendLoadStateChanged(GURL(kExampleUrl))); | 
|  | 551   SetPacScript(0); | 
|  | 552 | 
|  | 553   scoped_ptr<Request> request(MakeRequest(GURL(kExampleUrl))); | 
|  | 554   EXPECT_EQ(ERR_IO_PENDING, request->Resolve()); | 
|  | 555   EXPECT_EQ(LOAD_STATE_RESOLVING_PROXY_FOR_URL, request->load_state()); | 
|  | 556   while (request->load_state() == LOAD_STATE_RESOLVING_PROXY_FOR_URL) | 
|  | 557     base::RunLoop().RunUntilIdle(); | 
|  | 558   EXPECT_EQ(LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT, request->load_state()); | 
|  | 559   mojo_proxy_resolver_factory_.GetMockResolver().ClearBlockedClients(); | 
|  | 560   EXPECT_EQ(ERR_PAC_SCRIPT_TERMINATED, request->WaitForResult()); | 
|  | 561 } | 
|  | 562 | 
| 525 TEST_F(ProxyResolverMojoTest, GetProxyForURL_MultipleResults) { | 563 TEST_F(ProxyResolverMojoTest, GetProxyForURL_MultipleResults) { | 
| 526   static const char kPacString[] = | 564   static const char kPacString[] = | 
| 527       "PROXY foo1:80;DIRECT;SOCKS foo2:1234;" | 565       "PROXY foo1:80;DIRECT;SOCKS foo2:1234;" | 
| 528       "SOCKS5 foo3:1080;HTTPS foo4:443;QUIC foo6:8888"; | 566       "SOCKS5 foo3:1080;HTTPS foo4:443;QUIC foo6:8888"; | 
| 529   mojo_proxy_resolver_factory_.AddFutureGetProxyAction( | 567   mojo_proxy_resolver_factory_.AddFutureGetProxyAction( | 
| 530       0, GetProxyForUrlAction::ReturnServers( | 568       0, GetProxyForUrlAction::ReturnServers( | 
| 531              GURL(kExampleUrl), ProxyServersFromPacString(kPacString))); | 569              GURL(kExampleUrl), ProxyServersFromPacString(kPacString))); | 
| 532   SetPacScript(0); | 570   SetPacScript(0); | 
| 533 | 571 | 
| 534   scoped_ptr<Request> request(MakeRequest(GURL(kExampleUrl))); | 572   scoped_ptr<Request> request(MakeRequest(GURL(kExampleUrl))); | 
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 688   // resolve request. | 726   // resolve request. | 
| 689   scoped_ptr<Request> request(MakeRequest(GURL(kExampleUrl))); | 727   scoped_ptr<Request> request(MakeRequest(GURL(kExampleUrl))); | 
| 690   EXPECT_EQ(ERR_IO_PENDING, request->Resolve()); | 728   EXPECT_EQ(ERR_IO_PENDING, request->Resolve()); | 
| 691   EXPECT_EQ(OK, request->WaitForResult()); | 729   EXPECT_EQ(OK, request->WaitForResult()); | 
| 692   EXPECT_EQ("DIRECT", request->results().ToPacString()); | 730   EXPECT_EQ("DIRECT", request->results().ToPacString()); | 
| 693 | 731 | 
| 694   EXPECT_EQ(2, mojo_proxy_resolver_factory_.num_create_calls()); | 732   EXPECT_EQ(2, mojo_proxy_resolver_factory_.num_create_calls()); | 
| 695 } | 733 } | 
| 696 | 734 | 
| 697 }  // namespace net | 735 }  // namespace net | 
| OLD | NEW | 
|---|