sbTemporaryFileService.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 //------------------------------------------------------------------------------
28 //------------------------------------------------------------------------------
29 //
30 // Songbird temporary file service.
31 //
32 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------------------
34 
40 //------------------------------------------------------------------------------
41 //
42 // Songbird temporary file service imported services.
43 //
44 //------------------------------------------------------------------------------
45 
46 // Self imports.
47 #include "sbTemporaryFileService.h"
48 
49 // Songbird imports.
50 #include <sbFileUtils.h>
51 
52 // Mozilla imports.
53 #include <nsComponentManagerUtils.h>
54 #include <nsDirectoryServiceUtils.h>
55 #include <nsDirectoryServiceDefs.h>
56 
57 
58 //------------------------------------------------------------------------------
59 //
60 // Songbird temporary file service nsISupports implementation.
61 //
62 //------------------------------------------------------------------------------
63 
67 
68 
69 //------------------------------------------------------------------------------
70 //
71 // Songbird temporary file service sbITemporaryFileService implementation.
72 //
73 //------------------------------------------------------------------------------
74 
75 //-------------------------------------
76 //
77 // CreateFile
78 //
79 
80 NS_IMETHODIMP
81 sbTemporaryFileService::CreateFile(PRUint32 aType,
82  const nsAString& aBaseName,
83  const nsAString& aExtension,
84  nsIFile** _retval)
85 {
86  NS_ENSURE_STATE(mInitialized);
87  return mRootTemporaryFileFactory->CreateFile(aType,
88  aBaseName,
89  aExtension,
90  _retval);
91 }
92 
93 
94 //
95 // Getters/setters.
96 //
97 
98 //-------------------------------------
99 //
100 // rootTemporaryDirectory
101 //
102 
103 NS_IMETHODIMP
104 sbTemporaryFileService::GetRootTemporaryDirectory
105  (nsIFile** aRootTemporaryDirectory)
106 {
107  NS_ENSURE_STATE(mInitialized);
108  return mRootTemporaryFileFactory->GetRootTemporaryDirectory
109  (aRootTemporaryDirectory);
110 }
111 
112 
113 //------------------------------------------------------------------------------
114 //
115 // Songbird temporary file service nsIObserver implementation.
116 //
117 //------------------------------------------------------------------------------
118 
119 //-------------------------------------
120 //
121 // Observer
122 //
123 
124 NS_IMETHODIMP
125 sbTemporaryFileService::Observe(nsISupports* aSubject,
126  const char* aTopic,
127  const PRUnichar* aData)
128 {
129  nsresult rv;
130 
131  // Dispatch processing of event.
132  if (!strcmp(aTopic, "app-startup")) {
133  // Initialize the services.
134  rv = Initialize();
135  NS_ENSURE_SUCCESS(rv, rv);
136  }
137  else if (!strcmp(aTopic, "profile-after-change")) {
138  // User profile is now available.
139  mProfileAvailable = PR_TRUE;
140 
141  // Initialize the services.
142  rv = Initialize();
143  NS_ENSURE_SUCCESS(rv, rv);
144  }
145  else if (!strcmp(aTopic, "quit-application")) {
146  // Finalize the services.
147  Finalize();
148  }
149 
150  return NS_OK;
151 }
152 
153 
154 //------------------------------------------------------------------------------
155 //
156 // Songbird temporary file service public services.
157 //
158 //------------------------------------------------------------------------------
159 
160 //-------------------------------------
161 //
162 // sbTemporaryFileService
163 //
164 
166  mInitialized(PR_FALSE),
167  mProfileAvailable(PR_FALSE)
168 {
169 }
170 
171 
172 //-------------------------------------
173 //
174 // ~sbTemporaryFileService
175 //
176 
178 {
179  // Finalize the Songbird temporary file service.
180  Finalize();
181 }
182 
183 
184 //-------------------------------------
185 //
186 // Initialize
187 //
188 
189 nsresult
191 {
192  nsresult rv;
193 
194  // Add observers.
195  if (!mObserverService) {
196  mObserverService = do_GetService("@mozilla.org/observer-service;1", &rv);
197  NS_ENSURE_SUCCESS(rv, rv);
198  rv = mObserverService->AddObserver(this, "profile-after-change", PR_FALSE);
199  NS_ENSURE_SUCCESS(rv, rv);
200  rv = mObserverService->AddObserver(this, "quit-application", PR_FALSE);
201  NS_ENSURE_SUCCESS(rv, rv);
202  }
203 
204  // Wait for the user profile to be available.
205  if (!mProfileAvailable)
206  return NS_OK;
207 
208  // Set up the root temporary directory residing in the OS temporary directory.
209  nsCOMPtr<nsIFile> rootTemporaryDirectory;
210  rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR,
211  getter_AddRefs(rootTemporaryDirectory));
212  NS_ENSURE_SUCCESS(rv, rv);
213 
214  // Ensure the root temporary directory exists.
215  PRBool exists;
216  rv = rootTemporaryDirectory->Append
217  (NS_LITERAL_STRING(SB_TEMPORARY_FILE_SERVICE_ROOT_DIR_NAME));
218  NS_ENSURE_SUCCESS(rv, rv);
219  rv = rootTemporaryDirectory->Exists(&exists);
220  NS_ENSURE_SUCCESS(rv, rv);
221  if (!exists) {
222  rv = rootTemporaryDirectory->Create(nsIFile::DIRECTORY_TYPE,
224  NS_ENSURE_SUCCESS(rv, rv);
225  }
226 
227  // Create the root temporary file factory.
228  mRootTemporaryFileFactory =
229  do_CreateInstance("@songbirdnest.com/Songbird/TemporaryFileFactory;1", &rv);
230  NS_ENSURE_SUCCESS(rv, rv);
231  rv = mRootTemporaryFileFactory->SetRootTemporaryDirectory
232  (rootTemporaryDirectory);
233  NS_ENSURE_SUCCESS(rv, rv);
234 
235  // Services are now initialized.
236  mInitialized = PR_TRUE;
237 
238  return NS_OK;
239 }
240 
241 
242 //-------------------------------------
243 //
244 // Finalize
245 //
246 
247 void
249 {
250  // No longer initialized.
251  mInitialized = PR_FALSE;
252 
253  // Remove observers.
254  if (mObserverService) {
255  mObserverService->RemoveObserver(this, "profile-after-change");
256  mObserverService->RemoveObserver(this, "quit-application");
257  }
258  mObserverService = nsnull;
259 
260  // Remove the temporary file factory.
261  mRootTemporaryFileFactory = nsnull;
262 }
263 
return NS_OK
NS_IMPL_THREADSAFE_ISUPPORTS2(sbTemporaryFileService, sbITemporaryFileService, nsIObserver) NS_IMETHODIMP sbTemporaryFileService
NS_DECL_ISUPPORTS NS_DECL_SBITEMPORARYFILESERVICE NS_DECL_NSIOBSERVER sbTemporaryFileService()
const NS_OS_TEMP_DIR
readonly attribute nsIFile rootTemporaryDirectory
Root directory of temporary files and directories.
#define SB_TEMPORARY_FILE_SERVICE_ROOT_DIR_NAME
Songbird Temporary File Service Definitions.
#define SB_DEFAULT_DIRECTORY_PERMISSIONS
Definition: sbFileUtils.h:124
_updateTextAndScrollDataForFrame aData