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