1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/plugins/MediaResourceServer.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,96 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 +#if !defined(MediaResourceServer_h_) 1.10 +#define MediaResourceServer_h_ 1.11 + 1.12 +#include <map> 1.13 +#include "nsIServerSocket.h" 1.14 +#include "MediaResource.h" 1.15 + 1.16 +namespace mozilla { 1.17 + 1.18 +class MediaResource; 1.19 + 1.20 +/* 1.21 + MediaResourceServer instantiates a socket server that understands 1.22 + HTTP requests for MediaResource instances. The server runs on an 1.23 + automatically selected port and MediaResource instances are registered. 1.24 + The registration returns a string URL than can be used to fetch the 1.25 + resource. That URL contains a randomly generated path to make it 1.26 + difficult for other local applications on the device to guess it. 1.27 + 1.28 + The HTTP protocol is limited in that it supports only what the 1.29 + Android DataSource implementation uses to fetch media. It 1.30 + understands HTTP GET and byte range requests. 1.31 + 1.32 + The intent of this class is to be used in Media backends that 1.33 + have a system component that does its own network requests. These 1.34 + requests are made against this server which then uses standard 1.35 + Gecko network requests and media cache usage. 1.36 + 1.37 + The MediaResourceServer can be instantiated on any thread and 1.38 + its methods are threadsafe - they can be called on any thread. 1.39 + The server socket itself is always run on the main thread and 1.40 + this is done by the Start() static method by synchronously 1.41 + dispatching to the main thread. 1.42 +*/ 1.43 +class MediaResourceServer : public nsRunnable 1.44 +{ 1.45 +private: 1.46 + // Mutex protecting private members of MediaResourceServer. 1.47 + // All member variables below this point in the class definition 1.48 + // must acquire the mutex before access. 1.49 + mozilla::Mutex mMutex; 1.50 + 1.51 + // Server socket used to listen for incoming connections 1.52 + nsCOMPtr<nsIServerSocket> mSocket; 1.53 + 1.54 + // Mapping between MediaResource URL's to the MediaResource 1.55 + // object served at that URL. 1.56 + typedef std::map<nsCString, 1.57 + nsRefPtr<mozilla::MediaResource> > ResourceMap; 1.58 + ResourceMap mResources; 1.59 + 1.60 + // Create a MediaResourceServer that will listen on an automatically 1.61 + // selected port when started. This is private as it should only be 1.62 + // called internally from the public 'Start' method. 1.63 + MediaResourceServer(); 1.64 + NS_IMETHOD Run(); 1.65 + 1.66 + // Append a random URL path to a string. This is used for creating a 1.67 + // unique URl for a resource which helps prevent malicious software 1.68 + // running on the same machine as the server from guessing the URL 1.69 + // and accessing video data. 1.70 + nsresult AppendRandomPath(nsCString& aURL); 1.71 + 1.72 +public: 1.73 + // Create a MediaResourceServer and start it listening. This call will 1.74 + // perform a synchronous request on the main thread. 1.75 + static already_AddRefed<MediaResourceServer> Start(); 1.76 + 1.77 + // Stops the server from listening and accepting further connections. 1.78 + void Stop(); 1.79 + 1.80 + // Add a MediaResource to be served by this server. Stores the 1.81 + // absolute URL that can be used to access the resource in 'aUrl'. 1.82 + nsresult AddResource(mozilla::MediaResource* aResource, nsCString& aUrl); 1.83 + 1.84 + // Remove a MediaResource so it is no longer served by this server. 1.85 + // The URL provided must match exactly that provided by a previous 1.86 + // call to "AddResource". 1.87 + void RemoveResource(nsCString const& aUrl); 1.88 + 1.89 + // Returns the prefix for HTTP requests to the server. This plus 1.90 + // the result of AddResource results in an Absolute URL. 1.91 + nsCString GetURLPrefix(); 1.92 + 1.93 + // Returns the resource asociated with a given URL 1.94 + already_AddRefed<mozilla::MediaResource> GetResource(nsCString const& aUrl); 1.95 +}; 1.96 + 1.97 +} // namespace mozilla 1.98 + 1.99 +#endif