30 #include <nsAutoLock.h>
33 #include <nsAutoPtr.h>
35 #include <nsILocalFile.h>
36 #include <nsServiceManagerUtils.h>
37 #include <nsComponentManagerUtils.h>
38 #include <nsIComponentRegistrar.h>
39 #include <nsISupportsPrimitives.h>
42 #include <nsIFileStreams.h>
43 #include <nsIFileURL.h>
44 #include <nsIIOService.h>
46 #include <nsNetUtil.h>
48 #include <nsStringGlue.h>
52 extern PRLogModuleInfo* gMetadataLog;
56 gMetadataLog = PR_NewLogModule("metadata"); \
57 PR_LOG(gMetadataLog, PR_LOG_DEBUG, args); \
60 #define __FUNCTION__ __PRETTY_FUNCTION__
63 #define LOG(args) PR_BEGIN_MACRO PR_END_MACRO
81 : m_pContractListLock(nsnull)
83 m_pContractListLock = PR_NewLock();
84 NS_ASSERTION(m_pContractListLock,
"Failed to create sbMetadataManager::m_pContractListLock! Object *not* threadsafe!");
88 nsCOMPtr<nsIComponentRegistrar>
registrar;
89 rv = NS_GetComponentRegistrar(getter_AddRefs(registrar));
93 nsCOMPtr<nsISimpleEnumerator> simpleEnumerator;
94 rv = registrar->EnumerateContractIDs(getter_AddRefs(simpleEnumerator));
98 PRBool moreAvailable = PR_FALSE;
99 while(simpleEnumerator->HasMoreElements(&moreAvailable) ==
NS_OK && moreAvailable)
101 nsCOMPtr<nsISupportsCString> contractString;
102 if (simpleEnumerator->GetNext(getter_AddRefs(contractString)) ==
NS_OK)
105 contractString->GetData(contractID);
106 if (contractID.Find(
"@songbirdnest.com/Songbird/MetadataHandler/") != -1)
108 m_ContractList.push_back(contractID);
115 sbMetadataManager::~sbMetadataManager()
117 if(m_pContractListLock)
119 PR_DestroyLock(m_pContractListLock);
120 m_pContractListLock = nsnull;
127 if (gMetadataManager) {
128 NS_ADDREF(gMetadataManager);
133 if (!gMetadataManager)
136 NS_ADDREF(gMetadataManager);
137 NS_ADDREF(gMetadataManager);
143 void sbMetadataManager::DestroySingleton()
145 nsRefPtr<sbMetadataManager> dummy;
146 dummy.swap(gMetadataManager);
150 const nsAString &strURL,
158 if(!_retval)
return NS_ERROR_NULL_POINTER;
160 nsresult rv = NS_ERROR_UNEXPECTED;
162 nsRefPtr<sbIMetadataHandler> pHandler;
163 nsCOMPtr<nsIChannel> pChannel;
164 nsCOMPtr<nsIIOService> pIOService = do_GetIOService(&rv);
165 NS_ENSURE_SUCCESS(rv, rv);
167 NS_ConvertUTF16toUTF8 cstrURL(strURL);
168 LOG((
"sbMetadataManager:: found new URI \"%s\"\n", cstrURL.get()));
170 nsCOMPtr<nsIURI> pURI;
171 rv = pIOService->NewURI(cstrURL, nsnull, nsnull, getter_AddRefs(pURI));
172 NS_ENSURE_SUCCESS(rv, rv);
178 nsCString cstrScheme;
179 rv = pURI->GetScheme(cstrScheme);
180 NS_ENSURE_SUCCESS(rv, rv);
182 if(cstrScheme.Length() <= 1)
184 nsCString cstrFixedURL = NS_LITERAL_CSTRING(
"file://");
185 cstrFixedURL += cstrURL;
187 pIOService->NewURI(cstrFixedURL, nsnull, nsnull, getter_AddRefs(pURI));
188 rv = pURI->GetScheme(cstrScheme);
189 NS_ENSURE_SUCCESS(rv, rv);
192 #if defined(XP_UNIX) && !defined(XP_MACOSX)
196 if(cstrScheme.EqualsLiteral(
"file")) {
198 nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(pURI, &rv);
199 if(NS_SUCCEEDED(rv)) {
201 nsCOMPtr<nsIFile>
file;
202 rv = fileURL->GetFile(getter_AddRefs(file));
203 NS_ENSURE_SUCCESS(rv, rv);
205 nsCOMPtr<nsIFile> cloneFile;
206 rv = file->Clone(getter_AddRefs(cloneFile));
207 NS_ENSURE_SUCCESS(rv, rv);
209 nsCOMPtr<nsILocalFile> localFile(do_QueryInterface(cloneFile));
213 nsresult rv2 = localFile->GetPersistentDescriptor(spec);
214 nsCOMPtr<nsINetUtil> netUtil =
215 do_CreateInstance(
"@mozilla.org/network/util;1", &rv2);
216 nsCString escapedSpec;
217 rv2 = netUtil->EscapeString(spec,
218 nsINetUtil::ESCAPE_URL_PATH,
220 nsCOMPtr<nsIURI> pNewURI;
221 if (NS_SUCCEEDED(rv2)) {
222 escapedSpec.Insert(
"file://", 0);
223 rv2 = pIOService->NewURI(escapedSpec, nsnull, nsnull, getter_AddRefs(pNewURI));
224 if (NS_SUCCEEDED(rv2)) {
225 LOG((
"using persistentDescriptor instead: %s\n", escapedSpec.BeginReading()));
234 rv = pIOService->NewChannelFromURI(pURI, getter_AddRefs(pChannel));
235 NS_ENSURE_SUCCESS(rv, rv);
238 handlerlist_t handlerlist;
241 rv = pURI->GetSpec( u8Url );
242 NS_ENSURE_SUCCESS(rv, rv);
244 nsString
url = NS_ConvertUTF8toUTF16(u8Url);
246 NS_ENSURE_TRUE(m_ContractList.size() > 0, NS_ERROR_FAILURE);
249 for (contractlist_t::iterator
i = m_ContractList.begin();
i != m_ContractList.end();
i++ )
251 nsCOMPtr<sbIMetadataHandler>
handler = do_CreateInstance((*i).get(), &rv);
252 if(NS_SUCCEEDED(rv) && handler.get())
255 handler->Vote( url, &vote );
258 sbMetadataHandlerItem item;
262 item.m_ContractID = (*i);
264 handlerlist.insert( item );
270 if ( handlerlist.rbegin() != handlerlist.rend() )
272 handlerlist_t::reverse_iterator
i = handlerlist.rbegin();
277 pHandler = (*i).m_Handler;
283 nsCString previousContractID, nextContractID;
285 aHandler->GetContractID(previousContractID);
287 bool useNext =
false;
288 for(; i != handlerlist.rend(); ++
i)
293 pHandler = (*i).m_Handler;
297 (*i).m_Handler->GetContractID(nextContractID);
298 if (nextContractID == previousContractID)
307 #if defined(PR_LOGGING)
310 pHandler->GetContractID(contractID);
311 LOG((
"%s[%p]: using handler %s", __FUNCTION__,
this, contractID.BeginReading()));
315 NS_ENSURE_TRUE(pHandler, NS_ERROR_UNEXPECTED);
318 rv = pHandler->SetChannel(pChannel);
319 NS_ENSURE_SUCCESS(rv, rv);
321 pHandler.swap(*_retval);
328 NS_IMETHODIMP sbMetadataManager::GetHandlerForMediaURL(
const nsAString &strURL,
sbIMetadataHandler **_retval)
330 nsresult rv = GetHandlerInternal(nsnull, strURL, _retval);
331 NS_ENSURE_SUCCESS(rv, rv);
338 NS_IMETHODIMP sbMetadataManager::GetNextHandlerForMediaURL(
340 const nsAString &aUrl,
343 NS_ENSURE_ARG_POINTER(aHandler);
344 NS_ENSURE_ARG_POINTER(_retval);
345 nsresult rv = GetHandlerInternal(aHandler, aUrl, _retval);
346 NS_ENSURE_SUCCESS(rv, rv);
sbDeviceFirmwareAutoCheckForUpdate prototype contractID
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility
_getSelectedPageStyle s i