blob: 60919a1962791411efc02a44a67123c7310ad653 [file] [log] [blame]
[email protected]aa84a7e2012-03-15 21:29:061// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]f7817822009-09-24 05:11:582// 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]93b792792011-06-07 23:00:0111#include <exdisp.h>
[email protected]ea9ed97d2010-01-05 19:16:2312#include <wininet.h>
[email protected]f7817822009-09-24 05:11:5813#include <shdeprecated.h> // for IBrowserService2
14#include <shlguid.h>
15
16#include <set>
17#include <string>
[email protected]efd4dfc22010-03-26 20:14:4018#include <vector>
[email protected]f7817822009-09-24 05:11:5819
[email protected]835d7c82010-10-14 04:38:3820#include "base/metrics/histogram.h"
[email protected]f7817822009-09-24 05:11:5821#include "base/string_util.h"
[email protected]d3451d832010-10-01 11:17:3722#include "base/stringprintf.h"
[email protected]252cad62010-08-18 18:33:5723#include "base/utf_string_conversions.h"
[email protected]965722ff2010-10-20 15:50:3024#include "base/win/scoped_bstr.h"
25#include "base/win/scoped_comptr.h"
26#include "base/win/scoped_variant.h"
[email protected]f7817822009-09-24 05:11:5827#include "grit/chrome_frame_resources.h"
[email protected]7b15c0b92011-05-24 00:02:3528#include "chrome/app/chrome_command_ids.h"
[email protected]bc73b4e52010-03-26 04:16:2029#include "chrome/common/url_constants.h"
[email protected]f7817822009-09-24 05:11:5830#include "chrome_frame/chrome_frame_plugin.h"
[email protected]8ca280e2011-10-19 15:37:3331#include "chrome_frame/chrome_tab.h"
[email protected]a1800e82009-11-19 00:53:2332#include "chrome_frame/com_message_event.h"
[email protected]f7817822009-09-24 05:11:5833#include "chrome_frame/com_type_info_holder.h"
[email protected]3c6f8e12010-03-24 21:58:2134#include "chrome_frame/simple_resource_loader.h"
[email protected]f7817822009-09-24 05:11:5835#include "chrome_frame/urlmon_url_request.h"
[email protected]7ae80742010-03-25 22:02:2736#include "chrome_frame/urlmon_url_request_private.h"
[email protected]bc73b4e52010-03-26 04:16:2037#include "chrome_frame/utils.h"
[email protected]35f13ab2009-12-16 23:59:1738#include "grit/generated_resources.h"
[email protected]aa84a7e2012-03-15 21:29:0639#include "net/cookies/cookie_monster.h"
[email protected]f7817822009-09-24 05:11:5840
[email protected]f7817822009-09-24 05:11:5841// Connection point class to support firing IChromeFrameEvents (dispinterface).
42template<class T>
43class 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]efd4dfc22010-03-26 20:14:4049 // 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]965722ff2010-10-20 15:50:3054 std::vector< base::win::ScopedComPtr<IUnknown> > sink_array(
55 m_vec.GetSize());
[email protected]efd4dfc22010-03-26 20:14:4056 for (int connection = 0; connection < m_vec.GetSize(); ++connection)
57 sink_array[connection] = m_vec.GetAt(connection);
58 me->Unlock();
[email protected]f7817822009-09-24 05:11:5859
[email protected]efd4dfc22010-03-26 20:14:4060 for (size_t sink = 0; sink < sink_array.size(); ++sink) {
61 DIChromeFrameEvents* events =
62 static_cast<DIChromeFrameEvents*>(sink_array[sink].get());
[email protected]f7817822009-09-24 05:11:5863 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]d3451d832010-10-01 11:17:3774 base::StringPrintf("0x%08X", hr);
[email protected]f7817822009-09-24 05:11:5875 }
76 }
77 }
78
79 void FireMethodWithParam(ChromeFrameEventDispId dispid,
80 const VARIANT& param) {
81 FireMethodWithParams(dispid, &param, 1);
82 }
83
84 void Fire_onload(IDispatch* event) {
85 VARIANT var = { VT_DISPATCH };
86 var.pdispVal = event;
87 FireMethodWithParam(CF_EVENT_DISPID_ONLOAD, var);
88 }
89
90 void Fire_onloaderror(IDispatch* event) {
91 VARIANT var = { VT_DISPATCH };
92 var.pdispVal = event;
93 FireMethodWithParam(CF_EVENT_DISPID_ONLOADERROR, var);
94 }
95
96 void Fire_onmessage(IDispatch* event) {
97 VARIANT var = { VT_DISPATCH };
98 var.pdispVal = event;
99 FireMethodWithParam(CF_EVENT_DISPID_ONMESSAGE, var);
100 }
101
[email protected]62bb18dc12009-11-25 01:34:08102 void Fire_onreadystatechanged(long readystate) { // NOLINT
[email protected]f7817822009-09-24 05:11:58103 VARIANT var = { VT_I4 };
104 var.lVal = readystate;
105 FireMethodWithParam(CF_EVENT_DISPID_ONREADYSTATECHANGED, var);
106 }
107
108 void Fire_onprivatemessage(IDispatch* event, BSTR target) {
109 // Arguments in reverse order to the function declaration, because
110 // that's what DISPPARAMS requires.
111 VARIANT args[2] = { { VT_BSTR, }, {VT_DISPATCH, } };
112 args[0].bstrVal = target;
113 args[1].pdispVal = event;
114
115 FireMethodWithParams(CF_EVENT_DISPID_ONPRIVATEMESSAGE,
116 args,
117 arraysize(args));
118 }
[email protected]00f6b772009-10-23 17:03:41119
[email protected]efd4dfc22010-03-26 20:14:40120 void Fire_onchannelerror() { // NOLINT
121 FireMethodWithParams(CF_EVENT_DISPID_ONCHANNELERROR, NULL, 0);
122 }
[email protected]2f1793ea2010-10-27 17:32:51123
124 void Fire_onclose() { // NOLINT
125 FireMethodWithParams(CF_EVENT_DISPID_ONCLOSE, NULL, 0);
126 }
[email protected]f7817822009-09-24 05:11:58127};
128
129extern bool g_first_launch_by_process_;
130
[email protected]a22f7e02011-02-09 07:15:35131namespace chrome_frame {
132// Implemented outside this file so that the header doesn't include
133// automation_messages.h.
134std::string ActiveXCreateUrl(const GURL& parsed_url,
135 const AttachExternalTabParams& params);
136int GetDisposition(const AttachExternalTabParams& params);
137void GetMiniContextMenuData(UINT cmd,
138 const MiniContextMenuParams& params,
139 GURL* referrer,
140 GURL* url);
141} // namespace chrome_frame
142
[email protected]f7817822009-09-24 05:11:58143// Common implementation for ActiveX and Active Document
144template <class T, const CLSID& class_id>
[email protected]62bb18dc12009-11-25 01:34:08145class ATL_NO_VTABLE ChromeFrameActivexBase : // NOLINT
[email protected]b0febbf2009-11-12 17:49:35146 public CComObjectRootEx<CComMultiThreadModel>,
[email protected]f7817822009-09-24 05:11:58147 public IOleControlImpl<T>,
148 public IOleObjectImpl<T>,
149 public IOleInPlaceActiveObjectImpl<T>,
150 public IViewObjectExImpl<T>,
151 public IOleInPlaceObjectWindowlessImpl<T>,
152 public ISupportErrorInfo,
153 public IQuickActivateImpl<T>,
154 public com_util::IProvideClassInfo2Impl<class_id,
155 DIID_DIChromeFrameEvents>,
156 public com_util::IDispatchImpl<IChromeFrame>,
157 public IConnectionPointContainerImpl<T>,
158 public ProxyDIChromeFrameEvents<T>,
159 public IPropertyNotifySinkCP<T>,
160 public CComCoClass<T, &class_id>,
161 public CComControl<T>,
[email protected]0753db592010-12-15 14:45:05162 public ChromeFramePlugin<T> {
[email protected]f7817822009-09-24 05:11:58163 protected:
[email protected]965722ff2010-10-20 15:50:30164 typedef std::set<base::win::ScopedComPtr<IDispatch> > EventHandlers;
[email protected]97965e12010-04-09 00:51:10165 typedef ChromeFrameActivexBase<T, class_id> BasePlugin;
[email protected]f7817822009-09-24 05:11:58166
167 public:
168 ChromeFrameActivexBase()
[email protected]3eafbfb92010-08-02 16:54:22169 : ready_state_(READYSTATE_UNINITIALIZED),
[email protected]bbfa9a12010-08-10 14:09:37170 url_fetcher_(new UrlmonUrlRequestManager()),
171 failed_to_fetch_in_place_frame_(false),
[email protected]6e58a952011-01-12 19:28:50172 draw_sad_tab_(false) {
[email protected]f7817822009-09-24 05:11:58173 m_bWindowOnly = TRUE;
[email protected]bbfa9a12010-08-10 14:09:37174 url_fetcher_->set_container(static_cast<IDispatch*>(this));
[email protected]f7817822009-09-24 05:11:58175 }
176
177 ~ChromeFrameActivexBase() {
[email protected]bbfa9a12010-08-10 14:09:37178 url_fetcher_->set_container(NULL);
[email protected]f7817822009-09-24 05:11:58179 }
180
181DECLARE_OLEMISC_STATUS(OLEMISC_RECOMPOSEONRESIZE | OLEMISC_CANTLINKINSIDE |
182 OLEMISC_INSIDEOUT | OLEMISC_ACTIVATEWHENVISIBLE |
183 OLEMISC_SETCLIENTSITEFIRST)
184
185DECLARE_NOT_AGGREGATABLE(T)
186
187BEGIN_COM_MAP(ChromeFrameActivexBase)
188 COM_INTERFACE_ENTRY(IChromeFrame)
189 COM_INTERFACE_ENTRY(IDispatch)
190 COM_INTERFACE_ENTRY(IViewObjectEx)
191 COM_INTERFACE_ENTRY(IViewObject2)
192 COM_INTERFACE_ENTRY(IViewObject)
193 COM_INTERFACE_ENTRY(IOleInPlaceObjectWindowless)
194 COM_INTERFACE_ENTRY(IOleInPlaceObject)
195 COM_INTERFACE_ENTRY2(IOleWindow, IOleInPlaceObjectWindowless)
196 COM_INTERFACE_ENTRY(IOleInPlaceActiveObject)
197 COM_INTERFACE_ENTRY(IOleControl)
198 COM_INTERFACE_ENTRY(IOleObject)
199 COM_INTERFACE_ENTRY(ISupportErrorInfo)
200 COM_INTERFACE_ENTRY(IQuickActivate)
201 COM_INTERFACE_ENTRY(IProvideClassInfo)
202 COM_INTERFACE_ENTRY(IProvideClassInfo2)
[email protected]bbfa9a12010-08-10 14:09:37203 COM_INTERFACE_ENTRY(IConnectionPointContainer)
[email protected]f7817822009-09-24 05:11:58204 COM_INTERFACE_ENTRY_FUNC_BLIND(0, InterfaceNotSupported)
205END_COM_MAP()
206
207BEGIN_CONNECTION_POINT_MAP(T)
208 CONNECTION_POINT_ENTRY(IID_IPropertyNotifySink)
209 CONNECTION_POINT_ENTRY(DIID_DIChromeFrameEvents)
210END_CONNECTION_POINT_MAP()
211
212BEGIN_MSG_MAP(ChromeFrameActivexBase)
213 MESSAGE_HANDLER(WM_CREATE, OnCreate)
[email protected]7ae80742010-03-25 22:02:27214 MESSAGE_HANDLER(WM_DOWNLOAD_IN_HOST, OnDownloadRequestInHost)
[email protected]e9eb50c2009-10-24 16:26:46215 MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
[email protected]f7817822009-09-24 05:11:58216 CHAIN_MSG_MAP(ChromeFramePlugin<T>)
217 CHAIN_MSG_MAP(CComControl<T>)
218 DEFAULT_REFLECTION_HANDLER()
219END_MSG_MAP()
220
221 // IViewObjectEx
222 DECLARE_VIEW_STATUS(VIEWSTATUS_SOLIDBKGND | VIEWSTATUS_OPAQUE)
223
[email protected]48e6bb62009-09-30 20:32:46224 inline HRESULT IViewObject_Draw(DWORD draw_aspect, LONG index,
225 void* aspect_info, DVTARGETDEVICE* ptd, HDC info_dc, HDC dc,
226 LPCRECTL bounds, LPCRECTL win_bounds) {
227 // ATL ASSERTs if dwDrawAspect is DVASPECT_DOCPRINT, so we cheat.
228 DWORD aspect = draw_aspect;
229 if (aspect == DVASPECT_DOCPRINT)
230 aspect = DVASPECT_CONTENT;
231
232 return CComControl<T>::IViewObject_Draw(aspect, index, aspect_info, ptd,
233 info_dc, dc, bounds, win_bounds);
234 }
[email protected]f7817822009-09-24 05:11:58235
236 DECLARE_PROTECT_FINAL_CONSTRUCT()
237
[email protected]3eb8a70492010-12-03 21:32:19238 void SetResourceModule() {
[email protected]8e8bb6d2010-12-13 08:18:55239 SimpleResourceLoader* loader_instance = SimpleResourceLoader::GetInstance();
[email protected]3c6f8e12010-03-24 21:58:21240 DCHECK(loader_instance);
[email protected]6ae3d492010-10-20 14:01:21241 HMODULE res_dll = loader_instance->GetResourceModuleHandle();
[email protected]3eb8a70492010-12-03 21:32:19242 _AtlBaseModule.SetResourceInstance(res_dll);
[email protected]3c6f8e12010-03-24 21:58:21243 }
244
[email protected]f7817822009-09-24 05:11:58245 HRESULT FinalConstruct() {
[email protected]3c6f8e12010-03-24 21:58:21246 SetResourceModule();
247
[email protected]f7817822009-09-24 05:11:58248 if (!Initialize())
249 return E_OUTOFMEMORY;
250
251 // Set to true if this is the first launch by this process.
252 // Used to perform one time tasks.
253 if (g_first_launch_by_process_) {
254 g_first_launch_by_process_ = false;
[email protected]92c79d32011-04-06 18:41:15255 UMA_HISTOGRAM_CUSTOM_COUNTS("ChromeFrame.IEVersion",
256 GetIEVersion(),
257 IE_INVALID,
[email protected]5ba4b8a92011-09-29 05:09:42258 IE_10,
259 IE_10 + 1);
[email protected]f7817822009-09-24 05:11:58260 }
[email protected]8103c7f2010-09-08 22:36:09261
[email protected]f7817822009-09-24 05:11:58262 return S_OK;
263 }
264
265 void FinalRelease() {
[email protected]89f19542010-01-05 21:55:11266 Uninitialize();
[email protected]f7817822009-09-24 05:11:58267 }
268
[email protected]bbfa9a12010-08-10 14:09:37269 void ResetUrlRequestManager() {
270 url_fetcher_.reset(new UrlmonUrlRequestManager());
271 }
272
[email protected]f7817822009-09-24 05:11:58273 static HRESULT WINAPI InterfaceNotSupported(void* pv, REFIID riid, void** ppv,
274 DWORD dw) {
275#ifndef NDEBUG
276 wchar_t buffer[64] = {0};
277 ::StringFromGUID2(riid, buffer, arraysize(buffer));
[email protected]2b9a9f162010-10-19 20:30:45278 DVLOG(1) << "E_NOINTERFACE: " << buffer;
[email protected]f7817822009-09-24 05:11:58279#endif
280 return E_NOINTERFACE;
281 }
282
283 // ISupportsErrorInfo
284 STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid) {
285 static const IID* interfaces[] = {
286 &IID_IChromeFrame,
287 &IID_IDispatch
288 };
289
290 for (int i = 0; i < arraysize(interfaces); ++i) {
291 if (InlineIsEqualGUID(*interfaces[i], riid))
292 return S_OK;
293 }
294 return S_FALSE;
295 }
296
297 // Called to draw our control when chrome hasn't been initialized.
[email protected]62bb18dc12009-11-25 01:34:08298 virtual HRESULT OnDraw(ATL_DRAWINFO& draw_info) { // NOLINT
[email protected]f7817822009-09-24 05:11:58299 if (NULL == draw_info.prcBounds) {
300 NOTREACHED();
301 return E_FAIL;
302 }
[email protected]bbfa9a12010-08-10 14:09:37303
304 if (draw_sad_tab_) {
305 // TODO(tommi): Draw a proper sad tab.
306 RECT rc = {0};
307 if (draw_info.prcBounds) {
308 rc.top = draw_info.prcBounds->top;
309 rc.bottom = draw_info.prcBounds->bottom;
310 rc.left = draw_info.prcBounds->left;
311 rc.right = draw_info.prcBounds->right;
312 } else {
313 GetClientRect(&rc);
314 }
315 ::DrawTextA(draw_info.hdcDraw, ":-(", -1, &rc,
316 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
317 } else {
318 // Don't draw anything.
319 }
[email protected]f7817822009-09-24 05:11:58320 return S_OK;
321 }
322
[email protected]f7817822009-09-24 05:11:58323 // Used to setup the document_url_ member needed for completing navigation.
324 // Create external tab (possibly in incognito mode).
325 HRESULT IOleObject_SetClientSite(IOleClientSite* client_site) {
326 // If we currently have a document site pointer, release it.
327 doc_site_.Release();
328 if (client_site) {
329 doc_site_.QueryFrom(client_site);
330 }
331
[email protected]3eafbfb92010-08-02 16:54:22332 if (client_site == NULL) {
333 in_place_frame_.Release();
334 }
335
[email protected]f7817822009-09-24 05:11:58336 return CComControlBase::IOleObject_SetClientSite(client_site);
337 }
338
[email protected]f5494d42010-12-23 22:15:34339 bool HandleContextMenuCommand(UINT cmd, const MiniContextMenuParams& params) {
[email protected]f7817822009-09-24 05:11:58340 if (cmd == IDC_ABOUT_CHROME_FRAME) {
341 int tab_handle = automation_client_->tab()->handle();
[email protected]b1c55638612010-03-08 16:26:11342 HostNavigate(GURL("about:version"), GURL(), NEW_WINDOW);
[email protected]f7817822009-09-24 05:11:58343 return true;
[email protected]35f13ab2009-12-16 23:59:17344 } else {
345 switch (cmd) {
346 case IDS_CONTENT_CONTEXT_SAVEAUDIOAS:
347 case IDS_CONTENT_CONTEXT_SAVEVIDEOAS:
348 case IDS_CONTENT_CONTEXT_SAVEIMAGEAS:
349 case IDS_CONTENT_CONTEXT_SAVELINKAS: {
[email protected]a22f7e02011-02-09 07:15:35350 GURL referrer, url;
351 chrome_frame::GetMiniContextMenuData(cmd, params, &referrer, &url);
[email protected]35f13ab2009-12-16 23:59:17352 DoFileDownloadInIE(UTF8ToWide(url.spec()).c_str());
353 return true;
354 }
[email protected]7b15c0b92011-05-24 00:02:35355
356 case IDC_PRINT: {
357 automation_client_->PrintTab();
[email protected]df82d9b2011-06-16 23:45:58358 return true;
[email protected]7b15c0b92011-05-24 00:02:35359 }
[email protected]35f13ab2009-12-16 23:59:17360 }
[email protected]f7817822009-09-24 05:11:58361 }
362
363 return false;
364 }
365
[email protected]10bf1a72009-10-01 16:00:21366 // Should connections initiated by this class try to block
367 // responses served with the X-Frame-Options header?
368 // ActiveX controls genereally will want to do this,
369 // returning true, while true top-level documents
370 // (ActiveDocument servers) will not. Your specialization
371 // of this template should implement this method based on how
372 // it "feels" from a security perspective. If it's hosted in another
[email protected]f7817822009-09-24 05:11:58373 // scriptable document, return true, else false.
[email protected]2ce57c782009-11-17 18:15:40374 //
375 // The base implementation returns true unless we are in privileged
376 // mode, in which case we always trust our container so we return false.
[email protected]5778de6e2009-10-24 01:29:20377 bool is_frame_busting_enabled() const {
[email protected]0753db592010-12-15 14:45:05378 return !is_privileged();
[email protected]f7817822009-09-24 05:11:58379 }
380
[email protected]74b1eaf542011-10-05 19:34:27381 static void BringWebBrowserWindowToTop(IWebBrowser2* web_browser2) {
382 DCHECK(web_browser2);
383 if (web_browser2) {
384 web_browser2->put_Visible(VARIANT_TRUE);
385 HWND ie_window = NULL;
386 web_browser2->get_HWND(reinterpret_cast<long*>(&ie_window));
387 ::BringWindowToTop(ie_window);
388 }
389 }
390
[email protected]f7817822009-09-24 05:11:58391 protected:
[email protected]bc73b4e52010-03-26 04:16:20392 virtual void GetProfilePath(const std::wstring& profile_name,
393 FilePath* profile_path) {
394 bool is_IE = (lstrcmpi(profile_name.c_str(), kIexploreProfileName) == 0) ||
395 (lstrcmpi(profile_name.c_str(), kRundllProfileName) == 0);
396 // Browsers without IDeleteBrowsingHistory in non-priv mode
397 // have their profiles moved into "Temporary Internet Files".
[email protected]d9ca776c2011-01-31 21:44:26398 if (is_IE && GetIEVersion() < IE_8) {
[email protected]bc73b4e52010-03-26 04:16:20399 *profile_path = GetIETemporaryFilesFolder();
400 *profile_path = profile_path->Append(L"Google Chrome Frame");
401 } else {
402 ChromeFramePlugin::GetProfilePath(profile_name, profile_path);
403 }
[email protected]2b9a9f162010-10-19 20:30:45404 DVLOG(1) << __FUNCTION__ << ": " << profile_path->value();
[email protected]bc73b4e52010-03-26 04:16:20405 }
406
[email protected]f5494d42010-12-23 22:15:34407 void OnLoad(const GURL& url) {
[email protected]a1800e82009-11-19 00:53:23408 if (ready_state_ < READYSTATE_COMPLETE) {
409 ready_state_ = READYSTATE_COMPLETE;
410 FireOnChanged(DISPID_READYSTATE);
411 }
412
413 HRESULT hr = InvokeScriptFunction(onload_handler_, url.spec());
414 }
415
416 void OnLoadFailed(int error_code, const std::string& url) {
417 HRESULT hr = InvokeScriptFunction(onerror_handler_, url);
418 }
419
[email protected]f5494d42010-12-23 22:15:34420 void OnMessageFromChromeFrame(const std::string& message,
[email protected]a1800e82009-11-19 00:53:23421 const std::string& origin,
422 const std::string& target) {
[email protected]965722ff2010-10-20 15:50:30423 base::win::ScopedComPtr<IDispatch> message_event;
[email protected]a1800e82009-11-19 00:53:23424 if (SUCCEEDED(CreateDomEvent("message", message, origin,
425 message_event.Receive()))) {
[email protected]965722ff2010-10-20 15:50:30426 base::win::ScopedVariant event_var;
[email protected]a1800e82009-11-19 00:53:23427 event_var.Set(static_cast<IDispatch*>(message_event));
428 InvokeScriptFunction(onmessage_handler_, event_var.AsInput());
429 }
430 }
431
[email protected]f5494d42010-12-23 22:15:34432 virtual void OnTabbedOut(bool reverse) {
[email protected]f7817822009-09-24 05:11:58433 DCHECK(m_bInPlaceActive);
434
435 HWND parent = ::GetParent(m_hWnd);
436 ::SetFocus(parent);
[email protected]965722ff2010-10-20 15:50:30437 base::win::ScopedComPtr<IOleControlSite> control_site;
[email protected]f7817822009-09-24 05:11:58438 control_site.QueryFrom(m_spClientSite);
439 if (control_site)
440 control_site->OnFocus(FALSE);
441 }
442
[email protected]f5494d42010-12-23 22:15:34443 virtual void OnOpenURL(const GURL& url_to_open,
[email protected]b36a9f92009-10-19 17:34:57444 const GURL& referrer, int open_disposition) {
[email protected]b1c55638612010-03-08 16:26:11445 HostNavigate(url_to_open, referrer, open_disposition);
[email protected]f7817822009-09-24 05:11:58446 }
447
[email protected]7ae80742010-03-25 22:02:27448 // Called when Chrome has decided that a request needs to be treated as a
449 // download. The caller will be the UrlRequest worker thread.
450 // The worker thread will block while we process the request and take
451 // ownership of the request object.
452 // There's room for improvement here and also see todo below.
453 LPARAM OnDownloadRequestInHost(UINT message, WPARAM wparam, LPARAM lparam,
454 BOOL& handled) {
[email protected]93b792792011-06-07 23:00:01455 ChromeFrameUrl cf_url;
456 cf_url.Parse(UTF8ToWide(GetDocumentUrl()));
457
458 // Always issue the download request in a new window to ensure that the
459 // currently loaded ChromeFrame document does not inadvartently see an
460 // unload request. This runs javascript unload handlers on the page which
461 // renders the page non functional.
462 VARIANT flags = { VT_I4 };
463 V_I4(&flags) = navNoHistory;
464 if (!cf_url.attach_to_external_tab())
465 V_I4(&flags) |= navOpenInNewWindow;
466
[email protected]f4fbae682011-02-28 22:18:33467 DownloadInHostParams* download_params =
468 reinterpret_cast<DownloadInHostParams*>(wparam);
469 DCHECK(download_params);
[email protected]7ae80742010-03-25 22:02:27470 // TODO(tommi): It looks like we might have to switch the request object
471 // into a pass-through request object and serve up any thus far received
472 // content and headers to IE in order to prevent what can currently happen
473 // which is reissuing requests and turning POST into GET.
[email protected]f4fbae682011-02-28 22:18:33474 if (download_params->moniker) {
475 NavigateBrowserToMoniker(
476 doc_site_, download_params->moniker,
477 UTF8ToWide(download_params->request_headers).c_str(),
[email protected]93b792792011-06-07 23:00:01478 download_params->bind_ctx, NULL, download_params->post_data,
479 &flags);
[email protected]62bb18dc12009-11-25 01:34:08480 }
[email protected]7af08f62011-03-11 00:03:55481 delete download_params;
[email protected]7ae80742010-03-25 22:02:27482 return TRUE;
[email protected]f7817822009-09-24 05:11:58483 }
484
[email protected]f5494d42010-12-23 22:15:34485 virtual void OnAttachExternalTab(const AttachExternalTabParams& params) {
[email protected]74b1eaf542011-10-05 19:34:27486 GURL current_url(static_cast<BSTR>(url_));
487 std::string url = chrome_frame::ActiveXCreateUrl(current_url, params);
488 // Pass the current document url as the referrer for the new navigation.
489 HostNavigate(GURL(url), current_url, chrome_frame::GetDisposition(params));
[email protected]f7817822009-09-24 05:11:58490 }
491
[email protected]b516e2d2011-07-12 16:54:12492 virtual void OnHandleContextMenu(const ContextMenuModel& menu_model,
[email protected]045229a72010-03-03 22:11:19493 int align_flags,
[email protected]f5494d42010-12-23 22:15:34494 const MiniContextMenuParams& params) {
[email protected]97965e12010-04-09 00:51:10495 scoped_refptr<BasePlugin> ref(this);
[email protected]b516e2d2011-07-12 16:54:12496 ChromeFramePlugin<T>::OnHandleContextMenu(menu_model, align_flags, params);
[email protected]045229a72010-03-03 22:11:19497 }
498
[email protected]f7817822009-09-24 05:11:58499 LRESULT OnCreate(UINT message, WPARAM wparam, LPARAM lparam,
500 BOOL& handled) { // NO_LINT
501 ModifyStyle(0, WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0);
[email protected]bbfa9a12010-08-10 14:09:37502 url_fetcher_->put_notification_window(m_hWnd);
[email protected]dd4beb522010-07-13 18:18:14503 if (automation_client_.get()) {
504 automation_client_->SetParentWindow(m_hWnd);
505 } else {
506 NOTREACHED() << "No automation server";
507 return -1;
508 }
[email protected]f7817822009-09-24 05:11:58509 // Only fire the 'interactive' ready state if we aren't there already.
510 if (ready_state_ < READYSTATE_INTERACTIVE) {
511 ready_state_ = READYSTATE_INTERACTIVE;
512 FireOnChanged(DISPID_READYSTATE);
513 }
514 return 0;
515 }
516
[email protected]e9eb50c2009-10-24 16:26:46517 LRESULT OnDestroy(UINT message, WPARAM wparam, LPARAM lparam,
518 BOOL& handled) { // NO_LINT
[email protected]2b9a9f162010-10-19 20:30:45519 DVLOG(1) << __FUNCTION__;
[email protected]e9eb50c2009-10-24 16:26:46520 return 0;
521 }
522
[email protected]f7817822009-09-24 05:11:58523 // ChromeFrameDelegate override
524 virtual void OnAutomationServerReady() {
[email protected]bbfa9a12010-08-10 14:09:37525 draw_sad_tab_ = false;
[email protected]f7817822009-09-24 05:11:58526 ChromeFramePlugin<T>::OnAutomationServerReady();
527
528 ready_state_ = READYSTATE_COMPLETE;
529 FireOnChanged(DISPID_READYSTATE);
530 }
531
532 // ChromeFrameDelegate override
533 virtual void OnAutomationServerLaunchFailed(
534 AutomationLaunchResult reason, const std::string& server_version) {
[email protected]2b9a9f162010-10-19 20:30:45535 DVLOG(1) << __FUNCTION__;
[email protected]bbfa9a12010-08-10 14:09:37536 if (reason == AUTOMATION_SERVER_CRASHED)
537 draw_sad_tab_ = true;
538
[email protected]f7817822009-09-24 05:11:58539 ready_state_ = READYSTATE_UNINITIALIZED;
540 FireOnChanged(DISPID_READYSTATE);
541 }
542
[email protected]f5494d42010-12-23 22:15:34543 virtual void OnCloseTab() {
[email protected]2f1793ea2010-10-27 17:32:51544 Fire_onclose();
545 }
546
[email protected]f7817822009-09-24 05:11:58547 // Overridden to take advantage of readystate prop changes and send those
548 // to potential listeners.
549 HRESULT FireOnChanged(DISPID dispid) {
550 if (dispid == DISPID_READYSTATE) {
551 Fire_onreadystatechanged(ready_state_);
552 }
553 return __super::FireOnChanged(dispid);
554 }
555
556 // IChromeFrame
557 // Property getter/setters for the src attribute, which contains a URL.
558 // The ChromeFrameActivex control initiates navigation to this URL
559 // when instantiated.
560 STDMETHOD(get_src)(BSTR* src) {
561 if (NULL == src) {
562 return E_POINTER;
563 }
564
565 *src = SysAllocString(url_);
566 return S_OK;
567 }
568
569 STDMETHOD(put_src)(BSTR src) {
570 if (src == NULL)
571 return E_INVALIDARG;
572
573 // Switch the src to UTF8 and try to expand to full URL
574 std::string src_utf8;
575 WideToUTF8(src, SysStringLen(src), &src_utf8);
576 std::string full_url = ResolveURL(GetDocumentUrl(), src_utf8);
[email protected]f7817822009-09-24 05:11:58577
578 // We can initiate navigation here even if ready_state is not complete.
579 // We do not have to set proxy, and AutomationClient will take care
580 // of navigation just after CreateExternalTab is done.
[email protected]b36a9f92009-10-19 17:34:57581 if (!automation_client_->InitiateNavigation(full_url,
582 GetDocumentUrl(),
[email protected]354bcba2010-12-14 04:34:43583 this)) {
[email protected]f7817822009-09-24 05:11:58584 // TODO(robertshield): Make InitiateNavigation return more useful
585 // error information.
586 return E_INVALIDARG;
587 }
588
589 // Save full URL in BSTR member
590 url_.Reset(::SysAllocString(UTF8ToWide(full_url).c_str()));
591
592 return S_OK;
593 }
594
595 STDMETHOD(get_onload)(VARIANT* onload_handler) {
596 if (NULL == onload_handler)
597 return E_INVALIDARG;
598
599 *onload_handler = onload_handler_.Copy();
600
601 return S_OK;
602 }
603
604 // Property setter for the onload attribute, which contains a
605 // javascript function to be invoked on successful navigation.
606 STDMETHOD(put_onload)(VARIANT onload_handler) {
607 if (V_VT(&onload_handler) != VT_DISPATCH) {
608 DLOG(WARNING) << "Invalid onload handler type: "
609 << onload_handler.vt
610 << " specified";
611 return E_INVALIDARG;
612 }
613
614 onload_handler_ = onload_handler;
615
616 return S_OK;
617 }
618
619 // Property getter/setters for the onloaderror attribute, which contains a
620 // javascript function to be invoked on navigation failure.
621 STDMETHOD(get_onloaderror)(VARIANT* onerror_handler) {
622 if (NULL == onerror_handler)
623 return E_INVALIDARG;
624
625 *onerror_handler = onerror_handler_.Copy();
626
627 return S_OK;
628 }
629
630 STDMETHOD(put_onloaderror)(VARIANT onerror_handler) {
631 if (V_VT(&onerror_handler) != VT_DISPATCH) {
632 DLOG(WARNING) << "Invalid onloaderror handler type: "
633 << onerror_handler.vt
634 << " specified";
635 return E_INVALIDARG;
636 }
637
638 onerror_handler_ = onerror_handler;
639
640 return S_OK;
641 }
642
643 // Property getter/setters for the onmessage attribute, which contains a
644 // javascript function to be invoked when we receive a message from the
645 // chrome frame.
646 STDMETHOD(put_onmessage)(VARIANT onmessage_handler) {
647 if (V_VT(&onmessage_handler) != VT_DISPATCH) {
648 DLOG(WARNING) << "Invalid onmessage handler type: "
649 << onmessage_handler.vt
650 << " specified";
651 return E_INVALIDARG;
652 }
653
654 onmessage_handler_ = onmessage_handler;
655
656 return S_OK;
657 }
658
659 STDMETHOD(get_onmessage)(VARIANT* onmessage_handler) {
660 if (NULL == onmessage_handler)
661 return E_INVALIDARG;
662
663 *onmessage_handler = onmessage_handler_.Copy();
664
665 return S_OK;
666 }
667
668 STDMETHOD(get_readyState)(long* ready_state) { // NOLINT
[email protected]2b9a9f162010-10-19 20:30:45669 DVLOG(1) << __FUNCTION__;
[email protected]f7817822009-09-24 05:11:58670 DCHECK(ready_state);
671
672 if (!ready_state)
673 return E_INVALIDARG;
674
675 *ready_state = ready_state_;
676
677 return S_OK;
678 }
679
680 // Property getter/setters for use_chrome_network flag. This flag
681 // indicates if chrome network stack is to be used for fetching
682 // network requests.
683 STDMETHOD(get_useChromeNetwork)(VARIANT_BOOL* use_chrome_network) {
684 if (!use_chrome_network)
685 return E_INVALIDARG;
686
687 *use_chrome_network =
688 automation_client_->use_chrome_network() ? VARIANT_TRUE : VARIANT_FALSE;
689 return S_OK;
690 }
691
692 STDMETHOD(put_useChromeNetwork)(VARIANT_BOOL use_chrome_network) {
[email protected]0753db592010-12-15 14:45:05693 if (!is_privileged()) {
[email protected]f7817822009-09-24 05:11:58694 DLOG(ERROR) << "Attempt to set useChromeNetwork in non-privileged mode";
695 return E_ACCESSDENIED;
696 }
697
698 automation_client_->set_use_chrome_network(
699 (VARIANT_FALSE != use_chrome_network));
700 return S_OK;
701 }
702
703 // Posts a message to the chrome frame.
704 STDMETHOD(postMessage)(BSTR message, VARIANT target) {
705 if (NULL == message) {
706 return E_INVALIDARG;
707 }
708
709 if (!automation_client_.get())
710 return E_FAIL;
711
712 std::string utf8_target;
713 if (target.vt == VT_BSTR) {
714 int len = ::SysStringLen(target.bstrVal);
715 if (len == 1 && target.bstrVal[0] == L'*') {
716 utf8_target = "*";
717 } else {
718 GURL resolved(target.bstrVal);
719 if (!resolved.is_valid()) {
720 Error(L"Unable to parse the specified target URL.");
721 return E_INVALIDARG;
722 }
723
724 utf8_target = resolved.spec();
725 }
726 } else {
727 utf8_target = "*";
728 }
729
730 std::string utf8_message;
731 WideToUTF8(message, ::SysStringLen(message), &utf8_message);
732
733 GURL url(GURL(document_url_).GetOrigin());
734 std::string origin(url.is_empty() ? "null" : url.spec());
735 if (!automation_client_->ForwardMessageFromExternalHost(utf8_message,
736 origin,
737 utf8_target)) {
738 Error(L"Failed to post message to chrome frame");
739 return E_FAIL;
740 }
741
742 return S_OK;
743 }
744
745 STDMETHOD(addEventListener)(BSTR event_type, IDispatch* listener,
746 VARIANT use_capture) {
747 EventHandlers* handlers = NULL;
748 HRESULT hr = GetHandlersForEvent(event_type, &handlers);
749 if (FAILED(hr))
750 return hr;
751
752 DCHECK(handlers != NULL);
753
[email protected]965722ff2010-10-20 15:50:30754 handlers->insert(base::win::ScopedComPtr<IDispatch>(listener));
[email protected]f7817822009-09-24 05:11:58755
756 return hr;
757 }
758
759 STDMETHOD(removeEventListener)(BSTR event_type, IDispatch* listener,
760 VARIANT use_capture) {
761 EventHandlers* handlers = NULL;
762 HRESULT hr = GetHandlersForEvent(event_type, &handlers);
763 if (FAILED(hr))
764 return hr;
765
766 DCHECK(handlers != NULL);
[email protected]c90d8492011-01-30 22:47:42767 handlers->erase(base::win::ScopedComPtr<IDispatch>(listener));
[email protected]f7817822009-09-24 05:11:58768
769 return hr;
770 }
771
772 STDMETHOD(get_version)(BSTR* version) {
773 if (!automation_client_.get()) {
774 NOTREACHED();
775 return E_FAIL;
776 }
777
778 if (version == NULL) {
779 return E_INVALIDARG;
780 }
781
782 *version = SysAllocString(automation_client_->GetVersion().c_str());
783 return S_OK;
784 }
785
786 STDMETHOD(postPrivateMessage)(BSTR message, BSTR origin, BSTR target) {
787 if (NULL == message)
788 return E_INVALIDARG;
789
[email protected]0753db592010-12-15 14:45:05790 if (!is_privileged()) {
[email protected]f7817822009-09-24 05:11:58791 DLOG(ERROR) << "Attempt to postPrivateMessage in non-privileged mode";
792 return E_ACCESSDENIED;
793 }
794
795 DCHECK(automation_client_.get());
796 std::string utf8_message, utf8_origin, utf8_target;
797 WideToUTF8(message, ::SysStringLen(message), &utf8_message);
798 WideToUTF8(origin, ::SysStringLen(origin), &utf8_origin);
799 WideToUTF8(target, ::SysStringLen(target), &utf8_target);
800
801 if (!automation_client_->ForwardMessageFromExternalHost(utf8_message,
802 utf8_origin,
803 utf8_target)) {
804 Error(L"Failed to post message to chrome frame");
805 return E_FAIL;
806 }
807
808 return S_OK;
809 }
810
[email protected]00f6b772009-10-23 17:03:41811 STDMETHOD(installExtension)(BSTR crx_path) {
[email protected]7419d4b2011-04-06 15:18:04812 NOTREACHED(); // Deprecated.
813 return E_NOTIMPL;
[email protected]00f6b772009-10-23 17:03:41814 }
815
816 STDMETHOD(loadExtension)(BSTR path) {
[email protected]7419d4b2011-04-06 15:18:04817 NOTREACHED(); // Deprecated.
818 return E_NOTIMPL;
[email protected]00f6b772009-10-23 17:03:41819 }
820
[email protected]a1e62d12010-03-16 02:18:43821 STDMETHOD(getEnabledExtensions)() {
[email protected]7419d4b2011-04-06 15:18:04822 NOTREACHED(); // Deprecated.
823 return E_NOTIMPL;
[email protected]751bf4b2010-11-05 22:06:31824 }
825
[email protected]ca982ec42010-05-02 14:47:14826 STDMETHOD(registerBhoIfNeeded)() {
[email protected]e12a3b82010-05-01 00:06:00827 return E_NOTIMPL;
828 }
829
[email protected]f7817822009-09-24 05:11:58830 // Returns the vector of event handlers for a given event (e.g. "load").
831 // If the event type isn't recognized, the function fills in a descriptive
832 // error (IErrorInfo) and returns E_INVALIDARG.
833 HRESULT GetHandlersForEvent(BSTR event_type, EventHandlers** handlers) {
834 DCHECK(handlers != NULL);
835
[email protected]00f6b772009-10-23 17:03:41836 // TODO(tommi): make these if() statements data-driven.
[email protected]f7817822009-09-24 05:11:58837 HRESULT hr = S_OK;
838 const wchar_t* event_type_end = event_type + ::SysStringLen(event_type);
839 if (LowerCaseEqualsASCII(event_type, event_type_end, "message")) {
840 *handlers = &onmessage_;
841 } else if (LowerCaseEqualsASCII(event_type, event_type_end, "load")) {
842 *handlers = &onload_;
843 } else if (LowerCaseEqualsASCII(event_type, event_type_end, "loaderror")) {
844 *handlers = &onloaderror_;
845 } else if (LowerCaseEqualsASCII(event_type, event_type_end,
846 "readystatechanged")) {
847 *handlers = &onreadystatechanged_;
848 } else if (LowerCaseEqualsASCII(event_type, event_type_end,
[email protected]00f6b772009-10-23 17:03:41849 "privatemessage")) {
[email protected]f7817822009-09-24 05:11:58850 // This event handler is only available in privileged mode.
[email protected]0753db592010-12-15 14:45:05851 if (is_privileged()) {
[email protected]00f6b772009-10-23 17:03:41852 *handlers = &onprivatemessage_;
853 } else {
[email protected]f7817822009-09-24 05:11:58854 Error("Event type 'privatemessage' is privileged");
[email protected]00f6b772009-10-23 17:03:41855 hr = E_ACCESSDENIED;
[email protected]f7817822009-09-24 05:11:58856 }
[email protected]00f6b772009-10-23 17:03:41857 } else if (LowerCaseEqualsASCII(event_type, event_type_end,
858 "extensionready")) {
859 // This event handler is only available in privileged mode.
[email protected]0753db592010-12-15 14:45:05860 if (is_privileged()) {
[email protected]00f6b772009-10-23 17:03:41861 *handlers = &onextensionready_;
862 } else {
863 Error("Event type 'extensionready' is privileged");
864 hr = E_ACCESSDENIED;
865 }
[email protected]f7817822009-09-24 05:11:58866 } else {
[email protected]d3451d832010-10-01 11:17:37867 Error(base::StringPrintf(
868 "Event type '%ls' not found", event_type).c_str());
[email protected]f7817822009-09-24 05:11:58869 hr = E_INVALIDARG;
870 }
871
872 return hr;
873 }
874
[email protected]a1800e82009-11-19 00:53:23875 // Creates a new event object that supports the |data| property.
876 // Note: you should supply an empty string for |origin| unless you're
877 // creating a "message" event.
878 HRESULT CreateDomEvent(const std::string& event_type, const std::string& data,
879 const std::string& origin, IDispatch** event) {
[email protected]62bb18dc12009-11-25 01:34:08880 DCHECK(event_type.length() > 0); // NOLINT
[email protected]a1800e82009-11-19 00:53:23881 DCHECK(event != NULL);
882
883 CComObject<ComMessageEvent>* ev = NULL;
884 HRESULT hr = CComObject<ComMessageEvent>::CreateInstance(&ev);
885 if (SUCCEEDED(hr)) {
886 ev->AddRef();
887
[email protected]965722ff2010-10-20 15:50:30888 base::win::ScopedComPtr<IOleContainer> container;
[email protected]a1800e82009-11-19 00:53:23889 m_spClientSite->GetContainer(container.Receive());
890 if (ev->Initialize(container, data, origin, event_type)) {
891 *event = ev;
892 } else {
893 NOTREACHED() << "event->Initialize";
894 ev->Release();
895 hr = E_UNEXPECTED;
896 }
897 }
898
899 return hr;
900 }
901
902 // Helper function to execute a function on a script IDispatch interface.
903 HRESULT InvokeScriptFunction(const VARIANT& script_object,
904 const std::string& param) {
[email protected]965722ff2010-10-20 15:50:30905 base::win::ScopedVariant script_arg(UTF8ToWide(param.c_str()).c_str());
[email protected]a1800e82009-11-19 00:53:23906 return InvokeScriptFunction(script_object, script_arg.AsInput());
907 }
908
909 HRESULT InvokeScriptFunction(const VARIANT& script_object, VARIANT* param) {
910 return InvokeScriptFunction(script_object, param, 1);
911 }
912
913 HRESULT InvokeScriptFunction(const VARIANT& script_object, VARIANT* params,
914 int param_count) {
[email protected]62bb18dc12009-11-25 01:34:08915 DCHECK_GE(param_count, 0);
[email protected]a1800e82009-11-19 00:53:23916 DCHECK(params);
917
[email protected]086f367d52010-04-23 21:01:50918 if (V_VT(&script_object) != VT_DISPATCH ||
919 script_object.pdispVal == NULL) {
[email protected]a1800e82009-11-19 00:53:23920 return S_FALSE;
921 }
922
923 CComPtr<IDispatch> script(script_object.pdispVal);
924 HRESULT hr = script.InvokeN(static_cast<DISPID>(DISPID_VALUE),
925 params,
926 param_count);
927 // 0x80020101 == SCRIPT_E_REPORTED.
928 // When the script we're invoking has an error, we get this error back.
929 DLOG_IF(ERROR, FAILED(hr) && hr != 0x80020101) << "Failed to invoke script";
930 return hr;
931 }
932
[email protected]f7817822009-09-24 05:11:58933 // Gives the browser a chance to handle an accelerator that was
934 // sent to the out of proc chromium instance.
935 // Returns S_OK iff the accelerator was handled by the browser.
936 HRESULT AllowFrameToTranslateAccelerator(const MSG& msg) {
[email protected]84415812010-05-19 23:12:42937 static const int kMayTranslateAcceleratorOffset = 0x5c;
[email protected]f7817822009-09-24 05:11:58938 // Although IBrowserService2 is officially deprecated, it's still alive
939 // and well in IE7 and earlier. We have to use it here to correctly give
940 // the browser a chance to handle keyboard shortcuts.
941 // This happens automatically for activex components that have windows that
942 // belong to the current thread. In that circumstance IE owns the message
943 // loop and can walk the line of components allowing each participant the
944 // chance to handle the keystroke and eventually falls back to
945 // v_MayTranslateAccelerator. However in our case, the message loop is
946 // owned by the out-of-proc chromium instance so IE doesn't have a chance to
947 // fall back on its default behavior. Instead we give IE a chance to
948 // handle the shortcut here.
[email protected]01dba672010-02-12 21:55:48949 MSG accel_message = msg;
950 accel_message.hwnd = ::GetParent(m_hWnd);
[email protected]f7817822009-09-24 05:11:58951 HRESULT hr = S_FALSE;
[email protected]965722ff2010-10-20 15:50:30952 base::win::ScopedComPtr<IBrowserService2> bs2;
[email protected]3eafbfb92010-08-02 16:54:22953
954 // For non-IE containers, we use the standard IOleInPlaceFrame contract
955 // (which IE does not support). For IE, we try to use IBrowserService2,
956 // but need special handling for IE8 (see below).
957 //
958 // We try to cache an IOleInPlaceFrame for our site. If we fail, we don't
959 // retry, and we fall back to the IBrowserService2 and PostMessage
960 // approaches below.
961 if (!in_place_frame_ && !failed_to_fetch_in_place_frame_) {
[email protected]965722ff2010-10-20 15:50:30962 base::win::ScopedComPtr<IOleInPlaceUIWindow> dummy_ui_window;
[email protected]3eafbfb92010-08-02 16:54:22963 RECT dummy_pos_rect = {0};
964 RECT dummy_clip_rect = {0};
965 OLEINPLACEFRAMEINFO dummy_frame_info = {0};
[email protected]58640fa2011-02-15 00:05:22966 if (!m_spInPlaceSite ||
967 FAILED(m_spInPlaceSite->GetWindowContext(in_place_frame_.Receive(),
[email protected]3eafbfb92010-08-02 16:54:22968 dummy_ui_window.Receive(),
969 &dummy_pos_rect,
970 &dummy_clip_rect,
971 &dummy_frame_info))) {
972 failed_to_fetch_in_place_frame_ = true;
973 }
974 }
975
976 // The IBrowserService2 code below (second conditional) explicitly checks
977 // for whether the IBrowserService2::v_MayTranslateAccelerator function is
978 // valid. On IE8 there is one vtable ieframe!c_ImpostorBrowserService2Vtbl
979 // where this function entry is NULL which leads to a crash. We don't know
980 // under what circumstances this vtable is actually used though.
981 if (in_place_frame_) {
982 hr = in_place_frame_->TranslateAccelerator(&accel_message, 0);
983 } else if (S_OK == DoQueryService(
984 SID_STopLevelBrowser, m_spInPlaceSite,
985 bs2.Receive()) && bs2.get() &&
986 *(*(reinterpret_cast<void***>(bs2.get())) +
987 kMayTranslateAcceleratorOffset)) {
[email protected]01dba672010-02-12 21:55:48988 hr = bs2->v_MayTranslateAccelerator(&accel_message);
[email protected]f7817822009-09-24 05:11:58989 } else {
990 // IE8 doesn't support IBrowserService2 unless you enable a special,
991 // undocumented flag with CoInternetSetFeatureEnabled and even then,
992 // the object you get back implements only a couple of methods of
993 // that interface... all the other entries in the vtable are NULL.
994 // In addition, the class that implements it is called
995 // ImpostorBrowserService2 :)
996 // IE8 does have a new interface though, presumably called
997 // ITabBrowserService or something that can be abbreviated to TBS.
998 // That interface has a method, TranslateAcceleratorTBS that does
999 // call the root MayTranslateAccelerator function, but alas the
1000 // first argument to MayTranslateAccelerator is hard coded to FALSE
1001 // which means that global accelerators are not handled and we're
1002 // out of luck.
1003 // A third thing that's notable with regards to IE8 is that
1004 // none of the *MayTranslate* functions exist in a vtable inside
1005 // ieframe.dll. I checked this by scanning for the address of
1006 // those functions inside the dll and found none, which means that
1007 // all calls to those functions are relative.
[email protected]3eafbfb92010-08-02 16:54:221008 // So, for IE8 in certain cases, and for other containers that may
1009 // support neither IOleInPlaceFrame or IBrowserService2 our approach
1010 // is very simple. Just post the message to our parent window and IE
1011 // will pick it up if it's an accelerator. We won't know for sure if
1012 // the browser handled the keystroke or not.
[email protected]01dba672010-02-12 21:55:481013 ::PostMessage(accel_message.hwnd, accel_message.message,
1014 accel_message.wParam, accel_message.lParam);
[email protected]f7817822009-09-24 05:11:581015 }
1016
1017 return hr;
1018 }
1019
[email protected]f5494d42010-12-23 22:15:341020 virtual void OnAcceleratorPressed(const MSG& accel_message) {
[email protected]2b8fd322009-10-02 00:00:591021 DCHECK(m_spInPlaceSite != NULL);
1022 // Allow our host a chance to handle the accelerator.
1023 // This catches things like Ctrl+F, Ctrl+O etc, but not browser
1024 // accelerators such as F11, Ctrl+T etc.
1025 // (see AllowFrameToTranslateAccelerator for those).
1026 HRESULT hr = TranslateAccelerator(const_cast<MSG*>(&accel_message));
1027 if (hr != S_OK)
1028 hr = AllowFrameToTranslateAccelerator(accel_message);
1029
[email protected]2b9a9f162010-10-19 20:30:451030 DVLOG(1) << __FUNCTION__ << " browser response: "
1031 << base::StringPrintf("0x%08x", hr);
[email protected]2b8fd322009-10-02 00:00:591032
1033 if (hr != S_OK) {
[email protected]4e964792010-11-30 23:27:481034 // The WM_SYSKEYDOWN/WM_SYSKEYUP messages are not processed by the
1035 // IOleControlSite and IBrowserService2::v_MayTranslateAccelerator
1036 // implementations. We need to understand this better. That is for
1037 // another day. For now we just post these messages back to the parent
1038 // which forwards it off to the frame. This should not cause major
1039 // grief for Chrome as it does not need to handle WM_SYSKEY* messages in
1040 // in ChromeFrame mode.
[email protected]2b8fd322009-10-02 00:00:591041 // TODO(iyengar)
1042 // Understand and fix WM_SYSCHAR handling
1043 // We should probably unify the accelerator handling for the active
1044 // document and the activex.
[email protected]4e964792010-11-30 23:27:481045 if (accel_message.message == WM_SYSCHAR ||
1046 accel_message.message == WM_SYSKEYDOWN ||
1047 accel_message.message == WM_SYSKEYUP) {
1048 ::PostMessage(GetParent(), accel_message.message, accel_message.wParam,
[email protected]2b8fd322009-10-02 00:00:591049 accel_message.lParam);
1050 return;
1051 }
1052 }
[email protected]2b8fd322009-10-02 00:00:591053 // Last chance to handle the keystroke is to pass it to chromium.
1054 // We do this last partially because there's no way for us to tell if
1055 // chromium actually handled the keystroke, but also since the browser
1056 // should have first dibs anyway.
1057 if (hr != S_OK && automation_client_.get()) {
1058 TabProxy* tab = automation_client_->tab();
1059 if (tab) {
1060 tab->ProcessUnhandledAccelerator(accel_message);
1061 }
1062 }
1063 }
1064
[email protected]f7817822009-09-24 05:11:581065 protected:
[email protected]b1c55638612010-03-08 16:26:111066 void HostNavigate(const GURL& url_to_open,
1067 const GURL& referrer, int open_disposition) {
[email protected]965722ff2010-10-20 15:50:301068 base::win::ScopedComPtr<IWebBrowser2> web_browser2;
[email protected]b1c55638612010-03-08 16:26:111069 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive());
[email protected]8e33c8f5282010-04-02 05:59:551070 if (!web_browser2) {
1071 NOTREACHED() << "Failed to retrieve IWebBrowser2 interface";
1072 return;
1073 }
[email protected]965722ff2010-10-20 15:50:301074 base::win::ScopedVariant url;
[email protected]b1c55638612010-03-08 16:26:111075 // Check to see if the URL uses a "view-source:" prefix, if so, open it
1076 // using chrome frame full tab mode by using 'cf:' protocol handler.
1077 // Also change the disposition to NEW_WINDOW since IE6 doesn't have tabs.
1078 if (url_to_open.has_scheme() &&
1079 (url_to_open.SchemeIs(chrome::kViewSourceScheme) ||
1080 url_to_open.SchemeIs(chrome::kAboutScheme))) {
1081 std::wstring chrome_url;
1082 chrome_url.append(kChromeProtocolPrefix);
1083 chrome_url.append(UTF8ToWide(url_to_open.spec()));
1084 url.Set(chrome_url.c_str());
1085 open_disposition = NEW_WINDOW;
1086 } else {
1087 url.Set(UTF8ToWide(url_to_open.spec()).c_str());
1088 }
1089
1090 VARIANT flags = { VT_I4 };
1091 V_I4(&flags) = 0;
1092
1093 IEVersion ie_version = GetIEVersion();
1094 DCHECK(ie_version != NON_IE && ie_version != IE_UNSUPPORTED);
1095 // Since IE6 doesn't support tabs, so we just use window instead.
1096 if (ie_version == IE_6) {
1097 if (open_disposition == NEW_FOREGROUND_TAB ||
1098 open_disposition == NEW_BACKGROUND_TAB ||
1099 open_disposition == NEW_WINDOW ||
1100 open_disposition == NEW_POPUP) {
1101 V_I4(&flags) = navOpenInNewWindow;
1102 } else if (open_disposition != CURRENT_TAB) {
1103 NOTREACHED() << "Unsupported open disposition in IE6";
1104 }
1105 } else {
1106 switch (open_disposition) {
1107 case NEW_FOREGROUND_TAB:
1108 V_I4(&flags) = navOpenInNewTab;
1109 break;
1110 case NEW_BACKGROUND_TAB:
1111 V_I4(&flags) = navOpenInBackgroundTab;
1112 break;
1113 case NEW_WINDOW:
1114 case NEW_POPUP:
1115 V_I4(&flags) = navOpenInNewWindow;
1116 break;
1117 default:
1118 break;
1119 }
1120 }
1121
1122 // TODO(sanjeevr): The navOpenInNewWindow flag causes IE to open this
1123 // in a new window ONLY if the user has specified
1124 // "Always open popups in a new window". Otherwise it opens in a new tab.
1125 // We need to investigate more and see if we can force IE to display the
1126 // link in a new window. MSHTML uses the below code to force an open in a
1127 // new window. But this logic also fails for us. Perhaps this flag is not
1128 // honoured if the ActiveDoc is not MSHTML.
1129 // Even the HLNF_DISABLEWINDOWRESTRICTIONS flag did not work.
1130 // Start of MSHTML-like logic.
1131 // CComQIPtr<ITargetFramePriv2> target_frame = web_browser2;
1132 // if (target_frame) {
1133 // CComPtr<IUri> uri;
1134 // CreateUri(UTF8ToWide(open_url_command->url_.spec()).c_str(),
1135 // Uri_CREATE_IE_SETTINGS, 0, &uri);
1136 // CComPtr<IBindCtx> bind_ctx;
1137 // CreateBindCtx(0, &bind_ctx);
1138 // target_frame->AggregatedNavigation2(
1139 // HLNF_TRUSTFIRSTDOWNLOAD|HLNF_OPENINNEWWINDOW, bind_ctx, NULL,
1140 // L"No_Name", uri, L"");
1141 // }
1142 // End of MSHTML-like logic
[email protected]965722ff2010-10-20 15:50:301143 VARIANT empty = base::win::ScopedVariant::kEmptyVariant;
1144 base::win::ScopedVariant http_headers;
[email protected]b1c55638612010-03-08 16:26:111145
1146 if (referrer.is_valid()) {
1147 std::wstring referrer_header = L"Referer: ";
1148 referrer_header += UTF8ToWide(referrer.spec());
1149 referrer_header += L"\r\n\r\n";
1150 http_headers.Set(referrer_header.c_str());
1151 }
1152
[email protected]74b1eaf542011-10-05 19:34:271153 // IE6 does not support tabs. If Chrome sent us a window open request
1154 // indicating that the navigation needs to occur in a foreground tab or
1155 // a popup window, then we need to ensure that the new window in IE6 is
1156 // brought to the foreground.
1157 if (ie_version == IE_6) {
1158 ChromeFrameUrl cf_url;
1159 cf_url.Parse(static_cast<BSTR>(url_));
1160
1161 if (cf_url.attach_to_external_tab() &&
1162 (cf_url.disposition() == NEW_FOREGROUND_TAB ||
1163 cf_url.disposition() == NEW_POPUP)) {
1164 BringWebBrowserWindowToTop(web_browser2);
1165 }
1166 }
1167
[email protected]52622ca12011-01-06 22:53:061168 HRESULT hr = web_browser2->Navigate2(url.AsInput(), &flags, &empty, &empty,
1169 http_headers.AsInput());
1170 // If the current window is a popup window then attempting to open a new
1171 // tab for the navigation will fail. We attempt to issue the navigation in
1172 // a new window in this case.
1173 // http://msdn.microsoft.com/en-us/library/aa752133(v=vs.85).aspx
1174 if (FAILED(hr) && V_I4(&flags) != navOpenInNewWindow) {
1175 V_I4(&flags) = navOpenInNewWindow;
1176 hr = web_browser2->Navigate2(url.AsInput(), &flags, &empty, &empty,
1177 http_headers.AsInput());
[email protected]52622ca12011-01-06 22:53:061178 DLOG_IF(ERROR, FAILED(hr))
1179 << "Navigate2 failed with error: "
1180 << base::StringPrintf("0x%08X", hr);
1181 }
[email protected]b1c55638612010-03-08 16:26:111182 }
1183
[email protected]e1081d92010-09-10 20:29:111184 void InitializeAutomationSettings() {
1185 static const wchar_t kHandleTopLevelRequests[] = L"HandleTopLevelRequests";
1186 static const wchar_t kUseChromeNetworking[] = L"UseChromeNetworking";
1187
1188 // Query and assign the top-level-request routing, and host networking
1189 // settings from the registry.
1190 bool top_level_requests = GetConfigBool(true, kHandleTopLevelRequests);
1191 bool chrome_network = GetConfigBool(false, kUseChromeNetworking);
1192 automation_client_->set_handle_top_level_requests(top_level_requests);
1193 automation_client_->set_use_chrome_network(chrome_network);
1194 }
1195
[email protected]965722ff2010-10-20 15:50:301196 base::win::ScopedBstr url_;
1197 base::win::ScopedComPtr<IOleDocumentSite> doc_site_;
[email protected]f7817822009-09-24 05:11:581198
[email protected]3eafbfb92010-08-02 16:54:221199 // If false, we tried but failed to fetch an IOleInPlaceFrame from our host.
1200 // Cached here so we don't try to fetch it every time if we keep failing.
1201 bool failed_to_fetch_in_place_frame_;
[email protected]bbfa9a12010-08-10 14:09:371202 bool draw_sad_tab_;
1203
[email protected]965722ff2010-10-20 15:50:301204 base::win::ScopedComPtr<IOleInPlaceFrame> in_place_frame_;
[email protected]3eafbfb92010-08-02 16:54:221205
[email protected]f7817822009-09-24 05:11:581206 // For more information on the ready_state_ property see:
1207 // http://msdn.microsoft.com/en-us/library/aa768179(VS.85).aspx#
1208 READYSTATE ready_state_;
1209
1210 // The following members contain IDispatch interfaces representing the
1211 // onload/onerror/onmessage handlers on the page.
[email protected]965722ff2010-10-20 15:50:301212 base::win::ScopedVariant onload_handler_;
1213 base::win::ScopedVariant onerror_handler_;
1214 base::win::ScopedVariant onmessage_handler_;
[email protected]f7817822009-09-24 05:11:581215
1216 EventHandlers onmessage_;
1217 EventHandlers onloaderror_;
1218 EventHandlers onload_;
1219 EventHandlers onreadystatechanged_;
1220 EventHandlers onprivatemessage_;
[email protected]00f6b772009-10-23 17:03:411221 EventHandlers onextensionready_;
[email protected]f7817822009-09-24 05:11:581222
[email protected]3eb07da2010-02-01 19:48:361223 // Handle network requests when host network stack is used. Passed to the
1224 // automation client on initialization.
[email protected]bbfa9a12010-08-10 14:09:371225 scoped_ptr<UrlmonUrlRequestManager> url_fetcher_;
[email protected]f7817822009-09-24 05:11:581226};
1227
1228#endif // CHROME_FRAME_CHROME_FRAME_ACTIVEX_BASE_H_