dom/file/ArchiveEvent.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/file/ArchiveEvent.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,142 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "ArchiveEvent.h"
    1.11 +
    1.12 +#include "nsCExternalHandlerService.h"
    1.13 +#include "nsProxyRelease.h"
    1.14 +
    1.15 +USING_FILE_NAMESPACE
    1.16 +
    1.17 +NS_IMPL_ISUPPORTS0(ArchiveItem)
    1.18 +
    1.19 +ArchiveItem::ArchiveItem()
    1.20 +{
    1.21 +  MOZ_COUNT_CTOR(ArchiveItem);
    1.22 +}
    1.23 +
    1.24 +ArchiveItem::~ArchiveItem()
    1.25 +{
    1.26 +  MOZ_COUNT_DTOR(ArchiveItem);
    1.27 +}
    1.28 +
    1.29 +
    1.30 +nsCString
    1.31 +ArchiveItem::GetType()
    1.32 +{
    1.33 +  if (mType.IsEmpty()) {
    1.34 +    return NS_LITERAL_CSTRING("binary/octet-stream");
    1.35 +  }
    1.36 +
    1.37 +  return mType;
    1.38 +}
    1.39 +
    1.40 +void
    1.41 +ArchiveItem::SetType(const nsCString& aType)
    1.42 +{
    1.43 +  mType = aType;
    1.44 +}
    1.45 +
    1.46 +ArchiveReaderEvent::ArchiveReaderEvent(ArchiveReader* aArchiveReader)
    1.47 +: mArchiveReader(aArchiveReader)
    1.48 +{
    1.49 +  MOZ_COUNT_CTOR(ArchiveReaderEvent);
    1.50 +}
    1.51 +
    1.52 +ArchiveReaderEvent::~ArchiveReaderEvent()
    1.53 +{
    1.54 +  if (!NS_IsMainThread()) {
    1.55 +    nsIMIMEService* mimeService;
    1.56 +    mMimeService.forget(&mimeService);
    1.57 +
    1.58 +    if (mimeService) {
    1.59 +      nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
    1.60 +      NS_WARN_IF_FALSE(mainThread, "Couldn't get the main thread! Leaking!");
    1.61 +
    1.62 +      if (mainThread) {
    1.63 +        NS_ProxyRelease(mainThread, mimeService);
    1.64 +      }
    1.65 +    }
    1.66 +  }
    1.67 +
    1.68 +  MOZ_COUNT_DTOR(ArchiveReaderEvent);
    1.69 +}
    1.70 +
    1.71 +// From the filename to the mimetype:
    1.72 +nsresult
    1.73 +ArchiveReaderEvent::GetType(nsCString& aExt,
    1.74 +                            nsCString& aMimeType)
    1.75 +{
    1.76 +  MOZ_ASSERT(NS_IsMainThread());
    1.77 +
    1.78 +  nsresult rv;
    1.79 +  
    1.80 +  if (mMimeService.get() == nullptr) {
    1.81 +    mMimeService = do_GetService(NS_MIMESERVICE_CONTRACTID, &rv);
    1.82 +    NS_ENSURE_SUCCESS(rv, rv);
    1.83 +  }
    1.84 +
    1.85 +  rv = mMimeService->GetTypeFromExtension(aExt, aMimeType);
    1.86 +  NS_ENSURE_SUCCESS(rv, rv);
    1.87 +
    1.88 +  return NS_OK;
    1.89 +}
    1.90 +
    1.91 +NS_IMETHODIMP
    1.92 +ArchiveReaderEvent::Run()
    1.93 +{
    1.94 +  return Exec();
    1.95 +}
    1.96 +
    1.97 +nsresult
    1.98 +ArchiveReaderEvent::RunShare(nsresult aStatus)
    1.99 +{
   1.100 +  mStatus = aStatus;
   1.101 +
   1.102 +  nsCOMPtr<nsIRunnable> event = NS_NewRunnableMethod(this, &ArchiveReaderEvent::ShareMainThread);
   1.103 +  NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
   1.104 +
   1.105 +  return NS_OK;
   1.106 +}
   1.107 +
   1.108 +void
   1.109 +ArchiveReaderEvent::ShareMainThread()
   1.110 +{
   1.111 +  nsTArray<nsCOMPtr<nsIDOMFile> > fileList;
   1.112 +
   1.113 +  if (!NS_FAILED(mStatus)) {
   1.114 +    // This extra step must run in the main thread:
   1.115 +    for (uint32_t index = 0; index < mFileList.Length(); ++index) {
   1.116 +      nsRefPtr<ArchiveItem> item = mFileList[index];
   1.117 +
   1.118 +      nsString tmp;
   1.119 +      nsresult rv = item->GetFilename(tmp);
   1.120 +      nsCString filename = NS_ConvertUTF16toUTF8(tmp);
   1.121 +      if (NS_FAILED(rv)) {
   1.122 +        continue;
   1.123 +      }
   1.124 +
   1.125 +      int32_t offset = filename.RFindChar('.');
   1.126 +      if (offset != kNotFound) {
   1.127 +        filename.Cut(0, offset + 1);
   1.128 +
   1.129 +        // Just to be sure, if something goes wrong, the mimetype is an empty string:
   1.130 +        nsCString type;
   1.131 +        if (NS_SUCCEEDED(GetType(filename, type))) {
   1.132 +          item->SetType(type);
   1.133 +        }
   1.134 +      }
   1.135 +
   1.136 +      // This is a nsDOMFile:
   1.137 +      nsRefPtr<nsIDOMFile> file = item->File(mArchiveReader);
   1.138 +      if (file) {
   1.139 +        fileList.AppendElement(file);
   1.140 +      }
   1.141 +    }
   1.142 +  }
   1.143 +
   1.144 +  mArchiveReader->Ready(fileList, mStatus);
   1.145 +}

mercurial