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 file,
5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef DocGroup_h
8 #define DocGroup_h
9 
10 #include "nsISupports.h"
11 #include "nsISupportsImpl.h"
12 #include "nsIPrincipal.h"
13 #include "nsTHashtable.h"
14 #include "nsString.h"
15 
16 #include "mozilla/RefPtr.h"
17 
18 namespace mozilla {
19 namespace dom {
20 
21 // Two browsing contexts are considered "related" if they are reachable from one
22 // another through window.opener, window.parent, or window.frames. This is the
23 // spec concept of a "unit of related browsing contexts"
24 //
25 // Two browsing contexts are considered "similar-origin" if they can be made to
26 // have the same origin by setting document.domain. This is the spec concept of
27 // a "unit of similar-origin related browsing contexts"
28 //
29 // A TabGroup is a set of browsing contexts which are all "related". Within a
30 // TabGroup, browsing contexts are broken into "similar-origin" DocGroups. In
31 // more detail, a DocGroup is actually a collection of documents, and a
32 // TabGroup is a collection of DocGroups. A TabGroup typically will contain
33 // (through its DocGroups) the documents from one or more tabs related by
34 // window.opener. A DocGroup is a member of exactly one TabGroup.
35 
36 class TabGroup;
37 
38 class DocGroup final : public nsISupports
39 {
40 public:
41   typedef nsTArray<nsIDocument*>::iterator Iterator;
42   friend class TabGroup;
43 
44   NS_DECL_THREADSAFE_ISUPPORTS
45 
46   static void GetKey(nsIPrincipal* aPrincipal, nsACString& aString);
MatchesKey(const nsACString & aKey)47   bool MatchesKey(const nsACString& aKey)
48   {
49     return aKey == mKey;
50   }
GetTabGroup()51   TabGroup* GetTabGroup()
52   {
53     return mTabGroup;
54   }
55   void RemoveDocument(nsIDocument* aWindow);
56 
57   // Iterators for iterating over every document within the DocGroup
begin()58   Iterator begin()
59   {
60     return mDocuments.begin();
61   }
end()62   Iterator end()
63   {
64     return mDocuments.end();
65   }
66 
67 private:
68   DocGroup(TabGroup* aTabGroup, const nsACString& aKey);
69   ~DocGroup();
70 
71   nsCString mKey;
72   RefPtr<TabGroup> mTabGroup;
73   nsTArray<nsIDocument*> mDocuments;
74 };
75 
76 } // namespace dom
77 } // namespace mozilla
78 
79 #endif // defined(DocGroup_h)
80