Home | History | Annotate | Download | only in importer
      1 // Copyright (c) 2011 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 #pragma once
      8 
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/time.h"
     14 #include "build/build_config.h"
     15 #include "chrome/browser/history/history_types.h"
     16 #include "googleurl/src/gurl.h"
     17 
     18 class BookmarkModel;
     19 class Profile;
     20 class TemplateURL;
     21 
     22 namespace webkit_glue {
     23 struct PasswordForm;
     24 }
     25 
     26 #if defined(OS_WIN)
     27 struct IE7PasswordInfo;
     28 #endif
     29 
     30 // ProfileWriter encapsulates profile for writing entries into it.
     31 // This object must be invoked on UI thread.
     32 class ProfileWriter : public base::RefCountedThreadSafe<ProfileWriter> {
     33  public:
     34   // Used to identify how the bookmarks are added.
     35   enum BookmarkOptions {
     36     // Indicates the bookmark should only be added if unique. Uniqueness
     37     // is done by title, url and path. That is, if this is passed to
     38     // AddBookmarkEntry the bookmark is added only if there is no other
     39     // URL with the same url, path and title.
     40     ADD_IF_UNIQUE = 1 << 0,
     41 
     42     // Indicates the bookmarks should be added to the bookmark bar.
     43     IMPORT_TO_BOOKMARK_BAR = 1 << 1,
     44 
     45     // Indicates the bookmark bar is not shown.
     46     BOOKMARK_BAR_DISABLED = 1 << 2
     47   };
     48 
     49   struct BookmarkEntry {
     50     BookmarkEntry();
     51     ~BookmarkEntry();
     52 
     53     bool in_toolbar;
     54     bool is_folder;
     55     GURL url;
     56     std::vector<string16> path;
     57     string16 title;
     58     base::Time creation_time;
     59   };
     60 
     61   explicit ProfileWriter(Profile* profile);
     62 
     63   // These functions return true if the corresponding model has been loaded.
     64   // If the models haven't been loaded, the importer waits to run until they've
     65   // completed.
     66   virtual bool BookmarkModelIsLoaded() const;
     67   virtual bool TemplateURLModelIsLoaded() const;
     68 
     69   // Helper methods for adding data to local stores.
     70   virtual void AddPasswordForm(const webkit_glue::PasswordForm& form);
     71 
     72 #if defined(OS_WIN)
     73   virtual void AddIE7PasswordInfo(const IE7PasswordInfo& info);
     74 #endif
     75 
     76   virtual void AddHistoryPage(const std::vector<history::URLRow>& page,
     77                               history::VisitSource visit_source);
     78 
     79   virtual void AddHomepage(const GURL& homepage);
     80 
     81   // Adds the bookmarks to the BookmarkModel.
     82   // |options| is a bitmask of BookmarkOptions and dictates how and
     83   // which bookmarks are added. If the bitmask contains IMPORT_TO_BOOKMARK_BAR,
     84   // then any entries with a value of true for in_toolbar are added to
     85   // the bookmark bar. If the bitmask does not contain IMPORT_TO_BOOKMARK_BAR
     86   // then the folder name the bookmarks are added to is uniqued based on
     87   // |first_folder_name|. For example, if |first_folder_name| is 'foo'
     88   // and a folder with the name 'foo' already exists in the other
     89   // bookmarks folder, then the folder name 'foo 2' is used.
     90   // If |options| contains ADD_IF_UNIQUE, then the bookmark is added only
     91   // if another bookmarks does not exist with the same title, path and
     92   // url.
     93   virtual void AddBookmarkEntry(const std::vector<BookmarkEntry>& bookmark,
     94                                 const string16& first_folder_name,
     95                                 int options);
     96 
     97   virtual void AddFavicons(
     98       const std::vector<history::ImportedFaviconUsage>& favicons);
     99 
    100   // Add the TemplateURLs in |template_urls| to the local store and make the
    101   // TemplateURL at |default_keyword_index| the default keyword (does not set
    102   // a default keyword if it is -1).  The local store becomes the owner of the
    103   // TemplateURLs.  Some TemplateURLs in |template_urls| may conflict (same
    104   // keyword or same host name in the URL) with existing TemplateURLs in the
    105   // local store, in which case the existing ones takes precedence and the
    106   // duplicate in |template_urls| are deleted.
    107   // If unique_on_host_and_path a TemplateURL is only added if there is not an
    108   // existing TemplateURL that has a replaceable search url with the same
    109   // host+path combination.
    110 
    111   virtual void AddKeywords(const std::vector<TemplateURL*>& template_urls,
    112                            int default_keyword_index,
    113                            bool unique_on_host_and_path);
    114 
    115   // Shows the bookmarks toolbar.
    116   void ShowBookmarkBar();
    117 
    118  protected:
    119   friend class base::RefCountedThreadSafe<ProfileWriter>;
    120 
    121   virtual ~ProfileWriter();
    122 
    123  private:
    124   // Generates a unique folder name. If folder_name is not unique, then this
    125   // repeatedly tests for '|folder_name| + (i)' until a unique name is found.
    126   string16 GenerateUniqueFolderName(BookmarkModel* model,
    127                                     const string16& folder_name);
    128 
    129   // Returns true if a bookmark exists with the same url, title and path
    130   // as |entry|. |first_folder_name| is the name to use for the first
    131   // path entry if |import_to_bookmark_bar| is true.
    132   bool DoesBookmarkExist(BookmarkModel* model,
    133                          const BookmarkEntry& entry,
    134                          const string16& first_folder_name,
    135                          bool import_to_bookmark_bar);
    136 
    137   Profile* const profile_;
    138 
    139   DISALLOW_COPY_AND_ASSIGN(ProfileWriter);
    140 };
    141 
    142 #endif  // CHROME_BROWSER_IMPORTER_PROFILE_WRITER_H_
    143