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 #include "blimp/client/session/assignment_source.h" | 5 #include "blimp/client/session/assignment_source.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | |
| 8 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_util.h" | |
| 9 #include "base/location.h" | 11 #include "base/location.h" |
| 10 #include "base/numerics/safe_conversions.h" | 12 #include "base/numerics/safe_conversions.h" |
| 11 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 12 #include "blimp/client/app/blimp_client_switches.h" | 14 #include "blimp/client/app/blimp_client_switches.h" |
| 15 #include "net/base/hash_value.h" | |
| 13 #include "net/base/ip_address.h" | 16 #include "net/base/ip_address.h" |
| 14 #include "net/base/ip_endpoint.h" | 17 #include "net/base/ip_endpoint.h" |
| 18 #include "net/cert/pem_tokenizer.h" | |
|
Ryan Sleevi
2016/02/19 22:56:08
DESIGN: This is not intended to be used outside of
Kevin M
2016/02/23 00:28:09
Done.
| |
| 15 | 19 |
| 16 namespace blimp { | 20 namespace blimp { |
| 21 namespace client { | |
| 17 namespace { | 22 namespace { |
| 18 | 23 |
| 19 // TODO(kmarshall): Take values from configuration data. | 24 // TODO(kmarshall): Take values from configuration data. |
| 20 const char kDummyClientToken[] = "MyVoiceIsMyPassport"; | 25 const char kDummyClientToken[] = "MyVoiceIsMyPassport"; |
| 21 const std::string kDefaultBlimpletIPAddress = "127.0.0.1"; | 26 const std::string kDefaultBlimpletIPAddress = "127.0.0.1"; |
| 22 const uint16_t kDefaultBlimpletTCPPort = 25467; | |
| 23 | 27 |
| 24 net::IPAddress GetBlimpletIPAddress() { | 28 net::IPAddress GetBlimpletIPAddress() { |
| 25 std::string host; | 29 std::string host = |
| 26 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 30 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 27 switches::kBlimpletHost)) { | 31 switches::kEngineHost); |
| 28 host = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 32 if (host.empty()) { |
| 29 switches::kBlimpletHost); | |
| 30 } else { | |
| 31 host = kDefaultBlimpletIPAddress; | 33 host = kDefaultBlimpletIPAddress; |
| 32 } | 34 } |
| 33 net::IPAddress ip_address; | 35 net::IPAddress ip_address; |
| 34 if (!ip_address.AssignFromIPLiteral(host)) | 36 CHECK(ip_address.AssignFromIPLiteral(host)) |
|
Ryan Sleevi
2016/02/19 22:56:08
DESIGN: It seems counter to Chromium practices to
Kevin M
2016/02/22 22:53:31
Do command line parameters count as user input, as
Wez
2016/03/01 00:23:55
For code which users will invoke via the command-l
| |
| 35 CHECK(false) << "Invalid BlimpletAssignment host " << host; | 37 << "Invalid BlimpletAssignment host " << host; |
| 36 return ip_address; | 38 return ip_address; |
| 37 } | 39 } |
| 38 | 40 |
| 39 uint16_t GetBlimpletTCPPort() { | 41 // Puts the value of the command line parameter |param| in |output|. |
| 40 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 42 // If the parameter was not found, |output| is set to 0. |
| 41 switches::kBlimpletTCPPort)) { | 43 // CHECK()s that |params| decodes to a valid IP port number. |
| 42 std::string port_str = | 44 void GetUint16Parameter(const std::string& param, uint16_t* output) { |
| 43 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 45 *output = 0; |
| 44 switches::kBlimpletTCPPort); | 46 std::string param_str = |
| 45 uint port_64t; | 47 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(param); |
| 46 if (!base::StringToUint(port_str, &port_64t) || | 48 if (param_str.empty()) { |
| 47 !base::IsValueInRangeForNumericType<uint16_t>(port_64t)) { | 49 return; |
| 48 CHECK(false) << "Invalid BlimpletAssignment port " << port_str; | |
| 49 } | |
| 50 return base::checked_cast<uint16_t>(port_64t); | |
| 51 } else { | |
| 52 return kDefaultBlimpletTCPPort; | |
| 53 } | 50 } |
| 51 | |
| 52 uint param_parsed = 0; | |
|
Ryan Sleevi
2016/02/19 22:56:07
BUG: C++ does not define "uint". Please use one of
Kevin M
2016/02/22 22:53:31
Done.
| |
| 53 bool is_valid = base::StringToUint(param_str, ¶m_parsed) && | |
| 54 param_parsed > 0 && param_parsed <= 65535; | |
| 55 CHECK(is_valid) << "Invalid range for parameter " << param; | |
|
Ryan Sleevi
2016/02/19 22:56:08
DESIGN: Please use //base/numerics/safe_conversion
Kevin M
2016/02/22 22:53:31
Done. Should ParseHostAndPort be made to return |p
| |
| 56 *output = param_parsed; | |
| 57 } | |
| 58 | |
| 59 // Reads the contents of |path| into |output|. | |
| 60 void ReadFromDisk(const base::FilePath& path, std::string* output) { | |
| 61 DCHECK(output); | |
| 62 CHECK(base::ReadFileToString(path, output)) << "Couldn't read from file: " | |
| 63 << path.LossyDisplayName(); | |
| 64 } | |
| 65 | |
| 66 // Parses a certificate from PEM-encoded |cert_str| and attaches it to | |
| 67 // |assignment|. Returns the populated assignment object via |callback|. | |
| 68 void ParseCertForAssignment( | |
| 69 scoped_ptr<std::string> cert_str, | |
| 70 scoped_ptr<Assignment> assignment, | |
| 71 const AssignmentSource::AssignmentCallback& callback) { | |
| 72 DCHECK(cert_str); | |
| 73 DCHECK(!cert_str->empty()); | |
| 74 | |
| 75 net::PEMTokenizer pem_tokenizer(*cert_str, {"CERTIFICATE"}); | |
|
Ryan Sleevi
2016/02/19 22:56:07
STYLE: Uniform initialization syntax is explicitly
Kevin M
2016/02/22 22:53:31
Done. (FYI, other PEMTokenizer clients do the same
Kevin M
2016/02/23 00:28:09
PEMTokenizer was removed and replaced with CreateC
| |
| 76 while (pem_tokenizer.GetNext()) { | |
| 77 CHECK(!assignment->cert) << "More than one CERTIFICATE entries provided."; | |
| 78 assignment->cert = net::X509Certificate::CreateFromBytes( | |
| 79 pem_tokenizer.data().data(), pem_tokenizer.data().length()); | |
| 80 CHECK(assignment->cert) << "Couldn't parse CERTIFICATE entry."; | |
| 81 } | |
| 82 | |
| 83 callback.Run(*assignment); | |
| 54 } | 84 } |
| 55 | 85 |
| 56 } // namespace | 86 } // namespace |
| 57 | 87 |
| 58 namespace client { | 88 Assignment::Assignment() {} |
| 89 | |
| 90 Assignment::~Assignment() {} | |
| 59 | 91 |
| 60 AssignmentSource::AssignmentSource( | 92 AssignmentSource::AssignmentSource( |
| 61 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) | 93 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) |
| 62 : main_task_runner_(main_task_runner) {} | 94 : main_task_runner_(main_task_runner) {} |
| 63 | 95 |
| 64 AssignmentSource::~AssignmentSource() {} | 96 AssignmentSource::~AssignmentSource() {} |
| 65 | 97 |
| 98 scoped_refptr<base::SingleThreadTaskRunner> | |
|
Ryan Sleevi
2016/02/19 22:56:08
DESIGN: Your API contract does not require the use
Kevin M
2016/02/22 22:53:31
The URLRequestContextGetter (now integrated on tru
| |
| 99 AssignmentSource::GetIOTaskRunner() { | |
| 100 if (!io_thread_) { | |
| 101 io_thread_.reset(new base::Thread("CertFileThread")); | |
| 102 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); | |
| 103 io_thread_->StartWithOptions(options); | |
| 104 } | |
| 105 return io_thread_->task_runner(); | |
| 106 } | |
| 107 | |
| 66 void AssignmentSource::GetAssignment(const AssignmentCallback& callback) { | 108 void AssignmentSource::GetAssignment(const AssignmentCallback& callback) { |
| 67 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 109 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 68 Assignment assignment; | 110 |
| 69 assignment.ip_endpoint = | 111 scoped_ptr<Assignment> assignment(new Assignment); |
| 70 net::IPEndPoint(GetBlimpletIPAddress(), GetBlimpletTCPPort()); | 112 assignment->client_token = kDummyClientToken; |
| 71 assignment.client_token = kDummyClientToken; | 113 assignment->ip_addresses.push_back(GetBlimpletIPAddress()); |
| 72 main_task_runner_->PostTask(FROM_HERE, base::Bind(callback, assignment)); | 114 GetUint16Parameter(switches::kEngineTCPPort, &assignment->tcp_port); |
| 115 GetUint16Parameter(switches::kEngineSSLPort, &assignment->ssl_port); | |
| 116 if (assignment->ssl_port > 0) { | |
| 117 base::FilePath cert_path = | |
| 118 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( | |
| 119 switches::kEngineCertPath); | |
| 120 CHECK(!cert_path.empty()) << "Missing required parameter --" | |
| 121 << switches::kEngineCertPath << "."; | |
| 122 | |
| 123 scoped_ptr<std::string> cert_str(new std::string); | |
| 124 std::string* cert_str_ptr = cert_str.get(); | |
|
Ryan Sleevi
2016/02/19 22:56:08
PostTaskAndReplyWithResult explicitly exists to av
Kevin M
2016/02/22 22:53:31
Thanks, done. I switched GetCustomAssignment to us
| |
| 125 GetIOTaskRunner()->PostTaskAndReply( | |
|
Ryan Sleevi
2016/02/19 22:56:07
BUG/DESIGN: This seems improperly named, or at lea
Kevin M
2016/02/22 22:53:31
Question: how do we create a FILE thread using bas
Bernhard Bauer
2016/02/26 16:26:30
+1 on Ryan's comment; it was very confusing to me
Kevin M
2016/02/26 19:57:22
In the latest patch, you'll notice that we take a
| |
| 126 FROM_HERE, base::Bind(&ReadFromDisk, cert_path, cert_str_ptr), | |
| 127 base::Bind(&ParseCertForAssignment, base::Passed(std::move(cert_str)), | |
| 128 base::Passed(std::move(assignment)), callback)); | |
|
Ryan Sleevi
2016/02/19 22:56:07
DESIGN: While this is only one level, I find mysel
Kevin M
2016/02/22 22:53:31
Ack.
| |
| 129 } else { | |
| 130 callback.Run(*assignment); | |
| 131 } | |
| 73 } | 132 } |
| 74 | 133 |
| 75 } // namespace client | 134 } // namespace client |
| 76 } // namespace blimp | 135 } // namespace blimp |
| OLD | NEW |