• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_IMPORTER_HOST_H_
6 #define CHROME_BROWSER_IMPORTER_IMPORTER_HOST_H_
7 #pragma once
8 
9 #include "base/basictypes.h"
10 #include "base/compiler_specific.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "chrome/browser/bookmarks/base_bookmark_model_observer.h"
14 #include "chrome/browser/importer/importer_data_types.h"
15 #include "chrome/browser/importer/profile_writer.h"
16 #include "content/common/notification_observer.h"
17 #include "content/common/notification_registrar.h"
18 #include "ui/gfx/native_widget_types.h"
19 
20 class FirefoxProfileLock;
21 class Importer;
22 class Profile;
23 class Task;
24 
25 namespace importer {
26 class ImporterProgressObserver;
27 }
28 
29 // This class hosts the importers. It enumerates profiles from other
30 // browsers dynamically, and controls the process of importing. When
31 // the import process is done, ImporterHost deletes itself.
32 class ImporterHost : public base::RefCountedThreadSafe<ImporterHost>,
33                      public BaseBookmarkModelObserver,
34                      public NotificationObserver {
35  public:
36   ImporterHost();
37 
38   // ShowWarningDialog() asks user to close the application that is owning the
39   // lock. They can retry or skip the importing process.
40   void ShowWarningDialog();
41 
42   // This is called when when user ends the lock dialog by clicking on either
43   // the "Skip" or "Continue" buttons. |is_continue| is true when user clicked
44   // the "Continue" button.
45   void OnImportLockDialogEnd(bool is_continue);
46 
47   void SetObserver(importer::ImporterProgressObserver* observer);
48 
49   // A series of functions invoked at the start, during and end of the import
50   // process. The middle functions are notifications that the a harvesting of a
51   // particular source of data (specified by |item|) is under way.
52   void NotifyImportStarted();
53   void NotifyImportItemStarted(importer::ImportItem item);
54   void NotifyImportItemEnded(importer::ImportItem item);
55   void NotifyImportEnded();
56 
57   // When in headless mode, the importer will not show the warning dialog and
58   // the outcome is as if the user had canceled the import operation.
set_headless()59   void set_headless() { headless_ = true; }
is_headless()60   bool is_headless() const { return headless_; }
61 
set_parent_window(gfx::NativeWindow parent_window)62   void set_parent_window(gfx::NativeWindow parent_window) {
63     parent_window_ = parent_window;
64   }
65 
66   // Starts the process of importing the settings and data depending on what the
67   // user selected.
68   // |source_profile| - importer profile to import.
69   // |target_profile| - profile to import into.
70   // |items| - specifies which data to import (bitmask of importer::ImportItem).
71   // |writer| - called to actually write data back to the profile.
72   // |first_run| - true if this method is being called during first run.
73   virtual void StartImportSettings(
74       const importer::SourceProfile& source_profile,
75       Profile* target_profile,
76       uint16 items,
77       ProfileWriter* writer,
78       bool first_run);
79 
80   // Cancels the import process.
81   virtual void Cancel();
82 
83  protected:
84   ~ImporterHost();
85 
86   // Returns true if importer should import to bookmark bar.
87   bool ShouldImportToBookmarkBar(bool first_run);
88 
89   // Make sure that Firefox isn't running, if import browser is Firefox. Show
90   // to the user a dialog that notifies that is necessary to close Firefox
91   // prior to continue.
92   // |source_profile| - importer profile to import.
93   // |items| - specifies which data to import (bitmask of importer::ImportItem).
94   // |first_run| - true if this method is being called during first run.
95   void CheckForFirefoxLock(const importer::SourceProfile& source_profile,
96                            uint16 items,
97                            bool first_run);
98 
99   // Make sure BookmarkModel and TemplateURLModel are loaded before import
100   // process starts, if bookmarks and/or search engines are among the items
101   // which are to be imported.
102   void CheckForLoadedModels(uint16 items);
103 
104   // Profile we're importing from.
105   Profile* profile_;
106 
107   // TODO(mirandac): |task_| and |importer_| should be private. Can't just put
108   // them there without changing the order of construct/destruct, so do this
109   // after main CL has been committed.
110   // The task is the process of importing settings from other browsers.
111   Task* task_;
112 
113   // The importer used in the task.
114   Importer* importer_;
115 
116   // True if we're waiting for the model to finish loading.
117   bool waiting_for_bookmarkbar_model_;
118 
119   // Have we installed a listener on the bookmark model?
120   bool installed_bookmark_observer_;
121 
122   // True if source profile is readable.
123   bool is_source_readable_;
124 
125   // Receives notification when the TemplateURLModel has loaded.
126   NotificationRegistrar registrar_;
127 
128   // Writes data from the importer back to the profile.
129   scoped_refptr<ProfileWriter> writer_;
130 
131  private:
132   friend class base::RefCountedThreadSafe<ImporterHost>;
133 
134   // Launches the thread that starts the import task, unless bookmark or
135   // template model are not yet loaded. If load is not detected, this method
136   // will be called when the loading observer sees that model loading is
137   // complete.
138   virtual void InvokeTaskIfDone();
139 
140   // BaseBookmarkModelObserver:
141   virtual void Loaded(BookmarkModel* model) OVERRIDE;
142   virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE;
143   virtual void BookmarkModelChanged() OVERRIDE;
144 
145   // NotificationObserver:
146   // Called when TemplateURLModel has been loaded.
147   virtual void Observe(NotificationType type,
148                        const NotificationSource& source,
149                        const NotificationDetails& details) OVERRIDE;
150 
151   // True if UI is not to be shown.
152   bool headless_;
153 
154   // Parent window that we pass to the import lock dialog (i.e, the Firefox
155   // warning dialog).
156   gfx::NativeWindow parent_window_;
157 
158   // The observer that we need to notify about changes in the import process.
159   importer::ImporterProgressObserver* observer_;
160 
161   // Firefox profile lock.
162   scoped_ptr<FirefoxProfileLock> firefox_lock_;
163 
164   DISALLOW_COPY_AND_ASSIGN(ImporterHost);
165 };
166 
167 #endif  // CHROME_BROWSER_IMPORTER_IMPORTER_HOST_H_
168