LCOV - code coverage report
Current view: top level - dom/ipc - TabContext.h (source / functions) Hit Total Coverage
Test: output.info Lines: 6 8 75.0 %
Date: 2017-07-14 16:53:18 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

          Line data    Source code
       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_TabContext_h
       8             : #define mozilla_dom_TabContext_h
       9             : 
      10             : #include "nsCOMPtr.h"
      11             : #include "mozilla/BasePrincipal.h"
      12             : #include "nsPIDOMWindow.h"
      13             : #include "nsPIWindowRoot.h"
      14             : 
      15             : namespace mozilla {
      16             : namespace dom {
      17             : 
      18             : class IPCTabContext;
      19             : 
      20             : /**
      21             :  * TabContext encapsulates information about an iframe that may be a mozbrowser.
      22             :  *
      23             :  * TabParent and TabChild both inherit from TabContext, and you can also have
      24             :  * standalone TabContext objects.
      25             :  *
      26             :  * This class is immutable except by calling one of the protected
      27             :  * SetTabContext*() methods (and those methods can only be called once).  See
      28             :  * also MutableTabContext.
      29             :  */
      30           8 : class TabContext
      31             : {
      32             : public:
      33             :   TabContext();
      34             : 
      35             :   /* (The implicit copy-constructor and operator= are fine.) */
      36             : 
      37             :   /**
      38             :    * Generates IPCTabContext of type BrowserFrameIPCTabContext from this
      39             :    * TabContext's information.
      40             :    */
      41             :   IPCTabContext AsIPCTabContext() const;
      42             : 
      43             :   /**
      44             :    * Does this TabContext correspond to a mozbrowser?
      45             :    *
      46             :    * <iframe mozbrowser> is a mozbrowser element, but <xul:browser> is not.
      47             :    */
      48             :   bool IsMozBrowserElement() const;
      49             : 
      50             :   /**
      51             :    * Does this TabContext correspond to an isolated mozbrowser?
      52             :    *
      53             :    * <iframe mozbrowser> is a mozbrowser element, but <xul:browser> is not.
      54             :    * <iframe mozbrowser noisolation> does not count as isolated since isolation
      55             :    * is disabled.  Isolation can only be disabled by chrome pages.
      56             :    */
      57             :   bool IsIsolatedMozBrowserElement() const;
      58             : 
      59             :   /**
      60             :    * Does this TabContext correspond to a mozbrowser?  This is equivalent to
      61             :    * IsMozBrowserElement().  Returns false for <xul:browser>, which isn't a
      62             :    * mozbrowser.
      63             :    */
      64             :   bool IsMozBrowser() const;
      65             : 
      66             :   bool IsJSPlugin() const;
      67             :   int32_t JSPluginId() const;
      68             : 
      69             :   /**
      70             :    * OriginAttributesRef() returns the OriginAttributes of this frame to
      71             :    * the caller. This is used to store any attribute associated with the frame's
      72             :    * docshell.
      73             :    */
      74             :   const OriginAttributes& OriginAttributesRef() const;
      75             : 
      76             :   /**
      77             :    * Returns the presentation URL associated with the tab if this tab is
      78             :    * created for presented content
      79             :    */
      80             :   const nsAString& PresentationURL() const;
      81             : 
      82             :   UIStateChangeType ShowAccelerators() const;
      83             :   UIStateChangeType ShowFocusRings() const;
      84             : 
      85             : protected:
      86             :   friend class MaybeInvalidTabContext;
      87             : 
      88             :   /**
      89             :    * These protected mutator methods let you modify a TabContext once.  Further
      90             :    * attempts to modify a given TabContext will fail (the method will return
      91             :    * false).
      92             :    *
      93             :    * These mutators will also fail if the TabContext was created with anything
      94             :    * other than the no-args constructor.
      95             :    */
      96             : 
      97             :   /**
      98             :    * Set this TabContext to match the given TabContext.
      99             :    */
     100             :   bool SetTabContext(const TabContext& aContext);
     101             : 
     102             :   /**
     103             :    * Set the tab context's origin attributes to a private browsing value.
     104             :    */
     105             :   void SetPrivateBrowsingAttributes(bool aIsPrivateBrowsing);
     106             : 
     107             :   bool SetTabContext(bool aIsMozBrowserElement,
     108             :                      bool aIsPrerendered,
     109             :                      UIStateChangeType aShowAccelerators,
     110             :                      UIStateChangeType aShowFocusRings,
     111             :                      const OriginAttributes& aOriginAttributes,
     112             :                      const nsAString& aPresentationURL);
     113             : 
     114             :   /**
     115             :    * Modify this TabContext to match the given TabContext.  This is a special
     116             :    * case triggered by nsFrameLoader::SwapWithOtherRemoteLoader which may have
     117             :    * caused the owner content to change.
     118             :    *
     119             :    * This special case only allows the field `mIsMozBrowserElement` to be
     120             :    * changed.  If any other fields have changed, the update is ignored and
     121             :    * returns false.
     122             :    */
     123             :   bool UpdateTabContextAfterSwap(const TabContext& aContext);
     124             : 
     125             :   /**
     126             :    * Whether this TabContext is in prerender mode.
     127             :    */
     128             :   bool mIsPrerendered;
     129             : 
     130             :   /**
     131             :    * Set this TabContext to be for a JS plugin. aPluginID is the id of the JS plugin
     132             :    * (@see nsFakePlugin::mId).
     133             :    * As with the other protected mutator methods, this lets you modify a TabContext once.
     134             :    * (@see TabContext::SetTabContext above for more details).
     135             :    */
     136             :   bool SetTabContextForJSPluginFrame(int32_t aJSPluginID);
     137             : 
     138             : private:
     139             :   /**
     140             :    * Has this TabContext been initialized?  If so, mutator methods will fail.
     141             :    */
     142             :   bool mInitialized;
     143             : 
     144             :   /**
     145             :    * Whether this TabContext corresponds to a mozbrowser.
     146             :    *
     147             :    * <iframe mozbrowser> and <xul:browser> are not considered to be
     148             :    * mozbrowser elements.
     149             :    */
     150             :   bool mIsMozBrowserElement;
     151             : 
     152             :   int32_t mJSPluginID;
     153             : 
     154             :   /**
     155             :    * OriginAttributes of the top level tab docShell
     156             :    */
     157             :   OriginAttributes mOriginAttributes;
     158             : 
     159             :   /**
     160             :    * The requested presentation URL.
     161             :    */
     162             :   nsString mPresentationURL;
     163             : 
     164             :   /**
     165             :    * Keyboard indicator state (focus rings, accelerators).
     166             :    */
     167             :   UIStateChangeType mShowAccelerators;
     168             :   UIStateChangeType mShowFocusRings;
     169             : };
     170             : 
     171             : /**
     172             :  * MutableTabContext is the same as MaybeInvalidTabContext, except the mutation
     173             :  * methods are public instead of protected.  You can still only call these
     174             :  * mutation methods once on a given object.
     175             :  */
     176           6 : class MutableTabContext : public TabContext
     177             : {
     178             : public:
     179             :   bool SetTabContext(const TabContext& aContext)
     180             :   {
     181             :     return TabContext::SetTabContext(aContext);
     182             :   }
     183             : 
     184             :   bool
     185           3 :   SetTabContext(bool aIsMozBrowserElement,
     186             :                 bool aIsPrerendered,
     187             :                 UIStateChangeType aShowAccelerators,
     188             :                 UIStateChangeType aShowFocusRings,
     189             :                 const OriginAttributes& aOriginAttributes,
     190             :                 const nsAString& aPresentationURL = EmptyString())
     191             :   {
     192           3 :     return TabContext::SetTabContext(aIsMozBrowserElement,
     193             :                                      aIsPrerendered,
     194             :                                      aShowAccelerators,
     195             :                                      aShowFocusRings,
     196             :                                      aOriginAttributes,
     197           3 :                                      aPresentationURL);
     198             :   }
     199             : 
     200           0 :   bool SetTabContextForJSPluginFrame(uint32_t aJSPluginID)
     201             :   {
     202           0 :     return TabContext::SetTabContextForJSPluginFrame(aJSPluginID);
     203             :   }
     204             : 
     205             : };
     206             : 
     207             : /**
     208             :  * MaybeInvalidTabContext is a simple class that lets you transform an
     209             :  * IPCTabContext into a TabContext.
     210             :  *
     211             :  * The issue is that an IPCTabContext is not necessarily valid.  So to convert
     212             :  * an IPCTabContext into a TabContext, you construct a MaybeInvalidTabContext,
     213             :  * check whether it's valid, and, if so, read out your TabContext.
     214             :  *
     215             :  * Example usage:
     216             :  *
     217             :  *   void UseTabContext(const TabContext& aTabContext);
     218             :  *
     219             :  *   void CreateTab(const IPCTabContext& aContext) {
     220             :  *     MaybeInvalidTabContext tc(aContext);
     221             :  *     if (!tc.IsValid()) {
     222             :  *       NS_ERROR(nsPrintfCString("Got an invalid IPCTabContext: %s",
     223             :  *                                tc.GetInvalidReason()));
     224             :  *       return;
     225             :  *     }
     226             :  *     UseTabContext(tc.GetTabContext());
     227             :  *   }
     228             :  */
     229           2 : class MaybeInvalidTabContext
     230             : {
     231             : public:
     232             :   /**
     233             :    * This constructor copies the information in aContext and sets IsValid() as
     234             :    * appropriate.
     235             :    */
     236             :   explicit MaybeInvalidTabContext(const IPCTabContext& aContext);
     237             : 
     238             :   /**
     239             :    * Was the IPCTabContext we received in our constructor valid?
     240             :    */
     241             :   bool IsValid();
     242             : 
     243             :   /**
     244             :    * If IsValid(), this function returns null.  Otherwise, it returns a
     245             :    * human-readable string indicating why the IPCTabContext passed to our
     246             :    * constructor was not valid.
     247             :    */
     248             :   const char* GetInvalidReason();
     249             : 
     250             :   /**
     251             :    * If IsValid(), this function returns a reference to a TabContext
     252             :    * corresponding to the IPCTabContext passed to our constructor.  If
     253             :    * !IsValid(), this function crashes.
     254             :    */
     255             :   const TabContext& GetTabContext();
     256             : 
     257             : private:
     258             :   MaybeInvalidTabContext(const MaybeInvalidTabContext&) = delete;
     259             :   MaybeInvalidTabContext& operator=(const MaybeInvalidTabContext&) = delete;
     260             : 
     261             :   const char* mInvalidReason;
     262             :   MutableTabContext mTabContext;
     263             : };
     264             : 
     265             : } // namespace dom
     266             : } // namespace mozilla
     267             : 
     268             : #endif

Generated by: LCOV version 1.13