| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "device/test/usb_test_gadget.h" | 5 #include "device/test/usb_test_gadget.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 | 64 |
| 65 class UsbTestGadgetImpl : public UsbTestGadget { | 65 class UsbTestGadgetImpl : public UsbTestGadget { |
| 66 public: | 66 public: |
| 67 ~UsbTestGadgetImpl() override; | 67 ~UsbTestGadgetImpl() override; |
| 68 | 68 |
| 69 bool Unclaim() override; | 69 bool Unclaim() override; |
| 70 bool Disconnect() override; | 70 bool Disconnect() override; |
| 71 bool Reconnect() override; | 71 bool Reconnect() override; |
| 72 bool SetType(Type type) override; | 72 bool SetType(Type type) override; |
| 73 UsbDevice* GetDevice() const override; | 73 UsbDevice* GetDevice() const override; |
| 74 std::string GetSerialNumber() const override; | 74 const std::string& GetSerialNumber() const override; |
| 75 | 75 |
| 76 protected: | 76 protected: |
| 77 UsbTestGadgetImpl(); | 77 UsbTestGadgetImpl(); |
| 78 | 78 |
| 79 private: | 79 private: |
| 80 scoped_ptr<net::URLFetcher> CreateURLFetcher( | 80 scoped_ptr<net::URLFetcher> CreateURLFetcher( |
| 81 const GURL& url, | 81 const GURL& url, |
| 82 net::URLFetcher::RequestType request_type, | 82 net::URLFetcher::RequestType request_type, |
| 83 net::URLFetcherDelegate* delegate); | 83 net::URLFetcherDelegate* delegate); |
| 84 int SimplePOSTRequest(const GURL& url, const std::string& form_data); | 84 int SimplePOSTRequest(const GURL& url, const std::string& form_data); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 UsbTestGadgetImpl::~UsbTestGadgetImpl() { | 159 UsbTestGadgetImpl::~UsbTestGadgetImpl() { |
| 160 if (!device_address_.empty()) { | 160 if (!device_address_.empty()) { |
| 161 Unclaim(); | 161 Unclaim(); |
| 162 } | 162 } |
| 163 } | 163 } |
| 164 | 164 |
| 165 UsbDevice* UsbTestGadgetImpl::GetDevice() const { | 165 UsbDevice* UsbTestGadgetImpl::GetDevice() const { |
| 166 return device_.get(); | 166 return device_.get(); |
| 167 } | 167 } |
| 168 | 168 |
| 169 std::string UsbTestGadgetImpl::GetSerialNumber() const { | 169 const std::string& UsbTestGadgetImpl::GetSerialNumber() const { |
| 170 return device_address_; | 170 return device_address_; |
| 171 } | 171 } |
| 172 | 172 |
| 173 scoped_ptr<net::URLFetcher> UsbTestGadgetImpl::CreateURLFetcher( | 173 scoped_ptr<net::URLFetcher> UsbTestGadgetImpl::CreateURLFetcher( |
| 174 const GURL& url, net::URLFetcher::RequestType request_type, | 174 const GURL& url, net::URLFetcher::RequestType request_type, |
| 175 net::URLFetcherDelegate* delegate) { | 175 net::URLFetcherDelegate* delegate) { |
| 176 scoped_ptr<net::URLFetcher> url_fetcher( | 176 scoped_ptr<net::URLFetcher> url_fetcher( |
| 177 net::URLFetcher::Create(url, request_type, delegate)); | 177 net::URLFetcher::Create(url, request_type, delegate)); |
| 178 | 178 |
| 179 url_fetcher->SetRequestContext( | 179 url_fetcher->SetRequestContext(new net::TrivialURLRequestContextGetter( |
| 180 new net::TrivialURLRequestContextGetter( | 180 request_context_.get(), base::MessageLoop::current()->task_runner())); |
| 181 request_context_.get(), | |
| 182 base::MessageLoop::current()->message_loop_proxy())); | |
| 183 | 181 |
| 184 return url_fetcher; | 182 return url_fetcher; |
| 185 } | 183 } |
| 186 | 184 |
| 187 int UsbTestGadgetImpl::SimplePOSTRequest(const GURL& url, | 185 int UsbTestGadgetImpl::SimplePOSTRequest(const GURL& url, |
| 188 const std::string& form_data) { | 186 const std::string& form_data) { |
| 189 Delegate delegate; | 187 Delegate delegate; |
| 190 scoped_ptr<net::URLFetcher> url_fetcher = | 188 scoped_ptr<net::URLFetcher> url_fetcher = |
| 191 CreateURLFetcher(url, net::URLFetcher::POST, &delegate); | 189 CreateURLFetcher(url, net::URLFetcher::POST, &delegate); |
| 192 | 190 |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 bool UsbTestGadgetImpl::Unclaim() { | 417 bool UsbTestGadgetImpl::Unclaim() { |
| 420 VLOG(1) << "Releasing the device at " << device_address_ << "."; | 418 VLOG(1) << "Releasing the device at " << device_address_ << "."; |
| 421 | 419 |
| 422 const GURL url("http://" + device_address_ + "/unclaim"); | 420 const GURL url("http://" + device_address_ + "/unclaim"); |
| 423 const int response_code = SimplePOSTRequest(url, ""); | 421 const int response_code = SimplePOSTRequest(url, ""); |
| 424 | 422 |
| 425 if (response_code != 200) { | 423 if (response_code != 200) { |
| 426 LOG(ERROR) << "Unexpected HTTP " << response_code << " from /unclaim."; | 424 LOG(ERROR) << "Unexpected HTTP " << response_code << " from /unclaim."; |
| 427 return false; | 425 return false; |
| 428 } | 426 } |
| 427 |
| 428 device_address_.clear(); |
| 429 return true; | 429 return true; |
| 430 } | 430 } |
| 431 | 431 |
| 432 bool UsbTestGadgetImpl::SetType(Type type) { | 432 bool UsbTestGadgetImpl::SetType(Type type) { |
| 433 const struct UsbTestGadgetConfiguration* config = NULL; | 433 const struct UsbTestGadgetConfiguration* config = NULL; |
| 434 for (size_t i = 0; i < arraysize(kConfigurations); ++i) { | 434 for (size_t i = 0; i < arraysize(kConfigurations); ++i) { |
| 435 if (kConfigurations[i].type == type) { | 435 if (kConfigurations[i].type == type) { |
| 436 config = &kConfigurations[i]; | 436 config = &kConfigurations[i]; |
| 437 } | 437 } |
| 438 } | 438 } |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 } | 514 } |
| 515 PlatformThread::Sleep(TimeDelta::FromMilliseconds(kRetryPeriod)); | 515 PlatformThread::Sleep(TimeDelta::FromMilliseconds(kRetryPeriod)); |
| 516 } | 516 } |
| 517 VLOG(1) << "It took " << (kDisconnectRetries - retries) | 517 VLOG(1) << "It took " << (kDisconnectRetries - retries) |
| 518 << " retries for the device to reconnect."; | 518 << " retries for the device to reconnect."; |
| 519 | 519 |
| 520 return true; | 520 return true; |
| 521 } | 521 } |
| 522 | 522 |
| 523 } // namespace device | 523 } // namespace device |
| OLD | NEW |