michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #if !defined(MediaResourceServer_h_) michael@0: #define MediaResourceServer_h_ michael@0: michael@0: #include michael@0: #include "nsIServerSocket.h" michael@0: #include "MediaResource.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: class MediaResource; michael@0: michael@0: /* michael@0: MediaResourceServer instantiates a socket server that understands michael@0: HTTP requests for MediaResource instances. The server runs on an michael@0: automatically selected port and MediaResource instances are registered. michael@0: The registration returns a string URL than can be used to fetch the michael@0: resource. That URL contains a randomly generated path to make it michael@0: difficult for other local applications on the device to guess it. michael@0: michael@0: The HTTP protocol is limited in that it supports only what the michael@0: Android DataSource implementation uses to fetch media. It michael@0: understands HTTP GET and byte range requests. michael@0: michael@0: The intent of this class is to be used in Media backends that michael@0: have a system component that does its own network requests. These michael@0: requests are made against this server which then uses standard michael@0: Gecko network requests and media cache usage. michael@0: michael@0: The MediaResourceServer can be instantiated on any thread and michael@0: its methods are threadsafe - they can be called on any thread. michael@0: The server socket itself is always run on the main thread and michael@0: this is done by the Start() static method by synchronously michael@0: dispatching to the main thread. michael@0: */ michael@0: class MediaResourceServer : public nsRunnable michael@0: { michael@0: private: michael@0: // Mutex protecting private members of MediaResourceServer. michael@0: // All member variables below this point in the class definition michael@0: // must acquire the mutex before access. michael@0: mozilla::Mutex mMutex; michael@0: michael@0: // Server socket used to listen for incoming connections michael@0: nsCOMPtr mSocket; michael@0: michael@0: // Mapping between MediaResource URL's to the MediaResource michael@0: // object served at that URL. michael@0: typedef std::map > ResourceMap; michael@0: ResourceMap mResources; michael@0: michael@0: // Create a MediaResourceServer that will listen on an automatically michael@0: // selected port when started. This is private as it should only be michael@0: // called internally from the public 'Start' method. michael@0: MediaResourceServer(); michael@0: NS_IMETHOD Run(); michael@0: michael@0: // Append a random URL path to a string. This is used for creating a michael@0: // unique URl for a resource which helps prevent malicious software michael@0: // running on the same machine as the server from guessing the URL michael@0: // and accessing video data. michael@0: nsresult AppendRandomPath(nsCString& aURL); michael@0: michael@0: public: michael@0: // Create a MediaResourceServer and start it listening. This call will michael@0: // perform a synchronous request on the main thread. michael@0: static already_AddRefed Start(); michael@0: michael@0: // Stops the server from listening and accepting further connections. michael@0: void Stop(); michael@0: michael@0: // Add a MediaResource to be served by this server. Stores the michael@0: // absolute URL that can be used to access the resource in 'aUrl'. michael@0: nsresult AddResource(mozilla::MediaResource* aResource, nsCString& aUrl); michael@0: michael@0: // Remove a MediaResource so it is no longer served by this server. michael@0: // The URL provided must match exactly that provided by a previous michael@0: // call to "AddResource". michael@0: void RemoveResource(nsCString const& aUrl); michael@0: michael@0: // Returns the prefix for HTTP requests to the server. This plus michael@0: // the result of AddResource results in an Absolute URL. michael@0: nsCString GetURLPrefix(); michael@0: michael@0: // Returns the resource asociated with a given URL michael@0: already_AddRefed GetResource(nsCString const& aUrl); michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif