2010-03-25 04:34:31 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Places.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is the Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Marco Bonardo <mak77@bonardo.net> (original author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#ifndef AsyncFaviconHelpers_h_
|
|
|
|
#define AsyncFaviconHelpers_h_
|
|
|
|
|
2011-10-27 02:11:34 -07:00
|
|
|
#include "nsIFaviconService.h"
|
2010-03-25 04:34:40 -07:00
|
|
|
#include "nsIChannelEventSink.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
|
2011-10-27 02:11:34 -07:00
|
|
|
#include "Database.h"
|
|
|
|
#include "mozilla/storage.h"
|
|
|
|
|
2010-03-25 04:34:31 -07:00
|
|
|
#define ICON_STATUS_UNKNOWN 0
|
|
|
|
#define ICON_STATUS_CHANGED 1 << 0
|
|
|
|
#define ICON_STATUS_SAVED 1 << 1
|
|
|
|
#define ICON_STATUS_ASSOCIATED 1 << 2
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace places {
|
|
|
|
|
|
|
|
/**
|
2010-11-15 17:13:37 -08:00
|
|
|
* Indicates when a icon should be fetched from network.
|
2010-03-25 04:34:31 -07:00
|
|
|
*/
|
2010-11-15 17:13:37 -08:00
|
|
|
enum AsyncFaviconFetchMode {
|
|
|
|
FETCH_NEVER = 0
|
|
|
|
, FETCH_IF_MISSING
|
|
|
|
, FETCH_ALWAYS
|
2010-03-25 04:34:31 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2010-11-15 17:13:37 -08:00
|
|
|
* Data cache for a icon entry.
|
2010-03-25 04:34:31 -07:00
|
|
|
*/
|
2010-11-15 17:13:37 -08:00
|
|
|
struct IconData
|
|
|
|
{
|
|
|
|
IconData()
|
|
|
|
: id(0)
|
|
|
|
, expiration(0)
|
|
|
|
, fetchMode(FETCH_NEVER)
|
|
|
|
, status(ICON_STATUS_UNKNOWN)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt64 id;
|
|
|
|
nsCString spec;
|
|
|
|
nsCString data;
|
|
|
|
nsCString mimeType;
|
|
|
|
PRTime expiration;
|
|
|
|
enum AsyncFaviconFetchMode fetchMode;
|
|
|
|
PRUint16 status; // This is a bitset, see ICON_STATUS_* defines above.
|
2010-03-25 04:34:31 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2010-11-15 17:13:37 -08:00
|
|
|
* Data cache for a page entry.
|
2010-03-25 04:34:31 -07:00
|
|
|
*/
|
2010-11-15 17:13:37 -08:00
|
|
|
struct PageData
|
2010-03-25 04:34:31 -07:00
|
|
|
{
|
2010-11-15 17:13:37 -08:00
|
|
|
PageData()
|
|
|
|
: id(0)
|
|
|
|
, canAddToHistory(true)
|
|
|
|
, iconId(0)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
guid.SetIsVoid(true);
|
2010-11-15 17:13:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
PRInt64 id;
|
|
|
|
nsCString spec;
|
|
|
|
nsCString bookmarkedSpec;
|
|
|
|
nsString revHost;
|
|
|
|
bool canAddToHistory; // False for disabled history and unsupported schemas.
|
|
|
|
PRInt64 iconId;
|
2011-06-30 13:06:56 -07:00
|
|
|
nsCString guid;
|
2010-03-25 04:34:31 -07:00
|
|
|
};
|
|
|
|
|
2011-05-20 05:37:02 -07:00
|
|
|
/**
|
|
|
|
* Base class for events declared in this file. This class's main purpose is
|
2011-10-27 02:11:34 -07:00
|
|
|
* to declare a destructor which releases mCallback on the main thread.
|
2011-05-20 05:37:02 -07:00
|
|
|
*/
|
|
|
|
class AsyncFaviconHelperBase : public nsRunnable
|
|
|
|
{
|
|
|
|
protected:
|
2011-10-27 02:11:34 -07:00
|
|
|
AsyncFaviconHelperBase(nsCOMPtr<nsIFaviconDataCallback>& aCallback);
|
2011-05-20 05:37:02 -07:00
|
|
|
|
|
|
|
virtual ~AsyncFaviconHelperBase();
|
|
|
|
|
2011-10-27 02:11:34 -07:00
|
|
|
nsRefPtr<Database> mDB;
|
2011-05-20 05:37:02 -07:00
|
|
|
// Strong reference since we are responsible for its existence.
|
|
|
|
nsCOMPtr<nsIFaviconDataCallback> mCallback;
|
|
|
|
};
|
|
|
|
|
2010-03-25 04:34:31 -07:00
|
|
|
/**
|
2010-11-15 17:13:37 -08:00
|
|
|
* Async fetches icon from database or network, associates it with the required
|
|
|
|
* page and finally notifies the change.
|
2010-03-25 04:34:31 -07:00
|
|
|
*/
|
2011-05-20 05:37:02 -07:00
|
|
|
class AsyncFetchAndSetIconForPage : public AsyncFaviconHelperBase
|
2010-03-25 04:34:31 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-11-15 17:13:37 -08:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2010-03-25 04:34:31 -07:00
|
|
|
|
|
|
|
/**
|
2010-11-15 17:13:37 -08:00
|
|
|
* Creates the event and dispatches it to the async thread.
|
2010-03-25 04:34:31 -07:00
|
|
|
*
|
2010-11-15 17:13:37 -08:00
|
|
|
* @param aFaviconURI
|
|
|
|
* URI of the icon to be fetched and associated.
|
|
|
|
* @param aPageURI
|
|
|
|
* URI of the page to which associate the icon.
|
|
|
|
* @param aFetchMode
|
|
|
|
* Specifies whether a icon should be fetched from network if not found
|
|
|
|
* in the database.
|
2010-03-25 04:34:31 -07:00
|
|
|
* @param aCallback
|
2011-05-20 05:37:02 -07:00
|
|
|
* Function to be called when the fetch-and-associate process finishes.
|
2010-03-25 04:34:31 -07:00
|
|
|
*/
|
2010-11-15 17:13:37 -08:00
|
|
|
static nsresult start(nsIURI* aFaviconURI,
|
|
|
|
nsIURI* aPageURI,
|
|
|
|
enum AsyncFaviconFetchMode aFetchMode,
|
|
|
|
nsIFaviconDataCallback* aCallback);
|
2010-03-25 04:34:31 -07:00
|
|
|
|
|
|
|
/**
|
2010-11-15 17:13:37 -08:00
|
|
|
* Constructor.
|
2010-03-25 04:34:31 -07:00
|
|
|
*
|
2010-11-15 17:13:37 -08:00
|
|
|
* @param aIcon
|
|
|
|
* Icon to be fetched and associated.
|
|
|
|
* @param aPage
|
|
|
|
* Page to which associate the icon.
|
|
|
|
* @param aCallback
|
2011-05-20 05:37:02 -07:00
|
|
|
* Function to be called when the fetch-and-associate process finishes.
|
2010-03-25 04:34:31 -07:00
|
|
|
*/
|
2010-11-15 17:13:37 -08:00
|
|
|
AsyncFetchAndSetIconForPage(IconData& aIcon,
|
|
|
|
PageData& aPage,
|
|
|
|
nsCOMPtr<nsIFaviconDataCallback>& aCallback);
|
2010-03-25 04:34:31 -07:00
|
|
|
|
2010-11-15 17:13:37 -08:00
|
|
|
virtual ~AsyncFetchAndSetIconForPage();
|
2010-03-25 04:34:31 -07:00
|
|
|
|
2010-11-15 17:13:37 -08:00
|
|
|
protected:
|
|
|
|
IconData mIcon;
|
|
|
|
PageData mPage;
|
2010-03-25 04:34:40 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2010-11-15 17:13:37 -08:00
|
|
|
* If needed will asynchronously fetch the icon from the network. It will
|
|
|
|
* finally dispatch an event to the async thread to associate the icon with
|
|
|
|
* the required page.
|
2010-03-25 04:34:40 -07:00
|
|
|
*/
|
2011-05-20 05:37:02 -07:00
|
|
|
class AsyncFetchAndSetIconFromNetwork : public AsyncFaviconHelperBase
|
2010-11-15 17:13:37 -08:00
|
|
|
, public nsIStreamListener
|
|
|
|
, public nsIInterfaceRequestor
|
|
|
|
, public nsIChannelEventSink
|
2010-03-25 04:34:40 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
NS_DECL_NSICHANNELEVENTSINK
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
2010-11-15 17:13:37 -08:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2010-03-25 04:34:40 -07:00
|
|
|
|
2010-11-15 17:13:37 -08:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param aIcon
|
|
|
|
* Icon to be fetched and associated.
|
|
|
|
* @param aPage
|
|
|
|
* Page to which associate the icon.
|
|
|
|
* @param aCallback
|
2011-05-20 05:37:02 -07:00
|
|
|
* Function to be called when the fetch-and-associate process finishes.
|
2010-11-15 17:13:37 -08:00
|
|
|
*/
|
|
|
|
AsyncFetchAndSetIconFromNetwork(IconData& aIcon,
|
|
|
|
PageData& aPage,
|
|
|
|
nsCOMPtr<nsIFaviconDataCallback>& aCallback);
|
2010-03-25 04:34:40 -07:00
|
|
|
|
2010-11-15 17:13:37 -08:00
|
|
|
virtual ~AsyncFetchAndSetIconFromNetwork();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
IconData mIcon;
|
|
|
|
PageData mPage;
|
2010-03-25 04:34:40 -07:00
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
|
|
|
};
|
|
|
|
|
2010-03-25 04:34:42 -07:00
|
|
|
/**
|
2010-11-15 17:13:37 -08:00
|
|
|
* Associates the icon to the required page, finally dispatches an event to the
|
|
|
|
* main thread to notify the change to observers.
|
2010-03-25 04:34:42 -07:00
|
|
|
*/
|
2011-05-20 05:37:02 -07:00
|
|
|
class AsyncAssociateIconToPage : public AsyncFaviconHelperBase
|
2010-03-25 04:34:42 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-11-15 17:13:37 -08:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2010-03-25 04:34:42 -07:00
|
|
|
|
2010-11-15 17:13:37 -08:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param aIcon
|
|
|
|
* Icon to be associated.
|
|
|
|
* @param aPage
|
|
|
|
* Page to which associate the icon.
|
|
|
|
* @param aCallback
|
2011-05-20 05:37:02 -07:00
|
|
|
* Function to be called when the associate process finishes.
|
2010-11-15 17:13:37 -08:00
|
|
|
*/
|
|
|
|
AsyncAssociateIconToPage(IconData& aIcon,
|
|
|
|
PageData& aPage,
|
|
|
|
nsCOMPtr<nsIFaviconDataCallback>& aCallback);
|
2010-03-25 04:34:45 -07:00
|
|
|
|
2010-11-15 17:13:37 -08:00
|
|
|
virtual ~AsyncAssociateIconToPage();
|
2010-03-25 04:34:45 -07:00
|
|
|
|
2010-11-15 17:13:37 -08:00
|
|
|
protected:
|
|
|
|
IconData mIcon;
|
|
|
|
PageData mPage;
|
2011-05-20 05:37:02 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously tries to get the URL of a page's favicon. If this succeeds,
|
|
|
|
* notifies the given observer.
|
|
|
|
*/
|
|
|
|
class AsyncGetFaviconURLForPage : public AsyncFaviconHelperBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the event and dispatches it to the I/O thread.
|
|
|
|
*
|
|
|
|
* @param aPageURI
|
|
|
|
* URL of the page whose favicon's URL we're fetching
|
|
|
|
* @param aCallback
|
|
|
|
* function to be called once the URL is retrieved from the database
|
|
|
|
*/
|
|
|
|
static nsresult start(nsIURI* aPageURI,
|
|
|
|
nsIFaviconDataCallback* aCallback);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param aPageSpec
|
|
|
|
* URL of the page whose favicon's URL we're fetching
|
|
|
|
* @param aCallback
|
|
|
|
* function to be called once the URL is retrieved from the database
|
|
|
|
*/
|
|
|
|
AsyncGetFaviconURLForPage(const nsACString& aPageSpec,
|
|
|
|
nsCOMPtr<nsIFaviconDataCallback>& aCallback);
|
|
|
|
|
|
|
|
virtual ~AsyncGetFaviconURLForPage();
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCString mPageSpec;
|
2010-03-25 04:34:45 -07:00
|
|
|
};
|
|
|
|
|
2011-08-31 07:28:17 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously tries to get the URL and data of a page's favicon.
|
|
|
|
* If this succeeds, notifies the given observer.
|
|
|
|
*/
|
|
|
|
class AsyncGetFaviconDataForPage : public AsyncFaviconHelperBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the event and dispatches it to the I/O thread.
|
|
|
|
*
|
|
|
|
* @param aPageURI
|
|
|
|
* URL of the page whose favicon URL and data we're fetching
|
|
|
|
* @param aCallback
|
|
|
|
* function to be called once the URL and data is retrieved from the database
|
|
|
|
*/
|
|
|
|
static nsresult start(nsIURI* aPageURI,
|
|
|
|
nsIFaviconDataCallback* aCallback);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param aPageSpec
|
|
|
|
* URL of the page whose favicon URL and data we're fetching
|
|
|
|
* @param aCallback
|
|
|
|
* function to be called once the URL is retrieved from the database
|
|
|
|
*/
|
|
|
|
AsyncGetFaviconDataForPage(const nsACString& aPageSpec,
|
|
|
|
nsCOMPtr<nsIFaviconDataCallback>& aCallback);
|
|
|
|
|
|
|
|
virtual ~AsyncGetFaviconDataForPage();
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCString mPageSpec;
|
|
|
|
};
|
|
|
|
|
2010-03-25 04:34:47 -07:00
|
|
|
/**
|
2010-11-15 17:13:37 -08:00
|
|
|
* Notifies the icon change to favicon observers.
|
2010-03-25 04:34:47 -07:00
|
|
|
*/
|
2011-05-20 05:37:02 -07:00
|
|
|
class NotifyIconObservers : public AsyncFaviconHelperBase
|
2010-03-25 04:34:47 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-11-15 17:13:37 -08:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
NotifyIconObservers(IconData& aIcon,
|
|
|
|
PageData& aPage,
|
|
|
|
nsCOMPtr<nsIFaviconDataCallback>& aCallback);
|
|
|
|
virtual ~NotifyIconObservers();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
IconData mIcon;
|
|
|
|
PageData mPage;
|
2010-03-25 04:34:47 -07:00
|
|
|
};
|
|
|
|
|
2010-03-25 04:34:31 -07:00
|
|
|
} // namespace places
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // AsyncFaviconHelpers_h_
|