Skip to content

Commit

Permalink
Revert "Revert "Import History from Safari""
Browse files Browse the repository at this point in the history
This reverts commit c65b0fd.
  • Loading branch information
simonhong committed Oct 12, 2020
1 parent c558d6f commit 0281499
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
Full Disk Access required
</message>
<message name="IDS_FULL_DISK_ACCESS_CONFIRM_DIALOG_MESSAGE" desc="The label for full disk access dialog message">
Brave needs Full Disk Access to import your Bookmarks from Safari.
Brave needs Full Disk Access to import your Bookmarks and History from Safari.
</message>
<message name="IDS_FULL_DISK_ACCESS_CONFIRM_DIALOG_LINK_TEXT" desc="The label for full disk access dialog link text">
Learn how to grant Full Disk Access from your System Preferences.
Expand Down
34 changes: 31 additions & 3 deletions browser/ui/webui/settings/brave_import_data_handler_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@

using content::BrowserThread;

class ScopedOpenFile {
public:
explicit ScopedOpenFile(const base::FilePath& file_path) {
file_ = base::OpenFile(file_path, "r");
}

~ScopedOpenFile() {
if (file_)
base::CloseFile(file_);
}

bool CanRead() const { return !!file_; }

ScopedOpenFile(const ScopedOpenFile&) = delete;
ScopedOpenFile& operator=(const ScopedOpenFile&) = delete;

private:
FILE* file_ = nullptr;
};

class FullDiskAccessConfirmDialogDelegate
: public TabModalConfirmDialogDelegate {
public:
Expand Down Expand Up @@ -104,16 +124,24 @@ bool HasProperDiskAccessPermission(uint16_t imported_items) {

if (imported_items & importer::FAVORITES) {
const base::FilePath bookmarks_path = safari_dir.Append("Bookmarks.plist");
if(!PathIsWritable(bookmarks_path)) {
ScopedOpenFile open_file(bookmarks_path);
if(!open_file.CanRead()) {
LOG(ERROR) << __func__ << " " << bookmarks_path << " is not accessible."
<< " Please check full disk access permission.";
return false;
}
}

if (imported_items & importer::HISTORY) {
const base::FilePath history_path = safari_dir.Append("History.plist");
if(!PathIsWritable(history_path)) {
// HISTORY is set if plist or db exists.
base::FilePath history_path = safari_dir.Append("History.plist");
if (!base::PathExists(history_path)) {
history_path = safari_dir.Append("History.db");
DCHECK(base::PathExists(history_path));
}

ScopedOpenFile open_file(history_path);
if(!open_file.CanRead()) {
LOG(ERROR) << __func__ << " " << history_path << " is not accessible."
<< " Please check full disk access permission.";
return false;
Expand Down
20 changes: 20 additions & 0 deletions chromium_src/chrome/common/importer/safari_importer_utils.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#define SafariImporterCanImport SafariImporterCanImport_ChromiumImpl

#include "../../../../../chrome/common/importer/safari_importer_utils.mm"

#undef SafariImporterCanImport

bool SafariImporterCanImport(const base::FilePath& library_dir,
uint16_t* services_supported) {
SafariImporterCanImport_ChromiumImpl(library_dir, services_supported);

if (base::PathExists(library_dir.Append("Safari").Append("History.db")))
*services_supported |= importer::HISTORY;

return *services_supported != importer::NONE;
}
10 changes: 10 additions & 0 deletions chromium_src/chrome/utility/importer/importer_creator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* Copyright 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/utility/importer/brave_safari_importer.h"

#define SafariImporter BraveSafariImporter
#include "../../../../../chrome/utility/importer/importer_creator.cc"
#undef SafariImporter
19 changes: 19 additions & 0 deletions chromium_src/chrome/utility/importer/safari_importer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* Copyright 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_CHROME_UTILITY_IMPORTER_SAFARI_IMPORTER_H_
#define BRAVE_CHROMIUM_SRC_CHROME_UTILITY_IMPORTER_SAFARI_IMPORTER_H_

#define BRAVE_SAFARI_IMPORTER_H \
friend class BraveSafariImporter;

#define ImportHistory virtual ImportHistory

#include "../../../../../chrome/utility/importer/safari_importer.h"

#undef BRAVE_SAFARI_IMPORTER_H
#undef ImportHistory

#endif // BRAVE_CHROMIUM_SRC_CHROME_UTILITY_IMPORTER_SAFARI_IMPORTER_H_
12 changes: 12 additions & 0 deletions patches/chrome-utility-importer-safari_importer.h.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/chrome/utility/importer/safari_importer.h b/chrome/utility/importer/safari_importer.h
index 1891fa3eff16f4e7b8f0821bc9f793c5e00514f7..89bce81bbc3d2f682adb3aacd888ac29f6d06e74 100644
--- a/chrome/utility/importer/safari_importer.h
+++ b/chrome/utility/importer/safari_importer.h
@@ -46,6 +46,7 @@ class SafariImporter : public Importer {
uint16_t items,
ImporterBridge* bridge) override;

+ BRAVE_SAFARI_IMPORTER_H
private:
FRIEND_TEST_ALL_PREFIXES(SafariImporterTest, BookmarkImport);
FRIEND_TEST_ALL_PREFIXES(SafariImporterTest,
13 changes: 13 additions & 0 deletions utility/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ source_set("utility") {
]
}

if (is_mac) {
sources += [
"importer/brave_safari_importer.mm",
"importer/brave_safari_importer.h",
]

deps += [
"//base",
"//sql",
"//url",
]
}

if (enable_tor) {
deps += [ "//brave/components/services/tor" ]
}
Expand Down
25 changes: 25 additions & 0 deletions utility/importer/brave_safari_importer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Copyright 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_UTILITY_IMPORTER_BRAVE_SAFARI_IMPORTER_H_
#define BRAVE_UTILITY_IMPORTER_BRAVE_SAFARI_IMPORTER_H_

#include "chrome/utility/importer/safari_importer.h"

class BraveSafariImporter : public SafariImporter {
public:
using SafariImporter::SafariImporter;

BraveSafariImporter(const BraveSafariImporter&) = delete;
BraveSafariImporter& operator=(const BraveSafariImporter&) = delete;

private:
// SafariImporter overrides:
void ImportHistory() override;

~BraveSafariImporter() override;
};

#endif // BRAVE_UTILITY_IMPORTER_BRAVE_SAFARI_IMPORTER_H_
71 changes: 71 additions & 0 deletions utility/importer/brave_safari_importer.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* Copyright 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <Cocoa/Cocoa.h>

#include "brave/utility/importer/brave_safari_importer.h"

#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/common/importer/importer_bridge.h"
#include "chrome/common/importer/importer_url_row.h"
#include "sql/statement.h"
#include "url/gurl.h"

BraveSafariImporter::~BraveSafariImporter() = default;

void BraveSafariImporter::ImportHistory() {
// For importing history from History.plist.
SafariImporter::ImportHistory();

// From now, try to import history from History.db.
NSString* library_dir = [NSString
stringWithUTF8String:library_dir_.value().c_str()];
NSString* safari_dir = [library_dir
stringByAppendingPathComponent:@"Safari"];
NSString* history_db = [safari_dir
stringByAppendingPathComponent:@"History.db"];

// Import favicons.
sql::Database db;
const char* db_path = [history_db fileSystemRepresentation];
if (!db.Open(base::FilePath(db_path)))
return;

std::vector<ImporterURLRow> rows;
const char query[] = "SELECT hi.url, hi.visit_count, hv.visit_time, hv.title "
"FROM history_items as hi "
"JOIN history_visits as hv ON hi.id == hv.history_item";
sql::Statement s(db.GetUniqueStatement(query));
while (s.Step() && !cancelled()) {
const GURL url = GURL(s.ColumnString(0));
if (!url.is_valid())
continue;

ImporterURLRow row(url);
row.visit_count = s.ColumnInt(1);
double visit_time = s.ColumnDouble(2);
if (!visit_time)
continue;
row.last_visit =
base::Time::FromDoubleT(visit_time + kCFAbsoluteTimeIntervalSince1970);
std::string title = s.ColumnString(3);
if (title.empty())
title = url.spec();
row.title = base::UTF8ToUTF16(title);
row.hidden = 0;
row.typed_count = 0;
rows.push_back(row);
}

if (!rows.empty() && !cancelled()) {
bridge_->SetHistoryItems(rows, importer::VISIT_SOURCE_SAFARI_IMPORTED);
}
}

0 comments on commit 0281499

Please sign in to comment.