Skip to content

Commit a33e739

Browse files
authored
Create temporary folder for Edge in IEMode (#10006)
Signed-off-by: jimevans <james.h.evans.jr@gmail.com>
1 parent d9f2bb8 commit a33e739

File tree

4 files changed

+126
-2
lines changed

4 files changed

+126
-2
lines changed

cpp/iedriver/BrowserFactory.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,44 @@ void BrowserFactory::LaunchBrowserUsingCreateProcess(PROCESS_INFORMATION* proc_i
335335
delete[] command_line;
336336
}
337337

338+
bool BrowserFactory::DirectoryExists(std::wstring& dir_name) {
339+
DWORD attribs = ::GetFileAttributes(dir_name.c_str());
340+
if (attribs == INVALID_FILE_ATTRIBUTES) {
341+
return false;
342+
}
343+
return (attribs & FILE_ATTRIBUTE_DIRECTORY);
344+
}
345+
346+
bool BrowserFactory::CreateUniqueTempDir(std::wstring &temp_dir) {
347+
// get temporary folder for the current user
348+
wchar_t temp[128];
349+
::GetTempPath(128, temp);
350+
std::wstring wtemp = temp;
351+
if (!DirectoryExists(wtemp)) {
352+
return false;
353+
}
354+
355+
// create a IEDriver temporary folder inside the user level temporary folder
356+
bool temp_dir_created = false;
357+
for (int i=0; i<10; i++) {
358+
std::wstring output = wtemp + L"IEDriver-" + StringUtilities::CreateGuid();
359+
if (DirectoryExists(output)) {
360+
continue;
361+
}
362+
363+
::CreateDirectory(output.c_str(), NULL);
364+
if (!DirectoryExists(output)) {
365+
continue;
366+
}
367+
368+
temp_dir = output;
369+
temp_dir_created = true;
370+
break;
371+
}
372+
373+
return temp_dir_created;
374+
}
375+
338376
void BrowserFactory::LaunchEdgeInIEMode(PROCESS_INFORMATION* proc_info,
339377
std::string* error_message) {
340378
LOG(TRACE) << "Entering BrowserFactory::LaunchEdgeInIEMode";
@@ -353,6 +391,14 @@ void BrowserFactory::LaunchEdgeInIEMode(PROCESS_INFORMATION* proc_info,
353391
executable_and_url.append(L" --ie-mode-force");
354392
executable_and_url.append(L" --internet-explorer-integration=iemode");
355393

394+
// create a temporary directory for IEDriver test
395+
std::wstring temp_dir;
396+
if (CreateUniqueTempDir(temp_dir)) {
397+
LOG(TRACE) << L"Using temporary folder " << LOGWSTRING(temp_dir) << ".";
398+
executable_and_url.append(L" --user-data-dir=" + temp_dir);
399+
this->edge_user_data_dir_ = temp_dir;
400+
}
401+
356402
executable_and_url.append(L" ");
357403
executable_and_url.append(this->initial_browser_url_);
358404

@@ -1393,4 +1439,55 @@ bool BrowserFactory::IsEdgeMode() const {
13931439
return this->edge_ie_mode_;
13941440
}
13951441

1442+
// delete a folder recursively
1443+
int BrowserFactory::DeleteDirectory(const std::wstring &dir_name) {
1444+
WIN32_FIND_DATA file_info;
1445+
1446+
std::wstring file_pattern = dir_name + L"\\*.*";
1447+
HANDLE file_handle = ::FindFirstFile(file_pattern.c_str(), &file_info);
1448+
if (file_handle != INVALID_HANDLE_VALUE) {
1449+
do {
1450+
if (file_info.cFileName[0] == '.') {
1451+
continue;
1452+
}
1453+
std::wstring file_path = dir_name + L"\\" + file_info.cFileName;
1454+
1455+
if (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1456+
int return_value = DeleteDirectory(file_path);
1457+
if (return_value) {
1458+
return return_value;
1459+
}
1460+
} else {
1461+
if (::SetFileAttributes(file_path.c_str(), FILE_ATTRIBUTE_NORMAL) == FALSE) {
1462+
return ::GetLastError();
1463+
}
1464+
1465+
if (::DeleteFile(file_path.c_str()) == FALSE) {
1466+
return ::GetLastError();
1467+
}
1468+
}
1469+
} while (::FindNextFile(file_handle, &file_info) == TRUE);
1470+
1471+
::FindClose(file_handle);
1472+
DWORD dwError = ::GetLastError();
1473+
if (dwError != ERROR_NO_MORE_FILES) {
1474+
return dwError;
1475+
}
1476+
1477+
if (::SetFileAttributes(dir_name.c_str(), FILE_ATTRIBUTE_NORMAL) == FALSE) {
1478+
return ::GetLastError();
1479+
}
1480+
1481+
if (::RemoveDirectory(dir_name.c_str()) == FALSE) {
1482+
return ::GetLastError();
1483+
}
1484+
}
1485+
1486+
return 0;
1487+
}
1488+
1489+
std::wstring BrowserFactory::GetEdgeTempDir() {
1490+
return this->edge_user_data_dir_;
1491+
}
1492+
13961493
} // namespace webdriver

cpp/iedriver/BrowserFactory.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,20 @@ class BrowserFactory {
7474
static BOOL CALLBACK FindEdgeBrowserHandles(HWND hwnd, LPARAM arg);
7575

7676
static bool IsWindowsVistaOrGreater(void);
77+
static int DeleteDirectory(const std::wstring &dir_name);
7778

7879
bool IsEdgeMode(void) const;
80+
std::wstring GetEdgeTempDir(void);
7981
private:
8082
static BOOL CALLBACK FindBrowserWindow(HWND hwnd, LPARAM param);
81-
82-
8383
static BOOL CALLBACK FindEdgeWindow(HWND hwnd, LPARAM param);
8484
static bool IsWindowsVersionOrGreater(unsigned short major_version,
8585
unsigned short minor_version,
8686
unsigned short service_pack);
8787

88+
static bool DirectoryExists(std::wstring& dir_name);
89+
static bool CreateUniqueTempDir(std::wstring &temp_dir);
90+
8891
UINT html_getobject_msg_;
8992
HINSTANCE oleacc_instance_handle_;
9093

@@ -131,6 +134,7 @@ class BrowserFactory {
131134

132135
bool edge_ie_mode_;
133136
std::wstring edge_executable_location_;
137+
std::wstring edge_user_data_dir_;
134138
};
135139

136140
} // namespace webdriver

cpp/iedriver/IECommandExecutor.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ LRESULT IECommandExecutor::OnCreate(UINT uMsg,
119119
this->command_handlers_ = new CommandHandlerRepository();
120120

121121
this->is_edge_chromium_ = false;
122+
this->edge_temp_dir_ = L"";
122123

123124
return 0;
124125
}
@@ -465,6 +466,26 @@ LRESULT IECommandExecutor::OnBrowserQuit(UINT uMsg,
465466
} else {
466467
LOG(WARN) << "Unable to find browser to quit with ID " << browser_id;
467468
}
469+
470+
// Delete IEDriver temporary folder when IEDriver drvies Edge in IEMode.
471+
// Note that the this->factory_ object might have been deleted.
472+
if (this->edge_temp_dir_ != L"") {
473+
for (int i=0;i<100;i++) {
474+
::Sleep(100); // wait for the Edge browser completing read/write work
475+
// the delete usually completes in 1 retries
476+
if (BrowserFactory::DeleteDirectory(edge_temp_dir_)) {
477+
// directory delete failed when some files/folders are locked
478+
LOG(TRACE) << "Failed to delete Edge temporary user data directory " << LOGWSTRING(edge_temp_dir_)
479+
<< ", retrying " << i+1 << "...";
480+
} else {
481+
// the temporary folder has been deleted
482+
LOG(TRACE) << "Deleted Edge temporary user data directory " << LOGWSTRING(edge_temp_dir_) << ".";
483+
break;
484+
}
485+
}
486+
this->edge_temp_dir_ = L"";
487+
}
488+
468489
return 0;
469490
}
470491

@@ -1430,6 +1451,7 @@ int IECommandExecutor::CreateNewBrowser(std::string* error_message) {
14301451
LOG(WARN) << "Browser was launched and attached to, but is still busy.";
14311452
}
14321453
wrapper->SetFocusToBrowser();
1454+
this->edge_temp_dir_ = this->factory_->GetEdgeTempDir();
14331455
return WD_SUCCESS;
14341456
}
14351457

cpp/iedriver/IECommandExecutor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ class IECommandExecutor : public CWindowImpl<IECommandExecutor>, public IElement
289289
bool use_strict_file_interactability_;
290290
bool is_edge_chromium_;
291291
std::string edge_executable_path_;
292+
std::wstring edge_temp_dir_;
292293

293294
Command current_command_;
294295
std::string serialized_response_;

0 commit comments

Comments
 (0)