Chromium Code Reviews| Index: components/policy/core/common/remote_commands/testing_remote_commands_server.h |
| diff --git a/components/policy/core/common/remote_commands/testing_remote_commands_server.h b/components/policy/core/common/remote_commands/testing_remote_commands_server.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e2fad6807a2b64c57b63991dc6e96a5f27892b54 |
| --- /dev/null |
| +++ b/components/policy/core/common/remote_commands/testing_remote_commands_server.h |
| @@ -0,0 +1,119 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef COMPONENTS_POLICY_CORE_COMMON_REMOTE_COMMANDS_TESTING_REMOTE_COMMANDS_SERVER_H_ |
| +#define COMPONENTS_POLICY_CORE_COMMON_REMOTE_COMMANDS_TESTING_REMOTE_COMMANDS_SERVER_H_ |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/synchronization/lock.h" |
| +#include "base/threading/thread_checker.h" |
| +#include "components/policy/core/common/remote_commands/remote_command_job.h" |
| +#include "policy/proto/device_management_backend.pb.h" |
| + |
| +namespace base { |
| +class Clock; |
| +class SingleThreadTaskRunner; |
| +} // namespace base |
| + |
| +namespace policy { |
| + |
| +// This class implements server side logic for remote commands service. It |
| +// acts just like a queue, and there are mainly two exposed methods for this |
| +// purpose. Test authors are expected to call IssueCommand() to add a command |
| +// to the queue in their tests. The use of FetchCommandsFromIO() is internal |
| +// only for classes like subclasses of net::URLRequestInterceptor, thus it only |
| +// supports calling from IO thread at this moment. |
| +// In addition, this class also supports verifying command result sent back to |
| +// server, and test authors should override OnJobResultReported() for this |
| +// purpose. |
| +// All method (including ctor/dtor) except FetchCommandsFromIO should be called |
| +// from the same thread (like UI thread), and FetchCommandsFromIO() can be |
| +// called from any thread. |
| +class TestingRemoteCommandsServer |
| + : public base::SupportsWeakPtr<TestingRemoteCommandsServer> { |
|
bartfab (slow)
2015/02/23 13:13:57
Nit: Please go back to using a WeakPtrFactory. Sup
binjin
2015/02/24 05:29:50
Done.
|
| + public: |
| + TestingRemoteCommandsServer(); |
| + virtual ~TestingRemoteCommandsServer(); |
| + |
| + using RemoteCommandVector = std::vector<enterprise_management::RemoteCommand>; |
| + using RemoteCommandResultVector = |
| + std::vector<enterprise_management::RemoteCommandResult>; |
| + |
| + // Create and add a command with |type| as command type and |payload| as |
| + // command payload if it's not empty. |clock_| will be used to get the |
| + // command issued time. |skip_next_fetch| should be set default to false, and |
| + // if it's set to true instead, it will only be added to the main queue after |
| + // next fetch completes. |
| + void IssueCommand(enterprise_management::RemoteCommand_Type type, |
| + const std::string& payload, |
| + bool skip_next_fetch); |
| + |
| + // Fetch commands with |last_command_id| and provide |previous_job_results| |
| + // as results for previous commands, see protobuf definition for protocol |
| + // definition between server and client for remote commands fetching. |
| + // Unlike every other methods in the class, this method can be called from |
| + // any thread. |
| + RemoteCommandVector FetchCommands( |
| + RemoteCommandJob::UniqueIDType last_command_id, |
| + const RemoteCommandResultVector& previous_job_results); |
| + |
| + // Set alternative Clock for command issued time. The default clock uses |
| + // system clock. |
| + void SetClock(scoped_ptr<base::Clock> clock); |
| + |
| + // Get the number of commands which are still expecting command results for |
| + // verifying. Note that it's possible that some command is not even fetched. |
| + size_t NumberOfCommandsPendingResult() const; |
| + |
| + protected: |
| + // Called when a command result is sent back to server. This class will also |
| + // verify that the command ID belongs to an existing command /expecting a |
| + // command result. This class will NOT verify the order of these command |
| + // results and attached payload. Test author should override this method for |
| + // additional verifying. |
| + virtual void OnJobResultReported( |
| + const enterprise_management::RemoteCommandResult& job_result) const; |
| + |
| + private: |
| + void ReportJobResult( |
| + const enterprise_management::RemoteCommandResult& job_result) const; |
| + |
| + // The main commands queue. |
| + std::vector<enterprise_management::RemoteCommand> commands_; |
| + |
| + // Commands which will be added to the main queue after the next fetch. |
| + std::vector<enterprise_management::RemoteCommand> |
| + commands_issued_after_next_fetch_; |
| + |
| + // The latest acknowledged command ID from client, should be non-decreasing. |
| + RemoteCommandJob::UniqueIDType last_acknowledged_unique_id_ = 0; |
| + |
| + // The latest command ID generated by server. This test server generates |
| + // strictly monotonically increasing unique IDs. |
| + RemoteCommandJob::UniqueIDType last_generated_unique_id_ = 0; |
| + |
| + // Clock used to generate command issued time when IssueCommand() is called. |
| + scoped_ptr<base::Clock> clock_; |
| + |
| + // A lock protecting the command queues and recorded IDs. |
| + base::Lock lock_; |
| + |
| + // The thread on which this test server is created, usually UI thread. |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| + |
| + base::ThreadChecker thread_checker_; |
| + base::WeakPtr<TestingRemoteCommandsServer> weak_ptr_to_this_; |
|
bartfab (slow)
2015/02/23 13:13:57
Nit: Document that this weak pointer is created ea
binjin
2015/02/24 05:29:50
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestingRemoteCommandsServer); |
| +}; |
| + |
| +} // namespace policy |
| + |
| +#endif // COMPONENTS_POLICY_CORE_COMMON_REMOTE_COMMANDS_TESTING_REMOTE_COMMANDS_SERVER_H_ |