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