summaryrefslogtreecommitdiffstats
path: root/chrome/browser/importer/ie_importer.cc
blob: 2d6d319ad59d58a3df186a7b4a2d6ed3a9944fd4 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/importer/ie_importer.h"

#include <ole2.h>
#include <intshcut.h>
#include <pstore.h>
#include <shlobj.h>
#include <urlhist.h>

#include <algorithm>
#include <map>
#include <string>
#include <vector>

#include "app/l10n_util.h"
#include "app/win_util.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/registry.h"
#include "base/scoped_comptr_win.h"
#include "base/string_split.h"
#include "base/string_util.h"
#include "base/time.h"
#include "base/values.h"
#include "base/utf_string_conversions.h"
#include "base/win/windows_version.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/importer/importer_bridge.h"
#include "chrome/browser/importer/importer_data_types.h"
#include "chrome/browser/password_manager/ie7_password.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_model.h"
#include "chrome/common/time_format.h"
#include "chrome/common/url_constants.h"
#include "googleurl/src/gurl.h"
#include "grit/generated_resources.h"
#include "webkit/glue/password_form.h"

using base::Time;
using webkit_glue::PasswordForm;

namespace {

// Gets the creation time of the given file or directory.
static Time GetFileCreationTime(const std::wstring& file) {
  Time creation_time;
  ScopedHandle file_handle(
      CreateFile(file.c_str(), GENERIC_READ,
                 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                 NULL, OPEN_EXISTING,
                 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL));
  FILETIME creation_filetime;
  if (GetFileTime(file_handle, &creation_filetime, NULL, NULL))
    creation_time = Time::FromFileTime(creation_filetime);
  return creation_time;
}

}  // namespace

// static
// {E161255A-37C3-11D2-BCAA-00C04fD929DB}
const GUID IEImporter::kPStoreAutocompleteGUID = {0xe161255a, 0x37c3, 0x11d2,
    {0xbc, 0xaa, 0x00, 0xc0, 0x4f, 0xd9, 0x29, 0xdb}};
// {A79029D6-753E-4e27-B807-3D46AB1545DF}
const GUID IEImporter::kUnittestGUID = { 0xa79029d6, 0x753e, 0x4e27,
    {0xb8, 0x7, 0x3d, 0x46, 0xab, 0x15, 0x45, 0xdf}};

void IEImporter::StartImport(ProfileInfo profile_info,
                             uint16 items,
                             ImporterBridge* bridge) {
  bridge_ = bridge;
  source_path_ = profile_info.source_path.ToWStringHack();

  bridge_->NotifyStarted();

  // Some IE settings (such as Protected Storage) are obtained via COM APIs.
  win_util::ScopedCOMInitializer com_initializer;

  if ((items & importer::HOME_PAGE) && !cancelled())
    ImportHomepage();  // Doesn't have a UI item.
  // The order here is important!
  if ((items & importer::HISTORY) && !cancelled()) {
    bridge_->NotifyItemStarted(importer::HISTORY);
    ImportHistory();
    bridge_->NotifyItemEnded(importer::HISTORY);
  }
  if ((items & importer::FAVORITES) && !cancelled()) {
    bridge_->NotifyItemStarted(importer::FAVORITES);
    ImportFavorites();
    bridge_->NotifyItemEnded(importer::FAVORITES);
  }
  if ((items & importer::SEARCH_ENGINES) && !cancelled()) {
    bridge_->NotifyItemStarted(importer::SEARCH_ENGINES);
    ImportSearchEngines();
    bridge_->NotifyItemEnded(importer::SEARCH_ENGINES);
  }
  if ((items & importer::PASSWORDS) && !cancelled()) {
    bridge_->NotifyItemStarted(importer::PASSWORDS);
    // Always import IE6 passwords.
    ImportPasswordsIE6();

    if (CurrentIEVersion() >= 7)
      ImportPasswordsIE7();
    bridge_->NotifyItemEnded(importer::PASSWORDS);
  }
  bridge_->NotifyEnded();
}

void IEImporter::ImportFavorites() {
  std::wstring path;

  FavoritesInfo info;
  if (!GetFavoritesInfo(&info))
    return;

  BookmarkVector bookmarks;
  ParseFavoritesFolder(info, &bookmarks);

  if (!bookmarks.empty() && !cancelled()) {
    const std::wstring& first_folder_name =
        l10n_util::GetString(IDS_BOOKMARK_GROUP_FROM_IE);
    int options = 0;
    if (import_to_bookmark_bar())
      options = ProfileWriter::IMPORT_TO_BOOKMARK_BAR;
    bridge_->AddBookmarkEntries(bookmarks, first_folder_name, options);
  }
}

void IEImporter::ImportPasswordsIE6() {
  GUID AutocompleteGUID = kPStoreAutocompleteGUID;
  if (!source_path_.empty()) {
    // We supply a fake GUID for testting.
    AutocompleteGUID = kUnittestGUID;
  }

  // The PStoreCreateInstance function retrieves an interface pointer
  // to a storage provider. But this function has no associated import
  // library or header file, we must call it using the LoadLibrary()
  // and GetProcAddress() functions.
  typedef HRESULT (WINAPI *PStoreCreateFunc)(IPStore**, DWORD, DWORD, DWORD);
  HMODULE pstorec_dll = LoadLibrary(L"pstorec.dll");
  if (!pstorec_dll)
    return;
  PStoreCreateFunc PStoreCreateInstance =
      (PStoreCreateFunc)GetProcAddress(pstorec_dll, "PStoreCreateInstance");
  if (!PStoreCreateInstance) {
    FreeLibrary(pstorec_dll);
    return;
  }

  ScopedComPtr<IPStore, &IID_IPStore> pstore;
  HRESULT result = PStoreCreateInstance(pstore.Receive(), 0, 0, 0);
  if (result != S_OK) {
    FreeLibrary(pstorec_dll);
    return;
  }

  std::vector<AutoCompleteInfo> ac_list;

  // Enumerates AutoComplete items in the protected database.
  ScopedComPtr<IEnumPStoreItems, &IID_IEnumPStoreItems> item;
  result = pstore->EnumItems(0, &AutocompleteGUID,
                             &AutocompleteGUID, 0, item.Receive());
  if (result != PST_E_OK) {
    pstore.Release();
    FreeLibrary(pstorec_dll);
    return;
  }

  wchar_t* item_name;
  while (!cancelled() && SUCCEEDED(item->Next(1, &item_name, 0))) {
    DWORD length = 0;
    unsigned char* buffer = NULL;
    result = pstore->ReadItem(0, &AutocompleteGUID, &AutocompleteGUID,
                              item_name, &length, &buffer, NULL, 0);
    if (SUCCEEDED(result)) {
      AutoCompleteInfo ac;
      ac.key = item_name;
      std::wstring data;
      data.insert(0, reinterpret_cast<wchar_t*>(buffer),
                  length / sizeof(wchar_t));

      // The key name is always ended with ":StringData".
      const wchar_t kDataSuffix[] = L":StringData";
      size_t i = ac.key.rfind(kDataSuffix);
      if (i != std::wstring::npos && ac.key.substr(i) == kDataSuffix) {
        ac.key.erase(i);
        ac.is_url = (ac.key.find(L"://") != std::wstring::npos);
        ac_list.push_back(ac);
        base::SplitString(data, L'\0', &ac_list[ac_list.size() - 1].data);
      }
      CoTaskMemFree(buffer);
    }
    CoTaskMemFree(item_name);
  }
  // Releases them before unload the dll.
  item.Release();
  pstore.Release();
  FreeLibrary(pstorec_dll);

  size_t i;
  for (i = 0; i < ac_list.size(); i++) {
    if (!ac_list[i].is_url || ac_list[i].data.size() < 2)
      continue;

    GURL url(ac_list[i].key.c_str());
    if (!(LowerCaseEqualsASCII(url.scheme(), chrome::kHttpScheme) ||
        LowerCaseEqualsASCII(url.scheme(), chrome::kHttpsScheme))) {
      continue;
    }

    PasswordForm form;
    GURL::Replacements rp;
    rp.ClearUsername();
    rp.ClearPassword();
    rp.ClearQuery();
    rp.ClearRef();
    form.origin = url.ReplaceComponents(rp);
    form.username_value = ac_list[i].data[0];
    form.password_value = ac_list[i].data[1];
    form.signon_realm = url.GetOrigin().spec();

    // This is not precise, because a scheme of https does not imply a valid
    // certificate was presented; however we assign it this way so that if we
    // import a password from IE whose scheme is https, we give it the benefit
    // of the doubt and DONT auto-fill it unless the form appears under
    // valid SSL conditions.
    form.ssl_valid = url.SchemeIsSecure();

    // Goes through the list to find out the username field
    // of the web page.
    size_t list_it, item_it;
    for (list_it = 0; list_it < ac_list.size(); ++list_it) {
      if (ac_list[list_it].is_url)
        continue;

      for (item_it = 0; item_it < ac_list[list_it].data.size(); ++item_it)
        if (ac_list[list_it].data[item_it] == form.username_value) {
          form.username_element = ac_list[list_it].key;
          break;
        }
    }

    bridge_->SetPasswordForm(form);
  }
}

void IEImporter::ImportPasswordsIE7() {
  if (!source_path_.empty()) {
    // We have been called from the unit tests. Don't import real passwords.
    return;
  }

  const wchar_t kStorage2Path[] =
      L"Software\\Microsoft\\Internet Explorer\\IntelliForms\\Storage2";

  RegKey key(HKEY_CURRENT_USER, kStorage2Path, KEY_READ);
  RegistryValueIterator reg_iterator(HKEY_CURRENT_USER, kStorage2Path);
  while (reg_iterator.Valid() && !cancelled()) {
    // Get the size of the encrypted data.
    DWORD value_len = 0;
    if (key.ReadValue(reg_iterator.Name(), NULL, &value_len, NULL) &&
        value_len) {
      // Query the encrypted data.
      std::vector<unsigned char> value;
      value.resize(value_len);
      if (key.ReadValue(reg_iterator.Name(), &value.front(), &value_len,
                        NULL)) {
        IE7PasswordInfo password_info;
        password_info.url_hash = reg_iterator.Name();
        password_info.encrypted_data = value;
        password_info.date_created = Time::Now();

        bridge_->AddIE7PasswordInfo(password_info);
      }
    }

    ++reg_iterator;
  }
}

// Reads history information from COM interface.
void IEImporter::ImportHistory() {
  const std::string kSchemes[] = {chrome::kHttpScheme,
                                  chrome::kHttpsScheme,
                                  chrome::kFtpScheme,
                                  chrome::kFileScheme};
  int total_schemes = arraysize(kSchemes);

  ScopedComPtr<IUrlHistoryStg2> url_history_stg2;
  HRESULT result;
  result = url_history_stg2.CreateInstance(CLSID_CUrlHistory, NULL,
                                           CLSCTX_INPROC_SERVER);
  if (FAILED(result))
    return;
  ScopedComPtr<IEnumSTATURL> enum_url;
  if (SUCCEEDED(result = url_history_stg2->EnumUrls(enum_url.Receive()))) {
    std::vector<history::URLRow> rows;
    STATURL stat_url;
    ULONG fetched;
    while (!cancelled() &&
           (result = enum_url->Next(1, &stat_url, &fetched)) == S_OK) {
      std::wstring url_string;
      std::wstring title_string;
      if (stat_url.pwcsUrl) {
        url_string = stat_url.pwcsUrl;
        CoTaskMemFree(stat_url.pwcsUrl);
      }
      if (stat_url.pwcsTitle) {
        title_string = stat_url.pwcsTitle;
        CoTaskMemFree(stat_url.pwcsTitle);
      }

      GURL url(url_string);
      // Skips the URLs that are invalid or have other schemes.
      if (!url.is_valid() ||
          (std::find(kSchemes, kSchemes + total_schemes, url.scheme()) ==
           kSchemes + total_schemes))
        continue;

      history::URLRow row(url);
      row.set_title(title_string);
      row.set_last_visit(Time::FromFileTime(stat_url.ftLastVisited));
      if (stat_url.dwFlags == STATURL_QUERYFLAG_TOPLEVEL) {
        row.set_visit_count(1);
        row.set_hidden(false);
      } else {
        row.set_hidden(true);
      }

      rows.push_back(row);
    }

    if (!rows.empty() && !cancelled()) {
      bridge_->SetHistoryItems(rows, history::SOURCE_IE_IMPORTED);
    }
  }
}

void IEImporter::ImportSearchEngines() {
  // On IE, search engines are stored in the registry, under:
  // Software\Microsoft\Internet Explorer\SearchScopes
  // Each key represents a search engine. The URL value contains the URL and
  // the DisplayName the name.
  // The default key's name is contained under DefaultScope.
  const wchar_t kSearchScopePath[] =
      L"Software\\Microsoft\\Internet Explorer\\SearchScopes";

  RegKey key(HKEY_CURRENT_USER, kSearchScopePath, KEY_READ);
  std::wstring default_search_engine_name;
  const TemplateURL* default_search_engine = NULL;
  std::map<std::string, TemplateURL*> search_engines_map;
  key.ReadValue(L"DefaultScope", &default_search_engine_name);
  RegistryKeyIterator key_iterator(HKEY_CURRENT_USER, kSearchScopePath);
  while (key_iterator.Valid()) {
    std::wstring sub_key_name = kSearchScopePath;
    sub_key_name.append(L"\\").append(key_iterator.Name());
    RegKey sub_key(HKEY_CURRENT_USER, sub_key_name.c_str(), KEY_READ);
    std::wstring wide_url;
    if (!sub_key.ReadValue(L"URL", &wide_url) || wide_url.empty()) {
      LOG(INFO) << "No URL for IE search engine at " << key_iterator.Name();
      ++key_iterator;
      continue;
    }
    // For the name, we try the default value first (as Live Search uses a
    // non displayable name in DisplayName, and the readable name under the
    // default value).
    std::wstring name;
    if (!sub_key.ReadValue(NULL, &name) || name.empty()) {
      // Try the displayable name.
      if (!sub_key.ReadValue(L"DisplayName", &name) || name.empty()) {
        LOG(INFO) << "No name for IE search engine at " << key_iterator.Name();
        ++key_iterator;
        continue;
      }
    }

    std::string url(WideToUTF8(wide_url));
    std::map<std::string, TemplateURL*>::iterator t_iter =
        search_engines_map.find(url);
    TemplateURL* template_url =
        (t_iter != search_engines_map.end()) ? t_iter->second : NULL;
    if (!template_url) {
      // First time we see that URL.
      template_url = new TemplateURL();
      template_url->set_short_name(name);
      template_url->SetURL(url, 0, 0);
      // Give this a keyword to facilitate tab-to-search, if possible.
      template_url->set_keyword(TemplateURLModel::GenerateKeyword(GURL(url),
                                                                  false));
      template_url->set_show_in_default_list(true);
      search_engines_map[url] = template_url;
    }
    if (template_url && key_iterator.Name() == default_search_engine_name) {
      DCHECK(!default_search_engine);
      default_search_engine = template_url;
    }
    ++key_iterator;
  }

  // ProfileWriter::AddKeywords() requires a vector and we have a map.
  std::map<std::string, TemplateURL*>::iterator t_iter;
  std::vector<TemplateURL*> search_engines;
  int default_search_engine_index = -1;
  for (t_iter = search_engines_map.begin(); t_iter != search_engines_map.end();
       ++t_iter) {
    search_engines.push_back(t_iter->second);
    if (default_search_engine == t_iter->second) {
      default_search_engine_index =
          static_cast<int>(search_engines.size()) - 1;
    }
  }
  bridge_->SetKeywords(search_engines, default_search_engine_index, true);
}

void IEImporter::ImportHomepage() {
  const wchar_t kIESettingsMain[] =
      L"Software\\Microsoft\\Internet Explorer\\Main";
  const wchar_t kIEHomepage[] = L"Start Page";
  const wchar_t kIEDefaultHomepage[] = L"Default_Page_URL";

  RegKey key(HKEY_CURRENT_USER, kIESettingsMain, KEY_READ);
  std::wstring homepage_url;
  if (!key.ReadValue(kIEHomepage, &homepage_url) || homepage_url.empty())
    return;

  GURL homepage = GURL(homepage_url);
  if (!homepage.is_valid())
    return;

  // Check to see if this is the default website and skip import.
  RegKey keyDefault(HKEY_LOCAL_MACHINE, kIESettingsMain, KEY_READ);
  std::wstring default_homepage_url;
  if (keyDefault.ReadValue(kIEDefaultHomepage, &default_homepage_url) &&
      !default_homepage_url.empty()) {
    if (homepage.spec() == GURL(default_homepage_url).spec())
      return;
  }

  bridge_->AddHomePage(homepage);
}

bool IEImporter::GetFavoritesInfo(IEImporter::FavoritesInfo *info) {
  if (!source_path_.empty()) {
    // Source path exists during testing.
    info->path = source_path_;
    file_util::AppendToPath(&info->path, L"Favorites");
    info->links_folder = L"Links";
    return true;
  }

  // IE stores the favorites in the Favorites under user profile's folder.
  wchar_t buffer[MAX_PATH];
  if (FAILED(SHGetFolderPath(NULL, CSIDL_FAVORITES, NULL,
                             SHGFP_TYPE_CURRENT, buffer)))
    return false;
  info->path = buffer;

  // There is a Links folder under Favorites folder in Windows Vista, but it
  // is not recording in Vista's registry. So in Vista, we assume the Links
  // folder is under Favorites folder since it looks like there is not name
  // different in every language version of Windows Vista.
  if (base::win::GetVersion() < base::win::VERSION_VISTA) {
    // The Link folder name is stored in the registry.
    DWORD buffer_length = sizeof(buffer);
    RegKey reg_key(HKEY_CURRENT_USER,
                   L"Software\\Microsoft\\Internet Explorer\\Toolbar",
                   KEY_READ);
    if (!reg_key.ReadValue(L"LinksFolderName", buffer, &buffer_length, NULL))
      return false;
    info->links_folder = buffer;
  } else {
    info->links_folder = L"Links";
  }

  return true;
}

void IEImporter::ParseFavoritesFolder(const FavoritesInfo& info,
                                      BookmarkVector* bookmarks) {
  std::wstring ie_folder = l10n_util::GetString(IDS_BOOKMARK_GROUP_FROM_IE);
  BookmarkVector toolbar_bookmarks;
  FilePath file;
  std::vector<FilePath::StringType> file_list;
  FilePath favorites_path(info.path);
  // Favorites path length.  Make sure it doesn't include the trailing \.
  size_t favorites_path_len =
      favorites_path.StripTrailingSeparators().value().size();
  file_util::FileEnumerator file_enumerator(
      favorites_path, true, file_util::FileEnumerator::FILES);
  while (!(file = file_enumerator.Next()).value().empty() && !cancelled())
    file_list.push_back(file.value());

  // Keep the bookmarks in alphabetical order.
  std::sort(file_list.begin(), file_list.end());

  for (std::vector<FilePath::StringType>::iterator it = file_list.begin();
       it != file_list.end(); ++it) {
    FilePath shortcut(*it);
    if (!LowerCaseEqualsASCII(shortcut.Extension(), ".url"))
      continue;

    // Skip the bookmark with invalid URL.
    GURL url = GURL(ResolveInternetShortcut(*it));
    if (!url.is_valid())
      continue;

    // Make the relative path from the Favorites folder, without the basename.
    // ex. Suppose that the Favorites folder is C:\Users\Foo\Favorites.
    //   C:\Users\Foo\Favorites\Foo.url -> ""
    //   C:\Users\Foo\Favorites\Links\Bar\Baz.url -> "Links\Bar"
    FilePath::StringType relative_string =
        shortcut.DirName().value().substr(favorites_path_len);
    if (relative_string.size() > 0 && FilePath::IsSeparator(relative_string[0]))
      relative_string = relative_string.substr(1);
    FilePath relative_path(relative_string);

    ProfileWriter::BookmarkEntry entry;
    // Remove the dot, the file extension, and the directory path.
    entry.title = shortcut.RemoveExtension().BaseName().value();
    entry.url = url;
    entry.creation_time = GetFileCreationTime(*it);
    if (!relative_path.empty())
      relative_path.GetComponents(&entry.path);

    // Flatten the bookmarks in Link folder onto bookmark toolbar. Otherwise,
    // put it into "Other bookmarks".
    if (import_to_bookmark_bar() &&
        (entry.path.size() > 0 && entry.path[0] == info.links_folder)) {
      entry.in_toolbar = true;
      entry.path.erase(entry.path.begin());
      toolbar_bookmarks.push_back(entry);
    } else {
      // We put the bookmarks in a "Imported From IE"
      // folder, so that we don't mess up the "Other bookmarks".
      if (!import_to_bookmark_bar())
        entry.path.insert(entry.path.begin(), ie_folder);
      bookmarks->push_back(entry);
    }
  }
  bookmarks->insert(bookmarks->begin(), toolbar_bookmarks.begin(),
                    toolbar_bookmarks.end());
}

std::wstring IEImporter::ResolveInternetShortcut(const std::wstring& file) {
  win_util::CoMemReleaser<wchar_t> url;
  ScopedComPtr<IUniformResourceLocator> url_locator;
  HRESULT result = url_locator.CreateInstance(CLSID_InternetShortcut, NULL,
                                              CLSCTX_INPROC_SERVER);
  if (FAILED(result))
    return std::wstring();

  ScopedComPtr<IPersistFile> persist_file;
  result = persist_file.QueryFrom(url_locator);
  if (FAILED(result))
    return std::wstring();

  // Loads the Internet Shortcut from persistent storage.
  result = persist_file->Load(file.c_str(), STGM_READ);
  if (FAILED(result))
    return std::wstring();

  result = url_locator->GetURL(&url);
  // GetURL can return S_FALSE (FAILED(S_FALSE) is false) when url == NULL.
  if (FAILED(result) || (url == NULL))
    return std::wstring();

  return std::wstring(url);
}

int IEImporter::CurrentIEVersion() const {
  static int version = -1;
  if (version < 0) {
    wchar_t buffer[128];
    DWORD buffer_length = sizeof(buffer);
    RegKey reg_key(HKEY_LOCAL_MACHINE,
                   L"Software\\Microsoft\\Internet Explorer", KEY_READ);
    bool result = reg_key.ReadValue(L"Version", buffer, &buffer_length, NULL);
    version = (result ? _wtoi(buffer) : 0);
  }
  return version;
}