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