summaryrefslogtreecommitdiffstats
path: root/dom/system/gonk/VolumeManager.h
blob: 7c0503389fa2a28cfaa89ad46e3cf1529e4c448b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_system_volumemanager_h__
#define mozilla_system_volumemanager_h__

#include <vector>
#include <queue>

#include "base/message_loop.h"
#include "mozilla/FileUtils.h"
#include "mozilla/Observer.h"
#include "nsISupportsImpl.h"
#include "nsString.h"
#include "nsTArray.h"

#include "Volume.h"
#include "VolumeCommand.h"

namespace mozilla {
namespace system {

/***************************************************************************
*
*   All of the public API mentioned in this file (unless otherwise
*   mentioned) must run from the IOThread.
*
***************************************************************************/

/***************************************************************************
*
*   The VolumeManager class is a front-end for android's vold service.
*
*   Vold uses a unix socket interface and accepts null-terminated string
*   commands. The following commands were determined by examining the vold
*   source code:
*
*       volume list
*       volume mount <volname>
*       volume unmount <volname> [force]
*       volume debug [on|off]
*       volume format <volname>
*       volume share <volname> <method>
*       volume unshare <volname> <method>
*       volume shared <volname> <method>
*
*           <volname> is the name of the volume as used in /system/etc/vold.fstab
*           <method> is ums
*
*       dump
*
*       share status <method>	(Determines if a particular sharing method is available)
*                             (GB only - not available in ICS)
*
*       storage users		(??? always crashes vold ???)
*
*       asec list
*       asec ...lots more...
*
*       obb list
*       obb ...lots more...
*
*       xwarp enable
*       xwarp disable
*       xwarp status
*
*   There is also a command line tool called vdc, which can be used to send
*   the above commands to vold.
*
*   Currently, only the volume list, share/unshare, and mount/unmount
*   commands are being used.
*
***************************************************************************/

class VolumeManager final : public MessageLoopForIO::LineWatcher
{
  virtual ~VolumeManager();

public:
  NS_INLINE_DECL_REFCOUNTING(VolumeManager)

  typedef nsTArray<RefPtr<Volume>> VolumeArray;

  VolumeManager();

  //-----------------------------------------------------------------------
  //
  // State related methods.
  //
  // The VolumeManager starts off in the STARTING state. Once a connection
  // is established with vold, it asks for a list of volumes, and once the
  // volume list has been received, then the VolumeManager enters the
  // VOLUMES_READY state.
  //
  // If vold crashes, then the VolumeManager will once again enter the
  // STARTING state and try to reestablish a connection with vold.

  enum STATE
  {
    UNINITIALIZED,
    STARTING,
    VOLUMES_READY
  };

  static STATE State();
  static const char* StateStr(STATE aState);
  static const char* StateStr() { return StateStr(State()); }

  class StateChangedEvent
  {
  public:
    StateChangedEvent() {}
  };

  typedef mozilla::Observer<StateChangedEvent>      StateObserver;
  typedef mozilla::ObserverList<StateChangedEvent>  StateObserverList;

  static void RegisterStateObserver(StateObserver* aObserver);
  static void UnregisterStateObserver(StateObserver* aObserver);

  //-----------------------------------------------------------------------

  static void Start();
  static void Dump(const char* aLabel);

  static VolumeArray::size_type NumVolumes();
  static already_AddRefed<Volume> GetVolume(VolumeArray::index_type aIndex);
  static already_AddRefed<Volume> FindVolumeByName(const nsCSubstring& aName);
  static already_AddRefed<Volume> FindAddVolumeByName(const nsCSubstring& aName);
  static bool RemoveVolumeByName(const nsCSubstring& aName);
  static void InitConfig();

  static void       PostCommand(VolumeCommand* aCommand);

protected:

  virtual void OnLineRead(int aFd, nsDependentCSubstring& aMessage);
  virtual void OnFileCanWriteWithoutBlocking(int aFd);
  virtual void OnError();

  static void DefaultConfig();

private:
  bool OpenSocket();

  friend class VolumeListCallback; // Calls SetState

  static void SetState(STATE aNewState);

  void Restart();
  void WriteCommandData();
  void HandleBroadcast(int aResponseCode, nsCString& aResponseLine);

  typedef std::queue<RefPtr<VolumeCommand> > CommandQueue;

  static STATE              mState;
  static StateObserverList  mStateObserverList;

  static const int    kRcvBufSize = 1024;
  ScopedClose         mSocket;
  VolumeArray         mVolumeArray;
  CommandQueue        mCommands;
  bool                mCommandPending;
  MessageLoopForIO::FileDescriptorWatcher mReadWatcher;
  MessageLoopForIO::FileDescriptorWatcher mWriteWatcher;
  RefPtr<VolumeResponseCallback>          mBroadcastCallback;
};

/***************************************************************************
*
*   The initialization/shutdown functions do not need to be called from
*   the IOThread context.
*
***************************************************************************/

/**
 * Initialize the Volume Manager. On initialization, the VolumeManager will
 * attempt to connect with vold and collect the list of volumes that vold
 * knows about.
 */
void InitVolumeManager();

/**
 * Shuts down the Volume Manager.
 */
void ShutdownVolumeManager();

} // system
} // mozilla

#endif  // mozilla_system_volumemanager_h__