Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: chrome/utility/importer/safari_importer.mm

Issue 18501013: Move most importer code to chrome/utility/importer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: mac4 Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <Cocoa/Cocoa.h> 5 #include <Cocoa/Cocoa.h>
6 6
7 #include "chrome/browser/importer/safari_importer.h" 7 #include "chrome/utility/importer/safari_importer.h"
8 8
9 #include <map> 9 #include <map>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/mac/mac_util.h" 13 #include "base/mac/mac_util.h"
14 #include "base/strings/string16.h" 14 #include "base/strings/string16.h"
15 #include "base/strings/sys_string_conversions.h" 15 #include "base/strings/sys_string_conversions.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "base/time/time.h" 17 #include "base/time/time.h"
18 #include "chrome/browser/favicon/favicon_util.h"
19 #include "chrome/browser/importer/importer_bridge.h"
20 #include "chrome/common/importer/imported_bookmark_entry.h" 18 #include "chrome/common/importer/imported_bookmark_entry.h"
21 #include "chrome/common/importer/imported_favicon_usage.h" 19 #include "chrome/common/importer/imported_favicon_usage.h"
20 #include "chrome/common/importer/importer_bridge.h"
gab 2013/07/08 15:18:08 nit: alpha order
scottmg 2013/07/09 16:20:49 same
22 #include "chrome/common/url_constants.h" 21 #include "chrome/common/url_constants.h"
22 #include "chrome/utility/importer/favicon_reencode.h"
23 #include "grit/generated_resources.h" 23 #include "grit/generated_resources.h"
24 #include "net/base/data_url.h" 24 #include "net/base/data_url.h"
25 #include "sql/statement.h" 25 #include "sql/statement.h"
26 #include "url/gurl.h" 26 #include "url/gurl.h"
27 27
28 namespace { 28 namespace {
29 29
30 // A function like this is used by other importers in order to filter out 30 // A function like this is used by other importers in order to filter out
31 // URLS we don't want to import. 31 // URLS we don't want to import.
32 // For now it's pretty basic, but I've split it out so it's easy to slot 32 // For now it's pretty basic, but I've split it out so it's easy to slot
33 // in necessary logic for filtering URLS, should we need it. 33 // in necessary logic for filtering URLS, should we need it.
34 bool CanImportSafariURL(const GURL& url) { 34 bool CanImportSafariURL(const GURL& url) {
35 // The URL is not valid. 35 // The URL is not valid.
36 if (!url.is_valid()) 36 if (!url.is_valid())
37 return false; 37 return false;
38 38
39 return true; 39 return true;
40 } 40 }
41 41
42 } // namespace 42 } // namespace
43 43
44 SafariImporter::SafariImporter(const base::FilePath& library_dir) 44 SafariImporter::SafariImporter(const base::FilePath& library_dir)
45 : library_dir_(library_dir) { 45 : library_dir_(library_dir) {
46 } 46 }
47 47
48 SafariImporter::~SafariImporter() { 48 SafariImporter::~SafariImporter() {
49 } 49 }
50 50
51 // static
52 bool SafariImporter::CanImport(const base::FilePath& library_dir,
53 uint16* services_supported) {
54 DCHECK(services_supported);
55 *services_supported = importer::NONE;
56
57 // Import features are toggled by the following:
58 // bookmarks import: existence of ~/Library/Safari/Bookmarks.plist file.
59 // history import: existence of ~/Library/Safari/History.plist file.
60 base::FilePath safari_dir = library_dir.Append("Safari");
61 base::FilePath bookmarks_path = safari_dir.Append("Bookmarks.plist");
62 base::FilePath history_path = safari_dir.Append("History.plist");
63
64 if (file_util::PathExists(bookmarks_path))
65 *services_supported |= importer::FAVORITES;
66 if (file_util::PathExists(history_path))
67 *services_supported |= importer::HISTORY;
68
69 return *services_supported != importer::NONE;
70 }
71
72 void SafariImporter::StartImport(const importer::SourceProfile& source_profile, 51 void SafariImporter::StartImport(const importer::SourceProfile& source_profile,
73 uint16 items, 52 uint16 items,
74 ImporterBridge* bridge) { 53 ImporterBridge* bridge) {
75 bridge_ = bridge; 54 bridge_ = bridge;
76 // The order here is important! 55 // The order here is important!
77 bridge_->NotifyStarted(); 56 bridge_->NotifyStarted();
78 57
79 // In keeping with import on other platforms (and for other browsers), we 58 // In keeping with import on other platforms (and for other browsers), we
80 // don't import the home page (since it may lead to a useless homepage); see 59 // don't import the home page (since it may lead to a useless homepage); see
81 // crbug.com/25603. 60 // crbug.com/25603.
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 148
170 usage.favicon_url = GURL(s.ColumnString(0)); 149 usage.favicon_url = GURL(s.ColumnString(0));
171 if (!usage.favicon_url.is_valid()) 150 if (!usage.favicon_url.is_valid())
172 continue; // Don't bother importing favicons with invalid URLs. 151 continue; // Don't bother importing favicons with invalid URLs.
173 152
174 std::vector<unsigned char> data; 153 std::vector<unsigned char> data;
175 s.ColumnBlobAsVector(1, &data); 154 s.ColumnBlobAsVector(1, &data);
176 if (data.empty()) 155 if (data.empty())
177 continue; // Data definitely invalid. 156 continue; // Data definitely invalid.
178 157
179 if (!FaviconUtil::ReencodeFavicon(&data[0], data.size(), &usage.png_data)) 158 if (!importer::ReencodeFavicon(&data[0], data.size(), &usage.png_data))
180 continue; // Unable to decode. 159 continue; // Unable to decode.
181 160
182 usage.urls = i->second; 161 usage.urls = i->second;
183 favicons->push_back(usage); 162 favicons->push_back(usage);
184 } 163 }
185 } 164 }
186 } 165 }
187 166
188 void SafariImporter::RecursiveReadBookmarksFolder( 167 void SafariImporter::RecursiveReadBookmarksFolder(
189 NSDictionary* bookmark_folder, 168 NSDictionary* bookmark_folder,
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 // the user needing to explicitly input his username in a page and blurring 294 // the user needing to explicitly input his username in a page and blurring
316 // the field before we pick it up, but the details of that are beyond the 295 // the field before we pick it up, but the details of that are beyond the
317 // scope of this comment. 296 // scope of this comment.
318 } 297 }
319 298
320 void SafariImporter::ImportHistory() { 299 void SafariImporter::ImportHistory() {
321 std::vector<ImporterURLRow> rows; 300 std::vector<ImporterURLRow> rows;
322 ParseHistoryItems(&rows); 301 ParseHistoryItems(&rows);
323 302
324 if (!rows.empty() && !cancelled()) { 303 if (!rows.empty() && !cancelled()) {
325 bridge_->SetHistoryItems(rows, history::SOURCE_SAFARI_IMPORTED); 304 bridge_->SetHistoryItems(rows, IMPORTER_VISIT_SOURCE_SAFARI_IMPORTED);
326 } 305 }
327 } 306 }
328 307
329 double SafariImporter::HistoryTimeToEpochTime(NSString* history_time) { 308 double SafariImporter::HistoryTimeToEpochTime(NSString* history_time) {
330 DCHECK(history_time); 309 DCHECK(history_time);
331 // Add Difference between Unix epoch and CFAbsoluteTime epoch in seconds. 310 // Add Difference between Unix epoch and CFAbsoluteTime epoch in seconds.
332 // Unix epoch is 1970-01-01 00:00:00.0 UTC, 311 // Unix epoch is 1970-01-01 00:00:00.0 UTC,
333 // CF epoch is 2001-01-01 00:00:00.0 UTC. 312 // CF epoch is 2001-01-01 00:00:00.0 UTC.
334 return CFStringGetDoubleValue(base::mac::NSToCFCast(history_time)) + 313 return CFStringGetDoubleValue(base::mac::NSToCFCast(history_time)) +
335 kCFAbsoluteTimeIntervalSince1970; 314 kCFAbsoluteTimeIntervalSince1970;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 if (!last_visit_str) 369 if (!last_visit_str)
391 continue; 370 continue;
392 371
393 // Convert Safari's last visit time to Unix Epoch time. 372 // Convert Safari's last visit time to Unix Epoch time.
394 double seconds_since_unix_epoch = HistoryTimeToEpochTime(last_visit_str); 373 double seconds_since_unix_epoch = HistoryTimeToEpochTime(last_visit_str);
395 row.last_visit = base::Time::FromDoubleT(seconds_since_unix_epoch); 374 row.last_visit = base::Time::FromDoubleT(seconds_since_unix_epoch);
396 375
397 history_items->push_back(row); 376 history_items->push_back(row);
398 } 377 }
399 } 378 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698