toolkit/components/places/AsyncFaviconHelpers.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/AsyncFaviconHelpers.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1073 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "AsyncFaviconHelpers.h"
    1.11 +
    1.12 +#include "nsICacheService.h"
    1.13 +#include "nsICacheEntry.h"
    1.14 +#include "nsICachingChannel.h"
    1.15 +#include "nsIAsyncVerifyRedirectCallback.h"
    1.16 +
    1.17 +#include "nsNavHistory.h"
    1.18 +#include "nsFaviconService.h"
    1.19 +#include "mozilla/storage.h"
    1.20 +#include "nsNetUtil.h"
    1.21 +#include "nsPrintfCString.h"
    1.22 +#include "nsStreamUtils.h"
    1.23 +#include "nsIPrivateBrowsingChannel.h"
    1.24 +#include "nsISupportsPriority.h"
    1.25 +#include <algorithm>
    1.26 +
    1.27 +using namespace mozilla::places;
    1.28 +using namespace mozilla::storage;
    1.29 +
    1.30 +namespace mozilla {
    1.31 +namespace places {
    1.32 +
    1.33 +namespace {
    1.34 +
    1.35 +/**
    1.36 + * Fetches information on a page from the Places database.
    1.37 + *
    1.38 + * @param aDBConn
    1.39 + *        Database connection to history tables.
    1.40 + * @param _page
    1.41 + *        Page that should be fetched.
    1.42 + */
    1.43 +nsresult
    1.44 +FetchPageInfo(nsRefPtr<Database>& aDB,
    1.45 +              PageData& _page)
    1.46 +{
    1.47 +  NS_PRECONDITION(_page.spec.Length(), "Must have a non-empty spec!");
    1.48 +  NS_PRECONDITION(!NS_IsMainThread(),
    1.49 +                  "This should not be called on the main thread");
    1.50 +
    1.51 +  // This query finds the bookmarked uri we want to set the icon for,
    1.52 +  // walking up to two redirect levels.
    1.53 +  nsCString query = nsPrintfCString(
    1.54 +    "SELECT h.id, h.favicon_id, h.guid, ( "
    1.55 +      "SELECT h.url FROM moz_bookmarks b WHERE b.fk = h.id "
    1.56 +      "UNION ALL " // Union not directly bookmarked pages.
    1.57 +      "SELECT url FROM moz_places WHERE id = ( "
    1.58 +        "SELECT COALESCE(grandparent.place_id, parent.place_id) as r_place_id "
    1.59 +        "FROM moz_historyvisits dest "
    1.60 +        "LEFT JOIN moz_historyvisits parent ON parent.id = dest.from_visit "
    1.61 +                                          "AND dest.visit_type IN (%d, %d) "
    1.62 +        "LEFT JOIN moz_historyvisits grandparent ON parent.from_visit = grandparent.id "
    1.63 +          "AND parent.visit_type IN (%d, %d) "
    1.64 +        "WHERE dest.place_id = h.id "
    1.65 +        "AND EXISTS(SELECT 1 FROM moz_bookmarks b WHERE b.fk = r_place_id) "
    1.66 +        "LIMIT 1 "
    1.67 +      ") "
    1.68 +    ") FROM moz_places h WHERE h.url = :page_url",
    1.69 +    nsINavHistoryService::TRANSITION_REDIRECT_PERMANENT,
    1.70 +    nsINavHistoryService::TRANSITION_REDIRECT_TEMPORARY,
    1.71 +    nsINavHistoryService::TRANSITION_REDIRECT_PERMANENT,
    1.72 +    nsINavHistoryService::TRANSITION_REDIRECT_TEMPORARY
    1.73 +  );
    1.74 +
    1.75 +  nsCOMPtr<mozIStorageStatement> stmt = aDB->GetStatement(query);
    1.76 +  NS_ENSURE_STATE(stmt);
    1.77 +  mozStorageStatementScoper scoper(stmt);
    1.78 +
    1.79 +  nsresult rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("page_url"),
    1.80 +                                _page.spec);
    1.81 +  NS_ENSURE_SUCCESS(rv, rv);
    1.82 +
    1.83 +  bool hasResult;
    1.84 +  rv = stmt->ExecuteStep(&hasResult);
    1.85 +  NS_ENSURE_SUCCESS(rv, rv);
    1.86 +  if (!hasResult) {
    1.87 +    // The page does not exist.
    1.88 +    return NS_ERROR_NOT_AVAILABLE;
    1.89 +  }
    1.90 +
    1.91 +  rv = stmt->GetInt64(0, &_page.id);
    1.92 +  NS_ENSURE_SUCCESS(rv, rv);
    1.93 +  bool isNull;
    1.94 +  rv = stmt->GetIsNull(1, &isNull);
    1.95 +  NS_ENSURE_SUCCESS(rv, rv);
    1.96 +  // favicon_id can be nullptr.
    1.97 +  if (!isNull) {
    1.98 +    rv = stmt->GetInt64(1, &_page.iconId);
    1.99 +    NS_ENSURE_SUCCESS(rv, rv);
   1.100 +  }
   1.101 +  rv = stmt->GetUTF8String(2, _page.guid);
   1.102 +  NS_ENSURE_SUCCESS(rv, rv);
   1.103 +  rv = stmt->GetIsNull(3, &isNull);
   1.104 +  NS_ENSURE_SUCCESS(rv, rv);
   1.105 +  // The page could not be bookmarked.
   1.106 +  if (!isNull) {
   1.107 +    rv = stmt->GetUTF8String(3, _page.bookmarkedSpec);
   1.108 +    NS_ENSURE_SUCCESS(rv, rv);
   1.109 +  }
   1.110 +
   1.111 +  if (!_page.canAddToHistory) {
   1.112 +    // Either history is disabled or the scheme is not supported.  In such a
   1.113 +    // case we want to update the icon only if the page is bookmarked.
   1.114 +
   1.115 +    if (_page.bookmarkedSpec.IsEmpty()) {
   1.116 +      // The page is not bookmarked.  Since updating the icon with a disabled
   1.117 +      // history would be a privacy leak, bail out as if the page did not exist.
   1.118 +      return NS_ERROR_NOT_AVAILABLE;
   1.119 +    }
   1.120 +    else {
   1.121 +      // The page, or a redirect to it, is bookmarked.  If the bookmarked spec
   1.122 +      // is different from the requested one, use it.
   1.123 +      if (!_page.bookmarkedSpec.Equals(_page.spec)) {
   1.124 +        _page.spec = _page.bookmarkedSpec;
   1.125 +        rv = FetchPageInfo(aDB, _page);
   1.126 +        NS_ENSURE_SUCCESS(rv, rv);
   1.127 +      }
   1.128 +    }
   1.129 +  }
   1.130 +
   1.131 +  return NS_OK;
   1.132 +}
   1.133 +
   1.134 +/**
   1.135 + * Stores information on a icon in the database.
   1.136 + *
   1.137 + * @param aDBConn
   1.138 + *        Database connection to history tables.
   1.139 + * @param aIcon
   1.140 + *        Icon that should be stored.
   1.141 + */
   1.142 +nsresult
   1.143 +SetIconInfo(nsRefPtr<Database>& aDB,
   1.144 +            IconData& aIcon)
   1.145 +{
   1.146 +  NS_PRECONDITION(!NS_IsMainThread(),
   1.147 +                  "This should not be called on the main thread");
   1.148 +
   1.149 +  // The 'multi-coalesce' here ensures that replacing a favicon without
   1.150 +  // specifying a :guid parameter doesn't cause it to be allocated a new
   1.151 +  // GUID.
   1.152 +  nsCOMPtr<mozIStorageStatement> stmt = aDB->GetStatement(
   1.153 +    "INSERT OR REPLACE INTO moz_favicons "
   1.154 +      "(id, url, data, mime_type, expiration, guid) "
   1.155 +    "VALUES ((SELECT id FROM moz_favicons WHERE url = :icon_url), "
   1.156 +            ":icon_url, :data, :mime_type, :expiration, "
   1.157 +            "COALESCE(:guid, "
   1.158 +                     "(SELECT guid FROM moz_favicons "
   1.159 +                      "WHERE url = :icon_url), "
   1.160 +                     "GENERATE_GUID()))"
   1.161 +  );
   1.162 +  NS_ENSURE_STATE(stmt);
   1.163 +  mozStorageStatementScoper scoper(stmt);
   1.164 +  nsresult rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("icon_url"), aIcon.spec);
   1.165 +  NS_ENSURE_SUCCESS(rv, rv);
   1.166 +  rv = stmt->BindBlobByName(NS_LITERAL_CSTRING("data"),
   1.167 +                            TO_INTBUFFER(aIcon.data), aIcon.data.Length());
   1.168 +  NS_ENSURE_SUCCESS(rv, rv);
   1.169 +  rv = stmt->BindUTF8StringByName(NS_LITERAL_CSTRING("mime_type"), aIcon.mimeType);
   1.170 +  NS_ENSURE_SUCCESS(rv, rv);
   1.171 +  rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("expiration"), aIcon.expiration);
   1.172 +  NS_ENSURE_SUCCESS(rv, rv);
   1.173 +
   1.174 +  // Binding a GUID allows us to override the current (or generated) GUID.
   1.175 +  if (aIcon.guid.IsEmpty()) {
   1.176 +    rv = stmt->BindNullByName(NS_LITERAL_CSTRING("guid"));
   1.177 +  }
   1.178 +  else {
   1.179 +    rv = stmt->BindUTF8StringByName(NS_LITERAL_CSTRING("guid"), aIcon.guid);
   1.180 +  }
   1.181 +  NS_ENSURE_SUCCESS(rv, rv);
   1.182 +
   1.183 +  rv = stmt->Execute();
   1.184 +  NS_ENSURE_SUCCESS(rv, rv);
   1.185 +
   1.186 +  return NS_OK;
   1.187 +}
   1.188 +
   1.189 +/**
   1.190 + * Fetches information on a icon from the Places database.
   1.191 + *
   1.192 + * @param aDBConn
   1.193 + *        Database connection to history tables.
   1.194 + * @param _icon
   1.195 + *        Icon that should be fetched.
   1.196 + */
   1.197 +nsresult
   1.198 +FetchIconInfo(nsRefPtr<Database>& aDB,
   1.199 +              IconData& _icon)
   1.200 +{
   1.201 +  NS_PRECONDITION(_icon.spec.Length(), "Must have a non-empty spec!");
   1.202 +  NS_PRECONDITION(!NS_IsMainThread(),
   1.203 +                  "This should not be called on the main thread");
   1.204 +
   1.205 +  if (_icon.status & ICON_STATUS_CACHED) {
   1.206 +    return NS_OK;
   1.207 +  }
   1.208 +
   1.209 +  nsCOMPtr<mozIStorageStatement> stmt = aDB->GetStatement(
   1.210 +    "SELECT id, expiration, data, mime_type "
   1.211 +    "FROM moz_favicons WHERE url = :icon_url"
   1.212 +  );
   1.213 +  NS_ENSURE_STATE(stmt);
   1.214 +  mozStorageStatementScoper scoper(stmt);
   1.215 +
   1.216 +  DebugOnly<nsresult> rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("icon_url"),
   1.217 +                                           _icon.spec);
   1.218 +  MOZ_ASSERT(NS_SUCCEEDED(rv));
   1.219 +
   1.220 +  bool hasResult;
   1.221 +  rv = stmt->ExecuteStep(&hasResult);
   1.222 +  MOZ_ASSERT(NS_SUCCEEDED(rv));
   1.223 +  if (!hasResult) {
   1.224 +    // The icon does not exist yet, bail out.
   1.225 +    return NS_OK;
   1.226 +  }
   1.227 +
   1.228 +  rv = stmt->GetInt64(0, &_icon.id);
   1.229 +  MOZ_ASSERT(NS_SUCCEEDED(rv));
   1.230 +
   1.231 +  // Expiration can be nullptr.
   1.232 +  bool isNull;
   1.233 +  rv = stmt->GetIsNull(1, &isNull);
   1.234 +  MOZ_ASSERT(NS_SUCCEEDED(rv));
   1.235 +  if (!isNull) {
   1.236 +    rv = stmt->GetInt64(1, reinterpret_cast<int64_t*>(&_icon.expiration));
   1.237 +    MOZ_ASSERT(NS_SUCCEEDED(rv));
   1.238 +  }
   1.239 +
   1.240 +  // Data can be nullptr.
   1.241 +  rv = stmt->GetIsNull(2, &isNull);
   1.242 +  MOZ_ASSERT(NS_SUCCEEDED(rv));
   1.243 +  if (!isNull) {
   1.244 +    uint8_t* data;
   1.245 +    uint32_t dataLen = 0;
   1.246 +    rv = stmt->GetBlob(2, &dataLen, &data);
   1.247 +    MOZ_ASSERT(NS_SUCCEEDED(rv));
   1.248 +    _icon.data.Adopt(TO_CHARBUFFER(data), dataLen);
   1.249 +    // Read mime only if we have data.
   1.250 +    rv = stmt->GetUTF8String(3, _icon.mimeType);
   1.251 +    MOZ_ASSERT(NS_SUCCEEDED(rv));
   1.252 +  }
   1.253 +
   1.254 +  return NS_OK;
   1.255 +}
   1.256 +
   1.257 +nsresult
   1.258 +FetchIconURL(nsRefPtr<Database>& aDB,
   1.259 +             const nsACString& aPageSpec,
   1.260 +             nsACString& aIconSpec)
   1.261 +{
   1.262 +  NS_PRECONDITION(!aPageSpec.IsEmpty(), "Page spec must not be empty.");
   1.263 +  NS_PRECONDITION(!NS_IsMainThread(),
   1.264 +                  "This should not be called on the main thread.");
   1.265 +
   1.266 +  aIconSpec.Truncate();
   1.267 +
   1.268 +  nsCOMPtr<mozIStorageStatement> stmt = aDB->GetStatement(
   1.269 +    "SELECT f.url "
   1.270 +    "FROM moz_places h "
   1.271 +    "JOIN moz_favicons f ON h.favicon_id = f.id "
   1.272 +    "WHERE h.url = :page_url"
   1.273 +  );
   1.274 +  NS_ENSURE_STATE(stmt);
   1.275 +  mozStorageStatementScoper scoper(stmt);
   1.276 +
   1.277 +  nsresult rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("page_url"),
   1.278 +                                aPageSpec);
   1.279 +  NS_ENSURE_SUCCESS(rv, rv);
   1.280 +
   1.281 +  bool hasResult;
   1.282 +  if (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
   1.283 +    rv = stmt->GetUTF8String(0, aIconSpec);
   1.284 +    NS_ENSURE_SUCCESS(rv, rv);
   1.285 +  }
   1.286 +
   1.287 +  return NS_OK;
   1.288 +}
   1.289 +
   1.290 +/**
   1.291 + * Tries to compute the expiration time for a icon from the channel.
   1.292 + *
   1.293 + * @param aChannel
   1.294 + *        The network channel used to fetch the icon.
   1.295 + * @return a valid expiration value for the fetched icon.
   1.296 + */
   1.297 +PRTime
   1.298 +GetExpirationTimeFromChannel(nsIChannel* aChannel)
   1.299 +{
   1.300 +  NS_PRECONDITION(NS_IsMainThread(),
   1.301 +                  "This should be called on the main thread");
   1.302 +
   1.303 +  // Attempt to get an expiration time from the cache.  If this fails, we'll
   1.304 +  // make one up.
   1.305 +  PRTime expiration = -1;
   1.306 +  nsCOMPtr<nsICachingChannel> cachingChannel = do_QueryInterface(aChannel);
   1.307 +  if (cachingChannel) {
   1.308 +    nsCOMPtr<nsISupports> cacheToken;
   1.309 +    nsresult rv = cachingChannel->GetCacheToken(getter_AddRefs(cacheToken));
   1.310 +    if (NS_SUCCEEDED(rv)) {
   1.311 +      nsCOMPtr<nsICacheEntry> cacheEntry = do_QueryInterface(cacheToken);
   1.312 +      uint32_t seconds;
   1.313 +      rv = cacheEntry->GetExpirationTime(&seconds);
   1.314 +      if (NS_SUCCEEDED(rv)) {
   1.315 +        // Set the expiration, but make sure we honor our cap.
   1.316 +        expiration = PR_Now() + std::min((PRTime)seconds * PR_USEC_PER_SEC,
   1.317 +                                       MAX_FAVICON_EXPIRATION);
   1.318 +      }
   1.319 +    }
   1.320 +  }
   1.321 +  // If we did not obtain a time from the cache, use the cap value.
   1.322 +  return expiration < 0 ? PR_Now() + MAX_FAVICON_EXPIRATION
   1.323 +                        : expiration;
   1.324 +}
   1.325 +
   1.326 +/**
   1.327 + * Checks the icon and evaluates if it needs to be optimized.  In such a case it
   1.328 + * will try to reduce its size through OptimizeFaviconImage method of the
   1.329 + * favicons service.
   1.330 + *
   1.331 + * @param aIcon
   1.332 + *        The icon to be evaluated.
   1.333 + * @param aFaviconSvc
   1.334 + *        Pointer to the favicons service.
   1.335 + */
   1.336 +nsresult
   1.337 +OptimizeIconSize(IconData& aIcon,
   1.338 +                 nsFaviconService* aFaviconSvc)
   1.339 +{
   1.340 +  NS_PRECONDITION(NS_IsMainThread(),
   1.341 +                  "This should be called on the main thread");
   1.342 +
   1.343 +  // Even if the page provides a large image for the favicon (eg, a highres
   1.344 +  // image or a multiresolution .ico file), don't try to store more data than
   1.345 +  // needed.
   1.346 +  nsAutoCString newData, newMimeType;
   1.347 +  if (aIcon.data.Length() > MAX_ICON_FILESIZE(aFaviconSvc->GetOptimizedIconDimension())) {
   1.348 +    nsresult rv = aFaviconSvc->OptimizeFaviconImage(TO_INTBUFFER(aIcon.data),
   1.349 +                                                    aIcon.data.Length(),
   1.350 +                                                    aIcon.mimeType,
   1.351 +                                                    newData,
   1.352 +                                                    newMimeType);
   1.353 +    if (NS_SUCCEEDED(rv) && newData.Length() < aIcon.data.Length()) {
   1.354 +      aIcon.data = newData;
   1.355 +      aIcon.mimeType = newMimeType;
   1.356 +    }
   1.357 +  }
   1.358 +  return NS_OK;
   1.359 +}
   1.360 +
   1.361 +} // Anonymous namespace.
   1.362 +
   1.363 +
   1.364 +////////////////////////////////////////////////////////////////////////////////
   1.365 +//// AsyncFaviconHelperBase
   1.366 +
   1.367 +AsyncFaviconHelperBase::AsyncFaviconHelperBase(
   1.368 +  nsCOMPtr<nsIFaviconDataCallback>& aCallback
   1.369 +) : mDB(Database::GetDatabase())
   1.370 +{
   1.371 +  // Don't AddRef or Release in runnables for thread-safety.
   1.372 +  mCallback.swap(aCallback);
   1.373 +}
   1.374 +
   1.375 +AsyncFaviconHelperBase::~AsyncFaviconHelperBase()
   1.376 +{
   1.377 +  nsCOMPtr<nsIThread> thread;
   1.378 +  (void)NS_GetMainThread(getter_AddRefs(thread));
   1.379 +  if (mCallback) {
   1.380 +    (void)NS_ProxyRelease(thread, mCallback, true);
   1.381 +  }
   1.382 +}
   1.383 +
   1.384 +////////////////////////////////////////////////////////////////////////////////
   1.385 +//// AsyncFetchAndSetIconForPage
   1.386 +
   1.387 +// static
   1.388 +nsresult
   1.389 +AsyncFetchAndSetIconForPage::start(nsIURI* aFaviconURI,
   1.390 +                                   nsIURI* aPageURI,
   1.391 +                                   enum AsyncFaviconFetchMode aFetchMode,
   1.392 +                                   uint32_t aFaviconLoadType,
   1.393 +                                   nsIFaviconDataCallback* aCallback)
   1.394 +{
   1.395 +  NS_PRECONDITION(NS_IsMainThread(),
   1.396 +                  "This should be called on the main thread");
   1.397 +
   1.398 +  PageData page;
   1.399 +  nsresult rv = aPageURI->GetSpec(page.spec);
   1.400 +  NS_ENSURE_SUCCESS(rv, rv);
   1.401 +  // URIs can arguably miss a host.
   1.402 +  (void)GetReversedHostname(aPageURI, page.revHost);
   1.403 +  bool canAddToHistory;
   1.404 +  nsNavHistory* navHistory = nsNavHistory::GetHistoryService();
   1.405 +  NS_ENSURE_TRUE(navHistory, NS_ERROR_OUT_OF_MEMORY);
   1.406 +  rv = navHistory->CanAddURI(aPageURI, &canAddToHistory);
   1.407 +  NS_ENSURE_SUCCESS(rv, rv);
   1.408 +  page.canAddToHistory = !!canAddToHistory && aFaviconLoadType != nsIFaviconService::FAVICON_LOAD_PRIVATE;
   1.409 +
   1.410 +  IconData icon;
   1.411 +
   1.412 +  nsFaviconService* favicons = nsFaviconService::GetFaviconService();
   1.413 +  NS_ENSURE_STATE(favicons);
   1.414 +
   1.415 +  UnassociatedIconHashKey* iconKey =
   1.416 +    favicons->mUnassociatedIcons.GetEntry(aFaviconURI);
   1.417 +
   1.418 +  if (iconKey) {
   1.419 +    icon = iconKey->iconData;
   1.420 +    favicons->mUnassociatedIcons.RemoveEntry(aFaviconURI);
   1.421 +  } else {
   1.422 +    icon.fetchMode = aFetchMode;
   1.423 +    rv = aFaviconURI->GetSpec(icon.spec);
   1.424 +    NS_ENSURE_SUCCESS(rv, rv);
   1.425 +  }
   1.426 +
   1.427 +  // If the page url points to an image, the icon's url will be the same.
   1.428 +  // In future evaluate to store a resample of the image.  For now avoid that
   1.429 +  // for database size concerns.
   1.430 +  // Don't store favicons for error pages too.
   1.431 +  if (icon.spec.Equals(page.spec) ||
   1.432 +      icon.spec.Equals(FAVICON_ERRORPAGE_URL)) {
   1.433 +    return NS_OK;
   1.434 +  }
   1.435 +
   1.436 +  // The event will swap owning pointers, thus we need a new pointer.
   1.437 +  nsCOMPtr<nsIFaviconDataCallback> callback(aCallback);
   1.438 +  nsRefPtr<AsyncFetchAndSetIconForPage> event =
   1.439 +    new AsyncFetchAndSetIconForPage(icon, page, aFaviconLoadType, callback);
   1.440 +
   1.441 +  // Get the target thread and start the work.
   1.442 +  nsRefPtr<Database> DB = Database::GetDatabase();
   1.443 +  NS_ENSURE_STATE(DB);
   1.444 +  DB->DispatchToAsyncThread(event);
   1.445 +
   1.446 +  return NS_OK;
   1.447 +}
   1.448 +
   1.449 +AsyncFetchAndSetIconForPage::AsyncFetchAndSetIconForPage(
   1.450 +  IconData& aIcon
   1.451 +, PageData& aPage
   1.452 +, uint32_t aFaviconLoadType
   1.453 +, nsCOMPtr<nsIFaviconDataCallback>& aCallback
   1.454 +) : AsyncFaviconHelperBase(aCallback)
   1.455 +  , mIcon(aIcon)
   1.456 +  , mPage(aPage)
   1.457 +  , mFaviconLoadPrivate(aFaviconLoadType == nsIFaviconService::FAVICON_LOAD_PRIVATE)
   1.458 +{
   1.459 +}
   1.460 +
   1.461 +AsyncFetchAndSetIconForPage::~AsyncFetchAndSetIconForPage()
   1.462 +{
   1.463 +}
   1.464 +
   1.465 +NS_IMETHODIMP
   1.466 +AsyncFetchAndSetIconForPage::Run()
   1.467 +{
   1.468 +  NS_PRECONDITION(!NS_IsMainThread(),
   1.469 +                  "This should not be called on the main thread");
   1.470 +
   1.471 +  // Try to fetch the icon from the database.
   1.472 +  nsresult rv = FetchIconInfo(mDB, mIcon);
   1.473 +  NS_ENSURE_SUCCESS(rv, rv);
   1.474 +
   1.475 +  bool isInvalidIcon = mIcon.data.IsEmpty() ||
   1.476 +                       (mIcon.expiration && PR_Now() > mIcon.expiration);
   1.477 +  bool fetchIconFromNetwork = mIcon.fetchMode == FETCH_ALWAYS ||
   1.478 +                              (mIcon.fetchMode == FETCH_IF_MISSING && isInvalidIcon);
   1.479 +
   1.480 +  if (!fetchIconFromNetwork) {
   1.481 +    // There is already a valid icon or we don't want to fetch a new one,
   1.482 +    // directly proceed with association.
   1.483 +    nsRefPtr<AsyncAssociateIconToPage> event =
   1.484 +        new AsyncAssociateIconToPage(mIcon, mPage, mCallback);
   1.485 +    mDB->DispatchToAsyncThread(event);
   1.486 +
   1.487 +    return NS_OK;
   1.488 +  }
   1.489 +  else {
   1.490 +    // Fetch the icon from network.  When done this will associate the
   1.491 +    // icon to the page and notify.
   1.492 +    nsRefPtr<AsyncFetchAndSetIconFromNetwork> event =
   1.493 +      new AsyncFetchAndSetIconFromNetwork(mIcon, mPage, mFaviconLoadPrivate, mCallback);
   1.494 +
   1.495 +    // Start the work on the main thread.
   1.496 +    rv = NS_DispatchToMainThread(event);
   1.497 +    NS_ENSURE_SUCCESS(rv, rv);
   1.498 +  }
   1.499 +
   1.500 +  return NS_OK;
   1.501 +}
   1.502 +
   1.503 +////////////////////////////////////////////////////////////////////////////////
   1.504 +//// AsyncFetchAndSetIconFromNetwork
   1.505 +
   1.506 +NS_IMPL_ISUPPORTS_INHERITED(
   1.507 +  AsyncFetchAndSetIconFromNetwork
   1.508 +, nsRunnable
   1.509 +, nsIStreamListener
   1.510 +, nsIInterfaceRequestor
   1.511 +, nsIChannelEventSink
   1.512 +)
   1.513 +
   1.514 +AsyncFetchAndSetIconFromNetwork::AsyncFetchAndSetIconFromNetwork(
   1.515 +  IconData& aIcon
   1.516 +, PageData& aPage
   1.517 +, bool aFaviconLoadPrivate
   1.518 +, nsCOMPtr<nsIFaviconDataCallback>& aCallback
   1.519 +)
   1.520 +: AsyncFaviconHelperBase(aCallback)
   1.521 +, mIcon(aIcon)
   1.522 +, mPage(aPage)
   1.523 +, mFaviconLoadPrivate(aFaviconLoadPrivate)
   1.524 +{
   1.525 +}
   1.526 +
   1.527 +AsyncFetchAndSetIconFromNetwork::~AsyncFetchAndSetIconFromNetwork()
   1.528 +{
   1.529 +}
   1.530 +
   1.531 +NS_IMETHODIMP
   1.532 +AsyncFetchAndSetIconFromNetwork::Run()
   1.533 +{
   1.534 +  NS_PRECONDITION(NS_IsMainThread(),
   1.535 +                  "This should be called on the main thread");
   1.536 +
   1.537 +  // Ensure data is cleared, since it's going to be overwritten.
   1.538 +  if (mIcon.data.Length() > 0) {
   1.539 +    mIcon.data.Truncate(0);
   1.540 +    mIcon.mimeType.Truncate(0);
   1.541 +  }
   1.542 +
   1.543 +  nsCOMPtr<nsIURI> iconURI;
   1.544 +  nsresult rv = NS_NewURI(getter_AddRefs(iconURI), mIcon.spec);
   1.545 +  NS_ENSURE_SUCCESS(rv, rv);
   1.546 +  nsCOMPtr<nsIChannel> channel;
   1.547 +  rv = NS_NewChannel(getter_AddRefs(channel), iconURI);
   1.548 +  NS_ENSURE_SUCCESS(rv, rv);
   1.549 +  nsCOMPtr<nsIInterfaceRequestor> listenerRequestor =
   1.550 +    do_QueryInterface(reinterpret_cast<nsISupports*>(this));
   1.551 +  NS_ENSURE_STATE(listenerRequestor);
   1.552 +  rv = channel->SetNotificationCallbacks(listenerRequestor);
   1.553 +  NS_ENSURE_SUCCESS(rv, rv);
   1.554 +  nsCOMPtr<nsIPrivateBrowsingChannel> pbChannel = do_QueryInterface(channel);
   1.555 +  if (pbChannel) {
   1.556 +    rv = pbChannel->SetPrivate(mFaviconLoadPrivate);
   1.557 +    NS_ENSURE_SUCCESS(rv, rv);
   1.558 +  }
   1.559 +
   1.560 +  nsCOMPtr<nsISupportsPriority> priorityChannel = do_QueryInterface(channel);
   1.561 +  if (priorityChannel) {
   1.562 +    priorityChannel->AdjustPriority(nsISupportsPriority::PRIORITY_LOWEST);
   1.563 +  }
   1.564 +
   1.565 +  return channel->AsyncOpen(this, nullptr);
   1.566 +}
   1.567 +
   1.568 +NS_IMETHODIMP
   1.569 +AsyncFetchAndSetIconFromNetwork::OnStartRequest(nsIRequest* aRequest,
   1.570 +                                                nsISupports* aContext)
   1.571 +{
   1.572 +  return NS_OK;
   1.573 +}
   1.574 +
   1.575 +NS_IMETHODIMP
   1.576 +AsyncFetchAndSetIconFromNetwork::OnDataAvailable(nsIRequest* aRequest,
   1.577 +                                                 nsISupports* aContext,
   1.578 +                                                 nsIInputStream* aInputStream,
   1.579 +                                                 uint64_t aOffset,
   1.580 +                                                 uint32_t aCount)
   1.581 +{
   1.582 +  nsAutoCString buffer;
   1.583 +  nsresult rv = NS_ConsumeStream(aInputStream, aCount, buffer);
   1.584 +  if (rv != NS_BASE_STREAM_WOULD_BLOCK && NS_FAILED(rv)) {
   1.585 +    return rv;
   1.586 +  }
   1.587 +
   1.588 +  mIcon.data.Append(buffer);
   1.589 +  return NS_OK;
   1.590 +}
   1.591 +
   1.592 +
   1.593 +NS_IMETHODIMP
   1.594 +AsyncFetchAndSetIconFromNetwork::GetInterface(const nsIID& uuid,
   1.595 +                                              void** aResult)
   1.596 +{
   1.597 +  return QueryInterface(uuid, aResult);
   1.598 +}
   1.599 +
   1.600 +
   1.601 +NS_IMETHODIMP
   1.602 +AsyncFetchAndSetIconFromNetwork::AsyncOnChannelRedirect(
   1.603 +  nsIChannel* oldChannel
   1.604 +, nsIChannel* newChannel
   1.605 +, uint32_t flags
   1.606 +, nsIAsyncVerifyRedirectCallback *cb
   1.607 +)
   1.608 +{
   1.609 +  (void)cb->OnRedirectVerifyCallback(NS_OK);
   1.610 +  return NS_OK;
   1.611 +}
   1.612 +
   1.613 +NS_IMETHODIMP
   1.614 +AsyncFetchAndSetIconFromNetwork::OnStopRequest(nsIRequest* aRequest,
   1.615 +                                               nsISupports* aContext,
   1.616 +                                               nsresult aStatusCode)
   1.617 +{
   1.618 +  MOZ_ASSERT(NS_IsMainThread());
   1.619 +
   1.620 +  nsFaviconService* favicons = nsFaviconService::GetFaviconService();
   1.621 +  NS_ENSURE_STATE(favicons);
   1.622 +
   1.623 +  nsresult rv;
   1.624 +
   1.625 +  // If fetching the icon failed, add it to the failed cache.
   1.626 +  if (NS_FAILED(aStatusCode) || mIcon.data.Length() == 0) {
   1.627 +    nsCOMPtr<nsIURI> iconURI;
   1.628 +    rv = NS_NewURI(getter_AddRefs(iconURI), mIcon.spec);
   1.629 +    NS_ENSURE_SUCCESS(rv, rv);
   1.630 +    rv = favicons->AddFailedFavicon(iconURI);
   1.631 +    NS_ENSURE_SUCCESS(rv, rv);
   1.632 +    return NS_OK;
   1.633 +  }
   1.634 +
   1.635 +  NS_SniffContent(NS_DATA_SNIFFER_CATEGORY, aRequest,
   1.636 +                  TO_INTBUFFER(mIcon.data), mIcon.data.Length(),
   1.637 +                  mIcon.mimeType);
   1.638 +
   1.639 +  // If the icon does not have a valid MIME type, add it to the failed cache.
   1.640 +  if (mIcon.mimeType.IsEmpty()) {
   1.641 +    nsCOMPtr<nsIURI> iconURI;
   1.642 +    rv = NS_NewURI(getter_AddRefs(iconURI), mIcon.spec);
   1.643 +    NS_ENSURE_SUCCESS(rv, rv);
   1.644 +    rv = favicons->AddFailedFavicon(iconURI);
   1.645 +    NS_ENSURE_SUCCESS(rv, rv);
   1.646 +    return NS_OK;
   1.647 +  }
   1.648 +
   1.649 +  nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
   1.650 +  // aRequest should always QI to nsIChannel.
   1.651 +  // See AsyncFetchAndSetIconFromNetwork::Run()
   1.652 +  MOZ_ASSERT(channel);
   1.653 +  mIcon.expiration = GetExpirationTimeFromChannel(channel);
   1.654 +
   1.655 +  rv = OptimizeIconSize(mIcon, favicons);
   1.656 +  NS_ENSURE_SUCCESS(rv, rv);
   1.657 +
   1.658 +  // If over the maximum size allowed, don't save data to the database to
   1.659 +  // avoid bloating it.
   1.660 +  if (mIcon.data.Length() > MAX_FAVICON_SIZE) {
   1.661 +    return NS_OK;
   1.662 +  }
   1.663 +
   1.664 +  mIcon.status = ICON_STATUS_CHANGED;
   1.665 +
   1.666 +  nsRefPtr<AsyncAssociateIconToPage> event =
   1.667 +    new AsyncAssociateIconToPage(mIcon, mPage, mCallback);
   1.668 +  mDB->DispatchToAsyncThread(event);
   1.669 +
   1.670 +  return NS_OK;
   1.671 +}
   1.672 +
   1.673 +////////////////////////////////////////////////////////////////////////////////
   1.674 +//// AsyncAssociateIconToPage
   1.675 +
   1.676 +AsyncAssociateIconToPage::AsyncAssociateIconToPage(
   1.677 +  IconData& aIcon
   1.678 +, PageData& aPage
   1.679 +, nsCOMPtr<nsIFaviconDataCallback>& aCallback
   1.680 +) : AsyncFaviconHelperBase(aCallback)
   1.681 +  , mIcon(aIcon)
   1.682 +  , mPage(aPage)
   1.683 +{
   1.684 +}
   1.685 +
   1.686 +AsyncAssociateIconToPage::~AsyncAssociateIconToPage()
   1.687 +{
   1.688 +}
   1.689 +
   1.690 +NS_IMETHODIMP
   1.691 +AsyncAssociateIconToPage::Run()
   1.692 +{
   1.693 +  NS_PRECONDITION(!NS_IsMainThread(),
   1.694 +                  "This should not be called on the main thread");
   1.695 +
   1.696 +  nsresult rv = FetchPageInfo(mDB, mPage);
   1.697 +  if (rv == NS_ERROR_NOT_AVAILABLE){
   1.698 +    // We have never seen this page.  If we can add the page to history,
   1.699 +    // we will try to do it later, otherwise just bail out.
   1.700 +    if (!mPage.canAddToHistory) {
   1.701 +      return NS_OK;
   1.702 +    }
   1.703 +  }
   1.704 +  else {
   1.705 +    NS_ENSURE_SUCCESS(rv, rv);
   1.706 +  }
   1.707 +
   1.708 +  mozStorageTransaction transaction(mDB->MainConn(), false,
   1.709 +                                    mozIStorageConnection::TRANSACTION_IMMEDIATE);
   1.710 +
   1.711 +  // If there is no entry for this icon, or the entry is obsolete, replace it.
   1.712 +  if (mIcon.id == 0 || (mIcon.status & ICON_STATUS_CHANGED)) {
   1.713 +    rv = SetIconInfo(mDB, mIcon);
   1.714 +    NS_ENSURE_SUCCESS(rv, rv);
   1.715 +
   1.716 +    // Get the new icon id.  Do this regardless mIcon.id, since other code
   1.717 +    // could have added a entry before us.  Indeed we interrupted the thread
   1.718 +    // after the previous call to FetchIconInfo.
   1.719 +    mIcon.status = (mIcon.status & ~(ICON_STATUS_CACHED)) | ICON_STATUS_SAVED;
   1.720 +    rv = FetchIconInfo(mDB, mIcon);
   1.721 +    NS_ENSURE_SUCCESS(rv, rv);
   1.722 +  }
   1.723 +
   1.724 +  // If the page does not have an id, don't try to insert a new one, cause we
   1.725 +  // don't know where the page comes from.  Not doing so we may end adding
   1.726 +  // a page that otherwise we'd explicitly ignore, like a POST or an error page.
   1.727 +  if (mPage.id == 0) {
   1.728 +    return NS_OK;
   1.729 +  }
   1.730 +
   1.731 +  // Otherwise just associate the icon to the page, if needed.
   1.732 +  if (mPage.iconId != mIcon.id) {
   1.733 +    nsCOMPtr<mozIStorageStatement> stmt;
   1.734 +    if (mPage.id) {
   1.735 +      stmt = mDB->GetStatement(
   1.736 +        "UPDATE moz_places SET favicon_id = :icon_id WHERE id = :page_id"
   1.737 +      );
   1.738 +      NS_ENSURE_STATE(stmt);
   1.739 +      rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("page_id"), mPage.id);
   1.740 +      NS_ENSURE_SUCCESS(rv, rv);
   1.741 +    }
   1.742 +    else {
   1.743 +      stmt = mDB->GetStatement(
   1.744 +        "UPDATE moz_places SET favicon_id = :icon_id WHERE url = :page_url"
   1.745 +      );
   1.746 +      NS_ENSURE_STATE(stmt);
   1.747 +      rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("page_url"), mPage.spec);
   1.748 +      NS_ENSURE_SUCCESS(rv, rv);
   1.749 +    }
   1.750 +    rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("icon_id"), mIcon.id);
   1.751 +    NS_ENSURE_SUCCESS(rv, rv);
   1.752 +
   1.753 +    mozStorageStatementScoper scoper(stmt);
   1.754 +    rv = stmt->Execute();
   1.755 +    NS_ENSURE_SUCCESS(rv, rv);
   1.756 +
   1.757 +    mIcon.status |= ICON_STATUS_ASSOCIATED;
   1.758 +  }
   1.759 +
   1.760 +  rv = transaction.Commit();
   1.761 +  NS_ENSURE_SUCCESS(rv, rv);
   1.762 +
   1.763 +  // Finally, dispatch an event to the main thread to notify observers.
   1.764 +  nsCOMPtr<nsIRunnable> event = new NotifyIconObservers(mIcon, mPage, mCallback);
   1.765 +  rv = NS_DispatchToMainThread(event);
   1.766 +  NS_ENSURE_SUCCESS(rv, rv);
   1.767 +
   1.768 +  return NS_OK;
   1.769 +}
   1.770 +
   1.771 +////////////////////////////////////////////////////////////////////////////////
   1.772 +//// AsyncGetFaviconURLForPage
   1.773 +
   1.774 +// static
   1.775 +nsresult
   1.776 +AsyncGetFaviconURLForPage::start(nsIURI* aPageURI,
   1.777 +                                 nsIFaviconDataCallback* aCallback)
   1.778 +{
   1.779 +  NS_ENSURE_ARG(aCallback);
   1.780 +  NS_ENSURE_ARG(aPageURI);
   1.781 +  NS_PRECONDITION(NS_IsMainThread(),
   1.782 +                  "This should be called on the main thread.");
   1.783 +
   1.784 +  nsAutoCString pageSpec;
   1.785 +  nsresult rv = aPageURI->GetSpec(pageSpec);
   1.786 +  NS_ENSURE_SUCCESS(rv, rv);
   1.787 +
   1.788 +  nsCOMPtr<nsIFaviconDataCallback> callback = aCallback;
   1.789 +  nsRefPtr<AsyncGetFaviconURLForPage> event =
   1.790 +    new AsyncGetFaviconURLForPage(pageSpec, callback);
   1.791 +
   1.792 +  nsRefPtr<Database> DB = Database::GetDatabase();
   1.793 +  NS_ENSURE_STATE(DB);
   1.794 +  DB->DispatchToAsyncThread(event);
   1.795 +
   1.796 +  return NS_OK;
   1.797 +}
   1.798 +
   1.799 +AsyncGetFaviconURLForPage::AsyncGetFaviconURLForPage(
   1.800 +  const nsACString& aPageSpec
   1.801 +, nsCOMPtr<nsIFaviconDataCallback>& aCallback
   1.802 +) : AsyncFaviconHelperBase(aCallback)
   1.803 +{
   1.804 +  mPageSpec.Assign(aPageSpec);
   1.805 +}
   1.806 +
   1.807 +AsyncGetFaviconURLForPage::~AsyncGetFaviconURLForPage()
   1.808 +{
   1.809 +}
   1.810 +
   1.811 +NS_IMETHODIMP
   1.812 +AsyncGetFaviconURLForPage::Run()
   1.813 +{
   1.814 +  NS_PRECONDITION(!NS_IsMainThread(),
   1.815 +                  "This should not be called on the main thread.");
   1.816 +
   1.817 +  nsAutoCString iconSpec;
   1.818 +  nsresult rv = FetchIconURL(mDB, mPageSpec, iconSpec);
   1.819 +  NS_ENSURE_SUCCESS(rv, rv);
   1.820 +
   1.821 +  // Now notify our callback of the icon spec we retrieved, even if empty.
   1.822 +  IconData iconData;
   1.823 +  iconData.spec.Assign(iconSpec);
   1.824 +
   1.825 +  PageData pageData;
   1.826 +  pageData.spec.Assign(mPageSpec);
   1.827 +
   1.828 +  nsCOMPtr<nsIRunnable> event =
   1.829 +    new NotifyIconObservers(iconData, pageData, mCallback);
   1.830 +  rv = NS_DispatchToMainThread(event);
   1.831 +  NS_ENSURE_SUCCESS(rv, rv);
   1.832 +
   1.833 +  return NS_OK;
   1.834 +}
   1.835 +
   1.836 +////////////////////////////////////////////////////////////////////////////////
   1.837 +//// AsyncGetFaviconDataForPage
   1.838 +
   1.839 +// static
   1.840 +nsresult
   1.841 +AsyncGetFaviconDataForPage::start(nsIURI* aPageURI,
   1.842 +                                  nsIFaviconDataCallback* aCallback)
   1.843 +{
   1.844 +  NS_ENSURE_ARG(aCallback);
   1.845 +  NS_ENSURE_ARG(aPageURI);
   1.846 +  NS_PRECONDITION(NS_IsMainThread(),
   1.847 +                  "This should be called on the main thread.");
   1.848 +
   1.849 +  nsAutoCString pageSpec;
   1.850 +  nsresult rv = aPageURI->GetSpec(pageSpec);
   1.851 +  NS_ENSURE_SUCCESS(rv, rv);
   1.852 +
   1.853 +  nsCOMPtr<nsIFaviconDataCallback> callback = aCallback;
   1.854 +  nsRefPtr<AsyncGetFaviconDataForPage> event =
   1.855 +    new AsyncGetFaviconDataForPage(pageSpec, callback);
   1.856 +
   1.857 +  nsRefPtr<Database> DB = Database::GetDatabase();
   1.858 +  NS_ENSURE_STATE(DB);
   1.859 +  DB->DispatchToAsyncThread(event);
   1.860 +
   1.861 +  return NS_OK;
   1.862 +}
   1.863 +
   1.864 +AsyncGetFaviconDataForPage::AsyncGetFaviconDataForPage(
   1.865 +  const nsACString& aPageSpec
   1.866 +, nsCOMPtr<nsIFaviconDataCallback>& aCallback
   1.867 +) : AsyncFaviconHelperBase(aCallback)
   1.868 +{
   1.869 +  mPageSpec.Assign(aPageSpec);
   1.870 +}
   1.871 +
   1.872 +AsyncGetFaviconDataForPage::~AsyncGetFaviconDataForPage()
   1.873 +{
   1.874 +}
   1.875 +
   1.876 +NS_IMETHODIMP
   1.877 +AsyncGetFaviconDataForPage::Run()
   1.878 +{
   1.879 +  NS_PRECONDITION(!NS_IsMainThread(),
   1.880 +                  "This should not be called on the main thread.");
   1.881 +
   1.882 +  nsAutoCString iconSpec;
   1.883 +  nsresult rv = FetchIconURL(mDB, mPageSpec, iconSpec);
   1.884 +  NS_ENSURE_SUCCESS(rv, rv);
   1.885 +
   1.886 +  IconData iconData;
   1.887 +  iconData.spec.Assign(iconSpec);
   1.888 +
   1.889 +  PageData pageData;
   1.890 +  pageData.spec.Assign(mPageSpec);
   1.891 +
   1.892 +  if (!iconSpec.IsEmpty()) {
   1.893 +    rv = FetchIconInfo(mDB, iconData);
   1.894 +    if (NS_FAILED(rv)) {
   1.895 +      iconData.spec.Truncate();
   1.896 +    }
   1.897 +  }
   1.898 +
   1.899 +  nsCOMPtr<nsIRunnable> event =
   1.900 +    new NotifyIconObservers(iconData, pageData, mCallback);
   1.901 +  rv = NS_DispatchToMainThread(event);
   1.902 +  NS_ENSURE_SUCCESS(rv, rv);
   1.903 +  return NS_OK;
   1.904 +}
   1.905 +
   1.906 +////////////////////////////////////////////////////////////////////////////////
   1.907 +//// AsyncReplaceFaviconData
   1.908 +
   1.909 +// static
   1.910 +nsresult
   1.911 +AsyncReplaceFaviconData::start(IconData *aIcon)
   1.912 +{
   1.913 +  NS_ENSURE_ARG(aIcon);
   1.914 +  NS_PRECONDITION(NS_IsMainThread(),
   1.915 +                  "This should be called on the main thread.");
   1.916 +
   1.917 +  nsCOMPtr<nsIFaviconDataCallback> callback;
   1.918 +  nsRefPtr<AsyncReplaceFaviconData> event =
   1.919 +    new AsyncReplaceFaviconData(*aIcon, callback);
   1.920 +
   1.921 +  nsRefPtr<Database> DB = Database::GetDatabase();
   1.922 +  NS_ENSURE_STATE(DB);
   1.923 +  DB->DispatchToAsyncThread(event);
   1.924 +
   1.925 +  return NS_OK;
   1.926 +}
   1.927 +
   1.928 +AsyncReplaceFaviconData::AsyncReplaceFaviconData(
   1.929 +  IconData &aIcon
   1.930 +, nsCOMPtr<nsIFaviconDataCallback>& aCallback
   1.931 +) : AsyncFaviconHelperBase(aCallback)
   1.932 +  , mIcon(aIcon)
   1.933 +{
   1.934 +}
   1.935 +
   1.936 +AsyncReplaceFaviconData::~AsyncReplaceFaviconData()
   1.937 +{
   1.938 +}
   1.939 +
   1.940 +NS_IMETHODIMP
   1.941 +AsyncReplaceFaviconData::Run()
   1.942 +{
   1.943 +  NS_PRECONDITION(!NS_IsMainThread(),
   1.944 +                  "This should not be called on the main thread");
   1.945 +
   1.946 +  IconData dbIcon;
   1.947 +  dbIcon.spec.Assign(mIcon.spec);
   1.948 +  nsresult rv = FetchIconInfo(mDB, dbIcon);
   1.949 +  NS_ENSURE_SUCCESS(rv, rv);
   1.950 +
   1.951 +  if (!dbIcon.id) {
   1.952 +    return NS_OK;
   1.953 +  }
   1.954 +
   1.955 +  rv = SetIconInfo(mDB, mIcon);
   1.956 +  NS_ENSURE_SUCCESS(rv, rv);
   1.957 +
   1.958 +  // We can invalidate the cache version since we now persist the icon.
   1.959 +  nsCOMPtr<nsIRunnable> event = new RemoveIconDataCacheEntry(mIcon, mCallback);
   1.960 +  rv = NS_DispatchToMainThread(event);
   1.961 +  NS_ENSURE_SUCCESS(rv, rv);
   1.962 +
   1.963 +  return NS_OK;
   1.964 +}
   1.965 +
   1.966 +
   1.967 +////////////////////////////////////////////////////////////////////////////////
   1.968 +//// RemoveIconDataCacheEntry
   1.969 +
   1.970 +RemoveIconDataCacheEntry::RemoveIconDataCacheEntry(
   1.971 +  IconData& aIcon
   1.972 +, nsCOMPtr<nsIFaviconDataCallback>& aCallback
   1.973 +)
   1.974 +: AsyncFaviconHelperBase(aCallback)
   1.975 +, mIcon(aIcon)
   1.976 +{
   1.977 +}
   1.978 +
   1.979 +RemoveIconDataCacheEntry::~RemoveIconDataCacheEntry()
   1.980 +{
   1.981 +}
   1.982 +
   1.983 +NS_IMETHODIMP
   1.984 +RemoveIconDataCacheEntry::Run()
   1.985 +{
   1.986 +  NS_PRECONDITION(NS_IsMainThread(),
   1.987 +                  "This should be called on the main thread");
   1.988 +
   1.989 +  nsCOMPtr<nsIURI> iconURI;
   1.990 +  nsresult rv = NS_NewURI(getter_AddRefs(iconURI), mIcon.spec);
   1.991 +  NS_ENSURE_SUCCESS(rv, rv);
   1.992 +
   1.993 +  nsFaviconService* favicons = nsFaviconService::GetFaviconService();
   1.994 +  NS_ENSURE_STATE(favicons);
   1.995 +  favicons->mUnassociatedIcons.RemoveEntry(iconURI);
   1.996 +
   1.997 +  return NS_OK;
   1.998 +}
   1.999 +
  1.1000 +
  1.1001 +////////////////////////////////////////////////////////////////////////////////
  1.1002 +//// NotifyIconObservers
  1.1003 +
  1.1004 +NotifyIconObservers::NotifyIconObservers(
  1.1005 +  IconData& aIcon
  1.1006 +, PageData& aPage
  1.1007 +, nsCOMPtr<nsIFaviconDataCallback>& aCallback
  1.1008 +)
  1.1009 +: AsyncFaviconHelperBase(aCallback)
  1.1010 +, mIcon(aIcon)
  1.1011 +, mPage(aPage)
  1.1012 +{
  1.1013 +}
  1.1014 +
  1.1015 +NotifyIconObservers::~NotifyIconObservers()
  1.1016 +{
  1.1017 +}
  1.1018 +
  1.1019 +NS_IMETHODIMP
  1.1020 +NotifyIconObservers::Run()
  1.1021 +{
  1.1022 +  NS_PRECONDITION(NS_IsMainThread(),
  1.1023 +                  "This should be called on the main thread");
  1.1024 +
  1.1025 +  nsCOMPtr<nsIURI> iconURI;
  1.1026 +  if (!mIcon.spec.IsEmpty()) {
  1.1027 +    MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_NewURI(getter_AddRefs(iconURI), mIcon.spec)));
  1.1028 +    if (iconURI)
  1.1029 +    {
  1.1030 +      // Notify observers only if something changed.
  1.1031 +      if (mIcon.status & ICON_STATUS_SAVED ||
  1.1032 +          mIcon.status & ICON_STATUS_ASSOCIATED) {
  1.1033 +        SendGlobalNotifications(iconURI);
  1.1034 +      }
  1.1035 +    }
  1.1036 +  }
  1.1037 +
  1.1038 +  if (mCallback) {
  1.1039 +    (void)mCallback->OnComplete(iconURI, mIcon.data.Length(),
  1.1040 +                                TO_INTBUFFER(mIcon.data), mIcon.mimeType);
  1.1041 +  }
  1.1042 +
  1.1043 +  return NS_OK;
  1.1044 +}
  1.1045 +
  1.1046 +void
  1.1047 +NotifyIconObservers::SendGlobalNotifications(nsIURI* aIconURI)
  1.1048 +{
  1.1049 +  nsCOMPtr<nsIURI> pageURI;
  1.1050 +  MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_NewURI(getter_AddRefs(pageURI), mPage.spec)));
  1.1051 +  if (pageURI) {
  1.1052 +    nsFaviconService* favicons = nsFaviconService::GetFaviconService();
  1.1053 +    MOZ_ASSERT(favicons);
  1.1054 +    if (favicons) {
  1.1055 +      (void)favicons->SendFaviconNotifications(pageURI, aIconURI, mPage.guid);
  1.1056 +    }
  1.1057 +  }
  1.1058 +
  1.1059 +  // If the page is bookmarked and the bookmarked url is different from the
  1.1060 +  // updated one, start a new task to update its icon as well.
  1.1061 +  if (!mPage.bookmarkedSpec.IsEmpty() &&
  1.1062 +      !mPage.bookmarkedSpec.Equals(mPage.spec)) {
  1.1063 +    // Create a new page struct to avoid polluting it with old data.
  1.1064 +    PageData bookmarkedPage;
  1.1065 +    bookmarkedPage.spec = mPage.bookmarkedSpec;
  1.1066 +
  1.1067 +    // This will be silent, so be sure to not pass in the current callback.
  1.1068 +    nsCOMPtr<nsIFaviconDataCallback> nullCallback;
  1.1069 +    nsRefPtr<AsyncAssociateIconToPage> event =
  1.1070 +        new AsyncAssociateIconToPage(mIcon, bookmarkedPage, nullCallback);
  1.1071 +    mDB->DispatchToAsyncThread(event);
  1.1072 +  }
  1.1073 +}
  1.1074 +
  1.1075 +} // namespace places
  1.1076 +} // namespace mozilla

mercurial