1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is mozilla.org code.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Michal Novotny <michal.novotny@gmail.com>.
18 : * Portions created by the Initial Developer are Copyright (C) 2009
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either of the GNU General Public License Version 2 or later (the "GPL"),
25 : * or GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #include "nsHttpActivityDistributor.h"
38 : #include "nsIChannel.h"
39 : #include "nsCOMPtr.h"
40 : #include "nsAutoPtr.h"
41 : #include "nsNetUtil.h"
42 : #include "nsThreadUtils.h"
43 :
44 : using namespace mozilla;
45 :
46 : class nsHttpActivityEvent : public nsRunnable
47 : {
48 : public:
49 100 : nsHttpActivityEvent(nsISupports *aHttpChannel,
50 : PRUint32 aActivityType,
51 : PRUint32 aActivitySubtype,
52 : PRTime aTimestamp,
53 : PRUint64 aExtraSizeData,
54 : const nsACString & aExtraStringData,
55 : nsCOMArray<nsIHttpActivityObserver> *aObservers)
56 : : mHttpChannel(aHttpChannel)
57 : , mActivityType(aActivityType)
58 : , mActivitySubtype(aActivitySubtype)
59 : , mTimestamp(aTimestamp)
60 : , mExtraSizeData(aExtraSizeData)
61 : , mExtraStringData(aExtraStringData)
62 100 : , mObservers(*aObservers)
63 : {
64 100 : }
65 :
66 100 : NS_IMETHOD Run()
67 : {
68 200 : for (PRInt32 i = 0 ; i < mObservers.Count() ; i++)
69 100 : mObservers[i]->ObserveActivity(mHttpChannel, mActivityType,
70 : mActivitySubtype, mTimestamp,
71 100 : mExtraSizeData, mExtraStringData);
72 100 : return NS_OK;
73 : }
74 :
75 : private:
76 200 : virtual ~nsHttpActivityEvent()
77 100 : {
78 400 : }
79 :
80 : nsCOMPtr<nsISupports> mHttpChannel;
81 : PRUint32 mActivityType;
82 : PRUint32 mActivitySubtype;
83 : PRTime mTimestamp;
84 : PRUint64 mExtraSizeData;
85 : nsCString mExtraStringData;
86 :
87 : nsCOMArray<nsIHttpActivityObserver> mObservers;
88 : };
89 :
90 16081 : NS_IMPL_THREADSAFE_ISUPPORTS2(nsHttpActivityDistributor,
91 : nsIHttpActivityDistributor,
92 : nsIHttpActivityObserver)
93 :
94 263 : nsHttpActivityDistributor::nsHttpActivityDistributor()
95 263 : : mLock("nsHttpActivityDistributor.mLock")
96 : {
97 263 : }
98 :
99 526 : nsHttpActivityDistributor::~nsHttpActivityDistributor()
100 : {
101 1052 : }
102 :
103 : NS_IMETHODIMP
104 100 : nsHttpActivityDistributor::ObserveActivity(nsISupports *aHttpChannel,
105 : PRUint32 aActivityType,
106 : PRUint32 aActivitySubtype,
107 : PRTime aTimestamp,
108 : PRUint64 aExtraSizeData,
109 : const nsACString & aExtraStringData)
110 : {
111 200 : nsRefPtr<nsIRunnable> event;
112 : {
113 200 : MutexAutoLock lock(mLock);
114 :
115 100 : if (!mObservers.Count())
116 0 : return NS_OK;
117 :
118 : event = new nsHttpActivityEvent(aHttpChannel, aActivityType,
119 : aActivitySubtype, aTimestamp,
120 : aExtraSizeData, aExtraStringData,
121 200 : &mObservers);
122 : }
123 100 : NS_ENSURE_TRUE(event, NS_ERROR_OUT_OF_MEMORY);
124 100 : return NS_DispatchToMainThread(event);
125 : }
126 :
127 : NS_IMETHODIMP
128 2989 : nsHttpActivityDistributor::GetIsActive(bool *isActive)
129 : {
130 2989 : NS_ENSURE_ARG_POINTER(isActive);
131 5978 : MutexAutoLock lock(mLock);
132 2989 : *isActive = !!mObservers.Count();
133 2989 : return NS_OK;
134 : }
135 :
136 : NS_IMETHODIMP
137 4 : nsHttpActivityDistributor::AddObserver(nsIHttpActivityObserver *aObserver)
138 : {
139 8 : MutexAutoLock lock(mLock);
140 :
141 4 : if (!mObservers.AppendObject(aObserver))
142 0 : return NS_ERROR_OUT_OF_MEMORY;
143 :
144 4 : return NS_OK;
145 : }
146 :
147 : NS_IMETHODIMP
148 4 : nsHttpActivityDistributor::RemoveObserver(nsIHttpActivityObserver *aObserver)
149 : {
150 8 : MutexAutoLock lock(mLock);
151 :
152 4 : if (!mObservers.RemoveObject(aObserver))
153 0 : return NS_ERROR_FAILURE;
154 :
155 4 : return NS_OK;
156 : }
|