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.

426 lines
11 KiB

  1. // mediautil.cpp: media bar utility routines
  2. #include "stock.h"
  3. #include "browseui.h"
  4. #include "mediautil.h"
  5. #define WZ_SMIE_MEDIA TEXT("Software\\Microsoft\\Internet Explorer\\Media")
  6. #define WZ_SMIE_MEDIA_MIME TEXT("Software\\Microsoft\\Internet Explorer\\Media\\MimeTypes")
  7. #define WZ_AUTOPLAY TEXT("Autoplay")
  8. #define WZ_AUTOPLAYPROMPT TEXT("AutoplayPrompt")
  9. #define MAX_REG_VALUE_LENGTH 50
  10. #define MAX_MIME_LENGTH 256
  11. static LPTSTR rgszMimeTypes[] = {
  12. TEXT("video/avi"),
  13. TEXT("video/mpeg"),
  14. TEXT("video/msvideo"),
  15. TEXT("video/x-ivf"),
  16. TEXT("video/x-mpeg"),
  17. TEXT("video/x-mpeg2a"),
  18. TEXT("video/x-ms-asf"),
  19. TEXT("video/x-msvideo"),
  20. TEXT("video/x-ms-wm"),
  21. TEXT("video/x-ms-wmv"),
  22. TEXT("video/x-ms-wvx"),
  23. TEXT("video/x-ms-wmx"),
  24. TEXT("video/x-ms-wmp"),
  25. TEXT("audio/mp3"),
  26. TEXT("audio/aiff"),
  27. TEXT("audio/basic"),
  28. TEXT("audio/mid"),
  29. TEXT("audio/midi"),
  30. TEXT("audio/mpeg"),
  31. TEXT("audio/mpegurl"),
  32. TEXT("audio/wav"),
  33. TEXT("audio/x-aiff"),
  34. TEXT("audio/x-mid"),
  35. TEXT("audio/x-midi"),
  36. TEXT("audio/x-mpegurl"),
  37. TEXT("audio/x-ms-wax"),
  38. TEXT("audio/x-ms-wma"),
  39. TEXT("audio/x-background"),
  40. TEXT("audio/x-wav"),
  41. TEXT("midi/mid"),
  42. TEXT("application/x-ms-wmd")
  43. };
  44. //+----------------------------------------------------------------------------------------
  45. // CMediaBarUtil Methods
  46. //-----------------------------------------------------------------------------------------
  47. HUSKEY
  48. CMediaBarUtil::GetMediaRegKey()
  49. {
  50. return OpenRegKey(WZ_SMIE_MEDIA);
  51. }
  52. HUSKEY
  53. CMediaBarUtil::OpenRegKey(TCHAR * pchName)
  54. {
  55. HUSKEY hUSKey = NULL;
  56. if (pchName)
  57. {
  58. LONG lRet = SHRegCreateUSKey(
  59. pchName,
  60. KEY_ALL_ACCESS,
  61. NULL,
  62. &hUSKey,
  63. SHREGSET_HKCU);
  64. if ((ERROR_SUCCESS != lRet) || (NULL == hUSKey))
  65. {
  66. hUSKey = NULL;
  67. ASSERT(FALSE && L"couldn't open Key for registry settings");
  68. }
  69. }
  70. return hUSKey;
  71. }
  72. HRESULT
  73. CMediaBarUtil::CloseRegKey(HUSKEY hUSKey)
  74. {
  75. HRESULT hr = S_OK;
  76. if (hUSKey)
  77. {
  78. DWORD dwRet = SHRegCloseUSKey(hUSKey);
  79. if (ERROR_SUCCESS != dwRet)
  80. {
  81. ASSERT(FALSE && L"couldn't close Reg Key");
  82. hr = E_FAIL;
  83. }
  84. }
  85. return hr;
  86. }
  87. //+-----------------------------------------------------------------------
  88. //
  89. // Member: IsRegValueTrue
  90. //
  91. // Overview: Check if given value is true
  92. //
  93. // Arguments: [hUSKey] Key to read from
  94. // [pchName] name of the value to read out
  95. // [pfValue] out param (true/false Reg value)
  96. //
  97. // Returns: S_FALSE if Value does not exist
  98. // S_OK otherwise
  99. //
  100. //------------------------------------------------------------------------
  101. HRESULT CMediaBarUtil::IsRegValueTrue(HUSKEY hUSKey, TCHAR * pchName, BOOL *pfValue)
  102. {
  103. DWORD dwSize = MAX_REG_VALUE_LENGTH;
  104. DWORD dwType;
  105. BYTE bDataBuf[MAX_REG_VALUE_LENGTH];
  106. LONG lRet;
  107. BOOL bRet = FALSE;
  108. HRESULT hr = E_FAIL;
  109. if (!hUSKey || !pfValue || !pchName)
  110. {
  111. ASSERT(FALSE);
  112. hr = E_FAIL;
  113. goto done;
  114. }
  115. lRet = SHRegQueryUSValue(hUSKey,
  116. pchName,
  117. &dwType,
  118. bDataBuf,
  119. &dwSize,
  120. FALSE,
  121. NULL,
  122. 0);
  123. if (ERROR_SUCCESS != lRet)
  124. {
  125. hr = S_FALSE;
  126. goto done;
  127. }
  128. if (REG_DWORD == dwType)
  129. {
  130. bRet = (*(DWORD*)bDataBuf != 0);
  131. }
  132. else if (REG_SZ == dwType)
  133. {
  134. TCHAR ch = (TCHAR)(*bDataBuf);
  135. if (TEXT('1') == ch ||
  136. TEXT('y') == ch ||
  137. TEXT('Y') == ch)
  138. {
  139. bRet = TRUE;
  140. }
  141. else
  142. {
  143. bRet = FALSE;
  144. }
  145. }
  146. else if (REG_BINARY == dwType)
  147. {
  148. bRet = (*(BYTE*)bDataBuf != 0);
  149. }
  150. hr = S_OK;
  151. done:
  152. if (pfValue)
  153. *pfValue = bRet;
  154. return hr;
  155. }
  156. // Value is implicity TRUE, unless it exists and is set to FALSE
  157. BOOL
  158. CMediaBarUtil::GetImplicitMediaRegValue(TCHAR * pchName)
  159. {
  160. BOOL fRet = FALSE;
  161. if (pchName)
  162. {
  163. HUSKEY hMediaKey = GetMediaRegKey();
  164. if (hMediaKey)
  165. {
  166. BOOL fVal = FALSE;
  167. HRESULT hr = E_FAIL;
  168. hr = IsRegValueTrue(hMediaKey, pchName, &fVal);
  169. if ((S_OK == hr) && (FALSE == fVal))
  170. {
  171. fRet = FALSE;
  172. }
  173. else
  174. {
  175. // true if key is not present or explicitly set to true
  176. fRet = TRUE;
  177. }
  178. CloseRegKey(hMediaKey);
  179. }
  180. }
  181. return fRet;
  182. }
  183. BOOL
  184. CMediaBarUtil::GetAutoplay()
  185. {
  186. return GetImplicitMediaRegValue(WZ_AUTOPLAY);
  187. }
  188. HRESULT CMediaBarUtil::SetMediaRegValue(LPWSTR pstrName, DWORD dwRegDataType, void * pvData, DWORD cbData, BOOL fMime /* = FALSE */)
  189. {
  190. HRESULT hr = E_FAIL;
  191. if (pstrName && pvData && (cbData > 0))
  192. {
  193. HUSKEY hMediaKey = (fMime == TRUE) ? GetMimeRegKey() : GetMediaRegKey();
  194. if (hMediaKey)
  195. {
  196. LONG lRet = SHRegWriteUSValue(hMediaKey,
  197. pstrName,
  198. dwRegDataType,
  199. pvData,
  200. cbData,
  201. SHREGSET_FORCE_HKCU);
  202. if (ERROR_SUCCESS == lRet)
  203. {
  204. hr = S_OK;
  205. }
  206. else
  207. {
  208. ASSERT(FALSE && L"couldn't write reg value");
  209. }
  210. CloseRegKey(hMediaKey);
  211. }
  212. }
  213. return hr;
  214. }
  215. HUSKEY
  216. CMediaBarUtil::GetMimeRegKey()
  217. {
  218. return OpenRegKey(WZ_SMIE_MEDIA_MIME);
  219. }
  220. BOOL
  221. CMediaBarUtil::GetAutoplayPrompt()
  222. {
  223. return GetImplicitMediaRegValue(WZ_AUTOPLAYPROMPT);
  224. }
  225. HRESULT
  226. CMediaBarUtil::ToggleAutoplayPrompting(BOOL fOn)
  227. {
  228. HRESULT hr = E_FAIL;
  229. DWORD dwData = 0;
  230. dwData = (TRUE == fOn ? 0x1 : 0x0);
  231. hr = SetMediaRegValue(WZ_AUTOPLAYPROMPT, REG_BINARY, (void*) &dwData, (DWORD) 1);
  232. return hr;
  233. }
  234. HRESULT
  235. CMediaBarUtil::ToggleAutoplay(BOOL fOn)
  236. {
  237. HRESULT hr = E_FAIL;
  238. DWORD dwData = 0;
  239. dwData = (TRUE == fOn ? 0x1 : 0x0);
  240. hr = SetMediaRegValue(WZ_AUTOPLAY, REG_BINARY, (void*) &dwData, (DWORD) 1);
  241. return hr;
  242. }
  243. BOOL
  244. CMediaBarUtil::IsRecognizedMime(TCHAR * szMime)
  245. {
  246. BOOL fRet = FALSE;
  247. if (!szMime || !(*szMime))
  248. goto done;
  249. for (int i = 0; i < ARRAYSIZE(rgszMimeTypes); i++)
  250. {
  251. if (0 == StrCmpI(rgszMimeTypes[i], szMime))
  252. {
  253. fRet = TRUE;
  254. goto done;
  255. }
  256. }
  257. done:
  258. return fRet;
  259. }
  260. // this function checks if the media bar should play this mime type
  261. HRESULT
  262. CMediaBarUtil::ShouldPlay(TCHAR * szMime, BOOL * pfShouldPlay)
  263. {
  264. BOOL fRet = FALSE;
  265. HRESULT hr = E_FAIL;
  266. HUSKEY hKeyMime = GetMimeRegKey();
  267. if (!hKeyMime)
  268. goto done;
  269. // Bail if Autoplay is disabled
  270. if (FALSE == GetAutoplay())
  271. {
  272. goto done;
  273. }
  274. // bail if this is not a recognized mime type
  275. if (FALSE == IsRecognizedMime(szMime))
  276. goto done;
  277. // check if the user wants us to play everything
  278. if (FALSE == GetAutoplayPrompt())
  279. {
  280. fRet = TRUE;
  281. hr = S_OK;
  282. goto done;
  283. }
  284. // see if user wants us to play this mime type
  285. hr = IsRegValueTrue(hKeyMime, szMime, &fRet);
  286. if (FAILED(hr))
  287. goto done;
  288. if (S_FALSE == hr)
  289. {
  290. // S_FALSE means we have not asked the user about this mime type.
  291. // Which means the media bar should get a crack at this file
  292. // and ask the user if it should play this file.
  293. fRet = TRUE;
  294. }
  295. done:
  296. *pfShouldPlay = fRet;
  297. if (hKeyMime)
  298. CloseRegKey(hKeyMime);
  299. return hr;
  300. }
  301. BOOL
  302. CMediaBarUtil::IsWMP7OrGreaterInstalled()
  303. {
  304. TCHAR szPath[50];
  305. szPath[0] = 0;
  306. DWORD dwType, cb = sizeof(szPath), dwInstalled=0, cb2=sizeof(dwInstalled);
  307. return ((ERROR_SUCCESS==SHGetValue(HKEY_LOCAL_MACHINE, REG_WMP8_STR, TEXT("version"), &dwType, szPath, &cb))
  308. && ((DWORD)(*szPath-TEXT('0'))>=7)
  309. && (ERROR_SUCCESS==SHGetValue(HKEY_LOCAL_MACHINE, REG_WMP8_STR, TEXT("IsInstalled"), &dwType, &dwInstalled, &cb2))
  310. && (dwInstalled==1));
  311. }
  312. typedef UINT (WINAPI *GetSystemWow64DirectoryPtr) (PSTR pszBuffer, UINT uSize);
  313. typedef BOOL (WINAPI *IsNTAdmin) (DWORD, DWORD*);
  314. BOOL
  315. CMediaBarUtil::IsWMP7OrGreaterCapable()
  316. {
  317. static BOOL fInitialized = FALSE;
  318. static BOOL fCapable = TRUE;
  319. if (!fInitialized)
  320. {
  321. // WMP isn't supported on NT4, IA64, or DataCenter.
  322. // If WMP isn't already installed, and we're not running with admin privileges, we might as well fail
  323. // since we need WMP to function.
  324. fCapable = IsOS(OS_WIN2000ORGREATER);
  325. if (!fCapable)
  326. {
  327. fCapable = IsOS(OS_WIN98ORGREATER);
  328. }
  329. else
  330. {
  331. CHAR szPath[MAX_PATH];
  332. HMODULE hModule = GetModuleHandle(TEXT("kernel32.dll"));
  333. if (hModule)
  334. {
  335. GetSystemWow64DirectoryPtr func = (GetSystemWow64DirectoryPtr)GetProcAddress(hModule, "GetSystemWow64DirectoryA");
  336. fCapable = !(func && func(szPath, ARRAYSIZE(szPath)));
  337. }
  338. if (fCapable && !IsWMP7OrGreaterInstalled())
  339. {
  340. HMODULE hModule = LoadLibrary(TEXT("advpack.dll"));
  341. if (hModule)
  342. {
  343. IsNTAdmin func = (IsNTAdmin)GetProcAddress(hModule, "IsNTAdmin");
  344. fCapable = func && func(0, NULL);
  345. FreeLibrary(hModule);
  346. }
  347. }
  348. }
  349. if (IsOS(OS_DATACENTER))
  350. {
  351. fCapable = FALSE;
  352. }
  353. fInitialized = TRUE;
  354. }
  355. return fCapable;
  356. }