1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is MozMill Test code.
16 * The Initial Developer of the Original Code is Mozilla Foundation.
17 * Portions created by the Initial Developer are Copyright (C) 2009
18 * the Initial Developer. All Rights Reserved.
21 * Henrik Skupin <hskupin@mozilla.com>
22 * Geo Mealer <gmealer@mozilla.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
40 * The ModalDialogAPI adds support for handling modal dialogs. It
41 * has to be used e.g. for alert boxes and other commonDialog instances.
46 // Include required modules
47 var utils = require("utils");
49 const gTimeout = 5000;
51 // Default bookmarks.html file lives in omni.jar, get via resource URI
52 const BOOKMARKS_RESOURCE = "resource:///defaults/profile/bookmarks.html";
54 // Bookmarks can take up to ten seconds to restore
55 const BOOKMARKS_TIMEOUT = 10000;
57 // Observer topics we need to watch to know whether we're finished
58 const TOPIC_BOOKMARKS_RESTORE_SUCCESS = "bookmarks-restore-success";
61 * Instance of the bookmark service to gain access to the bookmark API.
63 * @see http://mxr.mozilla.org/mozilla-central (nsINavBookmarksService.idl)
65 var bookmarksService = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
66 getService(Ci.nsINavBookmarksService);
69 * Instance of the history service to gain access to the history API.
71 * @see http://mxr.mozilla.org/mozilla-central (nsINavHistoryService.idl)
73 var historyService = Cc["@mozilla.org/browser/nav-history-service;1"].
74 getService(Ci.nsINavHistoryService);
77 * Instance of the livemark service to gain access to the livemark API
79 * @see http://mxr.mozilla.org/mozilla-central (nsILivemarkService.idl)
81 var livemarkService = Cc["@mozilla.org/browser/livemark-service;2"].
82 getService(Ci.nsILivemarkService);
85 * Instance of the browser history interface to gain access to
86 * browser-specific history API
88 * @see http://mxr.mozilla.org/mozilla-central (nsIBrowserHistory.idl)
90 var browserHistory = Cc["@mozilla.org/browser/nav-history-service;1"].
91 getService(Ci.nsIBrowserHistory);
94 * Instance of the observer service to gain access to the observer API
96 * @see http://mxr.mozilla.org/mozilla-central (nsIObserverService.idl)
98 var observerService = Cc["@mozilla.org/observer-service;1"].
99 getService(Ci.nsIObserverService);
102 * Check if an URI is bookmarked within the specified folder
104 * @param (nsIURI) uri
105 * URI of the bookmark
106 * @param {String} folderId
107 * Folder in which the search has to take place
108 * @return Returns if the URI exists in the given folder
111 function isBookmarkInFolder(uri, folderId)
113 var ids = bookmarksService.getBookmarkIdsForURI(uri, {});
114 for (let i = 0; i < ids.length; i++) {
115 if (bookmarksService.getFolderIdForItem(ids[i]) == folderId)
123 * Restore the default bookmarks for the current profile
125 function restoreDefaultBookmarks() {
126 // Set up the observer -- we're only checking for success here, so we'll simply
127 // time out and throw on failure. It makes the code much clearer than handling
128 // finished state and success state separately.
129 var importSuccessful = false;
130 var importObserver = {
131 observe: function (aSubject, aTopic, aData) {
132 if (aTopic == TOPIC_BOOKMARKS_RESTORE_SUCCESS) {
133 importSuccessful = true;
137 observerService.addObserver(importObserver, TOPIC_BOOKMARKS_RESTORE_SUCCESS, false);
140 // Fire off the import
141 var bookmarksURI = utils.createURI(BOOKMARKS_RESOURCE);
142 var importer = Cc["@mozilla.org/browser/places/import-export-service;1"].
143 getService(Ci.nsIPlacesImportExportService);
144 importer.importHTMLFromURI(bookmarksURI, true);
146 // Wait for it to be finished--the observer above will flip this flag
147 mozmill.utils.waitFor(function () {
148 return importSuccessful;
149 }, "Default bookmarks have finished importing", BOOKMARKS_TIMEOUT);
152 // Whatever happens, remove the observer afterwards
153 observerService.removeObserver(importObserver, TOPIC_BOOKMARKS_RESTORE_SUCCESS);
158 * Synchronous wrapper around browserHistory.removeAllPages()
159 * Removes history and blocks until done
161 function removeAllHistory() {
162 const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished";
164 // Create flag visible to both the eval and the observer object
169 // Set up an observer so we get notified when remove completes
171 observe: function(aSubject, aTopic, aData) {
172 observerService.removeObserver(this, TOPIC_EXPIRATION_FINISHED);
173 finishedFlag.state = true;
176 observerService.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false);
178 // Remove the pages, then block until we're done or until timeout is reached
179 browserHistory.removeAllPages();
180 mozmill.controller.waitForEval("subject.state == true", gTimeout, 100, finishedFlag);
183 // Export of variables
184 exports.bookmarksService = bookmarksService;
185 exports.historyService = historyService;
186 exports.livemarkService = livemarkService;
187 exports.browserHistory = browserHistory;
189 // Export of functions
190 exports.isBookmarkInFolder = isBookmarkInFolder;
191 exports.restoreDefaultBookmarks = restoreDefaultBookmarks;
192 exports.removeAllHistory = removeAllHistory;