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 "base/memory/weak_ptr.h" | |
12 #include "blimp/client/blimp_client_export.h" | 11 #include "blimp/client/blimp_client_export.h" |
13 #include "net/base/ip_endpoint.h" | 12 #include "net/base/ip_endpoint.h" |
14 #include "net/url_request/url_fetcher_delegate.h" | 13 #include "net/url_request/url_fetcher_delegate.h" |
15 | 14 |
16 namespace base { | 15 namespace base { |
17 class FilePath; | |
18 class SingleThreadTaskRunner; | 16 class SingleThreadTaskRunner; |
19 class Value; | |
20 } | 17 } |
21 | 18 |
22 namespace net { | 19 namespace net { |
23 class URLFetcher; | 20 class URLFetcher; |
24 class URLRequestContextGetter; | 21 class URLRequestContextGetter; |
25 class X509Certificate; | |
26 } | 22 } |
27 | 23 |
28 namespace blimp { | 24 namespace blimp { |
29 namespace client { | 25 namespace client { |
30 | 26 |
31 // TODO(kmarshall): Take values from configuration data. | 27 // TODO(kmarshall): Take values from configuration data. |
32 const char kDummyClientToken[] = "MyVoiceIsMyPassport"; | 28 const char kDummyClientToken[] = "MyVoiceIsMyPassport"; |
33 | 29 |
34 // Potential assigner URLs. | 30 // Potential assigner URLs. |
35 const char kDefaultAssignerURL[] = | 31 const char kDefaultAssignerURL[] = |
36 "https://blimp-pa.googleapis.com/v1/assignment"; | 32 "https://blimp-pa.googleapis.com/v1/assignment"; |
37 | 33 |
38 // An Assignment contains the configuration data needed for a client | 34 // An Assignment contains the configuration data needed for a client |
39 // to connect to the engine. | 35 // to connect to the engine. |
40 struct BLIMP_CLIENT_EXPORT Assignment { | 36 struct BLIMP_CLIENT_EXPORT Assignment { |
41 enum TransportProtocol { | 37 enum TransportProtocol { |
42 UNKNOWN = 0, | 38 UNKNOWN = 0, |
43 SSL = 1, | 39 SSL = 1, |
44 TCP = 2, | 40 TCP = 2, |
| 41 QUIC = 3, |
45 }; | 42 }; |
46 | 43 |
47 Assignment(); | 44 Assignment(); |
48 ~Assignment(); | 45 ~Assignment(); |
49 | 46 |
| 47 TransportProtocol transport_protocol; |
| 48 net::IPEndPoint ip_endpoint; |
| 49 std::string client_token; |
| 50 std::string certificate; |
| 51 std::string certificate_fingerprint; |
| 52 |
50 // Returns true if the net::IPEndPoint has an unspecified IP, port, or | 53 // Returns true if the net::IPEndPoint has an unspecified IP, port, or |
51 // transport protocol. | 54 // transport protocol. |
52 bool IsValid() const; | 55 bool is_null() const; |
53 | |
54 // Specifies the transport to use to connect to the engine. | |
55 TransportProtocol transport_protocol; | |
56 | |
57 // Specifies the IP address and port on which to reach the engine. | |
58 net::IPEndPoint engine_endpoint; | |
59 | |
60 // Used to authenticate to the specified engine. | |
61 std::string client_token; | |
62 | |
63 // Specifies the X.509 certificate that the engine must report. | |
64 scoped_refptr<net::X509Certificate> cert; | |
65 }; | 56 }; |
66 | 57 |
67 // AssignmentSource provides functionality to find out how a client should | 58 // AssignmentSource provides functionality to find out how a client should |
68 // connect to an engine. | 59 // connect to an engine. |
69 class BLIMP_CLIENT_EXPORT AssignmentSource : public net::URLFetcherDelegate { | 60 class BLIMP_CLIENT_EXPORT AssignmentSource : public net::URLFetcherDelegate { |
70 public: | 61 public: |
71 // A Java counterpart will be generated for this enum. | 62 // A Java counterpart will be generated for this enum. |
72 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.blimp.assignment | 63 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.blimp.assignment |
73 enum Result { | 64 enum Result { |
74 RESULT_UNKNOWN = 0, | 65 RESULT_UNKNOWN = 0, |
75 RESULT_OK = 1, | 66 RESULT_OK = 1, |
76 RESULT_BAD_REQUEST = 2, | 67 RESULT_BAD_REQUEST = 2, |
77 RESULT_BAD_RESPONSE = 3, | 68 RESULT_BAD_RESPONSE = 3, |
78 RESULT_INVALID_PROTOCOL_VERSION = 4, | 69 RESULT_INVALID_PROTOCOL_VERSION = 4, |
79 RESULT_EXPIRED_ACCESS_TOKEN = 5, | 70 RESULT_EXPIRED_ACCESS_TOKEN = 5, |
80 RESULT_USER_INVALID = 6, | 71 RESULT_USER_INVALID = 6, |
81 RESULT_OUT_OF_VMS = 7, | 72 RESULT_OUT_OF_VMS = 7, |
82 RESULT_SERVER_ERROR = 8, | 73 RESULT_SERVER_ERROR = 8, |
83 RESULT_SERVER_INTERRUPTED = 9, | 74 RESULT_SERVER_INTERRUPTED = 9, |
84 RESULT_NETWORK_FAILURE = 10, | 75 RESULT_NETWORK_FAILURE = 10 |
85 RESULT_INVALID_CERT = 11, | |
86 }; | 76 }; |
87 | 77 |
88 typedef base::Callback<void(AssignmentSource::Result, const Assignment&)> | 78 typedef base::Callback<void(AssignmentSource::Result, const Assignment&)> |
89 AssignmentCallback; | 79 AssignmentCallback; |
90 | 80 |
91 // |network_task_runner|: The task runner to use for querying the Assigner API | 81 // The |main_task_runner| should be the task runner for the UI thread because |
92 // over the network. | 82 // this will in some cases be used to trigger user interaction on the UI |
93 // |file_task_runner|: The task runner to use for reading cert files from disk | 83 // thread. |
94 // (specified on the command line.) | |
95 AssignmentSource( | 84 AssignmentSource( |
96 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner, | 85 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner, |
97 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner); | 86 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); |
98 | |
99 ~AssignmentSource() override; | 87 ~AssignmentSource() override; |
100 | 88 |
101 // Retrieves a valid assignment for the client and posts the result to the | 89 // Retrieves a valid assignment for the client and posts the result to the |
102 // given callback. |client_auth_token| is the OAuth2 access token to send to | 90 // given callback. |client_auth_token| is the OAuth2 access token to send to |
103 // the assigner when requesting an assignment. If this is called before a | 91 // the assigner when requesting an assignment. If this is called before a |
104 // previous call has completed, the old callback will be called with | 92 // previous call has completed, the old callback will be called with |
105 // RESULT_SERVER_INTERRUPTED and no Assignment. | 93 // RESULT_SERVER_INTERRUPTED and no Assignment. |
106 void GetAssignment(const std::string& client_auth_token, | 94 void GetAssignment(const std::string& client_auth_token, |
107 const AssignmentCallback& callback); | 95 const AssignmentCallback& callback); |
108 | 96 |
109 private: | |
110 void OnGetAssignmentFromCommandLineDone(const std::string& client_auth_token, | |
111 Assignment parsed_assignment); | |
112 void QueryAssigner(const std::string& client_auth_token); | |
113 void ParseAssignerResponse(); | |
114 void OnJsonParsed(scoped_ptr<base::Value> json); | |
115 void OnJsonParseError(const std::string& error); | |
116 | |
117 // net::URLFetcherDelegate implementation: | 97 // net::URLFetcherDelegate implementation: |
118 void OnURLFetchComplete(const net::URLFetcher* source) override; | 98 void OnURLFetchComplete(const net::URLFetcher* source) override; |
119 | 99 |
120 // GetAssignment() callback, invoked after URLFetcher completion. | 100 private: |
121 AssignmentCallback callback_; | 101 void ParseAssignerResponse(); |
122 | 102 |
123 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | 103 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
| 104 |
124 scoped_refptr<net::URLRequestContextGetter> url_request_context_; | 105 scoped_refptr<net::URLRequestContextGetter> url_request_context_; |
125 scoped_ptr<net::URLFetcher> url_fetcher_; | 106 scoped_ptr<net::URLFetcher> url_fetcher_; |
126 | 107 |
127 base::WeakPtrFactory<AssignmentSource> weak_factory_; | 108 // 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). |
| 110 AssignmentCallback callback_; |
128 | 111 |
129 DISALLOW_COPY_AND_ASSIGN(AssignmentSource); | 112 DISALLOW_COPY_AND_ASSIGN(AssignmentSource); |
130 }; | 113 }; |
131 | 114 |
132 } // namespace client | 115 } // namespace client |
133 } // namespace blimp | 116 } // namespace blimp |
134 | 117 |
135 #endif // BLIMP_CLIENT_SESSION_ASSIGNMENT_SOURCE_H_ | 118 #endif // BLIMP_CLIENT_SESSION_ASSIGNMENT_SOURCE_H_ |
OLD | NEW |