Skip to content

Commit

Permalink
Chrome support
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Filetti committed Dec 11, 2015
1 parent 31f72bb commit d910456
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 12 deletions.
1 change: 1 addition & 0 deletions JustUsed/Model/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// Prepare browser tracking for each browser
browserManager.addFetcher(SafariHistoryFetcher())
browserManager.addFetcher(FirefoxHistoryFetcher())
browserManager.addFetcher(ChromeHistoryFetcher())

// View controller and its delegation
self.viewController = (storyboard.instantiateControllerWithIdentifier("View Controller") as! ViewController)
Expand Down
70 changes: 70 additions & 0 deletions JustUsed/Model/ChromeHistoryFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,73 @@

import Foundation
import Cocoa

class ChromeHistoryFetcher: BrowserHistoryFetcher {

private(set) var lastHistoryEntry: NSDate
var lastDBFileUpdate: NSDate
let browserType: BrowserType = .Safari

required init?() {

// initializes dates and performs first history check to update them
lastHistoryEntry = NSDate()
lastDBFileUpdate = NSDate.distantPast() // Initialise to be as early as possible.

// If no valid urls exist, fail initialization
if getDBURLs().count == 0 {
return nil
}

// initialization succeeded, do first history check
historyCheck()

}

func getNewHistoryItemsFromDB(dbPath: String) -> [BrowserHistItem] {

// Perform database read
var new_urls = [BrowserHistItem]()
let db = FMDatabase(path: dbPath)
db.open()
let lastTime = self.lastHistoryEntry.ldapTime
let urls_query = "SELECT url, title, last_visit_time FROM urls WHERE last_visit_time > ? ORDER BY last_visit_time asc"
if let urls_result = db.executeQuery(urls_query, withArgumentsInArray: ["\(lastTime)"]) {
while urls_result.next() {
let urls_dict = urls_result.resultDictionary()
let url = urls_dict["url"] as! String
let title = urls_dict["title"] as? String
let visit_time = urls_dict["last_visit_time"] as! Int
let visit_date = NSDate(fromLdapTime: visit_time)
self.lastHistoryEntry = visit_date
let location = LocationSingleton.getCurrentLocation()
new_urls.append(BrowserHistItem(browser: .Chrome, date: visit_date, url: url, title: title, location: location))
}
}
db.close()

return new_urls
}

/// Chrome implementation: return "History"
func getDBURLs() -> [NSURL] {
let appSupportDir = NSFileManager.defaultManager().URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask)[0]

let chromeDefaultDir = appSupportDir.URLByAppendingPathComponent("Google/Chrome/Default")

let filenames: [String] = ["History"]

var retVal = [NSURL]()
for filename in filenames {
retVal.append(chromeDefaultDir.URLByAppendingPathComponent(filename))
}

// If History does not exist, assume chrome is not being used
if !AppSingleton.fileManager.fileExistsAtPath(retVal[0].path!) {
return [NSURL]()
}

return retVal
}

}
9 changes: 6 additions & 3 deletions JustUsed/Model/FirefoxHistoryFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ class FirefoxHistoryFetcher: BrowserHistoryFetcher {
let db = FMDatabase(path: dbPath)
db.open()
let lastTime = self.lastHistoryEntry.unixTime_μs
self.lastHistoryEntry = NSDate()
let visits_query = "SELECT url, title, last_visit_date FROM moz_places WHERE last_visit_date > ?"
let visits_query = "SELECT url, title, last_visit_date FROM moz_places WHERE last_visit_date > ? ORDER BY last_visit_date asc"
if let visits_result = db.executeQuery(visits_query, withArgumentsInArray: ["\(lastTime)"]) {
while visits_result.next() {
let visits_dict = visits_result.resultDictionary()
let visit_url = visits_dict["url"] as! String
let visit_title = visits_dict["title"] as? String
let visit_time = visits_dict["last_visit_date"] as! Int
let visit_date = NSDate(fromUnixTime_μs: visit_time)
self.lastHistoryEntry = visit_date
let location = LocationSingleton.getCurrentLocation()
new_urls.append(BrowserHistItem(browser: .Firefox, date: visit_date, url: visit_url, title: visit_title, location: location))
}
Expand All @@ -72,11 +72,14 @@ class FirefoxHistoryFetcher: BrowserHistoryFetcher {
retVal.append(dbFolder.URLByAppendingPathComponent(filename))
}

// If History.db does not exist, assume Firefox is not being used
// If places.sqlite does not exist, assume Firefox is not being used
if !AppSingleton.fileManager.fileExistsAtPath(retVal[0].path!) {
return [NSURL]()
}

// filter by keeping only existing paths
retVal = retVal.filter({AppSingleton.fileManager.fileExistsAtPath($0.path!)})

return retVal
}

Expand Down
9 changes: 6 additions & 3 deletions JustUsed/Model/SafariHistoryFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SafariHistoryFetcher: BrowserHistoryFetcher {
lastHistoryEntry = NSDate()
lastDBFileUpdate = NSDate.distantPast() // Initialise to be as early as possible.

// If not valid urls exist, fail initialization
// If no valid urls exist, fail initialization
if getDBURLs().count == 0 {
return nil
}
Expand All @@ -41,15 +41,15 @@ class SafariHistoryFetcher: BrowserHistoryFetcher {
let db = FMDatabase(path: dbPath)
db.open()
let lastTime = self.lastHistoryEntry.timeIntervalSinceReferenceDate as Double
self.lastHistoryEntry = NSDate()
let visits_query = "SELECT history_item, visit_time, title FROM history_visits WHERE visit_time > ?"
let visits_query = "SELECT history_item, visit_time, title FROM history_visits WHERE visit_time > ? ORDER BY visit_time asc"
if let visits_result = db.executeQuery(visits_query, withArgumentsInArray: ["\(lastTime)"]) {
while visits_result.next() {
let visits_dict = visits_result.resultDictionary()
let visit_id = visits_dict["history_item"] as! NSNumber
let visit_title = visits_dict["title"] as? String
let visit_time = visits_dict["visit_time"] as! NSNumber
let visit_date = NSDate(timeIntervalSinceReferenceDate: visit_time as NSTimeInterval)
self.lastHistoryEntry = visit_date
let item_query = "SELECT url FROM history_items WHERE id = ?"
let item_result = db.executeQuery(item_query, withArgumentsInArray: [visit_id])
while item_result.next() {
Expand Down Expand Up @@ -81,6 +81,9 @@ class SafariHistoryFetcher: BrowserHistoryFetcher {
return [NSURL]()
}

// filter by keeping only existing paths
retVal = retVal.filter({AppSingleton.fileManager.fileExistsAtPath($0.path!)})

return retVal
}
}
12 changes: 6 additions & 6 deletions JustUsed/Utils/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ extension NSDate {
}
}

/// Creates a date from a windows time.
convenience init(fromWinTime wintime: Double) {
let unixtime_s = wintime/1000-11644473600
/// Creates a date from a ldap timestamp.
convenience init(fromLdapTime lt: Int) {
let unixtime_s = Double(lt)/1000000-11644473600
self.init(timeIntervalSince1970: unixtime_s)
}

/// Returns the corresponding date as a windows timestamp.
var winTime: Double { get {
return 1000 * (11644473600 + self.timeIntervalSince1970)
/// Returns the corresponding date as a LDAP timestamp.
var ldapTime: Int { get {
return Int(round(1000000 * (11644473600 + self.timeIntervalSince1970)))
}
}

Expand Down

0 comments on commit d910456

Please sign in to comment.