OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
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> | |
11 #include <exdisp.h> | |
12 #include <wininet.h> | |
13 #include <shdeprecated.h> // for IBrowserService2 | |
14 #include <shlguid.h> | |
15 | |
16 #include <set> | |
17 #include <string> | |
18 #include <vector> | |
19 | |
20 #include "base/metrics/histogram.h" | |
21 #include "base/strings/string_util.h" | |
22 #include "base/strings/stringprintf.h" | |
23 #include "base/strings/utf_string_conversions.h" | |
24 #include "base/win/scoped_bstr.h" | |
25 #include "base/win/scoped_comptr.h" | |
26 #include "base/win/scoped_variant.h" | |
27 #include "chrome/app/chrome_command_ids.h" | |
28 #include "chrome/common/url_constants.h" | |
29 #include "chrome_frame/chrome_frame_plugin.h" | |
30 #include "chrome_frame/chrome_tab.h" | |
31 #include "chrome_frame/com_message_event.h" | |
32 #include "chrome_frame/com_type_info_holder.h" | |
33 #include "chrome_frame/simple_resource_loader.h" | |
34 #include "chrome_frame/urlmon_url_request.h" | |
35 #include "chrome_frame/urlmon_url_request_private.h" | |
36 #include "chrome_frame/utils.h" | |
37 #include "grit/chrome_frame_resources.h" | |
38 #include "grit/generated_resources.h" | |
39 #include "net/cookies/cookie_monster.h" | |
40 | |
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); | |
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(); | |
54 std::vector< base::win::ScopedComPtr<IUnknown> > sink_array( | |
55 m_vec.GetSize()); | |
56 for (int connection = 0; connection < m_vec.GetSize(); ++connection) | |
57 sink_array[connection] = m_vec.GetAt(connection); | |
58 me->Unlock(); | |
59 | |
60 for (size_t sink = 0; sink < sink_array.size(); ++sink) { | |
61 DIChromeFrameEvents* events = | |
62 static_cast<DIChromeFrameEvents*>(sink_array[sink].get()); | |
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" << | |
74 base::StringPrintf("0x%08X", hr); | |
75 } | |
76 } | |
77 } | |
78 | |
79 void FireMethodWithParam(ChromeFrameEventDispId dispid, | |
80 const VARIANT& param) { | |
81 FireMethodWithParams(dispid, ¶m, 1); | |
82 } | |
83 | |
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 | |
96 void Fire_onreadystatechanged(long readystate) { // NOLINT | |
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 } | |
113 | |
114 void Fire_onchannelerror() { // NOLINT | |
115 FireMethodWithParams(CF_EVENT_DISPID_ONCHANNELERROR, NULL, 0); | |
116 } | |
117 }; | |
118 | |
119 extern bool g_first_launch_by_process_; | |
120 | |
121 // Common implementation for ActiveX and Active Document | |
122 template <class T, const CLSID& class_id> | |
123 class ATL_NO_VTABLE ChromeFrameActivexBase : // NOLINT | |
124 public CComObjectRootEx<CComMultiThreadModel>, | |
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>, | |
140 public ChromeFramePlugin<T> { | |
141 protected: | |
142 typedef std::set<base::win::ScopedComPtr<IDispatch> > EventHandlers; | |
143 typedef ChromeFrameActivexBase<T, class_id> BasePlugin; | |
144 | |
145 public: | |
146 ChromeFrameActivexBase() | |
147 : ready_state_(READYSTATE_UNINITIALIZED), | |
148 url_fetcher_(new UrlmonUrlRequestManager()), | |
149 failed_to_fetch_in_place_frame_(false), | |
150 draw_sad_tab_(false) { | |
151 m_bWindowOnly = TRUE; | |
152 url_fetcher_->set_container(static_cast<IDispatch*>(this)); | |
153 } | |
154 | |
155 ~ChromeFrameActivexBase() { | |
156 url_fetcher_->set_container(NULL); | |
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) | |
181 COM_INTERFACE_ENTRY(IConnectionPointContainer) | |
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) | |
192 MESSAGE_HANDLER(WM_DESTROY, OnDestroy) | |
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 | |
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 } | |
212 | |
213 DECLARE_PROTECT_FINAL_CONSTRUCT() | |
214 | |
215 void SetResourceModule() { | |
216 SimpleResourceLoader* loader_instance = SimpleResourceLoader::GetInstance(); | |
217 DCHECK(loader_instance); | |
218 HMODULE res_dll = loader_instance->GetResourceModuleHandle(); | |
219 _AtlBaseModule.SetResourceInstance(res_dll); | |
220 } | |
221 | |
222 HRESULT FinalConstruct() { | |
223 SetResourceModule(); | |
224 | |
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; | |
232 UMA_HISTOGRAM_CUSTOM_COUNTS("ChromeFrame.IEVersion", | |
233 GetIEVersion(), | |
234 IE_INVALID, | |
235 IE_10, | |
236 IE_10 + 1); | |
237 } | |
238 | |
239 return S_OK; | |
240 } | |
241 | |
242 void FinalRelease() { | |
243 Uninitialize(); | |
244 } | |
245 | |
246 void ResetUrlRequestManager() { | |
247 url_fetcher_.reset(new UrlmonUrlRequestManager()); | |
248 } | |
249 | |
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)); | |
255 DVLOG(1) << "E_NOINTERFACE: " << buffer; | |
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. | |
275 virtual HRESULT OnDraw(ATL_DRAWINFO& draw_info) { // NOLINT | |
276 if (NULL == draw_info.prcBounds) { | |
277 NOTREACHED(); | |
278 return E_FAIL; | |
279 } | |
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 } | |
297 return S_OK; | |
298 } | |
299 | |
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 | |
309 if (client_site == NULL) { | |
310 in_place_frame_.Release(); | |
311 } | |
312 | |
313 return CComControlBase::IOleObject_SetClientSite(client_site); | |
314 } | |
315 | |
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 | |
323 // scriptable document, return true, else false. | |
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. | |
327 bool is_frame_busting_enabled() const { | |
328 return !is_privileged(); | |
329 } | |
330 | |
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 | |
341 protected: | |
342 virtual void GetProfilePath(const std::wstring& profile_name, | |
343 base::FilePath* profile_path) { | |
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". | |
348 if (is_IE && GetIEVersion() < IE_8) { | |
349 *profile_path = GetIETemporaryFilesFolder(); | |
350 *profile_path = profile_path->Append(L"Google Chrome Frame"); | |
351 } else { | |
352 ChromeFramePlugin::GetProfilePath(profile_name, profile_path); | |
353 } | |
354 DVLOG(1) << __FUNCTION__ << ": " << profile_path->value(); | |
355 } | |
356 | |
357 void OnLoadFailed(int error_code, const std::string& url) { | |
358 HRESULT hr = InvokeScriptFunction(onerror_handler_, url); | |
359 } | |
360 | |
361 LRESULT OnCreate(UINT message, WPARAM wparam, LPARAM lparam, | |
362 BOOL& handled) { // NO_LINT | |
363 ModifyStyle(0, WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0); | |
364 url_fetcher_->put_notification_window(m_hWnd); | |
365 if (automation_client_.get()) { | |
366 automation_client_->SetParentWindow(m_hWnd); | |
367 } else { | |
368 NOTREACHED() << "No automation server"; | |
369 return -1; | |
370 } | |
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 | |
379 LRESULT OnDestroy(UINT message, WPARAM wparam, LPARAM lparam, | |
380 BOOL& handled) { // NO_LINT | |
381 DVLOG(1) << __FUNCTION__; | |
382 return 0; | |
383 } | |
384 | |
385 // ChromeFrameDelegate override | |
386 virtual void OnAutomationServerReady() { | |
387 draw_sad_tab_ = false; | |
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) { | |
397 DVLOG(1) << __FUNCTION__; | |
398 if (reason == AUTOMATION_SERVER_CRASHED) | |
399 draw_sad_tab_ = true; | |
400 | |
401 ready_state_ = READYSTATE_UNINITIALIZED; | |
402 FireOnChanged(DISPID_READYSTATE); | |
403 } | |
404 | |
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; | |
433 base::WideToUTF8(src, SysStringLen(src), &src_utf8); | |
434 std::string full_url = ResolveURL(GetDocumentUrl(), src_utf8); | |
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. | |
439 if (!automation_client_->InitiateNavigation(full_url, | |
440 GetDocumentUrl(), | |
441 this)) { | |
442 // TODO(robertshield): Make InitiateNavigation return more useful | |
443 // error information. | |
444 return E_INVALIDARG; | |
445 } | |
446 | |
447 // Save full URL in BSTR member | |
448 url_.Reset(::SysAllocString(base::UTF8ToWide(full_url).c_str())); | |
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 | |
527 DVLOG(1) << __FUNCTION__; | |
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) { | |
551 if (!is_privileged()) { | |
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) { | |
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 | |
575 handlers->insert(base::win::ScopedComPtr<IDispatch>(listener)); | |
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); | |
588 handlers->erase(base::win::ScopedComPtr<IDispatch>(listener)); | |
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) { | |
608 return S_OK; | |
609 } | |
610 | |
611 STDMETHOD(installExtension)(BSTR crx_path) { | |
612 NOTREACHED(); // Deprecated. | |
613 return E_NOTIMPL; | |
614 } | |
615 | |
616 STDMETHOD(loadExtension)(BSTR path) { | |
617 NOTREACHED(); // Deprecated. | |
618 return E_NOTIMPL; | |
619 } | |
620 | |
621 STDMETHOD(getEnabledExtensions)() { | |
622 NOTREACHED(); // Deprecated. | |
623 return E_NOTIMPL; | |
624 } | |
625 | |
626 STDMETHOD(registerBhoIfNeeded)() { | |
627 return E_NOTIMPL; | |
628 } | |
629 | |
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 | |
636 // TODO(tommi): make these if() statements data-driven. | |
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, | |
649 "privatemessage")) { | |
650 // This event handler is only available in privileged mode. | |
651 if (is_privileged()) { | |
652 *handlers = &onprivatemessage_; | |
653 } else { | |
654 Error("Event type 'privatemessage' is privileged"); | |
655 hr = E_ACCESSDENIED; | |
656 } | |
657 } else if (LowerCaseEqualsASCII(event_type, event_type_end, | |
658 "extensionready")) { | |
659 // This event handler is only available in privileged mode. | |
660 if (is_privileged()) { | |
661 *handlers = &onextensionready_; | |
662 } else { | |
663 Error("Event type 'extensionready' is privileged"); | |
664 hr = E_ACCESSDENIED; | |
665 } | |
666 } else { | |
667 Error(base::StringPrintf( | |
668 "Event type '%ls' not found", event_type).c_str()); | |
669 hr = E_INVALIDARG; | |
670 } | |
671 | |
672 return hr; | |
673 } | |
674 | |
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) { | |
680 DCHECK(event_type.length() > 0); // NOLINT | |
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 | |
688 base::win::ScopedComPtr<IOleContainer> container; | |
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) { | |
705 base::win::ScopedVariant script_arg( | |
706 base::UTF8ToWide(param.c_str()).c_str()); | |
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) { | |
716 DCHECK_GE(param_count, 0); | |
717 DCHECK(params); | |
718 | |
719 if (V_VT(&script_object) != VT_DISPATCH || | |
720 script_object.pdispVal == NULL) { | |
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 | |
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) { | |
738 static const int kMayTranslateAcceleratorOffset = 0x5c; | |
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. | |
750 MSG accel_message = msg; | |
751 accel_message.hwnd = ::GetParent(m_hWnd); | |
752 HRESULT hr = S_FALSE; | |
753 base::win::ScopedComPtr<IBrowserService2> bs2; | |
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_) { | |
763 base::win::ScopedComPtr<IOleInPlaceUIWindow> dummy_ui_window; | |
764 RECT dummy_pos_rect = {0}; | |
765 RECT dummy_clip_rect = {0}; | |
766 OLEINPLACEFRAMEINFO dummy_frame_info = {0}; | |
767 if (!m_spInPlaceSite || | |
768 FAILED(m_spInPlaceSite->GetWindowContext(in_place_frame_.Receive(), | |
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)) { | |
789 hr = bs2->v_MayTranslateAccelerator(&accel_message); | |
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. | |
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. | |
814 ::PostMessage(accel_message.hwnd, accel_message.message, | |
815 accel_message.wParam, accel_message.lParam); | |
816 } | |
817 | |
818 return hr; | |
819 } | |
820 | |
821 protected: | |
822 void HostNavigate(const GURL& url_to_open, | |
823 const GURL& referrer, int open_disposition) { | |
824 base::win::ScopedComPtr<IWebBrowser2> web_browser2; | |
825 DoQueryService(SID_SWebBrowserApp, m_spClientSite, web_browser2.Receive()); | |
826 if (!web_browser2) { | |
827 NOTREACHED() << "Failed to retrieve IWebBrowser2 interface"; | |
828 return; | |
829 } | |
830 base::win::ScopedVariant url; | |
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() && | |
835 (url_to_open.SchemeIs(content::kViewSourceScheme) || | |
836 url_to_open.SchemeIs(chrome::kAboutScheme))) { | |
837 std::wstring chrome_url; | |
838 chrome_url.append(kChromeProtocolPrefix); | |
839 chrome_url.append(base::UTF8ToWide(url_to_open.spec())); | |
840 url.Set(chrome_url.c_str()); | |
841 open_disposition = NEW_WINDOW; | |
842 } else { | |
843 url.Set(base::UTF8ToWide(url_to_open.spec()).c_str()); | |
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; | |
890 // CreateUri(base::UTF8ToWide(open_url_command->url_.spec()).c_str(), | |
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 | |
899 VARIANT empty = base::win::ScopedVariant::kEmptyVariant; | |
900 base::win::ScopedVariant http_headers; | |
901 | |
902 if (referrer.is_valid()) { | |
903 std::wstring referrer_header = L"Referer: "; | |
904 referrer_header += base::UTF8ToWide(referrer.spec()); | |
905 referrer_header += L"\r\n\r\n"; | |
906 http_headers.Set(referrer_header.c_str()); | |
907 } | |
908 | |
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 | |
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()); | |
934 DLOG_IF(ERROR, FAILED(hr)) | |
935 << "Navigate2 failed with error: " | |
936 << base::StringPrintf("0x%08X", hr); | |
937 } | |
938 } | |
939 | |
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 | |
952 base::win::ScopedBstr url_; | |
953 base::win::ScopedComPtr<IOleDocumentSite> doc_site_; | |
954 | |
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_; | |
958 bool draw_sad_tab_; | |
959 | |
960 base::win::ScopedComPtr<IOleInPlaceFrame> in_place_frame_; | |
961 | |
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. | |
968 base::win::ScopedVariant onload_handler_; | |
969 base::win::ScopedVariant onerror_handler_; | |
970 base::win::ScopedVariant onmessage_handler_; | |
971 | |
972 EventHandlers onmessage_; | |
973 EventHandlers onloaderror_; | |
974 EventHandlers onload_; | |
975 EventHandlers onreadystatechanged_; | |
976 EventHandlers onprivatemessage_; | |
977 EventHandlers onextensionready_; | |
978 | |
979 // Handle network requests when host network stack is used. Passed to the | |
980 // automation client on initialization. | |
981 scoped_ptr<UrlmonUrlRequestManager> url_fetcher_; | |
982 }; | |
983 | |
984 #endif // CHROME_FRAME_CHROME_FRAME_ACTIVEX_BASE_H_ | |
OLD | NEW |