• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_TEST_CHROMEDRIVER_CHROME_DEVICE_MANAGER_H_
6 #define CHROME_TEST_CHROMEDRIVER_CHROME_DEVICE_MANAGER_H_
7 
8 #include <list>
9 #include <string>
10 
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/synchronization/lock.h"
16 
17 class Adb;
18 class Status;
19 class DeviceManager;
20 
21 class Device {
22  public:
23   ~Device();
24 
25   Status SetUp(const std::string& package,
26                const std::string& activity,
27                const std::string& process,
28                const std::string& args,
29                bool use_running_app,
30                int port);
31 
32   Status TearDown();
33 
34  private:
35   friend class DeviceManager;
36 
37   Device(const std::string& device_serial,
38          Adb* adb,
39          base::Callback<void()> release_callback);
40 
41   Status ForwardDevtoolsPort(const std::string& package,
42                              const std::string& process,
43                              std::string& device_socket,
44                              int port);
45 
46   const std::string serial_;
47   std::string active_package_;
48   Adb* adb_;
49   base::Callback<void()> release_callback_;
50 
51   DISALLOW_COPY_AND_ASSIGN(Device);
52 };
53 
54 class DeviceManager {
55  public:
56   explicit DeviceManager(Adb* adb);
57   ~DeviceManager();
58 
59   // Returns a device which will not be reassigned during its lifetime.
60   Status AcquireDevice(scoped_ptr<Device>* device);
61 
62   // Returns a device with the same guarantees as AcquireDevice, but fails
63   // if the device with the given serial number is not avaliable.
64   Status AcquireSpecificDevice(const std::string& device_serial,
65                                scoped_ptr<Device>* device);
66 
67  private:
68   void ReleaseDevice(const std::string& device_serial);
69 
70   Device* LockDevice(const std::string& device_serial);
71   bool IsDeviceLocked(const std::string& device_serial);
72 
73   base::Lock devices_lock_;
74   std::list<std::string> active_devices_;
75   Adb* adb_;
76 
77   DISALLOW_COPY_AND_ASSIGN(DeviceManager);
78 };
79 
80 #endif  // CHROME_TEST_CHROMEDRIVER_CHROME_DEVICE_MANAGER_H_
81