Commit 570fd1ac authored by mwakizaka's avatar mwakizaka Committed by Commit Bot

androidDevtoolsPort capability for port filtering

Previously adb uses a random port number for forwarding devtools port to Android device.
It makes difficult to filter packages based on port number. Now we can specify the port number via chromeOption.

Bug: chromedriver:3598
Change-Id: Ib8de0b3abfc7f5c66898739314d552c85852680f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2433746Reviewed-by: default avatarShengfa Lin <shengfa@google.com>
Reviewed-by: default avatarJohn Chen <johnchen@chromium.org>
Commit-Queue: Shengfa Lin <shengfa@google.com>
Cr-Commit-Position: refs/heads/master@{#812479}
parent 84f5e5b9
......@@ -403,6 +403,23 @@ Status ParseExcludeSwitches(const base::Value& option,
return Status(kOk);
}
Status ParsePortNumber(int* to_set,
const base::Value& option,
Capabilities* capabilities) {
int max_port_number = 65535;
int parsed_int = 0;
if (!option.GetAsInteger(&parsed_int))
return Status(kInvalidArgument, "must be an integer");
if (parsed_int <= 0)
return Status(kInvalidArgument, "must be positive");
if (parsed_int > max_port_number)
return Status(kInvalidArgument, "must be less than or equal to " +
base::NumberToString(max_port_number));
*to_set = parsed_int;
return Status(kOk);
}
Status ParseNetAddress(NetAddress* to_set,
const base::Value& option,
Capabilities* capabilities) {
......@@ -575,6 +592,8 @@ Status ParseChromeOptions(
base::BindRepeating(&ParseString, &capabilities->android_device_socket);
parser_map["androidUseRunningApp"] = base::BindRepeating(
&ParseBoolean, &capabilities->android_use_running_app);
parser_map["androidDevToolsPort"] = base::BindRepeating(
&ParsePortNumber, &capabilities->android_devtools_port);
parser_map["args"] = base::BindRepeating(&ParseSwitches);
parser_map["excludeSwitches"] = base::BindRepeating(&ParseExcludeSwitches);
parser_map["loadAsync"] =
......
......@@ -142,6 +142,8 @@ struct Capabilities {
bool android_use_running_app;
int android_devtools_port = 0;
base::FilePath binary;
// If provided, the remote debugging address to connect to.
......
......@@ -143,18 +143,20 @@ Status AdbImpl::GetDevices(std::vector<std::string>* devices) {
Status AdbImpl::ForwardPort(const std::string& device_serial,
const std::string& remote_abstract,
int* local_port_output) {
int* local_port) {
std::string response;
Status adb_command_status = ExecuteHostCommand(
device_serial, "forward:tcp:0;localabstract:" + remote_abstract,
device_serial, "forward:tcp:" + base::NumberToString(*local_port) +
";localabstract:" + remote_abstract,
&response);
// response should be the port number like "39025".
if (!adb_command_status.IsOk())
return Status(kUnknownError, "Failed to forward ports to device " +
device_serial + ": " + response + ". " +
adb_command_status.message());
base::StringToInt(response, local_port_output);
if (*local_port_output == 0) {
int local_port_output;
base::StringToInt(response, &local_port_output);
if (local_port_output == 0) {
return Status(
kUnknownError,
base::StringPrintf(
......@@ -164,8 +166,13 @@ Status AdbImpl::ForwardPort(const std::string& device_serial,
"the host device to find your version of adb.",
device_serial.c_str(), response.c_str(),
kChromeDriverProductFullName));
} else if (*local_port != 0 && local_port_output != *local_port) {
return Status(
kUnknownError,
base::StringPrintf("Failed to forward ports to device %s with the"
"specified port: %d.", device_serial.c_str(), *local_port));
}
*local_port = local_port_output;
return Status(kOk);
}
......
......@@ -684,7 +684,7 @@ Status LaunchAndroidChrome(network::mojom::URLLoaderFactory* factory,
std::unique_ptr<Chrome>* chrome) {
Status status(kOk);
std::unique_ptr<Device> device;
int devtools_port;
int devtools_port = capabilities.android_devtools_port;
if (capabilities.android_device_serial.empty()) {
status = device_manager->AcquireDevice(&device);
} else {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment