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