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