1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/lib/sdk/places/events.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,121 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +'use strict'; 1.9 + 1.10 +module.metadata = { 1.11 + 'stability': 'experimental', 1.12 + 'engines': { 1.13 + 'Firefox': '*' 1.14 + } 1.15 +}; 1.16 + 1.17 +const { Cc, Ci } = require('chrome'); 1.18 +const { Unknown } = require('../platform/xpcom'); 1.19 +const { Class } = require('../core/heritage'); 1.20 +const { merge } = require('../util/object'); 1.21 +const bookmarkService = Cc['@mozilla.org/browser/nav-bookmarks-service;1'] 1.22 + .getService(Ci.nsINavBookmarksService); 1.23 +const historyService = Cc['@mozilla.org/browser/nav-history-service;1'] 1.24 + .getService(Ci.nsINavHistoryService); 1.25 +const { mapBookmarkItemType } = require('./utils'); 1.26 +const { EventTarget } = require('../event/target'); 1.27 +const { emit } = require('../event/core'); 1.28 + 1.29 +const emitter = EventTarget(); 1.30 + 1.31 +let HISTORY_ARGS = { 1.32 + onBeginUpdateBatch: [], 1.33 + onEndUpdateBatch: [], 1.34 + onClearHistory: [], 1.35 + onDeleteURI: ['url'], 1.36 + onDeleteVisits: ['url', 'visitTime'], 1.37 + onPageChanged: ['url', 'property', 'value'], 1.38 + onTitleChanged: ['url', 'title'], 1.39 + onVisit: [ 1.40 + 'url', 'visitId', 'time', 'sessionId', 'referringId', 'transitionType' 1.41 + ] 1.42 +}; 1.43 + 1.44 +let HISTORY_EVENTS = { 1.45 + onBeginUpdateBatch: 'history-start-batch', 1.46 + onEndUpdateBatch: 'history-end-batch', 1.47 + onClearHistory: 'history-start-clear', 1.48 + onDeleteURI: 'history-delete-url', 1.49 + onDeleteVisits: 'history-delete-visits', 1.50 + onPageChanged: 'history-page-changed', 1.51 + onTitleChanged: 'history-title-changed', 1.52 + onVisit: 'history-visit' 1.53 +}; 1.54 + 1.55 +let BOOKMARK_ARGS = { 1.56 + onItemAdded: [ 1.57 + 'id', 'parentId', 'index', 'type', 'url', 'title', 'dateAdded' 1.58 + ], 1.59 + onItemChanged: [ 1.60 + 'id', 'property', null, 'value', 'lastModified', 'type', 'parentId' 1.61 + ], 1.62 + onItemMoved: [ 1.63 + 'id', 'previousParentId', 'previousIndex', 'currentParentId', 1.64 + 'currentIndex', 'type' 1.65 + ], 1.66 + onItemRemoved: ['id', 'parentId', 'index', 'type', 'url'], 1.67 + onItemVisited: ['id', 'visitId', 'time', 'transitionType', 'url', 'parentId'] 1.68 +}; 1.69 + 1.70 +let BOOKMARK_EVENTS = { 1.71 + onItemAdded: 'bookmark-item-added', 1.72 + onItemChanged: 'bookmark-item-changed', 1.73 + onItemMoved: 'bookmark-item-moved', 1.74 + onItemRemoved: 'bookmark-item-removed', 1.75 + onItemVisited: 'bookmark-item-visited', 1.76 +}; 1.77 + 1.78 +function createHandler (type, propNames) { 1.79 + propNames = propNames || []; 1.80 + return function (...args) { 1.81 + let data = propNames.reduce((acc, prop, i) => { 1.82 + if (prop) 1.83 + acc[prop] = formatValue(prop, args[i]); 1.84 + return acc; 1.85 + }, {}); 1.86 + 1.87 + emit(emitter, 'data', { 1.88 + type: type, 1.89 + data: data 1.90 + }); 1.91 + }; 1.92 +} 1.93 + 1.94 +/* 1.95 + * Creates an observer, creating handlers based off of 1.96 + * the `events` names, and ordering arguments from `propNames` hash 1.97 + */ 1.98 +function createObserverInstance (events, propNames) { 1.99 + let definition = Object.keys(events).reduce((prototype, eventName) => { 1.100 + prototype[eventName] = createHandler(events[eventName], propNames[eventName]); 1.101 + return prototype; 1.102 + }, {}); 1.103 + 1.104 + return Class(merge(definition, { extends: Unknown }))(); 1.105 +} 1.106 + 1.107 +/* 1.108 + * Formats `data` based off of the value of `type` 1.109 + */ 1.110 +function formatValue (type, data) { 1.111 + if (type === 'type') 1.112 + return mapBookmarkItemType(data); 1.113 + if (type === 'url' && data) 1.114 + return data.spec; 1.115 + return data; 1.116 +} 1.117 + 1.118 +let historyObserver = createObserverInstance(HISTORY_EVENTS, HISTORY_ARGS); 1.119 +historyService.addObserver(historyObserver, false); 1.120 + 1.121 +let bookmarkObserver = createObserverInstance(BOOKMARK_EVENTS, BOOKMARK_ARGS); 1.122 +bookmarkService.addObserver(bookmarkObserver, false); 1.123 + 1.124 +exports.events = emitter;