1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef _mozilla_dom_ClientState_h
8 #define _mozilla_dom_ClientState_h
9 
10 #include "mozilla/dom/DocumentBinding.h"
11 #include "mozilla/Maybe.h"
12 #include "mozilla/StorageAccess.h"
13 #include "mozilla/TimeStamp.h"
14 #include "mozilla/UniquePtr.h"
15 #include "nsContentUtils.h"
16 
17 namespace mozilla {
18 namespace dom {
19 
20 class IPCClientState;
21 class IPCClientWindowState;
22 class IPCClientWorkerState;
23 
24 // This class defines the mutable nsGlobalWindow state we support querying
25 // through the ClientManagerService.  It is a snapshot of the state and
26 // is not live updated.
27 class ClientWindowState final {
28   UniquePtr<IPCClientWindowState> mData;
29 
30  public:
31   ClientWindowState(mozilla::dom::VisibilityState aVisibilityState,
32                     const TimeStamp& aLastFocusTime,
33                     StorageAccess aStorageAccess, bool aFocused);
34 
35   explicit ClientWindowState(const IPCClientWindowState& aData);
36 
37   ClientWindowState(const ClientWindowState& aRight);
38   ClientWindowState(ClientWindowState&& aRight);
39 
40   ClientWindowState& operator=(const ClientWindowState& aRight);
41 
42   ClientWindowState& operator=(ClientWindowState&& aRight);
43 
44   ~ClientWindowState();
45 
46   mozilla::dom::VisibilityState VisibilityState() const;
47 
48   const TimeStamp& LastFocusTime() const;
49 
50   bool Focused() const;
51 
52   StorageAccess GetStorageAccess() const;
53 
54   const IPCClientWindowState& ToIPC() const;
55 };
56 
57 // This class defines the mutable worker state we support querying
58 // through the ClientManagerService.  It is a snapshot of the state and
59 // is not live updated.  Right now, we don't actually providate any
60 // worker specific state values, but we may in the future.  This
61 // class also services as a placeholder that the state is referring
62 // to a worker in ClientState.
63 class ClientWorkerState final {
64   UniquePtr<IPCClientWorkerState> mData;
65 
66  public:
67   explicit ClientWorkerState(StorageAccess aStorageAccess);
68 
69   explicit ClientWorkerState(const IPCClientWorkerState& aData);
70 
71   ClientWorkerState(const ClientWorkerState& aRight);
72   ClientWorkerState(ClientWorkerState&& aRight);
73 
74   ClientWorkerState& operator=(const ClientWorkerState& aRight);
75 
76   ClientWorkerState& operator=(ClientWorkerState&& aRight);
77 
78   ~ClientWorkerState();
79 
80   StorageAccess GetStorageAccess() const;
81 
82   const IPCClientWorkerState& ToIPC() const;
83 };
84 
85 // This is a union of the various types of mutable state we support
86 // querying in ClientManagerService.  Right now it can contain either
87 // window or worker states.
88 class ClientState final {
89   Maybe<Variant<ClientWindowState, ClientWorkerState>> mData;
90 
91  public:
92   ClientState();
93 
94   explicit ClientState(const ClientWindowState& aWindowState);
95   explicit ClientState(const ClientWorkerState& aWorkerState);
96   explicit ClientState(const IPCClientWindowState& aData);
97   explicit ClientState(const IPCClientWorkerState& aData);
98 
99   ClientState(const ClientState& aRight) = default;
100   ClientState(ClientState&& aRight);
101 
102   ClientState& operator=(const ClientState& aRight) = default;
103 
104   ClientState& operator=(ClientState&& aRight);
105 
106   ~ClientState();
107 
108   static ClientState FromIPC(const IPCClientState& aData);
109 
110   bool IsWindowState() const;
111 
112   const ClientWindowState& AsWindowState() const;
113 
114   bool IsWorkerState() const;
115 
116   const ClientWorkerState& AsWorkerState() const;
117 
118   StorageAccess GetStorageAccess() const;
119 
120   const IPCClientState ToIPC() const;
121 };
122 
123 }  // namespace dom
124 }  // namespace mozilla
125 
126 #endif  // _mozilla_dom_ClientState_h
127