File: nsIEHistoryEnumerator.cpp

package info (click to toggle)
firefox-esr 68.10.0esr-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,143,932 kB
  • sloc: cpp: 5,227,879; javascript: 4,315,531; ansic: 2,467,042; python: 794,975; java: 349,993; asm: 232,034; xml: 228,320; sh: 82,008; lisp: 41,202; makefile: 22,347; perl: 15,555; objc: 5,277; cs: 4,725; yacc: 1,778; ada: 1,681; pascal: 1,673; lex: 1,417; exp: 527; php: 436; ruby: 225; awk: 162; sed: 53; csh: 44
file content (117 lines) | stat: -rw-r--r-- 3,427 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* 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;

  EnsureInitialized();
  MOZ_ASSERT(mURLEnumerator,
             "Should have instanced an IE History URLEnumerator");
  if (!mURLEnumerator) return NS_OK;

  if (!mCachedNextEntry) {
    bool hasMore = false;
    nsresult rv = this->HasMoreElements(&hasMore);
    if (NS_FAILED(rv)) {
      return rv;
    }
    if (!hasMore) {
      return NS_ERROR_FAILURE;
    }
  }

  NS_ADDREF(*_retval = mCachedNextEntry);
  // Release the cached entry, so it can't be returned twice.
  mCachedNextEntry = nullptr;

  return NS_OK;
}