WindowCloak.cpp
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25  */
26 
32 #include "WindowCloak.h"
33 
34 #include <nsIDOMWindow.h>
35 #include <nsPIDOMWindow.h>
36 #include <nsIDocShell.h>
37 #include <nsIDocShellTreeItem.h>
38 #include <nsIDocShellTreeOwner.h>
39 #include <nsIEmbeddingSiteWindow.h>
40 #include <nsCOMPtr.h>
41 #include <nsIInterfaceRequestorUtils.h>
42 
44 {
45  // This will delete all of the sbCloakInfo objects that are still laying
46  // around.
47  if (mCloakedWindows.IsInitialized())
48  mCloakedWindows.Clear();
49 }
50 
52 
53 NS_IMETHODIMP
54 sbWindowCloak::Cloak(nsIDOMWindow* aDOMWindow)
55 {
56  NS_ENSURE_ARG_POINTER(aDOMWindow);
57  return SetVisibility(aDOMWindow, PR_FALSE);
58 }
59 
60 NS_IMETHODIMP
61 sbWindowCloak::Uncloak(nsIDOMWindow* aDOMWindow)
62 {
63  NS_ENSURE_ARG_POINTER(aDOMWindow);
64  return SetVisibility(aDOMWindow, PR_TRUE);
65 }
66 
67 NS_IMETHODIMP
68 sbWindowCloak::IsCloaked(nsIDOMWindow* aDOMWindow,
69  PRBool* _retval)
70 {
71  NS_ENSURE_ARG_POINTER(aDOMWindow);
72 
73  // Return early if nothing has been added to our hashtable.
74  if (!mCloakedWindows.IsInitialized()) {
75  *_retval = PR_FALSE;
76  return NS_OK;
77  }
78 
79  nsresult rv;
80  nsCOMPtr<nsPIDOMWindow> pWindow =
81  do_QueryInterface(aDOMWindow, &rv);
82  NS_ENSURE_SUCCESS(rv, rv);
83 
84  nsIDocShell* docShell = pWindow->GetDocShell();
85  NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
86 
87  nsCOMPtr<nsIDocShellTreeItem> treeItem = do_QueryInterface(docShell, &rv);
88  NS_ENSURE_SUCCESS(rv, rv);
89 
90  nsCOMPtr<nsIDocShellTreeOwner> treeOwner;
91  rv = treeItem->GetTreeOwner(getter_AddRefs(treeOwner));
92  NS_ENSURE_SUCCESS(rv, rv);
93 
94  nsCOMPtr<nsIEmbeddingSiteWindow> embedWindow =
95  do_GetInterface(treeOwner, &rv);
96  NS_ENSURE_SUCCESS(rv, rv);
97 
98  sbCloakInfo* cloakInfo = nsnull;
99  mCloakedWindows.Get(embedWindow, &cloakInfo);
100 
101  *_retval = cloakInfo && !cloakInfo->mVisible ? PR_TRUE : PR_FALSE;
102  return NS_OK;
103 }
104 
105 NS_IMETHODIMP
107  PRBool aVisible)
108 {
109  // XXXben Someday we could just ask the window if it is visible rather than
110  // trying to maintain a list ourselves.
111 
112  // Make sure the hashtable is initialized.
113  if (!mCloakedWindows.IsInitialized())
114  NS_ENSURE_TRUE(mCloakedWindows.Init(), NS_ERROR_FAILURE);
115 
116  nsresult rv;
117  nsCOMPtr<nsPIDOMWindow> pWindow =
118  do_QueryInterface(aDOMWindow, &rv);
119  NS_ENSURE_SUCCESS(rv, rv);
120 
121  nsIDocShell* docShell = pWindow->GetDocShell();
122  NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
123 
124  nsCOMPtr<nsIDocShellTreeItem> treeItem = do_QueryInterface(docShell, &rv);
125  NS_ENSURE_SUCCESS(rv, rv);
126 
127  nsCOMPtr<nsIDocShellTreeOwner> treeOwner;
128  rv = treeItem->GetTreeOwner(getter_AddRefs(treeOwner));
129  NS_ENSURE_SUCCESS(rv, rv);
130 
131  nsCOMPtr<nsIEmbeddingSiteWindow> embedWindow =
132  do_GetInterface(treeOwner, &rv);
133  NS_ENSURE_SUCCESS(rv, rv);
134 
135  // See if we have previously cloaked this window.
136  sbCloakInfo* cloakInfo = nsnull;
137  mCloakedWindows.Get(embedWindow, &cloakInfo);
138 
139  if (!cloakInfo) {
140  // This is the first time we have seen this window so assume it is already
141  // visible. Return early if there is nothing to do.
142  if (aVisible)
143  return NS_OK;
144 
145  // Otherwise make a new sbCloakInfo structure to hold state info.
146  NS_NEWXPCOM(cloakInfo, sbCloakInfo);
147  NS_ENSURE_TRUE(cloakInfo, NS_ERROR_OUT_OF_MEMORY);
148 
149  // Add it to our hashtable here so that the memory will be freed on
150  // shutdown.
151  if (!mCloakedWindows.Put(embedWindow, cloakInfo))
152  return NS_ERROR_FAILURE;
153 
154  // Set the defaults.
155  cloakInfo->mVisible = PR_TRUE;
156  }
157 
158  rv = embedWindow->SetVisibility(aVisible);
159  NS_ENSURE_SUCCESS(rv, rv);
160 
161  // Everything has succeeded so update the sbCloakInfo with the new state.
162  cloakInfo->mVisible = aVisible;
163 
164  return NS_OK;
165 }
return NS_OK
browser docShell
NS_IMETHOD SetVisibility(nsIDOMWindow *aDOMWindow, PRBool aVisible)
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
PRBool mVisible
Definition: WindowCloak.h:58
nsClassHashtable< nsISupportsHashKey, sbCloakInfo > mCloakedWindows
Definition: WindowCloak.h:72
const nsIDOMWindow
NS_DECL_ISUPPORTS NS_DECL_SBIWINDOWCLOAK ~sbWindowCloak()
Definition: WindowCloak.cpp:43
Window cloaking service interface This is an interface to the window cloaking service, used to hide a xul window without closing it, and to unhide it at a later time.
Songbird Window Cloaker Object Definition.