addon-sdk/source/lib/sdk/places/events.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 'use strict';
     7 module.metadata = {
     8   'stability': 'experimental',
     9   'engines': {
    10     'Firefox': '*'
    11   }
    12 };
    14 const { Cc, Ci } = require('chrome');
    15 const { Unknown } = require('../platform/xpcom');
    16 const { Class } = require('../core/heritage');
    17 const { merge } = require('../util/object');
    18 const bookmarkService = Cc['@mozilla.org/browser/nav-bookmarks-service;1']
    19                         .getService(Ci.nsINavBookmarksService);
    20 const historyService = Cc['@mozilla.org/browser/nav-history-service;1']
    21                        .getService(Ci.nsINavHistoryService);
    22 const { mapBookmarkItemType } = require('./utils');
    23 const { EventTarget } = require('../event/target');
    24 const { emit } = require('../event/core');
    26 const emitter = EventTarget();
    28 let HISTORY_ARGS = {
    29   onBeginUpdateBatch: [],
    30   onEndUpdateBatch: [],
    31   onClearHistory: [],
    32   onDeleteURI: ['url'],
    33   onDeleteVisits: ['url', 'visitTime'],
    34   onPageChanged: ['url', 'property', 'value'],
    35   onTitleChanged: ['url', 'title'],
    36   onVisit: [
    37     'url', 'visitId', 'time', 'sessionId', 'referringId', 'transitionType'
    38   ]
    39 };
    41 let HISTORY_EVENTS = {
    42   onBeginUpdateBatch: 'history-start-batch',
    43   onEndUpdateBatch: 'history-end-batch',
    44   onClearHistory: 'history-start-clear',
    45   onDeleteURI: 'history-delete-url',
    46   onDeleteVisits: 'history-delete-visits',
    47   onPageChanged: 'history-page-changed',
    48   onTitleChanged: 'history-title-changed',
    49   onVisit: 'history-visit'
    50 };
    52 let BOOKMARK_ARGS = {
    53   onItemAdded: [
    54     'id', 'parentId', 'index', 'type', 'url', 'title', 'dateAdded'
    55   ],
    56   onItemChanged: [
    57     'id', 'property', null, 'value', 'lastModified', 'type', 'parentId'
    58   ],
    59   onItemMoved: [
    60     'id', 'previousParentId', 'previousIndex', 'currentParentId',
    61     'currentIndex', 'type'
    62   ],
    63   onItemRemoved: ['id', 'parentId', 'index', 'type', 'url'],
    64   onItemVisited: ['id', 'visitId', 'time', 'transitionType', 'url', 'parentId']
    65 };
    67 let BOOKMARK_EVENTS = {
    68   onItemAdded: 'bookmark-item-added',
    69   onItemChanged: 'bookmark-item-changed',
    70   onItemMoved: 'bookmark-item-moved',
    71   onItemRemoved: 'bookmark-item-removed',
    72   onItemVisited: 'bookmark-item-visited',
    73 };
    75 function createHandler (type, propNames) {
    76   propNames = propNames || [];
    77   return function (...args) {
    78     let data = propNames.reduce((acc, prop, i) => {
    79       if (prop)
    80         acc[prop] = formatValue(prop, args[i]);
    81       return acc;
    82     }, {});
    84     emit(emitter, 'data', {
    85       type: type,
    86       data: data
    87     });
    88   };
    89 }
    91 /*
    92  * Creates an observer, creating handlers based off of
    93  * the `events` names, and ordering arguments from `propNames` hash
    94  */
    95 function createObserverInstance (events, propNames) {
    96   let definition = Object.keys(events).reduce((prototype, eventName) => {
    97     prototype[eventName] = createHandler(events[eventName], propNames[eventName]);
    98     return prototype;
    99   }, {});
   101   return Class(merge(definition, { extends: Unknown }))();
   102 }
   104 /*
   105  * Formats `data` based off of the value of `type`
   106  */
   107 function formatValue (type, data) {
   108   if (type === 'type')
   109     return mapBookmarkItemType(data);
   110   if (type === 'url' && data)
   111     return data.spec;
   112   return data;
   113 }
   115 let historyObserver = createObserverInstance(HISTORY_EVENTS, HISTORY_ARGS);
   116 historyService.addObserver(historyObserver, false);
   118 let bookmarkObserver = createObserverInstance(BOOKMARK_EVENTS, BOOKMARK_ARGS);
   119 bookmarkService.addObserver(bookmarkObserver, false);
   121 exports.events = emitter;

mercurial