nsresult
sbMainThreadQueryInterface::operator()(const  nsIID& aIID,
                                       void** aInstancePtr) const
{
  nsresult rv;

  // Get the main thread QI'd object.
  if (NS_IsMainThread()) {
    // Already on main thread.  Just QI.
    rv = mSupports->QueryInterface(aIID, aInstancePtr);
  }
  else {
    // Not on main thread.  Get a proxy.
    nsCOMPtr<nsIThread> mainThread;
    rv = NS_GetMainThread(getter_AddRefs(mainThread));
    if (NS_SUCCEEDED(rv)) {
      rv = do_GetProxyForObject(mainThread,
                                aIID,
                                mSupports,
                                NS_PROXY_SYNC | NS_PROXY_ALWAYS,
                                aInstancePtr);
    }
  }

  // Return null on failure.
  if (NS_FAILED(rv))
    *aInstancePtr = nsnull;

  // Return result.
  if (mResult)
    *mResult = rv;

  return rv;
}
nsresult 
sbFileMetadataService::ProxiedStartJob(nsIArray* aMediaItemsArray,
                                       nsIStringEnumerator* aRequiredProperties,
                                       sbMetadataJob::JobType aJobType,
                                       sbIJobProgress** _retval)
{
  TRACE(("%s[%.8x]", __FUNCTION__, this));
  nsresult rv = NS_OK;  
  
  // Make sure StartJob is called on the main thread
  if (!NS_IsMainThread()) {
    LOG(("%s[%.8x] proxying main thread StartJob()", __FUNCTION__, this));
    nsCOMPtr<nsIThread> target;
    rv = NS_GetMainThread(getter_AddRefs(target));
    NS_ENSURE_SUCCESS(rv, rv);

    nsCOMPtr<sbIFileMetadataService> proxy;
    rv = do_GetProxyForObject(target,
                              NS_GET_IID(sbIFileMetadataService),
                              static_cast<sbIFileMetadataService*>(this),
                              NS_PROXY_SYNC | NS_PROXY_ALWAYS,
                              getter_AddRefs(proxy));
    NS_ENSURE_SUCCESS(rv, rv);
    // Can't call StartJob via proxy, since it is not
    // an interface method.  
    if (aJobType == sbMetadataJob::TYPE_WRITE) {
      rv = proxy->Write(aMediaItemsArray, aRequiredProperties, _retval);
    } else {
      rv = proxy->Read(aMediaItemsArray, _retval);
    }    
  } else {
    rv = StartJob(aMediaItemsArray, aRequiredProperties, aJobType, _retval);
  }
  return rv;
}
AsyncConnectionHelper::~AsyncConnectionHelper()
{
  if (!NS_IsMainThread()) {
    IDBDatabase* database;
    mDatabase.forget(&database);

    IDBTransaction* transaction;
    mTransaction.forget(&transaction);

    nsCOMPtr<nsIThread> mainThread;
    NS_GetMainThread(getter_AddRefs(mainThread));
    NS_WARN_IF_FALSE(mainThread, "Couldn't get the main thread!");

    if (mainThread) {
      if (database) {
        NS_ProxyRelease(mainThread, static_cast<nsIIDBDatabase*>(database));
      }
      if (transaction) {
        NS_ProxyRelease(mainThread,
                        static_cast<nsIIDBTransaction*>(transaction));
      }
    }
  }

  NS_ASSERTION(!mOldProgressHandler, "Should not have anything here!");
}
NS_IMETHODIMP
sbMediaListEnumeratorWrapper::Initialize(
                                nsISimpleEnumerator * aEnumerator,
                                sbIMediaListEnumeratorWrapperListener *aListener)
{
  NS_ENSURE_ARG_POINTER(aEnumerator);
  
  mMonitor = 
    nsAutoMonitor::NewMonitor("sbMediaListEnumeratorWrapper::mMonitor");
  NS_ENSURE_TRUE(mMonitor, NS_ERROR_OUT_OF_MEMORY);

  mEnumerator = aEnumerator;

  if(aListener) {
    nsCOMPtr<nsIThread> target;
    nsresult rv = NS_GetMainThread(getter_AddRefs(target));
    NS_ENSURE_SUCCESS(rv, rv);

    rv = do_GetProxyForObject(target,
                              NS_GET_IID(sbIMediaListEnumeratorWrapperListener),
                              aListener,
                              NS_PROXY_SYNC | NS_PROXY_ALWAYS,
                              getter_AddRefs(mListener));
    NS_ENSURE_SUCCESS(rv, rv);
  }

  return NS_OK;
}
Ejemplo n.º 5
0
AudioOffloadPlayer::AudioOffloadPlayer(MediaOmxCommonDecoder* aObserver) :
  mStarted(false),
  mPlaying(false),
  mReachedEOS(false),
  mIsElementVisible(true),
  mSampleRate(0),
  mStartPosUs(0),
  mPositionTimeMediaUs(-1),
  mInputBuffer(nullptr)
{
  MOZ_ASSERT(NS_IsMainThread());

  CHECK(aObserver);
#if ANDROID_VERSION >= 21
  mSessionId = AudioSystem::newAudioUniqueId();
  AudioSystem::acquireAudioSessionId(mSessionId, -1);
#else
  mSessionId = AudioSystem::newAudioSessionId();
  AudioSystem::acquireAudioSessionId(mSessionId);
#endif
  mAudioSink = new AudioOutput(mSessionId,
      IPCThreadState::self()->getCallingUid());

  nsCOMPtr<nsIThread> thread;
  MOZ_ALWAYS_SUCCEEDS(NS_GetMainThread(getter_AddRefs(thread)));
  mPositionChanged = mOnPositionChanged.Connect(
    thread, aObserver, &MediaOmxCommonDecoder::NotifyOffloadPlayerPositionChanged);
  mPlaybackEnded = mOnPlaybackEnded.Connect(
    thread, aObserver, &MediaDecoder::PlaybackEnded);
  mPlayerTearDown = mOnPlayerTearDown.Connect(
    thread, aObserver, &MediaOmxCommonDecoder::AudioOffloadTearDown);
  mSeekingStarted = mOnSeekingStarted.Connect(
    thread, aObserver, &MediaDecoder::SeekingStarted);
}
Ejemplo n.º 6
0
NS_METHOD
NS_DispatchToMainThread(nsIRunnable *event, PRUint32 dispatchFlags)
{
  nsCOMPtr<nsIThread> thread;
  nsresult rv = NS_GetMainThread(getter_AddRefs(thread));
  NS_ENSURE_SUCCESS(rv, rv);
  return thread->Dispatch(event, dispatchFlags);
}
SpeechStreamListener::~SpeechStreamListener()
{
  nsCOMPtr<nsIThread> mainThread;
  NS_GetMainThread(getter_AddRefs(mainThread));

  SpeechRecognition* forgottenRecognition = nullptr;
  mRecognition.swap(forgottenRecognition);
  NS_ProxyRelease(mainThread,
                  static_cast<DOMEventTargetHelper*>(forgottenRecognition));
}
Ejemplo n.º 8
0
nsWyciwygChannel::~nsWyciwygChannel()
{
    if (mLoadInfo) {
        nsCOMPtr<nsIThread> mainThread;
        NS_GetMainThread(getter_AddRefs(mainThread));

        nsILoadInfo *forgetableLoadInfo;
        mLoadInfo.forget(&forgetableLoadInfo);
        NS_ProxyRelease(mainThread, forgetableLoadInfo, false);
    }
}
Ejemplo n.º 9
0
 explicit EncodingCompleteEvent(EncodeCompleteCallback* aEncodeCompleteCallback)
   : mImgSize(0)
   , mType()
   , mImgData(nullptr)
   , mEncodeCompleteCallback(aEncodeCompleteCallback)
   , mFailed(false)
 {
   if (!NS_IsMainThread() && workers::GetCurrentThreadWorkerPrivate()) {
     mCreationThread = NS_GetCurrentThread();
   } else {
     NS_GetMainThread(getter_AddRefs(mCreationThread));
   }
 }
Ejemplo n.º 10
0
  jclass
  jsjni_GetGlobalClassRef(const char *className) {
    nsCOMPtr<nsIThread> mainThread;
    mozilla::DebugOnly<nsresult> rv = NS_GetMainThread(getter_AddRefs(mainThread));
    MOZ_ASSERT(NS_SUCCEEDED(rv));

    jclass foundClass;
    nsRefPtr<nsIRunnable> runnable_ref(new GetGlobalClassRefRunnable(className,
                                                                     &foundClass));
    mainThread->Dispatch(runnable_ref, NS_DISPATCH_SYNC);
    if (!foundClass)
      return NULL;

    return foundClass;
  }
void
AbstractThread::InitStatics()
{
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(!sMainThread);
  nsCOMPtr<nsIThread> mainThread;
  NS_GetMainThread(getter_AddRefs(mainThread));
  MOZ_DIAGNOSTIC_ASSERT(mainThread);
  sMainThread = new XPCOMThreadWrapper(mainThread.get(), /* aRequireTailDispatch = */ true);
  ClearOnShutdown(&sMainThread);

  if (!sCurrentThreadTLS.init()) {
    MOZ_CRASH();
  }
  sCurrentThreadTLS.set(sMainThread);
}
Ejemplo n.º 12
0
nsRefPtr<ShutdownPromise>
MediaOmxReader::Shutdown()
{
  nsCOMPtr<nsIRunnable> cancelEvent =
    NS_NewRunnableMethod(this, &MediaOmxReader::CancelProcessCachedData);
  NS_DispatchToMainThread(cancelEvent);

  nsRefPtr<ShutdownPromise> p = MediaDecoderReader::Shutdown();

  // Wait for the superclass to finish tearing things down before releasing
  // the decoder on the main thread.
  nsCOMPtr<nsIThread> mt;
  NS_GetMainThread(getter_AddRefs(mt));
  p->Then(mt.get(), __func__, this, &MediaOmxReader::ReleaseDecoder, &MediaOmxReader::ReleaseDecoder);

  return p;
}
HelperBase::~HelperBase()
{
  if (!NS_IsMainThread()) {
    IDBRequest* request;
    mRequest.forget(&request);

    if (request) {
      nsCOMPtr<nsIThread> mainThread;
      NS_GetMainThread(getter_AddRefs(mainThread));
      NS_WARN_IF_FALSE(mainThread, "Couldn't get the main thread!");

      if (mainThread) {
        NS_ProxyRelease(mainThread, static_cast<nsIDOMEventTarget*>(request));
      }
    }
  }
}
nsresult
sbFileMetadataService::ProxiedRestartProcessors(PRUint16 aProcessorsToRestart)
{
  TRACE(("%s[%.8x]", __FUNCTION__, this));
  nsresult rv = NS_OK;

  if (!NS_IsMainThread()) {
    LOG(("%s[%.8x] proxying main thread RestartProcessors()", __FUNCTION__, this));
    nsCOMPtr<nsIThread> target;
    rv = NS_GetMainThread(getter_AddRefs(target));
    NS_ENSURE_SUCCESS(rv, rv);

    nsCOMPtr<sbIFileMetadataService> proxy;
    rv = do_GetProxyForObject(target,
                              NS_GET_IID(sbIFileMetadataService),
                              static_cast<sbIFileMetadataService*>(this),
                              NS_PROXY_SYNC | NS_PROXY_ALWAYS,
                              getter_AddRefs(proxy));
    NS_ENSURE_SUCCESS(rv, rv);
    // Can't call ProxiedRestartProcessors via proxy, since it is not
    // an interface method.
    rv = proxy->RestartProcessors(aProcessorsToRestart);
    NS_ENSURE_SUCCESS(rv, rv);
  }
  else {
    NS_ENSURE_STATE(mMainThreadProcessor);
    NS_ENSURE_STATE(mBackgroundThreadProcessor);

    if (aProcessorsToRestart & sbIFileMetadataService::MAIN_THREAD_PROCESSOR) {
      rv = mMainThreadProcessor->Start();
      NS_ENSURE_SUCCESS(rv, rv);
    }

    if (aProcessorsToRestart & sbIFileMetadataService::BACKGROUND_THREAD_PROCESSOR) {
      nsCOMPtr<nsIRunnable> event =
        NS_NEW_RUNNABLE_METHOD(sbBackgroundThreadMetadataProcessor,
                               mBackgroundThreadProcessor.get(),
                               Start);
      NS_DispatchToCurrentThread(event);
    }
  }

  return NS_OK;
}
Ejemplo n.º 15
0
  jclass
  jsjni_GetGlobalClassRef(const char *className) {
    if (NS_IsMainThread()) {
      return __jsjni_GetGlobalClassRef(className);
    }

    nsCOMPtr<nsIThread> mainThread;
    mozilla::DebugOnly<nsresult> rv = NS_GetMainThread(getter_AddRefs(mainThread));
    MOZ_ASSERT(NS_SUCCEEDED(rv));

    jclass foundClass;
    nsCOMPtr<nsIRunnable> runnable_ref(new GetGlobalClassRefRunnable(className,
                                                                     &foundClass));
    nsRefPtr<mozilla::SyncRunnable> sr = new mozilla::SyncRunnable(runnable_ref);
    sr->DispatchToThread(mainThread);
    if (!foundClass)
      return nullptr;

    return foundClass;
  }
Ejemplo n.º 16
0
void
TextTrackList::CreateAndDispatchTrackEventRunner(TextTrack* aTrack,
                                                 const nsAString& aEventName)
{
  nsCOMPtr<nsIThread> thread;
  nsresult rv = NS_GetMainThread(getter_AddRefs(thread));
  if (NS_FAILED(rv)) {
    // If we are not able to get the main-thread object we are shutting down.
    return;
  }

  TrackEventInit eventInit;
  eventInit.mTrack.SetValue().SetAsTextTrack() = aTrack;
  nsRefPtr<TrackEvent> event =
    TrackEvent::Constructor(this, aEventName, eventInit);

  // Dispatch the TrackEvent asynchronously.
  rv = thread->Dispatch(do_AddRef(new TrackEventRunner(this, event)),
                        NS_DISPATCH_NORMAL);

  // If we are shutting down this can file but it's still ok.
  NS_WARN_IF(NS_FAILED(rv));
}
Ejemplo n.º 17
0
nsresult
SimpleTimer::Init(nsIRunnable* aTask, uint32_t aTimeoutMs, nsIThread* aTarget)
{
  nsresult rv;

  // Get target thread first, so we don't have to cancel the timer if it fails.
  nsCOMPtr<nsIThread> target;
  if (aTarget) {
    target = aTarget;
  } else {
    rv = NS_GetMainThread(getter_AddRefs(target));
    if (NS_FAILED(rv)) {
      return rv;
    }
  }

  nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
  if (NS_FAILED(rv)) {
    return rv;
  }
  // Note: set target before InitWithCallback in case the timer fires before
  // we change the event target.
  rv = timer->SetTarget(aTarget);
  if (NS_FAILED(rv)) {
    timer->Cancel();
    return rv;
  }
  rv = timer->InitWithCallback(this, aTimeoutMs, nsITimer::TYPE_ONE_SHOT);
  if (NS_FAILED(rv)) {
    return rv;
  }

  mTimer = timer.forget();
  mTask = aTask;
  return NS_OK;
}
/* void run (); */
NS_IMETHODIMP sbTestMediacoreStressThreads::Run()
{
  NS_ENSURE_FALSE(mMonitor, NS_ERROR_ALREADY_INITIALIZED);

  mMonitor = nsAutoMonitor::NewMonitor(__FILE__);
  NS_ENSURE_TRUE(mMonitor, NS_ERROR_OUT_OF_MEMORY);

  nsresult rv;

  mBaseEventTarget = new sbBaseMediacoreEventTarget(this);

  rv = mBaseEventTarget->AddListener(static_cast<sbIMediacoreEventListener*>(this));
  NS_ENSURE_SUCCESS(rv, rv);

  // spin up a *ton* of threads...
  mCounter = 0;
  for (int i = 0; i < 100; ++i) {
    nsAutoMonitor mon(mMonitor);
    nsCOMPtr<nsIRunnable> event =
      NS_NEW_RUNNABLE_METHOD(sbTestMediacoreStressThreads, this, OnEvent);
    NS_ENSURE_TRUE(event, NS_ERROR_OUT_OF_MEMORY);

    nsCOMPtr<nsIThread> thread;
    ++mCounter;
    rv = NS_NewThread(getter_AddRefs(thread), event);
    NS_ENSURE_SUCCESS(rv, rv);

    mThreads.AppendObject(thread);
  }

  // and wait for them to receive their messages... spin event loop until that
  // happens. We need to wait explicitly as the OnEvent method does not directly
  // queue all the events needed to complete the test - some get queued later,
  // so calling thread->Shutdown() at this point will not allow the test to
  // complete correctly.
  nsCOMPtr<nsIThread> target;
  rv = NS_GetMainThread(getter_AddRefs(target));
  NS_ENSURE_SUCCESS(rv, rv);

  PRBool processed = PR_FALSE;
  while(mCounter > 0) {
    rv = target->ProcessNextEvent(PR_FALSE, &processed);
    NS_ENSURE_SUCCESS(rv, rv);
  }

  // Now shutdown all the threads
  while (mThreads.Count()) {
    nsCOMPtr<nsIThread> thread = mThreads[0];
    PRBool succeeded = mThreads.RemoveObjectAt(0);
    NS_ENSURE_TRUE(succeeded, NS_ERROR_FAILURE);
    rv = thread->Shutdown();
    NS_ENSURE_SUCCESS(rv, rv);
  }

  rv = mBaseEventTarget->RemoveListener(static_cast<sbIMediacoreEventListener*>(this));
  NS_ENSURE_SUCCESS(rv, rv);

  NS_ENSURE_TRUE(mCounter == 0, NS_ERROR_FAILURE);

  mBaseEventTarget = nsnull;

  return NS_OK;
}