sbLocalDatabaseAsyncGUIDArray.h
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 #ifndef __SBLOCALDATABASEASYNCGUIDARRAY_H__
28 #define __SBLOCALDATABASEASYNCGUIDARRAY_H__
29 
30 #include <sbILocalDatabaseAsyncGUIDArray.h>
31 #include <sbILocalDatabaseGUIDArray.h>
32 
33 #include <nsAutoPtr.h>
34 #include <nsCOMPtr.h>
35 #include <nsIObserver.h>
36 #include <nsIRunnable.h>
37 #include <nsStringGlue.h>
38 #include <nsWeakReference.h>
39 #include <nsTArray.h>
40 #include <prlock.h>
41 #include <prmon.h>
42 
43 class nsIProxyObjectManager;
44 class nsIThread;
45 class nsIWeakReference;
48 
49 // These need compilation unit scoping so both classes can use them
56 };
57 
58 struct CommandSpec {
60  type(eNone),
61  index(0)
62  {
63  }
65  PRUint32 index;
66 };
67 
68 typedef nsTArray<CommandSpec> sbCommandQueue;
69 
71  public nsIObserver,
72  public nsSupportsWeakReference
73 {
74 public:
75  friend class CommandProcessor;
76 
78  NS_DECL_SBILOCALDATABASEGUIDARRAY
79  NS_DECL_SBILOCALDATABASEASYNCGUIDARRAY
80  NS_DECL_NSIOBSERVER
81 
82  nsresult Init();
83  nsresult InitalizeThread();
84  nsresult ShutdownThread();
85  nsresult EnqueueCommand(CommandType aType, PRUint32 aIndex);
86 
89 
90 private:
91 
92  // GUID array this class wraps
93  nsCOMPtr<sbILocalDatabaseGUIDArray> mInner;
94 
95  // Array of listeners that get notified when commands complete
96  typedef nsAutoPtr<sbLocalDatabaseAsyncGUIDArrayListenerInfo>
97  sbLocalDatabaseAsyncGUIDArrayListenerInfoAutoPtr;
98  nsTArray<sbLocalDatabaseAsyncGUIDArrayListenerInfoAutoPtr>
99  mAsyncListenerArray;
100 
101  // Queue of pending commands
102  sbCommandQueue mQueue;
103 
104  // This monitor protects methods that are called synchronously
105  PRMonitor* mSyncMonitor;
106 
107  // Monitor over mQueue and calls to InitalizeThread()
108  PRMonitor* mQueueMonitor;
109 
110  // Background thread
111  nsCOMPtr<nsIThread> mThread;
112 
113  // Tell our background thread it should exit
114  PRPackedBool mThreadShouldExit;
115 
116  nsresult SendOnGetLength(PRUint32 aLength, nsresult aResult);
117  nsresult SendOnGetGuidByIndex(PRUint32 aIndex,
118  const nsAString& aGUID,
119  nsresult aResult);
120  nsresult SendOnGetSortPropertyValueByIndex(PRUint32 aIndex,
121  const nsAString& aGUID,
122  nsresult aResult);
123  nsresult SendOnGetMediaItemIdByIndex(PRUint32 aIndex,
124  PRUint32 aMediaItemId,
125  nsresult aResult);
126  nsresult SendOnStateChange(PRUint32 aState);
127 };
128 
129 class CommandProcessor : public nsIRunnable
130 {
131 public:
133  NS_DECL_NSIRUNNABLE
134 
136 
138 
139 protected:
141 };
142 
144 {
145 public:
147  NS_DECL_SBILOCALDATABASEASYNCGUIDARRAYLISTENER
148 
149  sbWeakAsyncListenerWrapper(nsIWeakReference* aWeakListener);
151 
152  already_AddRefed<sbILocalDatabaseAsyncGUIDArrayListener> GetListener();
153 
154 private:
155  nsCOMPtr<nsIWeakReference> mWeakListener;
156 };
157 
159 {
161 public:
164 
165  nsresult Init(nsIProxyObjectManager *aProxyObjMgr,
166  nsIWeakReference* aWeakListener);
167 
168 private:
169 
170  nsCOMPtr<nsISupports> mRef;
171 
172  // The weak listener wrapper
173  nsRefPtr<sbWeakAsyncListenerWrapper> mWeakListenerWrapper;
174 
175  // Listener that gets notified when commands complete
176  nsCOMPtr<sbILocalDatabaseAsyncGUIDArrayListener> mProxiedListener;
177 };
178 
179 #endif /* __SBLOCALDATABASEASYNCGUIDARRAY_H__ */
180 
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
nsTArray< CommandSpec > sbCommandQueue
sbLocalDatabaseAsyncGUIDArray * mFriendArray
NS_DECL_ISUPPORTS NS_DECL_NSIRUNNABLE CommandProcessor(sbLocalDatabaseAsyncGUIDArray *aFriendArray)
nsresult EnqueueCommand(CommandType aType, PRUint32 aIndex)
already_AddRefed< sbILocalDatabaseAsyncGUIDArrayListener > GetListener()
nsresult Init(nsIProxyObjectManager *aProxyObjMgr, nsIWeakReference *aWeakListener)
NS_DECL_ISUPPORTS NS_DECL_SBILOCALDATABASEASYNCGUIDARRAYLISTENER sbWeakAsyncListenerWrapper(nsIWeakReference *aWeakListener)
NS_DECL_ISUPPORTS NS_DECL_SBILOCALDATABASEGUIDARRAY NS_DECL_SBILOCALDATABASEASYNCGUIDARRAY NS_DECL_NSIOBSERVER nsresult Init()
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
restoreWindow aState