void Dispatch() { if (nsCOMPtr<nsIGlobalObject> global = mCue->GetOwnerGlobal()) { global->Dispatch(TaskCategory::Other, do_AddRef(this)); } else { NS_DispatchToMainThread(do_AddRef(this)); } }
void TestBridgeMainSubParent::ActorDestroy(ActorDestroyReason why) { if (NormalShutdown != why) fail("unexpected destruction!"); // ActorDestroy() is just a callback from IPDL-generated code, // which needs the top-level actor (this) to stay alive a little // longer so other things can be cleaned up. MessageLoop::current()->PostTask( do_AddRef(new DeleteTask<TestBridgeMainSubParent>(this))); XRE_GetIOMessageLoop()->PostTask( do_AddRef(new DeleteTask<Transport>(mTransport))); }
already_AddRefed<VideoEngine> VideoEngine::Create( UniquePtr<const webrtc::Config>&& aConfig) { LOG((__PRETTY_FUNCTION__)); LOG(("Creating new VideoEngine with CaptureDeviceType %s", aConfig->Get<webrtc::CaptureDeviceInfo>().TypeName())); return do_AddRef(new VideoEngine(std::move(aConfig))); }
nsresult HTMLMenuItemElement::Clone(dom::NodeInfo* aNodeInfo, nsINode** aResult) const { *aResult = nullptr; RefPtr<HTMLMenuItemElement> it = new HTMLMenuItemElement(do_AddRef(aNodeInfo), NOT_FROM_PARSER); nsresult rv = const_cast<HTMLMenuItemElement*>(this)->CopyInnerTo(it); if (NS_SUCCEEDED(rv)) { switch (mType) { case CMD_TYPE_CHECKBOX: case CMD_TYPE_RADIO: if (mCheckedDirty) { // We no longer have our original checked state. Set our // checked state on the clone. it->mCheckedDirty = true; it->mChecked = mChecked; } break; } it.forget(aResult); } return rv; }
already_AddRefed<JSWindowActorChild> WindowGlobalChild::GetActor( const nsAString& aName, ErrorResult& aRv) { // Check if this actor has already been created, and return it if it has. if (mWindowActors.Contains(aName)) { return do_AddRef(mWindowActors.GetWeak(aName)); } // Otherwise, we want to create a new instance of this actor. Call into the // JSWindowActorService to trigger construction. RefPtr<JSWindowActorService> actorSvc = JSWindowActorService::GetSingleton(); if (!actorSvc) { return nullptr; } JS::RootedObject obj(RootingCx()); actorSvc->ConstructActor(aName, /* aChildSide */ false, mBrowsingContext, &obj, aRv); if (aRv.Failed()) { return nullptr; } // Unwrap our actor to a JSWindowActorChild object. RefPtr<JSWindowActorChild> actor; if (NS_FAILED(UNWRAP_OBJECT(JSWindowActorChild, &obj, actor))) { return nullptr; } MOZ_RELEASE_ASSERT(!actor->Manager(), "mManager was already initialized once!"); actor->Init(aName, this); mWindowActors.Put(aName, actor); return actor.forget(); }
/* static */ already_AddRefed<BrowsingContext> BrowsingContext::Get(uint64_t aId) { if (BrowsingContextMap<WeakPtr>::Ptr abc = sBrowsingContexts->lookup(aId)) { return do_AddRef(abc->value().get()); } return nullptr; }
already_AddRefed<nsPIDOMWindowInner> GetPluginCrashedEventTarget() override { MOZ_ASSERT(NS_IsMainThread()); // WeakPtr isn't thread safe. EME_LOG("MediaKeysGMPCrashHelper::GetPluginCrashedEventTarget()"); return (mMediaKeys && mMediaKeys->GetParentObject()) ? do_AddRef(mMediaKeys->GetParentObject()) : nullptr; }
void TestBridgeMainChild::ActorDestroy(ActorDestroyReason why) { if (NormalShutdown != why) fail("unexpected destruction!"); // NB: this is kosher because QuitChild() joins with the IO thread XRE_GetIOMessageLoop()->PostTask( do_AddRef(new DeleteTask<IPDLUnitTestSubprocess>(mSubprocess))); QuitChild(); }
nsresult MediaEngineTabVideoSource::Deallocate(AllocationHandle* aHandle) { MOZ_ASSERT(!aHandle); NS_DispatchToMainThread(do_AddRef(new DestroyRunnable(this))); { MonitorAutoLock mon(mMonitor); mState = kReleased; } return NS_OK; }
already_AddRefed<DataTransfer> InputEvent::GetDataTransfer( CallerType aCallerType) { InternalEditorInputEvent* editorInputEvent = mEvent->AsEditorInputEvent(); MOZ_ASSERT(editorInputEvent); // If clipboard event is disabled, user may not want to leak clipboard // information via DOM events. If so, we should return DataTransfer which // has empty string instead. The reason why we make it have empty string is, // web apps may not expect that InputEvent.dataTransfer returns empty and // non-null DataTransfer instance. if (mEvent->IsTrusted() && aCallerType != CallerType::System && !StaticPrefs::dom_event_clipboardevents_enabled() && ExposesClipboardDataOrDataTransfer(editorInputEvent->mInputType)) { if (!editorInputEvent->mDataTransfer) { return nullptr; } return do_AddRef( new DataTransfer(editorInputEvent->mDataTransfer->GetParentObject(), editorInputEvent->mMessage, EmptyString())); } return do_AddRef(editorInputEvent->mDataTransfer); }
void Animation::DoFinishNotification(SyncNotifyFlag aSyncNotifyFlag) { CycleCollectedJSRuntime* runtime = CycleCollectedJSRuntime::Get(); if (aSyncNotifyFlag == SyncNotifyFlag::Sync) { DoFinishNotificationImmediately(); } else if (!mFinishNotificationTask.IsPending()) { RefPtr<nsRunnableMethod<Animation>> runnable = NewRunnableMethod(this, &Animation::DoFinishNotificationImmediately); runtime->DispatchToMicroTask(do_AddRef(runnable)); mFinishNotificationTask = runnable.forget(); } }
void Gecko_SetMozBinding(nsStyleDisplay* aDisplay, const uint8_t* aURLString, uint32_t aURLStringLength, ThreadSafeURIHolder* aBaseURI, ThreadSafeURIHolder* aReferrer, ThreadSafePrincipalHolder* aPrincipal) { MOZ_ASSERT(aDisplay); MOZ_ASSERT(aURLString); MOZ_ASSERT(aBaseURI); MOZ_ASSERT(aReferrer); MOZ_ASSERT(aPrincipal); nsString url; nsDependentCSubstring urlString(reinterpret_cast<const char*>(aURLString), aURLStringLength); AppendUTF8toUTF16(urlString, url); RefPtr<nsStringBuffer> urlBuffer = nsCSSValue::BufferFromString(url); aDisplay->mBinding = new css::URLValue(urlBuffer, do_AddRef(aBaseURI), do_AddRef(aReferrer), do_AddRef(aPrincipal)); }
nsresult Attr::Clone(dom::NodeInfo* aNodeInfo, nsINode** aResult) const { nsAutoString value; const_cast<Attr*>(this)->GetValue(value); *aResult = new Attr(nullptr, do_AddRef(aNodeInfo), value); if (!*aResult) { return NS_ERROR_OUT_OF_MEMORY; } NS_ADDREF(*aResult); return NS_OK; }
bool MainThreadInvoker::Invoke(already_AddRefed<nsIRunnable>&& aRunnable) { nsCOMPtr<nsIRunnable> runnable(Move(aRunnable)); if (!runnable) { return false; } if (NS_IsMainThread()) { runnable->Run(); return true; } RefPtr<SyncRunnable> syncRunnable = new SyncRunnable(runnable.forget()); // The main thread could be either blocked on a condition variable waiting // for a Gecko event, or it could be blocked waiting on a Windows HANDLE in // IPC code (doing a sync message send). In the former case, we wake it by // posting a Gecko runnable to the main thread. In the latter case, we wake // it using an APC. However, the latter case doesn't happen very often now // and APCs aren't otherwise run by the main thread. To ensure the // SyncRunnable is cleaned up, we need both to run consistently. // To do this, we: // 1. Queue an APC which does the actual work. // This ref gets released in MainThreadAPC when it runs. SyncRunnable* syncRunnableRef = syncRunnable.get(); NS_ADDREF(syncRunnableRef); if (!::QueueUserAPC(&MainThreadAPC, sMainThread, reinterpret_cast<UINT_PTR>(syncRunnableRef))) { return false; } // 2. Post a Gecko runnable (which always runs). If the APC hasn't run, the // Gecko runnable runs it. Otherwise, it does nothing. if (NS_FAILED(SystemGroup::Dispatch( TaskCategory::Other, do_AddRef(syncRunnable)))) { return false; } bool result = syncRunnable->WaitUntilComplete(); mDuration = syncRunnable->GetDuration(); return result; }
NS_IMETHODIMP nsBaseCommandController::GetInterface(const nsIID& aIID, void** aResult) { NS_ENSURE_ARG_POINTER(aResult); if (NS_SUCCEEDED(QueryInterface(aIID, aResult))) { return NS_OK; } if (aIID.Equals(NS_GET_IID(nsIControllerCommandTable))) { if (mCommandTable) { *aResult = do_AddRef(static_cast<nsIControllerCommandTable*>(mCommandTable)) .take(); return NS_OK; } return NS_ERROR_NOT_INITIALIZED; } return NS_NOINTERFACE; }
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)); }
already_AddRefed<Service> Service::getSingleton() { if (gService) { return do_AddRef(gService); } // Ensure that we are using the same version of SQLite that we compiled with // or newer. Our configure check ensures we are using a new enough version // at compile time. if (SQLITE_VERSION_NUMBER > ::sqlite3_libversion_number()) { nsCOMPtr<nsIPromptService> ps(do_GetService(NS_PROMPTSERVICE_CONTRACTID)); if (ps) { nsAutoString title, message; title.AppendLiteral("SQLite Version Error"); message.AppendLiteral("The application has been updated, but the SQLite " "library wasn't updated properly and the application " "cannot run. Please try to launch the application again. " "If that should still fail, please try reinstalling " "it, or visit https://support.mozilla.org/."); (void)ps->Alert(nullptr, title.get(), message.get()); } MOZ_CRASH("SQLite Version Error"); } // The first reference to the storage service must be obtained on the // main thread. NS_ENSURE_TRUE(NS_IsMainThread(), nullptr); RefPtr<Service> service = new Service(); if (NS_SUCCEEDED(service->initialize())) { // Note: This is cleared in the Service destructor. gService = service.get(); return service.forget(); } return nullptr; }
already_AddRefed<DrawTarget> PrintTarget::GetReferenceDrawTarget(DrawEventRecorder* aRecorder) { if (!mRefDT) { const IntSize size(1, 1); cairo_surface_t* similar; switch (cairo_surface_get_type(mCairoSurface)) { #ifdef CAIRO_HAS_WIN32_SURFACE case CAIRO_SURFACE_TYPE_WIN32: similar = cairo_win32_surface_create_with_dib( CairoContentToCairoFormat(cairo_surface_get_content(mCairoSurface)), size.width, size.height); break; #endif #ifdef CAIRO_HAS_QUARTZ_SURFACE case CAIRO_SURFACE_TYPE_QUARTZ: similar = cairo_quartz_surface_create_cg_layer( mCairoSurface, cairo_surface_get_content(mCairoSurface), size.width, size.height); break; #endif default: similar = cairo_surface_create_similar( mCairoSurface, cairo_surface_get_content(mCairoSurface), size.width, size.height); break; } if (cairo_surface_status(similar)) { return nullptr; } RefPtr<DrawTarget> dt = Factory::CreateDrawTargetForCairoSurface(similar, size); // The DT addrefs the surface, so we need drop our own reference to it: cairo_surface_destroy(similar); if (!dt || !dt->IsValid()) { return nullptr; } mRefDT = dt.forget(); } if (aRecorder) { if (!mRecordingRefDT) { RefPtr<DrawTarget> dt = CreateRecordingDrawTarget(aRecorder, mRefDT); if (!dt || !dt->IsValid()) { return nullptr; } mRecordingRefDT = dt.forget(); #ifdef DEBUG mRecorder = aRecorder; #endif } #ifdef DEBUG else { MOZ_ASSERT(aRecorder == mRecorder, "Caching mRecordingRefDT assumes the aRecorder is an invariant"); } #endif return do_AddRef(mRecordingRefDT); } return do_AddRef(mRefDT); }
already_AddRefed<CharacterData> DocumentType::CloneDataNode(mozilla::dom::NodeInfo *aNodeInfo, bool aCloneText) const { already_AddRefed<mozilla::dom::NodeInfo> ni = RefPtr<mozilla::dom::NodeInfo>(aNodeInfo).forget(); return do_AddRef(new DocumentType(ni, mPublicId, mSystemId, mInternalSubset)); }
void QueueObject::Dispatch(nsIRunnable* aRunnable) { Dispatch(do_AddRef(aRunnable)); }
/* static */ already_AddRefed<WebExtensionPolicy> WebExtensionPolicy::GetByURI(dom::GlobalObject& aGlobal, nsIURI* aURI) { return do_AddRef(EPS().GetByURL(aURI)); }
/* static */ already_AddRefed<WebExtensionPolicy> WebExtensionPolicy::GetByHostname(dom::GlobalObject& aGlobal, const nsACString& aHostname) { return do_AddRef(EPS().GetByHost(aHostname)); }
/* static */ already_AddRefed<WebExtensionPolicy> WebExtensionPolicy::GetByID(dom::GlobalObject& aGlobal, const nsAString& aID) { return do_AddRef(EPS().GetByID(aID)); }
HRESULT MainThreadHandoff::OnCall(ICallFrame* aFrame) { // (1) Get info about the method call HRESULT hr; IID iid; ULONG method; hr = aFrame->GetIIDAndMethod(&iid, &method); if (FAILED(hr)) { return hr; } RefPtr<IInterceptor> interceptor; hr = mInterceptor->Resolve(IID_IInterceptor, (void**)getter_AddRefs(interceptor)); if (FAILED(hr)) { return hr; } InterceptorTargetPtr targetInterface; hr = interceptor->GetTargetForIID(iid, targetInterface); if (FAILED(hr)) { return hr; } // (2) Execute the method call syncrhonously on the main thread RefPtr<HandoffRunnable> handoffInfo(new HandoffRunnable(aFrame, targetInterface.get())); MainThreadInvoker invoker; if (!invoker.Invoke(do_AddRef(handoffInfo))) { MOZ_ASSERT(false); return E_UNEXPECTED; } hr = handoffInfo->GetResult(); MOZ_ASSERT(SUCCEEDED(hr)); if (FAILED(hr)) { return hr; } // (3) Log *before* wrapping outputs so that the log will contain pointers to // the true target interface, not the wrapped ones. InterceptorLog::Event(aFrame, targetInterface.get()); // (4) Scan the function call for outparams that contain interface pointers. // Those will need to be wrapped with MainThreadHandoff so that they too will // be exeuted on the main thread. hr = aFrame->GetReturnValue(); if (FAILED(hr)) { // If the call resulted in an error then there's not going to be anything // that needs to be wrapped. return S_OK; } // (5) Unfortunately ICallFrame::WalkFrame does not correctly handle array // outparams. Instead, we find out whether anybody has called // mscom::RegisterArrayData to supply array parameter information and use it // if available. This is a terrible hack, but it works for the short term. In // the longer term we want to be able to use COM proxy/stub metadata to // resolve array information for us. const ArrayData* arrayData = FindArrayData(iid, method); if (arrayData) { hr = FixArrayElements(aFrame, *arrayData); if (FAILED(hr)) { return hr; } } else { // (6) Scan the outputs looking for any outparam interfaces that need wrapping. // NB: WalkFrame does not correctly handle array outparams. It processes the // first element of an array but not the remaining elements (if any). hr = aFrame->WalkFrame(CALLFRAME_WALK_OUT, this); if (FAILED(hr)) { return hr; } } return S_OK; }