[email protected] | aa84a7e | 2012-03-15 21:29:06 | [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_CHROME_FRAME_ACTIVEX_BASE_H_ |
| 6 | #define CHROME_FRAME_CHROME_FRAME_ACTIVEX_BASE_H_ |
| 7 | |
| 8 | #include <atlbase.h> |
| 9 | #include <atlcom.h> |
| 10 | #include <atlctl.h> |
[email protected] | 93b79279 | 2011-06-07 23:00:01 | [diff] [blame] | 11 | #include <exdisp.h> |
[email protected] | ea9ed97d | 2010-01-05 19:16:23 | [diff] [blame] | 12 | #include <wininet.h> |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 13 | #include <shdeprecated.h> // for IBrowserService2 |
| 14 | #include <shlguid.h> |
| 15 | |
| 16 | #include <set> |
| 17 | #include <string> |
[email protected] | efd4dfc2 | 2010-03-26 20:14:40 | [diff] [blame] | 18 | #include <vector> |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 19 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 20 | #include "base/metrics/histogram.h" |
[email protected] | ce2086e | 2013-06-11 04:42:10 | [diff] [blame] | 21 | #include "base/strings/string_util.h" |
| 22 | #include "base/strings/stringprintf.h" |
[email protected] | d2d79d5 | 2013-06-07 22:23:48 | [diff] [blame] | 23 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 965722ff | 2010-10-20 15:50:30 | [diff] [blame] | 24 | #include "base/win/scoped_bstr.h" |
| 25 | #include "base/win/scoped_comptr.h" |
| 26 | #include "base/win/scoped_variant.h" |
[email protected] | 7b15c0b9 | 2011-05-24 00:02:35 | [diff] [blame] | 27 | #include "chrome/app/chrome_command_ids.h" |
[email protected] | bc73b4e5 | 2010-03-26 04:16:20 | [diff] [blame] | 28 | #include "chrome/common/url_constants.h" |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 29 | #include "chrome_frame/chrome_frame_plugin.h" |
[email protected] | 8ca280e | 2011-10-19 15:37:33 | [diff] [blame] | 30 | #include "chrome_frame/chrome_tab.h" |
[email protected] | a1800e8 | 2009-11-19 00:53:23 | [diff] [blame] | 31 | #include "chrome_frame/com_message_event.h" |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 32 | #include "chrome_frame/com_type_info_holder.h" |
[email protected] | 3c6f8e1 | 2010-03-24 21:58:21 | [diff] [blame] | 33 | #include "chrome_frame/simple_resource_loader.h" |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 34 | #include "chrome_frame/urlmon_url_request.h" |
[email protected] | 7ae8074 | 2010-03-25 22:02:27 | [diff] [blame] | 35 | #include "chrome_frame/urlmon_url_request_private.h" |
[email protected] | bc73b4e5 | 2010-03-26 04:16:20 | [diff] [blame] | 36 | #include "chrome_frame/utils.h" |
[email protected] | d2d79d5 | 2013-06-07 22:23:48 | [diff] [blame] | 37 | #include "grit/chrome_frame_resources.h" |
[email protected] | 35f13ab | 2009-12-16 23:59:17 | [diff] [blame] | 38 | #include "grit/generated_resources.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 39 | #include "net/cookies/cookie_monster.h" |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 40 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 41 | // Connection point class to support firing IChromeFrameEvents (dispinterface). |
| 42 | template<class T> |
| 43 | class ATL_NO_VTABLE ProxyDIChromeFrameEvents |
| 44 | : public IConnectionPointImpl<T, &DIID_DIChromeFrameEvents> { |
| 45 | public: |
| 46 | void FireMethodWithParams(ChromeFrameEventDispId dispid, |
| 47 | const VARIANT* params, size_t num_params) { |
| 48 | T* me = static_cast<T*>(this); |
[email protected] | efd4dfc2 | 2010-03-26 20:14:40 | [diff] [blame] | 49 | // We need to copy the whole vector and AddRef the sinks in case |
| 50 | // some would get disconnected as we fire methods. Note that this is not |
| 51 | // a threading issue, but a re-entrance issue, because the connection |
| 52 | // can be affected by the implementation of the sinks receiving the event. |
| 53 | me->Lock(); |
[email protected] | 965722ff | 2010-10-20 15:50:30 | [diff] [blame] | 54 | std::vector< base::win::ScopedComPtr<IUnknown> > sink_array( |
| 55 | m_vec.GetSize()); |
[email protected] | efd4dfc2 | 2010-03-26 20:14:40 | [diff] [blame] | 56 | for (int connection = 0; connection < m_vec.GetSize(); ++connection) |
| 57 | sink_array[connection] = m_vec.GetAt(connection); |
| 58 | me->Unlock(); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 59 | |
[email protected] | efd4dfc2 | 2010-03-26 20:14:40 | [diff] [blame] | 60 | for (size_t sink = 0; sink < sink_array.size(); ++sink) { |
| 61 | DIChromeFrameEvents* events = |
| 62 | static_cast<DIChromeFrameEvents*>(sink_array[sink].get()); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 63 | if (events) { |
| 64 | DISPPARAMS disp_params = { |
| 65 | const_cast<VARIANT*>(params), |
| 66 | NULL, |
| 67 | num_params, |
| 68 | 0}; |
| 69 | HRESULT hr = events->Invoke(static_cast<DISPID>(dispid), |
| 70 | DIID_DIChromeFrameEvents, |
| 71 | LOCALE_USER_DEFAULT, DISPATCH_METHOD, |
| 72 | &disp_params, NULL, NULL, NULL); |
| 73 | DLOG_IF(ERROR, FAILED(hr)) << "invoke(" << dispid << ") failed" << |
[email protected] | d3451d83 | 2010-10-01 11:17:37 | [diff] [blame] | 74 | base::StringPrintf("0x%08X", hr); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void FireMethodWithParam(ChromeFrameEventDispId dispid, |
| 80 | const VARIANT& param) { |
| 81 | FireMethodWithParams(dispid, ¶m, 1); |
| 82 | } |
| 83 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 84 | void Fire_onloaderror(IDispatch* event) { |
| 85 | VARIANT var = { VT_DISPATCH }; |
| 86 | var.pdispVal = event; |
| 87 | FireMethodWithParam(CF_EVENT_DISPID_ONLOADERROR, var); |
| 88 | } |
| 89 | |
| 90 | void Fire_onmessage(IDispatch* event) { |
| 91 | VARIANT var = { VT_DISPATCH }; |
| 92 | var.pdispVal = event; |
| 93 | FireMethodWithParam(CF_EVENT_DISPID_ONMESSAGE, var); |
| 94 | } |
| 95 | |
[email protected] | 62bb18dc1 | 2009-11-25 01:34:08 | [diff] [blame] | 96 | void Fire_onreadystatechanged(long readystate) { // NOLINT |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 97 | VARIANT var = { VT_I4 }; |
| 98 | var.lVal = readystate; |
| 99 | FireMethodWithParam(CF_EVENT_DISPID_ONREADYSTATECHANGED, var); |
| 100 | } |
| 101 | |
| 102 | void Fire_onprivatemessage(IDispatch* event, BSTR target) { |
| 103 | // Arguments in reverse order to the function declaration, because |
| 104 | // that's what DISPPARAMS requires. |
| 105 | VARIANT args[2] = { { VT_BSTR, }, {VT_DISPATCH, } }; |
| 106 | args[0].bstrVal = target; |
| 107 | args[1].pdispVal = event; |
| 108 | |
| 109 | FireMethodWithParams(CF_EVENT_DISPID_ONPRIVATEMESSAGE, |
| 110 | args, |
| 111 | arraysize(args)); |
| 112 | } |
[email protected] | 00f6b77 | 2009-10-23 17:03:41 | [diff] [blame] | 113 | |
[email protected] | efd4dfc2 | 2010-03-26 20:14:40 | [diff] [blame] | 114 | void Fire_onchannelerror() { // NOLINT |
| 115 | FireMethodWithParams(CF_EVENT_DISPID_ONCHANNELERROR, NULL, 0); |
| 116 | } |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | extern bool g_first_launch_by_process_; |
| 120 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 121 | // Common implementation for ActiveX and Active Document |
| 122 | template <class T, const CLSID& class_id> |
[email protected] | 62bb18dc1 | 2009-11-25 01:34:08 | [diff] [blame] | 123 | class ATL_NO_VTABLE ChromeFrameActivexBase : // NOLINT |
[email protected] | b0febbf | 2009-11-12 17:49:35 | [diff] [blame] | 124 | public CComObjectRootEx<CComMultiThreadModel>, |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 125 | public IOleControlImpl<T>, |
| 126 | public IOleObjectImpl<T>, |
| 127 | public IOleInPlaceActiveObjectImpl<T>, |
| 128 | public IViewObjectExImpl<T>, |
| 129 | public IOleInPlaceObjectWindowlessImpl<T>, |
| 130 | public ISupportErrorInfo, |
| 131 | public IQuickActivateImpl<T>, |
| 132 | public com_util::IProvideClassInfo2Impl<class_id, |
| 133 | DIID_DIChromeFrameEvents>, |
| 134 | public com_util::IDispatchImpl<IChromeFrame>, |
| 135 | public IConnectionPointContainerImpl<T>, |
| 136 | public ProxyDIChromeFrameEvents<T>, |
| 137 | public IPropertyNotifySinkCP<T>, |
| 138 | public CComCoClass<T, &class_id>, |
| 139 | public CComControl<T>, |
[email protected] | 0753db59 | 2010-12-15 14:45:05 | [diff] [blame] | 140 | public ChromeFramePlugin<T> { |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 141 | protected: |
[email protected] | 965722ff | 2010-10-20 15:50:30 | [diff] [blame] | 142 | typedef std::set<base::win::ScopedComPtr<IDispatch> > EventHandlers; |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 143 | typedef ChromeFrameActivexBase<T, class_id> BasePlugin; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 144 | |
| 145 | public: |
| 146 | ChromeFrameActivexBase() |
[email protected] | 3eafbfb9 | 2010-08-02 16:54:22 | [diff] [blame] | 147 | : ready_state_(READYSTATE_UNINITIALIZED), |
[email protected] | bbfa9a1 | 2010-08-10 14:09:37 | [diff] [blame] | 148 | url_fetcher_(new UrlmonUrlRequestManager()), |
| 149 | failed_to_fetch_in_place_frame_(false), |
[email protected] | 6e58a95 | 2011-01-12 19:28:50 | [diff] [blame] | 150 | draw_sad_tab_(false) { |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 151 | m_bWindowOnly = TRUE; |
[email protected] | bbfa9a1 | 2010-08-10 14:09:37 | [diff] [blame] | 152 | url_fetcher_->set_container(static_cast<IDispatch*>(this)); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | ~ChromeFrameActivexBase() { |
[email protected] | bbfa9a1 | 2010-08-10 14:09:37 | [diff] [blame] | 156 | url_fetcher_->set_container(NULL); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | DECLARE_OLEMISC_STATUS(OLEMISC_RECOMPOSEONRESIZE | OLEMISC_CANTLINKINSIDE | |
| 160 | OLEMISC_INSIDEOUT | OLEMISC_ACTIVATEWHENVISIBLE | |
| 161 | OLEMISC_SETCLIENTSITEFIRST) |
| 162 | |
| 163 | DECLARE_NOT_AGGREGATABLE(T) |
| 164 | |
| 165 | BEGIN_COM_MAP(ChromeFrameActivexBase) |
| 166 | COM_INTERFACE_ENTRY(IChromeFrame) |
| 167 | COM_INTERFACE_ENTRY(IDispatch) |
| 168 | COM_INTERFACE_ENTRY(IViewObjectEx) |
| 169 | COM_INTERFACE_ENTRY(IViewObject2) |
| 170 | COM_INTERFACE_ENTRY(IViewObject) |
| 171 | COM_INTERFACE_ENTRY(IOleInPlaceObjectWindowless) |
| 172 | COM_INTERFACE_ENTRY(IOleInPlaceObject) |
| 173 | COM_INTERFACE_ENTRY2(IOleWindow, IOleInPlaceObjectWindowless) |
| 174 | COM_INTERFACE_ENTRY(IOleInPlaceActiveObject) |
| 175 | COM_INTERFACE_ENTRY(IOleControl) |
| 176 | COM_INTERFACE_ENTRY(IOleObject) |
| 177 | COM_INTERFACE_ENTRY(ISupportErrorInfo) |
| 178 | COM_INTERFACE_ENTRY(IQuickActivate) |
| 179 | COM_INTERFACE_ENTRY(IProvideClassInfo) |
| 180 | COM_INTERFACE_ENTRY(IProvideClassInfo2) |
[email protected] | bbfa9a1 | 2010-08-10 14:09:37 | [diff] [blame] | 181 | COM_INTERFACE_ENTRY(IConnectionPointContainer) |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 182 | COM_INTERFACE_ENTRY_FUNC_BLIND(0, InterfaceNotSupported) |
| 183 | END_COM_MAP() |
| 184 | |
| 185 | BEGIN_CONNECTION_POINT_MAP(T) |
| 186 | CONNECTION_POINT_ENTRY(IID_IPropertyNotifySink) |
| 187 | CONNECTION_POINT_ENTRY(DIID_DIChromeFrameEvents) |
| 188 | END_CONNECTION_POINT_MAP() |
| 189 | |
| 190 | BEGIN_MSG_MAP(ChromeFrameActivexBase) |
| 191 | MESSAGE_HANDLER(WM_CREATE, OnCreate) |
[email protected] | e9eb50c | 2009-10-24 16:26:46 | [diff] [blame] | 192 | MESSAGE_HANDLER(WM_DESTROY, OnDestroy) |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 193 | CHAIN_MSG_MAP(ChromeFramePlugin<T>) |
| 194 | CHAIN_MSG_MAP(CComControl<T>) |
| 195 | DEFAULT_REFLECTION_HANDLER() |
| 196 | END_MSG_MAP() |
| 197 | |
| 198 | // IViewObjectEx |
| 199 | DECLARE_VIEW_STATUS(VIEWSTATUS_SOLIDBKGND | VIEWSTATUS_OPAQUE) |
| 200 | |
[email protected] | 48e6bb6 | 2009-09-30 20:32:46 | [diff] [blame] | 201 | inline HRESULT IViewObject_Draw(DWORD draw_aspect, LONG index, |
| 202 | void* aspect_info, DVTARGETDEVICE* ptd, HDC info_dc, HDC dc, |
| 203 | LPCRECTL bounds, LPCRECTL win_bounds) { |
| 204 | // ATL ASSERTs if dwDrawAspect is DVASPECT_DOCPRINT, so we cheat. |
| 205 | DWORD aspect = draw_aspect; |
| 206 | if (aspect == DVASPECT_DOCPRINT) |
| 207 | aspect = DVASPECT_CONTENT; |
| 208 | |
| 209 | return CComControl<T>::IViewObject_Draw(aspect, index, aspect_info, ptd, |
| 210 | info_dc, dc, bounds, win_bounds); |
| 211 | } |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 212 | |
| 213 | DECLARE_PROTECT_FINAL_CONSTRUCT() |
| 214 | |
[email protected] | 3eb8a7049 | 2010-12-03 21:32:19 | [diff] [blame] | 215 | void SetResourceModule() { |
[email protected] | 8e8bb6d | 2010-12-13 08:18:55 | [diff] [blame] | 216 | SimpleResourceLoader* loader_instance = SimpleResourceLoader::GetInstance(); |
[email protected] | 3c6f8e1 | 2010-03-24 21:58:21 | [diff] [blame] | 217 | DCHECK(loader_instance); |
[email protected] | 6ae3d49 | 2010-10-20 14:01:21 | [diff] [blame] | 218 | HMODULE res_dll = loader_instance->GetResourceModuleHandle(); |
[email protected] | 3eb8a7049 | 2010-12-03 21:32:19 | [diff] [blame] | 219 | _AtlBaseModule.SetResourceInstance(res_dll); |
[email protected] | 3c6f8e1 | 2010-03-24 21:58:21 | [diff] [blame] | 220 | } |
| 221 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 222 | HRESULT FinalConstruct() { |
[email protected] | 3c6f8e1 | 2010-03-24 21:58:21 | [diff] [blame] | 223 | SetResourceModule(); |
| 224 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 225 | if (!Initialize()) |
| 226 | return E_OUTOFMEMORY; |
| 227 | |
| 228 | // Set to true if this is the first launch by this process. |
| 229 | // Used to perform one time tasks. |
| 230 | if (g_first_launch_by_process_) { |
| 231 | g_first_launch_by_process_ = false; |
[email protected] | 92c79d3 | 2011-04-06 18:41:15 | [diff] [blame] | 232 | UMA_HISTOGRAM_CUSTOM_COUNTS("ChromeFrame.IEVersion", |
| 233 | GetIEVersion(), |
| 234 | IE_INVALID, |
[email protected] | 5ba4b8a9 | 2011-09-29 05:09:42 | [diff] [blame] | 235 | IE_10, |
| 236 | IE_10 + 1); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 237 | } |
[email protected] | 8103c7f | 2010-09-08 22:36:09 | [diff] [blame] | 238 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 239 | return S_OK; |
| 240 | } |
| 241 | |
| 242 | void FinalRelease() { |
[email protected] | 89f1954 | 2010-01-05 21:55:11 | [diff] [blame] | 243 | Uninitialize(); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 244 | } |
| 245 | |
[email protected] | bbfa9a1 | 2010-08-10 14:09:37 | [diff] [blame] | 246 | void ResetUrlRequestManager() { |
| 247 | url_fetcher_.reset(new UrlmonUrlRequestManager()); |
| 248 | } |
| 249 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 250 | static HRESULT WINAPI InterfaceNotSupported(void* pv, REFIID riid, void** ppv, |
| 251 | DWORD dw) { |
| 252 | #ifndef NDEBUG |
| 253 | wchar_t buffer[64] = {0}; |
| 254 | ::StringFromGUID2(riid, buffer, arraysize(buffer)); |
[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 255 | DVLOG(1) << "E_NOINTERFACE: " << buffer; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 256 | #endif |
| 257 | return E_NOINTERFACE; |
| 258 | } |
| 259 | |
| 260 | // ISupportsErrorInfo |
| 261 | STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid) { |
| 262 | static const IID* interfaces[] = { |
| 263 | &IID_IChromeFrame, |
| 264 | &IID_IDispatch |
| 265 | }; |
| 266 | |
| 267 | for (int i = 0; i < arraysize(interfaces); ++i) { |
| 268 | if (InlineIsEqualGUID(*interfaces[i], riid)) |
| 269 | return S_OK; |
| 270 | } |
| 271 | return S_FALSE; |
| 272 | } |
| 273 | |
| 274 | // Called to draw our control when chrome hasn't been initialized. |
[email protected] | 62bb18dc1 | 2009-11-25 01:34:08 | [diff] [blame] | 275 | virtual HRESULT OnDraw(ATL_DRAWINFO& draw_info) { // NOLINT |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 276 | if (NULL == draw_info.prcBounds) { |
| 277 | NOTREACHED(); |
| 278 | return E_FAIL; |
| 279 | } |
[email protected] | bbfa9a1 | 2010-08-10 14:09:37 | [diff] [blame] | 280 | |
| 281 | if (draw_sad_tab_) { |
| 282 | // TODO(tommi): Draw a proper sad tab. |
| 283 | RECT rc = {0}; |
| 284 | if (draw_info.prcBounds) { |
| 285 | rc.top = draw_info.prcBounds->top; |
| 286 | rc.bottom = draw_info.prcBounds->bottom; |
| 287 | rc.left = draw_info.prcBounds->left; |
| 288 | rc.right = draw_info.prcBounds->right; |
| 289 | } else { |
| 290 | GetClientRect(&rc); |
| 291 | } |
| 292 | ::DrawTextA(draw_info.hdcDraw, ":-(", -1, &rc, |
| 293 | DT_CENTER | DT_VCENTER | DT_SINGLELINE); |
| 294 | } else { |
| 295 | // Don't draw anything. |
| 296 | } |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 297 | return S_OK; |
| 298 | } |
| 299 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 300 | // Used to setup the document_url_ member needed for completing navigation. |
| 301 | // Create external tab (possibly in incognito mode). |
| 302 | HRESULT IOleObject_SetClientSite(IOleClientSite* client_site) { |
| 303 | // If we currently have a document site pointer, release it. |
| 304 | doc_site_.Release(); |
| 305 | if (client_site) { |
| 306 | doc_site_.QueryFrom(client_site); |
| 307 | } |
| 308 | |
[email protected] | 3eafbfb9 | 2010-08-02 16:54:22 | [diff] [blame] | 309 | if (client_site == NULL) { |
| 310 | in_place_frame_.Release(); |
| 311 | } |
| 312 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 313 | return CComControlBase::IOleObject_SetClientSite(client_site); |
| 314 | } |
| 315 | |
[email protected] | 10bf1a7 | 2009-10-01 16:00:21 | [diff] [blame] | 316 | // Should connections initiated by this class try to block |
| 317 | // responses served with the X-Frame-Options header? |
| 318 | // ActiveX controls genereally will want to do this, |
| 319 | // returning true, while true top-level documents |
| 320 | // (ActiveDocument servers) will not. Your specialization |
| 321 | // of this template should implement this method based on how |
| 322 | // it "feels" from a security perspective. If it's hosted in another |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 323 | // scriptable document, return true, else false. |
[email protected] | 2ce57c78 | 2009-11-17 18:15:40 | [diff] [blame] | 324 | // |
| 325 | // The base implementation returns true unless we are in privileged |
| 326 | // mode, in which case we always trust our container so we return false. |
[email protected] | 5778de6e | 2009-10-24 01:29:20 | [diff] [blame] | 327 | bool is_frame_busting_enabled() const { |
[email protected] | 0753db59 | 2010-12-15 14:45:05 | [diff] [blame] | 328 | return !is_privileged(); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 329 | } |
| 330 | |
[email protected] | 74b1eaf54 | 2011-10-05 19:34:27 | [diff] [blame] | 331 | static void BringWebBrowserWindowToTop(IWebBrowser2* web_browser2) { |
| 332 | DCHECK(web_browser2); |
| 333 | if (web_browser2) { |
| 334 | web_browser2->put_Visible(VARIANT_TRUE); |
| 335 | HWND ie_window = NULL; |
| 336 | web_browser2->get_HWND(reinterpret_cast<long*>(&ie_window)); |
| 337 | ::BringWindowToTop(ie_window); |
| 338 | } |
| 339 | } |
| 340 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 341 | protected: |
[email protected] | bc73b4e5 | 2010-03-26 04:16:20 | [diff] [blame] | 342 | virtual void GetProfilePath(const std::wstring& profile_name, |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 343 | base::FilePath* profile_path) { |
[email protected] | bc73b4e5 | 2010-03-26 04:16:20 | [diff] [blame] | 344 | bool is_IE = (lstrcmpi(profile_name.c_str(), kIexploreProfileName) == 0) || |
| 345 | (lstrcmpi(profile_name.c_str(), kRundllProfileName) == 0); |
| 346 | // Browsers without IDeleteBrowsingHistory in non-priv mode |
| 347 | // have their profiles moved into "Temporary Internet Files". |
[email protected] | d9ca776c | 2011-01-31 21:44:26 | [diff] [blame] | 348 | if (is_IE && GetIEVersion() < IE_8) { |
[email protected] | bc73b4e5 | 2010-03-26 04:16:20 | [diff] [blame] | 349 | *profile_path = GetIETemporaryFilesFolder(); |
| 350 | *profile_path = profile_path->Append(L"Google Chrome Frame"); |
| 351 | } else { |
| 352 | ChromeFramePlugin::GetProfilePath(profile_name, profile_path); |
| 353 | } |
[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 354 | DVLOG(1) << __FUNCTION__ << ": " << profile_path->value(); |
[email protected] | bc73b4e5 | 2010-03-26 04:16:20 | [diff] [blame] | 355 | } |
| 356 | |
[email protected] | a1800e8 | 2009-11-19 00:53:23 | [diff] [blame] | 357 | void OnLoadFailed(int error_code, const std::string& url) { |
| 358 | HRESULT hr = InvokeScriptFunction(onerror_handler_, url); |
| 359 | } |
| 360 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 361 | LRESULT OnCreate(UINT message, WPARAM wparam, LPARAM lparam, |
| 362 | BOOL& handled) { // NO_LINT |
| 363 | ModifyStyle(0, WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0); |
[email protected] | bbfa9a1 | 2010-08-10 14:09:37 | [diff] [blame] | 364 | url_fetcher_->put_notification_window(m_hWnd); |
[email protected] | dd4beb52 | 2010-07-13 18:18:14 | [diff] [blame] | 365 | if (automation_client_.get()) { |
| 366 | automation_client_->SetParentWindow(m_hWnd); |
| 367 | } else { |
| 368 | NOTREACHED() << "No automation server"; |
| 369 | return -1; |
| 370 | } |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 371 | // Only fire the 'interactive' ready state if we aren't there already. |
| 372 | if (ready_state_ < READYSTATE_INTERACTIVE) { |
| 373 | ready_state_ = READYSTATE_INTERACTIVE; |
| 374 | FireOnChanged(DISPID_READYSTATE); |
| 375 | } |
| 376 | return 0; |
| 377 | } |
| 378 | |
[email protected] | e9eb50c | 2009-10-24 16:26:46 | [diff] [blame] | 379 | LRESULT OnDestroy(UINT message, WPARAM wparam, LPARAM lparam, |
| 380 | BOOL& handled) { // NO_LINT |
[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 381 | DVLOG(1) << __FUNCTION__; |
[email protected] | e9eb50c | 2009-10-24 16:26:46 | [diff] [blame] | 382 | return 0; |
| 383 | } |
| 384 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 385 | // ChromeFrameDelegate override |
| 386 | virtual void OnAutomationServerReady() { |
[email protected] | bbfa9a1 | 2010-08-10 14:09:37 | [diff] [blame] | 387 | draw_sad_tab_ = false; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 388 | ChromeFramePlugin<T>::OnAutomationServerReady(); |
| 389 | |
| 390 | ready_state_ = READYSTATE_COMPLETE; |
| 391 | FireOnChanged(DISPID_READYSTATE); |
| 392 | } |
| 393 | |
| 394 | // ChromeFrameDelegate override |
| 395 | virtual void OnAutomationServerLaunchFailed( |
| 396 | AutomationLaunchResult reason, const std::string& server_version) { |
[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 397 | DVLOG(1) << __FUNCTION__; |
[email protected] | bbfa9a1 | 2010-08-10 14:09:37 | [diff] [blame] | 398 | if (reason == AUTOMATION_SERVER_CRASHED) |
| 399 | draw_sad_tab_ = true; |
| 400 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 401 | ready_state_ = READYSTATE_UNINITIALIZED; |
| 402 | FireOnChanged(DISPID_READYSTATE); |
| 403 | } |
| 404 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 405 | // Overridden to take advantage of readystate prop changes and send those |
| 406 | // to potential listeners. |
| 407 | HRESULT FireOnChanged(DISPID dispid) { |
| 408 | if (dispid == DISPID_READYSTATE) { |
| 409 | Fire_onreadystatechanged(ready_state_); |
| 410 | } |
| 411 | return __super::FireOnChanged(dispid); |
| 412 | } |
| 413 | |
| 414 | // IChromeFrame |
| 415 | // Property getter/setters for the src attribute, which contains a URL. |
| 416 | // The ChromeFrameActivex control initiates navigation to this URL |
| 417 | // when instantiated. |
| 418 | STDMETHOD(get_src)(BSTR* src) { |
| 419 | if (NULL == src) { |
| 420 | return E_POINTER; |
| 421 | } |
| 422 | |
| 423 | *src = SysAllocString(url_); |
| 424 | return S_OK; |
| 425 | } |
| 426 | |
| 427 | STDMETHOD(put_src)(BSTR src) { |
| 428 | if (src == NULL) |
| 429 | return E_INVALIDARG; |
| 430 | |
| 431 | // Switch the src to UTF8 and try to expand to full URL |
| 432 | std::string src_utf8; |
[email protected] | d519b81 | 2013-12-25 18:15:40 | [diff] [blame] | 433 | base::WideToUTF8(src, SysStringLen(src), &src_utf8); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 434 | std::string full_url = ResolveURL(GetDocumentUrl(), src_utf8); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 435 | |
| 436 | // We can initiate navigation here even if ready_state is not complete. |
| 437 | // We do not have to set proxy, and AutomationClient will take care |
| 438 | // of navigation just after CreateExternalTab is done. |
[email protected] | b36a9f9 | 2009-10-19 17:34:57 | [diff] [blame] | 439 | if (!automation_client_->InitiateNavigation(full_url, |
| 440 | GetDocumentUrl(), |
[email protected] | 354bcba | 2010-12-14 04:34:43 | [diff] [blame] | 441 | this)) { |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 442 | // TODO(robertshield): Make InitiateNavigation return more useful |
| 443 | // error information. |
| 444 | return E_INVALIDARG; |
| 445 | } |
| 446 | |
| 447 | // Save full URL in BSTR member |
[email protected] | d519b81 | 2013-12-25 18:15:40 | [diff] [blame] | 448 | url_.Reset(::SysAllocString(base::UTF8ToWide(full_url).c_str())); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 449 | |
| 450 | return S_OK; |
| 451 | } |
| 452 | |
| 453 | STDMETHOD(get_onload)(VARIANT* onload_handler) { |
| 454 | if (NULL == onload_handler) |
| 455 | return E_INVALIDARG; |
| 456 | |
| 457 | *onload_handler = onload_handler_.Copy(); |
| 458 | |
| 459 | return S_OK; |
| 460 | } |
| 461 | |
| 462 | // Property setter for the onload attribute, which contains a |
| 463 | // javascript function to be invoked on successful navigation. |
| 464 | STDMETHOD(put_onload)(VARIANT onload_handler) { |
| 465 | if (V_VT(&onload_handler) != VT_DISPATCH) { |
| 466 | DLOG(WARNING) << "Invalid onload handler type: " |
| 467 | << onload_handler.vt |
| 468 | << " specified"; |
| 469 | return E_INVALIDARG; |
| 470 | } |
| 471 | |
| 472 | onload_handler_ = onload_handler; |
| 473 | |
| 474 | return S_OK; |
| 475 | } |
| 476 | |
| 477 | // Property getter/setters for the onloaderror attribute, which contains a |
| 478 | // javascript function to be invoked on navigation failure. |
| 479 | STDMETHOD(get_onloaderror)(VARIANT* onerror_handler) { |
| 480 | if (NULL == onerror_handler) |
| 481 | return E_INVALIDARG; |
| 482 | |
| 483 | *onerror_handler = onerror_handler_.Copy(); |
| 484 | |
| 485 | return S_OK; |
| 486 | } |
| 487 | |
| 488 | STDMETHOD(put_onloaderror)(VARIANT onerror_handler) { |
| 489 | if (V_VT(&onerror_handler) != VT_DISPATCH) { |
| 490 | DLOG(WARNING) << "Invalid onloaderror handler type: " |
| 491 | << onerror_handler.vt |
| 492 | << " specified"; |
| 493 | return E_INVALIDARG; |
| 494 | } |
| 495 | |
| 496 | onerror_handler_ = onerror_handler; |
| 497 | |
| 498 | return S_OK; |
| 499 | } |
| 500 | |
| 501 | // Property getter/setters for the onmessage attribute, which contains a |
| 502 | // javascript function to be invoked when we receive a message from the |
| 503 | // chrome frame. |
| 504 | STDMETHOD(put_onmessage)(VARIANT onmessage_handler) { |
| 505 | if (V_VT(&onmessage_handler) != VT_DISPATCH) { |
| 506 | DLOG(WARNING) << "Invalid onmessage handler type: " |
| 507 | << onmessage_handler.vt |
| 508 | << " specified"; |
| 509 | return E_INVALIDARG; |
| 510 | } |
| 511 | |
| 512 | onmessage_handler_ = onmessage_handler; |
| 513 | |
| 514 | return S_OK; |
| 515 | } |
| 516 | |
| 517 | STDMETHOD(get_onmessage)(VARIANT* onmessage_handler) { |
| 518 | if (NULL == onmessage_handler) |
| 519 | return E_INVALIDARG; |
| 520 | |
| 521 | *onmessage_handler = onmessage_handler_.Copy(); |
| 522 | |
| 523 | return S_OK; |
| 524 | } |
| 525 | |
| 526 | STDMETHOD(get_readyState)(long* ready_state) { // NOLINT |
[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 527 | DVLOG(1) << __FUNCTION__; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 528 | DCHECK(ready_state); |
| 529 | |
| 530 | if (!ready_state) |
| 531 | return E_INVALIDARG; |
| 532 | |
| 533 | *ready_state = ready_state_; |
| 534 | |
| 535 | return S_OK; |
| 536 | } |
| 537 | |
| 538 | // Property getter/setters for use_chrome_network flag. This flag |
| 539 | // indicates if chrome network stack is to be used for fetching |
| 540 | // network requests. |
| 541 | STDMETHOD(get_useChromeNetwork)(VARIANT_BOOL* use_chrome_network) { |
| 542 | if (!use_chrome_network) |
| 543 | return E_INVALIDARG; |
| 544 | |
| 545 | *use_chrome_network = |
| 546 | automation_client_->use_chrome_network() ? VARIANT_TRUE : VARIANT_FALSE; |
| 547 | return S_OK; |
| 548 | } |
| 549 | |
| 550 | STDMETHOD(put_useChromeNetwork)(VARIANT_BOOL use_chrome_network) { |
[email protected] | 0753db59 | 2010-12-15 14:45:05 | [diff] [blame] | 551 | if (!is_privileged()) { |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 552 | DLOG(ERROR) << "Attempt to set useChromeNetwork in non-privileged mode"; |
| 553 | return E_ACCESSDENIED; |
| 554 | } |
| 555 | |
| 556 | automation_client_->set_use_chrome_network( |
| 557 | (VARIANT_FALSE != use_chrome_network)); |
| 558 | return S_OK; |
| 559 | } |
| 560 | |
| 561 | // Posts a message to the chrome frame. |
| 562 | STDMETHOD(postMessage)(BSTR message, VARIANT target) { |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 563 | return S_OK; |
| 564 | } |
| 565 | |
| 566 | STDMETHOD(addEventListener)(BSTR event_type, IDispatch* listener, |
| 567 | VARIANT use_capture) { |
| 568 | EventHandlers* handlers = NULL; |
| 569 | HRESULT hr = GetHandlersForEvent(event_type, &handlers); |
| 570 | if (FAILED(hr)) |
| 571 | return hr; |
| 572 | |
| 573 | DCHECK(handlers != NULL); |
| 574 | |
[email protected] | 965722ff | 2010-10-20 15:50:30 | [diff] [blame] | 575 | handlers->insert(base::win::ScopedComPtr<IDispatch>(listener)); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 576 | |
| 577 | return hr; |
| 578 | } |
| 579 | |
| 580 | STDMETHOD(removeEventListener)(BSTR event_type, IDispatch* listener, |
| 581 | VARIANT use_capture) { |
| 582 | EventHandlers* handlers = NULL; |
| 583 | HRESULT hr = GetHandlersForEvent(event_type, &handlers); |
| 584 | if (FAILED(hr)) |
| 585 | return hr; |
| 586 | |
| 587 | DCHECK(handlers != NULL); |
[email protected] | c90d849 | 2011-01-30 22:47:42 | [diff] [blame] | 588 | handlers->erase(base::win::ScopedComPtr<IDispatch>(listener)); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 589 | |
| 590 | return hr; |
| 591 | } |
| 592 | |
| 593 | STDMETHOD(get_version)(BSTR* version) { |
| 594 | if (!automation_client_.get()) { |
| 595 | NOTREACHED(); |
| 596 | return E_FAIL; |
| 597 | } |
| 598 | |
| 599 | if (version == NULL) { |
| 600 | return E_INVALIDARG; |
| 601 | } |
| 602 | |
| 603 | *version = SysAllocString(automation_client_->GetVersion().c_str()); |
| 604 | return S_OK; |
| 605 | } |
| 606 | |
| 607 | STDMETHOD(postPrivateMessage)(BSTR message, BSTR origin, BSTR target) { |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 608 | return S_OK; |
| 609 | } |
| 610 | |
[email protected] | 00f6b77 | 2009-10-23 17:03:41 | [diff] [blame] | 611 | STDMETHOD(installExtension)(BSTR crx_path) { |
[email protected] | 7419d4b | 2011-04-06 15:18:04 | [diff] [blame] | 612 | NOTREACHED(); // Deprecated. |
| 613 | return E_NOTIMPL; |
[email protected] | 00f6b77 | 2009-10-23 17:03:41 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | STDMETHOD(loadExtension)(BSTR path) { |
[email protected] | 7419d4b | 2011-04-06 15:18:04 | [diff] [blame] | 617 | NOTREACHED(); // Deprecated. |
| 618 | return E_NOTIMPL; |
[email protected] | 00f6b77 | 2009-10-23 17:03:41 | [diff] [blame] | 619 | } |
| 620 | |
[email protected] | a1e62d1 | 2010-03-16 02:18:43 | [diff] [blame] | 621 | STDMETHOD(getEnabledExtensions)() { |
[email protected] | 7419d4b | 2011-04-06 15:18:04 | [diff] [blame] | 622 | NOTREACHED(); // Deprecated. |
| 623 | return E_NOTIMPL; |
[email protected] | 751bf4b | 2010-11-05 22:06:31 | [diff] [blame] | 624 | } |
| 625 | |
[email protected] | ca982ec4 | 2010-05-02 14:47:14 | [diff] [blame] | 626 | STDMETHOD(registerBhoIfNeeded)() { |
[email protected] | e12a3b8 | 2010-05-01 00:06:00 | [diff] [blame] | 627 | return E_NOTIMPL; |
| 628 | } |
| 629 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 630 | // Returns the vector of event handlers for a given event (e.g. "load"). |
| 631 | // If the event type isn't recognized, the function fills in a descriptive |
| 632 | // error (IErrorInfo) and returns E_INVALIDARG. |
| 633 | HRESULT GetHandlersForEvent(BSTR event_type, EventHandlers** handlers) { |
| 634 | DCHECK(handlers != NULL); |
| 635 | |
[email protected] | 00f6b77 | 2009-10-23 17:03:41 | [diff] [blame] | 636 | // TODO(tommi): make these if() statements data-driven. |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 637 | HRESULT hr = S_OK; |
| 638 | const wchar_t* event_type_end = event_type + ::SysStringLen(event_type); |
| 639 | if (LowerCaseEqualsASCII(event_type, event_type_end, "message")) { |
| 640 | *handlers = &onmessage_; |
| 641 | } else if (LowerCaseEqualsASCII(event_type, event_type_end, "load")) { |
| 642 | *handlers = &onload_; |
| 643 | } else if (LowerCaseEqualsASCII(event_type, event_type_end, "loaderror")) { |
| 644 | *handlers = &onloaderror_; |
| 645 | } else if (LowerCaseEqualsASCII(event_type, event_type_end, |
| 646 | "readystatechanged")) { |
| 647 | *handlers = &onreadystatechanged_; |
| 648 | } else if (LowerCaseEqualsASCII(event_type, event_type_end, |
[email protected] | 00f6b77 | 2009-10-23 17:03:41 | [diff] [blame] | 649 | "privatemessage")) { |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 650 | // This event handler is only available in privileged mode. |
[email protected] | 0753db59 | 2010-12-15 14:45:05 | [diff] [blame] | 651 | if (is_privileged()) { |
[email protected] | 00f6b77 | 2009-10-23 17:03:41 | [diff] [blame] | 652 | *handlers = &onprivatemessage_; |
| 653 | } else { |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 654 | Error("Event type 'privatemessage' is privileged"); |
[email protected] | 00f6b77 | 2009-10-23 17:03:41 | [diff] [blame] | 655 | hr = E_ACCESSDENIED; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 656 | } |
[email protected] | 00f6b77 | 2009-10-23 17:03:41 | [diff] [blame] | 657 | } else if (LowerCaseEqualsASCII(event_type, event_type_end, |
| 658 | "extensionready")) { |
| 659 | // This event handler is only available in privileged mode. |
[email protected] | 0753db59 | 2010-12-15 14:45:05 | [diff] [blame] | 660 | if (is_privileged()) { |
[email protected] | 00f6b77 | 2009-10-23 17:03:41 | [diff] [blame] | 661 | *handlers = &onextensionready_; |
| 662 | } else { |
| 663 | Error("Event type 'extensionready' is privileged"); |
| 664 | hr = E_ACCESSDENIED; |
| 665 | } |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 666 | } else { |
[email protected] | d3451d83 | 2010-10-01 11:17:37 | [diff] [blame] | 667 | Error(base::StringPrintf( |
| 668 | "Event type '%ls' not found", event_type).c_str()); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 669 | hr = E_INVALIDARG; |
| 670 | } |
| 671 | |
| 672 | return hr; |
| 673 | } |
| 674 | |
[email protected] | a1800e8 | 2009-11-19 00:53:23 | [diff] [blame] | 675 | // Creates a new event object that supports the |data| property. |
| 676 | // Note: you should supply an empty string for |origin| unless you're |
| 677 | // creating a "message" event. |
| 678 | HRESULT CreateDomEvent(const std::string& event_type, const std::string& data, |
| 679 | const std::string& origin, IDispatch** event) { |
[email protected] | 62bb18dc1 | 2009-11-25 01:34:08 | [diff] [blame] | 680 | DCHECK(event_type.length() > 0); // NOLINT |
[email protected] | a1800e8 | 2009-11-19 00:53:23 | [diff] [blame] | 681 | DCHECK(event != NULL); |
| 682 | |
| 683 | CComObject<ComMessageEvent>* ev = NULL; |
| 684 | HRESULT hr = CComObject<ComMessageEvent>::CreateInstance(&ev); |
| 685 | if (SUCCEEDED(hr)) { |
| 686 | ev->AddRef(); |
| 687 | |
[email protected] | 965722ff | 2010-10-20 15:50:30 | [diff] [blame] | 688 | base::win::ScopedComPtr<IOleContainer> container; |
[email protected] | a1800e8 | 2009-11-19 00:53:23 | [diff] [blame] | 689 | m_spClientSite->GetContainer(container.Receive()); |
| 690 | if (ev->Initialize(container, data, origin, event_type)) { |
| 691 | *event = ev; |
| 692 | } else { |
| 693 | NOTREACHED() << "event->Initialize"; |
| 694 | ev->Release(); |
| 695 | hr = E_UNEXPECTED; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | return hr; |
| 700 | } |
| 701 | |
| 702 | // Helper function to execute a function on a script IDispatch interface. |
| 703 | HRESULT InvokeScriptFunction(const VARIANT& script_object, |
| 704 | const std::string& param) { |
[email protected] | d519b81 | 2013-12-25 18:15:40 | [diff] [blame] | 705 | base::win::ScopedVariant script_arg( |
| 706 | base::UTF8ToWide(param.c_str()).c_str()); |
[email protected] | a1800e8 | 2009-11-19 00:53:23 | [diff] [blame] | 707 | return InvokeScriptFunction(script_object, script_arg.AsInput()); |
| 708 | } |
| 709 | |
| 710 | HRESULT InvokeScriptFunction(const VARIANT& script_object, VARIANT* param) { |
| 711 | return InvokeScriptFunction(script_object, param, 1); |
| 712 | } |
| 713 | |
| 714 | HRESULT InvokeScriptFunction(const VARIANT& script_object, VARIANT* params, |
| 715 | int param_count) { |
[email protected] | 62bb18dc1 | 2009-11-25 01:34:08 | [diff] [blame] | 716 | DCHECK_GE(param_count, 0); |
[email protected] | a1800e8 | 2009-11-19 00:53:23 | [diff] [blame] | 717 | DCHECK(params); |
| 718 | |
[email protected] | 086f367d5 | 2010-04-23 21:01:50 | [diff] [blame] | 719 | if (V_VT(&script_object) != VT_DISPATCH || |
| 720 | script_object.pdispVal == NULL) { |
[email protected] | a1800e8 | 2009-11-19 00:53:23 | [diff] [blame] | 721 | return S_FALSE; |
| 722 | } |
| 723 | |
| 724 | CComPtr<IDispatch> script(script_object.pdispVal); |
| 725 | HRESULT hr = script.InvokeN(static_cast<DISPID>(DISPID_VALUE), |
| 726 | params, |
| 727 | param_count); |
| 728 | // 0x80020101 == SCRIPT_E_REPORTED. |
| 729 | // When the script we're invoking has an error, we get this error back. |
| 730 | DLOG_IF(ERROR, FAILED(hr) && hr != 0x80020101) << "Failed to invoke script"; |
| 731 | return hr; |
| 732 | } |
| 733 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 734 | // Gives the browser a chance to handle an accelerator that was |
| 735 | // sent to the out of proc chromium instance. |
| 736 | // Returns S_OK iff the accelerator was handled by the browser. |
| 737 | HRESULT AllowFrameToTranslateAccelerator(const MSG& msg) { |
[email protected] | 8441581 | 2010-05-19 23:12:42 | [diff] [blame] | 738 | static const int kMayTranslateAcceleratorOffset = 0x5c; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 739 | // Although IBrowserService2 is officially deprecated, it's still alive |
| 740 | // and well in IE7 and earlier. We have to use it here to correctly give |
| 741 | // the browser a chance to handle keyboard shortcuts. |
| 742 | // This happens automatically for activex components that have windows that |
| 743 | // belong to the current thread. In that circumstance IE owns the message |
| 744 | // loop and can walk the line of components allowing each participant the |
| 745 | // chance to handle the keystroke and eventually falls back to |
| 746 | // v_MayTranslateAccelerator. However in our case, the message loop is |
| 747 | // owned by the out-of-proc chromium instance so IE doesn't have a chance to |
| 748 | // fall back on its default behavior. Instead we give IE a chance to |
| 749 | // handle the shortcut here. |
[email protected] | 01dba67 | 2010-02-12 21:55:48 | [diff] [blame] | 750 | MSG accel_message = msg; |
| 751 | accel_message.hwnd = ::GetParent(m_hWnd); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 752 | HRESULT hr = S_FALSE; |
[email protected] | 965722ff | 2010-10-20 15:50:30 | [diff] [blame] | 753 | base::win::ScopedComPtr<IBrowserService2> bs2; |
[email protected] | 3eafbfb9 | 2010-08-02 16:54:22 | [diff] [blame] | 754 | |
| 755 | // For non-IE containers, we use the standard IOleInPlaceFrame contract |
| 756 | // (which IE does not support). For IE, we try to use IBrowserService2, |
| 757 | // but need special handling for IE8 (see below). |
| 758 | // |
| 759 | // We try to cache an IOleInPlaceFrame for our site. If we fail, we don't |
| 760 | // retry, and we fall back to the IBrowserService2 and PostMessage |
| 761 | // approaches below. |
| 762 | if (!in_place_frame_ && !failed_to_fetch_in_place_frame_) { |
[email protected] | 965722ff | 2010-10-20 15:50:30 | [diff] [blame] | 763 | base::win::ScopedComPtr<IOleInPlaceUIWindow> dummy_ui_window; |
[email protected] | 3eafbfb9 | 2010-08-02 16:54:22 | [diff] [blame] | 764 | RECT dummy_pos_rect = {0}; |
| 765 | RECT dummy_clip_rect = {0}; |
| 766 | OLEINPLACEFRAMEINFO dummy_frame_info = {0}; |
[email protected] | 58640fa | 2011-02-15 00:05:22 | [diff] [blame] | 767 | if (!m_spInPlaceSite || |
| 768 | FAILED(m_spInPlaceSite->GetWindowContext(in_place_frame_.Receive(), |
[email protected] | 3eafbfb9 | 2010-08-02 16:54:22 | [diff] [blame] | 769 | dummy_ui_window.Receive(), |
| 770 | &dummy_pos_rect, |
| 771 | &dummy_clip_rect, |
| 772 | &dummy_frame_info))) { |
| 773 | failed_to_fetch_in_place_frame_ = true; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | // The IBrowserService2 code below (second conditional) explicitly checks |
| 778 | // for whether the IBrowserService2::v_MayTranslateAccelerator function is |
| 779 | // valid. On IE8 there is one vtable ieframe!c_ImpostorBrowserService2Vtbl |
| 780 | // where this function entry is NULL which leads to a crash. We don't know |
| 781 | // under what circumstances this vtable is actually used though. |
| 782 | if (in_place_frame_) { |
| 783 | hr = in_place_frame_->TranslateAccelerator(&accel_message, 0); |
| 784 | } else if (S_OK == DoQueryService( |
| 785 | SID_STopLevelBrowser, m_spInPlaceSite, |
| 786 | bs2.Receive()) && bs2.get() && |
| 787 | *(*(reinterpret_cast<void***>(bs2.get())) + |
| 788 | kMayTranslateAcceleratorOffset)) { |
[email protected] | 01dba67 | 2010-02-12 21:55:48 | [diff] [blame] | 789 | hr = bs2->v_MayTranslateAccelerator(&accel_message); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 790 | } else { |
| 791 | // IE8 doesn't support IBrowserService2 unless you enable a special, |
| 792 | // undocumented flag with CoInternetSetFeatureEnabled and even then, |
| 793 | // the object you get back implements only a couple of methods of |
| 794 | // that interface... all the other entries in the vtable are NULL. |
| 795 | // In addition, the class that implements it is called |
| 796 | // ImpostorBrowserService2 :) |
| 797 | // IE8 does have a new interface though, presumably called |
| 798 | // ITabBrowserService or something that can be abbreviated to TBS. |
| 799 | // That interface has a method, TranslateAcceleratorTBS that does |
| 800 | // call the root MayTranslateAccelerator function, but alas the |
| 801 | // first argument to MayTranslateAccelerator is hard coded to FALSE |
| 802 | // which means that global accelerators are not handled and we're |
| 803 | // out of luck. |
| 804 | // A third thing that's notable with regards to IE8 is that |
| 805 | // none of the *MayTranslate* functions exist in a vtable inside |
| 806 | // ieframe.dll. I checked this by scanning for the address of |
| 807 | // those functions inside the dll and found none, which means that |
| 808 | // all calls to those functions are relative. |
[email protected] | 3eafbfb9 | 2010-08-02 16:54:22 | [diff] [blame] | 809 | // So, for IE8 in certain cases, and for other containers that may |
| 810 | // support neither IOleInPlaceFrame or IBrowserService2 our approach |
| 811 | // is very simple. Just post the message to our parent window and IE |
| 812 | // will pick it up if it's an accelerator. We won't know for sure if |
| 813 | // the browser handled the keystroke or not. |
[email protected] | 01dba67 | 2010-02-12 21:55:48 | [diff] [blame] | 814 | ::PostMessage(accel_message.hwnd, accel_message.message, |
| 815 | accel_message.wParam, accel_message.lParam); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | return hr; |
| 819 | } |
| 820 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 821 | protected: |
[email protected] | b1c5563861 | 2010-03-08 16:26:11 | [diff] [blame] | 822 | void HostNavigate(const GURL& url_to_open, |
| 823 | const GURL& referrer, int open_disposition) { |
[email protected] | 965722ff | 2010-10-20 15:50:30 | [diff] [blame] | 824 | base::win::ScopedComPtr<IWebBrowser2> web_browser2; |
[email protected] | b1c5563861 | 2010-03-08 16:26:11 | [diff] [blame] | 825 | DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive()); |
[email protected] | 8e33c8f528 | 2010-04-02 05:59:55 | [diff] [blame] | 826 | if (!web_browser2) { |
| 827 | NOTREACHED() << "Failed to retrieve IWebBrowser2 interface"; |
| 828 | return; |
| 829 | } |
[email protected] | 965722ff | 2010-10-20 15:50:30 | [diff] [blame] | 830 | base::win::ScopedVariant url; |
[email protected] | b1c5563861 | 2010-03-08 16:26:11 | [diff] [blame] | 831 | // Check to see if the URL uses a "view-source:" prefix, if so, open it |
| 832 | // using chrome frame full tab mode by using 'cf:' protocol handler. |
| 833 | // Also change the disposition to NEW_WINDOW since IE6 doesn't have tabs. |
| 834 | if (url_to_open.has_scheme() && |
[email protected] | dbdda540 | 2013-05-30 22:13:48 | [diff] [blame] | 835 | (url_to_open.SchemeIs(content::kViewSourceScheme) || |
[email protected] | b1c5563861 | 2010-03-08 16:26:11 | [diff] [blame] | 836 | url_to_open.SchemeIs(chrome::kAboutScheme))) { |
| 837 | std::wstring chrome_url; |
| 838 | chrome_url.append(kChromeProtocolPrefix); |
[email protected] | d519b81 | 2013-12-25 18:15:40 | [diff] [blame] | 839 | chrome_url.append(base::UTF8ToWide(url_to_open.spec())); |
[email protected] | b1c5563861 | 2010-03-08 16:26:11 | [diff] [blame] | 840 | url.Set(chrome_url.c_str()); |
| 841 | open_disposition = NEW_WINDOW; |
| 842 | } else { |
[email protected] | d519b81 | 2013-12-25 18:15:40 | [diff] [blame] | 843 | url.Set(base::UTF8ToWide(url_to_open.spec()).c_str()); |
[email protected] | b1c5563861 | 2010-03-08 16:26:11 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | VARIANT flags = { VT_I4 }; |
| 847 | V_I4(&flags) = 0; |
| 848 | |
| 849 | IEVersion ie_version = GetIEVersion(); |
| 850 | DCHECK(ie_version != NON_IE && ie_version != IE_UNSUPPORTED); |
| 851 | // Since IE6 doesn't support tabs, so we just use window instead. |
| 852 | if (ie_version == IE_6) { |
| 853 | if (open_disposition == NEW_FOREGROUND_TAB || |
| 854 | open_disposition == NEW_BACKGROUND_TAB || |
| 855 | open_disposition == NEW_WINDOW || |
| 856 | open_disposition == NEW_POPUP) { |
| 857 | V_I4(&flags) = navOpenInNewWindow; |
| 858 | } else if (open_disposition != CURRENT_TAB) { |
| 859 | NOTREACHED() << "Unsupported open disposition in IE6"; |
| 860 | } |
| 861 | } else { |
| 862 | switch (open_disposition) { |
| 863 | case NEW_FOREGROUND_TAB: |
| 864 | V_I4(&flags) = navOpenInNewTab; |
| 865 | break; |
| 866 | case NEW_BACKGROUND_TAB: |
| 867 | V_I4(&flags) = navOpenInBackgroundTab; |
| 868 | break; |
| 869 | case NEW_WINDOW: |
| 870 | case NEW_POPUP: |
| 871 | V_I4(&flags) = navOpenInNewWindow; |
| 872 | break; |
| 873 | default: |
| 874 | break; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | // TODO(sanjeevr): The navOpenInNewWindow flag causes IE to open this |
| 879 | // in a new window ONLY if the user has specified |
| 880 | // "Always open popups in a new window". Otherwise it opens in a new tab. |
| 881 | // We need to investigate more and see if we can force IE to display the |
| 882 | // link in a new window. MSHTML uses the below code to force an open in a |
| 883 | // new window. But this logic also fails for us. Perhaps this flag is not |
| 884 | // honoured if the ActiveDoc is not MSHTML. |
| 885 | // Even the HLNF_DISABLEWINDOWRESTRICTIONS flag did not work. |
| 886 | // Start of MSHTML-like logic. |
| 887 | // CComQIPtr<ITargetFramePriv2> target_frame = web_browser2; |
| 888 | // if (target_frame) { |
| 889 | // CComPtr<IUri> uri; |
[email protected] | d519b81 | 2013-12-25 18:15:40 | [diff] [blame] | 890 | // CreateUri(base::UTF8ToWide(open_url_command->url_.spec()).c_str(), |
[email protected] | b1c5563861 | 2010-03-08 16:26:11 | [diff] [blame] | 891 | // Uri_CREATE_IE_SETTINGS, 0, &uri); |
| 892 | // CComPtr<IBindCtx> bind_ctx; |
| 893 | // CreateBindCtx(0, &bind_ctx); |
| 894 | // target_frame->AggregatedNavigation2( |
| 895 | // HLNF_TRUSTFIRSTDOWNLOAD|HLNF_OPENINNEWWINDOW, bind_ctx, NULL, |
| 896 | // L"No_Name", uri, L""); |
| 897 | // } |
| 898 | // End of MSHTML-like logic |
[email protected] | 965722ff | 2010-10-20 15:50:30 | [diff] [blame] | 899 | VARIANT empty = base::win::ScopedVariant::kEmptyVariant; |
| 900 | base::win::ScopedVariant http_headers; |
[email protected] | b1c5563861 | 2010-03-08 16:26:11 | [diff] [blame] | 901 | |
| 902 | if (referrer.is_valid()) { |
| 903 | std::wstring referrer_header = L"Referer: "; |
[email protected] | d519b81 | 2013-12-25 18:15:40 | [diff] [blame] | 904 | referrer_header += base::UTF8ToWide(referrer.spec()); |
[email protected] | b1c5563861 | 2010-03-08 16:26:11 | [diff] [blame] | 905 | referrer_header += L"\r\n\r\n"; |
| 906 | http_headers.Set(referrer_header.c_str()); |
| 907 | } |
| 908 | |
[email protected] | 74b1eaf54 | 2011-10-05 19:34:27 | [diff] [blame] | 909 | // IE6 does not support tabs. If Chrome sent us a window open request |
| 910 | // indicating that the navigation needs to occur in a foreground tab or |
| 911 | // a popup window, then we need to ensure that the new window in IE6 is |
| 912 | // brought to the foreground. |
| 913 | if (ie_version == IE_6) { |
| 914 | ChromeFrameUrl cf_url; |
| 915 | cf_url.Parse(static_cast<BSTR>(url_)); |
| 916 | |
| 917 | if (cf_url.attach_to_external_tab() && |
| 918 | (cf_url.disposition() == NEW_FOREGROUND_TAB || |
| 919 | cf_url.disposition() == NEW_POPUP)) { |
| 920 | BringWebBrowserWindowToTop(web_browser2); |
| 921 | } |
| 922 | } |
| 923 | |
[email protected] | 52622ca1 | 2011-01-06 22:53:06 | [diff] [blame] | 924 | HRESULT hr = web_browser2->Navigate2(url.AsInput(), &flags, &empty, &empty, |
| 925 | http_headers.AsInput()); |
| 926 | // If the current window is a popup window then attempting to open a new |
| 927 | // tab for the navigation will fail. We attempt to issue the navigation in |
| 928 | // a new window in this case. |
| 929 | // http://msdn.microsoft.com/en-us/library/aa752133(v=vs.85).aspx |
| 930 | if (FAILED(hr) && V_I4(&flags) != navOpenInNewWindow) { |
| 931 | V_I4(&flags) = navOpenInNewWindow; |
| 932 | hr = web_browser2->Navigate2(url.AsInput(), &flags, &empty, &empty, |
| 933 | http_headers.AsInput()); |
[email protected] | 52622ca1 | 2011-01-06 22:53:06 | [diff] [blame] | 934 | DLOG_IF(ERROR, FAILED(hr)) |
| 935 | << "Navigate2 failed with error: " |
| 936 | << base::StringPrintf("0x%08X", hr); |
| 937 | } |
[email protected] | b1c5563861 | 2010-03-08 16:26:11 | [diff] [blame] | 938 | } |
| 939 | |
[email protected] | e1081d9 | 2010-09-10 20:29:11 | [diff] [blame] | 940 | void InitializeAutomationSettings() { |
| 941 | static const wchar_t kHandleTopLevelRequests[] = L"HandleTopLevelRequests"; |
| 942 | static const wchar_t kUseChromeNetworking[] = L"UseChromeNetworking"; |
| 943 | |
| 944 | // Query and assign the top-level-request routing, and host networking |
| 945 | // settings from the registry. |
| 946 | bool top_level_requests = GetConfigBool(true, kHandleTopLevelRequests); |
| 947 | bool chrome_network = GetConfigBool(false, kUseChromeNetworking); |
| 948 | automation_client_->set_handle_top_level_requests(top_level_requests); |
| 949 | automation_client_->set_use_chrome_network(chrome_network); |
| 950 | } |
| 951 | |
[email protected] | 965722ff | 2010-10-20 15:50:30 | [diff] [blame] | 952 | base::win::ScopedBstr url_; |
| 953 | base::win::ScopedComPtr<IOleDocumentSite> doc_site_; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 954 | |
[email protected] | 3eafbfb9 | 2010-08-02 16:54:22 | [diff] [blame] | 955 | // If false, we tried but failed to fetch an IOleInPlaceFrame from our host. |
| 956 | // Cached here so we don't try to fetch it every time if we keep failing. |
| 957 | bool failed_to_fetch_in_place_frame_; |
[email protected] | bbfa9a1 | 2010-08-10 14:09:37 | [diff] [blame] | 958 | bool draw_sad_tab_; |
| 959 | |
[email protected] | 965722ff | 2010-10-20 15:50:30 | [diff] [blame] | 960 | base::win::ScopedComPtr<IOleInPlaceFrame> in_place_frame_; |
[email protected] | 3eafbfb9 | 2010-08-02 16:54:22 | [diff] [blame] | 961 | |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 962 | // For more information on the ready_state_ property see: |
| 963 | // http://msdn.microsoft.com/en-us/library/aa768179(VS.85).aspx# |
| 964 | READYSTATE ready_state_; |
| 965 | |
| 966 | // The following members contain IDispatch interfaces representing the |
| 967 | // onload/onerror/onmessage handlers on the page. |
[email protected] | 965722ff | 2010-10-20 15:50:30 | [diff] [blame] | 968 | base::win::ScopedVariant onload_handler_; |
| 969 | base::win::ScopedVariant onerror_handler_; |
| 970 | base::win::ScopedVariant onmessage_handler_; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 971 | |
| 972 | EventHandlers onmessage_; |
| 973 | EventHandlers onloaderror_; |
| 974 | EventHandlers onload_; |
| 975 | EventHandlers onreadystatechanged_; |
| 976 | EventHandlers onprivatemessage_; |
[email protected] | 00f6b77 | 2009-10-23 17:03:41 | [diff] [blame] | 977 | EventHandlers onextensionready_; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 978 | |
[email protected] | 3eb07da | 2010-02-01 19:48:36 | [diff] [blame] | 979 | // Handle network requests when host network stack is used. Passed to the |
| 980 | // automation client on initialization. |
[email protected] | bbfa9a1 | 2010-08-10 14:09:37 | [diff] [blame] | 981 | scoped_ptr<UrlmonUrlRequestManager> url_fetcher_; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 982 | }; |
| 983 | |
| 984 | #endif // CHROME_FRAME_CHROME_FRAME_ACTIVEX_BASE_H_ |