places-helper.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/. */
  4. 'use strict'
  5. const { Cc, Ci } = require('chrome');
  6. const bmsrv = Cc['@mozilla.org/browser/nav-bookmarks-service;1'].
  7. getService(Ci.nsINavBookmarksService);
  8. const hsrv = Cc['@mozilla.org/browser/nav-history-service;1'].
  9. getService(Ci.nsINavHistoryService);
  10. const brsrv = Cc["@mozilla.org/browser/nav-history-service;1"]
  11. .getService(Ci.nsIBrowserHistory);
  12. const tagsrv = Cc['@mozilla.org/browser/tagging-service;1'].
  13. getService(Ci.nsITaggingService);
  14. const asyncHistory = Cc['@mozilla.org/browser/history;1'].
  15. getService(Ci.mozIAsyncHistory);
  16. const { send } = require('sdk/addon/events');
  17. const { setTimeout } = require('sdk/timers');
  18. const { newURI } = require('sdk/url/utils');
  19. const { defer, all } = require('sdk/core/promise');
  20. const { once } = require('sdk/system/events');
  21. const { set } = require('sdk/preferences/service');
  22. const {
  23. Bookmark, Group, Separator,
  24. save, search,
  25. MENU, TOOLBAR, UNSORTED
  26. } = require('sdk/places/bookmarks');
  27. function invalidResolve (assert) {
  28. return function (e) {
  29. assert.fail('Resolve state should not be called: ' + e);
  30. };
  31. }
  32. exports.invalidResolve = invalidResolve;
  33. function invalidReject (assert) {
  34. return function (e) {
  35. assert.fail('Reject state should not be called: ' + e);
  36. };
  37. }
  38. exports.invalidReject = invalidReject;
  39. // Removes all children of group
  40. function clearBookmarks (group) {
  41. group
  42. ? bmsrv.removeFolderChildren(group.id)
  43. : clearAllBookmarks();
  44. }
  45. function clearAllBookmarks () {
  46. [MENU, TOOLBAR, UNSORTED].forEach(clearBookmarks);
  47. }
  48. function clearHistory (done) {
  49. hsrv.removeAllPages();
  50. once('places-expiration-finished', done);
  51. }
  52. // Cleans bookmarks and history and disables maintanance
  53. function resetPlaces (done) {
  54. // Set last maintenance to current time to prevent
  55. // Places DB maintenance occuring and locking DB
  56. set('places.database.lastMaintenance', Math.floor(Date.now() / 1000));
  57. clearAllBookmarks();
  58. clearHistory(done);
  59. }
  60. exports.resetPlaces = resetPlaces;
  61. function compareWithHost (assert, item) {
  62. let id = item.id;
  63. let type = item.type === 'group' ? bmsrv.TYPE_FOLDER : bmsrv['TYPE_' + item.type.toUpperCase()];
  64. let url = item.url && !item.url.endsWith('/') ? item.url + '/' : item.url;
  65. if (type === bmsrv.TYPE_BOOKMARK) {
  66. assert.equal(url, bmsrv.getBookmarkURI(id).spec.toString(), 'Matches host url');
  67. let tags = tagsrv.getTagsForURI(newURI(item.url));
  68. for (let tag of tags) {
  69. // Handle both array for raw data and set for instances
  70. if (Array.isArray(item.tags))
  71. assert.ok(~item.tags.indexOf(tag), 'has correct tag');
  72. else
  73. assert.ok(item.tags.has(tag), 'has correct tag');
  74. }
  75. assert.equal(tags.length,
  76. Array.isArray(item.tags) ? item.tags.length : item.tags.size,
  77. 'matches tag count');
  78. }
  79. if (type !== bmsrv.TYPE_SEPARATOR) {
  80. assert.equal(item.title, bmsrv.getItemTitle(id), 'Matches host title');
  81. }
  82. assert.equal(item.index, bmsrv.getItemIndex(id), 'Matches host index');
  83. assert.equal(item.group.id || item.group, bmsrv.getFolderIdForItem(id), 'Matches host group id');
  84. assert.equal(type, bmsrv.getItemType(id), 'Matches host type');
  85. }
  86. exports.compareWithHost = compareWithHost;
  87. function addVisits (urls) {
  88. var deferred = defer();
  89. asyncHistory.updatePlaces([].concat(urls).map(createVisit), {
  90. handleResult: function () {},
  91. handleError: deferred.reject,
  92. handleCompletion: deferred.resolve
  93. });
  94. return deferred.promise;
  95. }
  96. exports.addVisits = addVisits;
  97. function removeVisits (urls) {
  98. [].concat(urls).map(url => {
  99. hsrv.removePage(newURI(url));
  100. });
  101. }
  102. exports.removeVisits = removeVisits;
  103. // Creates a mozIVisitInfo object
  104. function createVisit (url) {
  105. let place = {}
  106. place.uri = newURI(url);
  107. place.title = "Test visit for " + place.uri.spec;
  108. place.visits = [{
  109. transitionType: hsrv.TRANSITION_LINK,
  110. visitDate: +(new Date()) * 1000,
  111. referredURI: undefined
  112. }];
  113. return place;
  114. }
  115. function createBookmark (data) {
  116. data = data || {};
  117. let item = {
  118. title: data.title || 'Moz',
  119. url: data.url || (!data.type || data.type === 'bookmark' ?
  120. 'http://moz.com/' :
  121. undefined),
  122. tags: data.tags || (!data.type || data.type === 'bookmark' ?
  123. ['firefox'] :
  124. undefined),
  125. type: data.type || 'bookmark',
  126. group: data.group
  127. };
  128. return send('sdk-places-bookmarks-create', item);
  129. }
  130. exports.createBookmark = createBookmark;
  131. function createBookmarkItem (data) {
  132. let deferred = defer();
  133. data = data || {};
  134. save({
  135. title: data.title || 'Moz',
  136. url: data.url || 'http://moz.com/',
  137. tags: data.tags || (!data.type || data.type === 'bookmark' ?
  138. ['firefox'] :
  139. undefined),
  140. type: data.type || 'bookmark',
  141. group: data.group
  142. }).on('end', function (bookmark) {
  143. deferred.resolve(bookmark[0]);
  144. });
  145. return deferred.promise;
  146. }
  147. exports.createBookmarkItem = createBookmarkItem;
  148. function createBookmarkTree () {
  149. let agg = [];
  150. return createBookmarkItem({ type: 'group', title: 'mozgroup' })
  151. .then(group => {
  152. agg.push(group);
  153. return all([createBookmarkItem({
  154. title: 'mozilla.com',
  155. url: 'http://mozilla.com/',
  156. group: group,
  157. tags: ['mozilla', 'firefox', 'thunderbird', 'rust']
  158. }), createBookmarkItem({
  159. title: 'mozilla.org',
  160. url: 'http://mozilla.org/',
  161. group: group,
  162. tags: ['mozilla', 'firefox', 'thunderbird', 'rust']
  163. }), createBookmarkItem({
  164. title: 'firefox',
  165. url: 'http://firefox.com/',
  166. group: group,
  167. tags: ['mozilla', 'firefox', 'browser']
  168. }), createBookmarkItem({
  169. title: 'thunderbird',
  170. url: 'http://mozilla.org/thunderbird/',
  171. group: group,
  172. tags: ['mozilla', 'thunderbird', 'email']
  173. }), createBookmarkItem({
  174. title: 'moz subfolder',
  175. group: group,
  176. type: 'group'
  177. })
  178. ]);
  179. })
  180. .then(results => {
  181. agg = agg.concat(results);
  182. let subfolder = results.filter(item => item.type === 'group')[0];
  183. return createBookmarkItem({
  184. title: 'dark javascript secrets',
  185. url: 'http://w3schools.com',
  186. group: subfolder,
  187. tags: []
  188. });
  189. }).then(item => {
  190. agg.push(item);
  191. return createBookmarkItem(
  192. { type: 'group', group: MENU, title: 'other stuff' }
  193. );
  194. }).then(newGroup => {
  195. agg.push(newGroup);
  196. return all([
  197. createBookmarkItem({
  198. title: 'mdn',
  199. url: 'http://developer.mozilla.org/en-US/',
  200. group: newGroup,
  201. tags: ['javascript']
  202. }),
  203. createBookmarkItem({
  204. title: 'web audio',
  205. url: 'http://webaud.io',
  206. group: newGroup,
  207. tags: ['javascript', 'web audio']
  208. }),
  209. createBookmarkItem({
  210. title: 'web audio components',
  211. url: 'http://component.fm',
  212. group: newGroup,
  213. tags: ['javascript', 'web audio', 'components']
  214. })
  215. ]);
  216. }).then(results => {
  217. agg = agg.concat(results);
  218. return agg;
  219. });
  220. }
  221. exports.createBookmarkTree = createBookmarkTree;