| 470 | typedef DWORD(__stdcall *InternetGetCookieEx2Proc)(PCWSTR, PCWSTR, DWORD, INTERNETCOOKIE2**, PDWORD); |
| 471 | |
| 472 | LRESULT CALLBACK CookieWndProc(int nCode, WPARAM wParam, LPARAM lParam) { |
| 473 | CWPSTRUCT* call_window_proc_struct = reinterpret_cast<CWPSTRUCT*>(lParam); |
| 474 | if (WM_COPYDATA == call_window_proc_struct->message) { |
| 475 | COPYDATASTRUCT* data = reinterpret_cast<COPYDATASTRUCT*>(call_window_proc_struct->lParam); |
| 476 | webdriver::HookProcessor::CopyDataToBuffer(data->cbData, data->lpData); |
| 477 | } else if (WD_GET_ALL_COOKIES == call_window_proc_struct->message) { |
| 478 | std::wstring url = webdriver::HookProcessor::CopyWStringFromBuffer(); |
| 479 | int driver_process_id = static_cast<int>(call_window_proc_struct->wParam); |
| 480 | |
| 481 | CComPtr<IUri> uri_pointer; |
| 482 | HRESULT hr = ::CreateUri(url.c_str(), Uri_CREATE_ALLOW_RELATIVE, 0, &uri_pointer); |
| 483 | DWORD scheme = 0; |
| 484 | uri_pointer->GetScheme(&scheme); |
| 485 | CComBSTR scheme_bstr; |
| 486 | uri_pointer->GetSchemeName(&scheme_bstr); |
| 487 | CComBSTR host_bstr; |
| 488 | uri_pointer->GetHost(&host_bstr); |
| 489 | CComBSTR path_bstr; |
| 490 | uri_pointer->GetPath(&path_bstr); |
| 491 | |
| 492 | std::wstring parsed_uri = scheme_bstr; |
| 493 | parsed_uri.append(L"://"); |
| 494 | parsed_uri.append(host_bstr); |
| 495 | parsed_uri.append(path_bstr); |
| 496 | |
| 497 | InternetGetCookieEx2Proc get_cookie_proc = NULL; |
| 498 | InternetFreeCookiesProc free_cookies_proc = NULL; |
| 499 | HMODULE wininet_handle = ::GetModuleHandle(L"wininet"); |
| 500 | if (wininet_handle) { |
| 501 | get_cookie_proc = reinterpret_cast<InternetGetCookieEx2Proc>(::GetProcAddress(wininet_handle, "InternetGetCookieEx2")); |
| 502 | free_cookies_proc = reinterpret_cast<InternetFreeCookiesProc>(::GetProcAddress(wininet_handle, "InternetFreeCookies")); |
| 503 | } |
| 504 | |
| 505 | DWORD cookie_count = 0; |
| 506 | INTERNETCOOKIE2* cookie_pointer = NULL; |
| 507 | DWORD success = 1; |
| 508 | if (get_cookie_proc) { |
| 509 | success = get_cookie_proc(parsed_uri.c_str(), |
| 510 | NULL, |
| 511 | INTERNET_COOKIE_NON_SCRIPT, |
| 512 | &cookie_pointer, |
| 513 | &cookie_count); |
| 514 | } |
| 515 | |
| 516 | if (success == 0) { |
| 517 | // Mimic the format of the old persistent cookie files for ease of |
| 518 | // transmission back to the driver and parsing. |
| 519 | std::wstring all_cookies = L""; |
| 520 | for (DWORD cookie_index = 0; cookie_index < cookie_count; ++cookie_index) { |
| 521 | if (all_cookies.size() > 0) { |
| 522 | all_cookies.append(L"\n*\n"); |
| 523 | } |
| 524 | INTERNETCOOKIE2* current_cookie = cookie_pointer + cookie_index; |
| 525 | std::wstring cookie_name = L""; |
| 526 | if (current_cookie->pwszName) { |
| 527 | // Note that the spec appears to allow "nameless" cookies, |
| 528 | // which clients like Selenium may not support. |
| 529 | cookie_name = current_cookie->pwszName; |