Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
14 KiB

  1. #ifndef __DOWNLD_H
  2. #define __DOWNLD_H
  3. #include <mshtmdid.h>
  4. #define URLDL_WNDCLASS TEXT("WCUrlDlClass")
  5. class CUrlDownload_BSC;
  6. #define ACCEPT_LANG_MAX 256
  7. // Options for BeginDownloadURL2
  8. typedef enum {
  9. BDU2_BROWSER, // always download into the browser
  10. BDU2_URLMON, // always download with urlmon only
  11. BDU2_SMART, // browser if HTML; aggressive guessing
  12. BDU2_SNIFF, // browser if HTML; no guessing (urlmon then browser)
  13. BDU2_HEADONLY, // get HEAD info only (including Last-Modified time)
  14. } BDUMethod;
  15. typedef DWORD BDUOptions;
  16. // BDUOptions
  17. #define BDU2_NONE 0
  18. #define BDU2_NEEDSTREAM 1 // keep an istream around from bdu2_urlmon download
  19. #define BDU2_DOWNLOADNOTIFY_REQUIRED 2 // require IDownloadNotify callbacks for MSHTML
  20. #define BDU2_FAIL_IF_NOT_HTML 4 // only download if url is html (can't use w/BDU2_BROWSER)
  21. // OnDownloadComplete error codes
  22. #define BDU2_ERROR_NONE 0
  23. #define BDU2_ERROR_GENERAL 1
  24. #define BDU2_ERROR_ABORT 2
  25. #define BDU2_ERROR_MAXSIZE 3
  26. #define BDU2_ERROR_TIMEOUT 4
  27. #define BDU2_ERROR_NOT_HTML 5
  28. // CUrlDowload hosts one browser and can handle one download at a time.
  29. //
  30. // CUrlDownloadSink is defined in private.h
  31. //
  32. // Use of class CUrlDownload:
  33. //
  34. // 1) Create and AddRef it
  35. // 1.5) call put_Flags() to set the bind status callback IBrowseControl::Flags
  36. // 2) Call BeginDownloadURL2 to start a download
  37. // 3) Retrieve notifications through CUrlDownloadSink
  38. // 4) Call BeginDownloadURL2 to start another download, reusing browser
  39. // 5) Call DoneDownloading() when finished
  40. // 6) Release()
  41. // DoneDownloading() MUST be called before Release() or the CUrlDownload instance may
  42. // continue to receive notifications from the browser and attempt to pass them to
  43. // the parent. It unhooks itself as soon as OnProgress(-1) is received. But be safe.
  44. // AbortDownload() may cause DownloadComplete(TRUE) notification
  45. // to be sent to the CUrlDownloadSink
  46. // See webcrawl.h and webcrawl.cpp for example
  47. class CUrlDownload : public IOleClientSite // e_notimpl
  48. , public IPropertyNotifySink // for readystate change notifications
  49. , public IOleCommandTarget // for client pull callbacks
  50. , public IDispatch // for ambient properties
  51. , public IServiceProvider // for IAuthenticate and IHlinkFrame
  52. , public IAuthenticate // for Basic and NTLM authentication
  53. , public IHlinkFrame // for catching the post of a form
  54. , public IInternetSecurityManager // for allowing the post of a form
  55. , public IHttpSecurity // for OnSecurityProblem
  56. {
  57. friend CUrlDownload_BSC;
  58. private:
  59. ~CUrlDownload();
  60. public:
  61. CUrlDownload(CUrlDownloadSink *pParent, UINT iID=0);
  62. void LeaveMeAlone() { m_pParent=NULL; }
  63. void SetFormSubmitted(BOOL fFormSubmitted) { m_fFormSubmitted = fFormSubmitted; StartTimer(); }
  64. BOOL GetFormSubmitted(void) { return m_fFormSubmitted; }
  65. // An E_ return code from this function may be ignored if desired. The
  66. // client's OnDownloadComplete will be called with fAborted==TRUE after this
  67. // function returns with an error value.
  68. HRESULT BeginDownloadURL2(LPCWSTR, BDUMethod, BDUOptions, LPTSTR, DWORD);
  69. HRESULT SetDLCTL(long lFlags); // DLCTL flags used for browser control
  70. HRESULT AbortDownload(int iErrorCode=-1); // S_OK, S_FALSE, E_FAIL
  71. HRESULT GetRealURL(LPWSTR *ppwszURL); // Gets URL accounting for any and all redirections (MemFree)
  72. HRESULT GetScript(IHTMLWindow2 **pWin); // Will cache an *additional* reference internally
  73. void ReleaseScript() { SAFERELEASE(m_pScript); } // Releases internal reference
  74. HRESULT GetDocument(IHTMLDocument2 **ppDoc);
  75. HRESULT GetStream(IStream **ppStm); // Only if BDU2_NEEDSTREAM was specified
  76. void ReleaseStream() { SAFERELEASE(m_pStm); } // Release our internal reference
  77. HRESULT GetLastModified(SYSTEMTIME *pstLastModified); // Only if BDU2_HEADONLY was used
  78. HRESULT GetResponseCode(DWORD *pdwResponseCode);
  79. void DoneDownloading(); // Call before releasing. Will destroy browser and windows.
  80. void DestroyBrowser(); // Destroy hosted browser, leave all else alone
  81. LPCWSTR GetUserAgent(); // Get our webcrawler user-agent string
  82. // URL manipulation functions
  83. static HRESULT StripAnchor(LPWSTR lpURL);
  84. static BOOL IsHtmlUrl(LPCWSTR lpURL); // TRUE (yes) or FALSE (don't know)
  85. static BOOL IsNonHtmlUrl(LPCWSTR lpURL); // TRUE (yes) or FALSE (don't know)
  86. static BOOL IsValidURL(LPCWSTR lpURL); // TRUE (get it) or FALSE (skip it)
  87. // Should only be called from CUrlDownloadMsgProc
  88. BOOL HandleMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  89. // IUnknown members
  90. STDMETHODIMP QueryInterface(REFIID riid, void **punk);
  91. STDMETHODIMP_(ULONG) AddRef(void);
  92. STDMETHODIMP_(ULONG) Release(void);
  93. // IDispatch (ambient properties)
  94. STDMETHODIMP GetTypeInfoCount(UINT *pctinfo);
  95. STDMETHODIMP GetTypeInfo(UINT itinfo, LCID lcid, ITypeInfo **pptinfo);
  96. STDMETHODIMP GetIDsOfNames(REFIID riid, OLECHAR **rgszNames, UINT cNames,
  97. LCID lcid, DISPID *rgdispid);
  98. STDMETHODIMP Invoke(DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags,
  99. DISPPARAMS *pdispparams, VARIANT *pvarResult,
  100. EXCEPINFO *pexcepinfo, UINT *puArgErr);
  101. // IOleClientSite
  102. STDMETHODIMP SaveObject(void);
  103. STDMETHODIMP GetMoniker(DWORD dwAssign, DWORD dwWhichMoniker, IMoniker **ppmk);
  104. STDMETHODIMP GetContainer(IOleContainer **ppContainer);
  105. STDMETHODIMP ShowObject(void);
  106. STDMETHODIMP OnShowWindow(BOOL fShow);
  107. STDMETHODIMP RequestNewObjectLayout(void);
  108. // IPropertyNotifySink
  109. STDMETHODIMP OnChanged(DISPID dispID);
  110. STDMETHODIMP OnRequestEdit(DISPID dispID);
  111. // IOleCommandTarget
  112. STDMETHODIMP QueryStatus(const GUID *pguidCmdGroup,
  113. ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT *pCmdText);
  114. STDMETHODIMP Exec(const GUID *pguidCmdGroup, DWORD nCmdID,
  115. DWORD nCmdexecopt, VARIANTARG *pvaIn,
  116. VARIANTARG *pvaOut);
  117. // IServiceProvider
  118. STDMETHODIMP QueryService(REFGUID guidService, REFIID riid, void **ppvObject);
  119. // IAuthenticate
  120. STDMETHODIMP Authenticate(HWND *phwnd, LPWSTR *pszUsername, LPWSTR *pszPassword);
  121. // IHlinkFrame
  122. STDMETHODIMP SetBrowseContext(IHlinkBrowseContext *pihlbc);
  123. STDMETHODIMP GetBrowseContext(IHlinkBrowseContext **ppihlbc);
  124. STDMETHODIMP Navigate(DWORD grfHLNF, LPBC pbc, IBindStatusCallback *pibsc, IHlink *pihlNavigate);
  125. STDMETHODIMP OnNavigate(DWORD grfHLNF, IMoniker *pimkTarget, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName, DWORD dwreserved);
  126. STDMETHODIMP UpdateHlink(ULONG uHLID, IMoniker *pimkTarget, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName);
  127. // IInternetSecurityManager
  128. STDMETHODIMP SetSecuritySite(IInternetSecurityMgrSite *pSite);
  129. STDMETHODIMP GetSecuritySite(IInternetSecurityMgrSite **ppSite);
  130. STDMETHODIMP MapUrlToZone(LPCWSTR pwszUrl, DWORD *pdwZone, DWORD dwFlags);
  131. STDMETHODIMP GetSecurityId(LPCWSTR pwszUrl, BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved);
  132. STDMETHODIMP ProcessUrlAction(LPCWSTR pwszUrl, DWORD dwAction, BYTE __RPC_FAR *pPolicy, DWORD cbPolicy, BYTE *pContext, DWORD cbContext, DWORD dwFlags, DWORD dwReserved);
  133. STDMETHODIMP QueryCustomPolicy(LPCWSTR pwszUrl, REFGUID guidKey, BYTE **ppPolicy, DWORD *pcbPolicy, BYTE *pContext, DWORD cbContext, DWORD dwReserved);
  134. STDMETHODIMP SetZoneMapping(DWORD dwZone, LPCWSTR lpszPattern, DWORD dwFlags);
  135. STDMETHODIMP GetZoneMappings(DWORD dwZone, IEnumString **ppenumString, DWORD dwFlags);
  136. // IHttpSecurity
  137. STDMETHODIMP GetWindow(REFGUID rguidReason, HWND *phwnd);
  138. STDMETHODIMP OnSecurityProblem(DWORD dwProblem);
  139. protected:
  140. // main object stuff
  141. ULONG m_cRef;
  142. UINT m_iID; // our ID for callbacks
  143. CUrlDownloadSink *m_pParent; // to pass on WebBrowserEvents
  144. HWND m_hwndMe;
  145. UINT m_nTimeout; // Max timeout between notifications (seconds)
  146. // GetBrowser/CleanUpBrowser (browser download data)
  147. IPersistMoniker *m_pPersistMk;
  148. IHTMLDocument2 *m_pDocument;
  149. IOleCommandTarget *m_pOleCmdTarget;
  150. BOOL m_fWaitingForReadyState;
  151. BOOL m_fFormSubmitted;
  152. IConnectionPoint *m_pCP; // connection point for DIID_DWebBrowserEvents
  153. BOOL m_fAdviseOn; // our sink is hooked up? (ConnectionCookie valid)
  154. DWORD m_dwConnectionCookie;
  155. BOOL m_fBrowserValid; // Browser pointing to 'current' URL?
  156. // UrlMon download data
  157. CUrlDownload_BSC *m_pCbsc;
  158. BOOL m_fbscValid; // pCbsc alive for 'current' URL?
  159. IStream *m_pStm;
  160. SYSTEMTIME *m_pstLastModified; // Last Modified time
  161. DWORD m_dwResponseCode;
  162. WCHAR m_achLang[ACCEPT_LANG_MAX];
  163. UINT m_iLangStatus; // 0=uninit, 1=init, 2=failed
  164. HRESULT m_hrStatus;
  165. // General download data
  166. BDUMethod m_iMethod;
  167. BDUOptions m_iOptions;
  168. UINT_PTR m_iTimerID;
  169. LPWSTR m_pwszURL; // gives us the current url after redirections
  170. BOOL m_fSetResync; // need RESYNCHRONIZE?
  171. DWORD m_dwMaxSize; // in bytes
  172. LPWSTR m_pwszUserAgent;
  173. // IBrowseControl
  174. long m_lBindFlags;
  175. // allow caching GetScript calls
  176. IHTMLWindow2 *m_pScript;
  177. // Client pull
  178. LPWSTR m_pwszClientPullURL;
  179. int m_iNumClientPull;
  180. // methods that our bindstatuscallback calls back
  181. void BSC_OnStopBinding(HRESULT hrStatus, IStream *pStm);
  182. void BSC_OnStartBinding();
  183. void BSC_OnProgress(ULONG ulProgress, ULONG ulProgressMax);
  184. void BSC_FoundLastModified(SYSTEMTIME *pstLastModified);
  185. void BSC_FoundMimeType(CLIPFORMAT cf);
  186. // other internal stuff
  187. HRESULT CreateMyWindow();
  188. HRESULT GetBrowser(); // Get browser and set us on connection point
  189. void UnAdviseMe(); // Unhook our advise sink
  190. void CleanUpBrowser();
  191. void CleanUp(); // Clean up, including releasing browser
  192. void StartTimer(); // for 60 second timeout
  193. void StopTimer();
  194. LPCWSTR GetAcceptLanguages(); // NULL if failed
  195. HRESULT BeginDownloadWithUrlMon(LPCWSTR, LPTSTR, IEnumFORMATETC *);
  196. HRESULT BeginDownloadWithBrowser(LPCWSTR);
  197. HRESULT HandleRefresh(LPWSTR pwszEquivString, LPWSTR pwszContent, BOOL fDone);
  198. HRESULT OnDownloadComplete(int iError); // cancel timeout, send OnDownloadComplete
  199. HRESULT ProgressBytes(DWORD dwBytes); // Will abort if too many
  200. };
  201. class CUrlDownload_BSC : public IBindStatusCallback,
  202. public IHttpNegotiate, // To set User-Agent
  203. public IAuthenticate // for Basic and NTLM authentication
  204. {
  205. public:
  206. // IUnknown methods
  207. STDMETHODIMP QueryInterface(REFIID riid,void ** ppv);
  208. STDMETHODIMP_(ULONG) AddRef() { return m_cRef++; }
  209. STDMETHODIMP_(ULONG) Release() { if (--m_cRef == 0) { delete this; return 0; } return m_cRef; }
  210. // IBindStatusCallback methods
  211. STDMETHODIMP OnStartBinding(DWORD dwReserved, IBinding* pbinding);
  212. STDMETHODIMP GetPriority(LONG* pnPriority);
  213. STDMETHODIMP OnLowResource(DWORD dwReserved);
  214. STDMETHODIMP OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode,
  215. LPCWSTR pwzStatusText);
  216. STDMETHODIMP OnStopBinding(HRESULT hrResult, LPCWSTR szError);
  217. STDMETHODIMP GetBindInfo(DWORD* pgrfBINDF, BINDINFO* pbindinfo);
  218. STDMETHODIMP OnDataAvailable(DWORD grfBSCF, DWORD dwSize, FORMATETC *pfmtetc,
  219. STGMEDIUM* pstgmed);
  220. STDMETHODIMP OnObjectAvailable(REFIID riid, IUnknown* punk);
  221. // IHttpNegotiate methods
  222. STDMETHODIMP BeginningTransaction(LPCWSTR szURL, LPCWSTR szHeaders,
  223. DWORD dwReserved, LPWSTR *pszAdditionalHeaders);
  224. STDMETHODIMP OnResponse(DWORD dwResponseCode, LPCWSTR szResponseHeaders,
  225. LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders);
  226. // IAuthenticate methods
  227. STDMETHODIMP Authenticate(HWND *phwnd, LPWSTR *pszUsername, LPWSTR *pszPassword);
  228. // constructors/destructors
  229. CUrlDownload_BSC(BDUMethod, BDUOptions, LPTSTR);
  230. ~CUrlDownload_BSC();
  231. // other methods
  232. HRESULT Abort();
  233. void SetParent(CUrlDownload *pUrlDownload);
  234. // data members
  235. protected:
  236. DWORD m_cRef;
  237. IBinding* m_pBinding;
  238. IStream* m_pstm;
  239. LPTSTR m_pszLocalFileDest;
  240. LPWSTR m_pwszLocalFileSrc;
  241. BDUMethod m_iMethod;
  242. BDUOptions m_iOptions;
  243. BOOL m_fSentMimeType;
  244. CUrlDownload *m_pParent;
  245. BOOL m_fTriedAuthenticate;
  246. };
  247. #endif