| 90 | } |
| 91 | |
| 92 | int CookieManager::GetCookies(const std::string& url, |
| 93 | std::vector<BrowserCookie>* all_cookies) { |
| 94 | LOG(TRACE) << "Entering CookieManager::GetCookies"; |
| 95 | std::wstring wide_url = StringUtilities::ToWString(url); |
| 96 | CComPtr<IUri> parsed_url; |
| 97 | ::CreateUri(wide_url.c_str(), Uri_CREATE_ALLOW_RELATIVE, 0, &parsed_url); |
| 98 | DWORD url_scheme = 0; |
| 99 | parsed_url->GetScheme(&url_scheme); |
| 100 | bool is_secure_url = URL_SCHEME_HTTPS == url_scheme; |
| 101 | |
| 102 | HookSettings hook_settings; |
| 103 | hook_settings.hook_procedure_name = "CookieWndProc"; |
| 104 | hook_settings.hook_procedure_type = WH_CALLWNDPROC; |
| 105 | hook_settings.window_handle = this->window_handle_; |
| 106 | hook_settings.communication_type = TwoWay; |
| 107 | |
| 108 | HookProcessor hook; |
| 109 | if (!hook.CanSetWindowsHook(this->window_handle_)) { |
| 110 | LOG(WARN) << "Cannot get cookies because driver and browser are not the " |
| 111 | << "same bit-ness."; |
| 112 | return EUNHANDLEDERROR; |
| 113 | } |
| 114 | hook.Initialize(hook_settings); |
| 115 | |
| 116 | bool supports_advanced_api = this->IsAdvancedCookiesApi(); |
| 117 | if (supports_advanced_api) { |
| 118 | // The version of WinINet installed supports the InternetGetCookieEx2 |
| 119 | // API, which gets all cookies (session and persistent) at once. |
| 120 | std::wstring raw_cookie_data = |
| 121 | this->SendGetCookieMessage(wide_url, |
| 122 | WD_GET_ALL_COOKIES, |
| 123 | &hook); |
| 124 | std::string all_cookies_list = StringUtilities::ToString(raw_cookie_data); |
| 125 | std::map<std::string, BrowserCookie> cookies; |
| 126 | this->ParseCookieList(all_cookies_list, |
| 127 | is_secure_url, |
| 128 | &cookies); |
| 129 | std::map<std::string, BrowserCookie>::const_iterator cookie_iterator; |
| 130 | for (cookie_iterator = cookies.begin(); |
| 131 | cookie_iterator != cookies.end(); |
| 132 | ++cookie_iterator) { |
| 133 | all_cookies->push_back(cookie_iterator->second); |
| 134 | } |
| 135 | } else { |
| 136 | // Get all cookies for the current URL visible to JavaScript. |
| 137 | std::wstring scriptable_cookie_string = |
| 138 | this->SendGetCookieMessage(wide_url, |
| 139 | WD_GET_SCRIPTABLE_COOKIES, |
| 140 | &hook); |
| 141 | std::map<std::string, std::string> scriptable_cookies; |
| 142 | this->ParseCookieString(scriptable_cookie_string, &scriptable_cookies); |
| 143 | |
| 144 | // Get all cookies for the insecure version of the current URL, |
| 145 | // which will include HttpOnly cookies. |
| 146 | std::wstring insecure_cookie_string = |
| 147 | this->SendGetCookieMessage(wide_url, |
| 148 | WD_GET_HTTPONLY_COOKIES, |
| 149 | &hook); |
no test coverage detected