Can I suggest the following approach to wake lock interface:

1. User agent provides an interface to documents such that a document loaded 
into the browsing context can indicate whether it wishes to keep the device 
awake. Each browsing context has an independent state of this flag.
2. User agent manages global wake lock state considering the following 
circumstances:
    * Whether the wake lock is currently applicable to some browsing context. 
This means that there is at least one browsing context that is not blurred and 
has wake lock flag set.
    * Whether the wake lock is currently permitted by the state of the 
operating system (there is sufficient battery charge etc.)
User agent must observe the state of these conditions, acquiring the wake lock 
when they both become true and releasing the lock when one or both of them 
become false.
3. User agent has a global flag observable by each browsing context which 
indicates whether the global lock is currently held.
4. User agent recognizes some form of document identity (such as URL not 
including the fragment identifier) and resets context's wake lock flag when 
this identity changes.

The idea behind this is that as the global wake lock is shared between all 
browsing contexts (and thus not exclusively owned by any of them) and 
furthermore it is subject to conditions out of their control such as context 
being blurred or device having insufficient battery charge, it is undesirable 
that web applications handle acquisition/release (and failure of these 
operations) of the global wake lock themselves, rather, they should set wake 
lock preference and user agent should manage the global wake lock for them. 
Also, web applications should be able to observe the state of the global wake 
lock if they wish to take any special actions when the global wake lock is not 
actually set.

An implementation can e.g. look like this:
```
partial interface Document {
    // an application can set this flag to true to request wake lock or false 
to cancel request
    // this attribute is per-context
    attribute boolean keepAwake;
 }

partial interface Navigator {
    // an application can observe this attribute to be notified of the actual 
global lock status
    // this attribute has the same value for all browsing contexts
    [observable] readonly attribute boolean awakeLock;
}
```

Any comments are highly appreciated.

Kind regards,

Andrey Logvinov

Reply via email to