summaryrefslogtreecommitdiff
path: root/dom/system/gonk/Volume.h
blob: 821292a9a40d12637054dbb642b4dd41a851bef3 (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
/* 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_volume_h__
#define mozilla_system_volume_h__

#include "VolumeCommand.h"
#include "nsIVolume.h"
#include "nsString.h"
#include "mozilla/Observer.h"
#include "nsISupportsImpl.h"
#include "nsWhitespaceTokenizer.h"

namespace mozilla {
namespace system {

/***************************************************************************
*
*   There is an instance of the Volume class for each volume reported
*   from vold.
*
*   Each volume originates from the /system/etv/vold.fstab file.
*
***************************************************************************/

class Volume;

#define DEBUG_VOLUME_OBSERVER 0

#if DEBUG_VOLUME_OBSERVER
class VolumeObserverList : public mozilla::ObserverList<Volume*>
{
public:
  void Broadcast(Volume* const& aVolume);
};
#else
typedef mozilla::ObserverList<Volume*> VolumeObserverList;
#endif

class Volume final
{
public:
  NS_INLINE_DECL_REFCOUNTING(Volume)

  Volume(const nsCSubstring& aVolumeName);

  typedef long STATE; // States are now defined in nsIVolume.idl

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

  const nsCString& Name() const { return mName; }
  const char* NameStr() const   { return mName.get(); }

  void Dump(const char* aLabel) const;

  // The mount point is the name of the directory where the volume is mounted.
  // (i.e. path that leads to the files stored on the volume).
  const nsCString& MountPoint() const { return mMountPoint; }

  uint32_t Id() const                 { return mId; }

  int32_t MountGeneration() const     { return mMountGeneration; }
  bool IsMountLocked() const          { return mMountLocked; }
  bool MediaPresent() const           { return mMediaPresent; }
  bool CanBeShared() const            { return mCanBeShared; }
  bool CanBeFormatted() const         { return CanBeShared(); }
  bool CanBeMounted() const           { return CanBeShared(); }
  bool IsSharingEnabled() const       { return mCanBeShared && mSharingEnabled; }
  bool IsFormatRequested() const      { return CanBeFormatted() && mFormatRequested; }
  bool IsMountRequested() const       { return CanBeMounted() && mMountRequested; }
  bool IsUnmountRequested() const     { return CanBeMounted() && mUnmountRequested; }
  bool IsSharing() const              { return mIsSharing; }
  bool IsFormatting() const           { return mIsFormatting; }
  bool IsUnmounting() const           { return mIsUnmounting; }
  bool IsRemovable() const            { return mIsRemovable; }
  bool IsHotSwappable() const         { return mIsHotSwappable; }

  void SetFakeVolume(const nsACString& aMountPoint);

  void SetSharingEnabled(bool aSharingEnabled);
  void SetFormatRequested(bool aFormatRequested);
  void SetMountRequested(bool aMountRequested);
  void SetUnmountRequested(bool aUnmountRequested);

  typedef mozilla::Observer<Volume *>     EventObserver;

  // NOTE: that observers must live in the IOThread.
  static void RegisterVolumeObserver(EventObserver* aObserver, const char* aName);
  static void UnregisterVolumeObserver(EventObserver* aObserver, const char* aName);

protected:
  ~Volume() {}

private:
  friend class AutoMounter;         // Calls StartXxx
  friend class nsVolume;            // Calls UpdateMountLock
  friend class VolumeManager;       // Calls HandleVoldResponse
  friend class VolumeListCallback;  // Calls SetMountPoint, SetState

  // The StartXxx functions will queue up a command to the VolumeManager.
  // You can queue up as many commands as you like, and aCallback will
  // be called as each one completes.
  void StartMount(VolumeResponseCallback* aCallback);
  void StartUnmount(VolumeResponseCallback* aCallback);
  void StartFormat(VolumeResponseCallback* aCallback);
  void StartShare(VolumeResponseCallback* aCallback);
  void StartUnshare(VolumeResponseCallback* aCallback);

  void SetIsSharing(bool aIsSharing);
  void SetIsFormatting(bool aIsFormatting);
  void SetIsUnmounting(bool aIsUnmounting);
  void SetIsRemovable(bool aIsRemovable);
  void SetIsHotSwappable(bool aIsHotSwappable);
  void SetState(STATE aNewState);
  void SetMediaPresent(bool aMediaPresent);
  void SetMountPoint(const nsCSubstring& aMountPoint);
  void StartCommand(VolumeCommand* aCommand);

  void ResolveAndSetMountPoint(const nsCSubstring& aMountPoint);

  bool BoolConfigValue(const nsCString& aConfigValue, bool& aBoolValue);
  void SetConfig(const nsCString& aConfigName, const nsCString& aConfigValue);

  void HandleVoldResponse(int aResponseCode, nsCWhitespaceTokenizer& aTokenizer);

  static void UpdateMountLock(const nsACString& aVolumeName,
                              const int32_t& aMountGeneration,
                              const bool& aMountLocked);

  bool              mMediaPresent;
  STATE             mState;
  const nsCString   mName;
  nsCString         mMountPoint;
  int32_t           mMountGeneration;
  bool              mMountLocked;
  bool              mSharingEnabled;
  bool              mFormatRequested;
  bool              mMountRequested;
  bool              mUnmountRequested;
  bool              mCanBeShared;
  bool              mIsSharing;
  bool              mIsFormatting;
  bool              mIsUnmounting;
  bool              mIsRemovable;
  bool              mIsHotSwappable;
  uint32_t          mId;                // Unique ID (used by MTP)

  static VolumeObserverList sEventObserverList;
};

} // system
} // mozilla

#endif  // mozilla_system_volumemanager_h__