Skip to content

Commit c2ba682

Browse files
rogertaRoger Tawa
andauthored
Add AgentInfo structure to Client (#76)
Co-authored-by: Roger Tawa <[email protected]>
1 parent 74f03de commit c2ba682

File tree

12 files changed

+61
-27
lines changed

12 files changed

+61
-27
lines changed

agent/include/content_analysis/sdk/analysis_agent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace sdk {
4343
// Represents information about one instance of a Google Chrome browser
4444
// process that is connected to the agent.
4545
struct BrowserInfo {
46-
unsigned long pid = 0; // Process of Google Chrome browser process.
46+
unsigned long pid = 0; // Process ID of Google Chrome browser process.
4747
std::string binary_path; // The full path to the process's main binary.
4848
};
4949

agent/include/content_analysis/sdk/result_codes.inc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ RC_RECOVERABLE(ERR_INVALID_REQUEST_FROM_BROWSER, "The browser sent an incorrectl
1212
RC_RECOVERABLE(ERR_IO_PENDING, "IO incomplete, the operation is still pending.")
1313
RC_RECOVERABLE(ERR_MORE_DATA, "There is more data to read before the entire message has been received.")
1414
RC_RECOVERABLE(ERR_CANNOT_GET_BROWSER_PID, "Cannot get process Id of browser.")
15-
RC_RECOVERABLE(ERR_CANNOT_OPEN_BROWSER_PROCESS, "Cannot open browser process to extract info.")
1615
RC_RECOVERABLE(ERR_CANNOT_GET_BROWSER_BINARY_PATH, "Cannot get the full path to the brower's main binary file.")
1716
RC_RECOVERABLE(ERR_BROKEN_PIPE, "Browser process has disconnected.")
1817
RC_RECOVERABLE(ERR_UNEXPECTED, "An internal error has occured.")

agent/src/agent_win.cc

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -336,26 +336,13 @@ ResultCode AgentWin::Connection::BuildBrowserInfo() {
336336
ResultCode::ERR_CANNOT_GET_BROWSER_PID);
337337
}
338338

339-
HANDLE hProc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE,
340-
browser_info_.pid);
341-
if (hProc == nullptr) {
339+
if (!internal::GetProcessPath(browser_info_.pid,
340+
&browser_info_.binary_path)) {
342341
return NotifyIfError("BuildBrowserInfo",
343-
ResultCode::ERR_CANNOT_OPEN_BROWSER_PROCESS);
342+
ResultCode::ERR_CANNOT_GET_BROWSER_BINARY_PATH);
344343
}
345-
346-
auto rc = ResultCode::OK;
347-
char path[MAX_PATH];
348-
DWORD size = sizeof(path);
349-
DWORD length = QueryFullProcessImageNameA(hProc, /*flags=*/0, path, &size);
350-
if (length == 0) {
351-
rc = NotifyIfError("BuildBrowserInfo",
352-
ResultCode::ERR_CANNOT_GET_BROWSER_BINARY_PATH);
353-
}
354-
355-
CloseHandle(hProc);
356344

357-
browser_info_.binary_path = path;
358-
return rc;
345+
return ResultCode::OK;
359346
}
360347

361348
ResultCode AgentWin::Connection::NotifyIfError(

browser/include/content_analysis/sdk/analysis_client.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
namespace content_analysis {
2121
namespace sdk {
2222

23+
// Represents information about one instance of a content analysis agent
24+
// process that is connected to the browser.
25+
struct AgentInfo {
26+
unsigned long pid = 0; // Process ID of content analysis agent process.
27+
std::string binary_path; // The full path to the process's main binary.
28+
};
29+
2330
// Represents a client that can request content analysis for locally running
2431
// agent. This class holds the client endpoint that the browser connects
2532
// with when content analysis is required.
@@ -48,6 +55,9 @@ class Client {
4855
// Returns the configuration parameters used to create the client.
4956
virtual const Config& GetConfig() const = 0;
5057

58+
// Retrives information about the agent that is connected to the browser.
59+
virtual const AgentInfo& GetAgentInfo() const = 0;
60+
5161
// Sends an analysis request to the agent and waits for a response.
5262
virtual int Send(const ContentAnalysisRequest& request,
5363
ContentAnalysisResponse* response) = 0;

browser/src/client_base.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@ const Client::Config& ClientBase::GetConfig() const {
1313
return config_;
1414
}
1515

16+
const AgentInfo& ClientBase::GetAgentInfo() const {
17+
return agent_info_;
18+
}
19+
1620
} // namespace sdk
1721
} // namespace content_analysis

browser/src/client_base.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ class ClientBase : public Client {
1515
public:
1616
// Client:
1717
const Config& GetConfig() const override;
18+
const AgentInfo& GetAgentInfo() const override;
1819

1920
protected:
2021
ClientBase(Config config);
2122

2223
const Config& configuration() const { return config_; }
24+
AgentInfo& agent_info() { return agent_info_; }
2325

2426
private:
2527
Config config_;
28+
AgentInfo agent_info_;
2629
};
2730

2831
} // namespace sdk

browser/src/client_win.cc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,20 @@ ClientWin::ClientWin(Config config, int* rc) : ClientBase(std::move(config)) {
2828

2929
std::string pipename =
3030
internal::GetPipeName(configuration().name, configuration().user_specific);
31-
if (pipename.empty()) {
32-
return;
31+
if (!pipename.empty()) {
32+
unsigned long pid = 0;
33+
std::string binary_path;
34+
if (ConnectToPipe(pipename, &hPipe_) == ERROR_SUCCESS &&
35+
GetNamedPipeServerProcessId(hPipe_, &pid) &&
36+
internal::GetProcessPath(pid, &binary_path)) {
37+
agent_info().pid = pid;
38+
agent_info().binary_path = std::move(binary_path);
39+
*rc = 0;
40+
}
3341
}
3442

35-
pipename_ = pipename;
36-
if (ConnectToPipe(pipename_, &hPipe_) != ERROR_SUCCESS) {
43+
if (*rc != 0) {
3744
Shutdown();
38-
} else {
39-
*rc = 0;
4045
}
4146
}
4247

browser/src/client_win.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class ClientWin : public ClientBase {
3737
// Performs a clean shutdown of the client.
3838
void Shutdown();
3939

40-
std::string pipename_;
4140
HANDLE hPipe_ = INVALID_HANDLE_VALUE;
4241
};
4342

common/utils_win.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ DWORD CreatePipe(
118118
return err;
119119
}
120120

121+
bool GetProcessPath(unsigned long pid, std::string* binary_path) {
122+
HANDLE hProc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
123+
if (hProc == nullptr) {
124+
return false;
125+
}
126+
127+
char path[MAX_PATH];
128+
DWORD size = sizeof(path);
129+
DWORD length = QueryFullProcessImageNameA(hProc, /*flags=*/0, path, &size);
130+
CloseHandle(hProc);
131+
if (length == 0) {
132+
return false;
133+
}
134+
135+
*binary_path = path;
136+
return true;
137+
}
138+
121139
} // internal
122140
} // namespace sdk
123141
} // namespace content_analysis

common/utils_win.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ DWORD CreatePipe(const std::string& name,
4141
bool is_first_pipe,
4242
HANDLE* handle);
4343

44+
// Returns the full path to the main binary file of the process with the given
45+
// process ID.
46+
bool GetProcessPath(unsigned long pid, std::string* binary_path);
47+
4448
} // internal
4549
} // namespace sdk
4650
} // namespace content_analysis

0 commit comments

Comments
 (0)