Chromium Code Reviews| 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 #ifndef TOOLS_BATTOR_AGENT_BATTOR_AGENT_H_ | 5 #ifndef TOOLS_BATTOR_AGENT_BATTOR_AGENT_H_ |
| 6 #define TOOLS_BATTOR_AGENT_BATTOR_AGENT_H_ | 6 #define TOOLS_BATTOR_AGENT_BATTOR_AGENT_H_ |
| 7 | 7 |
| 8 #include "base/callback_forward.h" | 8 #include "base/callback_forward.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
| 13 #include "tools/battor_agent/battor_connection.h" | |
| 13 #include "tools/battor_agent/battor_error.h" | 14 #include "tools/battor_agent/battor_error.h" |
| 14 | 15 |
| 15 namespace device { | |
| 16 class SerialIoHandler; | |
| 17 } | |
| 18 | |
| 19 namespace battor { | 16 namespace battor { |
| 20 | 17 |
| 21 // A BattOrAgent is a class used to asynchronously communicate with a BattOr for | 18 // A BattOrAgent is a class used to asynchronously communicate with a BattOr for |
| 22 // the purpose of collecting power samples. A BattOr is an external USB device | 19 // the purpose of collecting power samples. A BattOr is an external USB device |
| 23 // that's capable of recording accurate, high-frequency (2000Hz) power samples. | 20 // that's capable of recording accurate, high-frequency (2000Hz) power samples. |
| 24 // | 21 // |
| 25 // The serial connection is automatically opened when the first command | 22 // The serial connection is automatically opened when the first command |
| 26 // (e.g. StartTracing(), StopTracing(), etc.) is issued, and automatically | 23 // (e.g. StartTracing(), StopTracing(), etc.) is issued, and automatically |
| 27 // closed when either StopTracing() or the destructor is called. For Telemetry, | 24 // closed when either StopTracing() or the destructor is called. For Telemetry, |
| 28 // this means that the connection must be reinitialized for every command that's | 25 // this means that the connection must be reinitialized for every command that's |
| 29 // issued because a new BattOrAgent is constructed. For Chromium, we use the | 26 // issued because a new BattOrAgent is constructed. For Chromium, we use the |
| 30 // same BattOrAgent for multiple commands and thus avoid having to reinitialize | 27 // same BattOrAgent for multiple commands and thus avoid having to reinitialize |
| 31 // the serial connection. | 28 // the serial connection. |
| 32 // | 29 // |
| 33 // This class is NOT thread safe, and must be interacted with only from the IO | 30 // This class is NOT thread safe, and must be interacted with only from the IO |
| 34 // thread. The IO thread must also have a running MessageLoop. | 31 // thread. The IO thread must also have a running MessageLoop. |
| 35 class BattOrAgent : public base::SupportsWeakPtr<BattOrAgent> { | 32 class BattOrAgent : public base::SupportsWeakPtr<BattOrAgent> { |
| 36 public: | 33 public: |
| 37 // The listener interface that must be implemented in order to interact with | 34 // The listener interface that must be implemented in order to interact with |
| 38 // the BattOrAgent. | 35 // the BattOrAgent. |
| 39 class Listener { | 36 class Listener { |
| 40 public: | 37 public: |
| 41 virtual void OnStartTracingComplete(BattOrError error) = 0; | 38 virtual void OnStartTracingComplete(BattOrError error) = 0; |
| 42 }; | 39 }; |
| 43 | 40 |
| 44 BattOrAgent( | 41 BattOrAgent( |
| 45 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | 42 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, |
| 46 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_rnuner, | 43 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner, |
| 47 const std::string& path, | 44 const std::string& path, |
| 48 Listener* listener); | 45 Listener* listener); |
| 49 virtual ~BattOrAgent(); | 46 virtual ~BattOrAgent(); |
| 50 | 47 |
| 51 // Tells the BattOr to start tracing. | 48 // Tells the BattOr to start tracing. |
| 52 void StartTracing(); | 49 void StartTracing(); |
| 53 | 50 |
| 54 // Returns whether the BattOr is able to record clock sync markers in its own | 51 // Returns whether the BattOr is able to record clock sync markers in its own |
| 55 // trace log. | 52 // trace log. |
| 56 static bool SupportsExplicitClockSync() { return true; } | 53 static bool SupportsExplicitClockSync() { return true; } |
| 57 | 54 |
| 58 private: | 55 private: |
| 59 // Initializes the serial connection (if not done already) and calls one of | 56 // Initializes the serial connection (if not done already) and calls one of |
| 60 // the two callbacks depending its success. The callback will be invoked on | 57 // the two callbacks depending its success. The callback will be invoked on |
| 61 // the same thread that this method is called on. | 58 // the same thread that this method is called on. |
| 62 void ConnectIfNeeded(const base::Closure& success_callback, | 59 void ConnectIfNeeded(const base::Closure& success_callback, |
| 63 const base::Closure& failure_callback); | 60 const base::Closure& failure_callback); |
| 64 void OnConnectComplete(const base::Closure& success_callback, | 61 void OnConnectComplete(const base::Closure& success_callback, |
| 65 const base::Closure& failure_callback, | 62 const base::Closure& failure_callback, |
| 66 bool success); | 63 bool success); |
| 67 | 64 |
| 68 // StartTracing continuation called once the connection is initialized. | 65 // StartTracing continuation called once the connection is initialized. |
| 69 void DoStartTracing(); | 66 void DoStartTracing(); |
| 70 | 67 |
| 71 // Resets the connection to its unopened state. | 68 // Resets the connection to its unopened state. |
| 72 void ResetConnection(); | 69 void ResetConnection(); |
| 73 | 70 |
| 74 // Threads needed for serial communication. | 71 // Threads needed for serial communication. |
| 75 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner_; | 72 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner_; |
|
Primiano Tucci (use gerrit)
2015/12/15 11:07:19
You don't seem to need these task runner here anym
charliea (OOO until 10-5)
2015/12/15 23:50:03
Done.
In an earlier code review, Zhen and I deci
| |
| 76 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner_; | 73 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner_; |
| 77 | 74 |
| 78 // The path of the BattOr (e.g. "/dev/tty.battor_serial"). | 75 // The path of the BattOr (e.g. "/dev/tty.battor_serial"). |
| 79 std::string path_; | 76 std::string path_; |
| 80 | 77 |
| 81 // The listener that handles the commands' results. It must outlive the agent. | 78 // The listener that handles the commands' results. It must outlive the agent. |
| 82 Listener* listener_; | 79 Listener* listener_; |
| 83 | 80 |
| 84 // IO handler capable of reading and writing from the serial connection. | 81 // The serial connection to the BattOr. |
| 85 scoped_refptr<device::SerialIoHandler> io_handler_; | 82 scoped_ptr<BattOrConnection> connection_; |
| 86 | 83 |
| 87 // Checker to make sure that this is only ever called on the IO thread. | 84 // Checker to make sure that this is only ever called on the IO thread. |
| 88 base::ThreadChecker thread_checker_; | 85 base::ThreadChecker thread_checker_; |
| 89 | 86 |
| 90 DISALLOW_COPY_AND_ASSIGN(BattOrAgent); | 87 DISALLOW_COPY_AND_ASSIGN(BattOrAgent); |
| 91 }; | 88 }; |
| 92 | 89 |
| 93 } // namespace battor | 90 } // namespace battor |
| 94 | 91 |
| 95 #endif // TOOLS_BATTOR_AGENT_BATTOR_AGENT_H_ | 92 #endif // TOOLS_BATTOR_AGENT_BATTOR_AGENT_H_ |
| OLD | NEW |