[email protected] | 9a18283 | 2012-02-10 18:45:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_FRAME_UTILS_H_ |
| 6 | #define CHROME_FRAME_UTILS_H_ |
| 7 | |
[email protected] | b57f0323 | 2010-10-25 14:14:39 | [diff] [blame] | 8 | #include <OAidl.h> |
[email protected] | f4fbae68 | 2011-02-28 22:18:33 | [diff] [blame] | 9 | #include <objidl.h> |
[email protected] | b57f0323 | 2010-10-25 14:14:39 | [diff] [blame] | 10 | #include <windows.h> |
[email protected] | 80b5a8d | 2010-03-19 16:50:43 | [diff] [blame] | 11 | #include <wininet.h> |
[email protected] | 7ae8074 | 2010-03-25 22:02:27 | [diff] [blame] | 12 | #include <string> |
[email protected] | b57f0323 | 2010-10-25 14:14:39 | [diff] [blame] | 13 | #include <vector> |
[email protected] | 7ae8074 | 2010-03-25 22:02:27 | [diff] [blame] | 14 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 15 | #include "base/basictypes.h" |
[email protected] | 3f55e87 | 2009-10-17 04:48:37 | [diff] [blame] | 16 | #include "base/logging.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 17 | #include "base/metrics/histogram.h" |
[email protected] | 57319e1 | 2012-03-30 22:52:34 | [diff] [blame] | 18 | #include "base/string16.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 19 | #include "base/threading/thread.h" |
[email protected] | 0152b51 | 2010-12-21 00:52:03 | [diff] [blame] | 20 | #include "base/win/scoped_comptr.h" |
[email protected] | e33f99a1 | 2010-04-13 01:27:22 | [diff] [blame] | 21 | #include "googleurl/src/gurl.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 22 | #include "ui/gfx/rect.h" |
[email protected] | e33f99a1 | 2010-04-13 01:27:22 | [diff] [blame] | 23 | |
[email protected] | 864b136 | 2010-08-19 03:49:38 | [diff] [blame] | 24 | class FilePath; |
[email protected] | 57319e1 | 2012-03-30 22:52:34 | [diff] [blame] | 25 | class RegistryListPreferencesHolder; |
[email protected] | b57f0323 | 2010-10-25 14:14:39 | [diff] [blame] | 26 | interface IBrowserService; |
[email protected] | a17930d8 | 2011-02-11 23:12:35 | [diff] [blame] | 27 | interface IWebBrowser2; |
[email protected] | b516e2d | 2011-07-12 16:54:12 | [diff] [blame] | 28 | struct ContextMenuModel; |
[email protected] | 864b136 | 2010-08-19 03:49:38 | [diff] [blame] | 29 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 30 | // utils.h : Various utility functions and classes |
[email protected] | d266ce8 | 2010-08-13 21:33:40 | [diff] [blame] | 31 | extern const char kGCFProtocol[]; |
[email protected] | 57319e1 | 2012-03-30 22:52:34 | [diff] [blame] | 32 | |
[email protected] | d266ce8 | 2010-08-13 21:33:40 | [diff] [blame] | 33 | extern const wchar_t kAllowUnsafeURLs[]; |
[email protected] | 57319e1 | 2012-03-30 22:52:34 | [diff] [blame] | 34 | extern const wchar_t kChromeContentPrefix[]; |
| 35 | extern const wchar_t kChromeFrameAccessibleMode[]; |
[email protected] | c4e45b3 | 2010-07-28 21:15:15 | [diff] [blame] | 36 | extern const wchar_t kChromeFrameAttachTabPattern[]; |
[email protected] | e67a73f4 | 2010-08-31 15:05:02 | [diff] [blame] | 37 | extern const wchar_t kChromeFrameConfigKey[]; |
[email protected] | 57319e1 | 2012-03-30 22:52:34 | [diff] [blame] | 38 | extern const wchar_t kChromeFrameHeadlessMode[]; |
| 39 | extern const wchar_t kChromeFrameUnpinnedMode[]; |
| 40 | extern const wchar_t kChromeMimeType[]; |
| 41 | extern const wchar_t kChromeProtocolPrefix[]; |
| 42 | extern const wchar_t kEnableBuggyBhoIntercept[]; |
| 43 | extern const wchar_t kEnableGCFRendererByDefault[]; |
| 44 | extern const wchar_t kExcludeUAFromDomainList[]; |
| 45 | extern const wchar_t kIexploreProfileName[]; |
[email protected] | e67a73f4 | 2010-08-31 15:05:02 | [diff] [blame] | 46 | extern const wchar_t kRenderInGCFUrlList[]; |
| 47 | extern const wchar_t kRenderInHostUrlList[]; |
[email protected] | e67a73f4 | 2010-08-31 15:05:02 | [diff] [blame] | 48 | extern const wchar_t kRundllProfileName[]; |
[email protected] | b0938a4 | 2011-10-05 20:59:38 | [diff] [blame] | 49 | extern const wchar_t kUseBackgroundThreadForSubResources[]; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 50 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 51 | // This function is very similar to the AtlRegisterTypeLib function except |
| 52 | // that it takes a parameter that specifies whether to register the typelib |
| 53 | // for the current user only or on a machine-wide basis |
| 54 | // Refer to the MSDN documentation for AtlRegisterTypeLib for a description of |
| 55 | // the arguments |
| 56 | HRESULT UtilRegisterTypeLib(HINSTANCE tlb_instance, |
| 57 | LPCOLESTR index, |
| 58 | bool for_current_user_only); |
| 59 | |
| 60 | // This function is very similar to the AtlUnRegisterTypeLib function except |
| 61 | // that it takes a parameter that specifies whether to unregister the typelib |
| 62 | // for the current user only or on a machine-wide basis |
| 63 | // Refer to the MSDN documentation for AtlUnRegisterTypeLib for a description |
| 64 | // of the arguments |
| 65 | HRESULT UtilUnRegisterTypeLib(HINSTANCE tlb_instance, |
| 66 | LPCOLESTR index, |
| 67 | bool for_current_user_only); |
| 68 | |
| 69 | HRESULT UtilRegisterTypeLib(LPCWSTR typelib_path, bool for_current_user_only); |
| 70 | |
| 71 | HRESULT UtilUnRegisterTypeLib(LPCWSTR typelib_path, bool for_current_user_only); |
| 72 | |
| 73 | HRESULT UtilRegisterTypeLib(ITypeLib* typelib, |
| 74 | LPCWSTR typelib_path, |
| 75 | LPCWSTR help_dir, |
| 76 | bool for_current_user_only); |
| 77 | |
| 78 | HRESULT UtilUnRegisterTypeLib(ITypeLib* typelib, |
| 79 | bool for_current_user_only); |
| 80 | |
[email protected] | 99d81e5 | 2011-06-29 02:29:53 | [diff] [blame] | 81 | // Clears a marker that causes legacy NPAPI registration to persist across |
| 82 | // updates. Returns false if the marker could not be removed. |
| 83 | bool UtilRemovePersistentNPAPIMarker(); |
[email protected] | c41468f | 2009-11-11 20:39:18 | [diff] [blame] | 84 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 85 | // Given an HTML fragment, this function looks for the |
| 86 | // <meta http-equiv="X-UA-Compatible"> tag and extracts the value of the |
| 87 | // "content" attribute |
| 88 | // This method will currently return a false positive if the tag appears |
| 89 | // inside a string in a <SCRIPT> block. |
| 90 | HRESULT UtilGetXUACompatContentValue(const std::wstring& html_string, |
| 91 | std::wstring* content_value); |
| 92 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 93 | // Returns a string from ChromeFrame's string table by resource. Must be |
| 94 | // provided with a valid resource id. |
| 95 | std::wstring GetResourceString(int resource_id); |
| 96 | |
| 97 | // Displays a message box indicating that there was a version mismatch between |
| 98 | // ChromeFrame and the running instance of Chrome. |
| 99 | // server_version is the version of the running instance of Chrome. |
| 100 | void DisplayVersionMismatchWarning(HWND parent, |
| 101 | const std::string& server_version); |
| 102 | |
| 103 | // This class provides a base implementation for ATL modules which want to |
| 104 | // perform all their registration under HKCU. This class overrides the |
| 105 | // RegisterServer and UnregisterServer methods and registers the type libraries |
[email protected] | e67a73f4 | 2010-08-31 15:05:02 | [diff] [blame] | 106 | // under HKCU (the rest of the registration is made under HKCU by changing the |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 107 | // appropriate .RGS files) |
| 108 | template < class BaseAtlModule > |
| 109 | class AtlPerUserModule : public BaseAtlModule { |
[email protected] | e67a73f4 | 2010-08-31 15:05:02 | [diff] [blame] | 110 | public: |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 111 | HRESULT RegisterServer(BOOL reg_typelib = FALSE, |
| 112 | const CLSID* clsid = NULL) throw() { |
| 113 | HRESULT hr = BaseAtlModule::RegisterServer(FALSE, clsid); |
| 114 | if (FAILED(hr)) { |
| 115 | return hr; |
| 116 | } |
| 117 | if (reg_typelib) { |
| 118 | hr = UtilRegisterTypeLib(_AtlComModule.m_hInstTypeLib, NULL, false); |
| 119 | } |
| 120 | return hr; |
| 121 | } |
| 122 | |
| 123 | HRESULT UnregisterServer(BOOL unreg_typelib, |
| 124 | const CLSID* clsid = NULL) throw() { |
| 125 | HRESULT hr = BaseAtlModule::UnregisterServer(FALSE, clsid); |
| 126 | if (FAILED(hr)) { |
| 127 | return hr; |
| 128 | } |
| 129 | if (unreg_typelib) { |
| 130 | hr = UtilUnRegisterTypeLib(_AtlComModule.m_hInstTypeLib, NULL, false); |
| 131 | } |
| 132 | return hr; |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | // Creates a javascript statement for execution from the function name and |
| 137 | // arguments passed in. |
| 138 | std::string CreateJavascript(const std::string& function_name, |
| 139 | const std::string args); |
| 140 | |
| 141 | // Use to prevent the DLL from being unloaded while there are still living |
| 142 | // objects with outstanding references. |
| 143 | class AddRefModule { |
| 144 | public: |
| 145 | AddRefModule(); |
| 146 | ~AddRefModule(); |
| 147 | }; |
| 148 | |
| 149 | // Retrieves the executable name of the process hosting us. If |
| 150 | // |include_extension| is false, then we strip the extension from the name. |
| 151 | std::wstring GetHostProcessName(bool include_extension); |
| 152 | |
| 153 | typedef enum BrowserType { |
| 154 | BROWSER_INVALID = -1, |
| 155 | BROWSER_UNKNOWN, |
| 156 | BROWSER_IE, |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | BrowserType GetBrowserType(); |
| 160 | |
| 161 | typedef enum IEVersion { |
| 162 | IE_INVALID, |
| 163 | NON_IE, |
| 164 | IE_UNSUPPORTED, |
| 165 | IE_6, |
| 166 | IE_7, |
| 167 | IE_8, |
[email protected] | d8e1351 | 2010-09-22 17:02:58 | [diff] [blame] | 168 | IE_9, |
[email protected] | 5ba4b8a9 | 2011-09-29 05:09:42 | [diff] [blame] | 169 | IE_10, |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 170 | }; |
| 171 | |
[email protected] | d8e1351 | 2010-09-22 17:02:58 | [diff] [blame] | 172 | // The renderer to be used for a page. Values for Chrome also convey the |
| 173 | // reason why Chrome is used. |
| 174 | enum RendererType { |
| 175 | RENDERER_TYPE_UNDETERMINED = 0, |
| 176 | RENDERER_TYPE_CHROME_MIN, |
| 177 | // NOTE: group all _CHROME_ values together below here, as they are used for |
| 178 | // generating metrics reported via UMA (adjust MIN/MAX as needed). |
| 179 | RENDERER_TYPE_CHROME_GCF_PROTOCOL = RENDERER_TYPE_CHROME_MIN, |
| 180 | RENDERER_TYPE_CHROME_HTTP_EQUIV, |
| 181 | RENDERER_TYPE_CHROME_RESPONSE_HEADER, |
| 182 | RENDERER_TYPE_CHROME_DEFAULT_RENDERER, |
| 183 | RENDERER_TYPE_CHROME_OPT_IN_URL, |
| 184 | RENDERER_TYPE_CHROME_WIDGET, |
| 185 | // NOTE: all _CHOME_ values must go above here (adjust MIN/MAX as needed). |
| 186 | RENDERER_TYPE_CHROME_MAX = RENDERER_TYPE_CHROME_WIDGET, |
| 187 | RENDERER_TYPE_OTHER, |
| 188 | }; |
| 189 | |
| 190 | // Returns true if the given RendererType represents Chrome. |
| 191 | bool IsChrome(RendererType renderer_type); |
| 192 | |
| 193 | // Convenience macro for logging a sample for the launch type metric. |
[email protected] | 92c79d3 | 2011-04-06 18:41:15 | [diff] [blame] | 194 | #define UMA_LAUNCH_TYPE_COUNT(sample) \ |
| 195 | UMA_HISTOGRAM_CUSTOM_COUNTS("ChromeFrame.LaunchType", sample, \ |
[email protected] | d8e1351 | 2010-09-22 17:02:58 | [diff] [blame] | 196 | RENDERER_TYPE_CHROME_MIN, RENDERER_TYPE_CHROME_MAX, \ |
| 197 | RENDERER_TYPE_CHROME_MAX + 1 - RENDERER_TYPE_CHROME_MIN) |
| 198 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 199 | // To get the IE version when Chrome Frame is hosted in IE. Make sure that |
| 200 | // the hosting browser is IE before calling this function, otherwise NON_IE |
| 201 | // will be returned. |
[email protected] | b57f0323 | 2010-10-25 14:14:39 | [diff] [blame] | 202 | // |
| 203 | // Versions newer than the newest supported version are reported as the newest |
| 204 | // supported version. |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 205 | IEVersion GetIEVersion(); |
| 206 | |
[email protected] | b57f0323 | 2010-10-25 14:14:39 | [diff] [blame] | 207 | // Returns the actual major version of the IE in which the current process is |
| 208 | // hosted. Returns 0 if the current process is not IE or any other error occurs. |
| 209 | uint32 GetIEMajorVersion(); |
| 210 | |
[email protected] | bc73b4e5 | 2010-03-26 04:16:20 | [diff] [blame] | 211 | FilePath GetIETemporaryFilesFolder(); |
| 212 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 213 | // Retrieves the file version from a module handle without extra round trips |
| 214 | // to the disk (as happens with the regular GetFileVersionInfo API). |
| 215 | // |
| 216 | // @param module A handle to the module for which to retrieve the version info. |
[email protected] | b57f0323 | 2010-10-25 14:14:39 | [diff] [blame] | 217 | // @param high On successful return holds the most significant part of the file |
| 218 | // version. Must be non-null. |
| 219 | // @param low On successful return holds the least significant part of the file |
| 220 | // version. May be NULL. |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 221 | // @returns true if the version info was successfully retrieved. |
| 222 | bool GetModuleVersion(HMODULE module, uint32* high, uint32* low); |
| 223 | |
| 224 | // Return if the IEXPLORE is in private mode. The IEIsInPrivateBrowsing() checks |
| 225 | // whether current process is IEXPLORE. |
| 226 | bool IsIEInPrivate(); |
| 227 | |
[email protected] | 35f13ab | 2009-12-16 23:59:17 | [diff] [blame] | 228 | // Calls [ieframe|shdocvw]!DoFileDownload to initiate a download. |
| 229 | HRESULT DoFileDownloadInIE(const wchar_t* url); |
| 230 | |
[email protected] | b516e2d | 2011-07-12 16:54:12 | [diff] [blame] | 231 | // Construct a menu from the model sent from Chrome. |
| 232 | HMENU BuildContextMenu(const ContextMenuModel& menu_model); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 233 | |
| 234 | // Uses GURL internally to append 'relative' to 'document' |
| 235 | std::string ResolveURL(const std::string& document, |
| 236 | const std::string& relative); |
| 237 | |
| 238 | // Returns true iff the two urls have the same scheme, same host and same port. |
| 239 | bool HaveSameOrigin(const std::string& url1, const std::string& url2); |
| 240 | |
| 241 | // Get a boolean configuration value from registry. |
| 242 | bool GetConfigBool(bool default_value, const wchar_t* value_name); |
| 243 | |
| 244 | // Gets an integer configuration value from the registry. |
| 245 | int GetConfigInt(int default_value, const wchar_t* value_name); |
| 246 | |
[email protected] | 7bc272f | 2009-12-09 01:09:28 | [diff] [blame] | 247 | // Sets an integer configuration value in the registry. |
| 248 | bool SetConfigInt(const wchar_t* value_name, int value); |
| 249 | |
| 250 | // Sets a boolean integer configuration value in the registry. |
| 251 | bool SetConfigBool(const wchar_t* value_name, bool value); |
| 252 | |
| 253 | // Deletes the configuration value passed in. |
| 254 | bool DeleteConfigValue(const wchar_t* value_name); |
| 255 | |
| 256 | // Returns true if we are running in headless mode in which case we need to |
| 257 | // gather crash dumps, etc to send them to the crash server. |
| 258 | bool IsHeadlessMode(); |
| 259 | |
[email protected] | 67700a9 | 2010-09-18 07:43:58 | [diff] [blame] | 260 | // Returns true if we are running in accessible mode in which we need to enable |
| 261 | // renderer accessibility for use in automation. |
| 262 | bool IsAccessibleMode(); |
| 263 | |
[email protected] | 11903b0d | 2010-03-26 23:54:34 | [diff] [blame] | 264 | // Returns true if we are running in unpinned mode in which case DLL |
| 265 | // eviction should be possible. |
| 266 | bool IsUnpinnedMode(); |
| 267 | |
[email protected] | e67a73f4 | 2010-08-31 15:05:02 | [diff] [blame] | 268 | // Returns true if all HTML pages should be rendered in GCF by default. |
| 269 | bool IsGcfDefaultRenderer(); |
| 270 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 271 | // Check if this url is opting into Chrome Frame based on static settings. |
[email protected] | d8e1351 | 2010-09-22 17:02:58 | [diff] [blame] | 272 | // Returns one of: |
| 273 | // - RENDERER_TYPE_UNDETERMINED if not opt-in or if explicit opt-out |
| 274 | // - RENDERER_TYPE_CHROME_DEFAULT_RENDERER |
| 275 | // - RENDERER_TYPE_CHROME_OPT_IN_URL |
| 276 | RendererType RendererTypeForUrl(const std::wstring& url); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 277 | |
[email protected] | 57319e1 | 2012-03-30 22:52:34 | [diff] [blame] | 278 | // Check if we should try to remove the CF user agent based on registry |
| 279 | // settings. |
| 280 | bool ShouldRemoveUAForUrl(const string16& url); |
| 281 | |
| 282 | // Testing methods that return the backing stores behind RendererTypeForUrl and |
| 283 | // ShouldRemoveUAForUrl. Intended to allow unit testing code that calls the |
| 284 | // above methods. |
| 285 | // TODO(robertshield): All of the FooForUrl code should be removed from here |
| 286 | // and further refactored. |
| 287 | RegistryListPreferencesHolder& GetRendererTypePreferencesHolderForTesting(); |
| 288 | RegistryListPreferencesHolder& GetUserAgentPreferencesHolderForTesting(); |
| 289 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 290 | // A shortcut for QueryService |
| 291 | template <typename T> |
[email protected] | 3f55e87 | 2009-10-17 04:48:37 | [diff] [blame] | 292 | HRESULT DoQueryService(const IID& service_id, IUnknown* unk, T** service) { |
| 293 | DCHECK(service); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 294 | if (!unk) |
| 295 | return E_INVALIDARG; |
[email protected] | 2b3102be | 2010-04-21 20:07:35 | [diff] [blame] | 296 | |
[email protected] | 0152b51 | 2010-12-21 00:52:03 | [diff] [blame] | 297 | base::win::ScopedComPtr<IServiceProvider> service_provider; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 298 | HRESULT hr = service_provider.QueryFrom(unk); |
[email protected] | 2b3102be | 2010-04-21 20:07:35 | [diff] [blame] | 299 | if (service_provider) |
| 300 | hr = service_provider->QueryService(service_id, service); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 301 | |
[email protected] | 2b3102be | 2010-04-21 20:07:35 | [diff] [blame] | 302 | DCHECK(FAILED(hr) || *service); |
[email protected] | 172acc4 | 2009-12-28 22:10:17 | [diff] [blame] | 303 | return hr; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 304 | } |
| 305 | |
[email protected] | b22ef28 | 2009-12-11 19:31:56 | [diff] [blame] | 306 | // Navigates an IWebBrowser2 object to a moniker. |
[email protected] | a542e82 | 2010-01-16 00:30:04 | [diff] [blame] | 307 | // |headers| can be NULL. |
[email protected] | 4736610c | 2011-03-04 06:43:35 | [diff] [blame] | 308 | HRESULT NavigateBrowserToMoniker(IUnknown* browser, IMoniker* moniker, |
| 309 | const wchar_t* headers, IBindCtx* bind_ctx, |
[email protected] | 93b79279 | 2011-06-07 23:00:01 | [diff] [blame] | 310 | const wchar_t* fragment, IStream* post_data, |
| 311 | VARIANT* flags); |
[email protected] | b22ef28 | 2009-12-11 19:31:56 | [diff] [blame] | 312 | |
| 313 | // Raises a flag on the current thread (using TLS) to indicate that an |
| 314 | // in-progress navigation should be rendered in chrome frame. |
| 315 | void MarkBrowserOnThreadForCFNavigation(IBrowserService* browser); |
| 316 | |
| 317 | // Checks if this browser instance has been marked as currently navigating |
| 318 | // to a CF document. If clear_flag is set to true, the tls flag is cleared but |
| 319 | // only if the browser has been marked. |
| 320 | bool CheckForCFNavigation(IBrowserService* browser, bool clear_flag); |
| 321 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 322 | // Returns true if the URL passed in is something which can be handled by |
| 323 | // Chrome. If this function returns false then we should fail the navigation. |
[email protected] | 10bf1a7 | 2009-10-01 16:00:21 | [diff] [blame] | 324 | // When is_privileged is true, chrome extension URLs will be considered valid. |
[email protected] | d266ce8 | 2010-08-13 21:33:40 | [diff] [blame] | 325 | bool IsValidUrlScheme(const GURL& url, bool is_privileged); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 326 | |
[email protected] | b55aaa6 | 2009-12-03 00:21:50 | [diff] [blame] | 327 | // Returns the raw http headers for the current request given an |
| 328 | // IWinInetHttpInfo pointer. |
| 329 | std::string GetRawHttpHeaders(IWinInetHttpInfo* info); |
| 330 | |
[email protected] | 723f2992 | 2009-12-04 20:42:33 | [diff] [blame] | 331 | // Can be used to determine whether a given request is being performed for |
| 332 | // a sub-frame or iframe in Internet Explorer. This can be called |
| 333 | // from various places, notably in request callbacks and the like. |
| 334 | // |
| 335 | // |service_provider| must not be NULL and should be a pointer to something |
| 336 | // that implements IServiceProvider (if it isn't this method returns false). |
| 337 | // |
| 338 | // Returns true if this method can determine with some certainty that the |
| 339 | // request did NOT originate from a top level frame, returns false otherwise. |
| 340 | bool IsSubFrameRequest(IUnknown* service_provider); |
| 341 | |
[email protected] | 3f55e87 | 2009-10-17 04:48:37 | [diff] [blame] | 342 | // See COM_INTERFACE_BLIND_DELEGATE below for details. |
| 343 | template <class T> |
| 344 | STDMETHODIMP CheckOutgoingInterface(void* obj, REFIID iid, void** ret, |
| 345 | DWORD cookie) { |
| 346 | T* instance = reinterpret_cast<T*>(obj); |
| 347 | HRESULT hr = E_NOINTERFACE; |
| 348 | IUnknown* delegate = instance ? instance->delegate() : NULL; |
| 349 | if (delegate) { |
| 350 | hr = delegate->QueryInterface(iid, ret); |
| 351 | #if !defined(NDEBUG) |
| 352 | if (SUCCEEDED(hr)) { |
| 353 | wchar_t iid_string[64] = {0}; |
| 354 | StringFromGUID2(iid, iid_string, arraysize(iid_string)); |
[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 355 | DVLOG(1) << __FUNCTION__ << " Giving out wrapped interface: " |
| 356 | << iid_string; |
[email protected] | 3f55e87 | 2009-10-17 04:48:37 | [diff] [blame] | 357 | } |
| 358 | #endif |
| 359 | } |
| 360 | |
| 361 | return hr; |
| 362 | } |
| 363 | |
| 364 | // See COM_INTERFACE_ENTRY_IF_DELEGATE_SUPPORTS below for details. |
| 365 | template <class T> |
| 366 | STDMETHODIMP QueryInterfaceIfDelegateSupports(void* obj, REFIID iid, |
| 367 | void** ret, DWORD cookie) { |
| 368 | HRESULT hr = E_NOINTERFACE; |
| 369 | T* instance = reinterpret_cast<T*>(obj); |
| 370 | IUnknown* delegate = instance ? instance->delegate() : NULL; |
| 371 | if (delegate) { |
[email protected] | 0152b51 | 2010-12-21 00:52:03 | [diff] [blame] | 372 | base::win::ScopedComPtr<IUnknown> original; |
[email protected] | 3f55e87 | 2009-10-17 04:48:37 | [diff] [blame] | 373 | hr = delegate->QueryInterface(iid, |
| 374 | reinterpret_cast<void**>(original.Receive())); |
| 375 | if (original) { |
| 376 | IUnknown* supported_interface = reinterpret_cast<IUnknown*>( |
| 377 | reinterpret_cast<DWORD_PTR>(obj) + cookie); |
| 378 | supported_interface->AddRef(); |
| 379 | *ret = supported_interface; |
| 380 | hr = S_OK; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | return hr; |
| 385 | } |
| 386 | |
| 387 | // Same as COM_INTERFACE_ENTRY but relies on the class to implement a |
| 388 | // delegate() method that returns a pointer to the delegated COM object. |
| 389 | #define COM_INTERFACE_ENTRY_IF_DELEGATE_SUPPORTS(x) \ |
| 390 | COM_INTERFACE_ENTRY_FUNC(_ATL_IIDOF(x), \ |
| 391 | offsetofclass(x, _ComMapClass), \ |
| 392 | QueryInterfaceIfDelegateSupports<_ComMapClass>) |
| 393 | |
| 394 | // Queries the delegated COM object for an interface, bypassing the wrapper. |
| 395 | #define COM_INTERFACE_BLIND_DELEGATE() \ |
| 396 | COM_INTERFACE_ENTRY_FUNC_BLIND(0, CheckOutgoingInterface<_ComMapClass>) |
| 397 | |
[email protected] | 3eb07da | 2010-02-01 19:48:36 | [diff] [blame] | 398 | // Thread that enters STA and has a UI message loop. |
| 399 | class STAThread : public base::Thread { |
| 400 | public: |
| 401 | explicit STAThread(const char *name) : Thread(name) {} |
[email protected] | d583c3a | 2011-11-02 15:31:56 | [diff] [blame] | 402 | ~STAThread() { |
| 403 | Stop(); |
| 404 | } |
[email protected] | 3eb07da | 2010-02-01 19:48:36 | [diff] [blame] | 405 | bool Start() { |
| 406 | return StartWithOptions(Options(MessageLoop::TYPE_UI, 0)); |
| 407 | } |
| 408 | protected: |
| 409 | // Called just prior to starting the message loop |
| 410 | virtual void Init() { |
| 411 | ::CoInitialize(0); |
| 412 | } |
| 413 | |
| 414 | // Called just after the message loop ends |
| 415 | virtual void CleanUp() { |
| 416 | ::CoUninitialize(); |
| 417 | } |
| 418 | }; |
| 419 | |
[email protected] | ecd9aa3 | 2010-03-01 19:34:39 | [diff] [blame] | 420 | std::wstring GuidToString(const GUID& guid); |
| 421 | |
[email protected] | 7e3544b | 2010-01-22 00:02:34 | [diff] [blame] | 422 | // The urls retrieved from the IMoniker interface don't contain the anchor |
| 423 | // portion of the actual url navigated to. This function checks whether the |
| 424 | // url passed in the bho_url parameter contains an anchor and if yes checks |
| 425 | // whether it matches the url retrieved from the moniker. If yes it returns |
| 426 | // the bho url, if not the moniker url. |
| 427 | std::wstring GetActualUrlFromMoniker(IMoniker* moniker, |
| 428 | IBindCtx* bind_context, |
| 429 | const std::wstring& bho_url); |
| 430 | |
[email protected] | 4e676aa | 2010-02-12 18:19:07 | [diff] [blame] | 431 | // Checks if a window is a top level window |
| 432 | bool IsTopLevelWindow(HWND window); |
| 433 | |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 434 | // Seeks a stream back to position 0. |
| 435 | HRESULT RewindStream(IStream* stream); |
| 436 | |
[email protected] | 80b5a8d | 2010-03-19 16:50:43 | [diff] [blame] | 437 | // Fired when we want to notify IE about privacy changes. |
| 438 | #define WM_FIRE_PRIVACY_CHANGE_NOTIFICATION (WM_APP + 1) |
| 439 | |
[email protected] | 7ae8074 | 2010-03-25 22:02:27 | [diff] [blame] | 440 | // Sent (not posted) when a request needs to be downloaded in the host browser |
| 441 | // instead of Chrome. WPARAM is 0 and LPARAM is a pointer to an IMoniker |
| 442 | // object. |
| 443 | // NOTE: Since the message is sent synchronously, the handler should only |
| 444 | // start asynchronous operations in order to not block the sender unnecessarily. |
| 445 | #define WM_DOWNLOAD_IN_HOST (WM_APP + 2) |
| 446 | |
[email protected] | f4fbae68 | 2011-02-28 22:18:33 | [diff] [blame] | 447 | // This structure contains the parameters sent over to initiate a download |
| 448 | // request in the host browser. |
| 449 | struct DownloadInHostParams { |
[email protected] | 7af08f6 | 2011-03-11 00:03:55 | [diff] [blame] | 450 | base::win::ScopedComPtr<IBindCtx> bind_ctx; |
| 451 | base::win::ScopedComPtr<IMoniker> moniker; |
| 452 | base::win::ScopedComPtr<IStream> post_data; |
[email protected] | f4fbae68 | 2011-02-28 22:18:33 | [diff] [blame] | 453 | std::string request_headers; |
| 454 | }; |
| 455 | |
[email protected] | 80b5a8d | 2010-03-19 16:50:43 | [diff] [blame] | 456 | // Maps the InternetCookieState enum to the corresponding CookieAction values |
| 457 | // used for IE privacy stuff. |
| 458 | int32 MapCookieStateToCookieAction(InternetCookieState cookie_state); |
| 459 | |
[email protected] | e33f99a1 | 2010-04-13 01:27:22 | [diff] [blame] | 460 | // Parses the url passed in and returns a GURL instance without the fragment. |
| 461 | GURL GetUrlWithoutFragment(const wchar_t* url); |
| 462 | |
| 463 | // Compares the URLs passed in after removing the fragments from them. |
| 464 | bool CompareUrlsWithoutFragment(const wchar_t* url1, const wchar_t* url2); |
| 465 | |
[email protected] | 6ee8853 | 2010-04-16 01:10:47 | [diff] [blame] | 466 | // Returns the Referrer from the HTTP headers and additional headers. |
| 467 | std::string FindReferrerFromHeaders(const wchar_t* headers, |
| 468 | const wchar_t* additional_headers); |
| 469 | |
| 470 | // Returns the HTTP headers from the binding passed in. |
| 471 | std::string GetHttpHeadersFromBinding(IBinding* binding); |
| 472 | |
| 473 | // Returns the HTTP response code from the binding passed in. |
| 474 | int GetHttpResponseStatusFromBinding(IBinding* binding); |
| 475 | |
[email protected] | 2b3102be | 2010-04-21 20:07:35 | [diff] [blame] | 476 | // Returns the clipboard format for text/html. |
| 477 | CLIPFORMAT GetTextHtmlClipboardFormat(); |
| 478 | |
| 479 | // Returns true iff the mime type is text/html. |
| 480 | bool IsTextHtmlMimeType(const wchar_t* mime_type); |
| 481 | |
| 482 | // Returns true iff the clipboard format is text/html. |
| 483 | bool IsTextHtmlClipFormat(CLIPFORMAT cf); |
| 484 | |
[email protected] | 74ecb5a4 | 2010-09-03 00:44:35 | [diff] [blame] | 485 | // Returns true if we can detect that we are running as SYSTEM, false otherwise. |
| 486 | bool IsSystemProcess(); |
| 487 | |
[email protected] | 77d7aee | 2010-05-14 20:31:55 | [diff] [blame] | 488 | // STL helper class that implements a functor to delete objects. |
| 489 | // E.g: std::for_each(v.begin(), v.end(), utils::DeleteObject()); |
| 490 | namespace utils { |
| 491 | class DeleteObject { |
| 492 | public: |
| 493 | template <typename T> |
| 494 | void operator()(T* obj) { |
| 495 | delete obj; |
| 496 | } |
| 497 | }; |
| 498 | } |
| 499 | |
[email protected] | ce2b9f9 | 2010-06-09 16:54:12 | [diff] [blame] | 500 | // Convert various protocol flags to text representation. Used for logging. |
| 501 | std::string BindStatus2Str(ULONG bind_status); |
| 502 | std::string PiFlags2Str(DWORD flags); |
| 503 | std::string Bscf2Str(DWORD flags); |
| 504 | |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 505 | // Reads data from a stream into a string. |
| 506 | HRESULT ReadStream(IStream* stream, size_t size, std::string* data); |
| 507 | |
[email protected] | e67a73f4 | 2010-08-31 15:05:02 | [diff] [blame] | 508 | // Parses urls targeted at ChromeFrame. This class maintains state like |
[email protected] | c4e45b3 | 2010-07-28 21:15:15 | [diff] [blame] | 509 | // whether a url is prefixed with the gcf: prefix, whether it is being |
| 510 | // attached to an existing external tab, etc. |
| 511 | class ChromeFrameUrl { |
| 512 | public: |
| 513 | ChromeFrameUrl(); |
| 514 | |
| 515 | // Parses the url passed in. Returns true on success. |
| 516 | bool Parse(const std::wstring& url); |
| 517 | |
| 518 | bool is_chrome_protocol() const { |
| 519 | return is_chrome_protocol_; |
| 520 | } |
| 521 | |
| 522 | bool attach_to_external_tab() const { |
| 523 | return attach_to_external_tab_; |
| 524 | } |
| 525 | |
| 526 | uint64 cookie() const { |
| 527 | return cookie_; |
| 528 | } |
| 529 | |
| 530 | int disposition() const { |
| 531 | return disposition_; |
| 532 | } |
| 533 | |
| 534 | const gfx::Rect& dimensions() const { |
| 535 | return dimensions_; |
| 536 | } |
| 537 | |
[email protected] | d266ce8 | 2010-08-13 21:33:40 | [diff] [blame] | 538 | const GURL& gurl() const { |
| 539 | return parsed_url_; |
[email protected] | c4e45b3 | 2010-07-28 21:15:15 | [diff] [blame] | 540 | } |
| 541 | |
[email protected] | 8103c7f | 2010-09-08 22:36:09 | [diff] [blame] | 542 | const std::string& profile_name() const { |
| 543 | return profile_name_; |
| 544 | } |
| 545 | |
[email protected] | c4e45b3 | 2010-07-28 21:15:15 | [diff] [blame] | 546 | private: |
| 547 | // If we are attaching to an existing external tab, this function parses the |
| 548 | // suffix portion of the URL which contains the attach_external_tab prefix. |
| 549 | bool ParseAttachExternalTabUrl(); |
| 550 | |
[email protected] | d266ce8 | 2010-08-13 21:33:40 | [diff] [blame] | 551 | // Clear state. |
| 552 | void Reset(); |
| 553 | |
[email protected] | c4e45b3 | 2010-07-28 21:15:15 | [diff] [blame] | 554 | bool attach_to_external_tab_; |
| 555 | bool is_chrome_protocol_; |
[email protected] | c4e45b3 | 2010-07-28 21:15:15 | [diff] [blame] | 556 | uint64 cookie_; |
| 557 | gfx::Rect dimensions_; |
| 558 | int disposition_; |
[email protected] | d266ce8 | 2010-08-13 21:33:40 | [diff] [blame] | 559 | |
| 560 | GURL parsed_url_; |
[email protected] | 8103c7f | 2010-09-08 22:36:09 | [diff] [blame] | 561 | std::string profile_name_; |
[email protected] | c4e45b3 | 2010-07-28 21:15:15 | [diff] [blame] | 562 | }; |
| 563 | |
[email protected] | 354bcba | 2010-12-14 04:34:43 | [diff] [blame] | 564 | class NavigationConstraints; |
[email protected] | c4e45b3 | 2010-07-28 21:15:15 | [diff] [blame] | 565 | // Returns true if we can navigate to this URL. |
[email protected] | 354bcba | 2010-12-14 04:34:43 | [diff] [blame] | 566 | // These decisions are controlled by the NavigationConstraints object passed |
| 567 | // in. |
| 568 | bool CanNavigate(const GURL& url, |
| 569 | NavigationConstraints* navigation_constraints); |
[email protected] | e3a91e7 | 2010-06-17 01:19:04 | [diff] [blame] | 570 | |
[email protected] | ec18ca67 | 2010-09-02 13:39:50 | [diff] [blame] | 571 | // Utility function that prevents the current module from ever being unloaded. |
| 572 | // Call if you make irreversible patches. |
| 573 | void PinModule(); |
| 574 | |
[email protected] | d9d8f0c | 2010-09-17 21:47:16 | [diff] [blame] | 575 | // Helper function to spin a message loop and dispatch messages while waiting |
| 576 | // for a handle to be signaled. |
| 577 | void WaitWithMessageLoop(HANDLE* handles, int count, DWORD timeout); |
| 578 | |
[email protected] | 5ac670a | 2010-10-04 23:07:40 | [diff] [blame] | 579 | // Enumerates values in a key and adds them to an array. |
| 580 | // The names of the values are not returned. |
| 581 | void EnumerateKeyValues(HKEY parent_key, const wchar_t* sub_key_name, |
| 582 | std::vector<std::wstring>* values); |
| 583 | |
[email protected] | b57f0323 | 2010-10-25 14:14:39 | [diff] [blame] | 584 | // Interprets the value of an X-UA-Compatible header (or <meta> tag equivalent) |
| 585 | // and indicates whether the header value contains a Chrome Frame directive |
| 586 | // matching a given host browser version. |
| 587 | // |
| 588 | // The header is a series of name-value pairs, with the names being HTTP tokens |
| 589 | // and the values being either tokens or quoted-strings. Names and values are |
[email protected] | 8e698b6c | 2010-10-26 20:29:12 | [diff] [blame] | 590 | // joined by '=' and pairs are delimited by either ';' or ','. LWS may be used |
| 591 | // liberally before and between names, values, '=', and ';' or ','. See RFC 2616 |
| 592 | // for definitions of token, quoted-string, and LWS. See Microsoft's |
| 593 | // documentation of the X-UA-COMPATIBLE header here: |
[email protected] | b57f0323 | 2010-10-25 14:14:39 | [diff] [blame] | 594 | // http://msdn.microsoft.com/en-us/library/cc288325(VS.85).aspx |
| 595 | // |
| 596 | // At most one 'Chrome=<FILTER>' entry is expected in the header value. The |
| 597 | // first valid instance is used. The value of "<FILTER>" (possibly after |
| 598 | // unquoting) is interpreted as follows: |
| 599 | // |
| 600 | // "1" - Always active |
| 601 | // "IE7" - Active for IE major version 7 or lower |
| 602 | // |
| 603 | // For example: |
| 604 | // X-UA-Compatible: IE=8; Chrome=IE6 |
[email protected] | 8e698b6c | 2010-10-26 20:29:12 | [diff] [blame] | 605 | // |
| 606 | // The string is first interpreted using ';' as a delimiter. It is reevaluated |
| 607 | // using ',' iff no valid 'chrome=' value is found. |
[email protected] | b57f0323 | 2010-10-25 14:14:39 | [diff] [blame] | 608 | bool CheckXUaCompatibleDirective(const std::string& directive, |
| 609 | int ie_major_version); |
| 610 | |
[email protected] | 6a8515e4 | 2010-12-16 19:38:52 | [diff] [blame] | 611 | // Returns the version of the current module as a string. |
| 612 | std::wstring GetCurrentModuleVersion(); |
| 613 | |
[email protected] | a17930d8 | 2011-02-11 23:12:35 | [diff] [blame] | 614 | // Returns true if ChromeFrame is the currently loaded document. |
| 615 | bool IsChromeFrameDocument(IWebBrowser2* web_browser); |
| 616 | |
[email protected] | fb123bc | 2011-02-16 19:38:27 | [diff] [blame] | 617 | // Increases the wininet connection limit for HTTP 1.0/1.1 connections to the |
| 618 | // value passed in. This is only done if the existing connection limit is |
| 619 | // lesser than the connection limit passed in. This function attempts to |
| 620 | // increase the connection count once per process. |
| 621 | // Returns true on success. |
| 622 | bool IncreaseWinInetConnections(DWORD connections); |
| 623 | |
[email protected] | 6a46c34 | 2012-05-03 14:49:04 | [diff] [blame] | 624 | // Sets |profile_path| to the path for the Chrome Frame |profile_name| |
| 625 | // profile. |
| 626 | void GetChromeFrameProfilePath(const string16& profile_name, |
| 627 | FilePath* profile_path); |
| 628 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 629 | #endif // CHROME_FRAME_UTILS_H_ |