Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 BLIMP_CLIENT_SESSION_ASSIGNMENT_SOURCE_H_ | 5 #ifndef BLIMP_CLIENT_SESSION_ASSIGNMENT_SOURCE_H_ |
| 6 #define BLIMP_CLIENT_SESSION_ASSIGNMENT_SOURCE_H_ | 6 #define BLIMP_CLIENT_SESSION_ASSIGNMENT_SOURCE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "blimp/client/blimp_client_export.h" | 11 #include "blimp/client/blimp_client_export.h" |
| 12 #include "net/base/ip_endpoint.h" | 12 #include "net/base/ip_endpoint.h" |
| 13 #include "net/url_request/url_fetcher_delegate.h" | 13 #include "net/url_request/url_fetcher_delegate.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 class FilePath; | |
| 16 class SingleThreadTaskRunner; | 17 class SingleThreadTaskRunner; |
| 18 class TaskRunner; | |
| 17 } | 19 } |
| 18 | 20 |
| 19 namespace net { | 21 namespace net { |
| 20 class URLFetcher; | 22 class URLFetcher; |
| 21 class URLRequestContextGetter; | 23 class URLRequestContextGetter; |
| 24 class X509Certificate; | |
| 22 } | 25 } |
| 23 | 26 |
| 24 namespace blimp { | 27 namespace blimp { |
| 25 namespace client { | 28 namespace client { |
| 26 | 29 |
| 27 // TODO(kmarshall): Take values from configuration data. | 30 // TODO(kmarshall): Take values from configuration data. |
| 28 const char kDummyClientToken[] = "MyVoiceIsMyPassport"; | 31 const char kDummyClientToken[] = "MyVoiceIsMyPassport"; |
| 29 | 32 |
| 30 // Potential assigner URLs. | 33 // Potential assigner URLs. |
| 31 const char kDefaultAssignerURL[] = | 34 const char kDefaultAssignerURL[] = |
| 32 "https://blimp-pa.googleapis.com/v1/assignment"; | 35 "https://blimp-pa.googleapis.com/v1/assignment"; |
| 33 | 36 |
| 34 // An Assignment contains the configuration data needed for a client | 37 // An Assignment contains the configuration data needed for a client |
| 35 // to connect to the engine. | 38 // to connect to the engine. |
| 36 struct BLIMP_CLIENT_EXPORT Assignment { | 39 struct BLIMP_CLIENT_EXPORT Assignment { |
| 37 enum TransportProtocol { | 40 enum TransportProtocol { |
| 38 UNKNOWN = 0, | 41 UNKNOWN = 0, |
| 39 SSL = 1, | 42 SSL = 1, |
| 40 TCP = 2, | 43 TCP = 2, |
| 41 QUIC = 3, | 44 QUIC = 3, |
| 42 }; | 45 }; |
| 43 | 46 |
| 44 Assignment(); | 47 Assignment(); |
| 45 ~Assignment(); | 48 ~Assignment(); |
| 46 | 49 |
| 47 TransportProtocol transport_protocol; | |
| 48 net::IPEndPoint ip_endpoint; | |
| 49 std::string client_token; | |
| 50 std::string certificate; | |
| 51 std::string certificate_fingerprint; | |
| 52 | |
| 53 // Returns true if the net::IPEndPoint has an unspecified IP, port, or | 50 // Returns true if the net::IPEndPoint has an unspecified IP, port, or |
| 54 // transport protocol. | 51 // transport protocol. |
| 55 bool is_null() const; | 52 bool is_null() const; |
| 53 | |
| 54 // The transport protocol used to contact the engine. | |
| 55 TransportProtocol transport_protocol; | |
| 56 | |
| 57 // The IP address of the engine. | |
| 58 net::IPEndPoint ip_endpoint; | |
| 59 | |
| 60 // The token used to authenticate the client to the engine. | |
| 61 std::string client_token; | |
| 62 | |
| 63 // The expected certificate of the engine. The peer certificate must match | |
| 64 // |cert| exactly, otherwise the connection will be dropped. | |
| 65 scoped_refptr<net::X509Certificate> cert; | |
| 66 }; | |
| 67 | |
| 68 class FileReader { | |
|
Ryan Sleevi
2016/02/23 00:49:45
What's the envisioned usage here for this? Judging
Kevin M
2016/02/23 01:58:25
This provides us a mockable hook for file access,
| |
| 69 public: | |
| 70 FileReader(); | |
| 71 virtual ~FileReader(); | |
| 72 | |
| 73 virtual bool ReadFileToString(const base::FilePath& path, | |
| 74 std::string* output); | |
| 75 | |
| 76 private: | |
| 77 DISALLOW_COPY_AND_ASSIGN(FileReader); | |
| 56 }; | 78 }; |
| 57 | 79 |
| 58 // AssignmentSource provides functionality to find out how a client should | 80 // AssignmentSource provides functionality to find out how a client should |
| 59 // connect to an engine. | 81 // connect to an engine. |
| 60 class BLIMP_CLIENT_EXPORT AssignmentSource : public net::URLFetcherDelegate { | 82 class BLIMP_CLIENT_EXPORT AssignmentSource : public net::URLFetcherDelegate { |
| 61 public: | 83 public: |
| 62 // A Java counterpart will be generated for this enum. | 84 // A Java counterpart will be generated for this enum. |
| 63 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.blimp.assignment | 85 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.blimp.assignment |
| 64 enum Result { | 86 enum Result { |
| 65 RESULT_UNKNOWN = 0, | 87 RESULT_UNKNOWN = 0, |
| 66 RESULT_OK = 1, | 88 RESULT_OK = 1, |
| 67 RESULT_BAD_REQUEST = 2, | 89 RESULT_BAD_REQUEST = 2, |
| 68 RESULT_BAD_RESPONSE = 3, | 90 RESULT_BAD_RESPONSE = 3, |
| 69 RESULT_INVALID_PROTOCOL_VERSION = 4, | 91 RESULT_INVALID_PROTOCOL_VERSION = 4, |
| 70 RESULT_EXPIRED_ACCESS_TOKEN = 5, | 92 RESULT_EXPIRED_ACCESS_TOKEN = 5, |
| 71 RESULT_USER_INVALID = 6, | 93 RESULT_USER_INVALID = 6, |
| 72 RESULT_OUT_OF_VMS = 7, | 94 RESULT_OUT_OF_VMS = 7, |
| 73 RESULT_SERVER_ERROR = 8, | 95 RESULT_SERVER_ERROR = 8, |
| 74 RESULT_SERVER_INTERRUPTED = 9, | 96 RESULT_SERVER_INTERRUPTED = 9, |
| 75 RESULT_NETWORK_FAILURE = 10 | 97 RESULT_NETWORK_FAILURE = 10, |
| 98 RESULT_INVALID_CERT = 11, | |
| 76 }; | 99 }; |
| 77 | 100 |
| 78 typedef base::Callback<void(AssignmentSource::Result, const Assignment&)> | 101 typedef base::Callback<void(AssignmentSource::Result, const Assignment&)> |
| 79 AssignmentCallback; | 102 AssignmentCallback; |
| 80 | 103 |
| 81 // The |main_task_runner| should be the task runner for the UI thread because | |
| 82 // this will in some cases be used to trigger user interaction on the UI | |
| 83 // thread. | |
| 84 AssignmentSource( | 104 AssignmentSource( |
| 85 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner, | |
| 86 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); | 105 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); |
| 87 ~AssignmentSource() override; | 106 ~AssignmentSource() override; |
| 88 | 107 |
| 89 // Retrieves a valid assignment for the client and posts the result to the | 108 // Retrieves a valid assignment for the client and posts the result to the |
| 90 // given callback. |client_auth_token| is the OAuth2 access token to send to | 109 // given callback. |client_auth_token| is the OAuth2 access token to send to |
| 91 // the assigner when requesting an assignment. If this is called before a | 110 // the assigner when requesting an assignment. If this is called before a |
| 92 // previous call has completed, the old callback will be called with | 111 // previous call has completed, the old callback will be called with |
| 93 // RESULT_SERVER_INTERRUPTED and no Assignment. | 112 // RESULT_SERVER_INTERRUPTED and no Assignment. |
| 94 void GetAssignment(const std::string& client_auth_token, | 113 void GetAssignment(const std::string& client_auth_token, |
| 95 const AssignmentCallback& callback); | 114 const AssignmentCallback& callback); |
| 96 | 115 |
| 116 // Populates an Assignment using command-line parameters, if provided. | |
| 117 // Returns a null Assignment if no parameters were set. | |
| 118 // Must be called on the IO thread. | |
| 119 scoped_ptr<Assignment> GetCustomAssignment(); | |
| 120 | |
| 121 // Called when GetCustomAssignment() has completed. | |
| 122 // Uses |custom_assignment| if provided; queries the Assigner for one | |
| 123 // otherwise. | |
| 124 void OnGetCustomAssignmentDone(const std::string& client_auth_token, | |
| 125 scoped_ptr<Assignment> custom_assignment); | |
| 126 | |
| 127 void SetFileReaderForTest(scoped_ptr<FileReader> reader); | |
| 128 | |
| 97 // net::URLFetcherDelegate implementation: | 129 // net::URLFetcherDelegate implementation: |
| 98 void OnURLFetchComplete(const net::URLFetcher* source) override; | 130 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 99 | 131 |
| 100 private: | 132 private: |
| 101 void ParseAssignerResponse(); | 133 void ParseAssignerResponse(); |
| 102 | 134 |
| 103 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | 135 scoped_refptr<base::TaskRunner> io_task_runner_; |
| 104 | 136 |
| 105 scoped_refptr<net::URLRequestContextGetter> url_request_context_; | 137 scoped_refptr<net::URLRequestContextGetter> url_request_context_; |
| 106 scoped_ptr<net::URLFetcher> url_fetcher_; | 138 scoped_ptr<net::URLFetcher> url_fetcher_; |
| 139 scoped_ptr<FileReader> file_reader_; | |
| 107 | 140 |
| 108 // This callback is set during a call to GetAssignment() and is cleared after | 141 // This callback is set during a call to GetAssignment() and is cleared after |
| 109 // the request has completed (whether it be a success or failure). | 142 // the request has completed (whether it be a success or failure). |
| 110 AssignmentCallback callback_; | 143 AssignmentCallback callback_; |
| 111 | 144 |
| 112 DISALLOW_COPY_AND_ASSIGN(AssignmentSource); | 145 DISALLOW_COPY_AND_ASSIGN(AssignmentSource); |
| 113 }; | 146 }; |
| 114 | 147 |
| 115 } // namespace client | 148 } // namespace client |
| 116 } // namespace blimp | 149 } // namespace blimp |
| 117 | 150 |
| 118 #endif // BLIMP_CLIENT_SESSION_ASSIGNMENT_SOURCE_H_ | 151 #endif // BLIMP_CLIENT_SESSION_ASSIGNMENT_SOURCE_H_ |
| OLD | NEW |