Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_places_History_h_
8
#define mozilla_places_History_h_
9
10
#include "mozilla/BaseHistory.h"
11
#include "mozilla/MemoryReporting.h"
12
#include "mozilla/Move.h"
13
#include "mozilla/Mutex.h"
14
#include "mozIAsyncHistory.h"
15
#include "Database.h"
16
17
#include "mozilla/dom/Link.h"
18
#include "mozilla/dom/PContentChild.h"
19
#include "nsDataHashtable.h"
20
#include "nsTHashtable.h"
21
#include "nsString.h"
22
#include "nsURIHashKey.h"
23
#include "nsTObserverArray.h"
24
#include "nsIMemoryReporter.h"
25
#include "nsIObserver.h"
26
#include "mozIStorageConnection.h"
27
28
namespace mozilla {
29
namespace places {
30
31
struct VisitData;
32
class ConcurrentStatementsHolder;
33
class VisitedQuery;
34
35
// Initial size of mRecentlyVisitedURIs.
36
#define RECENTLY_VISITED_URIS_SIZE 64
37
// Microseconds after which a visit can be expired from mRecentlyVisitedURIs.
38
// When an URI is reloaded we only take into account the first visit to it, and
39
// ignore any subsequent visits, if they happen before this time has elapsed.
40
// A commonly found case is to reload a page every 5 minutes, so we pick a time
41
// larger than that.
42
#define RECENTLY_VISITED_URIS_MAX_AGE 6 * 60 * PR_USEC_PER_SEC
43
// When notifying the main thread after inserting visits, we chunk the visits
44
// into medium-sized groups so that we can amortize the cost of the runnable
45
// without janking the main thread by expecting it to process hundreds at once.
46
#define NOTIFY_VISITS_CHUNK_SIZE 100
47
48
class History final : public BaseHistory,
49
public mozIAsyncHistory,
50
public nsIObserver,
51
public nsIMemoryReporter {
52
public:
53
NS_DECL_THREADSAFE_ISUPPORTS
54
NS_DECL_MOZIASYNCHISTORY
55
NS_DECL_NSIOBSERVER
56
NS_DECL_NSIMEMORYREPORTER
57
58
// IHistory
59
NS_IMETHOD VisitURI(nsIWidget*, nsIURI*, nsIURI* aLastVisitedURI,
60
uint32_t aFlags) final;
61
NS_IMETHOD SetURITitle(nsIURI*, const nsAString&) final;
62
63
// BaseHistory
64
void StartPendingVisitedQueries(const PendingVisitedQueries&) final;
65
66
History();
67
68
nsresult QueueVisitedStatement(RefPtr<VisitedQuery>);
69
70
/**
71
* Adds an entry in moz_places with the data in aVisitData.
72
*
73
* @param aVisitData
74
* The visit data to use to populate a new row in moz_places.
75
* @param aShouldNotifyFrecencyChanged
76
* Whether to dispatch OnFrecencyChanged notifications.
77
* Defaults to true. Set to false if you (the caller) are
78
* doing many inserts and will dispatch your own
79
* OnManyFrecenciesChanged notification.
80
*/
81
nsresult InsertPlace(VisitData& aVisitData,
82
bool aShouldNotifyFrecencyChanged = true);
83
84
/**
85
* Updates an entry in moz_places with the data in aVisitData.
86
*
87
* @param aVisitData
88
* The visit data to use to update the existing row in moz_places.
89
*/
90
nsresult UpdatePlace(const VisitData& aVisitData);
91
92
/**
93
* Loads information about the page into _place from moz_places.
94
*
95
* @param _place
96
* The VisitData for the place we need to know information about.
97
* @param [out] _exists
98
* Whether or the page was recorded in moz_places, false otherwise.
99
*/
100
nsresult FetchPageInfo(VisitData& _place, bool* _exists);
101
102
/**
103
* Get the number of bytes of memory this History object is using,
104
* including sizeof(*this))
105
*/
106
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);
107
108
/**
109
* Obtains a pointer to this service.
110
*/
111
static History* GetService();
112
113
/**
114
* Used by the service manager only.
115
*/
116
static already_AddRefed<History> GetSingleton();
117
118
template <int N>
119
already_AddRefed<mozIStorageStatement> GetStatement(const char (&aQuery)[N]) {
120
// May be invoked on both threads.
121
const mozIStorageConnection* dbConn = GetConstDBConn();
122
NS_ENSURE_TRUE(dbConn, nullptr);
123
return mDB->GetStatement(aQuery);
124
}
125
126
already_AddRefed<mozIStorageStatement> GetStatement(
127
const nsACString& aQuery) {
128
// May be invoked on both threads.
129
const mozIStorageConnection* dbConn = GetConstDBConn();
130
NS_ENSURE_TRUE(dbConn, nullptr);
131
return mDB->GetStatement(aQuery);
132
}
133
134
bool IsShuttingDown() const { return mShuttingDown; }
135
Mutex& GetShutdownMutex() { return mShutdownMutex; }
136
137
/**
138
* Helper function to append a new URI to mRecentlyVisitedURIs. See
139
* mRecentlyVisitedURIs.
140
* @param {nsIURI} aURI The URI to append
141
* @param {bool} aHidden The hidden status of the visit being appended.
142
*/
143
void AppendToRecentlyVisitedURIs(nsIURI* aURI, bool aHidden);
144
145
void NotifyVisitedParent(
146
const nsTArray<mozilla::dom::VisitedQueryResult>& aURIs);
147
148
private:
149
virtual ~History();
150
151
void InitMemoryReporter();
152
153
/**
154
* Obtains a read-write database connection, initializing the connection
155
* if needed. Must be invoked on the main thread.
156
*/
157
mozIStorageConnection* GetDBConn();
158
159
/**
160
* Obtains a read-write database connection, but won't try to initialize it.
161
* May be invoked on both threads, but first one must invoke GetDBConn() on
162
* the main-thread at least once.
163
*/
164
const mozIStorageConnection* GetConstDBConn();
165
166
/**
167
* The database handle. This is initialized lazily by the first call to
168
* GetDBConn(), so never use it directly, or, if you really need, always
169
* invoke GetDBConn() before.
170
*/
171
RefPtr<mozilla::places::Database> mDB;
172
173
RefPtr<ConcurrentStatementsHolder> mConcurrentStatementsHolder;
174
175
/**
176
* Remove any memory references to tasks and do not take on any more.
177
*/
178
void Shutdown();
179
180
static History* gService;
181
182
// Ensures new tasks aren't started on destruction.
183
bool mShuttingDown;
184
// This mutex guards mShuttingDown. Code running in other threads that might
185
// schedule tasks that use the database should grab it and check the value of
186
// mShuttingDown. If we are already shutting down, the code must gracefully
187
// avoid using the db. If we are not, the lock will prevent shutdown from
188
// starting in an unexpected moment.
189
Mutex mShutdownMutex;
190
191
/**
192
* mRecentlyVisitedURIs remembers URIs which have been recently added to
193
* history, to avoid saving these locations repeatedly in a short period.
194
*/
195
struct RecentURIVisit {
196
PRTime mTime;
197
bool mHidden;
198
};
199
200
nsDataHashtable<nsURIHashKey, RecentURIVisit> mRecentlyVisitedURIs;
201
};
202
203
} // namespace places
204
} // namespace mozilla
205
206
#endif // mozilla_places_History_h_