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..213b56742a5938bf0eaf52adc8e054b30c7fec03 |
| --- /dev/null |
| +++ b/components/policy/core/common/remote_commands/testing_remote_commands_server.h |
| @@ -0,0 +1,113 @@ |
| +// 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() should be |
| +// called from another thread like IO thread. |
| +class TestingRemoteCommandsServer { |
| + 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 be delayed to add the main queue after |
|
bartfab (slow)
2015/02/17 18:13:53
Nit: s/it will be delayed to add/it will only be a
binjin
2015/02/18 15:37:51
Done.
|
| + // 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. |
| + // Only calling from another thread is supported at this moment. |
| + RemoteCommandVector FetchCommandsFromIO( |
| + 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 |
|
bartfab (slow)
2015/02/17 18:13:53
Nit: Add full stop at the end.
binjin
2015/02/18 15:37:51
Done.
|
| + RemoteCommandJob::UniqueIDType last_generated_unique_id_ = 0; |
| + |
| + // Clock used to generate command issued time when IssueCommand() is called. |
| + scoped_ptr<base::Clock> clock_; |
| + |
| + base::Lock lock_; |
|
bartfab (slow)
2015/02/17 18:13:53
Nit: Document which members are protected by this
binjin
2015/02/18 15:37:51
Done.
|
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
|
bartfab (slow)
2015/02/17 18:13:53
Nit: Document which thread this represents.
binjin
2015/02/18 15:37:51
Done.
|
| + base::ThreadChecker thread_checker_; |
| + base::WeakPtrFactory<TestingRemoteCommandsServer> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestingRemoteCommandsServer); |
| +}; |
| + |
| +} // namespace policy |
| + |
| +#endif // COMPONENTS_POLICY_CORE_COMMON_REMOTE_COMMANDS_TESTING_REMOTE_COMMANDS_SERVER_H_ |