mobile/android/components/build/nsAndroidHistory.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "nsThreadUtils.h"
michael@0 6 #include "nsAndroidHistory.h"
michael@0 7 #include "AndroidBridge.h"
michael@0 8 #include "Link.h"
michael@0 9 #include "nsIURI.h"
michael@0 10 #include "mozilla/Services.h"
michael@0 11 #include "nsIObserverService.h"
michael@0 12
michael@0 13 #define NS_LINK_VISITED_EVENT_TOPIC "link-visited"
michael@0 14
michael@0 15 using namespace mozilla;
michael@0 16 using mozilla::dom::Link;
michael@0 17
michael@0 18 NS_IMPL_ISUPPORTS(nsAndroidHistory, IHistory, nsIRunnable)
michael@0 19
michael@0 20 nsAndroidHistory* nsAndroidHistory::sHistory = nullptr;
michael@0 21
michael@0 22 /*static*/
michael@0 23 nsAndroidHistory*
michael@0 24 nsAndroidHistory::GetSingleton()
michael@0 25 {
michael@0 26 if (!sHistory) {
michael@0 27 sHistory = new nsAndroidHistory();
michael@0 28 NS_ENSURE_TRUE(sHistory, nullptr);
michael@0 29 }
michael@0 30
michael@0 31 NS_ADDREF(sHistory);
michael@0 32 return sHistory;
michael@0 33 }
michael@0 34
michael@0 35 nsAndroidHistory::nsAndroidHistory()
michael@0 36 {
michael@0 37 }
michael@0 38
michael@0 39 NS_IMETHODIMP
michael@0 40 nsAndroidHistory::RegisterVisitedCallback(nsIURI *aURI, Link *aContent)
michael@0 41 {
michael@0 42 if (!aContent || !aURI)
michael@0 43 return NS_OK;
michael@0 44
michael@0 45 // Silently return if URI is something we would never add to DB.
michael@0 46 bool canAdd;
michael@0 47 nsresult rv = CanAddURI(aURI, &canAdd);
michael@0 48 NS_ENSURE_SUCCESS(rv, rv);
michael@0 49 if (!canAdd) {
michael@0 50 return NS_OK;
michael@0 51 }
michael@0 52
michael@0 53 nsAutoCString uri;
michael@0 54 rv = aURI->GetSpec(uri);
michael@0 55 if (NS_FAILED(rv)) return rv;
michael@0 56 NS_ConvertUTF8toUTF16 uriString(uri);
michael@0 57
michael@0 58 nsTArray<Link*>* list = mListeners.Get(uriString);
michael@0 59 if (! list) {
michael@0 60 list = new nsTArray<Link*>();
michael@0 61 mListeners.Put(uriString, list);
michael@0 62 }
michael@0 63 list->AppendElement(aContent);
michael@0 64
michael@0 65 if (AndroidBridge::HasEnv()) {
michael@0 66 mozilla::widget::android::GeckoAppShell::CheckURIVisited(uriString);
michael@0 67 }
michael@0 68
michael@0 69 return NS_OK;
michael@0 70 }
michael@0 71
michael@0 72 NS_IMETHODIMP
michael@0 73 nsAndroidHistory::UnregisterVisitedCallback(nsIURI *aURI, Link *aContent)
michael@0 74 {
michael@0 75 if (!aContent || !aURI)
michael@0 76 return NS_OK;
michael@0 77
michael@0 78 nsAutoCString uri;
michael@0 79 nsresult rv = aURI->GetSpec(uri);
michael@0 80 if (NS_FAILED(rv)) return rv;
michael@0 81 NS_ConvertUTF8toUTF16 uriString(uri);
michael@0 82
michael@0 83 nsTArray<Link*>* list = mListeners.Get(uriString);
michael@0 84 if (! list)
michael@0 85 return NS_OK;
michael@0 86
michael@0 87 list->RemoveElement(aContent);
michael@0 88 if (list->IsEmpty()) {
michael@0 89 mListeners.Remove(uriString);
michael@0 90 delete list;
michael@0 91 }
michael@0 92 return NS_OK;
michael@0 93 }
michael@0 94
michael@0 95 void
michael@0 96 nsAndroidHistory::AppendToRecentlyVisitedURIs(nsIURI* aURI) {
michael@0 97 if (mRecentlyVisitedURIs.Length() < RECENTLY_VISITED_URI_SIZE) {
michael@0 98 // Append a new element while the array is not full.
michael@0 99 mRecentlyVisitedURIs.AppendElement(aURI);
michael@0 100 } else {
michael@0 101 // Otherwise, replace the oldest member.
michael@0 102 mRecentlyVisitedURIsNextIndex %= RECENTLY_VISITED_URI_SIZE;
michael@0 103 mRecentlyVisitedURIs.ElementAt(mRecentlyVisitedURIsNextIndex) = aURI;
michael@0 104 mRecentlyVisitedURIsNextIndex++;
michael@0 105 }
michael@0 106 }
michael@0 107
michael@0 108 inline bool
michael@0 109 nsAndroidHistory::IsRecentlyVisitedURI(nsIURI* aURI) {
michael@0 110 bool equals = false;
michael@0 111 RecentlyVisitedArray::index_type i;
michael@0 112 RecentlyVisitedArray::size_type length = mRecentlyVisitedURIs.Length();
michael@0 113 for (i = 0; i < length && !equals; ++i) {
michael@0 114 aURI->Equals(mRecentlyVisitedURIs.ElementAt(i), &equals);
michael@0 115 }
michael@0 116 return equals;
michael@0 117 }
michael@0 118
michael@0 119 void
michael@0 120 nsAndroidHistory::AppendToEmbedURIs(nsIURI* aURI) {
michael@0 121 if (mEmbedURIs.Length() < EMBED_URI_SIZE) {
michael@0 122 // Append a new element while the array is not full.
michael@0 123 mEmbedURIs.AppendElement(aURI);
michael@0 124 } else {
michael@0 125 // Otherwise, replace the oldest member.
michael@0 126 mEmbedURIsNextIndex %= EMBED_URI_SIZE;
michael@0 127 mEmbedURIs.ElementAt(mEmbedURIsNextIndex) = aURI;
michael@0 128 mEmbedURIsNextIndex++;
michael@0 129 }
michael@0 130 }
michael@0 131
michael@0 132 inline bool
michael@0 133 nsAndroidHistory::IsEmbedURI(nsIURI* aURI) {
michael@0 134 bool equals = false;
michael@0 135 EmbedArray::index_type i;
michael@0 136 EmbedArray::size_type length = mEmbedURIs.Length();
michael@0 137 for (i = 0; i < length && !equals; ++i) {
michael@0 138 aURI->Equals(mEmbedURIs.ElementAt(i), &equals);
michael@0 139 }
michael@0 140 return equals;
michael@0 141 }
michael@0 142
michael@0 143 NS_IMETHODIMP
michael@0 144 nsAndroidHistory::VisitURI(nsIURI *aURI, nsIURI *aLastVisitedURI, uint32_t aFlags)
michael@0 145 {
michael@0 146 if (!aURI)
michael@0 147 return NS_OK;
michael@0 148
michael@0 149 // Silently return if URI is something we shouldn't add to DB.
michael@0 150 bool canAdd;
michael@0 151 nsresult rv = CanAddURI(aURI, &canAdd);
michael@0 152 NS_ENSURE_SUCCESS(rv, rv);
michael@0 153 if (!canAdd) {
michael@0 154 return NS_OK;
michael@0 155 }
michael@0 156
michael@0 157 if (aLastVisitedURI) {
michael@0 158 bool same;
michael@0 159 rv = aURI->Equals(aLastVisitedURI, &same);
michael@0 160 NS_ENSURE_SUCCESS(rv, rv);
michael@0 161 if (same && IsRecentlyVisitedURI(aURI)) {
michael@0 162 // Do not save refresh visits if we have visited this URI recently.
michael@0 163 return NS_OK;
michael@0 164 }
michael@0 165 }
michael@0 166
michael@0 167 if (!(aFlags & VisitFlags::TOP_LEVEL)) {
michael@0 168 AppendToEmbedURIs(aURI);
michael@0 169 return NS_OK;
michael@0 170 }
michael@0 171
michael@0 172 if (aFlags & VisitFlags::REDIRECT_SOURCE)
michael@0 173 return NS_OK;
michael@0 174
michael@0 175 if (aFlags & VisitFlags::UNRECOVERABLE_ERROR)
michael@0 176 return NS_OK;
michael@0 177
michael@0 178 if (AndroidBridge::HasEnv()) {
michael@0 179 nsAutoCString uri;
michael@0 180 rv = aURI->GetSpec(uri);
michael@0 181 if (NS_FAILED(rv)) return rv;
michael@0 182 NS_ConvertUTF8toUTF16 uriString(uri);
michael@0 183 mozilla::widget::android::GeckoAppShell::MarkURIVisited(uriString);
michael@0 184 }
michael@0 185
michael@0 186 AppendToRecentlyVisitedURIs(aURI);
michael@0 187
michael@0 188 // Finally, notify that we've been visited.
michael@0 189 nsCOMPtr<nsIObserverService> obsService =
michael@0 190 mozilla::services::GetObserverService();
michael@0 191 if (obsService) {
michael@0 192 obsService->NotifyObservers(aURI, NS_LINK_VISITED_EVENT_TOPIC, nullptr);
michael@0 193 }
michael@0 194
michael@0 195 return NS_OK;
michael@0 196 }
michael@0 197
michael@0 198 NS_IMETHODIMP
michael@0 199 nsAndroidHistory::SetURITitle(nsIURI *aURI, const nsAString& aTitle)
michael@0 200 {
michael@0 201 // Silently return if URI is something we shouldn't add to DB.
michael@0 202 bool canAdd;
michael@0 203 nsresult rv = CanAddURI(aURI, &canAdd);
michael@0 204 NS_ENSURE_SUCCESS(rv, rv);
michael@0 205 if (!canAdd) {
michael@0 206 return NS_OK;
michael@0 207 }
michael@0 208
michael@0 209 if (IsEmbedURI(aURI)) {
michael@0 210 return NS_OK;
michael@0 211 }
michael@0 212
michael@0 213 if (AndroidBridge::HasEnv()) {
michael@0 214 nsAutoCString uri;
michael@0 215 nsresult rv = aURI->GetSpec(uri);
michael@0 216 if (NS_FAILED(rv)) return rv;
michael@0 217 NS_ConvertUTF8toUTF16 uriString(uri);
michael@0 218 mozilla::widget::android::GeckoAppShell::SetURITitle(uriString, aTitle);
michael@0 219 }
michael@0 220 return NS_OK;
michael@0 221 }
michael@0 222
michael@0 223 NS_IMETHODIMP
michael@0 224 nsAndroidHistory::NotifyVisited(nsIURI *aURI)
michael@0 225 {
michael@0 226 if (aURI && sHistory) {
michael@0 227 nsAutoCString spec;
michael@0 228 (void)aURI->GetSpec(spec);
michael@0 229 sHistory->mPendingURIs.Push(NS_ConvertUTF8toUTF16(spec));
michael@0 230 NS_DispatchToMainThread(sHistory);
michael@0 231 }
michael@0 232 return NS_OK;
michael@0 233 }
michael@0 234
michael@0 235 NS_IMETHODIMP
michael@0 236 nsAndroidHistory::Run()
michael@0 237 {
michael@0 238 while (! mPendingURIs.IsEmpty()) {
michael@0 239 nsString uriString = mPendingURIs.Pop();
michael@0 240 nsTArray<Link*>* list = sHistory->mListeners.Get(uriString);
michael@0 241 if (list) {
michael@0 242 for (unsigned int i = 0; i < list->Length(); i++) {
michael@0 243 list->ElementAt(i)->SetLinkState(eLinkState_Visited);
michael@0 244 }
michael@0 245 // as per the IHistory interface contract, remove the
michael@0 246 // Link pointers once they have been notified
michael@0 247 mListeners.Remove(uriString);
michael@0 248 delete list;
michael@0 249 }
michael@0 250 }
michael@0 251 return NS_OK;
michael@0 252 }
michael@0 253
michael@0 254 // Filter out unwanted URIs such as "chrome:", "mailbox:", etc.
michael@0 255 //
michael@0 256 // The model is if we don't know differently then add which basically means
michael@0 257 // we are suppose to try all the things we know not to allow in and then if
michael@0 258 // we don't bail go on and allow it in.
michael@0 259 //
michael@0 260 // Logic ported from nsNavHistory::CanAddURI.
michael@0 261
michael@0 262 NS_IMETHODIMP
michael@0 263 nsAndroidHistory::CanAddURI(nsIURI* aURI, bool* canAdd)
michael@0 264 {
michael@0 265 NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread");
michael@0 266 NS_ENSURE_ARG(aURI);
michael@0 267 NS_ENSURE_ARG_POINTER(canAdd);
michael@0 268
michael@0 269 nsAutoCString scheme;
michael@0 270 nsresult rv = aURI->GetScheme(scheme);
michael@0 271 NS_ENSURE_SUCCESS(rv, rv);
michael@0 272
michael@0 273 // first check the most common cases (HTTP, HTTPS) to allow in to avoid most
michael@0 274 // of the work
michael@0 275 if (scheme.EqualsLiteral("http")) {
michael@0 276 *canAdd = true;
michael@0 277 return NS_OK;
michael@0 278 }
michael@0 279 if (scheme.EqualsLiteral("https")) {
michael@0 280 *canAdd = true;
michael@0 281 return NS_OK;
michael@0 282 }
michael@0 283
michael@0 284 // now check for all bad things
michael@0 285 if (scheme.EqualsLiteral("about") ||
michael@0 286 scheme.EqualsLiteral("imap") ||
michael@0 287 scheme.EqualsLiteral("news") ||
michael@0 288 scheme.EqualsLiteral("mailbox") ||
michael@0 289 scheme.EqualsLiteral("moz-anno") ||
michael@0 290 scheme.EqualsLiteral("view-source") ||
michael@0 291 scheme.EqualsLiteral("chrome") ||
michael@0 292 scheme.EqualsLiteral("resource") ||
michael@0 293 scheme.EqualsLiteral("data") ||
michael@0 294 scheme.EqualsLiteral("wyciwyg") ||
michael@0 295 scheme.EqualsLiteral("javascript") ||
michael@0 296 scheme.EqualsLiteral("blob")) {
michael@0 297 *canAdd = false;
michael@0 298 return NS_OK;
michael@0 299 }
michael@0 300 *canAdd = true;
michael@0 301 return NS_OK;
michael@0 302 }

mercurial