blob: a6621533b4568b3109b34849c3ee076df7c24cec [file] [log] [blame]
[email protected]f7817822009-09-24 05:11:581// Copyright (c) 2009 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#include "chrome_frame/chrome_frame_activex.h"
6
[email protected]f7817822009-09-24 05:11:587#include <wininet.h>
8
9#include <algorithm>
[email protected]2b19e2fe2010-02-16 02:24:1810#include <map>
[email protected]f7817822009-09-24 05:11:5811
12#include "base/basictypes.h"
13#include "base/command_line.h"
14#include "base/file_util.h"
15#include "base/logging.h"
16#include "base/path_service.h"
17#include "base/process_util.h"
18#include "base/scoped_bstr_win.h"
[email protected]2b19e2fe2010-02-16 02:24:1819#include "base/singleton.h"
[email protected]f7817822009-09-24 05:11:5820#include "base/string_util.h"
21#include "chrome/common/chrome_constants.h"
22#include "chrome/common/chrome_switches.h"
23#include "chrome/test/automation/tab_proxy.h"
24#include "googleurl/src/gurl.h"
[email protected]f7817822009-09-24 05:11:5825#include "chrome_frame/utils.h"
26
[email protected]2b19e2fe2010-02-16 02:24:1827namespace {
28
29// Class used to maintain a mapping from top-level windows to ChromeFrameActivex
30// instances.
31class TopLevelWindowMapping {
32 public:
33 typedef std::vector<HWND> WindowList;
34
35 static TopLevelWindowMapping* instance() {
36 return Singleton<TopLevelWindowMapping>::get();
37 }
38
39 // Add |cf_window| to the set of windows registered under |top_window|.
40 void AddMapping(HWND top_window, HWND cf_window) {
41 top_window_map_lock_.Lock();
42 top_window_map_[top_window].push_back(cf_window);
43 top_window_map_lock_.Unlock();
44 }
45
46 // Return the set of Chrome-Frame instances under |window|.
47 WindowList GetInstances(HWND window) {
48 top_window_map_lock_.Lock();
49 WindowList list = top_window_map_[window];
50 top_window_map_lock_.Unlock();
51 return list;
52 }
53
54 private:
55 // Constructor is private as this class it to be used as a singleton.
56 // See static method instance().
57 TopLevelWindowMapping() {}
58
59 friend struct DefaultSingletonTraits<TopLevelWindowMapping>;
60
61 typedef std::map<HWND, WindowList> TopWindowMap;
62 TopWindowMap top_window_map_;
63
64 CComAutoCriticalSection top_window_map_lock_;
65
66 DISALLOW_COPY_AND_ASSIGN(TopLevelWindowMapping);
67};
68
69// Message pump hook function that monitors for WM_MOVE and WM_MOVING
70// messages on a top-level window, and passes notification to the appropriate
71// Chrome-Frame instances.
72LRESULT CALLBACK TopWindowProc(int code, WPARAM wparam, LPARAM lparam) {
73 CWPSTRUCT *info = reinterpret_cast<CWPSTRUCT*>(lparam);
74 const UINT &message = info->message;
75 const HWND &message_hwnd = info->hwnd;
76
77 switch (message) {
78 case WM_MOVE:
79 case WM_MOVING: {
80 TopLevelWindowMapping::WindowList cf_instances =
81 TopLevelWindowMapping::instance()->GetInstances(message_hwnd);
82 TopLevelWindowMapping::WindowList::iterator
83 iter(cf_instances.begin()), end(cf_instances.end());
84 for (;iter != end; ++iter) {
85 PostMessage(*iter, WM_HOST_MOVED_NOTIFICATION, NULL, NULL);
86 }
87 break;
88 }
89 default:
90 break;
91 }
92
93 return CallNextHookEx(0, code, wparam, lparam);
94}
95
96HHOOK InstallLocalWindowHook(HWND window) {
97 if (!window)
98 return NULL;
99
100 DWORD proc_thread = ::GetWindowThreadProcessId(window, NULL);
101 if (!proc_thread)
102 return NULL;
103
104 // Note that this hook is installed as a LOCAL hook.
105 return ::SetWindowsHookEx(WH_CALLWNDPROC,
106 TopWindowProc,
107 NULL,
108 proc_thread);
109}
110
111} // unnamed namespace
112
113ChromeFrameActivex::ChromeFrameActivex()
114 : chrome_wndproc_hook_(NULL) {
[email protected]f7817822009-09-24 05:11:58115}
116
117HRESULT ChromeFrameActivex::FinalConstruct() {
118 HRESULT hr = Base::FinalConstruct();
119 if (FAILED(hr))
120 return hr;
121
122 // No need to call FireOnChanged at this point since nobody will be listening.
123 ready_state_ = READYSTATE_LOADING;
124 return S_OK;
125}
126
127ChromeFrameActivex::~ChromeFrameActivex() {
128 // We expect these to be released during a call to SetClientSite(NULL).
[email protected]efd4dfc22010-03-26 20:14:40129 DCHECK_EQ(0, onmessage_.size());
130 DCHECK_EQ(0, onloaderror_.size());
131 DCHECK_EQ(0, onload_.size());
132 DCHECK_EQ(0, onreadystatechanged_.size());
133 DCHECK_EQ(0, onextensionready_.size());
[email protected]3eb07da2010-02-01 19:48:36134
[email protected]2b19e2fe2010-02-16 02:24:18135 if (chrome_wndproc_hook_) {
136 BOOL unhook_success = ::UnhookWindowsHookEx(chrome_wndproc_hook_);
137 DCHECK(unhook_success);
138 }
139
[email protected]3eb07da2010-02-01 19:48:36140 // ChromeFramePlugin::Uninitialize()
141 Base::Uninitialize();
[email protected]f7817822009-09-24 05:11:58142}
143
144LRESULT ChromeFrameActivex::OnCreate(UINT message, WPARAM wparam, LPARAM lparam,
145 BOOL& handled) {
146 Base::OnCreate(message, wparam, lparam, handled);
[email protected]2b19e2fe2010-02-16 02:24:18147 // Install the notification hook on the top-level window, so that we can
148 // be notified on move events. Note that the return value is not checked.
149 // This hook is installed here, as opposed to during IOleObject_SetClientSite
150 // because m_hWnd has not yet been assigned during the SetSite call.
151 InstallTopLevelHook(m_spClientSite);
152 return 0;
153}
154
155LRESULT ChromeFrameActivex::OnHostMoved(UINT message, WPARAM wparam,
156 LPARAM lparam, BOOL& handled) {
157 Base::OnHostMoved();
[email protected]f7817822009-09-24 05:11:58158 return 0;
159}
160
[email protected]f7817822009-09-24 05:11:58161HRESULT ChromeFrameActivex::GetContainingDocument(IHTMLDocument2** doc) {
162 ScopedComPtr<IOleContainer> container;
163 HRESULT hr = m_spClientSite->GetContainer(container.Receive());
164 if (container)
165 hr = container.QueryInterface(doc);
166 return hr;
167}
168
169HRESULT ChromeFrameActivex::GetDocumentWindow(IHTMLWindow2** window) {
170 ScopedComPtr<IHTMLDocument2> document;
171 HRESULT hr = GetContainingDocument(document.Receive());
172 if (document)
173 hr = document->get_parentWindow(window);
174 return hr;
175}
176
177void ChromeFrameActivex::OnLoad(int tab_handle, const GURL& gurl) {
178 ScopedComPtr<IDispatch> event;
179 std::string url = gurl.spec();
180 if (SUCCEEDED(CreateDomEvent("event", url, "", event.Receive())))
181 Fire_onload(event);
182
183 FireEvent(onload_, url);
[email protected]a1800e82009-11-19 00:53:23184 Base::OnLoad(tab_handle, gurl);
[email protected]f7817822009-09-24 05:11:58185}
186
187void ChromeFrameActivex::OnLoadFailed(int error_code, const std::string& url) {
188 ScopedComPtr<IDispatch> event;
189 if (SUCCEEDED(CreateDomEvent("event", url, "", event.Receive())))
190 Fire_onloaderror(event);
191
192 FireEvent(onloaderror_, url);
[email protected]a1800e82009-11-19 00:53:23193 Base::OnLoadFailed(error_code, url);
[email protected]f7817822009-09-24 05:11:58194}
195
196void ChromeFrameActivex::OnMessageFromChromeFrame(int tab_handle,
197 const std::string& message,
198 const std::string& origin,
199 const std::string& target) {
200 DLOG(INFO) << __FUNCTION__;
201
202 if (target.compare("*") != 0) {
203 bool drop = true;
204
205 if (is_privileged_) {
206 // Forward messages if the control is in privileged mode.
207 ScopedComPtr<IDispatch> message_event;
208 if (SUCCEEDED(CreateDomEvent("message", message, origin,
209 message_event.Receive()))) {
210 ScopedBstr target_bstr(UTF8ToWide(target).c_str());
211 Fire_onprivatemessage(message_event, target_bstr);
212
213 FireEvent(onprivatemessage_, message_event, target_bstr);
214 }
215 } else {
216 if (HaveSameOrigin(target, document_url_)) {
217 drop = false;
218 } else {
219 DLOG(WARNING) << "Dropping posted message since target doesn't match "
220 "the current document's origin. target=" << target;
221 }
222 }
223
224 if (drop)
225 return;
226 }
227
228 ScopedComPtr<IDispatch> message_event;
229 if (SUCCEEDED(CreateDomEvent("message", message, origin,
230 message_event.Receive()))) {
231 Fire_onmessage(message_event);
232
233 FireEvent(onmessage_, message_event);
234
235 ScopedVariant event_var;
236 event_var.Set(static_cast<IDispatch*>(message_event));
237 InvokeScriptFunction(onmessage_handler_, event_var.AsInput());
238 }
239}
240
241void ChromeFrameActivex::OnAutomationServerLaunchFailed(
242 AutomationLaunchResult reason, const std::string& server_version) {
243 Base::OnAutomationServerLaunchFailed(reason, server_version);
244
245 if (reason == AUTOMATION_VERSION_MISMATCH) {
246 DisplayVersionMismatchWarning(m_hWnd, server_version);
247 }
248}
249
[email protected]00f6b772009-10-23 17:03:41250void ChromeFrameActivex::OnExtensionInstalled(
251 const FilePath& path,
252 void* user_data,
253 AutomationMsg_ExtensionResponseValues response) {
254 ScopedBstr path_str(path.value().c_str());
255 Fire_onextensionready(path_str, response);
256}
257
[email protected]a1e62d12010-03-16 02:18:43258void ChromeFrameActivex::OnGetEnabledExtensionsComplete(
259 void* user_data,
260 const std::vector<FilePath>& extension_directories) {
261 SAFEARRAY* sa = ::SafeArrayCreateVector(VT_BSTR, 0,
262 extension_directories.size());
263 sa->fFeatures = sa->fFeatures | FADF_BSTR;
264 ::SafeArrayLock(sa);
265
266 for (size_t i = 0; i < extension_directories.size(); ++i) {
267 LONG index = static_cast<LONG>(i);
268 ::SafeArrayPutElement(sa, &index, reinterpret_cast<void*>(
269 CComBSTR(extension_directories[i].ToWStringHack().c_str()).Detach()));
270 }
271
272 Fire_ongetenabledextensionscomplete(sa);
273 ::SafeArrayUnlock(sa);
274 ::SafeArrayDestroy(sa);
275}
276
[email protected]efd4dfc22010-03-26 20:14:40277void ChromeFrameActivex::OnChannelError() {
278 Fire_onchannelerror();
279}
280
281HRESULT ChromeFrameActivex::OnDraw(ATL_DRAWINFO& draw_info) { // NOLINT
[email protected]f7817822009-09-24 05:11:58282 HRESULT hr = S_OK;
283 int dc_type = ::GetObjectType(draw_info.hicTargetDev);
284 if (dc_type == OBJ_ENHMETADC) {
285 RECT print_bounds = {0};
286 print_bounds.left = draw_info.prcBounds->left;
287 print_bounds.right = draw_info.prcBounds->right;
288 print_bounds.top = draw_info.prcBounds->top;
289 print_bounds.bottom = draw_info.prcBounds->bottom;
290
291 automation_client_->Print(draw_info.hdcDraw, print_bounds);
292 } else {
293 hr = Base::OnDraw(draw_info);
294 }
295
296 return hr;
297}
298
299STDMETHODIMP ChromeFrameActivex::Load(IPropertyBag* bag, IErrorLog* error_log) {
300 DCHECK(bag);
301
302 const wchar_t* event_props[] = {
303 (L"onload"),
304 (L"onloaderror"),
305 (L"onmessage"),
306 (L"onreadystatechanged"),
307 };
308
309 ScopedComPtr<IHTMLObjectElement> obj_element;
310 GetObjectElement(obj_element.Receive());
311
312 ScopedBstr object_id;
313 GetObjectScriptId(obj_element, object_id.Receive());
314
315 ScopedComPtr<IHTMLElement2> element;
316 element.QueryFrom(obj_element);
317 HRESULT hr = S_OK;
318
319 for (int i = 0; SUCCEEDED(hr) && i < arraysize(event_props); ++i) {
320 ScopedBstr prop(event_props[i]);
321 ScopedVariant value;
322 if (SUCCEEDED(bag->Read(prop, value.Receive(), error_log))) {
323 if (value.type() != VT_BSTR ||
324 FAILED(hr = CreateScriptBlockForEvent(element, object_id,
325 V_BSTR(&value), prop))) {
326 DLOG(ERROR) << "Failed to create script block for " << prop
327 << StringPrintf(L"hr=0x%08X, vt=%i", hr, value.type());
328 } else {
329 DLOG(INFO) << "script block created for event " << prop <<
330 StringPrintf(" (0x%08X)", hr) << " connections: " <<
331 ProxyDIChromeFrameEvents<ChromeFrameActivex>::m_vec.GetSize();
332 }
333 } else {
334 DLOG(INFO) << "event property " << prop << " not in property bag";
335 }
336 }
337
338 ScopedVariant src;
339 if (SUCCEEDED(bag->Read(StackBstr(L"src"), src.Receive(), error_log))) {
340 if (src.type() == VT_BSTR) {
341 hr = put_src(V_BSTR(&src));
342 DCHECK(hr != E_UNEXPECTED);
343 }
344 }
345
346 ScopedVariant use_chrome_network;
347 if (SUCCEEDED(bag->Read(StackBstr(L"useChromeNetwork"),
348 use_chrome_network.Receive(), error_log))) {
349 VariantChangeType(use_chrome_network.AsInput(),
350 use_chrome_network.AsInput(),
351 0, VT_BOOL);
352 if (use_chrome_network.type() == VT_BOOL) {
353 hr = put_useChromeNetwork(V_BOOL(&use_chrome_network));
354 DCHECK(hr != E_UNEXPECTED);
355 }
356 }
357
358 DLOG_IF(ERROR, FAILED(hr))
359 << StringPrintf("Failed to load property bag: 0x%08X", hr);
360
361 return hr;
362}
363
364const wchar_t g_activex_mixed_content_error[] = {
365 L"data:text/html,<html><body><b>ChromeFrame Security Error<br><br>"
366 L"Cannot navigate to HTTP url when document URL is HTTPS</body></html>"};
[email protected]2b8fd322009-10-02 00:00:59367
[email protected]f7817822009-09-24 05:11:58368STDMETHODIMP ChromeFrameActivex::put_src(BSTR src) {
369 GURL document_url(GetDocumentUrl());
370 if (document_url.SchemeIsSecure()) {
371 GURL source_url(src);
372 if (!source_url.SchemeIsSecure()) {
373 Base::put_src(ScopedBstr(g_activex_mixed_content_error));
374 return E_ACCESSDENIED;
375 }
376 }
377 return Base::put_src(src);
378}
379
380HRESULT ChromeFrameActivex::IOleObject_SetClientSite(
381 IOleClientSite* client_site) {
382 HRESULT hr = Base::IOleObject_SetClientSite(client_site);
383 if (FAILED(hr) || !client_site) {
384 EventHandlers* handlers[] = {
385 &onmessage_,
386 &onloaderror_,
387 &onload_,
388 &onreadystatechanged_,
[email protected]00f6b772009-10-23 17:03:41389 &onextensionready_,
[email protected]f7817822009-09-24 05:11:58390 };
391
392 for (int i = 0; i < arraysize(handlers); ++i)
393 handlers[i]->clear();
394
395 // Drop privileged mode on uninitialization.
396 is_privileged_ = false;
397 } else {
398 ScopedComPtr<IHTMLDocument2> document;
399 GetContainingDocument(document.Receive());
400 if (document) {
401 ScopedBstr url;
402 if (SUCCEEDED(document->get_URL(url.Receive())))
403 WideToUTF8(url, url.Length(), &document_url_);
404 }
405
406 // Probe to see whether the host implements the privileged service.
407 ScopedComPtr<IChromeFramePrivileged> service;
408 HRESULT service_hr = DoQueryService(SID_ChromeFramePrivileged, client_site,
409 service.Receive());
410 if (SUCCEEDED(service_hr) && service) {
411 // Does the host want privileged mode?
412 boolean wants_privileged = false;
413 service_hr = service->GetWantsPrivileged(&wants_privileged);
414
415 if (SUCCEEDED(service_hr) && wants_privileged)
416 is_privileged_ = true;
[email protected]7403d38f2010-03-22 22:50:49417
418 url_fetcher_.set_privileged_mode(is_privileged_);
[email protected]f7817822009-09-24 05:11:58419 }
420
421 std::wstring chrome_extra_arguments;
422 std::wstring profile_name(GetHostProcessName(false));
423 if (is_privileged_) {
424 // Does the host want to provide extra arguments?
425 ScopedBstr extra_arguments_arg;
426 service_hr = service->GetChromeExtraArguments(
427 extra_arguments_arg.Receive());
428 if (S_OK == service_hr && extra_arguments_arg)
429 chrome_extra_arguments.assign(extra_arguments_arg,
430 extra_arguments_arg.Length());
431
[email protected]50f53162009-10-23 19:16:20432 ScopedBstr automated_functions_arg;
433 service_hr = service->GetExtensionApisToAutomate(
434 automated_functions_arg.Receive());
435 if (S_OK == service_hr && automated_functions_arg) {
436 std::string automated_functions(
437 WideToASCII(static_cast<BSTR>(automated_functions_arg)));
438 functions_enabled_.clear();
439 // SplitString writes one empty entry for blank strings, so we need this
440 // to allow specifying zero automation of API functions.
441 if (!automated_functions.empty())
442 SplitString(automated_functions, ',', &functions_enabled_);
443 }
444
[email protected]f7817822009-09-24 05:11:58445 ScopedBstr profile_name_arg;
446 service_hr = service->GetChromeProfileName(profile_name_arg.Receive());
447 if (S_OK == service_hr && profile_name_arg)
448 profile_name.assign(profile_name_arg, profile_name_arg.Length());
449 }
450
[email protected]3eb07da2010-02-01 19:48:36451 url_fetcher_.set_frame_busting(!is_privileged_);
452 automation_client_->SetUrlFetcher(&url_fetcher_);
[email protected]f7817822009-09-24 05:11:58453 if (!InitializeAutomation(profile_name, chrome_extra_arguments,
[email protected]f7019302010-03-26 19:58:32454 IsIEInPrivate(), true)) {
[email protected]f7817822009-09-24 05:11:58455 return E_FAIL;
456 }
[email protected]f7817822009-09-24 05:11:58457 }
458
459 return hr;
460}
461
462HRESULT ChromeFrameActivex::GetObjectScriptId(IHTMLObjectElement* object_elem,
463 BSTR* id) {
464 DCHECK(object_elem != NULL);
465 DCHECK(id != NULL);
466
467 HRESULT hr = E_FAIL;
468 if (object_elem) {
469 ScopedComPtr<IHTMLElement> elem;
470 hr = elem.QueryFrom(object_elem);
471 if (elem) {
472 hr = elem->get_id(id);
473 }
474 }
475
476 return hr;
477}
478
479HRESULT ChromeFrameActivex::GetObjectElement(IHTMLObjectElement** element) {
480 DCHECK(m_spClientSite);
481 if (!m_spClientSite)
482 return E_UNEXPECTED;
483
484 ScopedComPtr<IOleControlSite> site;
485 HRESULT hr = site.QueryFrom(m_spClientSite);
486 if (site) {
487 ScopedComPtr<IDispatch> disp;
488 hr = site->GetExtendedControl(disp.Receive());
489 if (disp) {
490 hr = disp.QueryInterface(element);
491 } else {
492 DCHECK(FAILED(hr));
493 }
494 }
495
496 return hr;
497}
498
499HRESULT ChromeFrameActivex::CreateScriptBlockForEvent(
500 IHTMLElement2* insert_after, BSTR instance_id, BSTR script,
501 BSTR event_name) {
502 DCHECK(insert_after);
[email protected]efd4dfc22010-03-26 20:14:40503 DCHECK_GT(::SysStringLen(event_name), 0UL); // should always have this
[email protected]f7817822009-09-24 05:11:58504
505 // This might be 0 if not specified in the HTML document.
506 if (!::SysStringLen(instance_id)) {
507 // TODO(tommi): Should we give ourselves an ID if this happens?
508 NOTREACHED() << "Need to handle this";
509 return E_INVALIDARG;
510 }
511
512 ScopedComPtr<IHTMLDocument2> document;
513 HRESULT hr = GetContainingDocument(document.Receive());
514 if (SUCCEEDED(hr)) {
515 ScopedComPtr<IHTMLElement> element, new_element;
516 document->createElement(StackBstr(L"script"), element.Receive());
517 if (element) {
518 ScopedComPtr<IHTMLScriptElement> script_element;
519 if (SUCCEEDED(hr = script_element.QueryFrom(element))) {
520 script_element->put_htmlFor(instance_id);
521 script_element->put_event(event_name);
522 script_element->put_text(script);
523
524 hr = insert_after->insertAdjacentElement(StackBstr(L"afterEnd"),
525 element,
526 new_element.Receive());
527 }
528 }
529 }
530
531 return hr;
532}
533
[email protected]f7817822009-09-24 05:11:58534void ChromeFrameActivex::FireEvent(const EventHandlers& handlers,
535 const std::string& arg) {
536 if (handlers.size()) {
537 ScopedComPtr<IDispatch> event;
538 if (SUCCEEDED(CreateDomEvent("event", arg, "", event.Receive()))) {
539 FireEvent(handlers, event);
540 }
541 }
542}
543
544void ChromeFrameActivex::FireEvent(const EventHandlers& handlers,
545 IDispatch* event) {
546 DCHECK(event != NULL);
547 VARIANT arg = { VT_DISPATCH };
548 arg.pdispVal = event;
549 DISPPARAMS params = { &arg, NULL, 1, 0 };
550 for (EventHandlers::const_iterator it = handlers.begin();
551 it != handlers.end();
552 ++it) {
553 HRESULT hr = (*it)->Invoke(DISPID_VALUE, IID_NULL, LOCALE_USER_DEFAULT,
554 DISPATCH_METHOD, &params, NULL, NULL, NULL);
555 // 0x80020101 == SCRIPT_E_REPORTED.
556 // When the script we're invoking has an error, we get this error back.
557 DLOG_IF(ERROR, FAILED(hr) && hr != 0x80020101)
558 << StringPrintf(L"Failed to invoke script: 0x%08X", hr);
559 }
560}
561
562void ChromeFrameActivex::FireEvent(const EventHandlers& handlers,
563 IDispatch* event, BSTR target) {
564 DCHECK(event != NULL);
565 // Arguments in reverse order to event handler function declaration,
566 // because that's what DISPPARAMS requires.
567 VARIANT args[2] = { { VT_BSTR }, { VT_DISPATCH }, };
568 args[0].bstrVal = target;
569 args[1].pdispVal = event;
570 DISPPARAMS params = { args, NULL, arraysize(args), 0 };
571 for (EventHandlers::const_iterator it = handlers.begin();
572 it != handlers.end();
573 ++it) {
574 HRESULT hr = (*it)->Invoke(DISPID_VALUE, IID_NULL, LOCALE_USER_DEFAULT,
575 DISPATCH_METHOD, &params, NULL, NULL, NULL);
576 // 0x80020101 == SCRIPT_E_REPORTED.
577 // When the script we're invoking has an error, we get this error back.
578 DLOG_IF(ERROR, FAILED(hr) && hr != 0x80020101)
579 << StringPrintf(L"Failed to invoke script: 0x%08X", hr);
580 }
581}
[email protected]2b19e2fe2010-02-16 02:24:18582
583HRESULT ChromeFrameActivex::InstallTopLevelHook(IOleClientSite* client_site) {
584 // Get the parent window of the site, and install our hook on the topmost
585 // window of the parent.
586 ScopedComPtr<IOleWindow> ole_window;
587 HRESULT hr = ole_window.QueryFrom(client_site);
588 if (FAILED(hr))
589 return hr;
590
591 HWND parent_wnd;
592 hr = ole_window->GetWindow(&parent_wnd);
593 if (FAILED(hr))
594 return hr;
595
596 HWND top_window = ::GetAncestor(parent_wnd, GA_ROOT);
597 chrome_wndproc_hook_ = InstallLocalWindowHook(top_window);
598 if (chrome_wndproc_hook_)
599 TopLevelWindowMapping::instance()->AddMapping(top_window, m_hWnd);
600
601 return chrome_wndproc_hook_ ? S_OK : E_FAIL;
602}
[email protected]c442e9f2010-04-29 18:37:44603
[email protected]ca982ec42010-05-02 14:47:14604HRESULT ChromeFrameActivex::registerBhoIfNeeded() {
605 if (!m_spUnkSite) {
[email protected]c442e9f2010-04-29 18:37:44606 NOTREACHED() << "Invalid client site";
607 return E_FAIL;
608 }
609
610 if (NavigationManager::GetThreadInstance() != NULL) {
611 DLOG(INFO) << "BHO already loaded";
612 return S_OK;
613 }
614
615 ScopedComPtr<IWebBrowser2> web_browser2;
[email protected]ca982ec42010-05-02 14:47:14616 HRESULT hr = DoQueryService(SID_SWebBrowserApp, m_spUnkSite,
[email protected]c442e9f2010-04-29 18:37:44617 web_browser2.Receive());
618 if (FAILED(hr) || web_browser2.get() == NULL) {
619 DLOG(WARNING) << "Failed to get IWebBrowser2 from client site. Error:"
620 << StringPrintf(" 0x%08X", hr);
621 return hr;
622 }
623
624 wchar_t bho_class_id_as_string[MAX_PATH] = {0};
625 StringFromGUID2(CLSID_ChromeFrameBHO, bho_class_id_as_string,
626 arraysize(bho_class_id_as_string));
627
628 ScopedComPtr<IObjectWithSite> bho;
629 hr = bho.CreateInstance(CLSID_ChromeFrameBHO, NULL, CLSCTX_INPROC_SERVER);
630 if (FAILED(hr)) {
631 NOTREACHED() << "Failed to register ChromeFrame BHO. Error:"
632 << StringPrintf(" 0x%08X", hr);
633 return hr;
634 }
635
636 hr = bho->SetSite(web_browser2);
637 if (FAILED(hr)) {
638 NOTREACHED() << "ChromeFrame BHO SetSite failed. Error:"
639 << StringPrintf(" 0x%08X", hr);
640 return hr;
641 }
642
643 web_browser2->PutProperty(ScopedBstr(bho_class_id_as_string),
644 ScopedVariant(bho));
645 return S_OK;
646}