[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome_frame/com_message_event.h" |
| 6 | |
| 7 | #include "base/logging.h" |
| 8 | #include "base/string_util.h" |
[email protected] | d3451d83 | 2010-10-01 11:17:37 | [diff] [blame] | 9 | #include "base/stringprintf.h" |
[email protected] | d55194ca | 2010-03-11 18:25:45 | [diff] [blame] | 10 | #include "base/utf_string_conversions.h" |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 11 | |
| 12 | ComMessageEvent::ComMessageEvent() { |
| 13 | } |
| 14 | |
| 15 | ComMessageEvent::~ComMessageEvent() { |
| 16 | } |
| 17 | |
| 18 | bool ComMessageEvent::Initialize(IOleContainer* container, |
| 19 | const std::string& message, |
| 20 | const std::string& origin, |
| 21 | const std::string& event_type) { |
[email protected] | 233ee5d5b | 2010-05-20 19:19:20 | [diff] [blame] | 22 | DLOG_IF(WARNING, !container) << __FUNCTION__ << " no container"; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 23 | message_ = message; |
| 24 | origin_ = origin; |
| 25 | type_ = event_type; |
| 26 | |
| 27 | // May remain NULL if container not IE |
[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 28 | base::win::ScopedComPtr<IHTMLEventObj> basic_event; |
| 29 | base::win::ScopedComPtr<IHTMLDocument2> doc; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 30 | |
[email protected] | 233ee5d5b | 2010-05-20 19:19:20 | [diff] [blame] | 31 | // Fetching doc may fail in non-IE containers |
| 32 | // and container might be NULL in some applications. |
| 33 | if (container) |
| 34 | container->QueryInterface(doc.Receive()); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 35 | if (doc) { |
[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 36 | base::win::ScopedComPtr<IHTMLDocument4> doc4; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 37 | doc4.QueryFrom(doc); |
| 38 | DCHECK(doc4); // supported by IE5.5 and higher |
| 39 | if (doc4) { |
| 40 | // IHTMLEventObj5 is only supported in IE8 and later, so we provide our |
| 41 | // own (minimalistic) implementation of it. |
| 42 | doc4->createEventObject(NULL, basic_event.Receive()); |
| 43 | DCHECK(basic_event); |
| 44 | } |
| 45 | } |
| 46 | |
[email protected] | 2a26854 | 2009-09-25 19:15:01 | [diff] [blame] | 47 | basic_event_ = basic_event; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 48 | return true; |
| 49 | } |
| 50 | |
| 51 | STDMETHODIMP ComMessageEvent::GetTypeInfoCount(UINT* info) { |
| 52 | // Don't DCHECK as python scripts might still call this function |
| 53 | // inadvertently. |
| 54 | DLOG(WARNING) << "Not implemented: " << __FUNCTION__; |
[email protected] | 2a26854 | 2009-09-25 19:15:01 | [diff] [blame] | 55 | return E_NOTIMPL; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | STDMETHODIMP ComMessageEvent::GetTypeInfo(UINT which_info, LCID lcid, |
| 59 | ITypeInfo** type_info) { |
[email protected] | 2a26854 | 2009-09-25 19:15:01 | [diff] [blame] | 60 | DLOG(WARNING) << "Not implemented: " << __FUNCTION__; |
| 61 | return E_NOTIMPL; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | STDMETHODIMP ComMessageEvent::GetIDsOfNames(REFIID iid, LPOLESTR* names, |
| 65 | UINT count_names, LCID lcid, |
| 66 | DISPID* dispids) { |
| 67 | HRESULT hr = S_OK; |
| 68 | |
| 69 | // Note that since we're using LowerCaseEqualsASCII for string comparison, |
| 70 | // the second argument _must_ be all lower case. I.e. we cannot compare |
| 71 | // against L"messagePort" since it has a capital 'P'. |
| 72 | for (UINT i = 0; SUCCEEDED(hr) && i < count_names; ++i) { |
| 73 | const wchar_t* name_begin = names[i]; |
| 74 | const wchar_t* name_end = name_begin + wcslen(name_begin); |
| 75 | if (LowerCaseEqualsASCII(name_begin, name_end, "data")) { |
| 76 | dispids[i] = DISPID_MESSAGE_EVENT_DATA; |
| 77 | } else if (LowerCaseEqualsASCII(name_begin, name_end, "origin")) { |
| 78 | dispids[i] = DISPID_MESSAGE_EVENT_ORIGIN; |
| 79 | } else if (LowerCaseEqualsASCII(name_begin, name_end, "lasteventid")) { |
| 80 | dispids[i] = DISPID_MESSAGE_EVENT_LAST_EVENT_ID; |
| 81 | } else if (LowerCaseEqualsASCII(name_begin, name_end, "source")) { |
| 82 | dispids[i] = DISPID_MESSAGE_EVENT_SOURCE; |
| 83 | } else if (LowerCaseEqualsASCII(name_begin, name_end, "messageport")) { |
| 84 | dispids[i] = DISPID_MESSAGE_EVENT_MESSAGE_PORT; |
| 85 | } else if (LowerCaseEqualsASCII(name_begin, name_end, "type")) { |
| 86 | dispids[i] = DISPID_MESSAGE_EVENT_TYPE; |
| 87 | } else { |
| 88 | if (basic_event_) { |
| 89 | hr = basic_event_->GetIDsOfNames(IID_IDispatch, &names[i], 1, lcid, |
| 90 | &dispids[i]); |
| 91 | } else { |
| 92 | hr = DISP_E_MEMBERNOTFOUND; |
| 93 | } |
| 94 | |
| 95 | if (FAILED(hr)) { |
| 96 | DLOG(WARNING) << "member not found: " << names[i] |
[email protected] | d3451d83 | 2010-10-01 11:17:37 | [diff] [blame] | 97 | << base::StringPrintf(L"0x%08X", hr); |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |
[email protected] | 2a26854 | 2009-09-25 19:15:01 | [diff] [blame] | 101 | return hr; |
[email protected] | f781782 | 2009-09-24 05:11:58 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | STDMETHODIMP ComMessageEvent::Invoke(DISPID dispid, REFIID iid, LCID lcid, |
| 105 | WORD flags, DISPPARAMS* params, |
| 106 | VARIANT* result, EXCEPINFO* excepinfo, |
| 107 | UINT* arg_err) { |
| 108 | HRESULT hr = DISP_E_MEMBERNOTFOUND; |
| 109 | switch (dispid) { |
| 110 | case DISPID_MESSAGE_EVENT_DATA: |
| 111 | hr = GetStringProperty(flags, UTF8ToWide(message_).c_str(), result); |
| 112 | break; |
| 113 | |
| 114 | case DISPID_MESSAGE_EVENT_ORIGIN: |
| 115 | hr = GetStringProperty(flags, UTF8ToWide(origin_).c_str(), result); |
| 116 | break; |
| 117 | |
| 118 | case DISPID_MESSAGE_EVENT_TYPE: |
| 119 | hr = GetStringProperty(flags, UTF8ToWide(type_).c_str(), result); |
| 120 | break; |
| 121 | |
| 122 | case DISPID_MESSAGE_EVENT_LAST_EVENT_ID: |
| 123 | hr = GetStringProperty(flags, L"", result); |
| 124 | break; |
| 125 | |
| 126 | case DISPID_MESSAGE_EVENT_SOURCE: |
| 127 | case DISPID_MESSAGE_EVENT_MESSAGE_PORT: |
| 128 | if (flags & DISPATCH_PROPERTYGET) { |
| 129 | result->vt = VT_NULL; |
| 130 | hr = S_OK; |
| 131 | } else { |
| 132 | hr = DISP_E_TYPEMISMATCH; |
| 133 | } |
| 134 | break; |
| 135 | |
| 136 | default: |
| 137 | if (basic_event_) { |
| 138 | hr = basic_event_->Invoke(dispid, iid, lcid, flags, params, result, |
| 139 | excepinfo, arg_err); |
| 140 | } |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | return hr; |
| 145 | } |
| 146 | |
| 147 | HRESULT ComMessageEvent::GetStringProperty(WORD flags, const wchar_t* value, |
| 148 | VARIANT* result) { |
| 149 | if (!result) |
| 150 | return E_INVALIDARG; |
| 151 | |
| 152 | HRESULT hr; |
| 153 | if (flags & DISPATCH_PROPERTYGET) { |
| 154 | result->vt = VT_BSTR; |
| 155 | result->bstrVal = ::SysAllocString(value); |
| 156 | hr = S_OK; |
| 157 | } else { |
| 158 | hr = DISP_E_TYPEMISMATCH; |
| 159 | } |
| 160 | return hr; |
| 161 | } |