Skip to content

Commit

Permalink
Win support added.
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Jun 24, 2013
1 parent fc2db86 commit 65cafdc
Show file tree
Hide file tree
Showing 19 changed files with 447 additions and 43 deletions.
133 changes: 116 additions & 17 deletions ADBPluginAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,36 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#if defined(OS_WIN)
#include <winsock2.h>
#elif defined(OS_POSIX)
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#endif

#include "JSObject.h"
#include "variant_list.h"
#include "DOM/Document.h"
#include "global/config.h"

#include "ADBPluginAPI.h"

FB::variant ADBPluginAPI::isServerRunning()
#if defined(OS_WIN)
typedef SOCKET Socket;
#else
typedef int Socket;
#endif

void CloseSocket(Socket socket)
{
std::string result = shell("echo '000Chost:devices' | telnet 127.0.0.1 5037 2>/dev/null");
return result.find("Connected to localhost") != std::string::npos;
#if defined(OS_WIN)
closesocket(socket);
#else
close(socket);
#endif
}

FB::variant ADBPluginAPI::startServer()
Expand All @@ -27,32 +46,112 @@ FB::variant ADBPluginAPI::killServer()

FB::variant ADBPluginAPI::devices()
{
return adb("devices");
}
struct sockaddr_in client;
client.sin_family = AF_INET;
client.sin_port = htons(5037);
client.sin_addr.s_addr = inet_addr("127.0.0.1");
Socket sock = socket(AF_INET, SOCK_STREAM, 0);

std::string ADBPluginAPI::shell(const std::string& command)
{
FILE * pPipe;
fd_set readfd;
char buffer[2048];
if (connect(sock, (struct sockaddr*)&client, sizeof(client)) != 0) {
CloseSocket(sock);
return "Error: could not connect to ADB";
}

if (!(pPipe = popen(command.c_str(), "r")))
return "failed starting server";
std::string devices = "000Chost:devices";
if (send(sock, devices.c_str(), devices.length(), 0) == -1) {
CloseSocket(sock);
return "Error: could not send command";
}

std::string response = "";
while(fgets(buffer, sizeof(buffer), pPipe) != NULL)
response += buffer;
pclose(pPipe);
std::string response;
char buffer[10240];
int result;
do {
result = recv(sock, &buffer[0], 10240, 0);
if (result > 0)
response += std::string(&buffer[0], result);
else if (result < 0)
response = "Error: could not read response";
} while (result != 0);
CloseSocket(sock);
return response;
}

std::string ADBPluginAPI::adb(const std::string& command)
{
std::string plugin_path = getPlugin()->getFilesystemPath();
#if defined(OS_WIN)
plugin_path = plugin_path.substr(0, plugin_path.rfind("\\"));
std::string adb_path = plugin_path + "\\win\\adb.exe";

SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;

HANDLE pipe_read, pipe_write;
int result = CreatePipe(&pipe_read, &pipe_write, &sa, 0);
if (!result)
return "Error: could not create pipe";

SetHandleInformation(pipe_read, HANDLE_FLAG_INHERIT, 0);

STARTUPINFOA startup;
ZeroMemory(&startup, sizeof(startup));
startup.cb = sizeof(startup);
startup.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
startup.hStdOutput = pipe_write;
startup.hStdError = GetStdHandle(STD_ERROR_HANDLE);
startup.dwFlags = STARTF_USESTDHANDLES;

PROCESS_INFORMATION pinfo;
ZeroMemory(&pinfo, sizeof(pinfo));
std::string adb_command = "adb " + command;
result = CreateProcessA(
adb_path.c_str(), /* program path */
const_cast<char*>(adb_command.c_str()), /* command */
NULL, /* process handle is not inheritable */
NULL, /* thread handle is not inheritable */
TRUE, /* yes, inherit some handles */
DETACHED_PROCESS, /* the new process doesn't have a console */
NULL, /* use parent's environment block */
NULL, /* use parent's starting directory */
&startup, /* startup info, i.e. std handles */
&pinfo);

CloseHandle(pipe_write);

if (!result)
return "Error: could not create process";

CloseHandle(pinfo.hProcess);
CloseHandle(pinfo.hThread);

// Read response.
char temp[1000];
DWORD count;
result = ReadFile(pipe_read, temp, 1000, &count, NULL);
CloseHandle(pipe_read);
if (!result)
return "Error: could not read response";
return std::string(temp, count);
#else
for (int i = 0; i < 4; ++i)
plugin_path = plugin_path.substr(0, plugin_path.rfind("/"));

return shell("/bin/sh '" + plugin_path + "/adb_command.sh' " + command);
std::string adb_command = "/bin/sh '" + plugin_path + "/mac/adb_command.sh' " + command;

FILE * pPipe;
char buffer[2048];
if (!(pPipe = popen(adb_command.c_str(), "r")))
return "Error: failed starting server";

std::string response = "";
while(fgets(buffer, sizeof(buffer), pPipe) != NULL)
response += buffer;
pclose(pPipe);
return response;
#endif
}

ADBPluginPtr ADBPluginAPI::getPlugin()
Expand Down
3 changes: 0 additions & 3 deletions ADBPluginAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class ADBPluginAPI : public FB::JSAPIAuto
ADBPluginAPI(const ADBPluginPtr& plugin, const FB::BrowserHostPtr& host) :
m_plugin(plugin), m_host(host)
{
registerMethod("isServerRunning", make_method(this, &ADBPluginAPI::isServerRunning));
registerMethod("startServer", make_method(this, &ADBPluginAPI::startServer));
registerMethod("killServer", make_method(this, &ADBPluginAPI::killServer));
registerMethod("devices", make_method(this, &ADBPluginAPI::devices));
Expand All @@ -28,13 +27,11 @@ class ADBPluginAPI : public FB::JSAPIAuto

ADBPluginPtr getPlugin();

FB::variant isServerRunning();
FB::variant startServer();
FB::variant killServer();
FB::variant devices();

private:
std::string shell(const std::string& command);
std::string adb(const std::string& command);

ADBPluginWeakPtr m_plugin;
Expand Down
16 changes: 12 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# Written to work with cmake 2.6
cmake_minimum_required (VERSION 2.6)
set (CMAKE_BACKWARDS_COMPATIBILITY 2.6)
set (CMAKE_BUILD_TYPE "Release")
set (CMAKE_CONFIGURATION_TYPES "Release")
set (CMAKE_OSX_ARCHITECTURES "i386")

Project(${PLUGIN_NAME})

Expand All @@ -18,6 +21,12 @@ file (GLOB GENERAL RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}

include_directories(${PLUGIN_INCLUDE_DIRS})

if (WIN32)
add_definitions("-DOS_WIN")
else (WIN32)
add_definitions("-DOS_POSIX")
endif (WIN32)

# Generated files are stored in ${GENERATED} by the project configuration
SET_SOURCE_FILES_PROPERTIES(
${GENERATED}
Expand All @@ -39,13 +48,13 @@ SET( SOURCES
include_platform()


function (chrome_package PROJNAME CRX_OUTDIR DLLFILE KEYFILE CRX_PROJDEP)
function (chrome_package PROJNAME CRX_OUTDIR KEYFILE CRX_PROJDEP)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/gen/${FBSTRING_PluginFileName}-crx)
add_custom_command(
TARGET ${PROJNAME}${FB_CRX_SUFFIX}
POST_BUILD
COMMAND ${CMD_CP} -r ${CMAKE_CURRENT_SOURCE_DIR}/Chrome/chromepackage/* ${CMAKE_CURRENT_BINARY_DIR}/gen/${FBSTRING_PluginFileName}-crx
COMMAND ${CMD_CP} -r ${CMAKE_CURRENT_BINARY_DIR}/Debug/ADB_x86_64.plugin ${CMAKE_CURRENT_BINARY_DIR}/gen/${FBSTRING_PluginFileName}-crx
COMMAND ${CMD_CP} -r ${CMAKE_CURRENT_BINARY_DIR}/Release/npADBPlugin.plugin ${CMAKE_CURRENT_BINARY_DIR}/gen/${FBSTRING_PluginFileName}-crx
)
configure_template(${CMAKE_CURRENT_SOURCE_DIR}/Chrome/chromepackage/manifest.json ${CMAKE_CURRENT_BINARY_DIR}/gen/${FBSTRING_PluginFileName}-crx/manifest.json)

Expand All @@ -59,7 +68,6 @@ function (chrome_package PROJNAME CRX_OUTDIR DLLFILE KEYFILE CRX_PROJDEP)
endfunction(chrome_package)

chrome_package(${PLUGIN_NAME}
${FB_BIN_DIR}/${PLUGIN_NAME}/${CMAKE_CFG_INTDIR}/
"${FB_BIN_DIR}/${PLUGIN_NAME}/${CMAKE_CFG_INTDIR}/${FBSTRING_PluginFileName}.dll"
${FB_BIN_DIR}/${PLUGIN_NAME}/${CMAKE_CFG_INTDIR}/
"${CMAKE_CURRENT_SOURCE_DIR}/sign/dummy_key.pem"
${PROJECT_NAME})
24 changes: 11 additions & 13 deletions Chrome/chromepackage/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ var plugin = document.getElementById('adb-plugin');
chrome.browserAction.setBadgeBackgroundColor({color: '#070'});

function update(oneTime) {
if (plugin.isServerRunning()) {
var devicesMessage = plugin.devices();
var serverIsRunning = devicesMessage.substr(0, 4) === "OKAY";
if (serverIsRunning) {
var devices = devicesMessage.substr(8).split("\n");
chrome.browserAction.setIcon({
path: {
19: 'on_19.png',
38: 'on_38.png'
}
});
var devices = plugin.devices().split('\n');
devices = devices.slice(1); // Trim the header
var count = 0;
for (var i = 0; i < devices.length; ++i) {
if (devices[i] !== '')
count++;
}
if (count) {
chrome.browserAction.setBadgeText({text: String(count)});

var deviceCount = devices.length - 1;
if (deviceCount) {
chrome.browserAction.setBadgeText({text: String(deviceCount)});
} else {
chrome.browserAction.setBadgeText({text: ''});
}
Expand All @@ -38,7 +36,7 @@ function update(oneTime) {
chrome.browserAction.setTitle({title: 'Start ADB'});
}
if (!oneTime)
setTimeout(update, 1000);
setTimeout(update, 3000);
}

update();
Expand All @@ -52,9 +50,9 @@ function stop() {
}

function isServerRunning() {
return plugin.isServerRunning();
return plugin.devices().substr(0, 4) === "OKAY";
}

function devices() {
chrome.tabs.create({url:'chrome://inspect'});
}
}
File renamed without changes.
File renamed without changes.
5 changes: 3 additions & 2 deletions Chrome/chromepackage/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ADB",
"version": "0.9.1",
"version": "0.9.2",
"description": "ADB Plugin",
"manifest_version": 2,

Expand All @@ -16,6 +16,7 @@
"default_popup": "popup.html"
},
"plugins": [
{ "path": "ADB_x86_64.plugin", "public": true }
{ "path": "npADBPlugin.plugin", "public": true },
{ "path": "npADBPlugin.dll", "public": true }
]
}
Binary file added Chrome/chromepackage/win/AdbWinApi.dll
Binary file not shown.
Binary file added Chrome/chromepackage/win/AdbWinUsbApi.dll
Binary file not shown.
Loading

0 comments on commit 65cafdc

Please sign in to comment.