dom/system/gonk/VolumeManager.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/system/gonk/VolumeManager.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,187 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef mozilla_system_volumemanager_h__
     1.9 +#define mozilla_system_volumemanager_h__
    1.10 +
    1.11 +#include <vector>
    1.12 +#include <queue>
    1.13 +
    1.14 +#include "base/message_loop.h"
    1.15 +#include "mozilla/FileUtils.h"
    1.16 +#include "mozilla/Observer.h"
    1.17 +#include "nsISupportsImpl.h"
    1.18 +#include "nsString.h"
    1.19 +#include "nsTArray.h"
    1.20 +
    1.21 +#include "Volume.h"
    1.22 +#include "VolumeCommand.h"
    1.23 +
    1.24 +namespace mozilla {
    1.25 +namespace system {
    1.26 +
    1.27 +/***************************************************************************
    1.28 +*
    1.29 +*   All of the public API mentioned in this file (unless otherwise
    1.30 +*   mentioned) must run from the IOThread.
    1.31 +*
    1.32 +***************************************************************************/
    1.33 +
    1.34 +/***************************************************************************
    1.35 +*
    1.36 +*   The VolumeManager class is a front-end for android's vold service.
    1.37 +*
    1.38 +*   Vold uses a unix socket interface and accepts null-terminated string
    1.39 +*   commands. The following commands were determined by examining the vold
    1.40 +*   source code:
    1.41 +*
    1.42 +*       volume list
    1.43 +*       volume mount <volname>
    1.44 +*       volume unmount <volname> [force]
    1.45 +*       volume debug [on|off]
    1.46 +*       volume format <volname>
    1.47 +*       volume share <volname> <method>
    1.48 +*       volume unshare <volname> <method>
    1.49 +*       volume shared <volname> <method>
    1.50 +*
    1.51 +*           <volname> is the name of the volume as used in /system/etc/vold.fstab
    1.52 +*           <method> is ums
    1.53 +*
    1.54 +*       dump
    1.55 +*
    1.56 +*       share status <method>	(Determines if a particular sharing method is available)
    1.57 +*                             (GB only - not available in ICS)
    1.58 +*
    1.59 +*       storage users		(??? always crashes vold ???)
    1.60 +*
    1.61 +*       asec list
    1.62 +*       asec ...lots more...
    1.63 +*
    1.64 +*       obb list
    1.65 +*       obb ...lots more...
    1.66 +*
    1.67 +*       xwarp enable
    1.68 +*       xwarp disable
    1.69 +*       xwarp status
    1.70 +*
    1.71 +*   There is also a command line tool called vdc, which can be used to send
    1.72 +*   the above commands to vold.
    1.73 +*
    1.74 +*   Currently, only the volume list, share/unshare, and mount/unmount
    1.75 +*   commands are being used.
    1.76 +*
    1.77 +***************************************************************************/
    1.78 +
    1.79 +class VolumeManager MOZ_FINAL : public MessageLoopForIO::LineWatcher
    1.80 +{
    1.81 +  virtual ~VolumeManager();
    1.82 +
    1.83 +public:
    1.84 +  NS_INLINE_DECL_REFCOUNTING(VolumeManager)
    1.85 +
    1.86 +  typedef nsTArray<RefPtr<Volume>> VolumeArray;
    1.87 +
    1.88 +  VolumeManager();
    1.89 +
    1.90 +  //-----------------------------------------------------------------------
    1.91 +  //
    1.92 +  // State related methods.
    1.93 +  //
    1.94 +  // The VolumeManager starts off in the STARTING state. Once a connection
    1.95 +  // is established with vold, it asks for a list of volumes, and once the
    1.96 +  // volume list has been received, then the VolumeManager enters the
    1.97 +  // VOLUMES_READY state.
    1.98 +  //
    1.99 +  // If vold crashes, then the VolumeManager will once again enter the
   1.100 +  // STARTING state and try to reestablish a connection with vold.
   1.101 +
   1.102 +  enum STATE
   1.103 +  {
   1.104 +    UNINITIALIZED,
   1.105 +    STARTING,
   1.106 +    VOLUMES_READY
   1.107 +  };
   1.108 +
   1.109 +  static STATE State();
   1.110 +  static const char* StateStr(STATE aState);
   1.111 +  static const char* StateStr() { return StateStr(State()); }
   1.112 +
   1.113 +  class StateChangedEvent
   1.114 +  {
   1.115 +  public:
   1.116 +    StateChangedEvent() {}
   1.117 +  };
   1.118 +
   1.119 +  typedef mozilla::Observer<StateChangedEvent>      StateObserver;
   1.120 +  typedef mozilla::ObserverList<StateChangedEvent>  StateObserverList;
   1.121 +
   1.122 +  static void RegisterStateObserver(StateObserver* aObserver);
   1.123 +  static void UnregisterStateObserver(StateObserver* aObserver);
   1.124 +
   1.125 +  //-----------------------------------------------------------------------
   1.126 +
   1.127 +  static void Start();
   1.128 +
   1.129 +  static VolumeArray::size_type NumVolumes();
   1.130 +  static TemporaryRef<Volume> GetVolume(VolumeArray::index_type aIndex);
   1.131 +  static TemporaryRef<Volume> FindVolumeByName(const nsCSubstring& aName);
   1.132 +  static TemporaryRef<Volume> FindAddVolumeByName(const nsCSubstring& aName);
   1.133 +
   1.134 +  static void       PostCommand(VolumeCommand* aCommand);
   1.135 +
   1.136 +protected:
   1.137 +
   1.138 +  virtual void OnLineRead(int aFd, nsDependentCSubstring& aMessage);
   1.139 +  virtual void OnFileCanWriteWithoutBlocking(int aFd);
   1.140 +  virtual void OnError();
   1.141 +
   1.142 +private:
   1.143 +  bool OpenSocket();
   1.144 +
   1.145 +  friend class VolumeListCallback; // Calls SetState
   1.146 +
   1.147 +  static void SetState(STATE aNewState);
   1.148 +
   1.149 +  void Restart();
   1.150 +  void WriteCommandData();
   1.151 +  void HandleBroadcast(int aResponseCode, nsCString& aResponseLine);
   1.152 +
   1.153 +  typedef std::queue<RefPtr<VolumeCommand> > CommandQueue;
   1.154 +
   1.155 +  static STATE              mState;
   1.156 +  static StateObserverList  mStateObserverList;
   1.157 +
   1.158 +  static const int    kRcvBufSize = 1024;
   1.159 +  ScopedClose         mSocket;
   1.160 +  VolumeArray         mVolumeArray;
   1.161 +  CommandQueue        mCommands;
   1.162 +  bool                mCommandPending;
   1.163 +  MessageLoopForIO::FileDescriptorWatcher mReadWatcher;
   1.164 +  MessageLoopForIO::FileDescriptorWatcher mWriteWatcher;
   1.165 +  RefPtr<VolumeResponseCallback>          mBroadcastCallback;
   1.166 +};
   1.167 +
   1.168 +/***************************************************************************
   1.169 +*
   1.170 +*   The initialization/shutdown functions do not need to be called from
   1.171 +*   the IOThread context.
   1.172 +*
   1.173 +***************************************************************************/
   1.174 +
   1.175 +/**
   1.176 + * Initialize the Volume Manager. On initialization, the VolumeManager will
   1.177 + * attempt to connect with vold and collect the list of volumes that vold
   1.178 + * knows about.
   1.179 + */
   1.180 +void InitVolumeManager();
   1.181 +
   1.182 +/**
   1.183 + * Shuts down the Volume Manager.
   1.184 + */
   1.185 +void ShutdownVolumeManager();
   1.186 +
   1.187 +} // system
   1.188 +} // mozilla
   1.189 +
   1.190 +#endif  // mozilla_system_volumemanager_h__

mercurial