dom/system/gonk/VolumeManager.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #ifndef mozilla_system_volumemanager_h__
     6 #define mozilla_system_volumemanager_h__
     8 #include <vector>
     9 #include <queue>
    11 #include "base/message_loop.h"
    12 #include "mozilla/FileUtils.h"
    13 #include "mozilla/Observer.h"
    14 #include "nsISupportsImpl.h"
    15 #include "nsString.h"
    16 #include "nsTArray.h"
    18 #include "Volume.h"
    19 #include "VolumeCommand.h"
    21 namespace mozilla {
    22 namespace system {
    24 /***************************************************************************
    25 *
    26 *   All of the public API mentioned in this file (unless otherwise
    27 *   mentioned) must run from the IOThread.
    28 *
    29 ***************************************************************************/
    31 /***************************************************************************
    32 *
    33 *   The VolumeManager class is a front-end for android's vold service.
    34 *
    35 *   Vold uses a unix socket interface and accepts null-terminated string
    36 *   commands. The following commands were determined by examining the vold
    37 *   source code:
    38 *
    39 *       volume list
    40 *       volume mount <volname>
    41 *       volume unmount <volname> [force]
    42 *       volume debug [on|off]
    43 *       volume format <volname>
    44 *       volume share <volname> <method>
    45 *       volume unshare <volname> <method>
    46 *       volume shared <volname> <method>
    47 *
    48 *           <volname> is the name of the volume as used in /system/etc/vold.fstab
    49 *           <method> is ums
    50 *
    51 *       dump
    52 *
    53 *       share status <method>	(Determines if a particular sharing method is available)
    54 *                             (GB only - not available in ICS)
    55 *
    56 *       storage users		(??? always crashes vold ???)
    57 *
    58 *       asec list
    59 *       asec ...lots more...
    60 *
    61 *       obb list
    62 *       obb ...lots more...
    63 *
    64 *       xwarp enable
    65 *       xwarp disable
    66 *       xwarp status
    67 *
    68 *   There is also a command line tool called vdc, which can be used to send
    69 *   the above commands to vold.
    70 *
    71 *   Currently, only the volume list, share/unshare, and mount/unmount
    72 *   commands are being used.
    73 *
    74 ***************************************************************************/
    76 class VolumeManager MOZ_FINAL : public MessageLoopForIO::LineWatcher
    77 {
    78   virtual ~VolumeManager();
    80 public:
    81   NS_INLINE_DECL_REFCOUNTING(VolumeManager)
    83   typedef nsTArray<RefPtr<Volume>> VolumeArray;
    85   VolumeManager();
    87   //-----------------------------------------------------------------------
    88   //
    89   // State related methods.
    90   //
    91   // The VolumeManager starts off in the STARTING state. Once a connection
    92   // is established with vold, it asks for a list of volumes, and once the
    93   // volume list has been received, then the VolumeManager enters the
    94   // VOLUMES_READY state.
    95   //
    96   // If vold crashes, then the VolumeManager will once again enter the
    97   // STARTING state and try to reestablish a connection with vold.
    99   enum STATE
   100   {
   101     UNINITIALIZED,
   102     STARTING,
   103     VOLUMES_READY
   104   };
   106   static STATE State();
   107   static const char* StateStr(STATE aState);
   108   static const char* StateStr() { return StateStr(State()); }
   110   class StateChangedEvent
   111   {
   112   public:
   113     StateChangedEvent() {}
   114   };
   116   typedef mozilla::Observer<StateChangedEvent>      StateObserver;
   117   typedef mozilla::ObserverList<StateChangedEvent>  StateObserverList;
   119   static void RegisterStateObserver(StateObserver* aObserver);
   120   static void UnregisterStateObserver(StateObserver* aObserver);
   122   //-----------------------------------------------------------------------
   124   static void Start();
   126   static VolumeArray::size_type NumVolumes();
   127   static TemporaryRef<Volume> GetVolume(VolumeArray::index_type aIndex);
   128   static TemporaryRef<Volume> FindVolumeByName(const nsCSubstring& aName);
   129   static TemporaryRef<Volume> FindAddVolumeByName(const nsCSubstring& aName);
   131   static void       PostCommand(VolumeCommand* aCommand);
   133 protected:
   135   virtual void OnLineRead(int aFd, nsDependentCSubstring& aMessage);
   136   virtual void OnFileCanWriteWithoutBlocking(int aFd);
   137   virtual void OnError();
   139 private:
   140   bool OpenSocket();
   142   friend class VolumeListCallback; // Calls SetState
   144   static void SetState(STATE aNewState);
   146   void Restart();
   147   void WriteCommandData();
   148   void HandleBroadcast(int aResponseCode, nsCString& aResponseLine);
   150   typedef std::queue<RefPtr<VolumeCommand> > CommandQueue;
   152   static STATE              mState;
   153   static StateObserverList  mStateObserverList;
   155   static const int    kRcvBufSize = 1024;
   156   ScopedClose         mSocket;
   157   VolumeArray         mVolumeArray;
   158   CommandQueue        mCommands;
   159   bool                mCommandPending;
   160   MessageLoopForIO::FileDescriptorWatcher mReadWatcher;
   161   MessageLoopForIO::FileDescriptorWatcher mWriteWatcher;
   162   RefPtr<VolumeResponseCallback>          mBroadcastCallback;
   163 };
   165 /***************************************************************************
   166 *
   167 *   The initialization/shutdown functions do not need to be called from
   168 *   the IOThread context.
   169 *
   170 ***************************************************************************/
   172 /**
   173  * Initialize the Volume Manager. On initialization, the VolumeManager will
   174  * attempt to connect with vold and collect the list of volumes that vold
   175  * knows about.
   176  */
   177 void InitVolumeManager();
   179 /**
   180  * Shuts down the Volume Manager.
   181  */
   182 void ShutdownVolumeManager();
   184 } // system
   185 } // mozilla
   187 #endif  // mozilla_system_volumemanager_h__

mercurial