Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(332)

Side by Side Diff: device/test/usb_test_gadget_impl.cc

Issue 1874313002: Convert device to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/test/usb_test_gadget.h ('k') | device/udev_linux/scoped_udev.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
6
7 #include <stddef.h> 5 #include <stddef.h>
8 #include <stdint.h> 6 #include <stdint.h>
9 7
8 #include <memory>
10 #include <string> 9 #include <string>
11 #include <vector> 10 #include <vector>
12 11
13 #include "base/command_line.h" 12 #include "base/command_line.h"
14 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
15 #include "base/files/file.h" 14 #include "base/files/file.h"
16 #include "base/files/file_path.h" 15 #include "base/files/file_path.h"
17 #include "base/logging.h" 16 #include "base/logging.h"
18 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/memory/ptr_util.h"
19 #include "base/memory/ref_counted.h" 19 #include "base/memory/ref_counted.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/path_service.h" 20 #include "base/path_service.h"
22 #include "base/process/process_handle.h" 21 #include "base/process/process_handle.h"
23 #include "base/run_loop.h" 22 #include "base/run_loop.h"
24 #include "base/scoped_observer.h" 23 #include "base/scoped_observer.h"
25 #include "base/strings/stringprintf.h" 24 #include "base/strings/stringprintf.h"
26 #include "base/strings/utf_string_conversions.h" 25 #include "base/strings/utf_string_conversions.h"
27 #include "base/thread_task_runner_handle.h" 26 #include "base/thread_task_runner_handle.h"
28 #include "base/time/time.h" 27 #include "base/time/time.h"
29 #include "device/core/device_client.h" 28 #include "device/core/device_client.h"
29 #include "device/test/usb_test_gadget.h"
30 #include "device/usb/usb_device.h" 30 #include "device/usb/usb_device.h"
31 #include "device/usb/usb_device_handle.h" 31 #include "device/usb/usb_device_handle.h"
32 #include "device/usb/usb_service.h" 32 #include "device/usb/usb_service.h"
33 #include "net/base/escape.h" 33 #include "net/base/escape.h"
34 #include "net/proxy/proxy_service.h" 34 #include "net/proxy/proxy_service.h"
35 #include "net/url_request/url_fetcher.h" 35 #include "net/url_request/url_fetcher.h"
36 #include "net/url_request/url_fetcher_delegate.h" 36 #include "net/url_request/url_fetcher_delegate.h"
37 #include "net/url_request/url_request_context.h" 37 #include "net/url_request/url_request_context.h"
38 #include "net/url_request/url_request_context_builder.h" 38 #include "net/url_request/url_request_context_builder.h"
39 #include "net/url_request/url_request_context_getter.h" 39 #include "net/url_request/url_request_context_getter.h"
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 } 116 }
117 117
118 bool ReadLocalPackage(std::string* package) { 118 bool ReadLocalPackage(std::string* package) {
119 base::FilePath file_path; 119 base::FilePath file_path;
120 CHECK(PathService::Get(base::DIR_EXE, &file_path)); 120 CHECK(PathService::Get(base::DIR_EXE, &file_path));
121 file_path = file_path.AppendASCII("usb_gadget.zip"); 121 file_path = file_path.AppendASCII("usb_gadget.zip");
122 122
123 return ReadFile(file_path, package); 123 return ReadFile(file_path, package);
124 } 124 }
125 125
126 scoped_ptr<net::URLFetcher> CreateURLFetcher( 126 std::unique_ptr<net::URLFetcher> CreateURLFetcher(
127 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 127 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
128 const GURL& url, 128 const GURL& url,
129 net::URLFetcher::RequestType request_type, 129 net::URLFetcher::RequestType request_type,
130 net::URLFetcherDelegate* delegate) { 130 net::URLFetcherDelegate* delegate) {
131 scoped_ptr<net::URLFetcher> url_fetcher = 131 std::unique_ptr<net::URLFetcher> url_fetcher =
132 net::URLFetcher::Create(url, request_type, delegate); 132 net::URLFetcher::Create(url, request_type, delegate);
133 133
134 url_fetcher->SetRequestContext(request_context_getter.get()); 134 url_fetcher->SetRequestContext(request_context_getter.get());
135 135
136 return url_fetcher; 136 return url_fetcher;
137 } 137 }
138 138
139 class URLRequestContextGetter : public net::URLRequestContextGetter { 139 class URLRequestContextGetter : public net::URLRequestContextGetter {
140 public: 140 public:
141 URLRequestContextGetter( 141 URLRequestContextGetter(
(...skipping 11 matching lines...) Expand all
153 context_ = context_builder.Build(); 153 context_ = context_builder.Build();
154 } 154 }
155 return context_.get(); 155 return context_.get();
156 } 156 }
157 157
158 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() 158 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
159 const override { 159 const override {
160 return network_task_runner_; 160 return network_task_runner_;
161 } 161 }
162 162
163 scoped_ptr<net::URLRequestContext> context_; 163 std::unique_ptr<net::URLRequestContext> context_;
164 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; 164 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
165 }; 165 };
166 166
167 class URLFetcherDelegate : public net::URLFetcherDelegate { 167 class URLFetcherDelegate : public net::URLFetcherDelegate {
168 public: 168 public:
169 URLFetcherDelegate() {} 169 URLFetcherDelegate() {}
170 ~URLFetcherDelegate() override {} 170 ~URLFetcherDelegate() override {}
171 171
172 void WaitForCompletion() { run_loop_.Run(); } 172 void WaitForCompletion() { run_loop_.Run(); }
173 173
174 void OnURLFetchComplete(const net::URLFetcher* source) override { 174 void OnURLFetchComplete(const net::URLFetcher* source) override {
175 run_loop_.Quit(); 175 run_loop_.Quit();
176 } 176 }
177 177
178 private: 178 private:
179 base::RunLoop run_loop_; 179 base::RunLoop run_loop_;
180 180
181 DISALLOW_COPY_AND_ASSIGN(URLFetcherDelegate); 181 DISALLOW_COPY_AND_ASSIGN(URLFetcherDelegate);
182 }; 182 };
183 183
184 int SimplePOSTRequest( 184 int SimplePOSTRequest(
185 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 185 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
186 const GURL& url, 186 const GURL& url,
187 const std::string& form_data) { 187 const std::string& form_data) {
188 URLFetcherDelegate delegate; 188 URLFetcherDelegate delegate;
189 scoped_ptr<net::URLFetcher> url_fetcher = CreateURLFetcher( 189 std::unique_ptr<net::URLFetcher> url_fetcher = CreateURLFetcher(
190 request_context_getter, url, net::URLFetcher::POST, &delegate); 190 request_context_getter, url, net::URLFetcher::POST, &delegate);
191 191
192 url_fetcher->SetUploadData("application/x-www-form-urlencoded", form_data); 192 url_fetcher->SetUploadData("application/x-www-form-urlencoded", form_data);
193 url_fetcher->Start(); 193 url_fetcher->Start();
194 delegate.WaitForCompletion(); 194 delegate.WaitForCompletion();
195 195
196 return url_fetcher->GetResponseCode(); 196 return url_fetcher->GetResponseCode();
197 } 197 }
198 198
199 class UsbGadgetFactory : public UsbService::Observer, 199 class UsbGadgetFactory : public UsbService::Observer,
200 public net::URLFetcherDelegate { 200 public net::URLFetcherDelegate {
201 public: 201 public:
202 UsbGadgetFactory(scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) 202 UsbGadgetFactory(scoped_refptr<base::SingleThreadTaskRunner> io_task_runner)
203 : observer_(this), weak_factory_(this) { 203 : observer_(this), weak_factory_(this) {
204 usb_service_ = DeviceClient::Get()->GetUsbService(); 204 usb_service_ = DeviceClient::Get()->GetUsbService();
205 request_context_getter_ = new URLRequestContextGetter(io_task_runner); 205 request_context_getter_ = new URLRequestContextGetter(io_task_runner);
206 206
207 static uint32_t next_session_id; 207 static uint32_t next_session_id;
208 base::ProcessId process_id = base::GetCurrentProcId(); 208 base::ProcessId process_id = base::GetCurrentProcId();
209 session_id_ = base::StringPrintf("%d-%d", process_id, next_session_id++); 209 session_id_ = base::StringPrintf("%d-%d", process_id, next_session_id++);
210 210
211 observer_.Add(usb_service_); 211 observer_.Add(usb_service_);
212 } 212 }
213 213
214 ~UsbGadgetFactory() override {} 214 ~UsbGadgetFactory() override {}
215 215
216 scoped_ptr<UsbTestGadget> WaitForDevice() { 216 std::unique_ptr<UsbTestGadget> WaitForDevice() {
217 EnumerateDevices(); 217 EnumerateDevices();
218 run_loop_.Run(); 218 run_loop_.Run();
219 return make_scoped_ptr( 219 return base::WrapUnique(
220 new UsbTestGadgetImpl(request_context_getter_, usb_service_, device_)); 220 new UsbTestGadgetImpl(request_context_getter_, usb_service_, device_));
221 } 221 }
222 222
223 private: 223 private:
224 void EnumerateDevices() { 224 void EnumerateDevices() {
225 if (!device_) { 225 if (!device_) {
226 usb_service_->GetDevices(base::Bind( 226 usb_service_->GetDevices(base::Bind(
227 &UsbGadgetFactory::OnDevicesEnumerated, weak_factory_.GetWeakPtr())); 227 &UsbGadgetFactory::OnDevicesEnumerated, weak_factory_.GetWeakPtr()));
228 } 228 }
229 } 229 }
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 // Wait a bit and then try again to find an available device. 379 // Wait a bit and then try again to find an available device.
380 base::MessageLoop::current()->PostDelayedTask( 380 base::MessageLoop::current()->PostDelayedTask(
381 FROM_HERE, base::Bind(&UsbGadgetFactory::EnumerateDevices, 381 FROM_HERE, base::Bind(&UsbGadgetFactory::EnumerateDevices,
382 weak_factory_.GetWeakPtr()), 382 weak_factory_.GetWeakPtr()),
383 base::TimeDelta::FromMilliseconds(kReenumeratePeriod)); 383 base::TimeDelta::FromMilliseconds(kReenumeratePeriod));
384 } 384 }
385 385
386 UsbService* usb_service_ = nullptr; 386 UsbService* usb_service_ = nullptr;
387 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; 387 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
388 std::string session_id_; 388 std::string session_id_;
389 scoped_ptr<net::URLFetcher> url_fetcher_; 389 std::unique_ptr<net::URLFetcher> url_fetcher_;
390 scoped_refptr<UsbDevice> device_; 390 scoped_refptr<UsbDevice> device_;
391 std::string serial_number_; 391 std::string serial_number_;
392 bool claimed_ = false; 392 bool claimed_ = false;
393 std::string version_; 393 std::string version_;
394 base::RunLoop run_loop_; 394 base::RunLoop run_loop_;
395 ScopedObserver<UsbService, UsbService::Observer> observer_; 395 ScopedObserver<UsbService, UsbService::Observer> observer_;
396 base::WeakPtrFactory<UsbGadgetFactory> weak_factory_; 396 base::WeakPtrFactory<UsbGadgetFactory> weak_factory_;
397 }; 397 };
398 398
399 class DeviceAddListener : public UsbService::Observer { 399 class DeviceAddListener : public UsbService::Observer {
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 DISALLOW_COPY_AND_ASSIGN(DeviceRemoveListener); 512 DISALLOW_COPY_AND_ASSIGN(DeviceRemoveListener);
513 }; 513 };
514 514
515 } // namespace 515 } // namespace
516 516
517 bool UsbTestGadget::IsTestEnabled() { 517 bool UsbTestGadget::IsTestEnabled() {
518 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 518 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
519 return command_line->HasSwitch(kCommandLineSwitch); 519 return command_line->HasSwitch(kCommandLineSwitch);
520 } 520 }
521 521
522 scoped_ptr<UsbTestGadget> UsbTestGadget::Claim( 522 std::unique_ptr<UsbTestGadget> UsbTestGadget::Claim(
523 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) { 523 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) {
524 UsbGadgetFactory gadget_factory(io_task_runner); 524 UsbGadgetFactory gadget_factory(io_task_runner);
525 return gadget_factory.WaitForDevice(); 525 return gadget_factory.WaitForDevice();
526 } 526 }
527 527
528 UsbTestGadgetImpl::UsbTestGadgetImpl( 528 UsbTestGadgetImpl::UsbTestGadgetImpl(
529 scoped_refptr<net::URLRequestContextGetter> request_context_getter_, 529 scoped_refptr<net::URLRequestContextGetter> request_context_getter_,
530 UsbService* usb_service, 530 UsbService* usb_service,
531 scoped_refptr<UsbDevice> device) 531 scoped_refptr<UsbDevice> device)
532 : device_address_(base::UTF16ToUTF8(device->serial_number())), 532 : device_address_(base::UTF16ToUTF8(device->serial_number())),
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 return false; 611 return false;
612 } 612 }
613 613
614 DeviceAddListener add_listener(usb_service_, device_address_, -1); 614 DeviceAddListener add_listener(usb_service_, device_address_, -1);
615 device_ = add_listener.WaitForAdd(); 615 device_ = add_listener.WaitForAdd();
616 DCHECK(device_.get()); 616 DCHECK(device_.get());
617 return true; 617 return true;
618 } 618 }
619 619
620 } // namespace device 620 } // namespace device
OLDNEW
« no previous file with comments | « device/test/usb_test_gadget.h ('k') | device/udev_linux/scoped_udev.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698