dom/ipc/TabContext.h

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

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

mercurial