Line data Source code
1 : //* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-/
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef nsUrlClassifierStreamUpdater_h_
7 : #define nsUrlClassifierStreamUpdater_h_
8 :
9 : #include <nsISupportsUtils.h>
10 :
11 : #include "nsCOMPtr.h"
12 : #include "nsIObserver.h"
13 : #include "nsIUrlClassifierStreamUpdater.h"
14 : #include "nsIStreamListener.h"
15 : #include "nsIChannel.h"
16 : #include "nsTArray.h"
17 : #include "nsITimer.h"
18 : #include "mozilla/Attributes.h"
19 :
20 : // Forward declare pointers
21 : class nsIURI;
22 :
23 : class nsUrlClassifierStreamUpdater final : public nsIUrlClassifierStreamUpdater,
24 : public nsIUrlClassifierUpdateObserver,
25 : public nsIStreamListener,
26 : public nsIObserver,
27 : public nsIInterfaceRequestor,
28 : public nsITimerCallback
29 : {
30 : public:
31 : nsUrlClassifierStreamUpdater();
32 :
33 : NS_DECL_THREADSAFE_ISUPPORTS
34 : NS_DECL_NSIURLCLASSIFIERSTREAMUPDATER
35 : NS_DECL_NSIURLCLASSIFIERUPDATEOBSERVER
36 : NS_DECL_NSIINTERFACEREQUESTOR
37 : NS_DECL_NSIREQUESTOBSERVER
38 : NS_DECL_NSISTREAMLISTENER
39 : NS_DECL_NSIOBSERVER
40 : NS_DECL_NSITIMERCALLBACK
41 :
42 : private:
43 : // No subclassing
44 0 : ~nsUrlClassifierStreamUpdater() {}
45 :
46 : // When the dbservice sends an UpdateComplete or UpdateFailure, we call this
47 : // to reset the stream updater.
48 : void DownloadDone();
49 :
50 : // Disallow copy constructor
51 : nsUrlClassifierStreamUpdater(nsUrlClassifierStreamUpdater&);
52 :
53 : nsresult AddRequestBody(const nsACString &aRequestBody);
54 :
55 : // Fetches an update for a single table.
56 : nsresult FetchUpdate(nsIURI *aURI,
57 : const nsACString &aRequest,
58 : bool aIsPostRequest,
59 : const nsACString &aTable);
60 : // Dumb wrapper so we don't have to create URIs.
61 : nsresult FetchUpdate(const nsACString &aURI,
62 : const nsACString &aRequest,
63 : bool aIsPostRequest,
64 : const nsACString &aTable);
65 :
66 : // Fetches the next table, from mPendingUpdates.
67 : nsresult FetchNext();
68 : // Fetches the next request, from mPendingRequests
69 : nsresult FetchNextRequest();
70 :
71 : enum UpdateTimeout {
72 : eNoTimeout = 0,
73 : eResponseTimeout = 1,
74 : eDownloadTimeout = 2,
75 : };
76 :
77 : bool mIsUpdating;
78 : bool mInitialized;
79 : bool mDownloadError;
80 : bool mBeganStream;
81 :
82 : nsCString mDownloadErrorStatusStr;
83 :
84 : // Note that mStreamTable is only used by v2, it is empty for v4 update.
85 : nsCString mStreamTable;
86 :
87 : nsCOMPtr<nsIChannel> mChannel;
88 : nsCOMPtr<nsIUrlClassifierDBService> mDBService;
89 :
90 : // In v2, a update response might contain redirection and this
91 : // timer is for fetching the redirected update.
92 : nsCOMPtr<nsITimer> mFetchIndirectUpdatesTimer;
93 :
94 : // When we DownloadUpdate(), the DBService might be busy on processing
95 : // request issused outside of StreamUpdater. We have to fire a timer to
96 : // retry on our own.
97 : nsCOMPtr<nsITimer> mFetchNextRequestTimer;
98 :
99 : // Timer to abort the download if the server takes too long to respond.
100 : nsCOMPtr<nsITimer> mResponseTimeoutTimer;
101 :
102 : // Timer to abort the download if it takes too long.
103 : nsCOMPtr<nsITimer> mTimeoutTimer;
104 :
105 0 : struct PendingRequest {
106 : nsCString mTables;
107 : nsCString mRequestPayload;
108 : bool mIsPostRequest;
109 : nsCString mUrl;
110 : nsCOMPtr<nsIUrlClassifierCallback> mSuccessCallback;
111 : nsCOMPtr<nsIUrlClassifierCallback> mUpdateErrorCallback;
112 : nsCOMPtr<nsIUrlClassifierCallback> mDownloadErrorCallback;
113 : };
114 : nsTArray<PendingRequest> mPendingRequests;
115 :
116 0 : struct PendingUpdate {
117 : nsCString mUrl;
118 : nsCString mTable;
119 : };
120 : nsTArray<PendingUpdate> mPendingUpdates;
121 :
122 : nsCOMPtr<nsIUrlClassifierCallback> mSuccessCallback;
123 : nsCOMPtr<nsIUrlClassifierCallback> mUpdateErrorCallback;
124 : nsCOMPtr<nsIUrlClassifierCallback> mDownloadErrorCallback;
125 :
126 : // The provider for current update request and should be only used by telemetry
127 : // since it would show up as "other" for any other providers.
128 : nsCString mTelemetryProvider;
129 : PRIntervalTime mTelemetryClockStart;
130 : };
131 :
132 : #endif // nsUrlClassifierStreamUpdater_h_
|