sbMediaSniffer.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 
27 #include "sbMediaSniffer.h"
28 
29 #include <nsICategoryManager.h>
30 #include <nsIHttpChannel.h>
31 #include <nsIRequest.h>
32 #include <nsIServiceManager.h>
33 #include <nsIURI.h>
34 #include <sbIMediacoreTypeSniffer.h>
35 #include <sbIPlaylistReader.h>
36 
37 #include <nsComponentManagerUtils.h>
38 #include <nsCOMPtr.h>
39 #include <nsNetCID.h>
40 #include <nsServiceManagerUtils.h>
41 #include <nsXPCOM.h>
42 #include <nsXPCOMCID.h>
43 #include <nsMemory.h>
44 
57 NS_IMPL_ISUPPORTS1(sbMediaSniffer, nsIContentSniffer)
58 
59 NS_IMETHODIMP
60 sbMediaSniffer::GetMIMETypeFromContent(nsIRequest* aRequest,
61  const PRUint8* aData,
62  PRUint32 aLength,
63  nsACString& aSniffedType)
64 {
65  NS_ENSURE_ARG_POINTER(aRequest);
66 
67  nsresult rv;
68 
69  // This won't always succeed, so don't warn for this expected failure.
70  nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aRequest, &rv);
71 
72  if (NS_SUCCEEDED(rv)) {
73  nsCAutoString requestMethod;
74  rv = httpChannel->GetRequestMethod(requestMethod);
75  NS_ENSURE_SUCCESS(rv, rv);
76 
77  if (!requestMethod.EqualsLiteral("GET") &&
78  !requestMethod.EqualsLiteral("POST")) {
79  aSniffedType.Truncate();
80  return NS_OK;
81  }
82  }
83 
84  nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest, &rv);
85  NS_ENSURE_SUCCESS(rv, rv);
86 
87  nsCAutoString contentType;
88  rv = channel->GetContentType(contentType);
89  NS_ENSURE_SUCCESS(rv, rv);
90 
91  // Never let a media core or playlist reader override the handling
92  // of the basic web content types.
93  if (contentType.EqualsLiteral("text/html")) {
94  aSniffedType.Truncate();
95  return NS_OK;
96  }
97 
98  // There are apparently problems with 'view-source'... See nsFeedSniffer.
99  nsCOMPtr<nsIURI> originalURI;
100  rv = channel->GetOriginalURI(getter_AddRefs(originalURI));
101  NS_ENSURE_SUCCESS(rv, rv);
102 
103  nsCAutoString scheme;
104  rv = originalURI->GetScheme(scheme);
105  NS_ENSURE_SUCCESS(rv, rv);
106 
107  if (scheme.EqualsLiteral("view-source")) {
108  aSniffedType.Truncate();
109  return NS_OK;
110  }
111 
112  nsCOMPtr<nsIURI> uri;
113  rv = channel->GetURI(getter_AddRefs(uri));
114  NS_ENSURE_SUCCESS(rv, rv);
115 
116  nsCOMPtr<sbIMediacoreTypeSniffer> typeSniffer =
117  do_GetService(SB_MEDIACORETYPESNIFFER_CONTRACTID, &rv);
118  NS_ENSURE_SUCCESS(rv, rv);
119 
120  PRBool isMediaURL = PR_FALSE;
121  rv = typeSniffer->IsValidMediaURL(uri,
122  &isMediaURL);
123 
124  if (NS_SUCCEEDED(rv) && isMediaURL) {
125 
126  // We seem to think this is media, make sure the content type that
127  // the server handed us at least makes sense. If it doesn't we will
128  // leave the contentType alone and let other sniffers have a go at
129  // it.
130  if( contentType.EqualsLiteral("text/html") ||
131  contentType.EqualsLiteral("application/atom+xml") ||
132  contentType.EqualsLiteral("application/rdf+xml") ||
133  contentType.EqualsLiteral("application/rss+xml") ||
134  contentType.EqualsLiteral("application/xml")) {
135  return NS_ERROR_NOT_AVAILABLE;
136  }
137 
138  aSniffedType.AssignLiteral(TYPE_MAYBE_MEDIA);
139  return NS_OK;
140  }
141 
142  PRBool isPlaylistURL = PR_FALSE;
143  rv = typeSniffer->IsValidWebSafePlaylistURL(uri,
144  &isPlaylistURL);
145  if (NS_SUCCEEDED(rv) && isPlaylistURL) {
146  aSniffedType.AssignLiteral(TYPE_MAYBE_PLAYLIST);
147  return NS_OK;
148  }
149 
150  aSniffedType.Truncate();
151  return NS_OK;
152 }
153 
154 NS_METHOD
155 sbMediaSniffer::Register(nsIComponentManager* aCompMgr,
156  nsIFile* aPath,
157  const char* aRegistryLocation,
158  const char* aComponentType,
159  const nsModuleComponentInfo* aInfo)
160 {
161  nsresult rv;
162  nsCOMPtr<nsICategoryManager> catman =
163  do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
164  NS_ENSURE_SUCCESS(rv, rv);
165 
166  rv = catman->AddCategoryEntry(NS_CONTENT_SNIFFER_CATEGORY,
169  PR_TRUE, PR_TRUE, nsnull);
170  NS_ENSURE_SUCCESS(rv, rv);
171 
172  return NS_OK;
173 }
174 
175 NS_METHOD
176 sbMediaSniffer::Unregister(nsIComponentManager* aCompMgr,
177  nsIFile* aPath,
178  const char* aRegistryLocation,
179  const nsModuleComponentInfo* aInfo)
180 {
181  nsresult rv;
182  nsCOMPtr<nsICategoryManager> catman =
183  do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
184  NS_ENSURE_SUCCESS(rv, rv);
185 
186  rv = catman->DeleteCategoryEntry(NS_CONTENT_SNIFFER_CATEGORY,
188  PR_TRUE);
189  NS_ENSURE_SUCCESS(rv, rv);
190 
191  return NS_OK;
192 }
const TYPE_MAYBE_MEDIA
return NS_OK
_updateCookies aPath
#define SB_MEDIACORETYPESNIFFER_CONTRACTID
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
#define SONGBIRD_MEDIASNIFFER_CONTRACTID
NS_DECL_ISUPPORTS static NS_DECL_NSICONTENTSNIFFER NS_METHOD Register(nsIComponentManager *compMgr, nsIFile *path, const char *registryLocation, const char *componentType, const nsModuleComponentInfo *info)
var uri
Definition: FeedWriter.js:1135
static NS_METHOD Unregister(nsIComponentManager *aCompMgr, nsIFile *aPath, const char *aRegistryLocation, const nsModuleComponentInfo *aInfo)
#define SONGBIRD_MEDIASNIFFER_DESCRIPTION
_updateTextAndScrollDataForFrame aData
const TYPE_MAYBE_PLAYLIST