Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / utility / importer / external_process_importer_bridge.cc
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 #include "chrome/utility/importer/external_process_importer_bridge.h"
6
7 #include "base/bind.h"
8 #include "base/debug/dump_without_crashing.h"
9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/task_runner.h"
13 #include "base/values.h"
14 #include "chrome/common/importer/imported_bookmark_entry.h"
15 #include "chrome/common/importer/imported_favicon_usage.h"
16 #include "chrome/common/importer/importer_data_types.h"
17 #include "chrome/common/importer/profile_import_process_messages.h"
18 #include "components/autofill/core/common/password_form.h"
19 #include "ipc/ipc_sender.h"
20
21 namespace {
22
23 // Rather than sending all import items over IPC at once we chunk them into
24 // separate requests.  This avoids the case of a large import causing
25 // oversized IPC messages.
26 const int kNumBookmarksToSend = 100;
27 const int kNumHistoryRowsToSend = 100;
28 const int kNumFaviconsToSend = 100;
29 const int kNumAutofillFormDataToSend = 100;
30
31 } // namespace
32
33 ExternalProcessImporterBridge::ExternalProcessImporterBridge(
34     const base::DictionaryValue& localized_strings,
35     IPC::Sender* sender,
36     base::TaskRunner* task_runner)
37     : sender_(sender),
38       task_runner_(task_runner) {
39   // Bridge needs to make its own copy because OS 10.6 autoreleases the
40   // localized_strings value that is passed in (see http://crbug.com/46003 ).
41   localized_strings_.reset(localized_strings.DeepCopy());
42 }
43
44 void ExternalProcessImporterBridge::AddBookmarks(
45     const std::vector<ImportedBookmarkEntry>& bookmarks,
46     const base::string16& first_folder_name) {
47   Send(new ProfileImportProcessHostMsg_NotifyBookmarksImportStart(
48       first_folder_name, bookmarks.size()));
49
50   // |bookmarks_left| is required for the checks below as Windows has a
51   // Debug bounds-check which prevents pushing an iterator beyond its end()
52   // (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 == s.end()|).
53   int bookmarks_left = bookmarks.end() - bookmarks.begin();
54   for (std::vector<ImportedBookmarkEntry>::const_iterator it =
55            bookmarks.begin(); it < bookmarks.end();) {
56     std::vector<ImportedBookmarkEntry> bookmark_group;
57     std::vector<ImportedBookmarkEntry>::const_iterator end_group =
58         it + std::min(bookmarks_left, kNumBookmarksToSend);
59     bookmark_group.assign(it, end_group);
60
61     Send(new ProfileImportProcessHostMsg_NotifyBookmarksImportGroup(
62         bookmark_group));
63     bookmarks_left -= end_group - it;
64     it = end_group;
65   }
66   DCHECK_EQ(0, bookmarks_left);
67 }
68
69 void ExternalProcessImporterBridge::AddHomePage(const GURL& home_page) {
70   Send(new ProfileImportProcessHostMsg_NotifyHomePageImportReady(home_page));
71 }
72
73 #if defined(OS_WIN)
74 void ExternalProcessImporterBridge::AddIE7PasswordInfo(
75     const importer::ImporterIE7PasswordInfo& password_info) {
76   Send(new ProfileImportProcessHostMsg_NotifyIE7PasswordInfo(password_info));
77 }
78 #endif
79
80 void ExternalProcessImporterBridge::SetFavicons(
81     const std::vector<ImportedFaviconUsage>& favicons) {
82   Send(new ProfileImportProcessHostMsg_NotifyFaviconsImportStart(
83     favicons.size()));
84
85   // |favicons_left| is required for the checks below as Windows has a
86   // Debug bounds-check which prevents pushing an iterator beyond its end()
87   // (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 == s.end()|).
88   int favicons_left = favicons.end() - favicons.begin();
89   for (std::vector<ImportedFaviconUsage>::const_iterator it =
90            favicons.begin(); it < favicons.end();) {
91     std::vector<ImportedFaviconUsage> favicons_group;
92     std::vector<ImportedFaviconUsage>::const_iterator end_group =
93         it + std::min(favicons_left, kNumFaviconsToSend);
94     favicons_group.assign(it, end_group);
95
96     Send(new ProfileImportProcessHostMsg_NotifyFaviconsImportGroup(
97         favicons_group));
98     favicons_left -= end_group - it;
99     it = end_group;
100   }
101   DCHECK_EQ(0, favicons_left);
102 }
103
104 void ExternalProcessImporterBridge::SetHistoryItems(
105     const std::vector<ImporterURLRow>& rows,
106     importer::VisitSource visit_source) {
107   Send(new ProfileImportProcessHostMsg_NotifyHistoryImportStart(rows.size()));
108
109   // |rows_left| is required for the checks below as Windows has a
110   // Debug bounds-check which prevents pushing an iterator beyond its end()
111   // (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 == s.end()|).
112   int rows_left = rows.end() - rows.begin();
113   for (std::vector<ImporterURLRow>::const_iterator it = rows.begin();
114        it < rows.end();) {
115     std::vector<ImporterURLRow> row_group;
116     std::vector<ImporterURLRow>::const_iterator end_group =
117         it + std::min(rows_left, kNumHistoryRowsToSend);
118     row_group.assign(it, end_group);
119
120     Send(new ProfileImportProcessHostMsg_NotifyHistoryImportGroup(
121         row_group, visit_source));
122     rows_left -= end_group - it;
123     it = end_group;
124   }
125   DCHECK_EQ(0, rows_left);
126 }
127
128 void ExternalProcessImporterBridge::SetKeywords(
129     const std::vector<importer::URLKeywordInfo>& url_keywords,
130     bool unique_on_host_and_path) {
131   Send(new ProfileImportProcessHostMsg_NotifyKeywordsReady(
132       url_keywords, unique_on_host_and_path));
133 }
134
135 void ExternalProcessImporterBridge::SetFirefoxSearchEnginesXMLData(
136     const std::vector<std::string>& search_engine_data) {
137   Send(new ProfileImportProcessHostMsg_NotifyFirefoxSearchEngData(
138       search_engine_data));
139 }
140
141 void ExternalProcessImporterBridge::SetPasswordForm(
142     const autofill::PasswordForm& form) {
143   Send(new ProfileImportProcessHostMsg_NotifyPasswordFormReady(form));
144 }
145
146 void ExternalProcessImporterBridge::SetAutofillFormData(
147     const std::vector<ImporterAutofillFormDataEntry>& entries) {
148   Send(new ProfileImportProcessHostMsg_AutofillFormDataImportStart(
149       entries.size()));
150
151   // |autofill_form_data_entries_left| is required for the checks below as
152   // Windows has a Debug bounds-check which prevents pushing an iterator beyond
153   // its end() (i.e., |it + 2 < s.end()| crashes in debug mode if |i + 1 ==
154   // s.end()|).
155   int autofill_form_data_entries_left = entries.end() - entries.begin();
156   for (std::vector<ImporterAutofillFormDataEntry>::const_iterator it =
157            entries.begin();
158        it < entries.end();) {
159     std::vector<ImporterAutofillFormDataEntry> autofill_form_data_entry_group;
160     std::vector<ImporterAutofillFormDataEntry>::const_iterator end_group =
161         it +
162         std::min(autofill_form_data_entries_left, kNumAutofillFormDataToSend);
163     autofill_form_data_entry_group.assign(it, end_group);
164
165     Send(new ProfileImportProcessHostMsg_AutofillFormDataImportGroup(
166         autofill_form_data_entry_group));
167     autofill_form_data_entries_left -= end_group - it;
168     it = end_group;
169   }
170   DCHECK_EQ(0, autofill_form_data_entries_left);
171 }
172
173 void ExternalProcessImporterBridge::NotifyStarted() {
174   Send(new ProfileImportProcessHostMsg_Import_Started());
175 }
176
177 void ExternalProcessImporterBridge::NotifyItemStarted(
178     importer::ImportItem item) {
179   Send(new ProfileImportProcessHostMsg_ImportItem_Started(item));
180 }
181
182 void ExternalProcessImporterBridge::NotifyItemEnded(importer::ImportItem item) {
183   Send(new ProfileImportProcessHostMsg_ImportItem_Finished(item));
184 }
185
186 void ExternalProcessImporterBridge::NotifyEnded() {
187   // The internal process detects import end when all items have been received.
188 }
189
190 base::string16 ExternalProcessImporterBridge::GetLocalizedString(
191     int message_id) {
192   base::string16 message;
193   localized_strings_->GetString(base::IntToString(message_id), &message);
194   return message;
195 }
196
197 ExternalProcessImporterBridge::~ExternalProcessImporterBridge() {}
198
199 void ExternalProcessImporterBridge::Send(IPC::Message* message) {
200   task_runner_->PostTask(
201       FROM_HERE,
202       base::Bind(&ExternalProcessImporterBridge::SendInternal,
203                  this, message));
204 }
205
206 void ExternalProcessImporterBridge::SendInternal(IPC::Message* message) {
207   DCHECK(task_runner_->RunsTasksOnCurrentThread());
208   sender_->Send(message);
209 }