Confirmed users
205
edits
Bevis Tseng (talk | contribs) (Update the guideline according to what has been done in IToplevelProtocol::GetMessageEventTarget().) |
Bevis Tseng (talk | contribs) (New explanation on replacing AbstractThread::MainThread() with AbstractMainThreadFor()) |
||
| Line 181: | Line 181: | ||
If the new actor is created on the parent side, normally, it inherits its event target from its manager. If the manager has no event target, you must override the '''GetConstructedEventTarget''' method on ContentChild (or whatever the top-level protocol is). All constructor messages are passed to this method. It can return an event target for the new actor or null if no special event target should be used. Be careful, because this method is called on the Gecko I/O thread! | If the new actor is created on the parent side, normally, it inherits its event target from its manager. If the manager has no event target, you must override the '''GetConstructedEventTarget''' method on ContentChild (or whatever the top-level protocol is). All constructor messages are passed to this method. It can return an event target for the new actor or null if no special event target should be used. Be careful, because this method is called on the Gecko I/O thread! | ||
== AbstractThread::MainThread == | |||
The AbstractThread::MainThread is a singleton of the AbstractThread wrapper for the main thread and is widely used with MozPromise and its Promise-chain. If AbstractThread::MainThread adopted in your implementation is related to a document or a window, to join the labeling, you can replace it with '''AbstractMainThreadFor''' provided from the Dispatcher interface. | |||
=== Example === | |||
already_AddRefed<Promise> | |||
WebAuthentication::MakeCredential(JSContext* aCx, const Account& aAccount, | |||
const Sequence<ScopedCredentialParameters>& aCryptoParameters, | |||
const ArrayBufferViewOrArrayBuffer& aChallenge, | |||
const ScopedCredentialOptions& aOptions) | |||
{ | |||
MOZ_ASSERT(mParent); | |||
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject()); | |||
if (!global) { | |||
return nullptr; | |||
} | |||
... // lots of code to initiate the request | |||
requestMonitor->CompleteTask(); | |||
- monitorPromise->Then(AbstractThread::MainThread(), __func__, | |||
+ monitorPromise->Then( | |||
+ global->AbstractMainThreadFor(TaskCategory::Other), __func__, | |||
[promise] (CredentialPtr aInfo) { | |||
promise->MaybeResolve(aInfo); | |||
}, | |||
[promise] (nsresult aErrorCode) { | |||
promise->MaybeReject(aErrorCode); | |||
}); | |||
return promise.forget(); | |||
} | |||