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_EXTERNAL_PROCESS_IMPORTER_CLIENT_H_
6#define CHROME_BROWSER_IMPORTER_EXTERNAL_PROCESS_IMPORTER_CLIENT_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/compiler_specific.h"
13#include "base/memory/weak_ptr.h"
14#include "base/strings/string16.h"
15#include "chrome/browser/history/history_types.h"
16#include "chrome/common/importer/importer_data_types.h"
17#include "chrome/common/importer/importer_url_row.h"
18#include "content/public/browser/browser_thread.h"
19#include "content/public/browser/utility_process_host_client.h"
20
21class ExternalProcessImporterHost;
22struct ImportedBookmarkEntry;
23struct ImportedFaviconUsage;
24class InProcessImporterBridge;
25
26namespace content {
27struct PasswordForm;
28class UtilityProcessHost;
29}
30
31namespace importer {
32#if defined(OS_WIN)
33struct ImporterIE7PasswordInfo;
34#endif
35struct URLKeywordInfo;
36}
37
38// This class is the client for the out of process profile importing.  It
39// collects notifications from this process host and feeds data back to the
40// importer host, who actually does the writing.
41class ExternalProcessImporterClient : public content::UtilityProcessHostClient {
42 public:
43  ExternalProcessImporterClient(ExternalProcessImporterHost* importer_host,
44                                const importer::SourceProfile& source_profile,
45                                uint16 items,
46                                InProcessImporterBridge* bridge);
47
48  // Launches the task to start the external process.
49  void Start();
50
51  // Called by the ExternalProcessImporterHost on import cancel.
52  void Cancel();
53
54  // UtilityProcessHostClient implementation:
55  virtual void OnProcessCrashed(int exit_code) OVERRIDE;
56  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
57
58  // Message handlers
59  void OnImportStart();
60  void OnImportFinished(bool succeeded, const std::string& error_msg);
61  void OnImportItemStart(int item);
62  void OnImportItemFinished(int item);
63  void OnHistoryImportStart(size_t total_history_rows_count);
64  void OnHistoryImportGroup(
65      const std::vector<ImporterURLRow>& history_rows_group,
66      int visit_source);
67  void OnHomePageImportReady(const GURL& home_page);
68  void OnBookmarksImportStart(const string16& first_folder_name,
69                              size_t total_bookmarks_count);
70  void OnBookmarksImportGroup(
71      const std::vector<ImportedBookmarkEntry>& bookmarks_group);
72  void OnFaviconsImportStart(size_t total_favicons_count);
73  void OnFaviconsImportGroup(
74      const std::vector<ImportedFaviconUsage>& favicons_group);
75  void OnPasswordFormImportReady(const content::PasswordForm& form);
76  void OnKeywordsImportReady(
77      const std::vector<importer::URLKeywordInfo>& url_keywords,
78      bool unique_on_host_and_path);
79  void OnFirefoxSearchEngineDataReceived(
80      const std::vector<std::string> search_engine_data);
81#if defined(OS_WIN)
82  void OnIE7PasswordReceived(
83        const importer::ImporterIE7PasswordInfo& importer_password_info);
84#endif
85
86 protected:
87  virtual ~ExternalProcessImporterClient();
88
89 private:
90  // Notifies the importerhost that import has finished, and calls Release().
91  void Cleanup();
92
93  // Cancel import process on IO thread.
94  void CancelImportProcessOnIOThread();
95
96  // Report item completely downloaded on IO thread.
97  void NotifyItemFinishedOnIOThread(importer::ImportItem import_item);
98
99  // Creates a new UtilityProcessHost, which launches the import process.
100  void StartProcessOnIOThread(content::BrowserThread::ID thread_id);
101
102  // These variables store data being collected from the importer until the
103  // entire group has been collected and is ready to be written to the profile.
104  std::vector<ImporterURLRow> history_rows_;
105  std::vector<ImportedBookmarkEntry> bookmarks_;
106  std::vector<ImportedFaviconUsage> favicons_;
107
108  // Usually some variation on IDS_BOOKMARK_GROUP_...; the name of the folder
109  // under which imported bookmarks will be placed.
110  string16 bookmarks_first_folder_name_;
111
112  // Total number of bookmarks to import.
113  size_t total_bookmarks_count_;
114
115  // Total number of history items to import.
116  size_t total_history_rows_count_;
117
118  // Total number of favicons to import.
119  size_t total_favicons_count_;
120
121  // Notifications received from the ProfileImportProcessHost are passed back
122  // to process_importer_host_, which calls the ProfileWriter to record the
123  // import data.  When the import process is done, process_importer_host_
124  // deletes itself.
125  ExternalProcessImporterHost* process_importer_host_;
126
127  // Handles sending messages to the external process.  Deletes itself when
128  // the external process dies (see
129  // BrowserChildProcessHost::OnChildDisconnected).
130  base::WeakPtr<content::UtilityProcessHost> utility_process_host_;
131
132  // Data to be passed from the importer host to the external importer.
133  importer::SourceProfile source_profile_;
134  uint16 items_;
135
136  // Takes import data coming over IPC and delivers it to be written by the
137  // ProfileWriter.
138  scoped_refptr<InProcessImporterBridge> bridge_;
139
140  // True if import process has been cancelled.
141  bool cancelled_;
142
143  DISALLOW_COPY_AND_ASSIGN(ExternalProcessImporterClient);
144};
145
146#endif  // CHROME_BROWSER_IMPORTER_EXTERNAL_PROCESS_IMPORTER_CLIENT_H_
147