1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_IMPORTER_PROFILE_WRITER_H_
6#define CHROME_BROWSER_IMPORTER_PROFILE_WRITER_H_
7
8#include <vector>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_vector.h"
13#include "base/strings/string16.h"
14#include "base/time/time.h"
15#include "build/build_config.h"
16#include "components/history/core/browser/history_types.h"
17#include "url/gurl.h"
18
19struct ImportedBookmarkEntry;
20struct ImportedFaviconUsage;
21class Profile;
22class TemplateURL;
23
24namespace autofill {
25struct PasswordForm;
26class AutofillEntry;
27}
28
29#if defined(OS_WIN)
30struct IE7PasswordInfo;
31#endif
32
33// ProfileWriter encapsulates profile for writing entries into it.
34// This object must be invoked on UI thread.
35class ProfileWriter : public base::RefCountedThreadSafe<ProfileWriter> {
36 public:
37  explicit ProfileWriter(Profile* profile);
38
39  // These functions return true if the corresponding model has been loaded.
40  // If the models haven't been loaded, the importer waits to run until they've
41  // completed.
42  virtual bool BookmarkModelIsLoaded() const;
43  virtual bool TemplateURLServiceIsLoaded() const;
44
45  // Helper methods for adding data to local stores.
46  virtual void AddPasswordForm(const autofill::PasswordForm& form);
47
48#if defined(OS_WIN)
49  virtual void AddIE7PasswordInfo(const IE7PasswordInfo& info);
50#endif
51
52  virtual void AddHistoryPage(const history::URLRows& page,
53                              history::VisitSource visit_source);
54
55  virtual void AddHomepage(const GURL& homepage);
56
57  // Adds the |bookmarks| to the bookmark model.
58  //
59  // (a) If the bookmarks bar is empty:
60  //     (i) If |bookmarks| includes at least one bookmark that was originally
61  //         located in a toolbar, all such bookmarks are imported directly to
62  //         the toolbar; any other bookmarks are imported to a subfolder in
63  //         the toolbar.
64  //     (i) If |bookmarks| includes no bookmarks that were originally located
65  //         in a toolbar, all bookmarks are imported directly to the toolbar.
66  // (b) If the bookmarks bar is not empty, all bookmarks are imported to a
67  //     subfolder in the toolbar.
68  //
69  // In either case, if a subfolder is created, the name will be the value of
70  // |top_level_folder_name|, unless a folder with this name already exists.
71  // If a folder with this name already exists, then the name is uniquified.
72  // For example, if |first_folder_name| is 'Imported from IE' and a folder with
73  // the name 'Imported from IE' already exists in the bookmarks toolbar, then
74  // we will instead create a subfolder named 'Imported from IE (1)'.
75  virtual void AddBookmarks(
76      const std::vector<ImportedBookmarkEntry>& bookmarks,
77      const base::string16& top_level_folder_name);
78
79  virtual void AddFavicons(
80      const std::vector<ImportedFaviconUsage>& favicons);
81
82  // Adds the TemplateURLs in |template_urls| to the local store.  The local
83  // store becomes the owner of the TemplateURLs.  Some TemplateURLs in
84  // |template_urls| may conflict (same keyword or same host name in the URL)
85  // with existing TemplateURLs in the local store, in which case the existing
86  // ones take precedence and the duplicates in |template_urls| are deleted.
87  // If |unique_on_host_and_path| is true, a TemplateURL is only added if there
88  // is not an existing TemplateURL that has a replaceable search url with the
89  // same host+path combination.
90  virtual void AddKeywords(ScopedVector<TemplateURL> template_urls,
91                           bool unique_on_host_and_path);
92
93  // Adds the imported autofill entries to the autofill database.
94  virtual void AddAutofillFormDataEntries(
95      const std::vector<autofill::AutofillEntry>& autofill_entries);
96
97 protected:
98  friend class base::RefCountedThreadSafe<ProfileWriter>;
99
100  virtual ~ProfileWriter();
101
102 private:
103  Profile* const profile_;
104
105  DISALLOW_COPY_AND_ASSIGN(ProfileWriter);
106};
107
108#endif  // CHROME_BROWSER_IMPORTER_PROFILE_WRITER_H_
109