summaryrefslogtreecommitdiffstats
path: root/toolkit/jetpack/sdk/places/host/host-tags.js
blob: 929a5d5af0353aca9ccc7090aed3e4c93b648781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* 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": "*",
    "SeaMonkey": "*"
  }
};

const { Cc, Ci } = require('chrome');
const taggingService = Cc["@mozilla.org/browser/tagging-service;1"].
                       getService(Ci.nsITaggingService);
const ios = Cc['@mozilla.org/network/io-service;1'].
            getService(Ci.nsIIOService);
const { URL } = require('../../url');
const { newURI } = require('../../url/utils');
const { request, response } = require('../../addon/host');
const { on, emit } = require('../../event/core');
const { filter } = require('../../event/utils');

const EVENT_MAP = {
  'sdk-places-tags-tag': tag,
  'sdk-places-tags-untag': untag,
  'sdk-places-tags-get-tags-by-url': getTagsByURL,
  'sdk-places-tags-get-urls-by-tag': getURLsByTag
};

function tag (message) {
  let data = message.data;
  let resData = {
    id: message.id,
    event: message.event
  };

  resData.data = taggingService.tagURI(newURI(data.url), data.tags);
  respond(resData);
}

function untag (message) {
  let data = message.data;
  let resData = {
    id: message.id,
    event: message.event
  };

  resData.data = taggingService.untagURI(newURI(data.url), data.tags);
  respond(resData);
}

function getURLsByTag (message) {
  let data = message.data;
  let resData = {
    id: message.id,
    event: message.event
  };

  resData.data = taggingService
    .getURIsForTag(data.tag).map(uri => uri.spec);
  respond(resData);
}

function getTagsByURL (message) {
  let data = message.data;
  let resData = {
    id: message.id,
    event: message.event
  };

  resData.data = taggingService.getTagsForURI(newURI(data.url), {});
  respond(resData);
}

/*
 * Hook into host
 */

var reqStream = filter(request, function (data) {
  return /sdk-places-tags/.test(data.event);
});

on(reqStream, 'data', function (e) {
  if (EVENT_MAP[e.event]) EVENT_MAP[e.event](e);
});

function respond (data) {
  emit(response, 'data', data);
}