Use Histogram algorithm to calculate DNS timeout.
[chromium-blink-merge.git] / chrome_frame / chrome_frame_activex.cc
blobb99ba0bc27fd4064dee91d0768dc2f6500ccd5ca
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.
5 #include "chrome_frame/chrome_frame_activex.h"
7 #include <wininet.h>
9 #include <algorithm>
10 #include <map>
12 #include "base/basictypes.h"
13 #include "base/command_line.h"
14 #include "base/debug/trace_event.h"
15 #include "base/logging.h"
16 #include "base/memory/singleton.h"
17 #include "base/path_service.h"
18 #include "base/process_util.h"
19 #include "base/strings/string_split.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "base/win/scoped_bstr.h"
24 #include "base/win/scoped_variant.h"
25 #include "chrome/common/automation_messages.h"
26 #include "chrome/common/chrome_constants.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/test/automation/tab_proxy.h"
29 #include "chrome_frame/utils.h"
30 #include "url/gurl.h"
32 namespace {
34 // Class used to maintain a mapping from top-level windows to ChromeFrameActivex
35 // instances.
36 class TopLevelWindowMapping {
37 public:
38 typedef std::vector<HWND> WindowList;
40 static TopLevelWindowMapping* GetInstance() {
41 return Singleton<TopLevelWindowMapping>::get();
44 // Add |cf_window| to the set of windows registered under |top_window|.
45 void AddMapping(HWND top_window, HWND cf_window) {
46 top_window_map_lock_.Lock();
47 top_window_map_[top_window].push_back(cf_window);
48 top_window_map_lock_.Unlock();
51 // Return the set of Chrome-Frame instances under |window|.
52 WindowList GetInstances(HWND window) {
53 top_window_map_lock_.Lock();
54 WindowList list = top_window_map_[window];
55 top_window_map_lock_.Unlock();
56 return list;
59 private:
60 // Constructor is private as this class it to be used as a singleton.
61 // See static method instance().
62 TopLevelWindowMapping() {}
64 friend struct DefaultSingletonTraits<TopLevelWindowMapping>;
66 typedef std::map<HWND, WindowList> TopWindowMap;
67 TopWindowMap top_window_map_;
69 CComAutoCriticalSection top_window_map_lock_;
71 DISALLOW_COPY_AND_ASSIGN(TopLevelWindowMapping);
74 // Message pump hook function that monitors for WM_MOVE and WM_MOVING
75 // messages on a top-level window, and passes notification to the appropriate
76 // Chrome-Frame instances.
77 LRESULT CALLBACK TopWindowProc(int code, WPARAM wparam, LPARAM lparam) {
78 CWPSTRUCT* info = reinterpret_cast<CWPSTRUCT*>(lparam);
79 const UINT &message = info->message;
80 const HWND &message_hwnd = info->hwnd;
82 switch (message) {
83 case WM_MOVE:
84 case WM_MOVING: {
85 TopLevelWindowMapping::WindowList cf_instances =
86 TopLevelWindowMapping::GetInstance()->GetInstances(message_hwnd);
87 TopLevelWindowMapping::WindowList::iterator
88 iter(cf_instances.begin()), end(cf_instances.end());
89 for (; iter != end; ++iter) {
90 PostMessage(*iter, WM_HOST_MOVED_NOTIFICATION, NULL, NULL);
92 break;
94 default:
95 break;
98 return CallNextHookEx(0, code, wparam, lparam);
101 HHOOK InstallLocalWindowHook(HWND window) {
102 if (!window)
103 return NULL;
105 DWORD proc_thread = ::GetWindowThreadProcessId(window, NULL);
106 if (!proc_thread)
107 return NULL;
109 // Note that this hook is installed as a LOCAL hook.
110 return ::SetWindowsHookEx(WH_CALLWNDPROC,
111 TopWindowProc,
112 NULL,
113 proc_thread);
116 } // unnamed namespace
118 namespace chrome_frame {
119 std::string ActiveXCreateUrl(const GURL& parsed_url,
120 const AttachExternalTabParams& params) {
121 return base::StringPrintf(
122 "%hs?attach_external_tab&%I64u&%d&%d&%d&%d&%d&%hs",
123 parsed_url.GetOrigin().spec().c_str(),
124 params.cookie,
125 params.disposition,
126 params.dimensions.x(),
127 params.dimensions.y(),
128 params.dimensions.width(),
129 params.dimensions.height(),
130 params.profile_name.c_str());
133 int GetDisposition(const AttachExternalTabParams& params) {
134 return params.disposition;
137 void GetMiniContextMenuData(UINT cmd,
138 const MiniContextMenuParams& params,
139 GURL* referrer,
140 GURL* url) {
141 *referrer = params.frame_url.is_empty() ? params.page_url : params.frame_url;
142 *url = (cmd == IDS_CONTENT_CONTEXT_SAVELINKAS ?
143 params.link_url : params.src_url);
146 } // namespace chrome_frame
148 ChromeFrameActivex::ChromeFrameActivex()
149 : chrome_wndproc_hook_(NULL),
150 attaching_to_existing_cf_tab_(false) {
151 TRACE_EVENT_BEGIN_ETW("chromeframe.createactivex", this, "");
154 HRESULT ChromeFrameActivex::FinalConstruct() {
155 HRESULT hr = Base::FinalConstruct();
156 if (FAILED(hr))
157 return hr;
159 // No need to call FireOnChanged at this point since nobody will be listening.
160 ready_state_ = READYSTATE_LOADING;
161 return S_OK;
164 ChromeFrameActivex::~ChromeFrameActivex() {
165 // We expect these to be released during a call to SetClientSite(NULL).
166 DCHECK_EQ(0u, onmessage_.size());
167 DCHECK_EQ(0u, onloaderror_.size());
168 DCHECK_EQ(0u, onload_.size());
169 DCHECK_EQ(0u, onreadystatechanged_.size());
170 DCHECK_EQ(0u, onextensionready_.size());
172 if (chrome_wndproc_hook_) {
173 BOOL unhook_success = ::UnhookWindowsHookEx(chrome_wndproc_hook_);
174 DCHECK(unhook_success);
177 // ChromeFramePlugin::Uninitialize()
178 Base::Uninitialize();
180 TRACE_EVENT_END_ETW("chromeframe.createactivex", this, "");
183 LRESULT ChromeFrameActivex::OnCreate(UINT message, WPARAM wparam, LPARAM lparam,
184 BOOL& handled) {
185 Base::OnCreate(message, wparam, lparam, handled);
186 // Install the notification hook on the top-level window, so that we can
187 // be notified on move events. Note that the return value is not checked.
188 // This hook is installed here, as opposed to during IOleObject_SetClientSite
189 // because m_hWnd has not yet been assigned during the SetSite call.
190 InstallTopLevelHook(m_spClientSite);
191 return 0;
194 LRESULT ChromeFrameActivex::OnHostMoved(UINT message, WPARAM wparam,
195 LPARAM lparam, BOOL& handled) {
196 Base::OnHostMoved();
197 return 0;
200 HRESULT ChromeFrameActivex::GetContainingDocument(IHTMLDocument2** doc) {
201 base::win::ScopedComPtr<IOleContainer> container;
202 HRESULT hr = m_spClientSite->GetContainer(container.Receive());
203 if (container)
204 hr = container.QueryInterface(doc);
205 return hr;
208 HRESULT ChromeFrameActivex::GetDocumentWindow(IHTMLWindow2** window) {
209 base::win::ScopedComPtr<IHTMLDocument2> document;
210 HRESULT hr = GetContainingDocument(document.Receive());
211 if (document)
212 hr = document->get_parentWindow(window);
213 return hr;
216 void ChromeFrameActivex::OnLoad(const GURL& gurl) {
217 base::win::ScopedComPtr<IDispatch> event;
218 std::string url = gurl.spec();
219 if (SUCCEEDED(CreateDomEvent("event", url, "", event.Receive())))
220 Fire_onload(event);
222 FireEvent(onload_, url);
223 Base::OnLoad(gurl);
226 void ChromeFrameActivex::OnLoadFailed(int error_code, const std::string& url) {
227 base::win::ScopedComPtr<IDispatch> event;
228 if (SUCCEEDED(CreateDomEvent("event", url, "", event.Receive())))
229 Fire_onloaderror(event);
231 FireEvent(onloaderror_, url);
232 Base::OnLoadFailed(error_code, url);
235 void ChromeFrameActivex::OnMessageFromChromeFrame(const std::string& message,
236 const std::string& origin,
237 const std::string& target) {
238 DVLOG(1) << __FUNCTION__;
240 if (target.compare("*") != 0) {
241 bool drop = true;
243 if (is_privileged()) {
244 // Forward messages if the control is in privileged mode.
245 base::win::ScopedComPtr<IDispatch> message_event;
246 if (SUCCEEDED(CreateDomEvent("message", message, origin,
247 message_event.Receive()))) {
248 base::win::ScopedBstr target_bstr(UTF8ToWide(target).c_str());
249 Fire_onprivatemessage(message_event, target_bstr);
251 FireEvent(onprivatemessage_, message_event, target_bstr);
253 } else {
254 if (HaveSameOrigin(target, document_url_)) {
255 drop = false;
256 } else {
257 DLOG(WARNING) << "Dropping posted message since target doesn't match "
258 "the current document's origin. target=" << target;
262 if (drop)
263 return;
266 base::win::ScopedComPtr<IDispatch> message_event;
267 if (SUCCEEDED(CreateDomEvent("message", message, origin,
268 message_event.Receive()))) {
269 Fire_onmessage(message_event);
271 FireEvent(onmessage_, message_event);
273 base::win::ScopedVariant event_var;
274 event_var.Set(static_cast<IDispatch*>(message_event));
275 InvokeScriptFunction(onmessage_handler_, event_var.AsInput());
279 bool ChromeFrameActivex::ShouldShowVersionMismatchDialog(
280 bool is_privileged,
281 IOleClientSite* client_site) {
282 if (!is_privileged) {
283 return true;
286 if (client_site) {
287 base::win::ScopedComPtr<IChromeFramePrivileged> service;
288 HRESULT hr = DoQueryService(SID_ChromeFramePrivileged,
289 client_site,
290 service.Receive());
291 if (SUCCEEDED(hr) && service) {
292 return (S_FALSE != service->ShouldShowVersionMismatchDialog());
296 NOTREACHED();
297 return true;
300 void ChromeFrameActivex::OnAutomationServerLaunchFailed(
301 AutomationLaunchResult reason, const std::string& server_version) {
302 Base::OnAutomationServerLaunchFailed(reason, server_version);
304 if (reason == AUTOMATION_VERSION_MISMATCH &&
305 ShouldShowVersionMismatchDialog(is_privileged(), m_spClientSite)) {
306 UMA_HISTOGRAM_COUNTS("ChromeFrame.VersionMismatchDisplayed", 1);
307 DisplayVersionMismatchWarning(m_hWnd, server_version);
311 void ChromeFrameActivex::OnChannelError() {
312 Fire_onchannelerror();
315 HRESULT ChromeFrameActivex::OnDraw(ATL_DRAWINFO& draw_info) { // NOLINT
316 HRESULT hr = S_OK;
317 int dc_type = ::GetObjectType(draw_info.hicTargetDev);
318 if (dc_type == OBJ_ENHMETADC) {
319 RECT print_bounds = {0};
320 print_bounds.left = draw_info.prcBounds->left;
321 print_bounds.right = draw_info.prcBounds->right;
322 print_bounds.top = draw_info.prcBounds->top;
323 print_bounds.bottom = draw_info.prcBounds->bottom;
325 automation_client_->Print(draw_info.hdcDraw, print_bounds);
326 } else {
327 hr = Base::OnDraw(draw_info);
330 return hr;
333 STDMETHODIMP ChromeFrameActivex::Load(IPropertyBag* bag, IErrorLog* error_log) {
334 DCHECK(bag);
336 const wchar_t* event_props[] = {
337 (L"onload"),
338 (L"onloaderror"),
339 (L"onmessage"),
340 (L"onreadystatechanged"),
343 base::win::ScopedComPtr<IHTMLObjectElement> obj_element;
344 GetObjectElement(obj_element.Receive());
346 base::win::ScopedBstr object_id;
347 GetObjectScriptId(obj_element, object_id.Receive());
349 base::win::ScopedComPtr<IHTMLElement2> element;
350 element.QueryFrom(obj_element);
351 HRESULT hr = S_OK;
353 for (int i = 0; SUCCEEDED(hr) && i < arraysize(event_props); ++i) {
354 base::win::ScopedBstr prop(event_props[i]);
355 base::win::ScopedVariant value;
356 if (SUCCEEDED(bag->Read(prop, value.Receive(), error_log))) {
357 if (value.type() != VT_BSTR ||
358 FAILED(hr = CreateScriptBlockForEvent(element, object_id,
359 V_BSTR(&value), prop))) {
360 DLOG(ERROR) << "Failed to create script block for " << prop
361 << base::StringPrintf(L"hr=0x%08X, vt=%i", hr,
362 value.type());
363 } else {
364 DVLOG(1) << "script block created for event " << prop
365 << base::StringPrintf(" (0x%08X)", hr) << " connections: " <<
366 ProxyDIChromeFrameEvents<ChromeFrameActivex>::m_vec.GetSize();
368 } else {
369 DVLOG(1) << "event property " << prop << " not in property bag";
373 base::win::ScopedVariant src;
374 if (SUCCEEDED(bag->Read(base::win::ScopedBstr(L"src"), src.Receive(),
375 error_log))) {
376 if (src.type() == VT_BSTR) {
377 hr = put_src(V_BSTR(&src));
378 DCHECK(hr != E_UNEXPECTED);
382 base::win::ScopedVariant use_chrome_network;
383 if (SUCCEEDED(bag->Read(base::win::ScopedBstr(L"useChromeNetwork"),
384 use_chrome_network.Receive(), error_log))) {
385 VariantChangeType(use_chrome_network.AsInput(),
386 use_chrome_network.AsInput(),
387 0, VT_BOOL);
388 if (use_chrome_network.type() == VT_BOOL) {
389 hr = put_useChromeNetwork(V_BOOL(&use_chrome_network));
390 DCHECK(hr != E_UNEXPECTED);
394 DLOG_IF(ERROR, FAILED(hr))
395 << base::StringPrintf("Failed to load property bag: 0x%08X", hr);
397 return hr;
400 const wchar_t g_activex_insecure_content_error[] = {
401 L"data:text/html,<html><body><b>ChromeFrame Security Error<br><br>"
402 L"Cannot navigate to HTTP url when document URL is HTTPS</body></html>"};
404 STDMETHODIMP ChromeFrameActivex::put_src(BSTR src) {
405 GURL document_url(GetDocumentUrl());
406 if (document_url.SchemeIsSecure()) {
407 GURL source_url(src);
408 if (!source_url.SchemeIsSecure()) {
409 Base::put_src(base::win::ScopedBstr(g_activex_insecure_content_error));
410 return E_ACCESSDENIED;
413 HRESULT hr = S_OK;
414 // If we are connecting to an existing ExternalTabContainer instance in
415 // Chrome then we should wait for Chrome to initiate the navigation.
416 if (!attaching_to_existing_cf_tab_) {
417 hr = Base::put_src(src);
418 } else {
419 url_.Reset(::SysAllocString(src));
420 attaching_to_existing_cf_tab_ = false;
422 return S_OK;
425 HRESULT ChromeFrameActivex::IOleObject_SetClientSite(
426 IOleClientSite* client_site) {
427 HRESULT hr = Base::IOleObject_SetClientSite(client_site);
428 if (FAILED(hr) || !client_site) {
429 EventHandlers* handlers[] = {
430 &onmessage_,
431 &onloaderror_,
432 &onload_,
433 &onreadystatechanged_,
434 &onextensionready_,
437 for (int i = 0; i < arraysize(handlers); ++i)
438 handlers[i]->clear();
440 // Drop privileged mode on uninitialization.
441 set_is_privileged(false);
442 } else {
443 base::win::ScopedComPtr<IHTMLDocument2> document;
444 GetContainingDocument(document.Receive());
445 if (document) {
446 base::win::ScopedBstr url;
447 if (SUCCEEDED(document->get_URL(url.Receive())))
448 WideToUTF8(url, url.Length(), &document_url_);
451 // Probe to see whether the host implements the privileged service.
452 base::win::ScopedComPtr<IChromeFramePrivileged> service;
453 HRESULT service_hr = DoQueryService(SID_ChromeFramePrivileged,
454 m_spClientSite,
455 service.Receive());
456 if (SUCCEEDED(service_hr) && service) {
457 // Does the host want privileged mode?
458 boolean wants_privileged = false;
459 service_hr = service->GetWantsPrivileged(&wants_privileged);
461 if (SUCCEEDED(service_hr) && wants_privileged)
462 set_is_privileged(true);
464 url_fetcher_->set_privileged_mode(is_privileged());
467 std::wstring profile_name(GetHostProcessName(false));
468 if (is_privileged()) {
469 base::win::ScopedBstr profile_name_arg;
470 service_hr = service->GetChromeProfileName(profile_name_arg.Receive());
471 if (S_OK == service_hr && profile_name_arg)
472 profile_name.assign(profile_name_arg, profile_name_arg.Length());
475 std::string utf8_url;
476 if (url_.Length()) {
477 WideToUTF8(url_, url_.Length(), &utf8_url);
480 InitializeAutomationSettings();
482 if (service) {
483 base::win::ScopedBstr navigation_url;
484 service->GetNavigationUrl(navigation_url.Receive());
485 if (navigation_url.Length()) {
486 ChromeFrameUrl cf_url;
487 cf_url.Parse(navigation_url.operator BSTR());
488 if (cf_url.attach_to_external_tab()) {
489 automation_client_->AttachExternalTab(cf_url.cookie());
490 attaching_to_existing_cf_tab_ = true;
494 url_fetcher_->set_frame_busting(!is_privileged());
495 automation_client_->SetUrlFetcher(url_fetcher_.get());
496 if (!InitializeAutomation(profile_name, IsIEInPrivate(), true,
497 GURL(utf8_url), GURL(), false)) {
498 DLOG(ERROR) << "Failed to navigate to url:" << utf8_url;
499 return E_FAIL;
502 // Log a metric that Chrome Frame is being used in Widget mode
503 UMA_LAUNCH_TYPE_COUNT(RENDERER_TYPE_CHROME_WIDGET);
506 return hr;
509 HRESULT ChromeFrameActivex::GetObjectScriptId(IHTMLObjectElement* object_elem,
510 BSTR* id) {
511 DCHECK(object_elem != NULL);
512 DCHECK(id != NULL);
514 HRESULT hr = E_FAIL;
515 if (object_elem) {
516 base::win::ScopedComPtr<IHTMLElement> elem;
517 hr = elem.QueryFrom(object_elem);
518 if (elem) {
519 hr = elem->get_id(id);
523 return hr;
526 HRESULT ChromeFrameActivex::GetObjectElement(IHTMLObjectElement** element) {
527 DCHECK(m_spClientSite);
528 if (!m_spClientSite)
529 return E_UNEXPECTED;
531 base::win::ScopedComPtr<IOleControlSite> site;
532 HRESULT hr = site.QueryFrom(m_spClientSite);
533 if (site) {
534 base::win::ScopedComPtr<IDispatch> disp;
535 hr = site->GetExtendedControl(disp.Receive());
536 if (disp) {
537 hr = disp.QueryInterface(element);
538 } else {
539 DCHECK(FAILED(hr));
543 return hr;
546 HRESULT ChromeFrameActivex::CreateScriptBlockForEvent(
547 IHTMLElement2* insert_after, BSTR instance_id, BSTR script,
548 BSTR event_name) {
549 DCHECK(insert_after);
550 DCHECK_GT(::SysStringLen(event_name), 0UL); // should always have this
552 // This might be 0 if not specified in the HTML document.
553 if (!::SysStringLen(instance_id)) {
554 // TODO(tommi): Should we give ourselves an ID if this happens?
555 NOTREACHED() << "Need to handle this";
556 return E_INVALIDARG;
559 base::win::ScopedComPtr<IHTMLDocument2> document;
560 HRESULT hr = GetContainingDocument(document.Receive());
561 if (SUCCEEDED(hr)) {
562 base::win::ScopedComPtr<IHTMLElement> element, new_element;
563 document->createElement(base::win::ScopedBstr(L"script"),
564 element.Receive());
565 if (element) {
566 base::win::ScopedComPtr<IHTMLScriptElement> script_element;
567 if (SUCCEEDED(hr = script_element.QueryFrom(element))) {
568 script_element->put_htmlFor(instance_id);
569 script_element->put_event(event_name);
570 script_element->put_text(script);
572 hr = insert_after->insertAdjacentElement(
573 base::win::ScopedBstr(L"afterEnd"),
574 element,
575 new_element.Receive());
580 return hr;
583 void ChromeFrameActivex::FireEvent(const EventHandlers& handlers,
584 const std::string& arg) {
585 if (handlers.size()) {
586 base::win::ScopedComPtr<IDispatch> event;
587 if (SUCCEEDED(CreateDomEvent("event", arg, "", event.Receive()))) {
588 FireEvent(handlers, event);
593 void ChromeFrameActivex::FireEvent(const EventHandlers& handlers,
594 IDispatch* event) {
595 DCHECK(event != NULL);
596 VARIANT arg = { VT_DISPATCH };
597 arg.pdispVal = event;
598 DISPPARAMS params = { &arg, NULL, 1, 0 };
599 for (EventHandlers::const_iterator it = handlers.begin();
600 it != handlers.end();
601 ++it) {
602 HRESULT hr = (*it)->Invoke(DISPID_VALUE, IID_NULL, LOCALE_USER_DEFAULT,
603 DISPATCH_METHOD, &params, NULL, NULL, NULL);
604 // 0x80020101 == SCRIPT_E_REPORTED.
605 // When the script we're invoking has an error, we get this error back.
606 DLOG_IF(ERROR, FAILED(hr) && hr != 0x80020101)
607 << base::StringPrintf(L"Failed to invoke script: 0x%08X", hr);
611 void ChromeFrameActivex::FireEvent(const EventHandlers& handlers,
612 IDispatch* event, BSTR target) {
613 DCHECK(event != NULL);
614 // Arguments in reverse order to event handler function declaration,
615 // because that's what DISPPARAMS requires.
616 VARIANT args[2] = { { VT_BSTR }, { VT_DISPATCH }, };
617 args[0].bstrVal = target;
618 args[1].pdispVal = event;
619 DISPPARAMS params = { args, NULL, arraysize(args), 0 };
620 for (EventHandlers::const_iterator it = handlers.begin();
621 it != handlers.end();
622 ++it) {
623 HRESULT hr = (*it)->Invoke(DISPID_VALUE, IID_NULL, LOCALE_USER_DEFAULT,
624 DISPATCH_METHOD, &params, NULL, NULL, NULL);
625 // 0x80020101 == SCRIPT_E_REPORTED.
626 // When the script we're invoking has an error, we get this error back.
627 DLOG_IF(ERROR, FAILED(hr) && hr != 0x80020101)
628 << base::StringPrintf(L"Failed to invoke script: 0x%08X", hr);
632 HRESULT ChromeFrameActivex::InstallTopLevelHook(IOleClientSite* client_site) {
633 // Get the parent window of the site, and install our hook on the topmost
634 // window of the parent.
635 base::win::ScopedComPtr<IOleWindow> ole_window;
636 HRESULT hr = ole_window.QueryFrom(client_site);
637 if (FAILED(hr))
638 return hr;
640 HWND parent_wnd;
641 hr = ole_window->GetWindow(&parent_wnd);
642 if (FAILED(hr))
643 return hr;
645 HWND top_window = ::GetAncestor(parent_wnd, GA_ROOT);
646 chrome_wndproc_hook_ = InstallLocalWindowHook(top_window);
647 if (chrome_wndproc_hook_)
648 TopLevelWindowMapping::GetInstance()->AddMapping(top_window, m_hWnd);
650 return chrome_wndproc_hook_ ? S_OK : E_FAIL;
653 HRESULT ChromeFrameActivex::registerBhoIfNeeded() {
654 if (!m_spUnkSite) {
655 NOTREACHED() << "Invalid client site";
656 return E_FAIL;
659 if (NavigationManager::GetThreadInstance() != NULL) {
660 DVLOG(1) << "BHO already loaded";
661 return S_OK;
664 base::win::ScopedComPtr<IWebBrowser2> web_browser2;
665 HRESULT hr = DoQueryService(SID_SWebBrowserApp, m_spUnkSite,
666 web_browser2.Receive());
667 if (FAILED(hr) || web_browser2.get() == NULL) {
668 DLOG(WARNING) << "Failed to get IWebBrowser2 from client site. Error:"
669 << base::StringPrintf(" 0x%08X", hr);
670 return hr;
673 wchar_t bho_class_id_as_string[MAX_PATH] = {0};
674 StringFromGUID2(CLSID_ChromeFrameBHO, bho_class_id_as_string,
675 arraysize(bho_class_id_as_string));
677 base::win::ScopedComPtr<IObjectWithSite> bho;
678 hr = bho.CreateInstance(CLSID_ChromeFrameBHO, NULL, CLSCTX_INPROC_SERVER);
679 if (FAILED(hr)) {
680 NOTREACHED() << "Failed to register ChromeFrame BHO. Error:"
681 << base::StringPrintf(" 0x%08X", hr);
682 return hr;
685 hr = UrlMkSetSessionOption(URLMON_OPTION_USERAGENT_REFRESH, NULL, 0, 0);
686 if (FAILED(hr)) {
687 DLOG(ERROR) << "Failed to refresh user agent string from registry. "
688 << "UrlMkSetSessionOption returned "
689 << base::StringPrintf("0x%08x", hr);
690 return hr;
693 hr = bho->SetSite(web_browser2);
694 if (FAILED(hr)) {
695 NOTREACHED() << "ChromeFrame BHO SetSite failed. Error:"
696 << base::StringPrintf(" 0x%08X", hr);
697 return hr;
700 web_browser2->PutProperty(base::win::ScopedBstr(bho_class_id_as_string),
701 base::win::ScopedVariant(bho));
702 return S_OK;