ContentHandlerService.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "ContentHandlerService.h"
  2. #include "HandlerServiceChild.h"
  3. #include "ContentChild.h"
  4. #include "nsIMutableArray.h"
  5. #include "nsIMIMEInfo.h"
  6. using mozilla::dom::ContentChild;
  7. using mozilla::dom::PHandlerServiceChild;
  8. using mozilla::dom::HandlerInfo;
  9. namespace mozilla {
  10. namespace dom {
  11. NS_IMPL_ISUPPORTS(ContentHandlerService, nsIHandlerService)
  12. ContentHandlerService::ContentHandlerService()
  13. {
  14. }
  15. nsresult
  16. ContentHandlerService::Init()
  17. {
  18. if (!XRE_IsContentProcess()) {
  19. return NS_ERROR_FAILURE;
  20. }
  21. ContentChild* cpc = ContentChild::GetSingleton();
  22. mHandlerServiceChild = static_cast<HandlerServiceChild*>(cpc->SendPHandlerServiceConstructor());
  23. return NS_OK;
  24. }
  25. void
  26. ContentHandlerService::nsIHandlerInfoToHandlerInfo(nsIHandlerInfo* aInfo,
  27. HandlerInfo* aHandlerInfo)
  28. {
  29. nsCString type;
  30. aInfo->GetType(type);
  31. nsCOMPtr<nsIMIMEInfo> mimeInfo = do_QueryInterface(aInfo);
  32. bool isMIMEInfo = !!mimeInfo;
  33. nsString description;
  34. aInfo->GetDescription(description);
  35. bool alwaysAskBeforeHandling;
  36. aInfo->GetAlwaysAskBeforeHandling(&alwaysAskBeforeHandling);
  37. nsCOMPtr<nsIHandlerApp> app;
  38. aInfo->GetPreferredApplicationHandler(getter_AddRefs(app));
  39. nsString name;
  40. nsString detailedDescription;
  41. if (app) {
  42. app->GetName(name);
  43. app->GetDetailedDescription(detailedDescription);
  44. }
  45. HandlerApp happ(name, detailedDescription);
  46. nsTArray<HandlerApp> happs;
  47. nsCOMPtr<nsIMutableArray> apps;
  48. aInfo->GetPossibleApplicationHandlers(getter_AddRefs(apps));
  49. if (apps) {
  50. unsigned int length;
  51. apps->GetLength(&length);
  52. for (unsigned int i = 0; i < length; i++) {
  53. apps->QueryElementAt(i, NS_GET_IID(nsIHandlerApp), getter_AddRefs(app));
  54. app->GetName(name);
  55. app->GetDetailedDescription(detailedDescription);
  56. happs.AppendElement(HandlerApp(name, detailedDescription));
  57. }
  58. }
  59. nsHandlerInfoAction action;
  60. aInfo->GetPreferredAction(&action);
  61. HandlerInfo info(type, isMIMEInfo, description, alwaysAskBeforeHandling, happ, happs, action);
  62. *aHandlerInfo = info;
  63. }
  64. NS_IMETHODIMP RemoteHandlerApp::GetName(nsAString & aName)
  65. {
  66. aName.Assign(mAppChild.name());
  67. return NS_OK;
  68. }
  69. NS_IMETHODIMP RemoteHandlerApp::SetName(const nsAString & aName)
  70. {
  71. return NS_ERROR_NOT_IMPLEMENTED;
  72. }
  73. NS_IMETHODIMP RemoteHandlerApp::GetDetailedDescription(nsAString & aDetailedDescription)
  74. {
  75. aDetailedDescription.Assign(mAppChild.detailedDescription());
  76. return NS_OK;
  77. }
  78. NS_IMETHODIMP RemoteHandlerApp::SetDetailedDescription(const nsAString & aDetailedDescription)
  79. {
  80. return NS_ERROR_NOT_IMPLEMENTED;
  81. }
  82. NS_IMETHODIMP RemoteHandlerApp::Equals(nsIHandlerApp *aHandlerApp, bool *_retval)
  83. {
  84. return NS_ERROR_NOT_IMPLEMENTED;
  85. }
  86. NS_IMETHODIMP RemoteHandlerApp::LaunchWithURI(nsIURI *aURI, nsIInterfaceRequestor *aWindowContext)
  87. {
  88. return NS_ERROR_NOT_IMPLEMENTED;
  89. }
  90. NS_IMPL_ISUPPORTS(RemoteHandlerApp, nsIHandlerApp)
  91. static inline void CopyHanderInfoTonsIHandlerInfo(HandlerInfo info, nsIHandlerInfo* aHandlerInfo)
  92. {
  93. HandlerApp preferredApplicationHandler = info.preferredApplicationHandler();
  94. nsCOMPtr<nsIHandlerApp> preferredApp(new RemoteHandlerApp(preferredApplicationHandler));
  95. aHandlerInfo->SetPreferredApplicationHandler(preferredApp);
  96. nsCOMPtr<nsIMutableArray> possibleHandlers;
  97. aHandlerInfo->GetPossibleApplicationHandlers(getter_AddRefs(possibleHandlers));
  98. possibleHandlers->AppendElement(preferredApp, false);
  99. }
  100. ContentHandlerService::~ContentHandlerService()
  101. {
  102. }
  103. NS_IMETHODIMP ContentHandlerService::Enumerate(nsISimpleEnumerator * *_retval)
  104. {
  105. return NS_ERROR_NOT_IMPLEMENTED;
  106. }
  107. NS_IMETHODIMP ContentHandlerService::FillHandlerInfo(nsIHandlerInfo *aHandlerInfo, const nsACString & aOverrideType)
  108. {
  109. HandlerInfo info;
  110. nsIHandlerInfoToHandlerInfo(aHandlerInfo, &info);
  111. mHandlerServiceChild->SendFillHandlerInfo(info, nsCString(aOverrideType), &info);
  112. CopyHanderInfoTonsIHandlerInfo(info, aHandlerInfo);
  113. return NS_OK;
  114. }
  115. NS_IMETHODIMP ContentHandlerService::Store(nsIHandlerInfo *aHandlerInfo)
  116. {
  117. return NS_ERROR_NOT_IMPLEMENTED;
  118. }
  119. NS_IMETHODIMP ContentHandlerService::Exists(nsIHandlerInfo *aHandlerInfo, bool *_retval)
  120. {
  121. HandlerInfo info;
  122. nsIHandlerInfoToHandlerInfo(aHandlerInfo, &info);
  123. mHandlerServiceChild->SendExists(info, _retval);
  124. return NS_OK;
  125. }
  126. NS_IMETHODIMP ContentHandlerService::Remove(nsIHandlerInfo *aHandlerInfo)
  127. {
  128. return NS_ERROR_NOT_IMPLEMENTED;
  129. }
  130. NS_IMETHODIMP ContentHandlerService::GetTypeFromExtension(const nsACString & aFileExtension, nsACString & _retval)
  131. {
  132. nsCString* cachedType = nullptr;
  133. if (!!mExtToTypeMap.Get(aFileExtension, &cachedType) && !!cachedType) {
  134. _retval.Assign(*cachedType);
  135. return NS_OK;
  136. }
  137. nsCString type;
  138. mHandlerServiceChild->SendGetTypeFromExtension(nsCString(aFileExtension), &type);
  139. _retval.Assign(type);
  140. mExtToTypeMap.Put(nsCString(aFileExtension), new nsCString(type));
  141. return NS_OK;
  142. }
  143. }
  144. }