Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/plugin/PluginClass.h

Issue 29332848: Issue #3432 - Manage COM events with a resource class
Patch Set: rebase only Created July 27, 2016, 8:55 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/plugin/PluginClass.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 23 matching lines...) Expand all
34 34
35 #define ICON_PLUGIN_DISABLED 0 35 #define ICON_PLUGIN_DISABLED 0
36 #define ICON_PLUGIN_ENABLED 1 36 #define ICON_PLUGIN_ENABLED 1
37 #define ICON_PLUGIN_DEACTIVATED 2 37 #define ICON_PLUGIN_DEACTIVATED 2
38 #define ICON_MAX 3 38 #define ICON_MAX 3
39 39
40 #define WM_LAUNCH_INFO (WM_APP + 10) 40 #define WM_LAUNCH_INFO (WM_APP + 10)
41 41
42 class CPluginMimeFilterClient; 42 class CPluginMimeFilterClient;
43 43
44 /**
45 * Resource class for COM events.
46 * If an instance exists, there's an active connection behind it.
47 *
48 * \tparam Sink
49 * subclass of ATL::IDispEventImpl.
50 * \tparam Source
51 * Type of event source. Implements IConnectionPoint
52 */
53 template<class Sink, class Source>
54 class AtlEventRegistration
55 {
56 CComPtr<Sink> consumer;
57 CComPtr<Source> producer;
58 public:
59 /**
60 * \throw std::runtime_error
61 * If connection is not established
62 *
63 * \par Precondition
64 * - consumer is not nullptr (not checked)
65 * - producer is not nullptr (not checked)
66 */
67 AtlEventRegistration(Sink* consumer, Source* producer)
68 : consumer(consumer), producer(producer)
69 {
70 auto hr = consumer->DispEventAdvise(producer);
71 if (FAILED(hr))
72 {
73 DEBUG_ERROR_LOG(hr, PLUGIN_ERROR_SET_SITE, PLUGIN_ERROR_SET_SITE_ADVICE, " AtlEventRegistrationImpl::<constructor> - Advise");
74 throw std::runtime_error("DispEventAdvise() failed");
75 }
76 }
77
78 ~AtlEventRegistration() // noexcept
79 {
80 try
81 {
82 // Smart pointer members ensure that this statement does not fail because of life span
83 auto hr = consumer->DispEventUnadvise(producer);
84 if (FAILED(hr))
85 {
86 DEBUG_ERROR_LOG(hr, PLUGIN_ERROR_SET_SITE, PLUGIN_ERROR_SET_SITE_UNADVIS E, "AtlEventRegistrationImpl::<destructor> - Unadvise");
87 }
88 }
89 catch (...)
90 {
91 // Suppress exceptions
92 }
93 }
94 };
95
96 /**
97 * Resource holder for COM events.
98 *
99 * Manages the life time of an 'AtlEventRegistration' instance.
100 * Allows events to be started and stopped at some time other than construction/ destruction.
101 *
102 * \tparam Sink
103 * subclass of ATL::IDispEventImpl.
104 * \tparam Source
105 * Type of event source. Implements IConnectionPoint
106 */
107 template<class Sink, class Source>
108 class AtlEventRegistrationHolder
109 {
110 typedef AtlEventRegistration<Sink, Source> ImplType;
111 std::unique_ptr<ImplType> impl;
112
113 public:
114 bool Start(Sink* consumer, Source* producer) // noexcept
115 {
116 try
117 {
118 impl.reset(new ImplType(consumer, producer));
119 }
120 catch (...)
121 {
122 impl.reset();
123 return false;
124 }
125 return true;
126 }
127
128 void Stop() // noexcept
129 {
130 impl.reset();
131 }
132 };
44 class ATL_NO_VTABLE CPluginClass : 133 class ATL_NO_VTABLE CPluginClass :
45 public ATL::CComObjectRootEx<ATL::CComMultiThreadModel>, 134 public ATL::CComObjectRootEx<ATL::CComMultiThreadModel>,
46 public ATL::CComCoClass<CPluginClass, &CLSID_PluginClass>, 135 public ATL::CComCoClass<CPluginClass, &CLSID_PluginClass>,
47 public ATL::IObjectWithSiteImpl<CPluginClass>, 136 public ATL::IObjectWithSiteImpl<CPluginClass>,
48 public IOleCommandTarget, 137 public IOleCommandTarget,
49 public ATL::IDispEventImpl<1, CPluginClass, &DIID_DWebBrowserEvents2, &LIBID_S HDocVw, 1, 1> 138 public ATL::IDispEventImpl<1, CPluginClass, &DIID_DWebBrowserEvents2, &LIBID_S HDocVw, 1, 1>
50 { 139 {
51 140
52 friend class CPluginTab; 141 friend class CPluginTab;
53 142
(...skipping 11 matching lines...) Expand all
65 COM_INTERFACE_ENTRY(IObjectWithSite) 154 COM_INTERFACE_ENTRY(IObjectWithSite)
66 COM_INTERFACE_ENTRY(IOleCommandTarget) 155 COM_INTERFACE_ENTRY(IOleCommandTarget)
67 END_COM_MAP() 156 END_COM_MAP()
68 157
69 BEGIN_SINK_MAP(CPluginClass) 158 BEGIN_SINK_MAP(CPluginClass)
70 SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_BEFORENAVIGATE2, OnBeforeNa vigate2) 159 SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_BEFORENAVIGATE2, OnBeforeNa vigate2)
71 SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOWNLOADCOMPLETE, OnDownloa dComplete) 160 SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOWNLOADCOMPLETE, OnDownloa dComplete)
72 SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnDocumen tComplete) 161 SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnDocumen tComplete)
73 SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_WINDOWSTATECHANGED, OnWindo wStateChanged) 162 SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_WINDOWSTATECHANGED, OnWindo wStateChanged)
74 SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_COMMANDSTATECHANGE, OnComma ndStateChange) 163 SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_COMMANDSTATECHANGE, OnComma ndStateChange)
75 SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_ONQUIT, OnOnQuit)
76 END_SINK_MAP() 164 END_SINK_MAP()
77 165
78 CPluginClass(); 166 CPluginClass();
79 ~CPluginClass(); 167 ~CPluginClass();
80 168
81 // IObjectWithSite 169 // IObjectWithSite
82 STDMETHOD(SetSite)(IUnknown *pUnkSite); 170 STDMETHOD(SetSite)(IUnknown *pUnkSite);
83 171
84 // IOleCommandTarget 172 // IOleCommandTarget
85 STDMETHOD(QueryStatus)(const GUID* pguidCmdGroup, ULONG cCmds, OLECMD prgCmds[ ], OLECMDTEXT* pCmdText); 173 STDMETHOD(QueryStatus)(const GUID* pguidCmdGroup, ULONG cCmds, OLECMD prgCmds[ ], OLECMDTEXT* pCmdText);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 VARIANT* URL /**< [in] */, 212 VARIANT* URL /**< [in] */,
125 VARIANT* Flags /**< [in] */, 213 VARIANT* Flags /**< [in] */,
126 VARIANT* TargetFrameName /**< [in] */ , 214 VARIANT* TargetFrameName /**< [in] */ ,
127 VARIANT* PostData /**< [in] */, 215 VARIANT* PostData /**< [in] */,
128 VARIANT* Headers /**< [in] */, 216 VARIANT* Headers /**< [in] */,
129 VARIANT_BOOL* Cancel /* [in, out] */) ; 217 VARIANT_BOOL* Cancel /* [in, out] */) ;
130 void STDMETHODCALLTYPE OnDownloadComplete(); 218 void STDMETHODCALLTYPE OnDownloadComplete();
131 void STDMETHODCALLTYPE OnDocumentComplete(IDispatch* frameBrowserDisp, VARIANT * /*urlOrPidl*/); 219 void STDMETHODCALLTYPE OnDocumentComplete(IDispatch* frameBrowserDisp, VARIANT * /*urlOrPidl*/);
132 void STDMETHODCALLTYPE OnWindowStateChanged(unsigned long flags, unsigned long validFlagsMask); 220 void STDMETHODCALLTYPE OnWindowStateChanged(unsigned long flags, unsigned long validFlagsMask);
133 void STDMETHODCALLTYPE OnCommandStateChange(long command, VARIANT_BOOL enable) ; 221 void STDMETHODCALLTYPE OnCommandStateChange(long command, VARIANT_BOOL enable) ;
134 void STDMETHODCALLTYPE OnOnQuit(); 222
135 void Unadvise();
136
137 void ShowStatusBar(); 223 void ShowStatusBar();
138 bool IsStatusBarEnabled(); 224 bool IsStatusBarEnabled();
139 225
140 /** 226 /**
141 * A browser interface pointer to our site object 227 * A browser interface pointer to our site object
142 * 228 *
143 * It's values are set and reset solely in SetSite(). 229 * It's values are set and reset solely in SetSite().
144 */ 230 */
145 CComPtr<IWebBrowser2> m_webBrowser2; 231 CComPtr<IWebBrowser2> m_webBrowser2;
232 /**
233 * Event manager for events coming from our site object.
234 */
235 AtlEventRegistrationHolder<CPluginClass, IWebBrowser2> browserEvents;
236 bool detachedInitializationFailed;
237
146 HWND m_hBrowserWnd; 238 HWND m_hBrowserWnd;
147 HWND m_hTabWnd; 239 HWND m_hTabWnd;
148 HWND m_hStatusBarWnd; 240 HWND m_hStatusBarWnd;
149 HWND m_hPaneWnd; 241 HWND m_hPaneWnd;
150 242
151 WNDPROC m_pWndProcStatus; 243 WNDPROC m_pWndProcStatus;
152 int m_nPaneWidth; 244 int m_nPaneWidth;
153 HANDLE m_hTheme; 245 HANDLE m_hTheme;
154 246
155 CriticalSection m_csStatusBar; 247 CriticalSection m_csStatusBar;
156 248
157 NotificationMessage notificationMessage; 249 NotificationMessage notificationMessage;
158 250
159 bool m_isAdvised;
160 bool m_isInitializedOk; 251 bool m_isInitializedOk;
161 252
162 // Atom pane class 253 // Atom pane class
163 static ATOM s_atomPaneClass; 254 static ATOM s_atomPaneClass;
164 255
165 static ATOM GetAtomPaneClass(); 256 static ATOM GetAtomPaneClass();
166 257
167 // Icons 258 // Icons
168 static HICON s_hIcons[ICON_MAX]; 259 static HICON s_hIcons[ICON_MAX];
169 static DWORD s_hIconTypes[ICON_MAX]; 260 static DWORD s_hIconTypes[ICON_MAX];
(...skipping 10 matching lines...) Expand all
180 static CComAutoCriticalSection s_criticalSectionWindow; 271 static CComAutoCriticalSection s_criticalSectionWindow;
181 272
182 // Async browser 273 // Async browser
183 static CComQIPtr<IWebBrowser2> s_asyncWebBrowser2; 274 static CComQIPtr<IWebBrowser2> s_asyncWebBrowser2;
184 static CComQIPtr<IWebBrowser2> GetAsyncBrowser(); 275 static CComQIPtr<IWebBrowser2> GetAsyncBrowser();
185 }; 276 };
186 277
187 OBJECT_ENTRY_AUTO(__uuidof(PluginClass), CPluginClass) 278 OBJECT_ENTRY_AUTO(__uuidof(PluginClass), CPluginClass)
188 279
189 #endif // _PLUGIN_CLASS_H_ 280 #endif // _PLUGIN_CLASS_H_
OLDNEW
« no previous file with comments | « no previous file | src/plugin/PluginClass.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld