fune/browser/components/migration/nsIEHistoryEnumerator.cpp
Kris Maglione 65c28aa0ad Bug 1484496: Part 2 - Add common base class for all nsISimpleEnumerator implementations. r=froydnj
In order to allow JS callers to use nsISimpleEnumerator instances with the JS
iteration protocol, we'll need to additional methods to every instance. Since
we currently have a large number of unrelated implementations, it would be
best if they could share the same implementation for the JS portion of the
protocol.

This patch adds a stub nsSimpleEnumerator base class, and updates all existing
implementations to inherit from it. A follow-up will add a new base interface
to this class, and implement the additional functionality required for JS
iteration.

Differential Revision: https://phabricator.services.mozilla.com/D3725

--HG--
extra : rebase_source : ad66d7b266856d5a750c772e4710679fab9434b1
extra : histedit_source : a83ebffbf2f0b191ba7de9007f73def6b9a955b8
2018-08-18 14:22:47 -07:00

118 lines
3.1 KiB
C++

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsIEHistoryEnumerator.h"
#include <urlhist.h>
#include <shlguid.h>
#include "nsArrayEnumerator.h"
#include "nsCOMArray.h"
#include "nsIURI.h"
#include "nsIVariant.h"
#include "nsNetUtil.h"
#include "nsString.h"
#include "nsWindowsMigrationUtils.h"
#include "prtime.h"
////////////////////////////////////////////////////////////////////////////////
//// nsIEHistoryEnumerator
nsIEHistoryEnumerator::nsIEHistoryEnumerator()
{
::CoInitialize(nullptr);
}
nsIEHistoryEnumerator::~nsIEHistoryEnumerator()
{
::CoUninitialize();
}
void
nsIEHistoryEnumerator::EnsureInitialized()
{
if (mURLEnumerator)
return;
HRESULT hr = ::CoCreateInstance(CLSID_CUrlHistory,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IUrlHistoryStg2,
getter_AddRefs(mIEHistory));
if (FAILED(hr))
return;
hr = mIEHistory->EnumUrls(getter_AddRefs(mURLEnumerator));
if (FAILED(hr))
return;
}
NS_IMETHODIMP
nsIEHistoryEnumerator::HasMoreElements(bool* _retval)
{
*_retval = false;
EnsureInitialized();
MOZ_ASSERT(mURLEnumerator, "Should have instanced an IE History URLEnumerator");
if (!mURLEnumerator)
return NS_OK;
STATURL statURL;
ULONG fetched;
// First argument is not implemented, so doesn't matter what we pass.
HRESULT hr = mURLEnumerator->Next(1, &statURL, &fetched);
if (FAILED(hr) || fetched != 1UL) {
// Reached the last entry.
return NS_OK;
}
nsCOMPtr<nsIURI> uri;
if (statURL.pwcsUrl) {
nsDependentString url(statURL.pwcsUrl);
nsresult rv = NS_NewURI(getter_AddRefs(uri), url);
::CoTaskMemFree(statURL.pwcsUrl);
if (NS_FAILED(rv)) {
// Got a corrupt or invalid URI, continue to the next entry.
return HasMoreElements(_retval);
}
}
nsDependentString title(statURL.pwcsTitle ? statURL.pwcsTitle : L"");
bool lastVisitTimeIsValid;
PRTime lastVisited = WinMigrationFileTimeToPRTime(&(statURL.ftLastVisited), &lastVisitTimeIsValid);
mCachedNextEntry = do_CreateInstance("@mozilla.org/hash-property-bag;1");
MOZ_ASSERT(mCachedNextEntry, "Should have instanced a new property bag");
if (mCachedNextEntry) {
mCachedNextEntry->SetPropertyAsInterface(NS_LITERAL_STRING("uri"), uri);
mCachedNextEntry->SetPropertyAsAString(NS_LITERAL_STRING("title"), title);
if (lastVisitTimeIsValid) {
mCachedNextEntry->SetPropertyAsInt64(NS_LITERAL_STRING("time"), lastVisited);
}
*_retval = true;
}
if (statURL.pwcsTitle)
::CoTaskMemFree(statURL.pwcsTitle);
return NS_OK;
}
NS_IMETHODIMP
nsIEHistoryEnumerator::GetNext(nsISupports** _retval)
{
*_retval = nullptr;
if (!mCachedNextEntry)
return NS_ERROR_FAILURE;
NS_ADDREF(*_retval = mCachedNextEntry);
// Release the cached entry, so it can't be returned twice.
mCachedNextEntry = nullptr;
return NS_OK;
}