bool
ServiceWorkerManagerParent::RecvUnregister(const PrincipalInfo& aPrincipalInfo,
                                           const nsString& aScope)
{
  AssertIsInMainProcess();
  AssertIsOnBackgroundThread();

  // Basic validation.
  if (aScope.IsEmpty() ||
      aPrincipalInfo.type() == PrincipalInfo::TNullPrincipalInfo ||
      aPrincipalInfo.type() == PrincipalInfo::TSystemPrincipalInfo) {
    return false;
  }

  RefPtr<UnregisterServiceWorkerCallback> callback =
    new UnregisterServiceWorkerCallback(aPrincipalInfo, aScope);

  RefPtr<ContentParent> parent =
    BackgroundParent::GetContentParent(Manager());

  // If the ContentParent is null we are dealing with a same-process actor.
  if (!parent) {
    callback->Run();
    return true;
  }

  RefPtr<CheckPrincipalWithCallbackRunnable> runnable =
    new CheckPrincipalWithCallbackRunnable(parent.forget(), aPrincipalInfo,
                                           callback);
  nsresult rv = NS_DispatchToMainThread(runnable);
  MOZ_ALWAYS_TRUE(NS_SUCCEEDED(rv));

  return true;
}
already_AddRefed<dom::DOMRequest>
BrowserElementAudioChannel::SetMuted(bool aMuted, ErrorResult& aRv)
{
  MOZ_ASSERT(NS_IsMainThread());
  AssertIsInMainProcess();

  if (!mFrameWindow) {
    nsCOMPtr<nsIDOMDOMRequest> request;
    aRv = mBrowserElementAPI->SetAudioChannelMuted((uint32_t)mAudioChannel,
                                                   aMuted,
                                                   getter_AddRefs(request));
    if (NS_WARN_IF(aRv.Failed())) {
      return nullptr;
    }

    return request.forget().downcast<DOMRequest>();
  }

  RefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
  MOZ_ASSERT(service);

  service->SetAudioChannelMuted(mFrameWindow, mAudioChannel, aMuted);

  RefPtr<DOMRequest> domRequest = new DOMRequest(GetOwner());
  nsCOMPtr<nsIRunnable> runnable = new FireSuccessRunnable(GetOwner(),
                                                           mFrameWindow,
                                                           domRequest,
                                                           mAudioChannel);
  NS_DispatchToMainThread(runnable);

  return domRequest.forget();
}
bool
ServiceWorkerManagerParent::RecvRegister(
                                     const ServiceWorkerRegistrationData& aData)
{
  AssertIsInMainProcess();
  AssertIsOnBackgroundThread();

  // Basic validation.
  if (aData.scope().IsEmpty() ||
      aData.scriptSpec().IsEmpty() ||
      aData.principal().type() == PrincipalInfo::TNullPrincipalInfo ||
      aData.principal().type() == PrincipalInfo::TSystemPrincipalInfo) {
    return false;
  }

  nsRefPtr<RegisterServiceWorkerCallback> callback =
    new RegisterServiceWorkerCallback(aData, mID);

  nsRefPtr<ContentParent> parent =
    BackgroundParent::GetContentParent(Manager());

  // If the ContentParent is null we are dealing with a same-process actor.
  if (!parent) {
    callback->Run();
    return true;
  }

  nsRefPtr<CheckPrincipalWithCallbackRunnable> runnable =
    new CheckPrincipalWithCallbackRunnable(parent.forget(), aData.principal(),
                                           callback);
  nsresult rv = NS_DispatchToMainThread(runnable);
  MOZ_ALWAYS_TRUE(NS_SUCCEEDED(rv));

  return true;
}
already_AddRefed<dom::DOMRequest>
BrowserElementAudioChannel::GetVolume(ErrorResult& aRv)
{
  MOZ_ASSERT(NS_IsMainThread());
  AssertIsInMainProcess();

  if (!mFrameWindow) {
    nsCOMPtr<nsIDOMDOMRequest> request;
    aRv = mBrowserElementAPI->GetAudioChannelVolume((uint32_t)mAudioChannel,
                                                    getter_AddRefs(request));
    if (NS_WARN_IF(aRv.Failed())) {
      return nullptr;
    }

    return request.forget().downcast<DOMRequest>();
  }

  RefPtr<DOMRequest> domRequest = new DOMRequest(GetOwner());

  nsCOMPtr<nsIRunnable> runnable =
    new GetVolumeRunnable(GetOwner(), mFrameWindow, domRequest, mAudioChannel);
  NS_DispatchToMainThread(runnable);

  return domRequest.forget();
}
/* static */ MessagePortService*
MessagePortService::Get()
{
  AssertIsInMainProcess();
  AssertIsOnBackgroundThread();

  return gInstance;
}
Esempio n. 6
0
void
FileSystemSecurity::Forget(ContentParentId aId)
{
  MOZ_ASSERT(NS_IsMainThread());
  AssertIsInMainProcess();

  mPaths.Remove(aId);
}
Esempio n. 7
0
/* static */ already_AddRefed<FileSystemSecurity>
FileSystemSecurity::Get()
{
  MOZ_ASSERT(NS_IsMainThread());
  AssertIsInMainProcess();

  RefPtr<FileSystemSecurity> service = gFileSystemSecurity.get();
  return service.forget();
}
/* static */
MessagePortService* MessagePortService::GetOrCreate() {
  AssertIsInMainProcess();
  AssertIsOnBackgroundThread();

  if (!gInstance) {
    gInstance = new MessagePortService();
  }

  return gInstance;
}
Esempio n. 9
0
void
DeleteDatabase(const nsAString& aName,
               const nsAString& aManifestURL)
{
  AssertIsInMainProcess();
  MOZ_ASSERT(NS_IsMainThread());

  nsRefPtr<DataStoreDB> db = new DataStoreDB(aManifestURL, aName);
  db->Delete();
}
Esempio n. 10
0
PLDHashOperator
DeleteDataStoresEnumerator(const nsAString& aName,
                           nsAutoPtr<HashApp>& aApps,
                           void* aUserData)
{
  AssertIsInMainProcess();
  MOZ_ASSERT(NS_IsMainThread());

  aApps->Enumerate(DeleteDataStoresAppEnumerator, aUserData);
  return aApps->Count() ? PL_DHASH_NEXT : PL_DHASH_REMOVE;
}
Esempio n. 11
0
/* static */ already_AddRefed<FileSystemSecurity>
FileSystemSecurity::GetOrCreate()
{
  MOZ_ASSERT(NS_IsMainThread());
  AssertIsInMainProcess();

  if (!gFileSystemSecurity) {
    gFileSystemSecurity = new FileSystemSecurity();
    ClearOnShutdown(&gFileSystemSecurity);
  }

  RefPtr<FileSystemSecurity> service = gFileSystemSecurity.get();
  return service.forget();
}
Esempio n. 12
0
PLDHashOperator
DeleteDataStoresAppEnumerator(
                             const uint32_t& aAppId,
                             nsAutoPtr<DataStoreInfo>& aInfo,
                             void* aUserData)
{
  AssertIsInMainProcess();
  MOZ_ASSERT(NS_IsMainThread());

  auto* appId = static_cast<uint32_t*>(aUserData);
  if (*appId != aAppId) {
    return PL_DHASH_NEXT;
  }

  DeleteDatabase(aInfo->mName, aInfo->mManifestURL);
  return PL_DHASH_REMOVE;
}
Esempio n. 13
0
void
FileSystemSecurity::GrantAccessToContentProcess(ContentParentId aId,
                                                const nsAString& aDirectoryPath)
{
  MOZ_ASSERT(NS_IsMainThread());
  AssertIsInMainProcess();

  nsTArray<nsString>* paths;
  if (!mPaths.Get(aId, &paths)) {
    paths = new nsTArray<nsString>();
    mPaths.Put(aId, paths);
  } else if (paths->Contains(aDirectoryPath)) {
    return;
  }

  paths->AppendElement(aDirectoryPath);
}
Esempio n. 14
0
bool
FileSystemSecurity::ContentProcessHasAccessTo(ContentParentId aId,
                                              const nsAString& aPath)
{
  MOZ_ASSERT(NS_IsMainThread());
  AssertIsInMainProcess();

  if (FindInReadable(NS_LITERAL_STRING(".."), aPath)) {
    return false;
  }

  nsTArray<nsString>* paths;
  if (!mPaths.Get(aId, &paths)) {
    return false;
  }

  for (uint32_t i = 0, len = paths->Length(); i < len; ++i) {
    if (FileSystemUtils::IsDescendantPath(paths->ElementAt(i), aPath)) {
      return true;
    }
  }

  return false;
}
Esempio n. 15
0
nsresult
ResetPermission(uint32_t aAppId, const nsAString& aOriginURL,
                const nsAString& aManifestURL,
                const nsAString& aPermission,
                bool aReadOnly)
{
  AssertIsInMainProcess();
  MOZ_ASSERT(NS_IsMainThread());

  nsresult rv;
  nsCOMPtr<nsIIOService> ioService(do_GetService(NS_IOSERVICE_CONTRACTID, &rv));
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  nsCOMPtr<nsIURI> uri;
  rv = ioService->NewURI(NS_ConvertUTF16toUTF8(aOriginURL), nullptr, nullptr,
                         getter_AddRefs(uri));
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
  if (!ssm) {
    return NS_ERROR_FAILURE;
  }

  nsCOMPtr<nsIPrincipal> principal;
  rv = ssm->GetAppCodebasePrincipal(uri, aAppId, false,
                                    getter_AddRefs(principal));
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  nsCOMPtr<nsIPermissionManager> pm =
    do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
  if (!pm) {
    return NS_ERROR_FAILURE;
  }

  nsCString basePermission;
  basePermission.Append(NS_ConvertUTF16toUTF8(aPermission));

  // Write permission
  {
    nsCString permission;
    permission.Append(basePermission);
    permission.AppendASCII("-write");

    uint32_t perm = nsIPermissionManager::UNKNOWN_ACTION;
    rv = pm->TestExactPermissionFromPrincipal(principal, permission.get(),
                                              &perm);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;
    }

    if (aReadOnly && perm == nsIPermissionManager::ALLOW_ACTION) {
      rv = pm->RemoveFromPrincipal(principal, permission.get());
    }
    else if (!aReadOnly && perm != nsIPermissionManager::ALLOW_ACTION) {
      rv = pm->AddFromPrincipal(principal, permission.get(),
                                nsIPermissionManager::ALLOW_ACTION,
                                nsIPermissionManager::EXPIRE_NEVER, 0);
    }

    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;
    }
  }

  // Read permission
  {
    nsCString permission;
    permission.Append(basePermission);
    permission.AppendASCII("-read");

    uint32_t perm = nsIPermissionManager::UNKNOWN_ACTION;
    rv = pm->TestExactPermissionFromPrincipal(principal, permission.get(),
                                              &perm);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;
    }

    if (perm != nsIPermissionManager::ALLOW_ACTION) {
      rv = pm->AddFromPrincipal(principal, permission.get(),
                                nsIPermissionManager::ALLOW_ACTION,
                                nsIPermissionManager::EXPIRE_NEVER, 0);
      if (NS_WARN_IF(NS_FAILED(rv))) {
        return rv;
      }
    }
  }

  // Generic permission
  uint32_t perm = nsIPermissionManager::UNKNOWN_ACTION;
  rv = pm->TestExactPermissionFromPrincipal(principal, basePermission.get(),
                                            &perm);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  if (perm != nsIPermissionManager::ALLOW_ACTION) {
    rv = pm->AddFromPrincipal(principal, basePermission.get(),
                              nsIPermissionManager::ALLOW_ACTION,
                              nsIPermissionManager::EXPIRE_NEVER, 0);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return rv;
    }
  }

  return NS_OK;
}
Esempio n. 16
0
FileSystemSecurity::~FileSystemSecurity()
{
  MOZ_ASSERT(NS_IsMainThread());
  AssertIsInMainProcess();
}