Bug 813438 - Part 1: Implement NS_SniffContent; r=bzbarsky
This commit is contained in:
@@ -79,6 +79,8 @@
|
|||||||
#include "nsIPrivateBrowsingChannel.h"
|
#include "nsIPrivateBrowsingChannel.h"
|
||||||
#include "mozIApplicationClearPrivateDataParams.h"
|
#include "mozIApplicationClearPrivateDataParams.h"
|
||||||
#include "nsIOfflineCacheUpdate.h"
|
#include "nsIOfflineCacheUpdate.h"
|
||||||
|
#include "nsIContentSniffer.h"
|
||||||
|
#include "nsCategoryCache.h"
|
||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
@@ -2154,4 +2156,46 @@ NS_GenerateHostPort(const nsCString& host, int32_t port,
|
|||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sniff the content type for a given request or a given buffer.
|
||||||
|
*
|
||||||
|
* aSnifferType can be either NS_CONTENT_SNIFFER_CATEGORY or
|
||||||
|
* NS_DATA_SNIFFER_CATEGORY. The function returns the sniffed content type
|
||||||
|
* in the aSniffedType argument. This argument will not be modified if the
|
||||||
|
* content type could not be sniffed.
|
||||||
|
*/
|
||||||
|
inline void
|
||||||
|
NS_SniffContent(const char* aSnifferType, nsIRequest* aRequest,
|
||||||
|
const uint8_t* aData, uint32_t aLength,
|
||||||
|
nsACString& aSniffedType)
|
||||||
|
{
|
||||||
|
typedef nsCategoryCache<nsIContentSniffer> ContentSnifferCache;
|
||||||
|
extern NS_HIDDEN_(ContentSnifferCache*) gNetSniffers;
|
||||||
|
extern NS_HIDDEN_(ContentSnifferCache*) gDataSniffers;
|
||||||
|
ContentSnifferCache* cache = nullptr;
|
||||||
|
if (!strcmp(aSnifferType, NS_CONTENT_SNIFFER_CATEGORY)) {
|
||||||
|
if (!gNetSniffers) {
|
||||||
|
gNetSniffers = new ContentSnifferCache(NS_CONTENT_SNIFFER_CATEGORY);
|
||||||
|
}
|
||||||
|
cache = gNetSniffers;
|
||||||
|
} else if (!strcmp(aSnifferType, NS_DATA_SNIFFER_CATEGORY)) {
|
||||||
|
if (!gDataSniffers) {
|
||||||
|
gDataSniffers = new ContentSnifferCache(NS_DATA_SNIFFER_CATEGORY);
|
||||||
|
}
|
||||||
|
cache = gDataSniffers;
|
||||||
|
} else {
|
||||||
|
// Invalid content sniffer type was requested
|
||||||
|
MOZ_ASSERT(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const nsCOMArray<nsIContentSniffer>& sniffers = cache->GetEntries();
|
||||||
|
for (int32_t i = 0; i < sniffers.Count(); ++i) {
|
||||||
|
nsresult rv = sniffers[i]->GetMIMETypeFromContent(aRequest, aData, aLength, aSniffedType);
|
||||||
|
if (NS_SUCCEEDED(rv) && !aSniffedType.IsEmpty()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endif // !nsNetUtil_h__
|
#endif // !nsNetUtil_h__
|
||||||
|
|||||||
@@ -1027,6 +1027,12 @@
|
|||||||
*/
|
*/
|
||||||
#define NS_CONTENT_SNIFFER_CATEGORY "net-content-sniffers"
|
#define NS_CONTENT_SNIFFER_CATEGORY "net-content-sniffers"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Services in this category can sniff content that is not necessarily loaded
|
||||||
|
* from the network, and they won't be told about each load.
|
||||||
|
*/
|
||||||
|
#define NS_DATA_SNIFFER_CATEGORY "content-sniffing-services"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Must implement nsINSSErrorsService.
|
* Must implement nsINSSErrorsService.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -34,6 +34,8 @@
|
|||||||
#include "nsDNSPrefetch.h"
|
#include "nsDNSPrefetch.h"
|
||||||
#include "nsAboutProtocolHandler.h"
|
#include "nsAboutProtocolHandler.h"
|
||||||
#include "nsXULAppAPI.h"
|
#include "nsXULAppAPI.h"
|
||||||
|
#include "nsCategoryCache.h"
|
||||||
|
#include "nsIContentSniffer.h"
|
||||||
|
|
||||||
#include "nsNetCID.h"
|
#include "nsNetCID.h"
|
||||||
|
|
||||||
@@ -45,6 +47,10 @@
|
|||||||
#define BUILD_BINHEX_DECODER 1
|
#define BUILD_BINHEX_DECODER 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
typedef nsCategoryCache<nsIContentSniffer> ContentSnifferCache;
|
||||||
|
NS_HIDDEN_(ContentSnifferCache*) gNetSniffers = nullptr;
|
||||||
|
NS_HIDDEN_(ContentSnifferCache*) gDataSniffers = nullptr;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include "nsIOService.h"
|
#include "nsIOService.h"
|
||||||
@@ -643,6 +649,11 @@ static void nsNetShutdown()
|
|||||||
// Release the Websocket Admission Manager
|
// Release the Websocket Admission Manager
|
||||||
mozilla::net::WebSocketChannel::Shutdown();
|
mozilla::net::WebSocketChannel::Shutdown();
|
||||||
#endif // NECKO_PROTOCOL_websocket
|
#endif // NECKO_PROTOCOL_websocket
|
||||||
|
|
||||||
|
delete gNetSniffers;
|
||||||
|
gNetSniffers = nullptr;
|
||||||
|
delete gDataSniffers;
|
||||||
|
gDataSniffers = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_DEFINE_NAMED_CID(NS_IOSERVICE_CID);
|
NS_DEFINE_NAMED_CID(NS_IOSERVICE_CID);
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ class NS_COM_GLUE nsCategoryObserver MOZ_FINAL : public nsIObserver {
|
|||||||
* then get the name of the category.
|
* then get the name of the category.
|
||||||
*/
|
*/
|
||||||
template<class T>
|
template<class T>
|
||||||
class nsCategoryCache : protected nsCategoryListener {
|
class nsCategoryCache MOZ_FINAL : protected nsCategoryListener {
|
||||||
public:
|
public:
|
||||||
explicit nsCategoryCache(const char* aCategory);
|
explicit nsCategoryCache(const char* aCategory);
|
||||||
~nsCategoryCache() { if (mObserver) mObserver->ListenerDied(); }
|
~nsCategoryCache() { if (mObserver) mObserver->ListenerDied(); }
|
||||||
|
|||||||
Reference in New Issue
Block a user