Commit cb404c1b authored by Tatiana Meshkova's avatar Tatiana Meshkova

Updated history module API

parent 40bae9c0
......@@ -19,168 +19,200 @@ EmbedHistoryListener* EmbedHistoryListener::sHistory = NULL;
EmbedHistoryListener*
EmbedHistoryListener::GetSingleton()
{
if (!sHistory) {
sHistory = new EmbedHistoryListener();
NS_ENSURE_TRUE(sHistory, nullptr);
}
if (!sHistory) {
sHistory = new EmbedHistoryListener();
NS_ENSURE_TRUE(sHistory, nullptr);
}
NS_ADDREF(sHistory);
return sHistory;
NS_ADDREF(sHistory);
return sHistory;
}
EmbedHistoryListener::EmbedHistoryListener()
{
mListeners.Init();
mListeners.Init();
nsresult rv;
nsCOMPtr<nsIObserverService> observerService =
do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
if (observerService) {
rv = observerService->AddObserver(this,
"history:notifyVisited", false);
}
}
NS_IMETHODIMP
EmbedHistoryListener::RegisterVisitedCallback(nsIURI *aURI, Link *aContent)
{
if (!aContent || !aURI)
return NS_OK;
nsAutoCString uri;
nsresult rv = aURI->GetSpec(uri);
if (NS_FAILED(rv)) return rv;
NS_ConvertUTF8toUTF16 uriString(uri);
nsTArray<Link*>* list = mListeners.Get(uriString);
if (! list) {
list = new nsTArray<Link*>();
mListeners.Put(uriString, list);
}
list->AppendElement(aContent);
nsString message;
// Just simple property bag support still
nsCOMPtr<nsIEmbedLiteJSON> json = do_GetService("@mozilla.org/embedlite-json;1");
nsCOMPtr<nsIWritablePropertyBag2> root;
json->CreateObject(getter_AddRefs(root));
root->SetPropertyAsACString(NS_LITERAL_STRING("uri"), uri);
json->CreateJSON(root, message);
nsCOMPtr<nsIObserverService> observerService =
do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
if (observerService)
observerService->NotifyObservers(nullptr, "history:checkurivisited", message.get());
if (!aContent || !aURI)
return NS_OK;
nsAutoCString uri;
nsresult rv = aURI->GetSpec(uri);
if (NS_FAILED(rv)) return rv;
nsTArray<Link*>* list = mListeners.Get(uri);
if (!list) {
list = new nsTArray<Link*>();
mListeners.Put(uri, list);
}
list->AppendElement(aContent);
nsString message;
// Just simple property bag support still
nsCOMPtr<nsIEmbedLiteJSON> json = do_GetService("@mozilla.org/embedlite-json;1");
nsCOMPtr<nsIWritablePropertyBag2> root;
json->CreateObject(getter_AddRefs(root));
root->SetPropertyAsACString(NS_LITERAL_STRING("msg"), NS_LITERAL_CSTRING("checkvisited"));
root->SetPropertyAsACString(NS_LITERAL_STRING("uri"), uri);
json->CreateJSON(root, message);
nsCOMPtr<nsIObserverService> observerService =
do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
// Possible we can avoid json stuff for this case and send uri directly
if (observerService) {
observerService->NotifyObservers(nullptr, "em:history", message.get());
}
return NS_OK;
}
NS_IMETHODIMP
EmbedHistoryListener::UnregisterVisitedCallback(nsIURI *aURI, Link *aContent)
{
if (!aContent || !aURI)
return NS_OK;
nsAutoCString uri;
nsresult rv = aURI->GetSpec(uri);
if (NS_FAILED(rv)) return rv;
NS_ConvertUTF8toUTF16 uriString(uri);
nsTArray<Link*>* list = mListeners.Get(uriString);
if (! list)
return NS_OK;
list->RemoveElement(aContent);
if (list->IsEmpty()) {
mListeners.Remove(uriString);
delete list;
}
if (!aContent || !aURI)
return NS_OK;
nsAutoCString uri;
nsresult rv = aURI->GetSpec(uri);
if (NS_FAILED(rv)) return rv;
nsTArray<Link*>* list = mListeners.Get(uri);
if (!list)
return NS_OK;
list->RemoveElement(aContent);
if (list->IsEmpty()) {
mListeners.Remove(uri);
delete list;
}
return NS_OK;
}
NS_IMETHODIMP
EmbedHistoryListener::VisitURI(nsIURI *aURI, nsIURI *aLastVisitedURI, uint32_t aFlags)
{
if (!aURI)
return NS_OK;
if (!(aFlags & VisitFlags::TOP_LEVEL))
return NS_OK;
if (aFlags & VisitFlags::REDIRECT_SOURCE)
return NS_OK;
if (aFlags & VisitFlags::UNRECOVERABLE_ERROR)
return NS_OK;
if (!aURI)
return NS_OK;
nsAutoCString uri;
nsresult rv = aURI->GetSpec(uri);
if (NS_FAILED(rv)) return rv;
if (!(aFlags & VisitFlags::TOP_LEVEL))
return NS_OK;
nsString message;
// Just simple property bag support still
nsCOMPtr<nsIEmbedLiteJSON> json = do_GetService("@mozilla.org/embedlite-json;1");
nsCOMPtr<nsIWritablePropertyBag2> root;
json->CreateObject(getter_AddRefs(root));
root->SetPropertyAsACString(NS_LITERAL_STRING("uri"), uri);
if (aFlags & VisitFlags::REDIRECT_SOURCE)
return NS_OK;
json->CreateJSON(root, message);
nsCOMPtr<nsIObserverService> observerService =
do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
if (observerService)
observerService->NotifyObservers(nullptr, "history:markurivisited", message.get());
if (aFlags & VisitFlags::UNRECOVERABLE_ERROR)
return NS_OK;
nsAutoCString uri;
nsresult rv = aURI->GetSpec(uri);
if (NS_FAILED(rv)) return rv;
nsString message;
// Just simple property bag support still
nsCOMPtr<nsIEmbedLiteJSON> json = do_GetService("@mozilla.org/embedlite-json;1");
nsCOMPtr<nsIWritablePropertyBag2> root;
json->CreateObject(getter_AddRefs(root));
root->SetPropertyAsACString(NS_LITERAL_STRING("msg"), NS_LITERAL_CSTRING("markvisited"));
root->SetPropertyAsACString(NS_LITERAL_STRING("uri"), uri);
json->CreateJSON(root, message);
nsCOMPtr<nsIObserverService> observerService =
do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
if (observerService) {
observerService->NotifyObservers(nullptr, "em:history", message.get());
}
return NS_OK;
}
nsIEmbedAppService*
EmbedHistoryListener::GetService()
{
if (!mService) {
mService = do_GetService("@mozilla.org/embedlite-app-service;1");
}
return mService.get();
if (!mService) {
mService = do_GetService("@mozilla.org/embedlite-app-service;1");
}
return mService.get();
}
NS_IMETHODIMP
EmbedHistoryListener::SetURITitle(nsIURI *aURI, const nsAString& aTitle)
{
if (!aURI)
return NS_OK;
if (!aURI)
return NS_OK;
// we don't do anything with this right now
#if 0
nsAutoCString uri;
nsresult rv = aURI->GetSpec(uri);
if (NS_FAILED(rv)) return rv;
#endif
// we don't do anything with this right now
nsAutoCString uri;
nsresult rv = aURI->GetSpec(uri);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIObserverService> observerService =
do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
nsString message;
// Just simple property bag support still
nsCOMPtr<nsIEmbedLiteJSON> json = do_GetService("@mozilla.org/embedlite-json;1");
nsCOMPtr<nsIWritablePropertyBag2> root;
json->CreateObject(getter_AddRefs(root));
root->SetPropertyAsACString(NS_LITERAL_STRING("msg"), NS_LITERAL_CSTRING("settitle"));
root->SetPropertyAsACString(NS_LITERAL_STRING("uri"), uri);
root->SetPropertyAsAString(NS_LITERAL_STRING("title"), aTitle);
json->CreateJSON(root, message);
if (observerService) {
observerService->NotifyObservers(nullptr, "em:history", message.get());
}
return NS_OK;
}
return NS_OK;
NS_IMETHODIMP
EmbedHistoryListener::Observe(nsISupports *aSubject,
const char *aTopic,
const PRUnichar *aData)
{
if (!strcmp(aTopic, "history:notifyVisited")) {
sHistory->mPendingURIs.Push(NS_ConvertUTF16toUTF8(aData));
NS_DispatchToMainThread(sHistory);
}
return NS_OK;
}
NS_IMETHODIMP
EmbedHistoryListener::NotifyVisited(nsIURI *aURI)
{
if (aURI && sHistory) {
nsAutoCString spec;
(void)aURI->GetSpec(spec);
sHistory->mPendingURIs.Push(NS_ConvertUTF8toUTF16(spec));
NS_DispatchToMainThread(sHistory);
}
return NS_OK;
if (aURI && sHistory) {
nsAutoCString spec;
(void)aURI->GetSpec(spec);
sHistory->mPendingURIs.Push(spec);
NS_DispatchToMainThread(sHistory);
}
return NS_OK;
}
NS_IMETHODIMP
EmbedHistoryListener::Run()
{
while (!mPendingURIs.IsEmpty()) {
nsString uriString = mPendingURIs.Pop();
nsTArray<Link*>* list = sHistory->mListeners.Get(uriString);
if (list) {
for (unsigned int i = 0; i < list->Length(); i++) {
list->ElementAt(i)->SetLinkState(eLinkState_Visited);
}
// as per the IHistory interface contract, remove the
// Link pointers once they have been notified
mListeners.Remove(uriString);
delete list;
}
while (!mPendingURIs.IsEmpty()) {
nsCString uriString = mPendingURIs.Pop();
nsTArray<Link*>* list = sHistory->mListeners.Get(uriString);
if (list) {
for (unsigned int i = 0; i < list->Length(); i++) {
list->ElementAt(i)->SetLinkState(eLinkState_Visited);
}
// as per the IHistory interface contract, remove the
// Link pointers once they have been notified
sHistory->mListeners.Remove(uriString);
delete list;
}
}
return NS_OK;
return NS_OK;
}
......@@ -13,6 +13,7 @@
#include "nsThreadUtils.h"
#include "nsIEmbedAppService.h"
#include "nsServiceManagerUtils.h"
#include "nsIObserver.h"
#define NS_EMBEDLITEHISTORY_CID \
{ 0xec7cf1e2, \
......@@ -20,31 +21,34 @@
0x11e2, \
{ 0xa7, 0x9a, 0xfb, 0x19, 0xfe, 0x29, 0x97 }}
class EmbedHistoryListener : public mozilla::IHistory, public nsIRunnable
class EmbedHistoryListener : public mozilla::IHistory
, public nsIRunnable
, public nsIObserver
{
public:
NS_DECL_ISUPPORTS
NS_DECL_IHISTORY
NS_DECL_NSIRUNNABLE
NS_DECL_ISUPPORTS
NS_DECL_IHISTORY
NS_DECL_NSIRUNNABLE
NS_DECL_NSIOBSERVER
nsresult Init() { return NS_OK; }
nsresult Init() { return NS_OK; }
/**
* Obtains a pointer that has had AddRef called on it. Used by the service
* manager only.
*/
static EmbedHistoryListener* GetSingleton();
/**
* Obtains a pointer that has had AddRef called on it. Used by the service
* manager only.
*/
static EmbedHistoryListener* GetSingleton();
EmbedHistoryListener();
EmbedHistoryListener();
private:
nsIEmbedAppService* GetService();
nsIEmbedAppService* GetService();
static EmbedHistoryListener* sHistory;
static EmbedHistoryListener* sHistory;
nsDataHashtable<nsStringHashKey, nsTArray<mozilla::dom::Link *> *> mListeners;
nsTPriorityQueue<nsString> mPendingURIs;
nsCOMPtr<nsIEmbedAppService> mService;
nsDataHashtable<nsCStringHashKey, nsTArray<mozilla::dom::Link*> *> mListeners;
nsTPriorityQueue<nsCString> mPendingURIs;
nsCOMPtr<nsIEmbedAppService> mService;
};
#define NS_EMBED_HISTORY_CONTRACTID "@mozilla.org/embed-history-component;1"
......
......@@ -13,46 +13,25 @@
#include "nsDocShellCID.h"
#include <iostream>
// XPCOMGlueStartup
#include "nsXPCOMGlue.h"
/* ===== XPCOM registration stuff ======== */
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(EmbedHistoryListener, EmbedHistoryListener::GetSingleton)
NS_DEFINE_NAMED_CID(NS_EMBED_HISTORY_SERVICE_CID);
static const mozilla::Module::CIDEntry kEMBEDHISTORYCIDs[] = {
{ &kNS_EMBED_HISTORY_SERVICE_CID, false, NULL, EmbedHistoryListenerConstructor },
{ NULL }
{ &kNS_EMBED_HISTORY_SERVICE_CID, false, NULL, EmbedHistoryListenerConstructor },
{ NULL }
};
static const mozilla::Module::ContractIDEntry kEMBEDHISTORYContracts[] = {
{ NS_IHISTORY_CONTRACTID, &kNS_EMBED_HISTORY_SERVICE_CID },
{ NULL }
{ NS_IHISTORY_CONTRACTID, &kNS_EMBED_HISTORY_SERVICE_CID },
{ NULL }
};
static nsresult
EmbedHistory_Initialize()
{
#ifdef XPCOM_GLUE
XPCOMGlueStartup(getenv("XRE_LIBXPCOM_PATH"));
#endif
return NS_OK;
}
static void
EmbedHistory_Shutdown()
{
}
static const mozilla::Module kEMBEDHISTORYModule = {
mozilla::Module::kVersion,
kEMBEDHISTORYCIDs,
kEMBEDHISTORYContracts,
NULL,
NULL,
EmbedHistory_Initialize,
EmbedHistory_Shutdown
mozilla::Module::kVersion,
kEMBEDHISTORYCIDs,
kEMBEDHISTORYContracts
};
NSMODULE_DEFN(nsEmbedHistoryModule) = &kEMBEDHISTORYModule;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment