content/media/plugins/MediaResourceServer.h

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6 #if !defined(MediaResourceServer_h_)
michael@0 7 #define MediaResourceServer_h_
michael@0 8
michael@0 9 #include <map>
michael@0 10 #include "nsIServerSocket.h"
michael@0 11 #include "MediaResource.h"
michael@0 12
michael@0 13 namespace mozilla {
michael@0 14
michael@0 15 class MediaResource;
michael@0 16
michael@0 17 /*
michael@0 18 MediaResourceServer instantiates a socket server that understands
michael@0 19 HTTP requests for MediaResource instances. The server runs on an
michael@0 20 automatically selected port and MediaResource instances are registered.
michael@0 21 The registration returns a string URL than can be used to fetch the
michael@0 22 resource. That URL contains a randomly generated path to make it
michael@0 23 difficult for other local applications on the device to guess it.
michael@0 24
michael@0 25 The HTTP protocol is limited in that it supports only what the
michael@0 26 Android DataSource implementation uses to fetch media. It
michael@0 27 understands HTTP GET and byte range requests.
michael@0 28
michael@0 29 The intent of this class is to be used in Media backends that
michael@0 30 have a system component that does its own network requests. These
michael@0 31 requests are made against this server which then uses standard
michael@0 32 Gecko network requests and media cache usage.
michael@0 33
michael@0 34 The MediaResourceServer can be instantiated on any thread and
michael@0 35 its methods are threadsafe - they can be called on any thread.
michael@0 36 The server socket itself is always run on the main thread and
michael@0 37 this is done by the Start() static method by synchronously
michael@0 38 dispatching to the main thread.
michael@0 39 */
michael@0 40 class MediaResourceServer : public nsRunnable
michael@0 41 {
michael@0 42 private:
michael@0 43 // Mutex protecting private members of MediaResourceServer.
michael@0 44 // All member variables below this point in the class definition
michael@0 45 // must acquire the mutex before access.
michael@0 46 mozilla::Mutex mMutex;
michael@0 47
michael@0 48 // Server socket used to listen for incoming connections
michael@0 49 nsCOMPtr<nsIServerSocket> mSocket;
michael@0 50
michael@0 51 // Mapping between MediaResource URL's to the MediaResource
michael@0 52 // object served at that URL.
michael@0 53 typedef std::map<nsCString,
michael@0 54 nsRefPtr<mozilla::MediaResource> > ResourceMap;
michael@0 55 ResourceMap mResources;
michael@0 56
michael@0 57 // Create a MediaResourceServer that will listen on an automatically
michael@0 58 // selected port when started. This is private as it should only be
michael@0 59 // called internally from the public 'Start' method.
michael@0 60 MediaResourceServer();
michael@0 61 NS_IMETHOD Run();
michael@0 62
michael@0 63 // Append a random URL path to a string. This is used for creating a
michael@0 64 // unique URl for a resource which helps prevent malicious software
michael@0 65 // running on the same machine as the server from guessing the URL
michael@0 66 // and accessing video data.
michael@0 67 nsresult AppendRandomPath(nsCString& aURL);
michael@0 68
michael@0 69 public:
michael@0 70 // Create a MediaResourceServer and start it listening. This call will
michael@0 71 // perform a synchronous request on the main thread.
michael@0 72 static already_AddRefed<MediaResourceServer> Start();
michael@0 73
michael@0 74 // Stops the server from listening and accepting further connections.
michael@0 75 void Stop();
michael@0 76
michael@0 77 // Add a MediaResource to be served by this server. Stores the
michael@0 78 // absolute URL that can be used to access the resource in 'aUrl'.
michael@0 79 nsresult AddResource(mozilla::MediaResource* aResource, nsCString& aUrl);
michael@0 80
michael@0 81 // Remove a MediaResource so it is no longer served by this server.
michael@0 82 // The URL provided must match exactly that provided by a previous
michael@0 83 // call to "AddResource".
michael@0 84 void RemoveResource(nsCString const& aUrl);
michael@0 85
michael@0 86 // Returns the prefix for HTTP requests to the server. This plus
michael@0 87 // the result of AddResource results in an Absolute URL.
michael@0 88 nsCString GetURLPrefix();
michael@0 89
michael@0 90 // Returns the resource asociated with a given URL
michael@0 91 already_AddRefed<mozilla::MediaResource> GetResource(nsCString const& aUrl);
michael@0 92 };
michael@0 93
michael@0 94 } // namespace mozilla
michael@0 95
michael@0 96 #endif

mercurial