1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 ts=8 et 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/. */
7 #ifndef mozilla_dom_TabContext_h
8 #define mozilla_dom_TabContext_h
10 #include "mozilla/layout/RenderFrameUtils.h"
11 #include "mozIApplication.h"
20 * TabContext encapsulates information about an iframe that may be a mozbrowser
21 * or mozapp. You can ask whether a TabContext corresponds to a mozbrowser or
22 * mozapp, get the app that contains the browser, and so on.
24 * TabParent and TabChild both inherit from TabContext, and you can also have
25 * standalone TabContext objects.
27 * This class is immutable except by calling one of the protected
28 * SetTabContext*() methods (and those methods can only be called once). See
29 * also MutableTabContext.
34 typedef mozilla::layout::ScrollingBehavior ScrollingBehavior
;
39 /* (The implicit copy-constructor and operator= are fine.) */
42 * Generates IPCTabContext of type BrowserFrameIPCTabContext or
43 * AppFrameIPCTabContext from this TabContext's information.
45 IPCTabContext
AsIPCTabContext() const;
48 * Does this TabContext correspond to a mozbrowser? (<iframe mozbrowser
49 * mozapp> is not a browser.)
51 * If IsBrowserElement() is true, HasOwnApp() and HasAppOwnerApp() are
52 * guaranteed to be false.
54 * If IsBrowserElement() is false, HasBrowserOwnerApp() is guaranteed to be
57 bool IsBrowserElement() const;
60 * Does this TabContext correspond to a mozbrowser or mozapp? This is
61 * equivalent to IsBrowserElement() || HasOwnApp().
63 bool IsBrowserOrApp() const;
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.
70 * If HasOwnApp() is true, IsBrowserElement() is guaranteed to be false.
72 uint32_t OwnAppId() const;
73 already_AddRefed
<mozIApplication
> GetOwnApp() const;
74 bool HasOwnApp() const;
77 * BrowserOwnerAppId() gets the ID of the app which contains this browser
78 * frame. If this is not a browser frame (i.e., if !IsBrowserElement()), then
79 * BrowserOwnerAppId() is guaranteed to return NO_APP_ID.
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.
84 uint32_t BrowserOwnerAppId() const;
85 already_AddRefed
<mozIApplication
> GetBrowserOwnerApp() const;
86 bool HasBrowserOwnerApp() const;
89 * AppOwnerAppId() gets the ID of the app which contains this app frame. If
90 * this is not an app frame (i.e., if !HasOwnApp()), then AppOwnerAppId() is
91 * guaranteed to return NO_APP_ID.
93 * Even if we are an app frame, AppOwnerAppId() may still return NO_APP_ID, if
94 * this app frame is not contained inside an app.
96 uint32_t AppOwnerAppId() const;
97 already_AddRefed
<mozIApplication
> GetAppOwnerApp() const;
98 bool HasAppOwnerApp() const;
101 * OwnOrContainingAppId() gets the ID of this frame, if HasOwnApp(). If this
102 * frame does not have its own app, it gets the ID of the app which contains
103 * this frame (i.e., the result of {Browser,App}OwnerAppId(), as applicable).
105 uint32_t OwnOrContainingAppId() const;
106 already_AddRefed
<mozIApplication
> GetOwnOrContainingApp() const;
107 bool HasOwnOrContainingApp() const;
110 * Return the requested scrolling behavior for this frame.
112 ScrollingBehavior
GetScrollingBehavior() const { return mScrollingBehavior
; }
115 friend class MaybeInvalidTabContext
;
118 * These protected mutator methods let you modify a TabContext once. Further
119 * attempts to modify a given TabContext will fail (the method will return
122 * These mutators will also fail if the TabContext was created with anything
123 * other than the no-args constructor.
127 * Set this TabContext to match the given TabContext.
129 bool SetTabContext(const TabContext
& aContext
);
132 * Set this TabContext to be an app frame (with the given own app) inside the
133 * given app. Either or both apps may be null.
135 bool SetTabContextForAppFrame(mozIApplication
* aOwnApp
,
136 mozIApplication
* aAppFrameOwnerApp
,
137 ScrollingBehavior aRequestedBehavior
);
140 * Set this TabContext to be a browser frame inside the given app (which may
143 bool SetTabContextForBrowserFrame(mozIApplication
* aBrowserFrameOwnerApp
,
144 ScrollingBehavior aRequestedBehavior
);
147 * Set this TabContext to be a normal non-browser non-app frame.
149 bool SetTabContextForNormalFrame(ScrollingBehavior aRequestedBehavior
);
153 * Has this TabContext been initialized? If so, mutator methods will fail.
158 * This TabContext's own app. If this is non-null, then this
159 * TabContext corresponds to an app, and mIsBrowser must be false.
161 nsCOMPtr
<mozIApplication
> mOwnApp
;
164 * A cache of mOwnApp->GetLocalId(). Speed really does matter here, since we
165 * read this ID often during process startup.
170 * This TabContext's containing app. If mIsBrowser, this corresponds to the
171 * app which contains the browser frame; otherwise, this corresponds to the
172 * app which contains the app frame.
174 nsCOMPtr
<mozIApplication
> mContainingApp
;
177 * Cache of mContainingApp->GetLocalId().
179 uint32_t mContainingAppId
;
182 * The requested scrolling behavior for this frame.
184 ScrollingBehavior mScrollingBehavior
;
187 * Does this TabContext correspond to a browser element?
189 * If this is true, mOwnApp must be null.
195 * MutableTabContext is the same as MaybeInvalidTabContext, except the mutation
196 * methods are public instead of protected. You can still only call these
197 * mutation methods once on a given object.
199 class MutableTabContext
: public TabContext
202 bool SetTabContext(const TabContext
& aContext
)
204 return TabContext::SetTabContext(aContext
);
207 bool SetTabContextForAppFrame(mozIApplication
* aOwnApp
, mozIApplication
* aAppFrameOwnerApp
,
208 ScrollingBehavior aRequestedBehavior
)
210 return TabContext::SetTabContextForAppFrame(aOwnApp
, aAppFrameOwnerApp
,
214 bool SetTabContextForBrowserFrame(mozIApplication
* aBrowserFrameOwnerApp
,
215 ScrollingBehavior aRequestedBehavior
)
217 return TabContext::SetTabContextForBrowserFrame(aBrowserFrameOwnerApp
,
221 bool SetTabContextForNormalFrame(ScrollingBehavior aRequestedBehavior
)
223 return TabContext::SetTabContextForNormalFrame(aRequestedBehavior
);
228 * MaybeInvalidTabContext is a simple class that lets you transform an
229 * IPCTabContext into a TabContext.
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.
238 * void UseTabContext(const TabContext& aTabContext);
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()));
247 * UseTabContext(tc.GetTabContext());
250 class MaybeInvalidTabContext
254 * This constructor copies the information in aContext and sets IsValid() as
257 explicit MaybeInvalidTabContext(const IPCTabContext
& aContext
);
260 * Was the IPCTabContext we received in our constructor valid?
265 * If IsValid(), this function returns null. Otherwise, it returns a
266 * human-readable string indicating why the IPCTabContext passed to our
267 * constructor was not valid.
269 const char* GetInvalidReason();
272 * If IsValid(), this function returns a reference to a TabContext
273 * corresponding to the IPCTabContext passed to our constructor. If
274 * !IsValid(), this function crashes.
276 const TabContext
& GetTabContext();
279 MaybeInvalidTabContext(const MaybeInvalidTabContext
&) MOZ_DELETE
;
280 MaybeInvalidTabContext
& operator=(const MaybeInvalidTabContext
&) MOZ_DELETE
;
282 const char* mInvalidReason
;
283 MutableTabContext mTabContext
;
287 } // namespace mozilla