| OLD | NEW | 
|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 CHROME_BROWSER_POLICY_CLOUD_TEST_REQUEST_INTERCEPTOR_H_ | 5 #ifndef CHROME_BROWSER_POLICY_CLOUD_TEST_REQUEST_INTERCEPTOR_H_ | 
| 6 #define CHROME_BROWSER_POLICY_CLOUD_TEST_REQUEST_INTERCEPTOR_H_ | 6 #define CHROME_BROWSER_POLICY_CLOUD_TEST_REQUEST_INTERCEPTOR_H_ | 
| 7 | 7 | 
| 8 #include <string> | 8 #include <string> | 
| 9 | 9 | 
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" | 
| 11 #include "base/callback.h" | 11 #include "base/callback.h" | 
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" | 
| 13 #include "policy/proto/device_management_backend.pb.h" | 13 #include "policy/proto/device_management_backend.pb.h" | 
| 14 | 14 | 
| 15 namespace base { | 15 namespace base { | 
| 16 class FilePath; | 16 class FilePath; | 
| 17 class SequencedTaskRunner; | 17 class SequencedTaskRunner; | 
| 18 } | 18 } | 
| 19 | 19 | 
| 20 namespace net { | 20 namespace net { | 
| 21 class NetworkDelegate; | 21 class NetworkDelegate; | 
| 22 class URLRequest; | 22 class URLRequest; | 
| 23 class URLRequestJob; | 23 class URLRequestJob; | 
| 24 } | 24 } | 
| 25 | 25 | 
| 26 namespace policy { | 26 namespace policy { | 
| 27 | 27 | 
|  | 28 class TestingRemoteCommandsServer; | 
|  | 29 | 
| 28 // Intercepts all requests to the given hostname while in scope, and allows | 30 // Intercepts all requests to the given hostname while in scope, and allows | 
| 29 // queuing callbacks to handle expected requests. Must be created and destroyed | 31 // queuing callbacks to handle expected requests. Must be created and destroyed | 
| 30 // while the IO thread is valid. | 32 // while the IO thread is valid. | 
| 31 class TestRequestInterceptor { | 33 class TestRequestInterceptor { | 
| 32  public: | 34  public: | 
| 33   // A callback that returns a new URLRequestJob given a URLRequest. | 35   // A callback that returns a new URLRequestJob given a URLRequest. | 
| 34   // This is used to queue callbacks that will handle expected requests. | 36   // This is used to queue callbacks that will handle expected requests. | 
| 35   typedef base::Callback< | 37   typedef base::Callback< | 
| 36       net::URLRequestJob*(net::URLRequest*, net::NetworkDelegate*)> JobCallback; | 38       net::URLRequestJob*(net::URLRequest*, net::NetworkDelegate*)> JobCallback; | 
| 37 | 39 | 
| 38   // Will intercept request to |hostname| made over HTTP. | 40   // Will intercept request to |hostname| made over HTTP. | 
| 39   TestRequestInterceptor( | 41   TestRequestInterceptor( | 
| 40       const std::string& hostname, | 42       const std::string& hostname, | 
| 41       scoped_refptr<base::SequencedTaskRunner> io_task_runner); | 43       scoped_refptr<base::SequencedTaskRunner> io_task_runner); | 
| 42   virtual ~TestRequestInterceptor(); | 44   virtual ~TestRequestInterceptor(); | 
| 43 | 45 | 
| 44   // Returns the number of pending callback jobs that haven't been used yet. | 46   // Returns the number of pending callback jobs that haven't been used yet. | 
| 45   size_t GetPendingSize(); | 47   size_t GetPendingSize(); | 
| 46 | 48 | 
| 47   // |callback| will be invoked on the current loop once the next request | 49   // |callback| will be invoked on the current loop once the next request | 
| 48   // intercepted by this class has been dispatched. | 50   // intercepted by this class has been dispatched. | 
| 49   void AddRequestServicedCallback(const base::Closure& callback); | 51   void AddRequestServicedCallback(const base::Closure& callback); | 
| 50 | 52 | 
| 51   // Queues |callback| to handle a request to |hostname_|. Each callback is | 53   // Queues |callback| to handle a request to |hostname_|. Each callback is | 
| 52   // used only once, and in the order that they're pushed. | 54   // used only once, and in the order that they're pushed. | 
| 53   void PushJobCallback(const JobCallback& callback); | 55   void PushJobCallback(const JobCallback& callback); | 
| 54 | 56 | 
|  | 57   // Explicitly add certain request types to be ignored. For valid request of | 
|  | 58   // given ignored types, callback from queue will NOT be consumed and a bad | 
|  | 59   // request response will be sent back instead. | 
|  | 60   void AddIgnoredRequestType(const std::string& ignored_type); | 
|  | 61 | 
| 55   // Returns a JobCallback that will fail with the given network |error|. | 62   // Returns a JobCallback that will fail with the given network |error|. | 
| 56   static JobCallback ErrorJob(int error); | 63   static JobCallback ErrorJob(int error); | 
| 57 | 64 | 
| 58   // Returns a JobCallback that will fail with HTTP 400 Bad Request. | 65   // Returns a JobCallback that will fail with HTTP 400 Bad Request. | 
| 59   static JobCallback BadRequestJob(); | 66   static JobCallback BadRequestJob(); | 
| 60 | 67 | 
| 61   // Returns a JobCallback that will process a policy register request that | 68   // Returns a JobCallback that will process a policy register request that | 
| 62   // should succeed. The request parameters are validated, and an appropriate | 69   // should succeed. The request parameters are validated, and an appropriate | 
| 63   // response is sent back. | 70   // response is sent back. | 
| 64   // |expected_type| is the expected type in the register request. | 71   // |expected_type| is the expected type in the register request. | 
| 65   // If |expect_reregister| is true then the request must have the reregister | 72   // If |expect_reregister| is true then the request must have the reregister | 
| 66   // flag set; otherwise the flag must be not set. | 73   // flag set; otherwise the flag must be not set. | 
| 67   static JobCallback RegisterJob( | 74   static JobCallback RegisterJob( | 
| 68       enterprise_management::DeviceRegisterRequest::Type expected_type, | 75       enterprise_management::DeviceRegisterRequest::Type expected_type, | 
| 69       bool expect_reregister); | 76       bool expect_reregister); | 
| 70 | 77 | 
| 71   // Returns a JobCallback that will send the contents of |file_path|. | 78   // Returns a JobCallback that will send the contents of |file_path|. | 
| 72   static JobCallback FileJob(const base::FilePath& file_path); | 79   static JobCallback FileJob(const base::FilePath& file_path); | 
| 73 | 80 | 
|  | 81   // Returns a JobCallback that will handle a "fetch remote command" request. | 
|  | 82   // |server| is a pointer to a TestingRemoteCommandsServer instance to handle | 
|  | 83   // the actual request, and is expected to be valid at the time the job is | 
|  | 84   // serviced. |expected_command_results| is the expected number of previous | 
|  | 85   // command results that will be sent by client.  |expected_fetched_commands| | 
|  | 86   // is the expected number of fetched commands by this callback. | 
|  | 87   static JobCallback FetchRemoteCommandsJob(TestingRemoteCommandsServer* server, | 
|  | 88                                             size_t expected_command_results, | 
|  | 89                                             size_t expected_fetched_commands); | 
|  | 90 | 
| 74  private: | 91  private: | 
| 75   class Delegate; | 92   class Delegate; | 
| 76 | 93 | 
| 77   // Helper to execute a |task| on IO, and return only after it has completed. | 94   // Helper to execute a |task| on IO, and return only after it has completed. | 
| 78   void PostToIOAndWait(const base::Closure& task); | 95   void PostToIOAndWait(const base::Closure& task); | 
| 79 | 96 | 
| 80   const std::string hostname_; | 97   const std::string hostname_; | 
| 81 | 98 | 
| 82   scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | 99   scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | 
| 83 | 100 | 
| 84   // Owned by URLRequestFilter. This handle is valid on IO and only while the | 101   // Owned by URLRequestFilter. This handle is valid on IO and only while the | 
| 85   // interceptor is valid. | 102   // interceptor is valid. | 
| 86   Delegate* delegate_; | 103   Delegate* delegate_; | 
| 87 | 104 | 
| 88   DISALLOW_COPY_AND_ASSIGN(TestRequestInterceptor); | 105   DISALLOW_COPY_AND_ASSIGN(TestRequestInterceptor); | 
| 89 }; | 106 }; | 
| 90 | 107 | 
| 91 }  // namespace policy | 108 }  // namespace policy | 
| 92 | 109 | 
| 93 #endif  // CHROME_BROWSER_POLICY_CLOUD_TEST_REQUEST_INTERCEPTOR_H_ | 110 #endif  // CHROME_BROWSER_POLICY_CLOUD_TEST_REQUEST_INTERCEPTOR_H_ | 
| OLD | NEW | 
|---|