Wed, 31 Dec 2014 06:09:35 +0100
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 file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "nsIEHistoryEnumerator.h" |
michael@0 | 6 | |
michael@0 | 7 | #include <urlhist.h> |
michael@0 | 8 | #include <shlguid.h> |
michael@0 | 9 | |
michael@0 | 10 | #include "nsStringAPI.h" |
michael@0 | 11 | #include "nsNetUtil.h" |
michael@0 | 12 | #include "nsIVariant.h" |
michael@0 | 13 | #include "nsCOMArray.h" |
michael@0 | 14 | #include "nsArrayEnumerator.h" |
michael@0 | 15 | |
michael@0 | 16 | namespace { |
michael@0 | 17 | |
michael@0 | 18 | PRTime FileTimeToPRTime(FILETIME* filetime) |
michael@0 | 19 | { |
michael@0 | 20 | SYSTEMTIME st; |
michael@0 | 21 | ::FileTimeToSystemTime(filetime, &st); |
michael@0 | 22 | PRExplodedTime prt; |
michael@0 | 23 | prt.tm_year = st.wYear; |
michael@0 | 24 | // SYSTEMTIME's day-of-month parameter is 1-based, |
michael@0 | 25 | // PRExplodedTime's is 0-based. |
michael@0 | 26 | prt.tm_month = st.wMonth - 1; |
michael@0 | 27 | prt.tm_mday = st.wDay; |
michael@0 | 28 | prt.tm_hour = st.wHour; |
michael@0 | 29 | prt.tm_min = st.wMinute; |
michael@0 | 30 | prt.tm_sec = st.wSecond; |
michael@0 | 31 | prt.tm_usec = st.wMilliseconds * 1000; |
michael@0 | 32 | prt.tm_wday = 0; |
michael@0 | 33 | prt.tm_yday = 0; |
michael@0 | 34 | prt.tm_params.tp_gmt_offset = 0; |
michael@0 | 35 | prt.tm_params.tp_dst_offset = 0; |
michael@0 | 36 | return PR_ImplodeTime(&prt); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | } // Anonymous namespace. |
michael@0 | 40 | |
michael@0 | 41 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 42 | //// nsIEHistoryEnumerator |
michael@0 | 43 | |
michael@0 | 44 | NS_IMPL_ISUPPORTS(nsIEHistoryEnumerator, nsISimpleEnumerator) |
michael@0 | 45 | |
michael@0 | 46 | nsIEHistoryEnumerator::nsIEHistoryEnumerator() |
michael@0 | 47 | { |
michael@0 | 48 | ::CoInitialize(nullptr); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | nsIEHistoryEnumerator::~nsIEHistoryEnumerator() |
michael@0 | 52 | { |
michael@0 | 53 | ::CoUninitialize(); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | void |
michael@0 | 57 | nsIEHistoryEnumerator::EnsureInitialized() |
michael@0 | 58 | { |
michael@0 | 59 | if (mURLEnumerator) |
michael@0 | 60 | return; |
michael@0 | 61 | |
michael@0 | 62 | HRESULT hr = ::CoCreateInstance(CLSID_CUrlHistory, |
michael@0 | 63 | nullptr, |
michael@0 | 64 | CLSCTX_INPROC_SERVER, |
michael@0 | 65 | IID_IUrlHistoryStg2, |
michael@0 | 66 | getter_AddRefs(mIEHistory)); |
michael@0 | 67 | if (FAILED(hr)) |
michael@0 | 68 | return; |
michael@0 | 69 | |
michael@0 | 70 | hr = mIEHistory->EnumUrls(getter_AddRefs(mURLEnumerator)); |
michael@0 | 71 | if (FAILED(hr)) |
michael@0 | 72 | return; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | NS_IMETHODIMP |
michael@0 | 76 | nsIEHistoryEnumerator::HasMoreElements(bool* _retval) |
michael@0 | 77 | { |
michael@0 | 78 | *_retval = false; |
michael@0 | 79 | |
michael@0 | 80 | EnsureInitialized(); |
michael@0 | 81 | MOZ_ASSERT(mURLEnumerator, "Should have instanced an IE History URLEnumerator"); |
michael@0 | 82 | if (!mURLEnumerator) |
michael@0 | 83 | return NS_OK; |
michael@0 | 84 | |
michael@0 | 85 | STATURL statURL; |
michael@0 | 86 | ULONG fetched; |
michael@0 | 87 | |
michael@0 | 88 | // First argument is not implemented, so doesn't matter what we pass. |
michael@0 | 89 | HRESULT hr = mURLEnumerator->Next(1, &statURL, &fetched); |
michael@0 | 90 | if (FAILED(hr) || fetched != 1UL) { |
michael@0 | 91 | // Reached the last entry. |
michael@0 | 92 | return NS_OK; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | nsCOMPtr<nsIURI> uri; |
michael@0 | 96 | if (statURL.pwcsUrl) { |
michael@0 | 97 | nsDependentString url(statURL.pwcsUrl); |
michael@0 | 98 | nsresult rv = NS_NewURI(getter_AddRefs(uri), url); |
michael@0 | 99 | ::CoTaskMemFree(statURL.pwcsUrl); |
michael@0 | 100 | if (NS_FAILED(rv)) { |
michael@0 | 101 | // Got a corrupt or invalid URI, continue to the next entry. |
michael@0 | 102 | return HasMoreElements(_retval); |
michael@0 | 103 | } |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | nsDependentString title(statURL.pwcsTitle); |
michael@0 | 107 | |
michael@0 | 108 | PRTime lastVisited = FileTimeToPRTime(&(statURL.ftLastVisited)); |
michael@0 | 109 | |
michael@0 | 110 | mCachedNextEntry = do_CreateInstance("@mozilla.org/hash-property-bag;1"); |
michael@0 | 111 | MOZ_ASSERT(mCachedNextEntry, "Should have instanced a new property bag"); |
michael@0 | 112 | if (mCachedNextEntry) { |
michael@0 | 113 | mCachedNextEntry->SetPropertyAsInterface(NS_LITERAL_STRING("uri"), uri); |
michael@0 | 114 | mCachedNextEntry->SetPropertyAsAString(NS_LITERAL_STRING("title"), title); |
michael@0 | 115 | mCachedNextEntry->SetPropertyAsInt64(NS_LITERAL_STRING("time"), lastVisited); |
michael@0 | 116 | |
michael@0 | 117 | *_retval = true; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | if (statURL.pwcsTitle) |
michael@0 | 121 | ::CoTaskMemFree(statURL.pwcsTitle); |
michael@0 | 122 | |
michael@0 | 123 | return NS_OK; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | NS_IMETHODIMP |
michael@0 | 127 | nsIEHistoryEnumerator::GetNext(nsISupports** _retval) |
michael@0 | 128 | { |
michael@0 | 129 | *_retval = nullptr; |
michael@0 | 130 | |
michael@0 | 131 | if (!mCachedNextEntry) |
michael@0 | 132 | return NS_ERROR_FAILURE; |
michael@0 | 133 | |
michael@0 | 134 | NS_ADDREF(*_retval = mCachedNextEntry); |
michael@0 | 135 | // Release the cached entry, so it can't be returned twice. |
michael@0 | 136 | mCachedNextEntry = nullptr; |
michael@0 | 137 | |
michael@0 | 138 | return NS_OK; |
michael@0 | 139 | } |