TabContext.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_TabContext_h
  6. #define mozilla_dom_TabContext_h
  7. #include "mozIApplication.h"
  8. #include "nsCOMPtr.h"
  9. #include "mozilla/BasePrincipal.h"
  10. #include "nsPIDOMWindow.h"
  11. #include "nsPIWindowRoot.h"
  12. namespace mozilla {
  13. namespace dom {
  14. class IPCTabContext;
  15. /**
  16. * TabContext encapsulates information about an iframe that may be a mozbrowser
  17. * or mozapp. You can ask whether a TabContext corresponds to a mozbrowser or
  18. * mozapp, get the app that contains the browser, and so on.
  19. *
  20. * TabParent and TabChild both inherit from TabContext, and you can also have
  21. * standalone TabContext objects.
  22. *
  23. * This class is immutable except by calling one of the protected
  24. * SetTabContext*() methods (and those methods can only be called once). See
  25. * also MutableTabContext.
  26. */
  27. class TabContext
  28. {
  29. public:
  30. TabContext();
  31. /* (The implicit copy-constructor and operator= are fine.) */
  32. /**
  33. * Generates IPCTabContext of type BrowserFrameIPCTabContext or
  34. * AppFrameIPCTabContext from this TabContext's information.
  35. */
  36. IPCTabContext AsIPCTabContext() const;
  37. /**
  38. * Does this TabContext correspond to a mozbrowser?
  39. *
  40. * <iframe mozbrowser mozapp> and <xul:browser> are not considered to be
  41. * mozbrowser elements.
  42. *
  43. * If IsMozBrowserElement() is true, HasOwnApp() and HasAppOwnerApp() are
  44. * guaranteed to be false.
  45. *
  46. * If IsMozBrowserElement() is false, HasBrowserOwnerApp() is guaranteed to be
  47. * false.
  48. */
  49. bool IsMozBrowserElement() const;
  50. /**
  51. * Does this TabContext correspond to an isolated mozbrowser?
  52. *
  53. * <iframe mozbrowser mozapp> and <xul:browser> are not considered to be
  54. * mozbrowser elements. <iframe mozbrowser noisolation> does not count as
  55. * isolated since isolation is disabled. Isolation can only be disabled by
  56. * chrome pages.
  57. */
  58. bool IsIsolatedMozBrowserElement() const;
  59. /**
  60. * Does this TabContext correspond to a mozbrowser or mozapp? This is
  61. * equivalent to IsMozBrowserElement() || HasOwnApp(). Returns false for
  62. * <xul:browser>, which is neither a mozbrowser nor a mozapp.
  63. */
  64. bool IsMozBrowserOrApp() const;
  65. /**
  66. * OwnAppId() returns the id of the app which directly corresponds to this
  67. * context's frame. GetOwnApp() returns the corresponding app object, and
  68. * HasOwnApp() returns true iff GetOwnApp() would return a non-null value.
  69. *
  70. * If HasOwnApp() is true, IsMozBrowserElement() is guaranteed to be
  71. * false.
  72. */
  73. uint32_t OwnAppId() const;
  74. already_AddRefed<mozIApplication> GetOwnApp() const;
  75. bool HasOwnApp() const;
  76. /**
  77. * BrowserOwnerAppId() gets the ID of the app which contains this browser
  78. * frame. If this is not a mozbrowser frame (if !IsMozBrowserElement()), then
  79. * BrowserOwnerAppId() is guaranteed to return NO_APP_ID.
  80. *
  81. * Even if we are a browser frame, BrowserOwnerAppId() may still return
  82. * NO_APP_ID, if this browser frame is not contained inside an app.
  83. */
  84. uint32_t BrowserOwnerAppId() const;
  85. already_AddRefed<mozIApplication> GetBrowserOwnerApp() const;
  86. bool HasBrowserOwnerApp() const;
  87. /**
  88. * AppOwnerAppId() gets the ID of the app which contains this app frame. If
  89. * this is not an app frame (i.e., if !HasOwnApp()), then AppOwnerAppId() is
  90. * guaranteed to return NO_APP_ID.
  91. *
  92. * Even if we are an app frame, AppOwnerAppId() may still return NO_APP_ID, if
  93. * this app frame is not contained inside an app.
  94. */
  95. uint32_t AppOwnerAppId() const;
  96. already_AddRefed<mozIApplication> GetAppOwnerApp() const;
  97. bool HasAppOwnerApp() const;
  98. /**
  99. * OwnOrContainingAppId() gets the ID of this frame, if HasOwnApp(). If this
  100. * frame does not have its own app, it gets the ID of the app which contains
  101. * this frame (i.e., the result of {Browser,App}OwnerAppId(), as applicable).
  102. */
  103. uint32_t OwnOrContainingAppId() const;
  104. already_AddRefed<mozIApplication> GetOwnOrContainingApp() const;
  105. bool HasOwnOrContainingApp() const;
  106. /**
  107. * OriginAttributesRef() returns the DocShellOriginAttributes of this frame to
  108. * the caller. This is used to store any attribute associated with the frame's
  109. * docshell, such as the AppId.
  110. */
  111. const DocShellOriginAttributes& OriginAttributesRef() const;
  112. UIStateChangeType ShowAccelerators() const;
  113. UIStateChangeType ShowFocusRings() const;
  114. protected:
  115. friend class MaybeInvalidTabContext;
  116. /**
  117. * These protected mutator methods let you modify a TabContext once. Further
  118. * attempts to modify a given TabContext will fail (the method will return
  119. * false).
  120. *
  121. * These mutators will also fail if the TabContext was created with anything
  122. * other than the no-args constructor.
  123. */
  124. /**
  125. * Set this TabContext to match the given TabContext.
  126. */
  127. bool SetTabContext(const TabContext& aContext);
  128. /**
  129. * Set the tab context's origin attributes to a private browsing value.
  130. */
  131. void SetPrivateBrowsingAttributes(bool aIsPrivateBrowsing);
  132. /**
  133. * Set the TabContext for this frame. This can either be:
  134. * - an app frame (with the given own app) inside the given owner app. Either
  135. * apps can be null.
  136. * - a browser frame inside the given owner app (which may be null).
  137. * - a non-browser, non-app frame. Both own app and owner app should be null.
  138. */
  139. bool SetTabContext(bool aIsMozBrowserElement,
  140. bool aIsPrerendered,
  141. mozIApplication* aOwnApp,
  142. mozIApplication* aAppFrameOwnerApp,
  143. UIStateChangeType aShowAccelerators,
  144. UIStateChangeType aShowFocusRings,
  145. const DocShellOriginAttributes& aOriginAttributes);
  146. /**
  147. * Modify this TabContext to match the given TabContext. This is a special
  148. * case triggered by nsFrameLoader::SwapWithOtherRemoteLoader which may have
  149. * caused the owner content to change.
  150. *
  151. * This special case only allows the field `mIsMozBrowserElement` to be
  152. * changed. If any other fields have changed, the update is ignored and
  153. * returns false.
  154. */
  155. bool UpdateTabContextAfterSwap(const TabContext& aContext);
  156. /**
  157. * Whether this TabContext is in prerender mode.
  158. */
  159. bool mIsPrerendered;
  160. private:
  161. /**
  162. * Has this TabContext been initialized? If so, mutator methods will fail.
  163. */
  164. bool mInitialized;
  165. /**
  166. * Whether this TabContext corresponds to a mozbrowser.
  167. *
  168. * <iframe mozbrowser mozapp> and <xul:browser> are not considered to be
  169. * mozbrowser elements.
  170. */
  171. bool mIsMozBrowserElement;
  172. /**
  173. * This TabContext's own app. If this is non-null, then this
  174. * TabContext corresponds to an app, and mIsMozBrowserElement must be false.
  175. */
  176. nsCOMPtr<mozIApplication> mOwnApp;
  177. /**
  178. * This TabContext's containing app. If mIsMozBrowserElement, this
  179. * corresponds to the app which contains the browser frame; otherwise, this
  180. * corresponds to the app which contains the app frame.
  181. */
  182. nsCOMPtr<mozIApplication> mContainingApp;
  183. /*
  184. * Cache of mContainingApp->GetLocalId().
  185. */
  186. uint32_t mContainingAppId;
  187. /**
  188. * DocShellOriginAttributes of the top level tab docShell
  189. */
  190. DocShellOriginAttributes mOriginAttributes;
  191. /**
  192. * Keyboard indicator state (focus rings, accelerators).
  193. */
  194. UIStateChangeType mShowAccelerators;
  195. UIStateChangeType mShowFocusRings;
  196. };
  197. /**
  198. * MutableTabContext is the same as MaybeInvalidTabContext, except the mutation
  199. * methods are public instead of protected. You can still only call these
  200. * mutation methods once on a given object.
  201. */
  202. class MutableTabContext : public TabContext
  203. {
  204. public:
  205. bool SetTabContext(const TabContext& aContext)
  206. {
  207. return TabContext::SetTabContext(aContext);
  208. }
  209. bool
  210. SetTabContext(bool aIsMozBrowserElement,
  211. bool aIsPrerendered,
  212. mozIApplication* aOwnApp,
  213. mozIApplication* aAppFrameOwnerApp,
  214. UIStateChangeType aShowAccelerators,
  215. UIStateChangeType aShowFocusRings,
  216. const DocShellOriginAttributes& aOriginAttributes)
  217. {
  218. return TabContext::SetTabContext(aIsMozBrowserElement,
  219. aIsPrerendered,
  220. aOwnApp,
  221. aAppFrameOwnerApp,
  222. aShowAccelerators,
  223. aShowFocusRings,
  224. aOriginAttributes);
  225. }
  226. };
  227. /**
  228. * MaybeInvalidTabContext is a simple class that lets you transform an
  229. * IPCTabContext into a TabContext.
  230. *
  231. * The issue is that an IPCTabContext is not necessarily valid; for example, it
  232. * might specify an app-id which doesn't exist. So to convert an IPCTabContext
  233. * into a TabContext, you construct a MaybeInvalidTabContext, check whether it's
  234. * valid, and, if so, read out your TabContext.
  235. *
  236. * Example usage:
  237. *
  238. * void UseTabContext(const TabContext& aTabContext);
  239. *
  240. * void CreateTab(const IPCTabContext& aContext) {
  241. * MaybeInvalidTabContext tc(aContext);
  242. * if (!tc.IsValid()) {
  243. * NS_ERROR(nsPrintfCString("Got an invalid IPCTabContext: %s",
  244. * tc.GetInvalidReason()));
  245. * return;
  246. * }
  247. * UseTabContext(tc.GetTabContext());
  248. * }
  249. */
  250. class MaybeInvalidTabContext
  251. {
  252. public:
  253. /**
  254. * This constructor copies the information in aContext and sets IsValid() as
  255. * appropriate.
  256. */
  257. explicit MaybeInvalidTabContext(const IPCTabContext& aContext);
  258. /**
  259. * Was the IPCTabContext we received in our constructor valid?
  260. */
  261. bool IsValid();
  262. /**
  263. * If IsValid(), this function returns null. Otherwise, it returns a
  264. * human-readable string indicating why the IPCTabContext passed to our
  265. * constructor was not valid.
  266. */
  267. const char* GetInvalidReason();
  268. /**
  269. * If IsValid(), this function returns a reference to a TabContext
  270. * corresponding to the IPCTabContext passed to our constructor. If
  271. * !IsValid(), this function crashes.
  272. */
  273. const TabContext& GetTabContext();
  274. private:
  275. MaybeInvalidTabContext(const MaybeInvalidTabContext&) = delete;
  276. MaybeInvalidTabContext& operator=(const MaybeInvalidTabContext&) = delete;
  277. const char* mInvalidReason;
  278. MutableTabContext mTabContext;
  279. };
  280. } // namespace dom
  281. } // namespace mozilla
  282. #endif