123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- /* 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/. */
- 'use strict';
- module.metadata = {
- 'stability': 'experimental',
- 'engines': {
- 'Firefox': '*'
- }
- };
- const { Cc, Ci } = require('chrome');
- const { Unknown } = require('../platform/xpcom');
- const { Class } = require('../core/heritage');
- const { merge } = require('../util/object');
- const bookmarkService = Cc['@mozilla.org/browser/nav-bookmarks-service;1']
- .getService(Ci.nsINavBookmarksService);
- const historyService = Cc['@mozilla.org/browser/nav-history-service;1']
- .getService(Ci.nsINavHistoryService);
- const { mapBookmarkItemType } = require('./utils');
- const { EventTarget } = require('../event/target');
- const { emit } = require('../event/core');
- const emitter = EventTarget();
- let HISTORY_ARGS = {
- onBeginUpdateBatch: [],
- onEndUpdateBatch: [],
- onClearHistory: [],
- onDeleteURI: ['url'],
- onDeleteVisits: ['url', 'visitTime'],
- onPageChanged: ['url', 'property', 'value'],
- onTitleChanged: ['url', 'title'],
- onVisit: [
- 'url', 'visitId', 'time', 'sessionId', 'referringId', 'transitionType'
- ]
- };
- let HISTORY_EVENTS = {
- onBeginUpdateBatch: 'history-start-batch',
- onEndUpdateBatch: 'history-end-batch',
- onClearHistory: 'history-start-clear',
- onDeleteURI: 'history-delete-url',
- onDeleteVisits: 'history-delete-visits',
- onPageChanged: 'history-page-changed',
- onTitleChanged: 'history-title-changed',
- onVisit: 'history-visit'
- };
- let BOOKMARK_ARGS = {
- onItemAdded: [
- 'id', 'parentId', 'index', 'type', 'url', 'title', 'dateAdded'
- ],
- onItemChanged: [
- 'id', 'property', null, 'value', 'lastModified', 'type', 'parentId'
- ],
- onItemMoved: [
- 'id', 'previousParentId', 'previousIndex', 'currentParentId',
- 'currentIndex', 'type'
- ],
- onItemRemoved: ['id', 'parentId', 'index', 'type', 'url'],
- onItemVisited: ['id', 'visitId', 'time', 'transitionType', 'url', 'parentId']
- };
- let BOOKMARK_EVENTS = {
- onItemAdded: 'bookmark-item-added',
- onItemChanged: 'bookmark-item-changed',
- onItemMoved: 'bookmark-item-moved',
- onItemRemoved: 'bookmark-item-removed',
- onItemVisited: 'bookmark-item-visited',
- };
- function createHandler (type, propNames) {
- propNames = propNames || [];
- return function (...args) {
- let data = propNames.reduce((acc, prop, i) => {
- if (prop)
- acc[prop] = formatValue(prop, args[i]);
- return acc;
- }, {});
- emit(emitter, 'data', {
- type: type,
- data: data
- });
- };
- }
- /*
- * Creates an observer, creating handlers based off of
- * the `events` names, and ordering arguments from `propNames` hash
- */
- function createObserverInstance (events, propNames) {
- let definition = Object.keys(events).reduce((prototype, eventName) => {
- prototype[eventName] = createHandler(events[eventName], propNames[eventName]);
- return prototype;
- }, {});
- return Class(merge(definition, { extends: Unknown }))();
- }
- /*
- * Formats `data` based off of the value of `type`
- */
- function formatValue (type, data) {
- if (type === 'type')
- return mapBookmarkItemType(data);
- if (type === 'url' && data)
- return data.spec;
- return data;
- }
- let historyObserver = createObserverInstance(HISTORY_EVENTS, HISTORY_ARGS);
- historyService.addObserver(historyObserver, false);
- let bookmarkObserver = createObserverInstance(BOOKMARK_EVENTS, BOOKMARK_ARGS);
- bookmarkService.addObserver(bookmarkObserver, false);
- exports.events = emitter;
|