diff --git a/b2g/app/b2g.js b/b2g/app/b2g.js index 7e791e01ae..9d5dc47808 100644 --- a/b2g/app/b2g.js +++ b/b2g/app/b2g.js @@ -338,63 +338,6 @@ pref("dom.w3c_touch_events.enabled", 1); pref("dom.w3c_touch_events.safetyX", 0); // escape borders in units of 1/240" pref("dom.w3c_touch_events.safetyY", 120); // escape borders in units of 1/240" -#ifdef MOZ_SAFE_BROWSING -// Safe browsing does nothing unless this pref is set -pref("browser.safebrowsing.enabled", false); - -// Prevent loading of pages identified as malware -pref("browser.safebrowsing.malware.enabled", false); - -pref("browser.safebrowsing.debug", false); -pref("browser.safebrowsing.updateURL", "https://safebrowsing.google.com/safebrowsing/downloads?client=SAFEBROWSING_ID&appver=%VERSION%&pver=2.2&key=%GOOGLE_API_KEY%"); -pref("browser.safebrowsing.gethashURL", "https://safebrowsing.google.com/safebrowsing/gethash?client=SAFEBROWSING_ID&appver=%VERSION%&pver=2.2"); -pref("browser.safebrowsing.reportURL", "https://safebrowsing.google.com/safebrowsing/report?"); -pref("browser.safebrowsing.reportGenericURL", "http://%LOCALE%.phish-generic.mozilla.com/?hl=%LOCALE%"); -pref("browser.safebrowsing.reportErrorURL", "http://%LOCALE%.phish-error.mozilla.com/?hl=%LOCALE%"); -pref("browser.safebrowsing.reportPhishURL", "http://%LOCALE%.phish-report.mozilla.com/?hl=%LOCALE%"); -pref("browser.safebrowsing.reportMalwareURL", "http://%LOCALE%.malware-report.mozilla.com/?hl=%LOCALE%"); -pref("browser.safebrowsing.reportMalwareErrorURL", "http://%LOCALE%.malware-error.mozilla.com/?hl=%LOCALE%"); -pref("browser.safebrowsing.appRepURL", "https://sb-ssl.google.com/safebrowsing/clientreport/download?key=%GOOGLE_API_KEY%"); - -pref("browser.safebrowsing.id", "Firefox"); - -// Tables for application reputation. -pref("urlclassifier.downloadBlockTable", "goog-badbinurl-shavar"); - -// Non-enhanced mode (local url lists) URL list to check for updates -pref("browser.safebrowsing.provider.0.updateURL", "https://safebrowsing.google.com/safebrowsing/downloads?client={moz:client}&appver={moz:version}&pver=2.2&key=%GOOGLE_API_KEY%"); - -pref("browser.safebrowsing.dataProvider", 0); - -// Does the provider name need to be localizable? -pref("browser.safebrowsing.provider.0.name", "Google"); -pref("browser.safebrowsing.provider.0.reportURL", "https://safebrowsing.google.com/safebrowsing/report?"); -pref("browser.safebrowsing.provider.0.gethashURL", "https://safebrowsing.google.com/safebrowsing/gethash?client={moz:client}&appver={moz:version}&pver=2.2"); - -// HTML report pages -pref("browser.safebrowsing.provider.0.reportGenericURL", "http://{moz:locale}.phish-generic.mozilla.com/?hl={moz:locale}"); -pref("browser.safebrowsing.provider.0.reportErrorURL", "http://{moz:locale}.phish-error.mozilla.com/?hl={moz:locale}"); -pref("browser.safebrowsing.provider.0.reportPhishURL", "http://{moz:locale}.phish-report.mozilla.com/?hl={moz:locale}"); -pref("browser.safebrowsing.provider.0.reportMalwareURL", "http://{moz:locale}.malware-report.mozilla.com/?hl={moz:locale}"); -pref("browser.safebrowsing.provider.0.reportMalwareErrorURL", "http://{moz:locale}.malware-error.mozilla.com/?hl={moz:locale}"); - -// FAQ URLs - -// The number of random entries to send with a gethash request. -pref("urlclassifier.gethashnoise", 4); - -// Gethash timeout for Safebrowsing. -pref("urlclassifier.gethash.timeout_ms", 5000); - -// If an urlclassifier table has not been updated in this number of seconds, -// a gethash request will be forced to check that the result is still in -// the database. -pref("urlclassifier.max-complete-age", 2700); - -// URL for checking the reason for a malware warning. -pref("browser.safebrowsing.malware.reportURL", "https://safebrowsing.google.com/safebrowsing/diagnostic?client=%NAME%&hl=%LOCALE%&site="); -#endif - // True if this is the first time we are showing about:firstrun pref("browser.firstrun.show.uidiscovery", true); pref("browser.firstrun.show.localepicker", true); diff --git a/b2g/chrome/content/shell.js b/b2g/chrome/content/shell.js index 2aba9ac5a6..3e7ed5821c 100644 --- a/b2g/chrome/content/shell.js +++ b/b2g/chrome/content/shell.js @@ -70,11 +70,6 @@ XPCOMUtils.defineLazyServiceGetter(Services, 'captivePortalDetector', 'nsICaptivePortalDetector'); #endif -#ifdef MOZ_SAFE_BROWSING -XPCOMUtils.defineLazyModuleGetter(this, "SafeBrowsing", - "resource://gre/modules/SafeBrowsing.jsm"); -#endif - function getContentWindow() { return shell.contentBrowser.contentWindow; } @@ -353,11 +348,6 @@ var shell = { ppmm.addMessageListener("sms-handler", this); ppmm.addMessageListener("mail-handler", this); ppmm.addMessageListener("file-picker", this); -#ifdef MOZ_SAFE_BROWSING - setTimeout(function() { - SafeBrowsing.init(); - }, 5000); -#endif }, stop: function shell_stop() { diff --git a/b2g/confvars.sh b/b2g/confvars.sh index cd309b6ade..a59eab08f3 100755 --- a/b2g/confvars.sh +++ b/b2g/confvars.sh @@ -17,7 +17,6 @@ MOZ_BRANDING_DIRECTORY=b2g/branding/unofficial MOZ_OFFICIAL_BRANDING_DIRECTORY=b2g/branding/official # MOZ_APP_DISPLAYNAME is set by branding/configure.sh -MOZ_SAFE_BROWSING=1 MOZ_SERVICES_COMMON=1 MOZ_SERVICES_METRICS=1 MOZ_CAPTIVEDETECT=1 diff --git a/browser/app/profile/firefox.js b/browser/app/profile/firefox.js index 5664050d86..87b6f7461e 100644 --- a/browser/app/profile/firefox.js +++ b/browser/app/profile/firefox.js @@ -726,46 +726,6 @@ pref("goanna.handlerService.schemes.ircs.3.uriTemplate", "chrome://browser-regio // By default, we don't want protocol/content handlers to be registered from a different host, see bug 402287 pref("goanna.handlerService.allowRegisterFromDifferentHost", false); -#ifdef MOZ_SAFE_BROWSING -pref("browser.safebrowsing.enabled", true); -pref("browser.safebrowsing.malware.enabled", true); -pref("browser.safebrowsing.debug", false); - -pref("browser.safebrowsing.updateURL", "http://safebrowsing.clients.google.com/safebrowsing/downloads?client=SAFEBROWSING_ID&appver=%VERSION%&pver=2.2"); -pref("browser.safebrowsing.keyURL", "https://sb-ssl.google.com/safebrowsing/newkey?client=SAFEBROWSING_ID&appver=%VERSION%&pver=2.2"); -pref("browser.safebrowsing.gethashURL", "http://safebrowsing.clients.google.com/safebrowsing/gethash?client=SAFEBROWSING_ID&appver=%VERSION%&pver=2.2"); -pref("browser.safebrowsing.reportURL", "http://safebrowsing.clients.google.com/safebrowsing/report?"); -pref("browser.safebrowsing.reportGenericURL", "http://%LOCALE%.phish-generic.mozilla.com/?hl=%LOCALE%"); -pref("browser.safebrowsing.reportErrorURL", "http://%LOCALE%.phish-error.mozilla.com/?hl=%LOCALE%"); -pref("browser.safebrowsing.reportPhishURL", "http://%LOCALE%.phish-report.mozilla.com/?hl=%LOCALE%"); -pref("browser.safebrowsing.reportMalwareURL", "http://%LOCALE%.malware-report.mozilla.com/?hl=%LOCALE%"); -pref("browser.safebrowsing.reportMalwareErrorURL", "http://%LOCALE%.malware-error.mozilla.com/?hl=%LOCALE%"); - -pref("browser.safebrowsing.warning.infoURL", "https://www.mozilla.org/%LOCALE%/firefox/phishing-protection/"); -pref("browser.safebrowsing.malware.reportURL", "http://safebrowsing.clients.google.com/safebrowsing/diagnostic?client=%NAME%&hl=%LOCALE%&site="); - -#ifdef MOZILLA_OFFICIAL -// Normally the "client ID" sent in updates is appinfo.name, but for -// official Firefox releases from Mozilla we use a special identifier. -pref("browser.safebrowsing.id", "navclient-auto-ffox"); -#endif - -// Name of the about: page contributed by safebrowsing to handle display of error -// pages on phishing/malware hits. (bug 399233) -pref("urlclassifier.alternate_error_page", "blocked"); - -// The number of random entries to send with a gethash request. -pref("urlclassifier.gethashnoise", 4); - -// The list of tables that use the gethash request to confirm partial results. -pref("urlclassifier.gethashtables", "goog-phish-shavar,goog-malware-shavar"); - -// If an urlclassifier table has not been updated in this number of seconds, -// a gethash request will be forced to check that the result is still in -// the database. -pref("urlclassifier.max-complete-age", 2700); -#endif - pref("browser.geolocation.warning.infoURL", "https://www.mozilla.org/%LOCALE%/firefox/geolocation/"); pref("browser.mixedcontent.warning.infoURL", "http://support.mozilla.org/1/%APP%/%VERSION%/%OS%/%LOCALE%/mixed-content/"); @@ -950,10 +910,6 @@ pref("services.sync.prefs.sync.browser.download.manager.showWhenStarting", true) pref("services.sync.prefs.sync.browser.formfill.enable", true); pref("services.sync.prefs.sync.browser.link.open_newwindow", true); pref("services.sync.prefs.sync.browser.offline-apps.notify", true); -#ifdef MOZ_SAFE_BROWSING -pref("services.sync.prefs.sync.browser.safebrowsing.enabled", true); -pref("services.sync.prefs.sync.browser.safebrowsing.malware.enabled", true); -#endif pref("services.sync.prefs.sync.browser.search.selectedEngine", true); pref("services.sync.prefs.sync.browser.search.update", true); pref("services.sync.prefs.sync.browser.sessionstore.restore_on_demand", true); diff --git a/browser/base/content/browser-doctype.inc b/browser/base/content/browser-doctype.inc index bd0cd28b7b..6ee6384b64 100644 --- a/browser/base/content/browser-doctype.inc +++ b/browser/base/content/browser-doctype.inc @@ -13,10 +13,6 @@ %customizeToolbarDTD; %placesDTD; -#ifdef MOZ_SAFE_BROWSING - -%safebrowsingDTD; -#endif %aboutHomeDTD; ]> diff --git a/browser/base/content/browser-safebrowsing.js b/browser/base/content/browser-safebrowsing.js deleted file mode 100644 index e40a31957d..0000000000 --- a/browser/base/content/browser-safebrowsing.js +++ /dev/null @@ -1,53 +0,0 @@ -# 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/. - -#ifdef MOZ_SAFE_BROWSING -var gSafeBrowsing = { - - setReportPhishingMenu: function() { - - // A phishing page will have a specific about:blocked content documentURI - var isPhishingPage = content.document.documentURI.startsWith("about:blocked?e=phishingBlocked"); - - // Show/hide the appropriate menu item. - document.getElementById("menu_HelpPopup_reportPhishingtoolmenu") - .hidden = isPhishingPage; - document.getElementById("menu_HelpPopup_reportPhishingErrortoolmenu") - .hidden = !isPhishingPage; - - var broadcasterId = isPhishingPage - ? "reportPhishingErrorBroadcaster" - : "reportPhishingBroadcaster"; - - var broadcaster = document.getElementById(broadcasterId); - if (!broadcaster) - return; - - var uri = getBrowser().currentURI; - if (uri && (uri.schemeIs("http") || uri.schemeIs("https"))) - broadcaster.removeAttribute("disabled"); - else - broadcaster.setAttribute("disabled", true); - }, - - /** - * Used to report a phishing page or a false positive - * @param name String One of "Phish", "Error", "Malware" or "MalwareError" - * @return String the report phishing URL. - */ - getReportURL: function(name) { - var reportUrl = SafeBrowsing.getReportURL(name); - - var pageUri = gBrowser.currentURI.clone(); - - // Remove the query to avoid including potentially sensitive data - if (pageUri instanceof Ci.nsIURL) - pageUri.query = ''; - - reportUrl += "&url=" + encodeURIComponent(pageUri.asciiSpec); - - return reportUrl; - } -} -#endif diff --git a/browser/base/content/browser.js b/browser/base/content/browser.js index 09c39f1d2c..b6afa9832b 100644 --- a/browser/base/content/browser.js +++ b/browser/base/content/browser.js @@ -125,11 +125,6 @@ XPCOMUtils.defineLazyGetter(this, "BrowserDebuggerProcess", function() { XPCOMUtils.defineLazyModuleGetter(this, "PageThumbs", "resource://gre/modules/PageThumbs.jsm"); -#ifdef MOZ_SAFE_BROWSING -XPCOMUtils.defineLazyModuleGetter(this, "SafeBrowsing", - "resource://gre/modules/SafeBrowsing.jsm"); -#endif - XPCOMUtils.defineLazyModuleGetter(this, "gBrowserNewTabPreloader", "resource:///modules/BrowserNewTabPreloader.jsm", "BrowserNewTabPreloader"); @@ -154,9 +149,6 @@ let gInitialPages = [ #include browser-fullZoom.js #include browser-places.js #include browser-plugins.js -#ifdef MOZ_SAFE_BROWSING -#include browser-safebrowsing.js -#endif #include browser-tabPreviews.js #include browser-thumbnails.js #include browser-webrtcUI.js @@ -984,11 +976,6 @@ var gBrowserInit = { loadOneOrMoreURIs(uriToLoad); } -#ifdef MOZ_SAFE_BROWSING - // Bug 778855 - Perf regression if we do this here. To be addressed in bug 779008. - setTimeout(function() { SafeBrowsing.init(); }, 2000); -#endif - Services.obs.addObserver(gSessionHistoryObserver, "browser:purge-session-history", false); Services.obs.addObserver(gXPInstallObserver, "addon-install-disabled", false); Services.obs.addObserver(gXPInstallObserver, "addon-install-started", false); @@ -2374,11 +2361,6 @@ let BrowserOnClick = { if (ownerDoc.documentURI.startsWith("about:certerror")) { this.onAboutCertError(originalTarget, ownerDoc); } -#ifdef MOZ_SAFE_BROWSING - else if (ownerDoc.documentURI.startsWith("about:blocked")) { - this.onAboutBlocked(originalTarget, ownerDoc); - } -#endif else if (ownerDoc.documentURI.startsWith("about:neterror")) { this.onAboutNetError(originalTarget, ownerDoc); } @@ -2428,115 +2410,6 @@ let BrowserOnClick = { } }, -#ifdef MOZ_SAFE_BROWSING - onAboutBlocked: function BrowserOnClick_onAboutBlocked(aTargetElm, aOwnerDoc) { - let elmId = aTargetElm.getAttribute("id"); - - // The event came from a button on a malware/phishing block page - // First check whether it's malware or phishing, so that we can - // use the right strings/links - let isMalware = /e=malwareBlocked/.test(aOwnerDoc.documentURI); - let bucketName = isMalware ? "WARNING_MALWARE_PAGE_":"WARNING_PHISHING_PAGE_"; - let nsISecTel = Ci.nsISecurityUITelemetry; - let isIframe = (aOwnerDoc.defaultView.parent === aOwnerDoc.defaultView); - bucketName += isIframe ? "TOP_" : "FRAME_"; - - switch (elmId) { - case "getMeOutButton": - getMeOutOfHere(); - break; - - case "reportButton": - // This is the "Why is this site blocked" button. For malware, - // we can fetch a site-specific report, for phishing, we redirect - // to the generic page describing phishing protection. - - if (isMalware) { - // Get the stop badware "why is this blocked" report url, - // append the current url, and go there. - try { - let reportURL = formatURL("browser.safebrowsing.malware.reportURL", true); - reportURL += aOwnerDoc.location.href; - content.location = reportURL; - } catch (e) { - Components.utils.reportError("Couldn't get malware report URL: " + e); - } - } - else { // It's a phishing site, not malware - try { - content.location = formatURL("browser.safebrowsing.warning.infoURL", true); - } catch (e) { - Components.utils.reportError("Couldn't get phishing info URL: " + e); - } - } - break; - - case "ignoreWarningButton": - this.ignoreWarningButton(isMalware); - break; - } - }, - - ignoreWarningButton: function BrowserOnClick_ignoreWarningButton(aIsMalware) { - // Allow users to override and continue through to the site, - // but add a notify bar as a reminder, so that they don't lose - // track after, e.g., tab switching. - gBrowser.loadURIWithFlags(content.location.href, - nsIWebNavigation.LOAD_FLAGS_BYPASS_CLASSIFIER, - null, null, null); - - Services.perms.add(makeURI(content.location.href), "safe-browsing", - Ci.nsIPermissionManager.ALLOW_ACTION, - Ci.nsIPermissionManager.EXPIRE_SESSION); - - let buttons = [{ - label: gNavigatorBundle.getString("safebrowsing.getMeOutOfHereButton.label"), - accessKey: gNavigatorBundle.getString("safebrowsing.getMeOutOfHereButton.accessKey"), - callback: function() { getMeOutOfHere(); } - }]; - - let title; - if (aIsMalware) { - title = gNavigatorBundle.getString("safebrowsing.reportedAttackSite"); - buttons[1] = { - label: gNavigatorBundle.getString("safebrowsing.notAnAttackButton.label"), - accessKey: gNavigatorBundle.getString("safebrowsing.notAnAttackButton.accessKey"), - callback: function() { - openUILinkIn(gSafeBrowsing.getReportURL('MalwareError'), 'tab'); - } - }; - } else { - title = gNavigatorBundle.getString("safebrowsing.reportedWebForgery"); - buttons[1] = { - label: gNavigatorBundle.getString("safebrowsing.notAForgeryButton.label"), - accessKey: gNavigatorBundle.getString("safebrowsing.notAForgeryButton.accessKey"), - callback: function() { - openUILinkIn(gSafeBrowsing.getReportURL('Error'), 'tab'); - } - }; - } - - let notificationBox = gBrowser.getNotificationBox(); - let value = "blocked-badware-page"; - - let previousNotification = notificationBox.getNotificationWithValue(value); - if (previousNotification) { - notificationBox.removeNotification(previousNotification); - } - - let notification = notificationBox.appendNotification( - title, - value, - "chrome://global/skin/icons/blacklist_favicon.png", - notificationBox.PRIORITY_CRITICAL_HIGH, - buttons - ); - // Persist the notification until the user removes so it - // doesn't get removed on redirects. - notification.persistence = -1; - }, -#endif - onAboutNetError: function BrowserOnClick_onAboutNetError(aTargetElm, aOwnerDoc) { let elmId = aTargetElm.getAttribute("id"); if (elmId != "errorTryAgain" || !/e=netOffline/.test(aOwnerDoc.documentURI)) diff --git a/browser/base/jar.mn b/browser/base/jar.mn index 42b56a91eb..f95fe4b462 100644 --- a/browser/base/jar.mn +++ b/browser/base/jar.mn @@ -131,8 +131,3 @@ browser.jar: # the following files are browser-specific overrides * content/browser/license.html (/toolkit/content/license.html) % override chrome://global/content/license.html chrome://browser/content/license.html -#ifdef MOZ_SAFE_BROWSING - content/browser/report-phishing-overlay.xul (content/report-phishing-overlay.xul) - content/browser/blockedSite.xhtml (content/blockedSite.xhtml) -% overlay chrome://browser/content/browser.xul chrome://browser/content/report-phishing-overlay.xul -#endif diff --git a/browser/components/about/AboutRedirector.cpp b/browser/components/about/AboutRedirector.cpp index 88a8472922..d5effc7780 100644 --- a/browser/components/about/AboutRedirector.cpp +++ b/browser/components/about/AboutRedirector.cpp @@ -33,12 +33,6 @@ struct RedirEntry { URI_SAFE_FOR_UNTRUSTED_CONTENT. */ static RedirEntry kRedirMap[] = { -#ifdef MOZ_SAFE_BROWSING - { "blocked", "chrome://browser/content/blockedSite.xhtml", - nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT | - nsIAboutModule::ALLOW_SCRIPT | - nsIAboutModule::HIDE_FROM_ABOUTABOUT }, -#endif { "certerror", "chrome://browser/content/certerror/aboutCertError.xhtml", nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT | nsIAboutModule::ALLOW_SCRIPT | diff --git a/browser/components/build/nsModule.cpp b/browser/components/build/nsModule.cpp index ed18dd74a6..d5b79b455d 100644 --- a/browser/components/build/nsModule.cpp +++ b/browser/components/build/nsModule.cpp @@ -85,9 +85,6 @@ static const mozilla::Module::ContractIDEntry kBrowserContracts[] = { { NS_SHELLSERVICE_CONTRACTID, &kNS_SHELLSERVICE_CID }, #endif { NS_FEEDSNIFFER_CONTRACTID, &kNS_FEEDSNIFFER_CID }, -#ifdef MOZ_SAFE_BROWSING - { NS_ABOUT_MODULE_CONTRACTID_PREFIX "blocked", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, -#endif { NS_ABOUT_MODULE_CONTRACTID_PREFIX "certerror", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, { NS_ABOUT_MODULE_CONTRACTID_PREFIX "socialerror", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, { NS_ABOUT_MODULE_CONTRACTID_PREFIX "feeds", &kNS_BROWSER_ABOUT_REDIRECTOR_CID }, diff --git a/browser/components/moz.build b/browser/components/moz.build index bf1953766b..7514c7cd76 100644 --- a/browser/components/moz.build +++ b/browser/components/moz.build @@ -23,9 +23,6 @@ DIRS += [ if CONFIG['MOZ_BROWSER_SIDEBAR']: DIRS += ['sidebar'] -if CONFIG['MOZ_SAFE_BROWSING']: - DIRS += ['safebrowsing'] - DIRS += ['build'] XPIDL_SOURCES += [ diff --git a/browser/components/preferences/security.xul b/browser/components/preferences/security.xul index 1db96273c9..35a6e1b66e 100644 --- a/browser/components/preferences/security.xul +++ b/browser/components/preferences/security.xul @@ -66,18 +66,6 @@ accesskey="&addonExceptions.accesskey;" oncommand="gSecurityPane.showAddonExceptions();"/> - -#ifdef MOZ_SAFE_BROWSING - - - -#endif diff --git a/browser/components/safebrowsing/moz.build b/browser/components/safebrowsing/moz.build deleted file mode 100644 index 8deb0b1382..0000000000 --- a/browser/components/safebrowsing/moz.build +++ /dev/null @@ -1,7 +0,0 @@ -# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*- -# vim: set filetype=python: -# 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/. - - diff --git a/browser/installer/package-manifest.in b/browser/installer/package-manifest.in index e617716a5e..5b4dce5414 100644 --- a/browser/installer/package-manifest.in +++ b/browser/installer/package-manifest.in @@ -635,15 +635,6 @@ @RESPATH@/browser/modules/* @RESPATH@/modules/* -; Safe Browsing -#ifdef MOZ_URL_CLASSIFIER -@RESPATH@/components/nsURLClassifier.manifest -@RESPATH@/components/nsUrlClassifierHashCompleter.js -@RESPATH@/components/nsUrlClassifierListManager.js -@RESPATH@/components/nsUrlClassifierLib.js -@RESPATH@/components/url-classifier.xpt -#endif - ; ANGLE GLES-on-D3D rendering library #ifdef MOZ_ANGLE_RENDERER @BINPATH@/libEGL.dll diff --git a/browser/locales/en-US/chrome/browser/safebrowsing/phishing-afterload-warning-message.dtd b/browser/locales/en-US/chrome/browser/safebrowsing/phishing-afterload-warning-message.dtd deleted file mode 100644 index eb8b71dc77..0000000000 --- a/browser/locales/en-US/chrome/browser/safebrowsing/phishing-afterload-warning-message.dtd +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - has been reported as an attack page and has been blocked based on your security preferences."> -Attack pages try to install programs that steal private information, use your computer to attack others, or damage your system.

Some attack pages intentionally distribute harmful software, but many are compromised without the knowledge or permission of their owners.

"> - - - - has been reported as a web forgery and has been blocked based on your security preferences."> -Web forgeries are designed to trick you into revealing personal or financial information by imitating sources you may trust.

Entering any information on this web page may result in identity theft or other fraud.

"> diff --git a/browser/locales/en-US/chrome/browser/safebrowsing/report-phishing.dtd b/browser/locales/en-US/chrome/browser/safebrowsing/report-phishing.dtd deleted file mode 100644 index 863e124545..0000000000 --- a/browser/locales/en-US/chrome/browser/safebrowsing/report-phishing.dtd +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/browser/locales/jar.mn b/browser/locales/jar.mn index 9619653b8e..a6499c1f25 100644 --- a/browser/locales/jar.mn +++ b/browser/locales/jar.mn @@ -101,10 +101,6 @@ locale/browser/places/bookmarkProperties.properties (%chrome/browser/places/bookmarkProperties.properties) locale/browser/preferences/selectBookmark.dtd (%chrome/browser/preferences/selectBookmark.dtd) locale/browser/places/moveBookmarks.dtd (%chrome/browser/places/moveBookmarks.dtd) -#ifdef MOZ_SAFE_BROWSING - locale/browser/safebrowsing/phishing-afterload-warning-message.dtd (%chrome/browser/safebrowsing/phishing-afterload-warning-message.dtd) - locale/browser/safebrowsing/report-phishing.dtd (%chrome/browser/safebrowsing/report-phishing.dtd) -#endif locale/browser/feeds/subscribe.dtd (%chrome/browser/feeds/subscribe.dtd) locale/browser/feeds/subscribe.properties (%chrome/browser/feeds/subscribe.properties) locale/browser/migration/migration.dtd (%chrome/browser/migration/migration.dtd) diff --git a/configure.in b/configure.in index 34db266ec7..4fc500c68d 100644 --- a/configure.in +++ b/configure.in @@ -3895,14 +3895,12 @@ MOZ_SOCIAL=1 MOZ_PREF_EXTENSIONS=1 MOZ_PROFILELOCKING=1 MOZ_REFLOW_PERF= -MOZ_SAFE_BROWSING= MOZ_HELP_VIEWER= MOZ_SPELLCHECK=1 MOZ_ANDROID_APZ= MOZ_TOOLKIT_SEARCH=1 MOZ_UI_LOCALE=en-US MOZ_UNIVERSALCHARDET=1 -MOZ_URL_CLASSIFIER= MOZ_XUL=1 MOZ_ZIPWRITER=1 NS_PRINTING=1 @@ -6675,34 +6673,6 @@ if test -n "$MOZ_HELP_VIEWER"; then AC_DEFINE(MOZ_HELP_VIEWER) fi -dnl ======================================================== -dnl = Enable safe browsing (anti-phishing) -dnl ======================================================== -MOZ_ARG_ENABLE_BOOL(safe-browsing, -[ --enable-safe-browsing Enable safe browsing (anti-phishing) implementation], - MOZ_SAFE_BROWSING=1, - MOZ_SAFE_BROWSING= ) -if test -n "$MOZ_SAFE_BROWSING"; then - AC_DEFINE(MOZ_SAFE_BROWSING) -fi -AC_SUBST(MOZ_SAFE_BROWSING) - -dnl ======================================================== -dnl = Enable url-classifier -dnl ======================================================== -dnl Implicitly enabled by default if building with safe-browsing -if test -n "$MOZ_SAFE_BROWSING"; then - MOZ_URL_CLASSIFIER=1 -fi -MOZ_ARG_ENABLE_BOOL(url-classifier, -[ --enable-url-classifier Enable url classifier module], - MOZ_URL_CLASSIFIER=1, - MOZ_URL_CLASSIFIER= ) -if test -n "$MOZ_URL_CLASSIFIER"; then - AC_DEFINE(MOZ_URL_CLASSIFIER) -fi -AC_SUBST(MOZ_URL_CLASSIFIER) - dnl ======================================================== dnl = Disable zipwriter dnl ======================================================== diff --git a/mobile/android/app/mobile.js b/mobile/android/app/mobile.js index a848fb80ba..6b70aea847 100644 --- a/mobile/android/app/mobile.js +++ b/mobile/android/app/mobile.js @@ -576,40 +576,6 @@ pref("shumway.disabled", true); // enable touch events interfaces pref("dom.w3c_touch_events.enabled", 1); -#ifdef MOZ_SAFE_BROWSING -pref("browser.safebrowsing.enabled", true); -pref("browser.safebrowsing.malware.enabled", true); -pref("browser.safebrowsing.debug", false); - -pref("browser.safebrowsing.updateURL", "https://safebrowsing.google.com/safebrowsing/downloads?client=SAFEBROWSING_ID&appver=%VERSION%&pver=2.2&key=%GOOGLE_API_KEY%"); -pref("browser.safebrowsing.gethashURL", "https://safebrowsing.google.com/safebrowsing/gethash?client=SAFEBROWSING_ID&appver=%VERSION%&pver=2.2"); -pref("browser.safebrowsing.reportURL", "https://safebrowsing.google.com/safebrowsing/report?"); -pref("browser.safebrowsing.reportGenericURL", "http://%LOCALE%.phish-generic.mozilla.com/?hl=%LOCALE%"); -pref("browser.safebrowsing.reportErrorURL", "http://%LOCALE%.phish-error.mozilla.com/?hl=%LOCALE%"); -pref("browser.safebrowsing.reportPhishURL", "http://%LOCALE%.phish-report.mozilla.com/?hl=%LOCALE%"); -pref("browser.safebrowsing.reportMalwareURL", "http://%LOCALE%.malware-report.mozilla.com/?hl=%LOCALE%"); -pref("browser.safebrowsing.reportMalwareErrorURL", "http://%LOCALE%.malware-error.mozilla.com/?hl=%LOCALE%"); - -pref("browser.safebrowsing.malware.reportURL", "https://safebrowsing.google.com/safebrowsing/diagnostic?client=%NAME%&hl=%LOCALE%&site="); - -pref("browser.safebrowsing.id", @MOZ_APP_UA_NAME@); - -// Name of the about: page contributed by safebrowsing to handle display of error -// pages on phishing/malware hits. (bug 399233) -pref("urlclassifier.alternate_error_page", "blocked"); - -// The number of random entries to send with a gethash request. -pref("urlclassifier.gethashnoise", 4); - -// Gethash timeout for Safebrowsing. -pref("urlclassifier.gethash.timeout_ms", 5000); - -// If an urlclassifier table has not been updated in this number of seconds, -// a gethash request will be forced to check that the result is still in -// the database. -pref("urlclassifier.max-complete-age", 2700); -#endif - // URL for posting tiles metrics. #ifdef RELEASE_BUILD pref("browser.tiles.reportURL", "https://tiles.services.mozilla.com/v2/links/click"); diff --git a/mobile/android/chrome/content/browser.js b/mobile/android/chrome/content/browser.js index 70f12b7f37..fdb88062dc 100644 --- a/mobile/android/chrome/content/browser.js +++ b/mobile/android/chrome/content/browser.js @@ -57,11 +57,6 @@ XPCOMUtils.defineLazyModuleGetter(this, "LoginManagerParent", XPCOMUtils.defineLazyModuleGetter(this, "Task", "resource://gre/modules/Task.jsm"); XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm"); -if (AppConstants.MOZ_SAFE_BROWSING) { - XPCOMUtils.defineLazyModuleGetter(this, "SafeBrowsing", - "resource://gre/modules/SafeBrowsing.jsm"); -} - XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils", "resource://gre/modules/PrivateBrowsingUtils.jsm"); @@ -381,11 +376,6 @@ var BrowserApp = { CastingApps.init(); DownloadNotifications.init(); - if (AppConstants.MOZ_SAFE_BROWSING) { - // Bug 778855 - Perf regression if we do this here. To be addressed in bug 779008. - SafeBrowsing.init(); - }; - // Delay this a minute because there's no rush setTimeout(() => { BrowserApp.gmpInstallManager = new GMPInstallManager(); diff --git a/mobile/android/components/MobileComponents.manifest b/mobile/android/components/MobileComponents.manifest index 9e33376a59..e2cb508104 100644 --- a/mobile/android/components/MobileComponents.manifest +++ b/mobile/android/components/MobileComponents.manifest @@ -15,9 +15,6 @@ contract @mozilla.org/network/protocol/about;1?what=privatebrowsing {322ba47e-70 #ifdef MOZ_SERVICES_HEALTHREPORT contract @mozilla.org/network/protocol/about;1?what=healthreport {322ba47e-7047-4f71-aebf-cb7d69325cd9} #endif -#ifdef MOZ_SAFE_BROWSING -contract @mozilla.org/network/protocol/about;1?what=blocked {322ba47e-7047-4f71-aebf-cb7d69325cd9} -#endif #ifdef MOZ_DEVICES contract @mozilla.org/network/protocol/about;1?what=devices {322ba47e-7047-4f71-aebf-cb7d69325cd9} #endif diff --git a/mobile/android/confvars.sh b/mobile/android/confvars.sh index 85de36d794..003f340ba5 100755 --- a/mobile/android/confvars.sh +++ b/mobile/android/confvars.sh @@ -16,8 +16,6 @@ MOZ_OFFICIAL_BRANDING_DIRECTORY=mobile/android/branding/official # See the --enable-android-min-sdk and --enable-android-max-sdk arguments in configure.in. MOZ_ANDROID_MIN_SDK_VERSION=9 -MOZ_SAFE_BROWSING=1 - MOZ_NO_SMART_CARDS=1 # Enable getUserMedia diff --git a/mobile/android/installer/package-manifest.in b/mobile/android/installer/package-manifest.in index ae9e77b422..e8bf5cae62 100644 --- a/mobile/android/installer/package-manifest.in +++ b/mobile/android/installer/package-manifest.in @@ -457,15 +457,6 @@ ; Modules @BINPATH@/modules/* -#ifdef MOZ_SAFE_BROWSING -; Safe Browsing -@BINPATH@/components/nsURLClassifier.manifest -@BINPATH@/components/nsUrlClassifierHashCompleter.js -@BINPATH@/components/nsUrlClassifierListManager.js -@BINPATH@/components/nsUrlClassifierLib.js -@BINPATH@/components/url-classifier.xpt -#endif - ; GNOME hooks #ifdef MOZ_ENABLE_GNOME_COMPONENT @BINPATH@/components/@DLL_PREFIX@mozgnome@DLL_SUFFIX@ @@ -606,9 +597,6 @@ bin/libfreebl_32int64_3.so @BINPATH@/components/PaymentsUI.js @BINPATH@/components/PaymentProviderStrategy.js -#ifdef MOZ_SAFE_BROWSING -@BINPATH@/components/SafeBrowsing.jsm -#endif @BINPATH@/components/XPIDialogService.js @BINPATH@/components/browsercomps.xpt diff --git a/mobile/android/modules/AppConstants.jsm b/mobile/android/modules/AppConstants.jsm index 903e75725e..5524e00599 100644 --- a/mobile/android/modules/AppConstants.jsm +++ b/mobile/android/modules/AppConstants.jsm @@ -63,13 +63,6 @@ let AppConstants = Object.freeze({ false, #endif - MOZ_SAFE_BROWSING: -#ifdef MOZ_SAFE_BROWSING - true, -#else - false, -#endif - MOZ_TELEMETRY_REPORTING: #ifdef MOZ_TELEMETRY_REPORTING true, diff --git a/mobile/android/modules/moz.build b/mobile/android/modules/moz.build index 1a69369f07..c0437db14f 100644 --- a/mobile/android/modules/moz.build +++ b/mobile/android/modules/moz.build @@ -36,8 +36,7 @@ for var in ('ANDROID_PACKAGE_NAME', 'MOZ_APP_VERSION'): for var in ('NIGHTLY_BUILD', 'RELEASE_BUILD', 'ACCESSIBILITY', 'MOZILLA_OFFICIAL', 'MOZ_OFFICIAL_BRANDING', 'MOZ_SERVICES_HEALTHREPORT', - 'MOZ_DEVICES', 'MOZ_DEVICES', 'MOZ_SAFE_BROWSING', - 'MOZ_TELEMETRY_REPORTING', 'MOZ_WEBRTC'): + 'MOZ_DEVICES', 'MOZ_DEVICES', 'MOZ_TELEMETRY_REPORTING', 'MOZ_WEBRTC'): if CONFIG[var]: DEFINES[var] = 1 diff --git a/toolkit/components/build/moz.build b/toolkit/components/build/moz.build index 4998edc865..57f7272e7d 100644 --- a/toolkit/components/build/moz.build +++ b/toolkit/components/build/moz.build @@ -25,7 +25,6 @@ LOCAL_INCLUDES += [ '../startup', '../statusfilter', '../typeaheadfind', - '../url-classifier', ] if not CONFIG['MOZ_DISABLE_PARENTAL_CONTROLS']: diff --git a/toolkit/components/build/nsToolkitCompsModule.cpp b/toolkit/components/build/nsToolkitCompsModule.cpp index 6c3f36fe3a..9988bbcda1 100644 --- a/toolkit/components/build/nsToolkitCompsModule.cpp +++ b/toolkit/components/build/nsToolkitCompsModule.cpp @@ -24,14 +24,6 @@ #include "nsTypeAheadFind.h" -#ifdef MOZ_URL_CLASSIFIER -#include "ApplicationReputation.h" -#include "nsUrlClassifierDBService.h" -#include "nsUrlClassifierStreamUpdater.h" -#include "nsUrlClassifierUtils.h" -#include "nsUrlClassifierPrefixSet.h" -#endif - #include "nsBrowserStatusFilter.h" #include "mozilla/FinalizationWitnessService.h" #include "mozilla/NativeOSFileInternals.h" @@ -77,33 +69,6 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsDownloadProxy) NS_GENERIC_FACTORY_CONSTRUCTOR(nsTypeAheadFind) -#ifdef MOZ_URL_CLASSIFIER -NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(ApplicationReputationService, - ApplicationReputationService::GetSingleton) -NS_GENERIC_FACTORY_CONSTRUCTOR(nsUrlClassifierPrefixSet) -NS_GENERIC_FACTORY_CONSTRUCTOR(nsUrlClassifierStreamUpdater) -NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsUrlClassifierUtils, Init) - -static nsresult -nsUrlClassifierDBServiceConstructor(nsISupports *aOuter, REFNSIID aIID, - void **aResult) -{ - nsresult rv; - NS_ENSURE_ARG_POINTER(aResult); - NS_ENSURE_NO_AGGREGATION(aOuter); - - nsUrlClassifierDBService *inst = nsUrlClassifierDBService::GetInstance(&rv); - if (nullptr == inst) { - return rv; - } - /* NS_ADDREF(inst); */ - rv = inst->QueryInterface(aIID, aResult); - NS_RELEASE(inst); - - return rv; -} -#endif - NS_GENERIC_FACTORY_CONSTRUCTOR(nsBrowserStatusFilter) #if defined(USE_MOZ_UPDATER) NS_GENERIC_FACTORY_CONSTRUCTOR(nsUpdateProcessor) @@ -128,13 +93,6 @@ NS_DEFINE_NAMED_CID(NS_DOWNLOADPLATFORM_CID); NS_DEFINE_NAMED_CID(NS_DOWNLOAD_CID); NS_DEFINE_NAMED_CID(NS_FIND_SERVICE_CID); NS_DEFINE_NAMED_CID(NS_TYPEAHEADFIND_CID); -#ifdef MOZ_URL_CLASSIFIER -NS_DEFINE_NAMED_CID(NS_APPLICATION_REPUTATION_SERVICE_CID); -NS_DEFINE_NAMED_CID(NS_URLCLASSIFIERPREFIXSET_CID); -NS_DEFINE_NAMED_CID(NS_URLCLASSIFIERDBSERVICE_CID); -NS_DEFINE_NAMED_CID(NS_URLCLASSIFIERSTREAMUPDATER_CID); -NS_DEFINE_NAMED_CID(NS_URLCLASSIFIERUTILS_CID); -#endif NS_DEFINE_NAMED_CID(NS_BROWSERSTATUSFILTER_CID); #if defined(USE_MOZ_UPDATER) NS_DEFINE_NAMED_CID(NS_UPDATEPROCESSOR_CID); @@ -159,13 +117,6 @@ static const Module::CIDEntry kToolkitCIDs[] = { { &kNS_DOWNLOAD_CID, false, nullptr, nsDownloadProxyConstructor }, { &kNS_FIND_SERVICE_CID, false, nullptr, nsFindServiceConstructor }, { &kNS_TYPEAHEADFIND_CID, false, nullptr, nsTypeAheadFindConstructor }, -#ifdef MOZ_URL_CLASSIFIER - { &kNS_APPLICATION_REPUTATION_SERVICE_CID, false, nullptr, ApplicationReputationServiceConstructor }, - { &kNS_URLCLASSIFIERPREFIXSET_CID, false, nullptr, nsUrlClassifierPrefixSetConstructor }, - { &kNS_URLCLASSIFIERDBSERVICE_CID, false, nullptr, nsUrlClassifierDBServiceConstructor }, - { &kNS_URLCLASSIFIERSTREAMUPDATER_CID, false, nullptr, nsUrlClassifierStreamUpdaterConstructor }, - { &kNS_URLCLASSIFIERUTILS_CID, false, nullptr, nsUrlClassifierUtilsConstructor }, -#endif { &kNS_BROWSERSTATUSFILTER_CID, false, nullptr, nsBrowserStatusFilterConstructor }, #if defined(USE_MOZ_UPDATER) { &kNS_UPDATEPROCESSOR_CID, false, nullptr, nsUpdateProcessorConstructor }, @@ -191,14 +142,6 @@ static const Module::ContractIDEntry kToolkitContracts[] = { { NS_DOWNLOADPLATFORM_CONTRACTID, &kNS_DOWNLOADPLATFORM_CID }, { NS_FIND_SERVICE_CONTRACTID, &kNS_FIND_SERVICE_CID }, { NS_TYPEAHEADFIND_CONTRACTID, &kNS_TYPEAHEADFIND_CID }, -#ifdef MOZ_URL_CLASSIFIER - { NS_APPLICATION_REPUTATION_SERVICE_CONTRACTID, &kNS_APPLICATION_REPUTATION_SERVICE_CID }, - { NS_URLCLASSIFIERPREFIXSET_CONTRACTID, &kNS_URLCLASSIFIERPREFIXSET_CID }, - { NS_URLCLASSIFIERDBSERVICE_CONTRACTID, &kNS_URLCLASSIFIERDBSERVICE_CID }, - { NS_URICLASSIFIERSERVICE_CONTRACTID, &kNS_URLCLASSIFIERDBSERVICE_CID }, - { NS_URLCLASSIFIERSTREAMUPDATER_CONTRACTID, &kNS_URLCLASSIFIERSTREAMUPDATER_CID }, - { NS_URLCLASSIFIERUTILS_CONTRACTID, &kNS_URLCLASSIFIERUTILS_CID }, -#endif { NS_BROWSERSTATUSFILTER_CONTRACTID, &kNS_BROWSERSTATUSFILTER_CID }, #if defined(USE_MOZ_UPDATER) { NS_UPDATEPROCESSOR_CONTRACTID, &kNS_UPDATEPROCESSOR_CID }, diff --git a/toolkit/components/downloads/ApplicationReputation.cpp b/toolkit/components/downloads/ApplicationReputation.cpp deleted file mode 100644 index cb8c85ae57..0000000000 --- a/toolkit/components/downloads/ApplicationReputation.cpp +++ /dev/null @@ -1,1125 +0,0 @@ -/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set ts=2 et sw=2 tw=80: */ -/* 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/. */ -// See -// https://wiki.mozilla.org/Security/Features/Application_Reputation_Design_Doc -// for a description of Chrome's implementation of this feature. -#include "ApplicationReputation.h" -#include "csd.pb.h" - -#include "nsIArray.h" -#include "nsIApplicationReputation.h" -#include "nsIChannel.h" -#include "nsIHttpChannel.h" -#include "nsIIOService.h" -#include "nsIPrefService.h" -#include "nsIScriptSecurityManager.h" -#include "nsIStreamListener.h" -#include "nsIStringStream.h" -#include "nsIUploadChannel2.h" -#include "nsIURI.h" -#include "nsIUrlClassifierDBService.h" -#include "nsIX509Cert.h" -#include "nsIX509CertDB.h" -#include "nsIX509CertList.h" - -#include "mozilla/Preferences.h" -#include "mozilla/Services.h" -#include "mozilla/Telemetry.h" -#include "mozilla/TimeStamp.h" -#include "mozilla/LoadContext.h" - -#include "nsAutoPtr.h" -#include "nsCOMPtr.h" -#include "nsDebug.h" -#include "nsError.h" -#include "nsNetCID.h" -#include "nsReadableUtils.h" -#include "nsServiceManagerUtils.h" -#include "nsString.h" -#include "nsTArray.h" -#include "nsThreadUtils.h" -#include "nsXPCOMStrings.h" - -#include "nsIContentPolicy.h" -#include "nsILoadInfo.h" -#include "nsContentUtils.h" - -using mozilla::Preferences; -using mozilla::TimeStamp; -using mozilla::Telemetry::Accumulate; -using safe_browsing::ClientDownloadRequest; -using safe_browsing::ClientDownloadRequest_CertificateChain; -using safe_browsing::ClientDownloadRequest_Resource; -using safe_browsing::ClientDownloadRequest_SignatureInfo; - -// Preferences that we need to initialize the query. -#define PREF_SB_APP_REP_URL "browser.safebrowsing.appRepURL" -#define PREF_SB_MALWARE_ENABLED "browser.safebrowsing.malware.enabled" -#define PREF_SB_DOWNLOADS_ENABLED "browser.safebrowsing.downloads.enabled" -#define PREF_SB_DOWNLOADS_REMOTE_ENABLED "browser.safebrowsing.downloads.remote.enabled" -#define PREF_GENERAL_LOCALE "general.useragent.locale" -#define PREF_DOWNLOAD_BLOCK_TABLE "urlclassifier.downloadBlockTable" -#define PREF_DOWNLOAD_ALLOW_TABLE "urlclassifier.downloadAllowTable" - -// NSPR_LOG_MODULES=ApplicationReputation:5 -#if defined(PR_LOGGING) -PRLogModuleInfo *ApplicationReputationService::prlog = nullptr; -#define LOG(args) PR_LOG(ApplicationReputationService::prlog, PR_LOG_DEBUG, args) -#define LOG_ENABLED() PR_LOG_TEST(ApplicationReputationService::prlog, 4) -#else -#define LOG(args) -#define LOG_ENABLED() (false) -#endif - -class PendingDBLookup; - -// A single use class private to ApplicationReputationService encapsulating an -// nsIApplicationReputationQuery and an nsIApplicationReputationCallback. Once -// created by ApplicationReputationService, it is guaranteed to call mCallback. -// This class is private to ApplicationReputationService. -class PendingLookup final : public nsIStreamListener -{ -public: - NS_DECL_ISUPPORTS - NS_DECL_NSIREQUESTOBSERVER - NS_DECL_NSISTREAMLISTENER - - // Constructor and destructor. - PendingLookup(nsIApplicationReputationQuery* aQuery, - nsIApplicationReputationCallback* aCallback); - - // Start the lookup. The lookup may have 2 parts: local and remote. In the - // local lookup, PendingDBLookups are created to query the local allow and - // blocklists for various URIs associated with this downloaded file. In the - // event that no results are found, a remote lookup is sent to the Application - // Reputation server. - nsresult StartLookup(); - -private: - ~PendingLookup(); - - friend class PendingDBLookup; - - // Telemetry states. - // Status of the remote response (valid or not). - enum SERVER_RESPONSE_TYPES { - SERVER_RESPONSE_VALID = 0, - SERVER_RESPONSE_FAILED = 1, - SERVER_RESPONSE_INVALID = 2, - }; - - // Number of blocklist and allowlist hits we have seen. - uint32_t mBlocklistCount; - uint32_t mAllowlistCount; - - // The query containing metadata about the downloaded file. - nsCOMPtr mQuery; - - // The callback with which to report the verdict. - nsCOMPtr mCallback; - - // An array of strings created from certificate information used to whitelist - // the downloaded file. - nsTArray mAllowlistSpecs; - // The source URI of the download, the referrer and possibly any redirects. - nsTArray mAnylistSpecs; - - // When we started this query - TimeStamp mStartTime; - - // A protocol buffer for storing things we need in the remote request. We - // store the resource chain (redirect information) as well as signature - // information extracted using the Windows Authenticode API, if the binary is - // signed. - ClientDownloadRequest mRequest; - - // The response from the application reputation query. This is read in chunks - // as part of our nsIStreamListener implementation and may contain embedded - // NULLs. - nsCString mResponse; - - // Returns true if the file is likely to be binary on Windows. - bool IsBinaryFile(); - - // Clean up and call the callback. PendingLookup must not be used after this - // function is called. - nsresult OnComplete(bool shouldBlock, nsresult rv); - - // Wrapper function for nsIStreamListener.onStopRequest to make it easy to - // guarantee calling the callback - nsresult OnStopRequestInternal(nsIRequest *aRequest, - nsISupports *aContext, - nsresult aResult, - bool* aShouldBlock); - - // Strip url parameters, fragments, and user@pass fields from the URI spec - // using nsIURL. If aURI is not an nsIURL, returns the original nsIURI.spec. - nsresult GetStrippedSpec(nsIURI* aUri, nsACString& spec); - - // Escape '/' and '%' in certificate attribute values. - nsCString EscapeCertificateAttribute(const nsACString& aAttribute); - - // Escape ':' in fingerprint values. - nsCString EscapeFingerprint(const nsACString& aAttribute); - - // Generate whitelist strings for the given certificate pair from the same - // certificate chain. - nsresult GenerateWhitelistStringsForPair( - nsIX509Cert* certificate, nsIX509Cert* issuer); - - // Generate whitelist strings for the given certificate chain, which starts - // with the signer and may go all the way to the root cert. - nsresult GenerateWhitelistStringsForChain( - const ClientDownloadRequest_CertificateChain& aChain); - - // For signed binaries, generate strings of the form: - // http://sb-ssl.google.com/safebrowsing/csd/certificate/ - // [/CN=][/O=][/OU=] - // for each (cert, issuer) pair in each chain of certificates that is - // associated with the binary. - nsresult GenerateWhitelistStrings(); - - // Parse the XPCOM certificate lists and stick them into the protocol buffer - // version. - nsresult ParseCertificates(nsIArray* aSigArray); - - // Adds the redirects to mAnylistSpecs to be looked up. - nsresult AddRedirects(nsIArray* aRedirects); - - // Helper function to ensure that we call PendingLookup::LookupNext or - // PendingLookup::OnComplete. - nsresult DoLookupInternal(); - - // Looks up all the URIs that may be responsible for allowlisting or - // blocklisting the downloaded file. These URIs may include whitelist strings - // generated by certificates verifying the binary as well as the target URI - // from which the file was downloaded. - nsresult LookupNext(); - - // Sends a query to the remote application reputation service. Returns NS_OK - // on success. - nsresult SendRemoteQuery(); - - // Helper function to ensure that we always call the callback. - nsresult SendRemoteQueryInternal(); -}; - -// A single-use class for looking up a single URI in the safebrowsing DB. This -// class is private to PendingLookup. -class PendingDBLookup final : public nsIUrlClassifierCallback -{ -public: - NS_DECL_ISUPPORTS - NS_DECL_NSIURLCLASSIFIERCALLBACK - - // Constructor and destructor - explicit PendingDBLookup(PendingLookup* aPendingLookup); - - // Look up the given URI in the safebrowsing DBs, optionally on both the allow - // list and the blocklist. If there is a match, call - // PendingLookup::OnComplete. Otherwise, call PendingLookup::LookupNext. - nsresult LookupSpec(const nsACString& aSpec, bool aAllowlistOnly); - -private: - ~PendingDBLookup(); - - // The download appeared on the allowlist, blocklist, or no list (and thus - // could trigger a remote query. - enum LIST_TYPES { - ALLOW_LIST = 0, - BLOCK_LIST = 1, - NO_LIST = 2, - }; - - nsCString mSpec; - bool mAllowlistOnly; - nsRefPtr mPendingLookup; - nsresult LookupSpecInternal(const nsACString& aSpec); -}; - -NS_IMPL_ISUPPORTS(PendingDBLookup, - nsIUrlClassifierCallback) - -PendingDBLookup::PendingDBLookup(PendingLookup* aPendingLookup) : - mAllowlistOnly(false), - mPendingLookup(aPendingLookup) -{ - LOG(("Created pending DB lookup [this = %p]", this)); -} - -PendingDBLookup::~PendingDBLookup() -{ - LOG(("Destroying pending DB lookup [this = %p]", this)); - mPendingLookup = nullptr; -} - -nsresult -PendingDBLookup::LookupSpec(const nsACString& aSpec, - bool aAllowlistOnly) -{ - LOG(("Checking principal %s [this=%p]", aSpec.Data(), this)); - mSpec = aSpec; - mAllowlistOnly = aAllowlistOnly; - nsresult rv = LookupSpecInternal(aSpec); - if (NS_FAILED(rv)) { - LOG(("Error in LookupSpecInternal")); - return mPendingLookup->OnComplete(false, NS_OK); - } - // LookupSpecInternal has called nsIUrlClassifierCallback.lookup, which is - // guaranteed to call HandleEvent. - return rv; -} - -nsresult -PendingDBLookup::LookupSpecInternal(const nsACString& aSpec) -{ - nsresult rv; - - nsCOMPtr uri; - nsCOMPtr ios = do_GetService(NS_IOSERVICE_CONTRACTID, &rv); - rv = ios->NewURI(aSpec, nullptr, nullptr, getter_AddRefs(uri)); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr principal; - nsCOMPtr secMan = - do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - rv = secMan->GetNoAppCodebasePrincipal(uri, getter_AddRefs(principal)); - NS_ENSURE_SUCCESS(rv, rv); - - // Check local lists to see if the URI has already been whitelisted or - // blacklisted. - LOG(("Checking DB service for principal %s [this = %p]", mSpec.get(), this)); - nsCOMPtr dbService = - do_GetService(NS_URLCLASSIFIERDBSERVICE_CONTRACTID, &rv); - nsAutoCString tables; - nsAutoCString allowlist; - Preferences::GetCString(PREF_DOWNLOAD_ALLOW_TABLE, &allowlist); - if (!allowlist.IsEmpty()) { - tables.Append(allowlist); - } - nsAutoCString blocklist; - Preferences::GetCString(PREF_DOWNLOAD_BLOCK_TABLE, &blocklist); - if (!mAllowlistOnly && !blocklist.IsEmpty()) { - tables.Append(','); - tables.Append(blocklist); - } - return dbService->Lookup(principal, tables, this); -} - -NS_IMETHODIMP -PendingDBLookup::HandleEvent(const nsACString& tables) -{ - // HandleEvent is guaranteed to call either: - // 1) PendingLookup::OnComplete if the URL matches the blocklist, or - // 2) PendingLookup::LookupNext if the URL does not match the blocklist. - // Blocklisting trumps allowlisting. - nsAutoCString blockList; - Preferences::GetCString(PREF_DOWNLOAD_BLOCK_TABLE, &blockList); - if (!mAllowlistOnly && FindInReadable(blockList, tables)) { - mPendingLookup->mBlocklistCount++; - Accumulate(mozilla::Telemetry::APPLICATION_REPUTATION_LOCAL, BLOCK_LIST); - LOG(("Found principal %s on blocklist [this = %p]", mSpec.get(), this)); - return mPendingLookup->OnComplete(true, NS_OK); - } - - nsAutoCString allowList; - Preferences::GetCString(PREF_DOWNLOAD_ALLOW_TABLE, &allowList); - if (FindInReadable(allowList, tables)) { - mPendingLookup->mAllowlistCount++; - Accumulate(mozilla::Telemetry::APPLICATION_REPUTATION_LOCAL, ALLOW_LIST); - LOG(("Found principal %s on allowlist [this = %p]", mSpec.get(), this)); - // Don't call onComplete, since blocklisting trumps allowlisting - } else { - LOG(("Didn't find principal %s on any list [this = %p]", mSpec.get(), - this)); - Accumulate(mozilla::Telemetry::APPLICATION_REPUTATION_LOCAL, NO_LIST); - } - return mPendingLookup->LookupNext(); -} - -NS_IMPL_ISUPPORTS(PendingLookup, - nsIStreamListener, - nsIRequestObserver) - -PendingLookup::PendingLookup(nsIApplicationReputationQuery* aQuery, - nsIApplicationReputationCallback* aCallback) : - mBlocklistCount(0), - mAllowlistCount(0), - mQuery(aQuery), - mCallback(aCallback) -{ - LOG(("Created pending lookup [this = %p]", this)); -} - -PendingLookup::~PendingLookup() -{ - LOG(("Destroying pending lookup [this = %p]", this)); -} - -bool -PendingLookup::IsBinaryFile() -{ - nsString fileName; - nsresult rv = mQuery->GetSuggestedFileName(fileName); - if (NS_FAILED(rv)) { - LOG(("No suggested filename [this = %p]", this)); - return false; - } - LOG(("Suggested filename: %s [this = %p]", - NS_ConvertUTF16toUTF8(fileName).get(), this)); - return - // Executable extensions for MS Windows, from - // https://code.google.com/p/chromium/codesearch#chromium/src/chrome/common/safe_browsing/download_protection_util.cc&l=14 - StringEndsWith(fileName, NS_LITERAL_STRING(".apk")) || - StringEndsWith(fileName, NS_LITERAL_STRING(".bas")) || - StringEndsWith(fileName, NS_LITERAL_STRING(".bat")) || - StringEndsWith(fileName, NS_LITERAL_STRING(".cab")) || - StringEndsWith(fileName, NS_LITERAL_STRING(".cmd")) || - StringEndsWith(fileName, NS_LITERAL_STRING(".com")) || - StringEndsWith(fileName, NS_LITERAL_STRING(".exe")) || - StringEndsWith(fileName, NS_LITERAL_STRING(".hta")) || - StringEndsWith(fileName, NS_LITERAL_STRING(".msi")) || - StringEndsWith(fileName, NS_LITERAL_STRING(".pif")) || - StringEndsWith(fileName, NS_LITERAL_STRING(".reg")) || - StringEndsWith(fileName, NS_LITERAL_STRING(".scr")) || - StringEndsWith(fileName, NS_LITERAL_STRING(".vb")) || - StringEndsWith(fileName, NS_LITERAL_STRING(".vbs")) || - StringEndsWith(fileName, NS_LITERAL_STRING(".zip")); -} - -nsresult -PendingLookup::LookupNext() -{ - // We must call LookupNext or SendRemoteQuery upon return. - // Look up all of the URLs that could allow or block this download. - // Blocklist first. - if (mBlocklistCount > 0) { - return OnComplete(true, NS_OK); - } - int index = mAnylistSpecs.Length() - 1; - nsCString spec; - if (index >= 0) { - // Check the source URI, referrer and redirect chain. - spec = mAnylistSpecs[index]; - mAnylistSpecs.RemoveElementAt(index); - nsRefPtr lookup(new PendingDBLookup(this)); - return lookup->LookupSpec(spec, false); - } - // If any of mAnylistSpecs matched the blocklist, go ahead and block. - if (mBlocklistCount > 0) { - return OnComplete(true, NS_OK); - } - // If any of mAnylistSpecs matched the allowlist, go ahead and pass. - if (mAllowlistCount > 0) { - return OnComplete(false, NS_OK); - } - // Only binary signatures remain. - index = mAllowlistSpecs.Length() - 1; - if (index >= 0) { - spec = mAllowlistSpecs[index]; - LOG(("PendingLookup::LookupNext: checking %s on allowlist", spec.get())); - mAllowlistSpecs.RemoveElementAt(index); - nsRefPtr lookup(new PendingDBLookup(this)); - return lookup->LookupSpec(spec, true); - } -#ifdef XP_WIN - // There are no more URIs to check against local list. If the file is - // not eligible for remote lookup, bail. - if (!IsBinaryFile()) { - LOG(("Not eligible for remote lookups [this=%x]", this)); - return OnComplete(false, NS_OK); - } - // Send the remote query if we are on Windows. - nsresult rv = SendRemoteQuery(); - if (NS_FAILED(rv)) { - return OnComplete(false, rv); - } - return NS_OK; -#else - LOG(("PendingLookup: Nothing left to check [this=%p]", this)); - return OnComplete(false, NS_OK); -#endif -} - -nsCString -PendingLookup::EscapeCertificateAttribute(const nsACString& aAttribute) -{ - // Escape '/' because it's a field separator, and '%' because Chrome does - nsCString escaped; - escaped.SetCapacity(aAttribute.Length()); - for (unsigned int i = 0; i < aAttribute.Length(); ++i) { - if (aAttribute.Data()[i] == '%') { - escaped.AppendLiteral("%25"); - } else if (aAttribute.Data()[i] == '/') { - escaped.AppendLiteral("%2F"); - } else if (aAttribute.Data()[i] == ' ') { - escaped.AppendLiteral("%20"); - } else { - escaped.Append(aAttribute.Data()[i]); - } - } - return escaped; -} - -nsCString -PendingLookup::EscapeFingerprint(const nsACString& aFingerprint) -{ - // Google's fingerprint doesn't have colons - nsCString escaped; - escaped.SetCapacity(aFingerprint.Length()); - for (unsigned int i = 0; i < aFingerprint.Length(); ++i) { - if (aFingerprint.Data()[i] != ':') { - escaped.Append(aFingerprint.Data()[i]); - } - } - return escaped; -} - -nsresult -PendingLookup::GenerateWhitelistStringsForPair( - nsIX509Cert* certificate, - nsIX509Cert* issuer) -{ - // The whitelist paths have format: - // http://sb-ssl.google.com/safebrowsing/csd/certificate/[/CN=][/O=][/OU=] - // Any of CN, O, or OU may be omitted from the whitelist entry. Unfortunately - // this is not publicly documented, but the Chrome implementation can be found - // here: - // https://code.google.com/p/chromium/codesearch#search/&q=GetCertificateWhitelistStrings - nsCString whitelistString( - "http://sb-ssl.google.com/safebrowsing/csd/certificate/"); - - nsString fingerprint; - nsresult rv = issuer->GetSha1Fingerprint(fingerprint); - NS_ENSURE_SUCCESS(rv, rv); - whitelistString.Append( - EscapeFingerprint(NS_ConvertUTF16toUTF8(fingerprint))); - - nsString commonName; - rv = certificate->GetCommonName(commonName); - NS_ENSURE_SUCCESS(rv, rv); - if (!commonName.IsEmpty()) { - whitelistString.AppendLiteral("/CN="); - whitelistString.Append( - EscapeCertificateAttribute(NS_ConvertUTF16toUTF8(commonName))); - } - - nsString organization; - rv = certificate->GetOrganization(organization); - NS_ENSURE_SUCCESS(rv, rv); - if (!organization.IsEmpty()) { - whitelistString.AppendLiteral("/O="); - whitelistString.Append( - EscapeCertificateAttribute(NS_ConvertUTF16toUTF8(organization))); - } - - nsString organizationalUnit; - rv = certificate->GetOrganizationalUnit(organizationalUnit); - NS_ENSURE_SUCCESS(rv, rv); - if (!organizationalUnit.IsEmpty()) { - whitelistString.AppendLiteral("/OU="); - whitelistString.Append( - EscapeCertificateAttribute(NS_ConvertUTF16toUTF8(organizationalUnit))); - } - LOG(("Whitelisting %s", whitelistString.get())); - - mAllowlistSpecs.AppendElement(whitelistString); - return NS_OK; -} - -nsresult -PendingLookup::GenerateWhitelistStringsForChain( - const safe_browsing::ClientDownloadRequest_CertificateChain& aChain) -{ - // We need a signing certificate and an issuer to construct a whitelist - // entry. - if (aChain.element_size() < 2) { - return NS_OK; - } - - // Get the signer. - nsresult rv; - nsCOMPtr certDB = do_GetService(NS_X509CERTDB_CONTRACTID, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr signer; - rv = certDB->ConstructX509( - const_cast(aChain.element(0).certificate().data()), - aChain.element(0).certificate().size(), getter_AddRefs(signer)); - NS_ENSURE_SUCCESS(rv, rv); - - for (int i = 1; i < aChain.element_size(); ++i) { - // Get the issuer. - nsCOMPtr issuer; - rv = certDB->ConstructX509( - const_cast(aChain.element(i).certificate().data()), - aChain.element(i).certificate().size(), getter_AddRefs(issuer)); - NS_ENSURE_SUCCESS(rv, rv); - - nsresult rv = GenerateWhitelistStringsForPair(signer, issuer); - NS_ENSURE_SUCCESS(rv, rv); - } - return NS_OK; -} - -nsresult -PendingLookup::GenerateWhitelistStrings() -{ - for (int i = 0; i < mRequest.signature().certificate_chain_size(); ++i) { - nsresult rv = GenerateWhitelistStringsForChain( - mRequest.signature().certificate_chain(i)); - NS_ENSURE_SUCCESS(rv, rv); - } - return NS_OK; -} - -nsresult -PendingLookup::AddRedirects(nsIArray* aRedirects) -{ - uint32_t length = 0; - aRedirects->GetLength(&length); - LOG(("ApplicationReputation: Got %u redirects", length)); - nsCOMPtr iter; - nsresult rv = aRedirects->Enumerate(getter_AddRefs(iter)); - NS_ENSURE_SUCCESS(rv, rv); - - bool hasMoreRedirects = false; - rv = iter->HasMoreElements(&hasMoreRedirects); - NS_ENSURE_SUCCESS(rv, rv); - - while (hasMoreRedirects) { - nsCOMPtr supports; - rv = iter->GetNext(getter_AddRefs(supports)); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr principal = do_QueryInterface(supports, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr uri; - rv = principal->GetURI(getter_AddRefs(uri)); - NS_ENSURE_SUCCESS(rv, rv); - - // Add the spec to our list of local lookups. The most recent redirect is - // the last element. - nsCString spec; - rv = GetStrippedSpec(uri, spec); - NS_ENSURE_SUCCESS(rv, rv); - mAnylistSpecs.AppendElement(spec); - LOG(("ApplicationReputation: Appending redirect %s\n", spec.get())); - - // Store the redirect information in the remote request. - ClientDownloadRequest_Resource* resource = mRequest.add_resources(); - resource->set_url(spec.get()); - resource->set_type(ClientDownloadRequest::DOWNLOAD_REDIRECT); - - rv = iter->HasMoreElements(&hasMoreRedirects); - NS_ENSURE_SUCCESS(rv, rv); - } - return NS_OK; -} - -nsresult -PendingLookup::StartLookup() -{ - mStartTime = TimeStamp::Now(); - nsresult rv = DoLookupInternal(); - if (NS_FAILED(rv)) { - return OnComplete(false, NS_OK); - }; - return rv; -} - -nsresult -PendingLookup::GetStrippedSpec(nsIURI* aUri, nsACString& escaped) -{ - // If aURI is not an nsIURL, we do not want to check the lists or send a - // remote query. - nsresult rv; - nsCOMPtr url = do_QueryInterface(aUri, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - rv = url->GetScheme(escaped); - NS_ENSURE_SUCCESS(rv, rv); - - nsCString temp; - rv = url->GetHostPort(temp); - NS_ENSURE_SUCCESS(rv, rv); - - escaped.Append("://"); - escaped.Append(temp); - - rv = url->GetFilePath(temp); - NS_ENSURE_SUCCESS(rv, rv); - - // nsIUrl.filePath starts with '/' - escaped.Append(temp); - - return NS_OK; -} - -nsresult -PendingLookup::DoLookupInternal() -{ - // We want to check the target URI, its referrer, and associated redirects - // against the local lists. - nsCOMPtr uri; - nsresult rv = mQuery->GetSourceURI(getter_AddRefs(uri)); - NS_ENSURE_SUCCESS(rv, rv); - - nsCString spec; - rv = GetStrippedSpec(uri, spec); - NS_ENSURE_SUCCESS(rv, rv); - - mAnylistSpecs.AppendElement(spec); - - ClientDownloadRequest_Resource* resource = mRequest.add_resources(); - resource->set_url(spec.get()); - resource->set_type(ClientDownloadRequest::DOWNLOAD_URL); - - nsCOMPtr referrer = nullptr; - rv = mQuery->GetReferrerURI(getter_AddRefs(referrer)); - if (referrer) { - nsCString spec; - rv = GetStrippedSpec(referrer, spec); - NS_ENSURE_SUCCESS(rv, rv); - mAnylistSpecs.AppendElement(spec); - resource->set_referrer(spec.get()); - } - nsCOMPtr redirects; - rv = mQuery->GetRedirects(getter_AddRefs(redirects)); - if (redirects) { - AddRedirects(redirects); - } else { - LOG(("ApplicationReputation: Got no redirects [this=%p]", this)); - } - - // Extract the signature and parse certificates so we can use it to check - // whitelists. - nsCOMPtr sigArray; - rv = mQuery->GetSignatureInfo(getter_AddRefs(sigArray)); - NS_ENSURE_SUCCESS(rv, rv); - - if (sigArray) { - rv = ParseCertificates(sigArray); - NS_ENSURE_SUCCESS(rv, rv); - } - - rv = GenerateWhitelistStrings(); - NS_ENSURE_SUCCESS(rv, rv); - - // Start the call chain. - return LookupNext(); -} - -nsresult -PendingLookup::OnComplete(bool shouldBlock, nsresult rv) -{ - Accumulate(mozilla::Telemetry::APPLICATION_REPUTATION_SHOULD_BLOCK, - shouldBlock); -#if defined(PR_LOGGING) - double t = (TimeStamp::Now() - mStartTime).ToMilliseconds(); -#endif - if (shouldBlock) { - LOG(("Application Reputation check failed, blocking bad binary in %f ms " - "[this = %p]", t, this)); - } else { - LOG(("Application Reputation check passed in %f ms [this = %p]", t, this)); - } - nsresult res = mCallback->OnComplete(shouldBlock, rv); - return res; -} - -nsresult -PendingLookup::ParseCertificates(nsIArray* aSigArray) -{ - // If we haven't been set for any reason, bail. - NS_ENSURE_ARG_POINTER(aSigArray); - - // Binaries may be signed by multiple chains of certificates. If there are no - // chains, the binary is unsigned (or we were unable to extract signature - // information on a non-Windows platform) - nsCOMPtr chains; - nsresult rv = aSigArray->Enumerate(getter_AddRefs(chains)); - NS_ENSURE_SUCCESS(rv, rv); - - bool hasMoreChains = false; - rv = chains->HasMoreElements(&hasMoreChains); - NS_ENSURE_SUCCESS(rv, rv); - - while (hasMoreChains) { - nsCOMPtr supports; - rv = chains->GetNext(getter_AddRefs(supports)); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr certList = do_QueryInterface(supports, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - safe_browsing::ClientDownloadRequest_CertificateChain* certChain = - mRequest.mutable_signature()->add_certificate_chain(); - nsCOMPtr chainElt; - rv = certList->GetEnumerator(getter_AddRefs(chainElt)); - NS_ENSURE_SUCCESS(rv, rv); - - // Each chain may have multiple certificates. - bool hasMoreCerts = false; - rv = chainElt->HasMoreElements(&hasMoreCerts); - while (hasMoreCerts) { - nsCOMPtr supports; - rv = chainElt->GetNext(getter_AddRefs(supports)); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr cert = do_QueryInterface(supports, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - uint8_t* data = nullptr; - uint32_t len = 0; - rv = cert->GetRawDER(&len, &data); - NS_ENSURE_SUCCESS(rv, rv); - - // Add this certificate to the protobuf to send remotely. - certChain->add_element()->set_certificate(data, len); - nsMemory::Free(data); - - rv = chainElt->HasMoreElements(&hasMoreCerts); - NS_ENSURE_SUCCESS(rv, rv); - } - rv = chains->HasMoreElements(&hasMoreChains); - NS_ENSURE_SUCCESS(rv, rv); - } - if (mRequest.signature().certificate_chain_size() > 0) { - mRequest.mutable_signature()->set_trusted(true); - } - return NS_OK; -} - -nsresult -PendingLookup::SendRemoteQuery() -{ - nsresult rv = SendRemoteQueryInternal(); - if (NS_FAILED(rv)) { - LOG(("Failed sending remote query for application reputation " - "[this = %p]", this)); - return OnComplete(false, rv); - } - // SendRemoteQueryInternal has fired off the query and we call OnComplete in - // the nsIStreamListener.onStopRequest. - return rv; -} - -nsresult -PendingLookup::SendRemoteQueryInternal() -{ - // If we aren't supposed to do remote lookups, bail. - if (!Preferences::GetBool(PREF_SB_DOWNLOADS_REMOTE_ENABLED, false)) { - return NS_ERROR_NOT_AVAILABLE; - } - // If the remote lookup URL is empty or absent, bail. - nsCString serviceUrl; - NS_ENSURE_SUCCESS(Preferences::GetCString(PREF_SB_APP_REP_URL, &serviceUrl), - NS_ERROR_NOT_AVAILABLE); - if (serviceUrl.EqualsLiteral("")) { - return NS_ERROR_NOT_AVAILABLE; - } - - // If the blocklist or allowlist is empty (so we couldn't do local lookups), - // bail - nsCString table; - NS_ENSURE_SUCCESS(Preferences::GetCString(PREF_DOWNLOAD_BLOCK_TABLE, &table), - NS_ERROR_NOT_AVAILABLE); - if (table.EqualsLiteral("")) { - return NS_ERROR_NOT_AVAILABLE; - } - NS_ENSURE_SUCCESS(Preferences::GetCString(PREF_DOWNLOAD_ALLOW_TABLE, &table), - NS_ERROR_NOT_AVAILABLE); - if (table.EqualsLiteral("")) { - return NS_ERROR_NOT_AVAILABLE; - } - - LOG(("Sending remote query for application reputation [this = %p]", - this)); - // We did not find a local result, so fire off the query to the - // application reputation service. - nsCOMPtr uri; - nsresult rv; - rv = mQuery->GetSourceURI(getter_AddRefs(uri)); - NS_ENSURE_SUCCESS(rv, rv); - nsCString spec; - rv = GetStrippedSpec(uri, spec); - NS_ENSURE_SUCCESS(rv, rv); - mRequest.set_url(spec.get()); - - uint32_t fileSize; - rv = mQuery->GetFileSize(&fileSize); - NS_ENSURE_SUCCESS(rv, rv); - mRequest.set_length(fileSize); - // We have no way of knowing whether or not a user initiated the - // download. Set it to true to lessen the chance of false positives. - mRequest.set_user_initiated(true); - - nsCString locale; - NS_ENSURE_SUCCESS(Preferences::GetCString(PREF_GENERAL_LOCALE, &locale), - NS_ERROR_NOT_AVAILABLE); - mRequest.set_locale(locale.get()); - nsCString sha256Hash; - rv = mQuery->GetSha256Hash(sha256Hash); - NS_ENSURE_SUCCESS(rv, rv); - mRequest.mutable_digests()->set_sha256(sha256Hash.Data()); - nsString fileName; - rv = mQuery->GetSuggestedFileName(fileName); - NS_ENSURE_SUCCESS(rv, rv); - mRequest.set_file_basename(NS_ConvertUTF16toUTF8(fileName).get()); - - if (mRequest.signature().trusted()) { - LOG(("Got signed binary for remote application reputation check " - "[this = %p]", this)); - } else { - LOG(("Got unsigned binary for remote application reputation check " - "[this = %p]", this)); - } - - // Serialize the protocol buffer to a string. This can only fail if we are - // out of memory, or if the protocol buffer req is missing required fields - // (only the URL for now). - std::string serialized; - if (!mRequest.SerializeToString(&serialized)) { - return NS_ERROR_UNEXPECTED; - } - - // Set the input stream to the serialized protocol buffer - nsCOMPtr sstream = - do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv); - NS_ENSURE_SUCCESS(rv, rv); - - rv = sstream->SetData(serialized.c_str(), serialized.length()); - NS_ENSURE_SUCCESS(rv, rv); - - // Set up the channel to transmit the request to the service. - nsCOMPtr channel; - nsCOMPtr ios = do_GetService(NS_IOSERVICE_CONTRACTID, &rv); - rv = ios->NewChannel2(serviceUrl, - nullptr, - nullptr, - nullptr, // aLoadingNode - nsContentUtils::GetSystemPrincipal(), - nullptr, // aTriggeringPrincipal - nsILoadInfo::SEC_NORMAL, - nsIContentPolicy::TYPE_OTHER, - getter_AddRefs(channel)); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr httpChannel(do_QueryInterface(channel, &rv)); - NS_ENSURE_SUCCESS(rv, rv); - - // Upload the protobuf to the application reputation service. - nsCOMPtr uploadChannel = do_QueryInterface(channel, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - rv = uploadChannel->ExplicitSetUploadStream(sstream, - NS_LITERAL_CSTRING("application/octet-stream"), serialized.size(), - NS_LITERAL_CSTRING("POST"), false); - NS_ENSURE_SUCCESS(rv, rv); - - // Set the Safebrowsing cookie jar, so that the regular Google cookie is not - // sent with this request. See bug 897516. - nsCOMPtr loadContext = - new mozilla::LoadContext(NECKO_SAFEBROWSING_APP_ID); - rv = channel->SetNotificationCallbacks(loadContext); - NS_ENSURE_SUCCESS(rv, rv); - - rv = channel->AsyncOpen(this, nullptr); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -//////////////////////////////////////////////////////////////////////////////// -//// nsIStreamListener -static NS_METHOD -AppendSegmentToString(nsIInputStream* inputStream, - void *closure, - const char *rawSegment, - uint32_t toOffset, - uint32_t count, - uint32_t *writeCount) { - nsAutoCString* decodedData = static_cast(closure); - decodedData->Append(rawSegment, count); - *writeCount = count; - return NS_OK; -} - -NS_IMETHODIMP -PendingLookup::OnDataAvailable(nsIRequest *aRequest, - nsISupports *aContext, - nsIInputStream *aStream, - uint64_t offset, - uint32_t count) { - uint32_t read; - return aStream->ReadSegments(AppendSegmentToString, &mResponse, count, &read); -} - -NS_IMETHODIMP -PendingLookup::OnStartRequest(nsIRequest *aRequest, - nsISupports *aContext) { - return NS_OK; -} - -NS_IMETHODIMP -PendingLookup::OnStopRequest(nsIRequest *aRequest, - nsISupports *aContext, - nsresult aResult) { - NS_ENSURE_STATE(mCallback); - - bool shouldBlock = false; - nsresult rv = OnStopRequestInternal(aRequest, aContext, aResult, - &shouldBlock); - OnComplete(shouldBlock, rv); - return rv; -} - -nsresult -PendingLookup::OnStopRequestInternal(nsIRequest *aRequest, - nsISupports *aContext, - nsresult aResult, - bool* aShouldBlock) { - if (NS_FAILED(aResult)) { - Accumulate(mozilla::Telemetry::APPLICATION_REPUTATION_SERVER, - SERVER_RESPONSE_FAILED); - return aResult; - } - - *aShouldBlock = false; - nsresult rv; - nsCOMPtr channel = do_QueryInterface(aRequest, &rv); - if (NS_FAILED(rv)) { - Accumulate(mozilla::Telemetry::APPLICATION_REPUTATION_SERVER, - SERVER_RESPONSE_FAILED); - return rv; - } - - uint32_t status = 0; - rv = channel->GetResponseStatus(&status); - if (NS_FAILED(rv)) { - Accumulate(mozilla::Telemetry::APPLICATION_REPUTATION_SERVER, - SERVER_RESPONSE_FAILED); - return rv; - } - - if (status != 200) { - Accumulate(mozilla::Telemetry::APPLICATION_REPUTATION_SERVER, - SERVER_RESPONSE_FAILED); - return NS_ERROR_NOT_AVAILABLE; - } - - std::string buf(mResponse.Data(), mResponse.Length()); - safe_browsing::ClientDownloadResponse response; - if (!response.ParseFromString(buf)) { - NS_WARNING("Could not parse protocol buffer"); - Accumulate(mozilla::Telemetry::APPLICATION_REPUTATION_SERVER, - SERVER_RESPONSE_INVALID); - return NS_ERROR_CANNOT_CONVERT_DATA; - } - - // There are several more verdicts, but we only respect DANGEROUS and - // DANGEROUS_HOST for now and treat everything else as SAFE. - Accumulate(mozilla::Telemetry::APPLICATION_REPUTATION_SERVER, - SERVER_RESPONSE_VALID); - // Clamp responses 0-7, we only know about 0-4 for now. - Accumulate(mozilla::Telemetry::APPLICATION_REPUTATION_SERVER_VERDICT, - std::min(response.verdict(), 7)); - switch(response.verdict()) { - case safe_browsing::ClientDownloadResponse::DANGEROUS: - case safe_browsing::ClientDownloadResponse::DANGEROUS_HOST: - *aShouldBlock = true; - break; - default: - break; - } - - return NS_OK; -} - -NS_IMPL_ISUPPORTS(ApplicationReputationService, - nsIApplicationReputationService) - -ApplicationReputationService* - ApplicationReputationService::gApplicationReputationService = nullptr; - -ApplicationReputationService* -ApplicationReputationService::GetSingleton() -{ - if (gApplicationReputationService) { - NS_ADDREF(gApplicationReputationService); - return gApplicationReputationService; - } - - // We're not initialized yet. - gApplicationReputationService = new ApplicationReputationService(); - if (gApplicationReputationService) { - NS_ADDREF(gApplicationReputationService); - } - - return gApplicationReputationService; -} - -ApplicationReputationService::ApplicationReputationService() -{ -#if defined(PR_LOGGING) - if (!prlog) { - prlog = PR_NewLogModule("ApplicationReputation"); - } -#endif - LOG(("Application reputation service started up")); -} - -ApplicationReputationService::~ApplicationReputationService() { - LOG(("Application reputation service shutting down")); -} - -NS_IMETHODIMP -ApplicationReputationService::QueryReputation( - nsIApplicationReputationQuery* aQuery, - nsIApplicationReputationCallback* aCallback) { - LOG(("Starting application reputation check [query=%p]", aQuery)); - NS_ENSURE_ARG_POINTER(aQuery); - NS_ENSURE_ARG_POINTER(aCallback); - - Accumulate(mozilla::Telemetry::APPLICATION_REPUTATION_COUNT, true); - nsresult rv = QueryReputationInternal(aQuery, aCallback); - if (NS_FAILED(rv)) { - Accumulate(mozilla::Telemetry::APPLICATION_REPUTATION_SHOULD_BLOCK, - false); - aCallback->OnComplete(false, rv); - } - return NS_OK; -} - -nsresult ApplicationReputationService::QueryReputationInternal( - nsIApplicationReputationQuery* aQuery, - nsIApplicationReputationCallback* aCallback) { - nsresult rv; - // If malware checks aren't enabled, don't query application reputation. - if (!Preferences::GetBool(PREF_SB_MALWARE_ENABLED, false)) { - return NS_ERROR_NOT_AVAILABLE; - } - - if (!Preferences::GetBool(PREF_SB_DOWNLOADS_ENABLED, false)) { - return NS_ERROR_NOT_AVAILABLE; - } - - nsCOMPtr uri; - rv = aQuery->GetSourceURI(getter_AddRefs(uri)); - NS_ENSURE_SUCCESS(rv, rv); - // Bail if the URI hasn't been set. - NS_ENSURE_STATE(uri); - - // Create a new pending lookup and start the call chain. - nsRefPtr lookup(new PendingLookup(aQuery, aCallback)); - NS_ENSURE_STATE(lookup); - - return lookup->StartLookup(); -} diff --git a/toolkit/components/downloads/ApplicationReputation.h b/toolkit/components/downloads/ApplicationReputation.h deleted file mode 100644 index 7ae0208a6b..0000000000 --- a/toolkit/components/downloads/ApplicationReputation.h +++ /dev/null @@ -1,55 +0,0 @@ -/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set ts=2 et sw=2 tw=80: */ -/* 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/. */ - -#ifndef ApplicationReputation_h__ -#define ApplicationReputation_h__ - -#include "nsIApplicationReputation.h" -#include "nsIRequestObserver.h" -#include "nsIStreamListener.h" -#include "nsISupports.h" - -#include "nsCOMPtr.h" -#include "nsString.h" - -class nsIRequest; -class PendingDBLookup; -class PendingLookup; -struct PRLogModuleInfo; - -class ApplicationReputationService final : - public nsIApplicationReputationService { -public: - NS_DECL_ISUPPORTS - NS_DECL_NSIAPPLICATIONREPUTATIONSERVICE - -public: - static ApplicationReputationService* GetSingleton(); - -private: - friend class PendingLookup; - friend class PendingDBLookup; - /** - * Global singleton object for holding this factory service. - */ - static ApplicationReputationService* gApplicationReputationService; - /** - * NSPR_LOG_MODULES=ApplicationReputation:5 - */ - static PRLogModuleInfo* prlog; - /** - * This is a singleton, so disallow construction. - */ - ApplicationReputationService(); - ~ApplicationReputationService(); - /** - * Wrapper function for QueryReputation that makes it easier to ensure the - * callback is called. - */ - nsresult QueryReputationInternal(nsIApplicationReputationQuery* aQuery, - nsIApplicationReputationCallback* aCallback); -}; -#endif /* ApplicationReputation_h__ */ diff --git a/toolkit/components/downloads/csd.pb.cc b/toolkit/components/downloads/csd.pb.cc deleted file mode 100644 index 99108deecf..0000000000 --- a/toolkit/components/downloads/csd.pb.cc +++ /dev/null @@ -1,2887 +0,0 @@ -// Generated by the protocol buffer compiler. DO NOT EDIT! - -#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION -#include "csd.pb.h" - -#include - -#include -#include -#include -// @@protoc_insertion_point(includes) - -namespace safe_browsing { - -void protobuf_ShutdownFile_csd_2eproto() { - delete ClientDownloadRequest::default_instance_; - delete ClientDownloadRequest_Digests::default_instance_; - delete ClientDownloadRequest_Resource::default_instance_; - delete ClientDownloadRequest_CertificateChain::default_instance_; - delete ClientDownloadRequest_CertificateChain_Element::default_instance_; - delete ClientDownloadRequest_SignatureInfo::default_instance_; - delete ClientDownloadRequest_PEImageHeaders::default_instance_; - delete ClientDownloadRequest_PEImageHeaders_DebugData::default_instance_; - delete ClientDownloadRequest_ImageHeaders::default_instance_; - delete ClientDownloadResponse::default_instance_; - delete ClientDownloadResponse_MoreInfo::default_instance_; -} - -void protobuf_AddDesc_csd_2eproto() { - static bool already_here = false; - if (already_here) return; - already_here = true; - GOOGLE_PROTOBUF_VERIFY_VERSION; - - ClientDownloadRequest::default_instance_ = new ClientDownloadRequest(); - ClientDownloadRequest_Digests::default_instance_ = new ClientDownloadRequest_Digests(); - ClientDownloadRequest_Resource::default_instance_ = new ClientDownloadRequest_Resource(); - ClientDownloadRequest_CertificateChain::default_instance_ = new ClientDownloadRequest_CertificateChain(); - ClientDownloadRequest_CertificateChain_Element::default_instance_ = new ClientDownloadRequest_CertificateChain_Element(); - ClientDownloadRequest_SignatureInfo::default_instance_ = new ClientDownloadRequest_SignatureInfo(); - ClientDownloadRequest_PEImageHeaders::default_instance_ = new ClientDownloadRequest_PEImageHeaders(); - ClientDownloadRequest_PEImageHeaders_DebugData::default_instance_ = new ClientDownloadRequest_PEImageHeaders_DebugData(); - ClientDownloadRequest_ImageHeaders::default_instance_ = new ClientDownloadRequest_ImageHeaders(); - ClientDownloadResponse::default_instance_ = new ClientDownloadResponse(); - ClientDownloadResponse_MoreInfo::default_instance_ = new ClientDownloadResponse_MoreInfo(); - ClientDownloadRequest::default_instance_->InitAsDefaultInstance(); - ClientDownloadRequest_Digests::default_instance_->InitAsDefaultInstance(); - ClientDownloadRequest_Resource::default_instance_->InitAsDefaultInstance(); - ClientDownloadRequest_CertificateChain::default_instance_->InitAsDefaultInstance(); - ClientDownloadRequest_CertificateChain_Element::default_instance_->InitAsDefaultInstance(); - ClientDownloadRequest_SignatureInfo::default_instance_->InitAsDefaultInstance(); - ClientDownloadRequest_PEImageHeaders::default_instance_->InitAsDefaultInstance(); - ClientDownloadRequest_PEImageHeaders_DebugData::default_instance_->InitAsDefaultInstance(); - ClientDownloadRequest_ImageHeaders::default_instance_->InitAsDefaultInstance(); - ClientDownloadResponse::default_instance_->InitAsDefaultInstance(); - ClientDownloadResponse_MoreInfo::default_instance_->InitAsDefaultInstance(); - ::google::protobuf::internal::OnShutdown(&protobuf_ShutdownFile_csd_2eproto); -} - -// Force AddDescriptors() to be called at static initialization time. -struct StaticDescriptorInitializer_csd_2eproto { - StaticDescriptorInitializer_csd_2eproto() { - protobuf_AddDesc_csd_2eproto(); - } -} static_descriptor_initializer_csd_2eproto_; - - -// =================================================================== - -bool ClientDownloadRequest_ResourceType_IsValid(int value) { - switch(value) { - case 0: - case 1: - case 2: - case 3: - return true; - default: - return false; - } -} - -#ifndef _MSC_VER -const ClientDownloadRequest_ResourceType ClientDownloadRequest::DOWNLOAD_URL; -const ClientDownloadRequest_ResourceType ClientDownloadRequest::DOWNLOAD_REDIRECT; -const ClientDownloadRequest_ResourceType ClientDownloadRequest::TAB_URL; -const ClientDownloadRequest_ResourceType ClientDownloadRequest::TAB_REDIRECT; -const ClientDownloadRequest_ResourceType ClientDownloadRequest::ResourceType_MIN; -const ClientDownloadRequest_ResourceType ClientDownloadRequest::ResourceType_MAX; -const int ClientDownloadRequest::ResourceType_ARRAYSIZE; -#endif // _MSC_VER -bool ClientDownloadRequest_DownloadType_IsValid(int value) { - switch(value) { - case 0: - case 1: - case 2: - case 3: - return true; - default: - return false; - } -} - -#ifndef _MSC_VER -const ClientDownloadRequest_DownloadType ClientDownloadRequest::WIN_EXECUTABLE; -const ClientDownloadRequest_DownloadType ClientDownloadRequest::CHROME_EXTENSION; -const ClientDownloadRequest_DownloadType ClientDownloadRequest::ANDROID_APK; -const ClientDownloadRequest_DownloadType ClientDownloadRequest::ZIPPED_EXECUTABLE; -const ClientDownloadRequest_DownloadType ClientDownloadRequest::DownloadType_MIN; -const ClientDownloadRequest_DownloadType ClientDownloadRequest::DownloadType_MAX; -const int ClientDownloadRequest::DownloadType_ARRAYSIZE; -#endif // _MSC_VER -#ifndef _MSC_VER -const int ClientDownloadRequest_Digests::kSha256FieldNumber; -const int ClientDownloadRequest_Digests::kSha1FieldNumber; -const int ClientDownloadRequest_Digests::kMd5FieldNumber; -#endif // !_MSC_VER - -ClientDownloadRequest_Digests::ClientDownloadRequest_Digests() - : ::google::protobuf::MessageLite() { - SharedCtor(); -} - -void ClientDownloadRequest_Digests::InitAsDefaultInstance() { -} - -ClientDownloadRequest_Digests::ClientDownloadRequest_Digests(const ClientDownloadRequest_Digests& from) - : ::google::protobuf::MessageLite() { - SharedCtor(); - MergeFrom(from); -} - -void ClientDownloadRequest_Digests::SharedCtor() { - _cached_size_ = 0; - sha256_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - sha1_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - md5_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -ClientDownloadRequest_Digests::~ClientDownloadRequest_Digests() { - SharedDtor(); -} - -void ClientDownloadRequest_Digests::SharedDtor() { - if (sha256_ != &::google::protobuf::internal::kEmptyString) { - delete sha256_; - } - if (sha1_ != &::google::protobuf::internal::kEmptyString) { - delete sha1_; - } - if (md5_ != &::google::protobuf::internal::kEmptyString) { - delete md5_; - } - if (this != default_instance_) { - } -} - -void ClientDownloadRequest_Digests::SetCachedSize(int size) const { - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); -} -const ClientDownloadRequest_Digests& ClientDownloadRequest_Digests::default_instance() { - if (default_instance_ == NULL) protobuf_AddDesc_csd_2eproto(); return *default_instance_; -} - -ClientDownloadRequest_Digests* ClientDownloadRequest_Digests::default_instance_ = NULL; - -ClientDownloadRequest_Digests* ClientDownloadRequest_Digests::New() const { - return new ClientDownloadRequest_Digests; -} - -void ClientDownloadRequest_Digests::Clear() { - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (has_sha256()) { - if (sha256_ != &::google::protobuf::internal::kEmptyString) { - sha256_->clear(); - } - } - if (has_sha1()) { - if (sha1_ != &::google::protobuf::internal::kEmptyString) { - sha1_->clear(); - } - } - if (has_md5()) { - if (md5_ != &::google::protobuf::internal::kEmptyString) { - md5_->clear(); - } - } - } - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -bool ClientDownloadRequest_Digests::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!(EXPRESSION)) return false - ::google::protobuf::uint32 tag; - while ((tag = input->ReadTag()) != 0) { - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { - // optional bytes sha256 = 1; - case 1: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_sha256())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(18)) goto parse_sha1; - break; - } - - // optional bytes sha1 = 2; - case 2: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_sha1: - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_sha1())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(26)) goto parse_md5; - break; - } - - // optional bytes md5 = 3; - case 3: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_md5: - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_md5())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectAtEnd()) return true; - break; - } - - default: { - handle_uninterpreted: - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { - return true; - } - DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag)); - break; - } - } - } - return true; -#undef DO_ -} - -void ClientDownloadRequest_Digests::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // optional bytes sha256 = 1; - if (has_sha256()) { - ::google::protobuf::internal::WireFormatLite::WriteBytes( - 1, this->sha256(), output); - } - - // optional bytes sha1 = 2; - if (has_sha1()) { - ::google::protobuf::internal::WireFormatLite::WriteBytes( - 2, this->sha1(), output); - } - - // optional bytes md5 = 3; - if (has_md5()) { - ::google::protobuf::internal::WireFormatLite::WriteBytes( - 3, this->md5(), output); - } - -} - -int ClientDownloadRequest_Digests::ByteSize() const { - int total_size = 0; - - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - // optional bytes sha256 = 1; - if (has_sha256()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::BytesSize( - this->sha256()); - } - - // optional bytes sha1 = 2; - if (has_sha1()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::BytesSize( - this->sha1()); - } - - // optional bytes md5 = 3; - if (has_md5()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::BytesSize( - this->md5()); - } - - } - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = total_size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); - return total_size; -} - -void ClientDownloadRequest_Digests::CheckTypeAndMergeFrom( - const ::google::protobuf::MessageLite& from) { - MergeFrom(*::google::protobuf::down_cast(&from)); -} - -void ClientDownloadRequest_Digests::MergeFrom(const ClientDownloadRequest_Digests& from) { - GOOGLE_CHECK_NE(&from, this); - if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (from.has_sha256()) { - set_sha256(from.sha256()); - } - if (from.has_sha1()) { - set_sha1(from.sha1()); - } - if (from.has_md5()) { - set_md5(from.md5()); - } - } -} - -void ClientDownloadRequest_Digests::CopyFrom(const ClientDownloadRequest_Digests& from) { - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool ClientDownloadRequest_Digests::IsInitialized() const { - - return true; -} - -void ClientDownloadRequest_Digests::Swap(ClientDownloadRequest_Digests* other) { - if (other != this) { - std::swap(sha256_, other->sha256_); - std::swap(sha1_, other->sha1_); - std::swap(md5_, other->md5_); - std::swap(_has_bits_[0], other->_has_bits_[0]); - std::swap(_cached_size_, other->_cached_size_); - } -} - -::std::string ClientDownloadRequest_Digests::GetTypeName() const { - return "safe_browsing.ClientDownloadRequest.Digests"; -} - - -// ------------------------------------------------------------------- - -#ifndef _MSC_VER -const int ClientDownloadRequest_Resource::kUrlFieldNumber; -const int ClientDownloadRequest_Resource::kTypeFieldNumber; -const int ClientDownloadRequest_Resource::kRemoteIpFieldNumber; -const int ClientDownloadRequest_Resource::kReferrerFieldNumber; -#endif // !_MSC_VER - -ClientDownloadRequest_Resource::ClientDownloadRequest_Resource() - : ::google::protobuf::MessageLite() { - SharedCtor(); -} - -void ClientDownloadRequest_Resource::InitAsDefaultInstance() { -} - -ClientDownloadRequest_Resource::ClientDownloadRequest_Resource(const ClientDownloadRequest_Resource& from) - : ::google::protobuf::MessageLite() { - SharedCtor(); - MergeFrom(from); -} - -void ClientDownloadRequest_Resource::SharedCtor() { - _cached_size_ = 0; - url_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - type_ = 0; - remote_ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - referrer_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -ClientDownloadRequest_Resource::~ClientDownloadRequest_Resource() { - SharedDtor(); -} - -void ClientDownloadRequest_Resource::SharedDtor() { - if (url_ != &::google::protobuf::internal::kEmptyString) { - delete url_; - } - if (remote_ip_ != &::google::protobuf::internal::kEmptyString) { - delete remote_ip_; - } - if (referrer_ != &::google::protobuf::internal::kEmptyString) { - delete referrer_; - } - if (this != default_instance_) { - } -} - -void ClientDownloadRequest_Resource::SetCachedSize(int size) const { - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); -} -const ClientDownloadRequest_Resource& ClientDownloadRequest_Resource::default_instance() { - if (default_instance_ == NULL) protobuf_AddDesc_csd_2eproto(); return *default_instance_; -} - -ClientDownloadRequest_Resource* ClientDownloadRequest_Resource::default_instance_ = NULL; - -ClientDownloadRequest_Resource* ClientDownloadRequest_Resource::New() const { - return new ClientDownloadRequest_Resource; -} - -void ClientDownloadRequest_Resource::Clear() { - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (has_url()) { - if (url_ != &::google::protobuf::internal::kEmptyString) { - url_->clear(); - } - } - type_ = 0; - if (has_remote_ip()) { - if (remote_ip_ != &::google::protobuf::internal::kEmptyString) { - remote_ip_->clear(); - } - } - if (has_referrer()) { - if (referrer_ != &::google::protobuf::internal::kEmptyString) { - referrer_->clear(); - } - } - } - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -bool ClientDownloadRequest_Resource::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!(EXPRESSION)) return false - ::google::protobuf::uint32 tag; - while ((tag = input->ReadTag()) != 0) { - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { - // required string url = 1; - case 1: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( - input, this->mutable_url())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(16)) goto parse_type; - break; - } - - // required .safe_browsing.ClientDownloadRequest.ResourceType type = 2; - case 2: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { - parse_type: - int value; - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( - input, &value))); - if (::safe_browsing::ClientDownloadRequest_ResourceType_IsValid(value)) { - set_type(static_cast< ::safe_browsing::ClientDownloadRequest_ResourceType >(value)); - } - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(26)) goto parse_remote_ip; - break; - } - - // optional bytes remote_ip = 3; - case 3: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_remote_ip: - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_remote_ip())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(34)) goto parse_referrer; - break; - } - - // optional string referrer = 4; - case 4: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_referrer: - DO_(::google::protobuf::internal::WireFormatLite::ReadString( - input, this->mutable_referrer())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectAtEnd()) return true; - break; - } - - default: { - handle_uninterpreted: - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { - return true; - } - DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag)); - break; - } - } - } - return true; -#undef DO_ -} - -void ClientDownloadRequest_Resource::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // required string url = 1; - if (has_url()) { - ::google::protobuf::internal::WireFormatLite::WriteString( - 1, this->url(), output); - } - - // required .safe_browsing.ClientDownloadRequest.ResourceType type = 2; - if (has_type()) { - ::google::protobuf::internal::WireFormatLite::WriteEnum( - 2, this->type(), output); - } - - // optional bytes remote_ip = 3; - if (has_remote_ip()) { - ::google::protobuf::internal::WireFormatLite::WriteBytes( - 3, this->remote_ip(), output); - } - - // optional string referrer = 4; - if (has_referrer()) { - ::google::protobuf::internal::WireFormatLite::WriteString( - 4, this->referrer(), output); - } - -} - -int ClientDownloadRequest_Resource::ByteSize() const { - int total_size = 0; - - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - // required string url = 1; - if (has_url()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( - this->url()); - } - - // required .safe_browsing.ClientDownloadRequest.ResourceType type = 2; - if (has_type()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::EnumSize(this->type()); - } - - // optional bytes remote_ip = 3; - if (has_remote_ip()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::BytesSize( - this->remote_ip()); - } - - // optional string referrer = 4; - if (has_referrer()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( - this->referrer()); - } - - } - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = total_size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); - return total_size; -} - -void ClientDownloadRequest_Resource::CheckTypeAndMergeFrom( - const ::google::protobuf::MessageLite& from) { - MergeFrom(*::google::protobuf::down_cast(&from)); -} - -void ClientDownloadRequest_Resource::MergeFrom(const ClientDownloadRequest_Resource& from) { - GOOGLE_CHECK_NE(&from, this); - if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (from.has_url()) { - set_url(from.url()); - } - if (from.has_type()) { - set_type(from.type()); - } - if (from.has_remote_ip()) { - set_remote_ip(from.remote_ip()); - } - if (from.has_referrer()) { - set_referrer(from.referrer()); - } - } -} - -void ClientDownloadRequest_Resource::CopyFrom(const ClientDownloadRequest_Resource& from) { - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool ClientDownloadRequest_Resource::IsInitialized() const { - if ((_has_bits_[0] & 0x00000003) != 0x00000003) return false; - - return true; -} - -void ClientDownloadRequest_Resource::Swap(ClientDownloadRequest_Resource* other) { - if (other != this) { - std::swap(url_, other->url_); - std::swap(type_, other->type_); - std::swap(remote_ip_, other->remote_ip_); - std::swap(referrer_, other->referrer_); - std::swap(_has_bits_[0], other->_has_bits_[0]); - std::swap(_cached_size_, other->_cached_size_); - } -} - -::std::string ClientDownloadRequest_Resource::GetTypeName() const { - return "safe_browsing.ClientDownloadRequest.Resource"; -} - - -// ------------------------------------------------------------------- - -#ifndef _MSC_VER -const int ClientDownloadRequest_CertificateChain_Element::kCertificateFieldNumber; -#endif // !_MSC_VER - -ClientDownloadRequest_CertificateChain_Element::ClientDownloadRequest_CertificateChain_Element() - : ::google::protobuf::MessageLite() { - SharedCtor(); -} - -void ClientDownloadRequest_CertificateChain_Element::InitAsDefaultInstance() { -} - -ClientDownloadRequest_CertificateChain_Element::ClientDownloadRequest_CertificateChain_Element(const ClientDownloadRequest_CertificateChain_Element& from) - : ::google::protobuf::MessageLite() { - SharedCtor(); - MergeFrom(from); -} - -void ClientDownloadRequest_CertificateChain_Element::SharedCtor() { - _cached_size_ = 0; - certificate_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -ClientDownloadRequest_CertificateChain_Element::~ClientDownloadRequest_CertificateChain_Element() { - SharedDtor(); -} - -void ClientDownloadRequest_CertificateChain_Element::SharedDtor() { - if (certificate_ != &::google::protobuf::internal::kEmptyString) { - delete certificate_; - } - if (this != default_instance_) { - } -} - -void ClientDownloadRequest_CertificateChain_Element::SetCachedSize(int size) const { - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); -} -const ClientDownloadRequest_CertificateChain_Element& ClientDownloadRequest_CertificateChain_Element::default_instance() { - if (default_instance_ == NULL) protobuf_AddDesc_csd_2eproto(); return *default_instance_; -} - -ClientDownloadRequest_CertificateChain_Element* ClientDownloadRequest_CertificateChain_Element::default_instance_ = NULL; - -ClientDownloadRequest_CertificateChain_Element* ClientDownloadRequest_CertificateChain_Element::New() const { - return new ClientDownloadRequest_CertificateChain_Element; -} - -void ClientDownloadRequest_CertificateChain_Element::Clear() { - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (has_certificate()) { - if (certificate_ != &::google::protobuf::internal::kEmptyString) { - certificate_->clear(); - } - } - } - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -bool ClientDownloadRequest_CertificateChain_Element::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!(EXPRESSION)) return false - ::google::protobuf::uint32 tag; - while ((tag = input->ReadTag()) != 0) { - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { - // optional bytes certificate = 1; - case 1: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_certificate())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectAtEnd()) return true; - break; - } - - default: { - handle_uninterpreted: - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { - return true; - } - DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag)); - break; - } - } - } - return true; -#undef DO_ -} - -void ClientDownloadRequest_CertificateChain_Element::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // optional bytes certificate = 1; - if (has_certificate()) { - ::google::protobuf::internal::WireFormatLite::WriteBytes( - 1, this->certificate(), output); - } - -} - -int ClientDownloadRequest_CertificateChain_Element::ByteSize() const { - int total_size = 0; - - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - // optional bytes certificate = 1; - if (has_certificate()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::BytesSize( - this->certificate()); - } - - } - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = total_size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); - return total_size; -} - -void ClientDownloadRequest_CertificateChain_Element::CheckTypeAndMergeFrom( - const ::google::protobuf::MessageLite& from) { - MergeFrom(*::google::protobuf::down_cast(&from)); -} - -void ClientDownloadRequest_CertificateChain_Element::MergeFrom(const ClientDownloadRequest_CertificateChain_Element& from) { - GOOGLE_CHECK_NE(&from, this); - if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (from.has_certificate()) { - set_certificate(from.certificate()); - } - } -} - -void ClientDownloadRequest_CertificateChain_Element::CopyFrom(const ClientDownloadRequest_CertificateChain_Element& from) { - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool ClientDownloadRequest_CertificateChain_Element::IsInitialized() const { - - return true; -} - -void ClientDownloadRequest_CertificateChain_Element::Swap(ClientDownloadRequest_CertificateChain_Element* other) { - if (other != this) { - std::swap(certificate_, other->certificate_); - std::swap(_has_bits_[0], other->_has_bits_[0]); - std::swap(_cached_size_, other->_cached_size_); - } -} - -::std::string ClientDownloadRequest_CertificateChain_Element::GetTypeName() const { - return "safe_browsing.ClientDownloadRequest.CertificateChain.Element"; -} - - -// ------------------------------------------------------------------- - -#ifndef _MSC_VER -const int ClientDownloadRequest_CertificateChain::kElementFieldNumber; -#endif // !_MSC_VER - -ClientDownloadRequest_CertificateChain::ClientDownloadRequest_CertificateChain() - : ::google::protobuf::MessageLite() { - SharedCtor(); -} - -void ClientDownloadRequest_CertificateChain::InitAsDefaultInstance() { -} - -ClientDownloadRequest_CertificateChain::ClientDownloadRequest_CertificateChain(const ClientDownloadRequest_CertificateChain& from) - : ::google::protobuf::MessageLite() { - SharedCtor(); - MergeFrom(from); -} - -void ClientDownloadRequest_CertificateChain::SharedCtor() { - _cached_size_ = 0; - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -ClientDownloadRequest_CertificateChain::~ClientDownloadRequest_CertificateChain() { - SharedDtor(); -} - -void ClientDownloadRequest_CertificateChain::SharedDtor() { - if (this != default_instance_) { - } -} - -void ClientDownloadRequest_CertificateChain::SetCachedSize(int size) const { - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); -} -const ClientDownloadRequest_CertificateChain& ClientDownloadRequest_CertificateChain::default_instance() { - if (default_instance_ == NULL) protobuf_AddDesc_csd_2eproto(); return *default_instance_; -} - -ClientDownloadRequest_CertificateChain* ClientDownloadRequest_CertificateChain::default_instance_ = NULL; - -ClientDownloadRequest_CertificateChain* ClientDownloadRequest_CertificateChain::New() const { - return new ClientDownloadRequest_CertificateChain; -} - -void ClientDownloadRequest_CertificateChain::Clear() { - element_.Clear(); - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -bool ClientDownloadRequest_CertificateChain::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!(EXPRESSION)) return false - ::google::protobuf::uint32 tag; - while ((tag = input->ReadTag()) != 0) { - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { - // repeated .safe_browsing.ClientDownloadRequest.CertificateChain.Element element = 1; - case 1: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_element: - DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( - input, add_element())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(10)) goto parse_element; - if (input->ExpectAtEnd()) return true; - break; - } - - default: { - handle_uninterpreted: - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { - return true; - } - DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag)); - break; - } - } - } - return true; -#undef DO_ -} - -void ClientDownloadRequest_CertificateChain::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // repeated .safe_browsing.ClientDownloadRequest.CertificateChain.Element element = 1; - for (int i = 0; i < this->element_size(); i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessage( - 1, this->element(i), output); - } - -} - -int ClientDownloadRequest_CertificateChain::ByteSize() const { - int total_size = 0; - - // repeated .safe_browsing.ClientDownloadRequest.CertificateChain.Element element = 1; - total_size += 1 * this->element_size(); - for (int i = 0; i < this->element_size(); i++) { - total_size += - ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( - this->element(i)); - } - - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = total_size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); - return total_size; -} - -void ClientDownloadRequest_CertificateChain::CheckTypeAndMergeFrom( - const ::google::protobuf::MessageLite& from) { - MergeFrom(*::google::protobuf::down_cast(&from)); -} - -void ClientDownloadRequest_CertificateChain::MergeFrom(const ClientDownloadRequest_CertificateChain& from) { - GOOGLE_CHECK_NE(&from, this); - element_.MergeFrom(from.element_); -} - -void ClientDownloadRequest_CertificateChain::CopyFrom(const ClientDownloadRequest_CertificateChain& from) { - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool ClientDownloadRequest_CertificateChain::IsInitialized() const { - - return true; -} - -void ClientDownloadRequest_CertificateChain::Swap(ClientDownloadRequest_CertificateChain* other) { - if (other != this) { - element_.Swap(&other->element_); - std::swap(_has_bits_[0], other->_has_bits_[0]); - std::swap(_cached_size_, other->_cached_size_); - } -} - -::std::string ClientDownloadRequest_CertificateChain::GetTypeName() const { - return "safe_browsing.ClientDownloadRequest.CertificateChain"; -} - - -// ------------------------------------------------------------------- - -#ifndef _MSC_VER -const int ClientDownloadRequest_SignatureInfo::kCertificateChainFieldNumber; -const int ClientDownloadRequest_SignatureInfo::kTrustedFieldNumber; -#endif // !_MSC_VER - -ClientDownloadRequest_SignatureInfo::ClientDownloadRequest_SignatureInfo() - : ::google::protobuf::MessageLite() { - SharedCtor(); -} - -void ClientDownloadRequest_SignatureInfo::InitAsDefaultInstance() { -} - -ClientDownloadRequest_SignatureInfo::ClientDownloadRequest_SignatureInfo(const ClientDownloadRequest_SignatureInfo& from) - : ::google::protobuf::MessageLite() { - SharedCtor(); - MergeFrom(from); -} - -void ClientDownloadRequest_SignatureInfo::SharedCtor() { - _cached_size_ = 0; - trusted_ = false; - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -ClientDownloadRequest_SignatureInfo::~ClientDownloadRequest_SignatureInfo() { - SharedDtor(); -} - -void ClientDownloadRequest_SignatureInfo::SharedDtor() { - if (this != default_instance_) { - } -} - -void ClientDownloadRequest_SignatureInfo::SetCachedSize(int size) const { - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); -} -const ClientDownloadRequest_SignatureInfo& ClientDownloadRequest_SignatureInfo::default_instance() { - if (default_instance_ == NULL) protobuf_AddDesc_csd_2eproto(); return *default_instance_; -} - -ClientDownloadRequest_SignatureInfo* ClientDownloadRequest_SignatureInfo::default_instance_ = NULL; - -ClientDownloadRequest_SignatureInfo* ClientDownloadRequest_SignatureInfo::New() const { - return new ClientDownloadRequest_SignatureInfo; -} - -void ClientDownloadRequest_SignatureInfo::Clear() { - if (_has_bits_[1 / 32] & (0xffu << (1 % 32))) { - trusted_ = false; - } - certificate_chain_.Clear(); - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -bool ClientDownloadRequest_SignatureInfo::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!(EXPRESSION)) return false - ::google::protobuf::uint32 tag; - while ((tag = input->ReadTag()) != 0) { - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { - // repeated .safe_browsing.ClientDownloadRequest.CertificateChain certificate_chain = 1; - case 1: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_certificate_chain: - DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( - input, add_certificate_chain())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(10)) goto parse_certificate_chain; - if (input->ExpectTag(16)) goto parse_trusted; - break; - } - - // optional bool trusted = 2; - case 2: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { - parse_trusted: - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( - input, &trusted_))); - set_has_trusted(); - } else { - goto handle_uninterpreted; - } - if (input->ExpectAtEnd()) return true; - break; - } - - default: { - handle_uninterpreted: - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { - return true; - } - DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag)); - break; - } - } - } - return true; -#undef DO_ -} - -void ClientDownloadRequest_SignatureInfo::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // repeated .safe_browsing.ClientDownloadRequest.CertificateChain certificate_chain = 1; - for (int i = 0; i < this->certificate_chain_size(); i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessage( - 1, this->certificate_chain(i), output); - } - - // optional bool trusted = 2; - if (has_trusted()) { - ::google::protobuf::internal::WireFormatLite::WriteBool(2, this->trusted(), output); - } - -} - -int ClientDownloadRequest_SignatureInfo::ByteSize() const { - int total_size = 0; - - if (_has_bits_[1 / 32] & (0xffu << (1 % 32))) { - // optional bool trusted = 2; - if (has_trusted()) { - total_size += 1 + 1; - } - - } - // repeated .safe_browsing.ClientDownloadRequest.CertificateChain certificate_chain = 1; - total_size += 1 * this->certificate_chain_size(); - for (int i = 0; i < this->certificate_chain_size(); i++) { - total_size += - ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( - this->certificate_chain(i)); - } - - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = total_size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); - return total_size; -} - -void ClientDownloadRequest_SignatureInfo::CheckTypeAndMergeFrom( - const ::google::protobuf::MessageLite& from) { - MergeFrom(*::google::protobuf::down_cast(&from)); -} - -void ClientDownloadRequest_SignatureInfo::MergeFrom(const ClientDownloadRequest_SignatureInfo& from) { - GOOGLE_CHECK_NE(&from, this); - certificate_chain_.MergeFrom(from.certificate_chain_); - if (from._has_bits_[1 / 32] & (0xffu << (1 % 32))) { - if (from.has_trusted()) { - set_trusted(from.trusted()); - } - } -} - -void ClientDownloadRequest_SignatureInfo::CopyFrom(const ClientDownloadRequest_SignatureInfo& from) { - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool ClientDownloadRequest_SignatureInfo::IsInitialized() const { - - return true; -} - -void ClientDownloadRequest_SignatureInfo::Swap(ClientDownloadRequest_SignatureInfo* other) { - if (other != this) { - certificate_chain_.Swap(&other->certificate_chain_); - std::swap(trusted_, other->trusted_); - std::swap(_has_bits_[0], other->_has_bits_[0]); - std::swap(_cached_size_, other->_cached_size_); - } -} - -::std::string ClientDownloadRequest_SignatureInfo::GetTypeName() const { - return "safe_browsing.ClientDownloadRequest.SignatureInfo"; -} - - -// ------------------------------------------------------------------- - -#ifndef _MSC_VER -const int ClientDownloadRequest_PEImageHeaders_DebugData::kDirectoryEntryFieldNumber; -const int ClientDownloadRequest_PEImageHeaders_DebugData::kRawDataFieldNumber; -#endif // !_MSC_VER - -ClientDownloadRequest_PEImageHeaders_DebugData::ClientDownloadRequest_PEImageHeaders_DebugData() - : ::google::protobuf::MessageLite() { - SharedCtor(); -} - -void ClientDownloadRequest_PEImageHeaders_DebugData::InitAsDefaultInstance() { -} - -ClientDownloadRequest_PEImageHeaders_DebugData::ClientDownloadRequest_PEImageHeaders_DebugData(const ClientDownloadRequest_PEImageHeaders_DebugData& from) - : ::google::protobuf::MessageLite() { - SharedCtor(); - MergeFrom(from); -} - -void ClientDownloadRequest_PEImageHeaders_DebugData::SharedCtor() { - _cached_size_ = 0; - directory_entry_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - raw_data_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -ClientDownloadRequest_PEImageHeaders_DebugData::~ClientDownloadRequest_PEImageHeaders_DebugData() { - SharedDtor(); -} - -void ClientDownloadRequest_PEImageHeaders_DebugData::SharedDtor() { - if (directory_entry_ != &::google::protobuf::internal::kEmptyString) { - delete directory_entry_; - } - if (raw_data_ != &::google::protobuf::internal::kEmptyString) { - delete raw_data_; - } - if (this != default_instance_) { - } -} - -void ClientDownloadRequest_PEImageHeaders_DebugData::SetCachedSize(int size) const { - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); -} -const ClientDownloadRequest_PEImageHeaders_DebugData& ClientDownloadRequest_PEImageHeaders_DebugData::default_instance() { - if (default_instance_ == NULL) protobuf_AddDesc_csd_2eproto(); return *default_instance_; -} - -ClientDownloadRequest_PEImageHeaders_DebugData* ClientDownloadRequest_PEImageHeaders_DebugData::default_instance_ = NULL; - -ClientDownloadRequest_PEImageHeaders_DebugData* ClientDownloadRequest_PEImageHeaders_DebugData::New() const { - return new ClientDownloadRequest_PEImageHeaders_DebugData; -} - -void ClientDownloadRequest_PEImageHeaders_DebugData::Clear() { - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (has_directory_entry()) { - if (directory_entry_ != &::google::protobuf::internal::kEmptyString) { - directory_entry_->clear(); - } - } - if (has_raw_data()) { - if (raw_data_ != &::google::protobuf::internal::kEmptyString) { - raw_data_->clear(); - } - } - } - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -bool ClientDownloadRequest_PEImageHeaders_DebugData::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!(EXPRESSION)) return false - ::google::protobuf::uint32 tag; - while ((tag = input->ReadTag()) != 0) { - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { - // optional bytes directory_entry = 1; - case 1: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_directory_entry())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(18)) goto parse_raw_data; - break; - } - - // optional bytes raw_data = 2; - case 2: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_raw_data: - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_raw_data())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectAtEnd()) return true; - break; - } - - default: { - handle_uninterpreted: - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { - return true; - } - DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag)); - break; - } - } - } - return true; -#undef DO_ -} - -void ClientDownloadRequest_PEImageHeaders_DebugData::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // optional bytes directory_entry = 1; - if (has_directory_entry()) { - ::google::protobuf::internal::WireFormatLite::WriteBytes( - 1, this->directory_entry(), output); - } - - // optional bytes raw_data = 2; - if (has_raw_data()) { - ::google::protobuf::internal::WireFormatLite::WriteBytes( - 2, this->raw_data(), output); - } - -} - -int ClientDownloadRequest_PEImageHeaders_DebugData::ByteSize() const { - int total_size = 0; - - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - // optional bytes directory_entry = 1; - if (has_directory_entry()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::BytesSize( - this->directory_entry()); - } - - // optional bytes raw_data = 2; - if (has_raw_data()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::BytesSize( - this->raw_data()); - } - - } - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = total_size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); - return total_size; -} - -void ClientDownloadRequest_PEImageHeaders_DebugData::CheckTypeAndMergeFrom( - const ::google::protobuf::MessageLite& from) { - MergeFrom(*::google::protobuf::down_cast(&from)); -} - -void ClientDownloadRequest_PEImageHeaders_DebugData::MergeFrom(const ClientDownloadRequest_PEImageHeaders_DebugData& from) { - GOOGLE_CHECK_NE(&from, this); - if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (from.has_directory_entry()) { - set_directory_entry(from.directory_entry()); - } - if (from.has_raw_data()) { - set_raw_data(from.raw_data()); - } - } -} - -void ClientDownloadRequest_PEImageHeaders_DebugData::CopyFrom(const ClientDownloadRequest_PEImageHeaders_DebugData& from) { - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool ClientDownloadRequest_PEImageHeaders_DebugData::IsInitialized() const { - - return true; -} - -void ClientDownloadRequest_PEImageHeaders_DebugData::Swap(ClientDownloadRequest_PEImageHeaders_DebugData* other) { - if (other != this) { - std::swap(directory_entry_, other->directory_entry_); - std::swap(raw_data_, other->raw_data_); - std::swap(_has_bits_[0], other->_has_bits_[0]); - std::swap(_cached_size_, other->_cached_size_); - } -} - -::std::string ClientDownloadRequest_PEImageHeaders_DebugData::GetTypeName() const { - return "safe_browsing.ClientDownloadRequest.PEImageHeaders.DebugData"; -} - - -// ------------------------------------------------------------------- - -#ifndef _MSC_VER -const int ClientDownloadRequest_PEImageHeaders::kDosHeaderFieldNumber; -const int ClientDownloadRequest_PEImageHeaders::kFileHeaderFieldNumber; -const int ClientDownloadRequest_PEImageHeaders::kOptionalHeaders32FieldNumber; -const int ClientDownloadRequest_PEImageHeaders::kOptionalHeaders64FieldNumber; -const int ClientDownloadRequest_PEImageHeaders::kSectionHeaderFieldNumber; -const int ClientDownloadRequest_PEImageHeaders::kExportSectionDataFieldNumber; -const int ClientDownloadRequest_PEImageHeaders::kDebugDataFieldNumber; -#endif // !_MSC_VER - -ClientDownloadRequest_PEImageHeaders::ClientDownloadRequest_PEImageHeaders() - : ::google::protobuf::MessageLite() { - SharedCtor(); -} - -void ClientDownloadRequest_PEImageHeaders::InitAsDefaultInstance() { -} - -ClientDownloadRequest_PEImageHeaders::ClientDownloadRequest_PEImageHeaders(const ClientDownloadRequest_PEImageHeaders& from) - : ::google::protobuf::MessageLite() { - SharedCtor(); - MergeFrom(from); -} - -void ClientDownloadRequest_PEImageHeaders::SharedCtor() { - _cached_size_ = 0; - dos_header_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - file_header_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - optional_headers32_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - optional_headers64_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - export_section_data_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -ClientDownloadRequest_PEImageHeaders::~ClientDownloadRequest_PEImageHeaders() { - SharedDtor(); -} - -void ClientDownloadRequest_PEImageHeaders::SharedDtor() { - if (dos_header_ != &::google::protobuf::internal::kEmptyString) { - delete dos_header_; - } - if (file_header_ != &::google::protobuf::internal::kEmptyString) { - delete file_header_; - } - if (optional_headers32_ != &::google::protobuf::internal::kEmptyString) { - delete optional_headers32_; - } - if (optional_headers64_ != &::google::protobuf::internal::kEmptyString) { - delete optional_headers64_; - } - if (export_section_data_ != &::google::protobuf::internal::kEmptyString) { - delete export_section_data_; - } - if (this != default_instance_) { - } -} - -void ClientDownloadRequest_PEImageHeaders::SetCachedSize(int size) const { - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); -} -const ClientDownloadRequest_PEImageHeaders& ClientDownloadRequest_PEImageHeaders::default_instance() { - if (default_instance_ == NULL) protobuf_AddDesc_csd_2eproto(); return *default_instance_; -} - -ClientDownloadRequest_PEImageHeaders* ClientDownloadRequest_PEImageHeaders::default_instance_ = NULL; - -ClientDownloadRequest_PEImageHeaders* ClientDownloadRequest_PEImageHeaders::New() const { - return new ClientDownloadRequest_PEImageHeaders; -} - -void ClientDownloadRequest_PEImageHeaders::Clear() { - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (has_dos_header()) { - if (dos_header_ != &::google::protobuf::internal::kEmptyString) { - dos_header_->clear(); - } - } - if (has_file_header()) { - if (file_header_ != &::google::protobuf::internal::kEmptyString) { - file_header_->clear(); - } - } - if (has_optional_headers32()) { - if (optional_headers32_ != &::google::protobuf::internal::kEmptyString) { - optional_headers32_->clear(); - } - } - if (has_optional_headers64()) { - if (optional_headers64_ != &::google::protobuf::internal::kEmptyString) { - optional_headers64_->clear(); - } - } - if (has_export_section_data()) { - if (export_section_data_ != &::google::protobuf::internal::kEmptyString) { - export_section_data_->clear(); - } - } - } - section_header_.Clear(); - debug_data_.Clear(); - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -bool ClientDownloadRequest_PEImageHeaders::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!(EXPRESSION)) return false - ::google::protobuf::uint32 tag; - while ((tag = input->ReadTag()) != 0) { - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { - // optional bytes dos_header = 1; - case 1: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_dos_header())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(18)) goto parse_file_header; - break; - } - - // optional bytes file_header = 2; - case 2: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_file_header: - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_file_header())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(26)) goto parse_optional_headers32; - break; - } - - // optional bytes optional_headers32 = 3; - case 3: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_optional_headers32: - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_optional_headers32())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(34)) goto parse_optional_headers64; - break; - } - - // optional bytes optional_headers64 = 4; - case 4: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_optional_headers64: - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_optional_headers64())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(42)) goto parse_section_header; - break; - } - - // repeated bytes section_header = 5; - case 5: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_section_header: - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->add_section_header())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(42)) goto parse_section_header; - if (input->ExpectTag(50)) goto parse_export_section_data; - break; - } - - // optional bytes export_section_data = 6; - case 6: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_export_section_data: - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_export_section_data())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(58)) goto parse_debug_data; - break; - } - - // repeated .safe_browsing.ClientDownloadRequest.PEImageHeaders.DebugData debug_data = 7; - case 7: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_debug_data: - DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( - input, add_debug_data())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(58)) goto parse_debug_data; - if (input->ExpectAtEnd()) return true; - break; - } - - default: { - handle_uninterpreted: - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { - return true; - } - DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag)); - break; - } - } - } - return true; -#undef DO_ -} - -void ClientDownloadRequest_PEImageHeaders::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // optional bytes dos_header = 1; - if (has_dos_header()) { - ::google::protobuf::internal::WireFormatLite::WriteBytes( - 1, this->dos_header(), output); - } - - // optional bytes file_header = 2; - if (has_file_header()) { - ::google::protobuf::internal::WireFormatLite::WriteBytes( - 2, this->file_header(), output); - } - - // optional bytes optional_headers32 = 3; - if (has_optional_headers32()) { - ::google::protobuf::internal::WireFormatLite::WriteBytes( - 3, this->optional_headers32(), output); - } - - // optional bytes optional_headers64 = 4; - if (has_optional_headers64()) { - ::google::protobuf::internal::WireFormatLite::WriteBytes( - 4, this->optional_headers64(), output); - } - - // repeated bytes section_header = 5; - for (int i = 0; i < this->section_header_size(); i++) { - ::google::protobuf::internal::WireFormatLite::WriteBytes( - 5, this->section_header(i), output); - } - - // optional bytes export_section_data = 6; - if (has_export_section_data()) { - ::google::protobuf::internal::WireFormatLite::WriteBytes( - 6, this->export_section_data(), output); - } - - // repeated .safe_browsing.ClientDownloadRequest.PEImageHeaders.DebugData debug_data = 7; - for (int i = 0; i < this->debug_data_size(); i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessage( - 7, this->debug_data(i), output); - } - -} - -int ClientDownloadRequest_PEImageHeaders::ByteSize() const { - int total_size = 0; - - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - // optional bytes dos_header = 1; - if (has_dos_header()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::BytesSize( - this->dos_header()); - } - - // optional bytes file_header = 2; - if (has_file_header()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::BytesSize( - this->file_header()); - } - - // optional bytes optional_headers32 = 3; - if (has_optional_headers32()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::BytesSize( - this->optional_headers32()); - } - - // optional bytes optional_headers64 = 4; - if (has_optional_headers64()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::BytesSize( - this->optional_headers64()); - } - - // optional bytes export_section_data = 6; - if (has_export_section_data()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::BytesSize( - this->export_section_data()); - } - - } - // repeated bytes section_header = 5; - total_size += 1 * this->section_header_size(); - for (int i = 0; i < this->section_header_size(); i++) { - total_size += ::google::protobuf::internal::WireFormatLite::BytesSize( - this->section_header(i)); - } - - // repeated .safe_browsing.ClientDownloadRequest.PEImageHeaders.DebugData debug_data = 7; - total_size += 1 * this->debug_data_size(); - for (int i = 0; i < this->debug_data_size(); i++) { - total_size += - ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( - this->debug_data(i)); - } - - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = total_size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); - return total_size; -} - -void ClientDownloadRequest_PEImageHeaders::CheckTypeAndMergeFrom( - const ::google::protobuf::MessageLite& from) { - MergeFrom(*::google::protobuf::down_cast(&from)); -} - -void ClientDownloadRequest_PEImageHeaders::MergeFrom(const ClientDownloadRequest_PEImageHeaders& from) { - GOOGLE_CHECK_NE(&from, this); - section_header_.MergeFrom(from.section_header_); - debug_data_.MergeFrom(from.debug_data_); - if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (from.has_dos_header()) { - set_dos_header(from.dos_header()); - } - if (from.has_file_header()) { - set_file_header(from.file_header()); - } - if (from.has_optional_headers32()) { - set_optional_headers32(from.optional_headers32()); - } - if (from.has_optional_headers64()) { - set_optional_headers64(from.optional_headers64()); - } - if (from.has_export_section_data()) { - set_export_section_data(from.export_section_data()); - } - } -} - -void ClientDownloadRequest_PEImageHeaders::CopyFrom(const ClientDownloadRequest_PEImageHeaders& from) { - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool ClientDownloadRequest_PEImageHeaders::IsInitialized() const { - - return true; -} - -void ClientDownloadRequest_PEImageHeaders::Swap(ClientDownloadRequest_PEImageHeaders* other) { - if (other != this) { - std::swap(dos_header_, other->dos_header_); - std::swap(file_header_, other->file_header_); - std::swap(optional_headers32_, other->optional_headers32_); - std::swap(optional_headers64_, other->optional_headers64_); - section_header_.Swap(&other->section_header_); - std::swap(export_section_data_, other->export_section_data_); - debug_data_.Swap(&other->debug_data_); - std::swap(_has_bits_[0], other->_has_bits_[0]); - std::swap(_cached_size_, other->_cached_size_); - } -} - -::std::string ClientDownloadRequest_PEImageHeaders::GetTypeName() const { - return "safe_browsing.ClientDownloadRequest.PEImageHeaders"; -} - - -// ------------------------------------------------------------------- - -#ifndef _MSC_VER -const int ClientDownloadRequest_ImageHeaders::kPeHeadersFieldNumber; -#endif // !_MSC_VER - -ClientDownloadRequest_ImageHeaders::ClientDownloadRequest_ImageHeaders() - : ::google::protobuf::MessageLite() { - SharedCtor(); -} - -void ClientDownloadRequest_ImageHeaders::InitAsDefaultInstance() { - pe_headers_ = const_cast< ::safe_browsing::ClientDownloadRequest_PEImageHeaders*>(&::safe_browsing::ClientDownloadRequest_PEImageHeaders::default_instance()); -} - -ClientDownloadRequest_ImageHeaders::ClientDownloadRequest_ImageHeaders(const ClientDownloadRequest_ImageHeaders& from) - : ::google::protobuf::MessageLite() { - SharedCtor(); - MergeFrom(from); -} - -void ClientDownloadRequest_ImageHeaders::SharedCtor() { - _cached_size_ = 0; - pe_headers_ = NULL; - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -ClientDownloadRequest_ImageHeaders::~ClientDownloadRequest_ImageHeaders() { - SharedDtor(); -} - -void ClientDownloadRequest_ImageHeaders::SharedDtor() { - if (this != default_instance_) { - delete pe_headers_; - } -} - -void ClientDownloadRequest_ImageHeaders::SetCachedSize(int size) const { - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); -} -const ClientDownloadRequest_ImageHeaders& ClientDownloadRequest_ImageHeaders::default_instance() { - if (default_instance_ == NULL) protobuf_AddDesc_csd_2eproto(); return *default_instance_; -} - -ClientDownloadRequest_ImageHeaders* ClientDownloadRequest_ImageHeaders::default_instance_ = NULL; - -ClientDownloadRequest_ImageHeaders* ClientDownloadRequest_ImageHeaders::New() const { - return new ClientDownloadRequest_ImageHeaders; -} - -void ClientDownloadRequest_ImageHeaders::Clear() { - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (has_pe_headers()) { - if (pe_headers_ != NULL) pe_headers_->::safe_browsing::ClientDownloadRequest_PEImageHeaders::Clear(); - } - } - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -bool ClientDownloadRequest_ImageHeaders::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!(EXPRESSION)) return false - ::google::protobuf::uint32 tag; - while ((tag = input->ReadTag()) != 0) { - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { - // optional .safe_browsing.ClientDownloadRequest.PEImageHeaders pe_headers = 1; - case 1: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( - input, mutable_pe_headers())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectAtEnd()) return true; - break; - } - - default: { - handle_uninterpreted: - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { - return true; - } - DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag)); - break; - } - } - } - return true; -#undef DO_ -} - -void ClientDownloadRequest_ImageHeaders::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // optional .safe_browsing.ClientDownloadRequest.PEImageHeaders pe_headers = 1; - if (has_pe_headers()) { - ::google::protobuf::internal::WireFormatLite::WriteMessage( - 1, this->pe_headers(), output); - } - -} - -int ClientDownloadRequest_ImageHeaders::ByteSize() const { - int total_size = 0; - - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - // optional .safe_browsing.ClientDownloadRequest.PEImageHeaders pe_headers = 1; - if (has_pe_headers()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( - this->pe_headers()); - } - - } - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = total_size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); - return total_size; -} - -void ClientDownloadRequest_ImageHeaders::CheckTypeAndMergeFrom( - const ::google::protobuf::MessageLite& from) { - MergeFrom(*::google::protobuf::down_cast(&from)); -} - -void ClientDownloadRequest_ImageHeaders::MergeFrom(const ClientDownloadRequest_ImageHeaders& from) { - GOOGLE_CHECK_NE(&from, this); - if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (from.has_pe_headers()) { - mutable_pe_headers()->::safe_browsing::ClientDownloadRequest_PEImageHeaders::MergeFrom(from.pe_headers()); - } - } -} - -void ClientDownloadRequest_ImageHeaders::CopyFrom(const ClientDownloadRequest_ImageHeaders& from) { - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool ClientDownloadRequest_ImageHeaders::IsInitialized() const { - - return true; -} - -void ClientDownloadRequest_ImageHeaders::Swap(ClientDownloadRequest_ImageHeaders* other) { - if (other != this) { - std::swap(pe_headers_, other->pe_headers_); - std::swap(_has_bits_[0], other->_has_bits_[0]); - std::swap(_cached_size_, other->_cached_size_); - } -} - -::std::string ClientDownloadRequest_ImageHeaders::GetTypeName() const { - return "safe_browsing.ClientDownloadRequest.ImageHeaders"; -} - - -// ------------------------------------------------------------------- - -#ifndef _MSC_VER -const int ClientDownloadRequest::kUrlFieldNumber; -const int ClientDownloadRequest::kDigestsFieldNumber; -const int ClientDownloadRequest::kLengthFieldNumber; -const int ClientDownloadRequest::kResourcesFieldNumber; -const int ClientDownloadRequest::kSignatureFieldNumber; -const int ClientDownloadRequest::kUserInitiatedFieldNumber; -const int ClientDownloadRequest::kFileBasenameFieldNumber; -const int ClientDownloadRequest::kDownloadTypeFieldNumber; -const int ClientDownloadRequest::kLocaleFieldNumber; -const int ClientDownloadRequest::kImageHeadersFieldNumber; -#endif // !_MSC_VER - -ClientDownloadRequest::ClientDownloadRequest() - : ::google::protobuf::MessageLite() { - SharedCtor(); -} - -void ClientDownloadRequest::InitAsDefaultInstance() { - digests_ = const_cast< ::safe_browsing::ClientDownloadRequest_Digests*>(&::safe_browsing::ClientDownloadRequest_Digests::default_instance()); - signature_ = const_cast< ::safe_browsing::ClientDownloadRequest_SignatureInfo*>(&::safe_browsing::ClientDownloadRequest_SignatureInfo::default_instance()); - image_headers_ = const_cast< ::safe_browsing::ClientDownloadRequest_ImageHeaders*>(&::safe_browsing::ClientDownloadRequest_ImageHeaders::default_instance()); -} - -ClientDownloadRequest::ClientDownloadRequest(const ClientDownloadRequest& from) - : ::google::protobuf::MessageLite() { - SharedCtor(); - MergeFrom(from); -} - -void ClientDownloadRequest::SharedCtor() { - _cached_size_ = 0; - url_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - digests_ = NULL; - length_ = GOOGLE_LONGLONG(0); - signature_ = NULL; - user_initiated_ = false; - file_basename_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - download_type_ = 0; - locale_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - image_headers_ = NULL; - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -ClientDownloadRequest::~ClientDownloadRequest() { - SharedDtor(); -} - -void ClientDownloadRequest::SharedDtor() { - if (url_ != &::google::protobuf::internal::kEmptyString) { - delete url_; - } - if (file_basename_ != &::google::protobuf::internal::kEmptyString) { - delete file_basename_; - } - if (locale_ != &::google::protobuf::internal::kEmptyString) { - delete locale_; - } - if (this != default_instance_) { - delete digests_; - delete signature_; - delete image_headers_; - } -} - -void ClientDownloadRequest::SetCachedSize(int size) const { - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); -} -const ClientDownloadRequest& ClientDownloadRequest::default_instance() { - if (default_instance_ == NULL) protobuf_AddDesc_csd_2eproto(); return *default_instance_; -} - -ClientDownloadRequest* ClientDownloadRequest::default_instance_ = NULL; - -ClientDownloadRequest* ClientDownloadRequest::New() const { - return new ClientDownloadRequest; -} - -void ClientDownloadRequest::Clear() { - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (has_url()) { - if (url_ != &::google::protobuf::internal::kEmptyString) { - url_->clear(); - } - } - if (has_digests()) { - if (digests_ != NULL) digests_->::safe_browsing::ClientDownloadRequest_Digests::Clear(); - } - length_ = GOOGLE_LONGLONG(0); - if (has_signature()) { - if (signature_ != NULL) signature_->::safe_browsing::ClientDownloadRequest_SignatureInfo::Clear(); - } - user_initiated_ = false; - if (has_file_basename()) { - if (file_basename_ != &::google::protobuf::internal::kEmptyString) { - file_basename_->clear(); - } - } - download_type_ = 0; - } - if (_has_bits_[8 / 32] & (0xffu << (8 % 32))) { - if (has_locale()) { - if (locale_ != &::google::protobuf::internal::kEmptyString) { - locale_->clear(); - } - } - if (has_image_headers()) { - if (image_headers_ != NULL) image_headers_->::safe_browsing::ClientDownloadRequest_ImageHeaders::Clear(); - } - } - resources_.Clear(); - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -bool ClientDownloadRequest::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!(EXPRESSION)) return false - ::google::protobuf::uint32 tag; - while ((tag = input->ReadTag()) != 0) { - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { - // required string url = 1; - case 1: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( - input, this->mutable_url())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(18)) goto parse_digests; - break; - } - - // required .safe_browsing.ClientDownloadRequest.Digests digests = 2; - case 2: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_digests: - DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( - input, mutable_digests())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(24)) goto parse_length; - break; - } - - // required int64 length = 3; - case 3: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { - parse_length: - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - ::google::protobuf::int64, ::google::protobuf::internal::WireFormatLite::TYPE_INT64>( - input, &length_))); - set_has_length(); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(34)) goto parse_resources; - break; - } - - // repeated .safe_browsing.ClientDownloadRequest.Resource resources = 4; - case 4: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_resources: - DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( - input, add_resources())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(34)) goto parse_resources; - if (input->ExpectTag(42)) goto parse_signature; - break; - } - - // optional .safe_browsing.ClientDownloadRequest.SignatureInfo signature = 5; - case 5: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_signature: - DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( - input, mutable_signature())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(48)) goto parse_user_initiated; - break; - } - - // optional bool user_initiated = 6; - case 6: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { - parse_user_initiated: - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>( - input, &user_initiated_))); - set_has_user_initiated(); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(74)) goto parse_file_basename; - break; - } - - // optional string file_basename = 9; - case 9: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_file_basename: - DO_(::google::protobuf::internal::WireFormatLite::ReadString( - input, this->mutable_file_basename())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(80)) goto parse_download_type; - break; - } - - // optional .safe_browsing.ClientDownloadRequest.DownloadType download_type = 10 [default = WIN_EXECUTABLE]; - case 10: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { - parse_download_type: - int value; - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( - input, &value))); - if (::safe_browsing::ClientDownloadRequest_DownloadType_IsValid(value)) { - set_download_type(static_cast< ::safe_browsing::ClientDownloadRequest_DownloadType >(value)); - } - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(90)) goto parse_locale; - break; - } - - // optional string locale = 11; - case 11: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_locale: - DO_(::google::protobuf::internal::WireFormatLite::ReadString( - input, this->mutable_locale())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(146)) goto parse_image_headers; - break; - } - - // optional .safe_browsing.ClientDownloadRequest.ImageHeaders image_headers = 18; - case 18: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_image_headers: - DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( - input, mutable_image_headers())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectAtEnd()) return true; - break; - } - - default: { - handle_uninterpreted: - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { - return true; - } - DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag)); - break; - } - } - } - return true; -#undef DO_ -} - -void ClientDownloadRequest::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // required string url = 1; - if (has_url()) { - ::google::protobuf::internal::WireFormatLite::WriteString( - 1, this->url(), output); - } - - // required .safe_browsing.ClientDownloadRequest.Digests digests = 2; - if (has_digests()) { - ::google::protobuf::internal::WireFormatLite::WriteMessage( - 2, this->digests(), output); - } - - // required int64 length = 3; - if (has_length()) { - ::google::protobuf::internal::WireFormatLite::WriteInt64(3, this->length(), output); - } - - // repeated .safe_browsing.ClientDownloadRequest.Resource resources = 4; - for (int i = 0; i < this->resources_size(); i++) { - ::google::protobuf::internal::WireFormatLite::WriteMessage( - 4, this->resources(i), output); - } - - // optional .safe_browsing.ClientDownloadRequest.SignatureInfo signature = 5; - if (has_signature()) { - ::google::protobuf::internal::WireFormatLite::WriteMessage( - 5, this->signature(), output); - } - - // optional bool user_initiated = 6; - if (has_user_initiated()) { - ::google::protobuf::internal::WireFormatLite::WriteBool(6, this->user_initiated(), output); - } - - // optional string file_basename = 9; - if (has_file_basename()) { - ::google::protobuf::internal::WireFormatLite::WriteString( - 9, this->file_basename(), output); - } - - // optional .safe_browsing.ClientDownloadRequest.DownloadType download_type = 10 [default = WIN_EXECUTABLE]; - if (has_download_type()) { - ::google::protobuf::internal::WireFormatLite::WriteEnum( - 10, this->download_type(), output); - } - - // optional string locale = 11; - if (has_locale()) { - ::google::protobuf::internal::WireFormatLite::WriteString( - 11, this->locale(), output); - } - - // optional .safe_browsing.ClientDownloadRequest.ImageHeaders image_headers = 18; - if (has_image_headers()) { - ::google::protobuf::internal::WireFormatLite::WriteMessage( - 18, this->image_headers(), output); - } - -} - -int ClientDownloadRequest::ByteSize() const { - int total_size = 0; - - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - // required string url = 1; - if (has_url()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( - this->url()); - } - - // required .safe_browsing.ClientDownloadRequest.Digests digests = 2; - if (has_digests()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( - this->digests()); - } - - // required int64 length = 3; - if (has_length()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::Int64Size( - this->length()); - } - - // optional .safe_browsing.ClientDownloadRequest.SignatureInfo signature = 5; - if (has_signature()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( - this->signature()); - } - - // optional bool user_initiated = 6; - if (has_user_initiated()) { - total_size += 1 + 1; - } - - // optional string file_basename = 9; - if (has_file_basename()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( - this->file_basename()); - } - - // optional .safe_browsing.ClientDownloadRequest.DownloadType download_type = 10 [default = WIN_EXECUTABLE]; - if (has_download_type()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::EnumSize(this->download_type()); - } - - } - if (_has_bits_[8 / 32] & (0xffu << (8 % 32))) { - // optional string locale = 11; - if (has_locale()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( - this->locale()); - } - - // optional .safe_browsing.ClientDownloadRequest.ImageHeaders image_headers = 18; - if (has_image_headers()) { - total_size += 2 + - ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( - this->image_headers()); - } - - } - // repeated .safe_browsing.ClientDownloadRequest.Resource resources = 4; - total_size += 1 * this->resources_size(); - for (int i = 0; i < this->resources_size(); i++) { - total_size += - ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( - this->resources(i)); - } - - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = total_size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); - return total_size; -} - -void ClientDownloadRequest::CheckTypeAndMergeFrom( - const ::google::protobuf::MessageLite& from) { - MergeFrom(*::google::protobuf::down_cast(&from)); -} - -void ClientDownloadRequest::MergeFrom(const ClientDownloadRequest& from) { - GOOGLE_CHECK_NE(&from, this); - resources_.MergeFrom(from.resources_); - if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (from.has_url()) { - set_url(from.url()); - } - if (from.has_digests()) { - mutable_digests()->::safe_browsing::ClientDownloadRequest_Digests::MergeFrom(from.digests()); - } - if (from.has_length()) { - set_length(from.length()); - } - if (from.has_signature()) { - mutable_signature()->::safe_browsing::ClientDownloadRequest_SignatureInfo::MergeFrom(from.signature()); - } - if (from.has_user_initiated()) { - set_user_initiated(from.user_initiated()); - } - if (from.has_file_basename()) { - set_file_basename(from.file_basename()); - } - if (from.has_download_type()) { - set_download_type(from.download_type()); - } - } - if (from._has_bits_[8 / 32] & (0xffu << (8 % 32))) { - if (from.has_locale()) { - set_locale(from.locale()); - } - if (from.has_image_headers()) { - mutable_image_headers()->::safe_browsing::ClientDownloadRequest_ImageHeaders::MergeFrom(from.image_headers()); - } - } -} - -void ClientDownloadRequest::CopyFrom(const ClientDownloadRequest& from) { - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool ClientDownloadRequest::IsInitialized() const { - if ((_has_bits_[0] & 0x00000007) != 0x00000007) return false; - - for (int i = 0; i < resources_size(); i++) { - if (!this->resources(i).IsInitialized()) return false; - } - return true; -} - -void ClientDownloadRequest::Swap(ClientDownloadRequest* other) { - if (other != this) { - std::swap(url_, other->url_); - std::swap(digests_, other->digests_); - std::swap(length_, other->length_); - resources_.Swap(&other->resources_); - std::swap(signature_, other->signature_); - std::swap(user_initiated_, other->user_initiated_); - std::swap(file_basename_, other->file_basename_); - std::swap(download_type_, other->download_type_); - std::swap(locale_, other->locale_); - std::swap(image_headers_, other->image_headers_); - std::swap(_has_bits_[0], other->_has_bits_[0]); - std::swap(_cached_size_, other->_cached_size_); - } -} - -::std::string ClientDownloadRequest::GetTypeName() const { - return "safe_browsing.ClientDownloadRequest"; -} - - -// =================================================================== - -bool ClientDownloadResponse_Verdict_IsValid(int value) { - switch(value) { - case 0: - case 1: - case 2: - case 3: - case 4: - return true; - default: - return false; - } -} - -#ifndef _MSC_VER -const ClientDownloadResponse_Verdict ClientDownloadResponse::SAFE; -const ClientDownloadResponse_Verdict ClientDownloadResponse::DANGEROUS; -const ClientDownloadResponse_Verdict ClientDownloadResponse::UNCOMMON; -const ClientDownloadResponse_Verdict ClientDownloadResponse::POTENTIALLY_UNWANTED; -const ClientDownloadResponse_Verdict ClientDownloadResponse::DANGEROUS_HOST; -const ClientDownloadResponse_Verdict ClientDownloadResponse::Verdict_MIN; -const ClientDownloadResponse_Verdict ClientDownloadResponse::Verdict_MAX; -const int ClientDownloadResponse::Verdict_ARRAYSIZE; -#endif // _MSC_VER -#ifndef _MSC_VER -const int ClientDownloadResponse_MoreInfo::kDescriptionFieldNumber; -const int ClientDownloadResponse_MoreInfo::kUrlFieldNumber; -#endif // !_MSC_VER - -ClientDownloadResponse_MoreInfo::ClientDownloadResponse_MoreInfo() - : ::google::protobuf::MessageLite() { - SharedCtor(); -} - -void ClientDownloadResponse_MoreInfo::InitAsDefaultInstance() { -} - -ClientDownloadResponse_MoreInfo::ClientDownloadResponse_MoreInfo(const ClientDownloadResponse_MoreInfo& from) - : ::google::protobuf::MessageLite() { - SharedCtor(); - MergeFrom(from); -} - -void ClientDownloadResponse_MoreInfo::SharedCtor() { - _cached_size_ = 0; - description_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - url_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -ClientDownloadResponse_MoreInfo::~ClientDownloadResponse_MoreInfo() { - SharedDtor(); -} - -void ClientDownloadResponse_MoreInfo::SharedDtor() { - if (description_ != &::google::protobuf::internal::kEmptyString) { - delete description_; - } - if (url_ != &::google::protobuf::internal::kEmptyString) { - delete url_; - } - if (this != default_instance_) { - } -} - -void ClientDownloadResponse_MoreInfo::SetCachedSize(int size) const { - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); -} -const ClientDownloadResponse_MoreInfo& ClientDownloadResponse_MoreInfo::default_instance() { - if (default_instance_ == NULL) protobuf_AddDesc_csd_2eproto(); return *default_instance_; -} - -ClientDownloadResponse_MoreInfo* ClientDownloadResponse_MoreInfo::default_instance_ = NULL; - -ClientDownloadResponse_MoreInfo* ClientDownloadResponse_MoreInfo::New() const { - return new ClientDownloadResponse_MoreInfo; -} - -void ClientDownloadResponse_MoreInfo::Clear() { - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (has_description()) { - if (description_ != &::google::protobuf::internal::kEmptyString) { - description_->clear(); - } - } - if (has_url()) { - if (url_ != &::google::protobuf::internal::kEmptyString) { - url_->clear(); - } - } - } - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -bool ClientDownloadResponse_MoreInfo::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!(EXPRESSION)) return false - ::google::protobuf::uint32 tag; - while ((tag = input->ReadTag()) != 0) { - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { - // optional string description = 1; - case 1: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - DO_(::google::protobuf::internal::WireFormatLite::ReadString( - input, this->mutable_description())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(18)) goto parse_url; - break; - } - - // optional string url = 2; - case 2: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_url: - DO_(::google::protobuf::internal::WireFormatLite::ReadString( - input, this->mutable_url())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectAtEnd()) return true; - break; - } - - default: { - handle_uninterpreted: - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { - return true; - } - DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag)); - break; - } - } - } - return true; -#undef DO_ -} - -void ClientDownloadResponse_MoreInfo::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // optional string description = 1; - if (has_description()) { - ::google::protobuf::internal::WireFormatLite::WriteString( - 1, this->description(), output); - } - - // optional string url = 2; - if (has_url()) { - ::google::protobuf::internal::WireFormatLite::WriteString( - 2, this->url(), output); - } - -} - -int ClientDownloadResponse_MoreInfo::ByteSize() const { - int total_size = 0; - - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - // optional string description = 1; - if (has_description()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( - this->description()); - } - - // optional string url = 2; - if (has_url()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::StringSize( - this->url()); - } - - } - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = total_size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); - return total_size; -} - -void ClientDownloadResponse_MoreInfo::CheckTypeAndMergeFrom( - const ::google::protobuf::MessageLite& from) { - MergeFrom(*::google::protobuf::down_cast(&from)); -} - -void ClientDownloadResponse_MoreInfo::MergeFrom(const ClientDownloadResponse_MoreInfo& from) { - GOOGLE_CHECK_NE(&from, this); - if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (from.has_description()) { - set_description(from.description()); - } - if (from.has_url()) { - set_url(from.url()); - } - } -} - -void ClientDownloadResponse_MoreInfo::CopyFrom(const ClientDownloadResponse_MoreInfo& from) { - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool ClientDownloadResponse_MoreInfo::IsInitialized() const { - - return true; -} - -void ClientDownloadResponse_MoreInfo::Swap(ClientDownloadResponse_MoreInfo* other) { - if (other != this) { - std::swap(description_, other->description_); - std::swap(url_, other->url_); - std::swap(_has_bits_[0], other->_has_bits_[0]); - std::swap(_cached_size_, other->_cached_size_); - } -} - -::std::string ClientDownloadResponse_MoreInfo::GetTypeName() const { - return "safe_browsing.ClientDownloadResponse.MoreInfo"; -} - - -// ------------------------------------------------------------------- - -#ifndef _MSC_VER -const int ClientDownloadResponse::kVerdictFieldNumber; -const int ClientDownloadResponse::kMoreInfoFieldNumber; -const int ClientDownloadResponse::kTokenFieldNumber; -#endif // !_MSC_VER - -ClientDownloadResponse::ClientDownloadResponse() - : ::google::protobuf::MessageLite() { - SharedCtor(); -} - -void ClientDownloadResponse::InitAsDefaultInstance() { - more_info_ = const_cast< ::safe_browsing::ClientDownloadResponse_MoreInfo*>(&::safe_browsing::ClientDownloadResponse_MoreInfo::default_instance()); -} - -ClientDownloadResponse::ClientDownloadResponse(const ClientDownloadResponse& from) - : ::google::protobuf::MessageLite() { - SharedCtor(); - MergeFrom(from); -} - -void ClientDownloadResponse::SharedCtor() { - _cached_size_ = 0; - verdict_ = 0; - more_info_ = NULL; - token_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -ClientDownloadResponse::~ClientDownloadResponse() { - SharedDtor(); -} - -void ClientDownloadResponse::SharedDtor() { - if (token_ != &::google::protobuf::internal::kEmptyString) { - delete token_; - } - if (this != default_instance_) { - delete more_info_; - } -} - -void ClientDownloadResponse::SetCachedSize(int size) const { - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); -} -const ClientDownloadResponse& ClientDownloadResponse::default_instance() { - if (default_instance_ == NULL) protobuf_AddDesc_csd_2eproto(); return *default_instance_; -} - -ClientDownloadResponse* ClientDownloadResponse::default_instance_ = NULL; - -ClientDownloadResponse* ClientDownloadResponse::New() const { - return new ClientDownloadResponse; -} - -void ClientDownloadResponse::Clear() { - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - verdict_ = 0; - if (has_more_info()) { - if (more_info_ != NULL) more_info_->::safe_browsing::ClientDownloadResponse_MoreInfo::Clear(); - } - if (has_token()) { - if (token_ != &::google::protobuf::internal::kEmptyString) { - token_->clear(); - } - } - } - ::memset(_has_bits_, 0, sizeof(_has_bits_)); -} - -bool ClientDownloadResponse::MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input) { -#define DO_(EXPRESSION) if (!(EXPRESSION)) return false - ::google::protobuf::uint32 tag; - while ((tag = input->ReadTag()) != 0) { - switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { - // required .safe_browsing.ClientDownloadResponse.Verdict verdict = 1; - case 1: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { - int value; - DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< - int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>( - input, &value))); - if (::safe_browsing::ClientDownloadResponse_Verdict_IsValid(value)) { - set_verdict(static_cast< ::safe_browsing::ClientDownloadResponse_Verdict >(value)); - } - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(18)) goto parse_more_info; - break; - } - - // optional .safe_browsing.ClientDownloadResponse.MoreInfo more_info = 2; - case 2: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_more_info: - DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( - input, mutable_more_info())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectTag(26)) goto parse_token; - break; - } - - // optional bytes token = 3; - case 3: { - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { - parse_token: - DO_(::google::protobuf::internal::WireFormatLite::ReadBytes( - input, this->mutable_token())); - } else { - goto handle_uninterpreted; - } - if (input->ExpectAtEnd()) return true; - break; - } - - default: { - handle_uninterpreted: - if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { - return true; - } - DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag)); - break; - } - } - } - return true; -#undef DO_ -} - -void ClientDownloadResponse::SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const { - // required .safe_browsing.ClientDownloadResponse.Verdict verdict = 1; - if (has_verdict()) { - ::google::protobuf::internal::WireFormatLite::WriteEnum( - 1, this->verdict(), output); - } - - // optional .safe_browsing.ClientDownloadResponse.MoreInfo more_info = 2; - if (has_more_info()) { - ::google::protobuf::internal::WireFormatLite::WriteMessage( - 2, this->more_info(), output); - } - - // optional bytes token = 3; - if (has_token()) { - ::google::protobuf::internal::WireFormatLite::WriteBytes( - 3, this->token(), output); - } - -} - -int ClientDownloadResponse::ByteSize() const { - int total_size = 0; - - if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { - // required .safe_browsing.ClientDownloadResponse.Verdict verdict = 1; - if (has_verdict()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::EnumSize(this->verdict()); - } - - // optional .safe_browsing.ClientDownloadResponse.MoreInfo more_info = 2; - if (has_more_info()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual( - this->more_info()); - } - - // optional bytes token = 3; - if (has_token()) { - total_size += 1 + - ::google::protobuf::internal::WireFormatLite::BytesSize( - this->token()); - } - - } - GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); - _cached_size_ = total_size; - GOOGLE_SAFE_CONCURRENT_WRITES_END(); - return total_size; -} - -void ClientDownloadResponse::CheckTypeAndMergeFrom( - const ::google::protobuf::MessageLite& from) { - MergeFrom(*::google::protobuf::down_cast(&from)); -} - -void ClientDownloadResponse::MergeFrom(const ClientDownloadResponse& from) { - GOOGLE_CHECK_NE(&from, this); - if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { - if (from.has_verdict()) { - set_verdict(from.verdict()); - } - if (from.has_more_info()) { - mutable_more_info()->::safe_browsing::ClientDownloadResponse_MoreInfo::MergeFrom(from.more_info()); - } - if (from.has_token()) { - set_token(from.token()); - } - } -} - -void ClientDownloadResponse::CopyFrom(const ClientDownloadResponse& from) { - if (&from == this) return; - Clear(); - MergeFrom(from); -} - -bool ClientDownloadResponse::IsInitialized() const { - if ((_has_bits_[0] & 0x00000001) != 0x00000001) return false; - - return true; -} - -void ClientDownloadResponse::Swap(ClientDownloadResponse* other) { - if (other != this) { - std::swap(verdict_, other->verdict_); - std::swap(more_info_, other->more_info_); - std::swap(token_, other->token_); - std::swap(_has_bits_[0], other->_has_bits_[0]); - std::swap(_cached_size_, other->_cached_size_); - } -} - -::std::string ClientDownloadResponse::GetTypeName() const { - return "safe_browsing.ClientDownloadResponse"; -} - - -// @@protoc_insertion_point(namespace_scope) - -} // namespace safe_browsing - -// @@protoc_insertion_point(global_scope) diff --git a/toolkit/components/downloads/csd.pb.h b/toolkit/components/downloads/csd.pb.h deleted file mode 100644 index 77e5d79329..0000000000 --- a/toolkit/components/downloads/csd.pb.h +++ /dev/null @@ -1,2890 +0,0 @@ -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: csd.proto - -#ifndef PROTOBUF_csd_2eproto__INCLUDED -#define PROTOBUF_csd_2eproto__INCLUDED - -#include - -#include - -#if GOOGLE_PROTOBUF_VERSION < 2004000 -#error This file was generated by a newer version of protoc which is -#error incompatible with your Protocol Buffer headers. Please update -#error your headers. -#endif -#if 2004001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION -#error This file was generated by an older version of protoc which is -#error incompatible with your Protocol Buffer headers. Please -#error regenerate this file with a newer version of protoc. -#endif - -#include -#include -#include -// @@protoc_insertion_point(includes) - -namespace safe_browsing { - -// Internal implementation detail -- do not call these. -void protobuf_AddDesc_csd_2eproto(); -void protobuf_AssignDesc_csd_2eproto(); -void protobuf_ShutdownFile_csd_2eproto(); - -class ClientDownloadRequest; -class ClientDownloadRequest_Digests; -class ClientDownloadRequest_Resource; -class ClientDownloadRequest_CertificateChain; -class ClientDownloadRequest_CertificateChain_Element; -class ClientDownloadRequest_SignatureInfo; -class ClientDownloadRequest_PEImageHeaders; -class ClientDownloadRequest_PEImageHeaders_DebugData; -class ClientDownloadRequest_ImageHeaders; -class ClientDownloadResponse; -class ClientDownloadResponse_MoreInfo; - -enum ClientDownloadRequest_ResourceType { - ClientDownloadRequest_ResourceType_DOWNLOAD_URL = 0, - ClientDownloadRequest_ResourceType_DOWNLOAD_REDIRECT = 1, - ClientDownloadRequest_ResourceType_TAB_URL = 2, - ClientDownloadRequest_ResourceType_TAB_REDIRECT = 3 -}; -bool ClientDownloadRequest_ResourceType_IsValid(int value); -const ClientDownloadRequest_ResourceType ClientDownloadRequest_ResourceType_ResourceType_MIN = ClientDownloadRequest_ResourceType_DOWNLOAD_URL; -const ClientDownloadRequest_ResourceType ClientDownloadRequest_ResourceType_ResourceType_MAX = ClientDownloadRequest_ResourceType_TAB_REDIRECT; -const int ClientDownloadRequest_ResourceType_ResourceType_ARRAYSIZE = ClientDownloadRequest_ResourceType_ResourceType_MAX + 1; - -enum ClientDownloadRequest_DownloadType { - ClientDownloadRequest_DownloadType_WIN_EXECUTABLE = 0, - ClientDownloadRequest_DownloadType_CHROME_EXTENSION = 1, - ClientDownloadRequest_DownloadType_ANDROID_APK = 2, - ClientDownloadRequest_DownloadType_ZIPPED_EXECUTABLE = 3 -}; -bool ClientDownloadRequest_DownloadType_IsValid(int value); -const ClientDownloadRequest_DownloadType ClientDownloadRequest_DownloadType_DownloadType_MIN = ClientDownloadRequest_DownloadType_WIN_EXECUTABLE; -const ClientDownloadRequest_DownloadType ClientDownloadRequest_DownloadType_DownloadType_MAX = ClientDownloadRequest_DownloadType_ZIPPED_EXECUTABLE; -const int ClientDownloadRequest_DownloadType_DownloadType_ARRAYSIZE = ClientDownloadRequest_DownloadType_DownloadType_MAX + 1; - -enum ClientDownloadResponse_Verdict { - ClientDownloadResponse_Verdict_SAFE = 0, - ClientDownloadResponse_Verdict_DANGEROUS = 1, - ClientDownloadResponse_Verdict_UNCOMMON = 2, - ClientDownloadResponse_Verdict_POTENTIALLY_UNWANTED = 3, - ClientDownloadResponse_Verdict_DANGEROUS_HOST = 4 -}; -bool ClientDownloadResponse_Verdict_IsValid(int value); -const ClientDownloadResponse_Verdict ClientDownloadResponse_Verdict_Verdict_MIN = ClientDownloadResponse_Verdict_SAFE; -const ClientDownloadResponse_Verdict ClientDownloadResponse_Verdict_Verdict_MAX = ClientDownloadResponse_Verdict_DANGEROUS_HOST; -const int ClientDownloadResponse_Verdict_Verdict_ARRAYSIZE = ClientDownloadResponse_Verdict_Verdict_MAX + 1; - -// =================================================================== - -class ClientDownloadRequest_Digests : public ::google::protobuf::MessageLite { - public: - ClientDownloadRequest_Digests(); - virtual ~ClientDownloadRequest_Digests(); - - ClientDownloadRequest_Digests(const ClientDownloadRequest_Digests& from); - - inline ClientDownloadRequest_Digests& operator=(const ClientDownloadRequest_Digests& from) { - CopyFrom(from); - return *this; - } - - static const ClientDownloadRequest_Digests& default_instance(); - - void Swap(ClientDownloadRequest_Digests* other); - - // implements Message ---------------------------------------------- - - ClientDownloadRequest_Digests* New() const; - void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from); - void CopyFrom(const ClientDownloadRequest_Digests& from); - void MergeFrom(const ClientDownloadRequest_Digests& from); - void Clear(); - bool IsInitialized() const; - - int ByteSize() const; - bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input); - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const; - int GetCachedSize() const { return _cached_size_; } - private: - void SharedCtor(); - void SharedDtor(); - void SetCachedSize(int size) const; - public: - - ::std::string GetTypeName() const; - - // nested types ---------------------------------------------------- - - // accessors ------------------------------------------------------- - - // optional bytes sha256 = 1; - inline bool has_sha256() const; - inline void clear_sha256(); - static const int kSha256FieldNumber = 1; - inline const ::std::string& sha256() const; - inline void set_sha256(const ::std::string& value); - inline void set_sha256(const char* value); - inline void set_sha256(const void* value, size_t size); - inline ::std::string* mutable_sha256(); - inline ::std::string* release_sha256(); - - // optional bytes sha1 = 2; - inline bool has_sha1() const; - inline void clear_sha1(); - static const int kSha1FieldNumber = 2; - inline const ::std::string& sha1() const; - inline void set_sha1(const ::std::string& value); - inline void set_sha1(const char* value); - inline void set_sha1(const void* value, size_t size); - inline ::std::string* mutable_sha1(); - inline ::std::string* release_sha1(); - - // optional bytes md5 = 3; - inline bool has_md5() const; - inline void clear_md5(); - static const int kMd5FieldNumber = 3; - inline const ::std::string& md5() const; - inline void set_md5(const ::std::string& value); - inline void set_md5(const char* value); - inline void set_md5(const void* value, size_t size); - inline ::std::string* mutable_md5(); - inline ::std::string* release_md5(); - - // @@protoc_insertion_point(class_scope:safe_browsing.ClientDownloadRequest.Digests) - private: - inline void set_has_sha256(); - inline void clear_has_sha256(); - inline void set_has_sha1(); - inline void clear_has_sha1(); - inline void set_has_md5(); - inline void clear_has_md5(); - - ::std::string* sha256_; - ::std::string* sha1_; - ::std::string* md5_; - - mutable int _cached_size_; - ::google::protobuf::uint32 _has_bits_[(3 + 31) / 32]; - - friend void protobuf_AddDesc_csd_2eproto(); - friend void protobuf_AssignDesc_csd_2eproto(); - friend void protobuf_ShutdownFile_csd_2eproto(); - - void InitAsDefaultInstance(); - static ClientDownloadRequest_Digests* default_instance_; -}; -// ------------------------------------------------------------------- - -class ClientDownloadRequest_Resource : public ::google::protobuf::MessageLite { - public: - ClientDownloadRequest_Resource(); - virtual ~ClientDownloadRequest_Resource(); - - ClientDownloadRequest_Resource(const ClientDownloadRequest_Resource& from); - - inline ClientDownloadRequest_Resource& operator=(const ClientDownloadRequest_Resource& from) { - CopyFrom(from); - return *this; - } - - static const ClientDownloadRequest_Resource& default_instance(); - - void Swap(ClientDownloadRequest_Resource* other); - - // implements Message ---------------------------------------------- - - ClientDownloadRequest_Resource* New() const; - void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from); - void CopyFrom(const ClientDownloadRequest_Resource& from); - void MergeFrom(const ClientDownloadRequest_Resource& from); - void Clear(); - bool IsInitialized() const; - - int ByteSize() const; - bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input); - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const; - int GetCachedSize() const { return _cached_size_; } - private: - void SharedCtor(); - void SharedDtor(); - void SetCachedSize(int size) const; - public: - - ::std::string GetTypeName() const; - - // nested types ---------------------------------------------------- - - // accessors ------------------------------------------------------- - - // required string url = 1; - inline bool has_url() const; - inline void clear_url(); - static const int kUrlFieldNumber = 1; - inline const ::std::string& url() const; - inline void set_url(const ::std::string& value); - inline void set_url(const char* value); - inline void set_url(const char* value, size_t size); - inline ::std::string* mutable_url(); - inline ::std::string* release_url(); - - // required .safe_browsing.ClientDownloadRequest.ResourceType type = 2; - inline bool has_type() const; - inline void clear_type(); - static const int kTypeFieldNumber = 2; - inline ::safe_browsing::ClientDownloadRequest_ResourceType type() const; - inline void set_type(::safe_browsing::ClientDownloadRequest_ResourceType value); - - // optional bytes remote_ip = 3; - inline bool has_remote_ip() const; - inline void clear_remote_ip(); - static const int kRemoteIpFieldNumber = 3; - inline const ::std::string& remote_ip() const; - inline void set_remote_ip(const ::std::string& value); - inline void set_remote_ip(const char* value); - inline void set_remote_ip(const void* value, size_t size); - inline ::std::string* mutable_remote_ip(); - inline ::std::string* release_remote_ip(); - - // optional string referrer = 4; - inline bool has_referrer() const; - inline void clear_referrer(); - static const int kReferrerFieldNumber = 4; - inline const ::std::string& referrer() const; - inline void set_referrer(const ::std::string& value); - inline void set_referrer(const char* value); - inline void set_referrer(const char* value, size_t size); - inline ::std::string* mutable_referrer(); - inline ::std::string* release_referrer(); - - // @@protoc_insertion_point(class_scope:safe_browsing.ClientDownloadRequest.Resource) - private: - inline void set_has_url(); - inline void clear_has_url(); - inline void set_has_type(); - inline void clear_has_type(); - inline void set_has_remote_ip(); - inline void clear_has_remote_ip(); - inline void set_has_referrer(); - inline void clear_has_referrer(); - - ::std::string* url_; - ::std::string* remote_ip_; - ::std::string* referrer_; - int type_; - - mutable int _cached_size_; - ::google::protobuf::uint32 _has_bits_[(4 + 31) / 32]; - - friend void protobuf_AddDesc_csd_2eproto(); - friend void protobuf_AssignDesc_csd_2eproto(); - friend void protobuf_ShutdownFile_csd_2eproto(); - - void InitAsDefaultInstance(); - static ClientDownloadRequest_Resource* default_instance_; -}; -// ------------------------------------------------------------------- - -class ClientDownloadRequest_CertificateChain_Element : public ::google::protobuf::MessageLite { - public: - ClientDownloadRequest_CertificateChain_Element(); - virtual ~ClientDownloadRequest_CertificateChain_Element(); - - ClientDownloadRequest_CertificateChain_Element(const ClientDownloadRequest_CertificateChain_Element& from); - - inline ClientDownloadRequest_CertificateChain_Element& operator=(const ClientDownloadRequest_CertificateChain_Element& from) { - CopyFrom(from); - return *this; - } - - static const ClientDownloadRequest_CertificateChain_Element& default_instance(); - - void Swap(ClientDownloadRequest_CertificateChain_Element* other); - - // implements Message ---------------------------------------------- - - ClientDownloadRequest_CertificateChain_Element* New() const; - void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from); - void CopyFrom(const ClientDownloadRequest_CertificateChain_Element& from); - void MergeFrom(const ClientDownloadRequest_CertificateChain_Element& from); - void Clear(); - bool IsInitialized() const; - - int ByteSize() const; - bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input); - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const; - int GetCachedSize() const { return _cached_size_; } - private: - void SharedCtor(); - void SharedDtor(); - void SetCachedSize(int size) const; - public: - - ::std::string GetTypeName() const; - - // nested types ---------------------------------------------------- - - // accessors ------------------------------------------------------- - - // optional bytes certificate = 1; - inline bool has_certificate() const; - inline void clear_certificate(); - static const int kCertificateFieldNumber = 1; - inline const ::std::string& certificate() const; - inline void set_certificate(const ::std::string& value); - inline void set_certificate(const char* value); - inline void set_certificate(const void* value, size_t size); - inline ::std::string* mutable_certificate(); - inline ::std::string* release_certificate(); - - // @@protoc_insertion_point(class_scope:safe_browsing.ClientDownloadRequest.CertificateChain.Element) - private: - inline void set_has_certificate(); - inline void clear_has_certificate(); - - ::std::string* certificate_; - - mutable int _cached_size_; - ::google::protobuf::uint32 _has_bits_[(1 + 31) / 32]; - - friend void protobuf_AddDesc_csd_2eproto(); - friend void protobuf_AssignDesc_csd_2eproto(); - friend void protobuf_ShutdownFile_csd_2eproto(); - - void InitAsDefaultInstance(); - static ClientDownloadRequest_CertificateChain_Element* default_instance_; -}; -// ------------------------------------------------------------------- - -class ClientDownloadRequest_CertificateChain : public ::google::protobuf::MessageLite { - public: - ClientDownloadRequest_CertificateChain(); - virtual ~ClientDownloadRequest_CertificateChain(); - - ClientDownloadRequest_CertificateChain(const ClientDownloadRequest_CertificateChain& from); - - inline ClientDownloadRequest_CertificateChain& operator=(const ClientDownloadRequest_CertificateChain& from) { - CopyFrom(from); - return *this; - } - - static const ClientDownloadRequest_CertificateChain& default_instance(); - - void Swap(ClientDownloadRequest_CertificateChain* other); - - // implements Message ---------------------------------------------- - - ClientDownloadRequest_CertificateChain* New() const; - void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from); - void CopyFrom(const ClientDownloadRequest_CertificateChain& from); - void MergeFrom(const ClientDownloadRequest_CertificateChain& from); - void Clear(); - bool IsInitialized() const; - - int ByteSize() const; - bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input); - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const; - int GetCachedSize() const { return _cached_size_; } - private: - void SharedCtor(); - void SharedDtor(); - void SetCachedSize(int size) const; - public: - - ::std::string GetTypeName() const; - - // nested types ---------------------------------------------------- - - typedef ClientDownloadRequest_CertificateChain_Element Element; - - // accessors ------------------------------------------------------- - - // repeated .safe_browsing.ClientDownloadRequest.CertificateChain.Element element = 1; - inline int element_size() const; - inline void clear_element(); - static const int kElementFieldNumber = 1; - inline const ::safe_browsing::ClientDownloadRequest_CertificateChain_Element& element(int index) const; - inline ::safe_browsing::ClientDownloadRequest_CertificateChain_Element* mutable_element(int index); - inline ::safe_browsing::ClientDownloadRequest_CertificateChain_Element* add_element(); - inline const ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_CertificateChain_Element >& - element() const; - inline ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_CertificateChain_Element >* - mutable_element(); - - // @@protoc_insertion_point(class_scope:safe_browsing.ClientDownloadRequest.CertificateChain) - private: - - ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_CertificateChain_Element > element_; - - mutable int _cached_size_; - ::google::protobuf::uint32 _has_bits_[(1 + 31) / 32]; - - friend void protobuf_AddDesc_csd_2eproto(); - friend void protobuf_AssignDesc_csd_2eproto(); - friend void protobuf_ShutdownFile_csd_2eproto(); - - void InitAsDefaultInstance(); - static ClientDownloadRequest_CertificateChain* default_instance_; -}; -// ------------------------------------------------------------------- - -class ClientDownloadRequest_SignatureInfo : public ::google::protobuf::MessageLite { - public: - ClientDownloadRequest_SignatureInfo(); - virtual ~ClientDownloadRequest_SignatureInfo(); - - ClientDownloadRequest_SignatureInfo(const ClientDownloadRequest_SignatureInfo& from); - - inline ClientDownloadRequest_SignatureInfo& operator=(const ClientDownloadRequest_SignatureInfo& from) { - CopyFrom(from); - return *this; - } - - static const ClientDownloadRequest_SignatureInfo& default_instance(); - - void Swap(ClientDownloadRequest_SignatureInfo* other); - - // implements Message ---------------------------------------------- - - ClientDownloadRequest_SignatureInfo* New() const; - void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from); - void CopyFrom(const ClientDownloadRequest_SignatureInfo& from); - void MergeFrom(const ClientDownloadRequest_SignatureInfo& from); - void Clear(); - bool IsInitialized() const; - - int ByteSize() const; - bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input); - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const; - int GetCachedSize() const { return _cached_size_; } - private: - void SharedCtor(); - void SharedDtor(); - void SetCachedSize(int size) const; - public: - - ::std::string GetTypeName() const; - - // nested types ---------------------------------------------------- - - // accessors ------------------------------------------------------- - - // repeated .safe_browsing.ClientDownloadRequest.CertificateChain certificate_chain = 1; - inline int certificate_chain_size() const; - inline void clear_certificate_chain(); - static const int kCertificateChainFieldNumber = 1; - inline const ::safe_browsing::ClientDownloadRequest_CertificateChain& certificate_chain(int index) const; - inline ::safe_browsing::ClientDownloadRequest_CertificateChain* mutable_certificate_chain(int index); - inline ::safe_browsing::ClientDownloadRequest_CertificateChain* add_certificate_chain(); - inline const ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_CertificateChain >& - certificate_chain() const; - inline ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_CertificateChain >* - mutable_certificate_chain(); - - // optional bool trusted = 2; - inline bool has_trusted() const; - inline void clear_trusted(); - static const int kTrustedFieldNumber = 2; - inline bool trusted() const; - inline void set_trusted(bool value); - - // @@protoc_insertion_point(class_scope:safe_browsing.ClientDownloadRequest.SignatureInfo) - private: - inline void set_has_trusted(); - inline void clear_has_trusted(); - - ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_CertificateChain > certificate_chain_; - bool trusted_; - - mutable int _cached_size_; - ::google::protobuf::uint32 _has_bits_[(2 + 31) / 32]; - - friend void protobuf_AddDesc_csd_2eproto(); - friend void protobuf_AssignDesc_csd_2eproto(); - friend void protobuf_ShutdownFile_csd_2eproto(); - - void InitAsDefaultInstance(); - static ClientDownloadRequest_SignatureInfo* default_instance_; -}; -// ------------------------------------------------------------------- - -class ClientDownloadRequest_PEImageHeaders_DebugData : public ::google::protobuf::MessageLite { - public: - ClientDownloadRequest_PEImageHeaders_DebugData(); - virtual ~ClientDownloadRequest_PEImageHeaders_DebugData(); - - ClientDownloadRequest_PEImageHeaders_DebugData(const ClientDownloadRequest_PEImageHeaders_DebugData& from); - - inline ClientDownloadRequest_PEImageHeaders_DebugData& operator=(const ClientDownloadRequest_PEImageHeaders_DebugData& from) { - CopyFrom(from); - return *this; - } - - static const ClientDownloadRequest_PEImageHeaders_DebugData& default_instance(); - - void Swap(ClientDownloadRequest_PEImageHeaders_DebugData* other); - - // implements Message ---------------------------------------------- - - ClientDownloadRequest_PEImageHeaders_DebugData* New() const; - void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from); - void CopyFrom(const ClientDownloadRequest_PEImageHeaders_DebugData& from); - void MergeFrom(const ClientDownloadRequest_PEImageHeaders_DebugData& from); - void Clear(); - bool IsInitialized() const; - - int ByteSize() const; - bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input); - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const; - int GetCachedSize() const { return _cached_size_; } - private: - void SharedCtor(); - void SharedDtor(); - void SetCachedSize(int size) const; - public: - - ::std::string GetTypeName() const; - - // nested types ---------------------------------------------------- - - // accessors ------------------------------------------------------- - - // optional bytes directory_entry = 1; - inline bool has_directory_entry() const; - inline void clear_directory_entry(); - static const int kDirectoryEntryFieldNumber = 1; - inline const ::std::string& directory_entry() const; - inline void set_directory_entry(const ::std::string& value); - inline void set_directory_entry(const char* value); - inline void set_directory_entry(const void* value, size_t size); - inline ::std::string* mutable_directory_entry(); - inline ::std::string* release_directory_entry(); - - // optional bytes raw_data = 2; - inline bool has_raw_data() const; - inline void clear_raw_data(); - static const int kRawDataFieldNumber = 2; - inline const ::std::string& raw_data() const; - inline void set_raw_data(const ::std::string& value); - inline void set_raw_data(const char* value); - inline void set_raw_data(const void* value, size_t size); - inline ::std::string* mutable_raw_data(); - inline ::std::string* release_raw_data(); - - // @@protoc_insertion_point(class_scope:safe_browsing.ClientDownloadRequest.PEImageHeaders.DebugData) - private: - inline void set_has_directory_entry(); - inline void clear_has_directory_entry(); - inline void set_has_raw_data(); - inline void clear_has_raw_data(); - - ::std::string* directory_entry_; - ::std::string* raw_data_; - - mutable int _cached_size_; - ::google::protobuf::uint32 _has_bits_[(2 + 31) / 32]; - - friend void protobuf_AddDesc_csd_2eproto(); - friend void protobuf_AssignDesc_csd_2eproto(); - friend void protobuf_ShutdownFile_csd_2eproto(); - - void InitAsDefaultInstance(); - static ClientDownloadRequest_PEImageHeaders_DebugData* default_instance_; -}; -// ------------------------------------------------------------------- - -class ClientDownloadRequest_PEImageHeaders : public ::google::protobuf::MessageLite { - public: - ClientDownloadRequest_PEImageHeaders(); - virtual ~ClientDownloadRequest_PEImageHeaders(); - - ClientDownloadRequest_PEImageHeaders(const ClientDownloadRequest_PEImageHeaders& from); - - inline ClientDownloadRequest_PEImageHeaders& operator=(const ClientDownloadRequest_PEImageHeaders& from) { - CopyFrom(from); - return *this; - } - - static const ClientDownloadRequest_PEImageHeaders& default_instance(); - - void Swap(ClientDownloadRequest_PEImageHeaders* other); - - // implements Message ---------------------------------------------- - - ClientDownloadRequest_PEImageHeaders* New() const; - void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from); - void CopyFrom(const ClientDownloadRequest_PEImageHeaders& from); - void MergeFrom(const ClientDownloadRequest_PEImageHeaders& from); - void Clear(); - bool IsInitialized() const; - - int ByteSize() const; - bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input); - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const; - int GetCachedSize() const { return _cached_size_; } - private: - void SharedCtor(); - void SharedDtor(); - void SetCachedSize(int size) const; - public: - - ::std::string GetTypeName() const; - - // nested types ---------------------------------------------------- - - typedef ClientDownloadRequest_PEImageHeaders_DebugData DebugData; - - // accessors ------------------------------------------------------- - - // optional bytes dos_header = 1; - inline bool has_dos_header() const; - inline void clear_dos_header(); - static const int kDosHeaderFieldNumber = 1; - inline const ::std::string& dos_header() const; - inline void set_dos_header(const ::std::string& value); - inline void set_dos_header(const char* value); - inline void set_dos_header(const void* value, size_t size); - inline ::std::string* mutable_dos_header(); - inline ::std::string* release_dos_header(); - - // optional bytes file_header = 2; - inline bool has_file_header() const; - inline void clear_file_header(); - static const int kFileHeaderFieldNumber = 2; - inline const ::std::string& file_header() const; - inline void set_file_header(const ::std::string& value); - inline void set_file_header(const char* value); - inline void set_file_header(const void* value, size_t size); - inline ::std::string* mutable_file_header(); - inline ::std::string* release_file_header(); - - // optional bytes optional_headers32 = 3; - inline bool has_optional_headers32() const; - inline void clear_optional_headers32(); - static const int kOptionalHeaders32FieldNumber = 3; - inline const ::std::string& optional_headers32() const; - inline void set_optional_headers32(const ::std::string& value); - inline void set_optional_headers32(const char* value); - inline void set_optional_headers32(const void* value, size_t size); - inline ::std::string* mutable_optional_headers32(); - inline ::std::string* release_optional_headers32(); - - // optional bytes optional_headers64 = 4; - inline bool has_optional_headers64() const; - inline void clear_optional_headers64(); - static const int kOptionalHeaders64FieldNumber = 4; - inline const ::std::string& optional_headers64() const; - inline void set_optional_headers64(const ::std::string& value); - inline void set_optional_headers64(const char* value); - inline void set_optional_headers64(const void* value, size_t size); - inline ::std::string* mutable_optional_headers64(); - inline ::std::string* release_optional_headers64(); - - // repeated bytes section_header = 5; - inline int section_header_size() const; - inline void clear_section_header(); - static const int kSectionHeaderFieldNumber = 5; - inline const ::std::string& section_header(int index) const; - inline ::std::string* mutable_section_header(int index); - inline void set_section_header(int index, const ::std::string& value); - inline void set_section_header(int index, const char* value); - inline void set_section_header(int index, const void* value, size_t size); - inline ::std::string* add_section_header(); - inline void add_section_header(const ::std::string& value); - inline void add_section_header(const char* value); - inline void add_section_header(const void* value, size_t size); - inline const ::google::protobuf::RepeatedPtrField< ::std::string>& section_header() const; - inline ::google::protobuf::RepeatedPtrField< ::std::string>* mutable_section_header(); - - // optional bytes export_section_data = 6; - inline bool has_export_section_data() const; - inline void clear_export_section_data(); - static const int kExportSectionDataFieldNumber = 6; - inline const ::std::string& export_section_data() const; - inline void set_export_section_data(const ::std::string& value); - inline void set_export_section_data(const char* value); - inline void set_export_section_data(const void* value, size_t size); - inline ::std::string* mutable_export_section_data(); - inline ::std::string* release_export_section_data(); - - // repeated .safe_browsing.ClientDownloadRequest.PEImageHeaders.DebugData debug_data = 7; - inline int debug_data_size() const; - inline void clear_debug_data(); - static const int kDebugDataFieldNumber = 7; - inline const ::safe_browsing::ClientDownloadRequest_PEImageHeaders_DebugData& debug_data(int index) const; - inline ::safe_browsing::ClientDownloadRequest_PEImageHeaders_DebugData* mutable_debug_data(int index); - inline ::safe_browsing::ClientDownloadRequest_PEImageHeaders_DebugData* add_debug_data(); - inline const ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_PEImageHeaders_DebugData >& - debug_data() const; - inline ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_PEImageHeaders_DebugData >* - mutable_debug_data(); - - // @@protoc_insertion_point(class_scope:safe_browsing.ClientDownloadRequest.PEImageHeaders) - private: - inline void set_has_dos_header(); - inline void clear_has_dos_header(); - inline void set_has_file_header(); - inline void clear_has_file_header(); - inline void set_has_optional_headers32(); - inline void clear_has_optional_headers32(); - inline void set_has_optional_headers64(); - inline void clear_has_optional_headers64(); - inline void set_has_export_section_data(); - inline void clear_has_export_section_data(); - - ::std::string* dos_header_; - ::std::string* file_header_; - ::std::string* optional_headers32_; - ::std::string* optional_headers64_; - ::google::protobuf::RepeatedPtrField< ::std::string> section_header_; - ::std::string* export_section_data_; - ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_PEImageHeaders_DebugData > debug_data_; - - mutable int _cached_size_; - ::google::protobuf::uint32 _has_bits_[(7 + 31) / 32]; - - friend void protobuf_AddDesc_csd_2eproto(); - friend void protobuf_AssignDesc_csd_2eproto(); - friend void protobuf_ShutdownFile_csd_2eproto(); - - void InitAsDefaultInstance(); - static ClientDownloadRequest_PEImageHeaders* default_instance_; -}; -// ------------------------------------------------------------------- - -class ClientDownloadRequest_ImageHeaders : public ::google::protobuf::MessageLite { - public: - ClientDownloadRequest_ImageHeaders(); - virtual ~ClientDownloadRequest_ImageHeaders(); - - ClientDownloadRequest_ImageHeaders(const ClientDownloadRequest_ImageHeaders& from); - - inline ClientDownloadRequest_ImageHeaders& operator=(const ClientDownloadRequest_ImageHeaders& from) { - CopyFrom(from); - return *this; - } - - static const ClientDownloadRequest_ImageHeaders& default_instance(); - - void Swap(ClientDownloadRequest_ImageHeaders* other); - - // implements Message ---------------------------------------------- - - ClientDownloadRequest_ImageHeaders* New() const; - void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from); - void CopyFrom(const ClientDownloadRequest_ImageHeaders& from); - void MergeFrom(const ClientDownloadRequest_ImageHeaders& from); - void Clear(); - bool IsInitialized() const; - - int ByteSize() const; - bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input); - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const; - int GetCachedSize() const { return _cached_size_; } - private: - void SharedCtor(); - void SharedDtor(); - void SetCachedSize(int size) const; - public: - - ::std::string GetTypeName() const; - - // nested types ---------------------------------------------------- - - // accessors ------------------------------------------------------- - - // optional .safe_browsing.ClientDownloadRequest.PEImageHeaders pe_headers = 1; - inline bool has_pe_headers() const; - inline void clear_pe_headers(); - static const int kPeHeadersFieldNumber = 1; - inline const ::safe_browsing::ClientDownloadRequest_PEImageHeaders& pe_headers() const; - inline ::safe_browsing::ClientDownloadRequest_PEImageHeaders* mutable_pe_headers(); - inline ::safe_browsing::ClientDownloadRequest_PEImageHeaders* release_pe_headers(); - - // @@protoc_insertion_point(class_scope:safe_browsing.ClientDownloadRequest.ImageHeaders) - private: - inline void set_has_pe_headers(); - inline void clear_has_pe_headers(); - - ::safe_browsing::ClientDownloadRequest_PEImageHeaders* pe_headers_; - - mutable int _cached_size_; - ::google::protobuf::uint32 _has_bits_[(1 + 31) / 32]; - - friend void protobuf_AddDesc_csd_2eproto(); - friend void protobuf_AssignDesc_csd_2eproto(); - friend void protobuf_ShutdownFile_csd_2eproto(); - - void InitAsDefaultInstance(); - static ClientDownloadRequest_ImageHeaders* default_instance_; -}; -// ------------------------------------------------------------------- - -class ClientDownloadRequest : public ::google::protobuf::MessageLite { - public: - ClientDownloadRequest(); - virtual ~ClientDownloadRequest(); - - ClientDownloadRequest(const ClientDownloadRequest& from); - - inline ClientDownloadRequest& operator=(const ClientDownloadRequest& from) { - CopyFrom(from); - return *this; - } - - static const ClientDownloadRequest& default_instance(); - - void Swap(ClientDownloadRequest* other); - - // implements Message ---------------------------------------------- - - ClientDownloadRequest* New() const; - void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from); - void CopyFrom(const ClientDownloadRequest& from); - void MergeFrom(const ClientDownloadRequest& from); - void Clear(); - bool IsInitialized() const; - - int ByteSize() const; - bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input); - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const; - int GetCachedSize() const { return _cached_size_; } - private: - void SharedCtor(); - void SharedDtor(); - void SetCachedSize(int size) const; - public: - - ::std::string GetTypeName() const; - - // nested types ---------------------------------------------------- - - typedef ClientDownloadRequest_Digests Digests; - typedef ClientDownloadRequest_Resource Resource; - typedef ClientDownloadRequest_CertificateChain CertificateChain; - typedef ClientDownloadRequest_SignatureInfo SignatureInfo; - typedef ClientDownloadRequest_PEImageHeaders PEImageHeaders; - typedef ClientDownloadRequest_ImageHeaders ImageHeaders; - - typedef ClientDownloadRequest_ResourceType ResourceType; - static const ResourceType DOWNLOAD_URL = ClientDownloadRequest_ResourceType_DOWNLOAD_URL; - static const ResourceType DOWNLOAD_REDIRECT = ClientDownloadRequest_ResourceType_DOWNLOAD_REDIRECT; - static const ResourceType TAB_URL = ClientDownloadRequest_ResourceType_TAB_URL; - static const ResourceType TAB_REDIRECT = ClientDownloadRequest_ResourceType_TAB_REDIRECT; - static inline bool ResourceType_IsValid(int value) { - return ClientDownloadRequest_ResourceType_IsValid(value); - } - static const ResourceType ResourceType_MIN = - ClientDownloadRequest_ResourceType_ResourceType_MIN; - static const ResourceType ResourceType_MAX = - ClientDownloadRequest_ResourceType_ResourceType_MAX; - static const int ResourceType_ARRAYSIZE = - ClientDownloadRequest_ResourceType_ResourceType_ARRAYSIZE; - - typedef ClientDownloadRequest_DownloadType DownloadType; - static const DownloadType WIN_EXECUTABLE = ClientDownloadRequest_DownloadType_WIN_EXECUTABLE; - static const DownloadType CHROME_EXTENSION = ClientDownloadRequest_DownloadType_CHROME_EXTENSION; - static const DownloadType ANDROID_APK = ClientDownloadRequest_DownloadType_ANDROID_APK; - static const DownloadType ZIPPED_EXECUTABLE = ClientDownloadRequest_DownloadType_ZIPPED_EXECUTABLE; - static inline bool DownloadType_IsValid(int value) { - return ClientDownloadRequest_DownloadType_IsValid(value); - } - static const DownloadType DownloadType_MIN = - ClientDownloadRequest_DownloadType_DownloadType_MIN; - static const DownloadType DownloadType_MAX = - ClientDownloadRequest_DownloadType_DownloadType_MAX; - static const int DownloadType_ARRAYSIZE = - ClientDownloadRequest_DownloadType_DownloadType_ARRAYSIZE; - - // accessors ------------------------------------------------------- - - // required string url = 1; - inline bool has_url() const; - inline void clear_url(); - static const int kUrlFieldNumber = 1; - inline const ::std::string& url() const; - inline void set_url(const ::std::string& value); - inline void set_url(const char* value); - inline void set_url(const char* value, size_t size); - inline ::std::string* mutable_url(); - inline ::std::string* release_url(); - - // required .safe_browsing.ClientDownloadRequest.Digests digests = 2; - inline bool has_digests() const; - inline void clear_digests(); - static const int kDigestsFieldNumber = 2; - inline const ::safe_browsing::ClientDownloadRequest_Digests& digests() const; - inline ::safe_browsing::ClientDownloadRequest_Digests* mutable_digests(); - inline ::safe_browsing::ClientDownloadRequest_Digests* release_digests(); - - // required int64 length = 3; - inline bool has_length() const; - inline void clear_length(); - static const int kLengthFieldNumber = 3; - inline ::google::protobuf::int64 length() const; - inline void set_length(::google::protobuf::int64 value); - - // repeated .safe_browsing.ClientDownloadRequest.Resource resources = 4; - inline int resources_size() const; - inline void clear_resources(); - static const int kResourcesFieldNumber = 4; - inline const ::safe_browsing::ClientDownloadRequest_Resource& resources(int index) const; - inline ::safe_browsing::ClientDownloadRequest_Resource* mutable_resources(int index); - inline ::safe_browsing::ClientDownloadRequest_Resource* add_resources(); - inline const ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_Resource >& - resources() const; - inline ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_Resource >* - mutable_resources(); - - // optional .safe_browsing.ClientDownloadRequest.SignatureInfo signature = 5; - inline bool has_signature() const; - inline void clear_signature(); - static const int kSignatureFieldNumber = 5; - inline const ::safe_browsing::ClientDownloadRequest_SignatureInfo& signature() const; - inline ::safe_browsing::ClientDownloadRequest_SignatureInfo* mutable_signature(); - inline ::safe_browsing::ClientDownloadRequest_SignatureInfo* release_signature(); - - // optional bool user_initiated = 6; - inline bool has_user_initiated() const; - inline void clear_user_initiated(); - static const int kUserInitiatedFieldNumber = 6; - inline bool user_initiated() const; - inline void set_user_initiated(bool value); - - // optional string file_basename = 9; - inline bool has_file_basename() const; - inline void clear_file_basename(); - static const int kFileBasenameFieldNumber = 9; - inline const ::std::string& file_basename() const; - inline void set_file_basename(const ::std::string& value); - inline void set_file_basename(const char* value); - inline void set_file_basename(const char* value, size_t size); - inline ::std::string* mutable_file_basename(); - inline ::std::string* release_file_basename(); - - // optional .safe_browsing.ClientDownloadRequest.DownloadType download_type = 10 [default = WIN_EXECUTABLE]; - inline bool has_download_type() const; - inline void clear_download_type(); - static const int kDownloadTypeFieldNumber = 10; - inline ::safe_browsing::ClientDownloadRequest_DownloadType download_type() const; - inline void set_download_type(::safe_browsing::ClientDownloadRequest_DownloadType value); - - // optional string locale = 11; - inline bool has_locale() const; - inline void clear_locale(); - static const int kLocaleFieldNumber = 11; - inline const ::std::string& locale() const; - inline void set_locale(const ::std::string& value); - inline void set_locale(const char* value); - inline void set_locale(const char* value, size_t size); - inline ::std::string* mutable_locale(); - inline ::std::string* release_locale(); - - // optional .safe_browsing.ClientDownloadRequest.ImageHeaders image_headers = 18; - inline bool has_image_headers() const; - inline void clear_image_headers(); - static const int kImageHeadersFieldNumber = 18; - inline const ::safe_browsing::ClientDownloadRequest_ImageHeaders& image_headers() const; - inline ::safe_browsing::ClientDownloadRequest_ImageHeaders* mutable_image_headers(); - inline ::safe_browsing::ClientDownloadRequest_ImageHeaders* release_image_headers(); - - // @@protoc_insertion_point(class_scope:safe_browsing.ClientDownloadRequest) - private: - inline void set_has_url(); - inline void clear_has_url(); - inline void set_has_digests(); - inline void clear_has_digests(); - inline void set_has_length(); - inline void clear_has_length(); - inline void set_has_signature(); - inline void clear_has_signature(); - inline void set_has_user_initiated(); - inline void clear_has_user_initiated(); - inline void set_has_file_basename(); - inline void clear_has_file_basename(); - inline void set_has_download_type(); - inline void clear_has_download_type(); - inline void set_has_locale(); - inline void clear_has_locale(); - inline void set_has_image_headers(); - inline void clear_has_image_headers(); - - ::std::string* url_; - ::safe_browsing::ClientDownloadRequest_Digests* digests_; - ::google::protobuf::int64 length_; - ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_Resource > resources_; - ::safe_browsing::ClientDownloadRequest_SignatureInfo* signature_; - ::std::string* file_basename_; - bool user_initiated_; - int download_type_; - ::std::string* locale_; - ::safe_browsing::ClientDownloadRequest_ImageHeaders* image_headers_; - - mutable int _cached_size_; - ::google::protobuf::uint32 _has_bits_[(10 + 31) / 32]; - - friend void protobuf_AddDesc_csd_2eproto(); - friend void protobuf_AssignDesc_csd_2eproto(); - friend void protobuf_ShutdownFile_csd_2eproto(); - - void InitAsDefaultInstance(); - static ClientDownloadRequest* default_instance_; -}; -// ------------------------------------------------------------------- - -class ClientDownloadResponse_MoreInfo : public ::google::protobuf::MessageLite { - public: - ClientDownloadResponse_MoreInfo(); - virtual ~ClientDownloadResponse_MoreInfo(); - - ClientDownloadResponse_MoreInfo(const ClientDownloadResponse_MoreInfo& from); - - inline ClientDownloadResponse_MoreInfo& operator=(const ClientDownloadResponse_MoreInfo& from) { - CopyFrom(from); - return *this; - } - - static const ClientDownloadResponse_MoreInfo& default_instance(); - - void Swap(ClientDownloadResponse_MoreInfo* other); - - // implements Message ---------------------------------------------- - - ClientDownloadResponse_MoreInfo* New() const; - void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from); - void CopyFrom(const ClientDownloadResponse_MoreInfo& from); - void MergeFrom(const ClientDownloadResponse_MoreInfo& from); - void Clear(); - bool IsInitialized() const; - - int ByteSize() const; - bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input); - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const; - int GetCachedSize() const { return _cached_size_; } - private: - void SharedCtor(); - void SharedDtor(); - void SetCachedSize(int size) const; - public: - - ::std::string GetTypeName() const; - - // nested types ---------------------------------------------------- - - // accessors ------------------------------------------------------- - - // optional string description = 1; - inline bool has_description() const; - inline void clear_description(); - static const int kDescriptionFieldNumber = 1; - inline const ::std::string& description() const; - inline void set_description(const ::std::string& value); - inline void set_description(const char* value); - inline void set_description(const char* value, size_t size); - inline ::std::string* mutable_description(); - inline ::std::string* release_description(); - - // optional string url = 2; - inline bool has_url() const; - inline void clear_url(); - static const int kUrlFieldNumber = 2; - inline const ::std::string& url() const; - inline void set_url(const ::std::string& value); - inline void set_url(const char* value); - inline void set_url(const char* value, size_t size); - inline ::std::string* mutable_url(); - inline ::std::string* release_url(); - - // @@protoc_insertion_point(class_scope:safe_browsing.ClientDownloadResponse.MoreInfo) - private: - inline void set_has_description(); - inline void clear_has_description(); - inline void set_has_url(); - inline void clear_has_url(); - - ::std::string* description_; - ::std::string* url_; - - mutable int _cached_size_; - ::google::protobuf::uint32 _has_bits_[(2 + 31) / 32]; - - friend void protobuf_AddDesc_csd_2eproto(); - friend void protobuf_AssignDesc_csd_2eproto(); - friend void protobuf_ShutdownFile_csd_2eproto(); - - void InitAsDefaultInstance(); - static ClientDownloadResponse_MoreInfo* default_instance_; -}; -// ------------------------------------------------------------------- - -class ClientDownloadResponse : public ::google::protobuf::MessageLite { - public: - ClientDownloadResponse(); - virtual ~ClientDownloadResponse(); - - ClientDownloadResponse(const ClientDownloadResponse& from); - - inline ClientDownloadResponse& operator=(const ClientDownloadResponse& from) { - CopyFrom(from); - return *this; - } - - static const ClientDownloadResponse& default_instance(); - - void Swap(ClientDownloadResponse* other); - - // implements Message ---------------------------------------------- - - ClientDownloadResponse* New() const; - void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from); - void CopyFrom(const ClientDownloadResponse& from); - void MergeFrom(const ClientDownloadResponse& from); - void Clear(); - bool IsInitialized() const; - - int ByteSize() const; - bool MergePartialFromCodedStream( - ::google::protobuf::io::CodedInputStream* input); - void SerializeWithCachedSizes( - ::google::protobuf::io::CodedOutputStream* output) const; - int GetCachedSize() const { return _cached_size_; } - private: - void SharedCtor(); - void SharedDtor(); - void SetCachedSize(int size) const; - public: - - ::std::string GetTypeName() const; - - // nested types ---------------------------------------------------- - - typedef ClientDownloadResponse_MoreInfo MoreInfo; - - typedef ClientDownloadResponse_Verdict Verdict; - static const Verdict SAFE = ClientDownloadResponse_Verdict_SAFE; - static const Verdict DANGEROUS = ClientDownloadResponse_Verdict_DANGEROUS; - static const Verdict UNCOMMON = ClientDownloadResponse_Verdict_UNCOMMON; - static const Verdict POTENTIALLY_UNWANTED = ClientDownloadResponse_Verdict_POTENTIALLY_UNWANTED; - static const Verdict DANGEROUS_HOST = ClientDownloadResponse_Verdict_DANGEROUS_HOST; - static inline bool Verdict_IsValid(int value) { - return ClientDownloadResponse_Verdict_IsValid(value); - } - static const Verdict Verdict_MIN = - ClientDownloadResponse_Verdict_Verdict_MIN; - static const Verdict Verdict_MAX = - ClientDownloadResponse_Verdict_Verdict_MAX; - static const int Verdict_ARRAYSIZE = - ClientDownloadResponse_Verdict_Verdict_ARRAYSIZE; - - // accessors ------------------------------------------------------- - - // required .safe_browsing.ClientDownloadResponse.Verdict verdict = 1; - inline bool has_verdict() const; - inline void clear_verdict(); - static const int kVerdictFieldNumber = 1; - inline ::safe_browsing::ClientDownloadResponse_Verdict verdict() const; - inline void set_verdict(::safe_browsing::ClientDownloadResponse_Verdict value); - - // optional .safe_browsing.ClientDownloadResponse.MoreInfo more_info = 2; - inline bool has_more_info() const; - inline void clear_more_info(); - static const int kMoreInfoFieldNumber = 2; - inline const ::safe_browsing::ClientDownloadResponse_MoreInfo& more_info() const; - inline ::safe_browsing::ClientDownloadResponse_MoreInfo* mutable_more_info(); - inline ::safe_browsing::ClientDownloadResponse_MoreInfo* release_more_info(); - - // optional bytes token = 3; - inline bool has_token() const; - inline void clear_token(); - static const int kTokenFieldNumber = 3; - inline const ::std::string& token() const; - inline void set_token(const ::std::string& value); - inline void set_token(const char* value); - inline void set_token(const void* value, size_t size); - inline ::std::string* mutable_token(); - inline ::std::string* release_token(); - - // @@protoc_insertion_point(class_scope:safe_browsing.ClientDownloadResponse) - private: - inline void set_has_verdict(); - inline void clear_has_verdict(); - inline void set_has_more_info(); - inline void clear_has_more_info(); - inline void set_has_token(); - inline void clear_has_token(); - - ::safe_browsing::ClientDownloadResponse_MoreInfo* more_info_; - ::std::string* token_; - int verdict_; - - mutable int _cached_size_; - ::google::protobuf::uint32 _has_bits_[(3 + 31) / 32]; - - friend void protobuf_AddDesc_csd_2eproto(); - friend void protobuf_AssignDesc_csd_2eproto(); - friend void protobuf_ShutdownFile_csd_2eproto(); - - void InitAsDefaultInstance(); - static ClientDownloadResponse* default_instance_; -}; -// =================================================================== - - -// =================================================================== - -// ClientDownloadRequest_Digests - -// optional bytes sha256 = 1; -inline bool ClientDownloadRequest_Digests::has_sha256() const { - return (_has_bits_[0] & 0x00000001u) != 0; -} -inline void ClientDownloadRequest_Digests::set_has_sha256() { - _has_bits_[0] |= 0x00000001u; -} -inline void ClientDownloadRequest_Digests::clear_has_sha256() { - _has_bits_[0] &= ~0x00000001u; -} -inline void ClientDownloadRequest_Digests::clear_sha256() { - if (sha256_ != &::google::protobuf::internal::kEmptyString) { - sha256_->clear(); - } - clear_has_sha256(); -} -inline const ::std::string& ClientDownloadRequest_Digests::sha256() const { - return *sha256_; -} -inline void ClientDownloadRequest_Digests::set_sha256(const ::std::string& value) { - set_has_sha256(); - if (sha256_ == &::google::protobuf::internal::kEmptyString) { - sha256_ = new ::std::string; - } - sha256_->assign(value); -} -inline void ClientDownloadRequest_Digests::set_sha256(const char* value) { - set_has_sha256(); - if (sha256_ == &::google::protobuf::internal::kEmptyString) { - sha256_ = new ::std::string; - } - sha256_->assign(value); -} -inline void ClientDownloadRequest_Digests::set_sha256(const void* value, size_t size) { - set_has_sha256(); - if (sha256_ == &::google::protobuf::internal::kEmptyString) { - sha256_ = new ::std::string; - } - sha256_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest_Digests::mutable_sha256() { - set_has_sha256(); - if (sha256_ == &::google::protobuf::internal::kEmptyString) { - sha256_ = new ::std::string; - } - return sha256_; -} -inline ::std::string* ClientDownloadRequest_Digests::release_sha256() { - clear_has_sha256(); - if (sha256_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = sha256_; - sha256_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// optional bytes sha1 = 2; -inline bool ClientDownloadRequest_Digests::has_sha1() const { - return (_has_bits_[0] & 0x00000002u) != 0; -} -inline void ClientDownloadRequest_Digests::set_has_sha1() { - _has_bits_[0] |= 0x00000002u; -} -inline void ClientDownloadRequest_Digests::clear_has_sha1() { - _has_bits_[0] &= ~0x00000002u; -} -inline void ClientDownloadRequest_Digests::clear_sha1() { - if (sha1_ != &::google::protobuf::internal::kEmptyString) { - sha1_->clear(); - } - clear_has_sha1(); -} -inline const ::std::string& ClientDownloadRequest_Digests::sha1() const { - return *sha1_; -} -inline void ClientDownloadRequest_Digests::set_sha1(const ::std::string& value) { - set_has_sha1(); - if (sha1_ == &::google::protobuf::internal::kEmptyString) { - sha1_ = new ::std::string; - } - sha1_->assign(value); -} -inline void ClientDownloadRequest_Digests::set_sha1(const char* value) { - set_has_sha1(); - if (sha1_ == &::google::protobuf::internal::kEmptyString) { - sha1_ = new ::std::string; - } - sha1_->assign(value); -} -inline void ClientDownloadRequest_Digests::set_sha1(const void* value, size_t size) { - set_has_sha1(); - if (sha1_ == &::google::protobuf::internal::kEmptyString) { - sha1_ = new ::std::string; - } - sha1_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest_Digests::mutable_sha1() { - set_has_sha1(); - if (sha1_ == &::google::protobuf::internal::kEmptyString) { - sha1_ = new ::std::string; - } - return sha1_; -} -inline ::std::string* ClientDownloadRequest_Digests::release_sha1() { - clear_has_sha1(); - if (sha1_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = sha1_; - sha1_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// optional bytes md5 = 3; -inline bool ClientDownloadRequest_Digests::has_md5() const { - return (_has_bits_[0] & 0x00000004u) != 0; -} -inline void ClientDownloadRequest_Digests::set_has_md5() { - _has_bits_[0] |= 0x00000004u; -} -inline void ClientDownloadRequest_Digests::clear_has_md5() { - _has_bits_[0] &= ~0x00000004u; -} -inline void ClientDownloadRequest_Digests::clear_md5() { - if (md5_ != &::google::protobuf::internal::kEmptyString) { - md5_->clear(); - } - clear_has_md5(); -} -inline const ::std::string& ClientDownloadRequest_Digests::md5() const { - return *md5_; -} -inline void ClientDownloadRequest_Digests::set_md5(const ::std::string& value) { - set_has_md5(); - if (md5_ == &::google::protobuf::internal::kEmptyString) { - md5_ = new ::std::string; - } - md5_->assign(value); -} -inline void ClientDownloadRequest_Digests::set_md5(const char* value) { - set_has_md5(); - if (md5_ == &::google::protobuf::internal::kEmptyString) { - md5_ = new ::std::string; - } - md5_->assign(value); -} -inline void ClientDownloadRequest_Digests::set_md5(const void* value, size_t size) { - set_has_md5(); - if (md5_ == &::google::protobuf::internal::kEmptyString) { - md5_ = new ::std::string; - } - md5_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest_Digests::mutable_md5() { - set_has_md5(); - if (md5_ == &::google::protobuf::internal::kEmptyString) { - md5_ = new ::std::string; - } - return md5_; -} -inline ::std::string* ClientDownloadRequest_Digests::release_md5() { - clear_has_md5(); - if (md5_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = md5_; - md5_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// ------------------------------------------------------------------- - -// ClientDownloadRequest_Resource - -// required string url = 1; -inline bool ClientDownloadRequest_Resource::has_url() const { - return (_has_bits_[0] & 0x00000001u) != 0; -} -inline void ClientDownloadRequest_Resource::set_has_url() { - _has_bits_[0] |= 0x00000001u; -} -inline void ClientDownloadRequest_Resource::clear_has_url() { - _has_bits_[0] &= ~0x00000001u; -} -inline void ClientDownloadRequest_Resource::clear_url() { - if (url_ != &::google::protobuf::internal::kEmptyString) { - url_->clear(); - } - clear_has_url(); -} -inline const ::std::string& ClientDownloadRequest_Resource::url() const { - return *url_; -} -inline void ClientDownloadRequest_Resource::set_url(const ::std::string& value) { - set_has_url(); - if (url_ == &::google::protobuf::internal::kEmptyString) { - url_ = new ::std::string; - } - url_->assign(value); -} -inline void ClientDownloadRequest_Resource::set_url(const char* value) { - set_has_url(); - if (url_ == &::google::protobuf::internal::kEmptyString) { - url_ = new ::std::string; - } - url_->assign(value); -} -inline void ClientDownloadRequest_Resource::set_url(const char* value, size_t size) { - set_has_url(); - if (url_ == &::google::protobuf::internal::kEmptyString) { - url_ = new ::std::string; - } - url_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest_Resource::mutable_url() { - set_has_url(); - if (url_ == &::google::protobuf::internal::kEmptyString) { - url_ = new ::std::string; - } - return url_; -} -inline ::std::string* ClientDownloadRequest_Resource::release_url() { - clear_has_url(); - if (url_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = url_; - url_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// required .safe_browsing.ClientDownloadRequest.ResourceType type = 2; -inline bool ClientDownloadRequest_Resource::has_type() const { - return (_has_bits_[0] & 0x00000002u) != 0; -} -inline void ClientDownloadRequest_Resource::set_has_type() { - _has_bits_[0] |= 0x00000002u; -} -inline void ClientDownloadRequest_Resource::clear_has_type() { - _has_bits_[0] &= ~0x00000002u; -} -inline void ClientDownloadRequest_Resource::clear_type() { - type_ = 0; - clear_has_type(); -} -inline ::safe_browsing::ClientDownloadRequest_ResourceType ClientDownloadRequest_Resource::type() const { - return static_cast< ::safe_browsing::ClientDownloadRequest_ResourceType >(type_); -} -inline void ClientDownloadRequest_Resource::set_type(::safe_browsing::ClientDownloadRequest_ResourceType value) { - GOOGLE_DCHECK(::safe_browsing::ClientDownloadRequest_ResourceType_IsValid(value)); - set_has_type(); - type_ = value; -} - -// optional bytes remote_ip = 3; -inline bool ClientDownloadRequest_Resource::has_remote_ip() const { - return (_has_bits_[0] & 0x00000004u) != 0; -} -inline void ClientDownloadRequest_Resource::set_has_remote_ip() { - _has_bits_[0] |= 0x00000004u; -} -inline void ClientDownloadRequest_Resource::clear_has_remote_ip() { - _has_bits_[0] &= ~0x00000004u; -} -inline void ClientDownloadRequest_Resource::clear_remote_ip() { - if (remote_ip_ != &::google::protobuf::internal::kEmptyString) { - remote_ip_->clear(); - } - clear_has_remote_ip(); -} -inline const ::std::string& ClientDownloadRequest_Resource::remote_ip() const { - return *remote_ip_; -} -inline void ClientDownloadRequest_Resource::set_remote_ip(const ::std::string& value) { - set_has_remote_ip(); - if (remote_ip_ == &::google::protobuf::internal::kEmptyString) { - remote_ip_ = new ::std::string; - } - remote_ip_->assign(value); -} -inline void ClientDownloadRequest_Resource::set_remote_ip(const char* value) { - set_has_remote_ip(); - if (remote_ip_ == &::google::protobuf::internal::kEmptyString) { - remote_ip_ = new ::std::string; - } - remote_ip_->assign(value); -} -inline void ClientDownloadRequest_Resource::set_remote_ip(const void* value, size_t size) { - set_has_remote_ip(); - if (remote_ip_ == &::google::protobuf::internal::kEmptyString) { - remote_ip_ = new ::std::string; - } - remote_ip_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest_Resource::mutable_remote_ip() { - set_has_remote_ip(); - if (remote_ip_ == &::google::protobuf::internal::kEmptyString) { - remote_ip_ = new ::std::string; - } - return remote_ip_; -} -inline ::std::string* ClientDownloadRequest_Resource::release_remote_ip() { - clear_has_remote_ip(); - if (remote_ip_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = remote_ip_; - remote_ip_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// optional string referrer = 4; -inline bool ClientDownloadRequest_Resource::has_referrer() const { - return (_has_bits_[0] & 0x00000008u) != 0; -} -inline void ClientDownloadRequest_Resource::set_has_referrer() { - _has_bits_[0] |= 0x00000008u; -} -inline void ClientDownloadRequest_Resource::clear_has_referrer() { - _has_bits_[0] &= ~0x00000008u; -} -inline void ClientDownloadRequest_Resource::clear_referrer() { - if (referrer_ != &::google::protobuf::internal::kEmptyString) { - referrer_->clear(); - } - clear_has_referrer(); -} -inline const ::std::string& ClientDownloadRequest_Resource::referrer() const { - return *referrer_; -} -inline void ClientDownloadRequest_Resource::set_referrer(const ::std::string& value) { - set_has_referrer(); - if (referrer_ == &::google::protobuf::internal::kEmptyString) { - referrer_ = new ::std::string; - } - referrer_->assign(value); -} -inline void ClientDownloadRequest_Resource::set_referrer(const char* value) { - set_has_referrer(); - if (referrer_ == &::google::protobuf::internal::kEmptyString) { - referrer_ = new ::std::string; - } - referrer_->assign(value); -} -inline void ClientDownloadRequest_Resource::set_referrer(const char* value, size_t size) { - set_has_referrer(); - if (referrer_ == &::google::protobuf::internal::kEmptyString) { - referrer_ = new ::std::string; - } - referrer_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest_Resource::mutable_referrer() { - set_has_referrer(); - if (referrer_ == &::google::protobuf::internal::kEmptyString) { - referrer_ = new ::std::string; - } - return referrer_; -} -inline ::std::string* ClientDownloadRequest_Resource::release_referrer() { - clear_has_referrer(); - if (referrer_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = referrer_; - referrer_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// ------------------------------------------------------------------- - -// ClientDownloadRequest_CertificateChain_Element - -// optional bytes certificate = 1; -inline bool ClientDownloadRequest_CertificateChain_Element::has_certificate() const { - return (_has_bits_[0] & 0x00000001u) != 0; -} -inline void ClientDownloadRequest_CertificateChain_Element::set_has_certificate() { - _has_bits_[0] |= 0x00000001u; -} -inline void ClientDownloadRequest_CertificateChain_Element::clear_has_certificate() { - _has_bits_[0] &= ~0x00000001u; -} -inline void ClientDownloadRequest_CertificateChain_Element::clear_certificate() { - if (certificate_ != &::google::protobuf::internal::kEmptyString) { - certificate_->clear(); - } - clear_has_certificate(); -} -inline const ::std::string& ClientDownloadRequest_CertificateChain_Element::certificate() const { - return *certificate_; -} -inline void ClientDownloadRequest_CertificateChain_Element::set_certificate(const ::std::string& value) { - set_has_certificate(); - if (certificate_ == &::google::protobuf::internal::kEmptyString) { - certificate_ = new ::std::string; - } - certificate_->assign(value); -} -inline void ClientDownloadRequest_CertificateChain_Element::set_certificate(const char* value) { - set_has_certificate(); - if (certificate_ == &::google::protobuf::internal::kEmptyString) { - certificate_ = new ::std::string; - } - certificate_->assign(value); -} -inline void ClientDownloadRequest_CertificateChain_Element::set_certificate(const void* value, size_t size) { - set_has_certificate(); - if (certificate_ == &::google::protobuf::internal::kEmptyString) { - certificate_ = new ::std::string; - } - certificate_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest_CertificateChain_Element::mutable_certificate() { - set_has_certificate(); - if (certificate_ == &::google::protobuf::internal::kEmptyString) { - certificate_ = new ::std::string; - } - return certificate_; -} -inline ::std::string* ClientDownloadRequest_CertificateChain_Element::release_certificate() { - clear_has_certificate(); - if (certificate_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = certificate_; - certificate_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// ------------------------------------------------------------------- - -// ClientDownloadRequest_CertificateChain - -// repeated .safe_browsing.ClientDownloadRequest.CertificateChain.Element element = 1; -inline int ClientDownloadRequest_CertificateChain::element_size() const { - return element_.size(); -} -inline void ClientDownloadRequest_CertificateChain::clear_element() { - element_.Clear(); -} -inline const ::safe_browsing::ClientDownloadRequest_CertificateChain_Element& ClientDownloadRequest_CertificateChain::element(int index) const { - return element_.Get(index); -} -inline ::safe_browsing::ClientDownloadRequest_CertificateChain_Element* ClientDownloadRequest_CertificateChain::mutable_element(int index) { - return element_.Mutable(index); -} -inline ::safe_browsing::ClientDownloadRequest_CertificateChain_Element* ClientDownloadRequest_CertificateChain::add_element() { - return element_.Add(); -} -inline const ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_CertificateChain_Element >& -ClientDownloadRequest_CertificateChain::element() const { - return element_; -} -inline ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_CertificateChain_Element >* -ClientDownloadRequest_CertificateChain::mutable_element() { - return &element_; -} - -// ------------------------------------------------------------------- - -// ClientDownloadRequest_SignatureInfo - -// repeated .safe_browsing.ClientDownloadRequest.CertificateChain certificate_chain = 1; -inline int ClientDownloadRequest_SignatureInfo::certificate_chain_size() const { - return certificate_chain_.size(); -} -inline void ClientDownloadRequest_SignatureInfo::clear_certificate_chain() { - certificate_chain_.Clear(); -} -inline const ::safe_browsing::ClientDownloadRequest_CertificateChain& ClientDownloadRequest_SignatureInfo::certificate_chain(int index) const { - return certificate_chain_.Get(index); -} -inline ::safe_browsing::ClientDownloadRequest_CertificateChain* ClientDownloadRequest_SignatureInfo::mutable_certificate_chain(int index) { - return certificate_chain_.Mutable(index); -} -inline ::safe_browsing::ClientDownloadRequest_CertificateChain* ClientDownloadRequest_SignatureInfo::add_certificate_chain() { - return certificate_chain_.Add(); -} -inline const ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_CertificateChain >& -ClientDownloadRequest_SignatureInfo::certificate_chain() const { - return certificate_chain_; -} -inline ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_CertificateChain >* -ClientDownloadRequest_SignatureInfo::mutable_certificate_chain() { - return &certificate_chain_; -} - -// optional bool trusted = 2; -inline bool ClientDownloadRequest_SignatureInfo::has_trusted() const { - return (_has_bits_[0] & 0x00000002u) != 0; -} -inline void ClientDownloadRequest_SignatureInfo::set_has_trusted() { - _has_bits_[0] |= 0x00000002u; -} -inline void ClientDownloadRequest_SignatureInfo::clear_has_trusted() { - _has_bits_[0] &= ~0x00000002u; -} -inline void ClientDownloadRequest_SignatureInfo::clear_trusted() { - trusted_ = false; - clear_has_trusted(); -} -inline bool ClientDownloadRequest_SignatureInfo::trusted() const { - return trusted_; -} -inline void ClientDownloadRequest_SignatureInfo::set_trusted(bool value) { - set_has_trusted(); - trusted_ = value; -} - -// ------------------------------------------------------------------- - -// ClientDownloadRequest_PEImageHeaders_DebugData - -// optional bytes directory_entry = 1; -inline bool ClientDownloadRequest_PEImageHeaders_DebugData::has_directory_entry() const { - return (_has_bits_[0] & 0x00000001u) != 0; -} -inline void ClientDownloadRequest_PEImageHeaders_DebugData::set_has_directory_entry() { - _has_bits_[0] |= 0x00000001u; -} -inline void ClientDownloadRequest_PEImageHeaders_DebugData::clear_has_directory_entry() { - _has_bits_[0] &= ~0x00000001u; -} -inline void ClientDownloadRequest_PEImageHeaders_DebugData::clear_directory_entry() { - if (directory_entry_ != &::google::protobuf::internal::kEmptyString) { - directory_entry_->clear(); - } - clear_has_directory_entry(); -} -inline const ::std::string& ClientDownloadRequest_PEImageHeaders_DebugData::directory_entry() const { - return *directory_entry_; -} -inline void ClientDownloadRequest_PEImageHeaders_DebugData::set_directory_entry(const ::std::string& value) { - set_has_directory_entry(); - if (directory_entry_ == &::google::protobuf::internal::kEmptyString) { - directory_entry_ = new ::std::string; - } - directory_entry_->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders_DebugData::set_directory_entry(const char* value) { - set_has_directory_entry(); - if (directory_entry_ == &::google::protobuf::internal::kEmptyString) { - directory_entry_ = new ::std::string; - } - directory_entry_->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders_DebugData::set_directory_entry(const void* value, size_t size) { - set_has_directory_entry(); - if (directory_entry_ == &::google::protobuf::internal::kEmptyString) { - directory_entry_ = new ::std::string; - } - directory_entry_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest_PEImageHeaders_DebugData::mutable_directory_entry() { - set_has_directory_entry(); - if (directory_entry_ == &::google::protobuf::internal::kEmptyString) { - directory_entry_ = new ::std::string; - } - return directory_entry_; -} -inline ::std::string* ClientDownloadRequest_PEImageHeaders_DebugData::release_directory_entry() { - clear_has_directory_entry(); - if (directory_entry_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = directory_entry_; - directory_entry_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// optional bytes raw_data = 2; -inline bool ClientDownloadRequest_PEImageHeaders_DebugData::has_raw_data() const { - return (_has_bits_[0] & 0x00000002u) != 0; -} -inline void ClientDownloadRequest_PEImageHeaders_DebugData::set_has_raw_data() { - _has_bits_[0] |= 0x00000002u; -} -inline void ClientDownloadRequest_PEImageHeaders_DebugData::clear_has_raw_data() { - _has_bits_[0] &= ~0x00000002u; -} -inline void ClientDownloadRequest_PEImageHeaders_DebugData::clear_raw_data() { - if (raw_data_ != &::google::protobuf::internal::kEmptyString) { - raw_data_->clear(); - } - clear_has_raw_data(); -} -inline const ::std::string& ClientDownloadRequest_PEImageHeaders_DebugData::raw_data() const { - return *raw_data_; -} -inline void ClientDownloadRequest_PEImageHeaders_DebugData::set_raw_data(const ::std::string& value) { - set_has_raw_data(); - if (raw_data_ == &::google::protobuf::internal::kEmptyString) { - raw_data_ = new ::std::string; - } - raw_data_->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders_DebugData::set_raw_data(const char* value) { - set_has_raw_data(); - if (raw_data_ == &::google::protobuf::internal::kEmptyString) { - raw_data_ = new ::std::string; - } - raw_data_->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders_DebugData::set_raw_data(const void* value, size_t size) { - set_has_raw_data(); - if (raw_data_ == &::google::protobuf::internal::kEmptyString) { - raw_data_ = new ::std::string; - } - raw_data_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest_PEImageHeaders_DebugData::mutable_raw_data() { - set_has_raw_data(); - if (raw_data_ == &::google::protobuf::internal::kEmptyString) { - raw_data_ = new ::std::string; - } - return raw_data_; -} -inline ::std::string* ClientDownloadRequest_PEImageHeaders_DebugData::release_raw_data() { - clear_has_raw_data(); - if (raw_data_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = raw_data_; - raw_data_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// ------------------------------------------------------------------- - -// ClientDownloadRequest_PEImageHeaders - -// optional bytes dos_header = 1; -inline bool ClientDownloadRequest_PEImageHeaders::has_dos_header() const { - return (_has_bits_[0] & 0x00000001u) != 0; -} -inline void ClientDownloadRequest_PEImageHeaders::set_has_dos_header() { - _has_bits_[0] |= 0x00000001u; -} -inline void ClientDownloadRequest_PEImageHeaders::clear_has_dos_header() { - _has_bits_[0] &= ~0x00000001u; -} -inline void ClientDownloadRequest_PEImageHeaders::clear_dos_header() { - if (dos_header_ != &::google::protobuf::internal::kEmptyString) { - dos_header_->clear(); - } - clear_has_dos_header(); -} -inline const ::std::string& ClientDownloadRequest_PEImageHeaders::dos_header() const { - return *dos_header_; -} -inline void ClientDownloadRequest_PEImageHeaders::set_dos_header(const ::std::string& value) { - set_has_dos_header(); - if (dos_header_ == &::google::protobuf::internal::kEmptyString) { - dos_header_ = new ::std::string; - } - dos_header_->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders::set_dos_header(const char* value) { - set_has_dos_header(); - if (dos_header_ == &::google::protobuf::internal::kEmptyString) { - dos_header_ = new ::std::string; - } - dos_header_->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders::set_dos_header(const void* value, size_t size) { - set_has_dos_header(); - if (dos_header_ == &::google::protobuf::internal::kEmptyString) { - dos_header_ = new ::std::string; - } - dos_header_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest_PEImageHeaders::mutable_dos_header() { - set_has_dos_header(); - if (dos_header_ == &::google::protobuf::internal::kEmptyString) { - dos_header_ = new ::std::string; - } - return dos_header_; -} -inline ::std::string* ClientDownloadRequest_PEImageHeaders::release_dos_header() { - clear_has_dos_header(); - if (dos_header_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = dos_header_; - dos_header_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// optional bytes file_header = 2; -inline bool ClientDownloadRequest_PEImageHeaders::has_file_header() const { - return (_has_bits_[0] & 0x00000002u) != 0; -} -inline void ClientDownloadRequest_PEImageHeaders::set_has_file_header() { - _has_bits_[0] |= 0x00000002u; -} -inline void ClientDownloadRequest_PEImageHeaders::clear_has_file_header() { - _has_bits_[0] &= ~0x00000002u; -} -inline void ClientDownloadRequest_PEImageHeaders::clear_file_header() { - if (file_header_ != &::google::protobuf::internal::kEmptyString) { - file_header_->clear(); - } - clear_has_file_header(); -} -inline const ::std::string& ClientDownloadRequest_PEImageHeaders::file_header() const { - return *file_header_; -} -inline void ClientDownloadRequest_PEImageHeaders::set_file_header(const ::std::string& value) { - set_has_file_header(); - if (file_header_ == &::google::protobuf::internal::kEmptyString) { - file_header_ = new ::std::string; - } - file_header_->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders::set_file_header(const char* value) { - set_has_file_header(); - if (file_header_ == &::google::protobuf::internal::kEmptyString) { - file_header_ = new ::std::string; - } - file_header_->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders::set_file_header(const void* value, size_t size) { - set_has_file_header(); - if (file_header_ == &::google::protobuf::internal::kEmptyString) { - file_header_ = new ::std::string; - } - file_header_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest_PEImageHeaders::mutable_file_header() { - set_has_file_header(); - if (file_header_ == &::google::protobuf::internal::kEmptyString) { - file_header_ = new ::std::string; - } - return file_header_; -} -inline ::std::string* ClientDownloadRequest_PEImageHeaders::release_file_header() { - clear_has_file_header(); - if (file_header_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = file_header_; - file_header_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// optional bytes optional_headers32 = 3; -inline bool ClientDownloadRequest_PEImageHeaders::has_optional_headers32() const { - return (_has_bits_[0] & 0x00000004u) != 0; -} -inline void ClientDownloadRequest_PEImageHeaders::set_has_optional_headers32() { - _has_bits_[0] |= 0x00000004u; -} -inline void ClientDownloadRequest_PEImageHeaders::clear_has_optional_headers32() { - _has_bits_[0] &= ~0x00000004u; -} -inline void ClientDownloadRequest_PEImageHeaders::clear_optional_headers32() { - if (optional_headers32_ != &::google::protobuf::internal::kEmptyString) { - optional_headers32_->clear(); - } - clear_has_optional_headers32(); -} -inline const ::std::string& ClientDownloadRequest_PEImageHeaders::optional_headers32() const { - return *optional_headers32_; -} -inline void ClientDownloadRequest_PEImageHeaders::set_optional_headers32(const ::std::string& value) { - set_has_optional_headers32(); - if (optional_headers32_ == &::google::protobuf::internal::kEmptyString) { - optional_headers32_ = new ::std::string; - } - optional_headers32_->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders::set_optional_headers32(const char* value) { - set_has_optional_headers32(); - if (optional_headers32_ == &::google::protobuf::internal::kEmptyString) { - optional_headers32_ = new ::std::string; - } - optional_headers32_->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders::set_optional_headers32(const void* value, size_t size) { - set_has_optional_headers32(); - if (optional_headers32_ == &::google::protobuf::internal::kEmptyString) { - optional_headers32_ = new ::std::string; - } - optional_headers32_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest_PEImageHeaders::mutable_optional_headers32() { - set_has_optional_headers32(); - if (optional_headers32_ == &::google::protobuf::internal::kEmptyString) { - optional_headers32_ = new ::std::string; - } - return optional_headers32_; -} -inline ::std::string* ClientDownloadRequest_PEImageHeaders::release_optional_headers32() { - clear_has_optional_headers32(); - if (optional_headers32_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = optional_headers32_; - optional_headers32_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// optional bytes optional_headers64 = 4; -inline bool ClientDownloadRequest_PEImageHeaders::has_optional_headers64() const { - return (_has_bits_[0] & 0x00000008u) != 0; -} -inline void ClientDownloadRequest_PEImageHeaders::set_has_optional_headers64() { - _has_bits_[0] |= 0x00000008u; -} -inline void ClientDownloadRequest_PEImageHeaders::clear_has_optional_headers64() { - _has_bits_[0] &= ~0x00000008u; -} -inline void ClientDownloadRequest_PEImageHeaders::clear_optional_headers64() { - if (optional_headers64_ != &::google::protobuf::internal::kEmptyString) { - optional_headers64_->clear(); - } - clear_has_optional_headers64(); -} -inline const ::std::string& ClientDownloadRequest_PEImageHeaders::optional_headers64() const { - return *optional_headers64_; -} -inline void ClientDownloadRequest_PEImageHeaders::set_optional_headers64(const ::std::string& value) { - set_has_optional_headers64(); - if (optional_headers64_ == &::google::protobuf::internal::kEmptyString) { - optional_headers64_ = new ::std::string; - } - optional_headers64_->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders::set_optional_headers64(const char* value) { - set_has_optional_headers64(); - if (optional_headers64_ == &::google::protobuf::internal::kEmptyString) { - optional_headers64_ = new ::std::string; - } - optional_headers64_->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders::set_optional_headers64(const void* value, size_t size) { - set_has_optional_headers64(); - if (optional_headers64_ == &::google::protobuf::internal::kEmptyString) { - optional_headers64_ = new ::std::string; - } - optional_headers64_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest_PEImageHeaders::mutable_optional_headers64() { - set_has_optional_headers64(); - if (optional_headers64_ == &::google::protobuf::internal::kEmptyString) { - optional_headers64_ = new ::std::string; - } - return optional_headers64_; -} -inline ::std::string* ClientDownloadRequest_PEImageHeaders::release_optional_headers64() { - clear_has_optional_headers64(); - if (optional_headers64_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = optional_headers64_; - optional_headers64_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// repeated bytes section_header = 5; -inline int ClientDownloadRequest_PEImageHeaders::section_header_size() const { - return section_header_.size(); -} -inline void ClientDownloadRequest_PEImageHeaders::clear_section_header() { - section_header_.Clear(); -} -inline const ::std::string& ClientDownloadRequest_PEImageHeaders::section_header(int index) const { - return section_header_.Get(index); -} -inline ::std::string* ClientDownloadRequest_PEImageHeaders::mutable_section_header(int index) { - return section_header_.Mutable(index); -} -inline void ClientDownloadRequest_PEImageHeaders::set_section_header(int index, const ::std::string& value) { - section_header_.Mutable(index)->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders::set_section_header(int index, const char* value) { - section_header_.Mutable(index)->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders::set_section_header(int index, const void* value, size_t size) { - section_header_.Mutable(index)->assign( - reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest_PEImageHeaders::add_section_header() { - return section_header_.Add(); -} -inline void ClientDownloadRequest_PEImageHeaders::add_section_header(const ::std::string& value) { - section_header_.Add()->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders::add_section_header(const char* value) { - section_header_.Add()->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders::add_section_header(const void* value, size_t size) { - section_header_.Add()->assign(reinterpret_cast(value), size); -} -inline const ::google::protobuf::RepeatedPtrField< ::std::string>& -ClientDownloadRequest_PEImageHeaders::section_header() const { - return section_header_; -} -inline ::google::protobuf::RepeatedPtrField< ::std::string>* -ClientDownloadRequest_PEImageHeaders::mutable_section_header() { - return §ion_header_; -} - -// optional bytes export_section_data = 6; -inline bool ClientDownloadRequest_PEImageHeaders::has_export_section_data() const { - return (_has_bits_[0] & 0x00000020u) != 0; -} -inline void ClientDownloadRequest_PEImageHeaders::set_has_export_section_data() { - _has_bits_[0] |= 0x00000020u; -} -inline void ClientDownloadRequest_PEImageHeaders::clear_has_export_section_data() { - _has_bits_[0] &= ~0x00000020u; -} -inline void ClientDownloadRequest_PEImageHeaders::clear_export_section_data() { - if (export_section_data_ != &::google::protobuf::internal::kEmptyString) { - export_section_data_->clear(); - } - clear_has_export_section_data(); -} -inline const ::std::string& ClientDownloadRequest_PEImageHeaders::export_section_data() const { - return *export_section_data_; -} -inline void ClientDownloadRequest_PEImageHeaders::set_export_section_data(const ::std::string& value) { - set_has_export_section_data(); - if (export_section_data_ == &::google::protobuf::internal::kEmptyString) { - export_section_data_ = new ::std::string; - } - export_section_data_->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders::set_export_section_data(const char* value) { - set_has_export_section_data(); - if (export_section_data_ == &::google::protobuf::internal::kEmptyString) { - export_section_data_ = new ::std::string; - } - export_section_data_->assign(value); -} -inline void ClientDownloadRequest_PEImageHeaders::set_export_section_data(const void* value, size_t size) { - set_has_export_section_data(); - if (export_section_data_ == &::google::protobuf::internal::kEmptyString) { - export_section_data_ = new ::std::string; - } - export_section_data_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest_PEImageHeaders::mutable_export_section_data() { - set_has_export_section_data(); - if (export_section_data_ == &::google::protobuf::internal::kEmptyString) { - export_section_data_ = new ::std::string; - } - return export_section_data_; -} -inline ::std::string* ClientDownloadRequest_PEImageHeaders::release_export_section_data() { - clear_has_export_section_data(); - if (export_section_data_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = export_section_data_; - export_section_data_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// repeated .safe_browsing.ClientDownloadRequest.PEImageHeaders.DebugData debug_data = 7; -inline int ClientDownloadRequest_PEImageHeaders::debug_data_size() const { - return debug_data_.size(); -} -inline void ClientDownloadRequest_PEImageHeaders::clear_debug_data() { - debug_data_.Clear(); -} -inline const ::safe_browsing::ClientDownloadRequest_PEImageHeaders_DebugData& ClientDownloadRequest_PEImageHeaders::debug_data(int index) const { - return debug_data_.Get(index); -} -inline ::safe_browsing::ClientDownloadRequest_PEImageHeaders_DebugData* ClientDownloadRequest_PEImageHeaders::mutable_debug_data(int index) { - return debug_data_.Mutable(index); -} -inline ::safe_browsing::ClientDownloadRequest_PEImageHeaders_DebugData* ClientDownloadRequest_PEImageHeaders::add_debug_data() { - return debug_data_.Add(); -} -inline const ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_PEImageHeaders_DebugData >& -ClientDownloadRequest_PEImageHeaders::debug_data() const { - return debug_data_; -} -inline ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_PEImageHeaders_DebugData >* -ClientDownloadRequest_PEImageHeaders::mutable_debug_data() { - return &debug_data_; -} - -// ------------------------------------------------------------------- - -// ClientDownloadRequest_ImageHeaders - -// optional .safe_browsing.ClientDownloadRequest.PEImageHeaders pe_headers = 1; -inline bool ClientDownloadRequest_ImageHeaders::has_pe_headers() const { - return (_has_bits_[0] & 0x00000001u) != 0; -} -inline void ClientDownloadRequest_ImageHeaders::set_has_pe_headers() { - _has_bits_[0] |= 0x00000001u; -} -inline void ClientDownloadRequest_ImageHeaders::clear_has_pe_headers() { - _has_bits_[0] &= ~0x00000001u; -} -inline void ClientDownloadRequest_ImageHeaders::clear_pe_headers() { - if (pe_headers_ != NULL) pe_headers_->::safe_browsing::ClientDownloadRequest_PEImageHeaders::Clear(); - clear_has_pe_headers(); -} -inline const ::safe_browsing::ClientDownloadRequest_PEImageHeaders& ClientDownloadRequest_ImageHeaders::pe_headers() const { - return pe_headers_ != NULL ? *pe_headers_ : *default_instance_->pe_headers_; -} -inline ::safe_browsing::ClientDownloadRequest_PEImageHeaders* ClientDownloadRequest_ImageHeaders::mutable_pe_headers() { - set_has_pe_headers(); - if (pe_headers_ == NULL) pe_headers_ = new ::safe_browsing::ClientDownloadRequest_PEImageHeaders; - return pe_headers_; -} -inline ::safe_browsing::ClientDownloadRequest_PEImageHeaders* ClientDownloadRequest_ImageHeaders::release_pe_headers() { - clear_has_pe_headers(); - ::safe_browsing::ClientDownloadRequest_PEImageHeaders* temp = pe_headers_; - pe_headers_ = NULL; - return temp; -} - -// ------------------------------------------------------------------- - -// ClientDownloadRequest - -// required string url = 1; -inline bool ClientDownloadRequest::has_url() const { - return (_has_bits_[0] & 0x00000001u) != 0; -} -inline void ClientDownloadRequest::set_has_url() { - _has_bits_[0] |= 0x00000001u; -} -inline void ClientDownloadRequest::clear_has_url() { - _has_bits_[0] &= ~0x00000001u; -} -inline void ClientDownloadRequest::clear_url() { - if (url_ != &::google::protobuf::internal::kEmptyString) { - url_->clear(); - } - clear_has_url(); -} -inline const ::std::string& ClientDownloadRequest::url() const { - return *url_; -} -inline void ClientDownloadRequest::set_url(const ::std::string& value) { - set_has_url(); - if (url_ == &::google::protobuf::internal::kEmptyString) { - url_ = new ::std::string; - } - url_->assign(value); -} -inline void ClientDownloadRequest::set_url(const char* value) { - set_has_url(); - if (url_ == &::google::protobuf::internal::kEmptyString) { - url_ = new ::std::string; - } - url_->assign(value); -} -inline void ClientDownloadRequest::set_url(const char* value, size_t size) { - set_has_url(); - if (url_ == &::google::protobuf::internal::kEmptyString) { - url_ = new ::std::string; - } - url_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest::mutable_url() { - set_has_url(); - if (url_ == &::google::protobuf::internal::kEmptyString) { - url_ = new ::std::string; - } - return url_; -} -inline ::std::string* ClientDownloadRequest::release_url() { - clear_has_url(); - if (url_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = url_; - url_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// required .safe_browsing.ClientDownloadRequest.Digests digests = 2; -inline bool ClientDownloadRequest::has_digests() const { - return (_has_bits_[0] & 0x00000002u) != 0; -} -inline void ClientDownloadRequest::set_has_digests() { - _has_bits_[0] |= 0x00000002u; -} -inline void ClientDownloadRequest::clear_has_digests() { - _has_bits_[0] &= ~0x00000002u; -} -inline void ClientDownloadRequest::clear_digests() { - if (digests_ != NULL) digests_->::safe_browsing::ClientDownloadRequest_Digests::Clear(); - clear_has_digests(); -} -inline const ::safe_browsing::ClientDownloadRequest_Digests& ClientDownloadRequest::digests() const { - return digests_ != NULL ? *digests_ : *default_instance_->digests_; -} -inline ::safe_browsing::ClientDownloadRequest_Digests* ClientDownloadRequest::mutable_digests() { - set_has_digests(); - if (digests_ == NULL) digests_ = new ::safe_browsing::ClientDownloadRequest_Digests; - return digests_; -} -inline ::safe_browsing::ClientDownloadRequest_Digests* ClientDownloadRequest::release_digests() { - clear_has_digests(); - ::safe_browsing::ClientDownloadRequest_Digests* temp = digests_; - digests_ = NULL; - return temp; -} - -// required int64 length = 3; -inline bool ClientDownloadRequest::has_length() const { - return (_has_bits_[0] & 0x00000004u) != 0; -} -inline void ClientDownloadRequest::set_has_length() { - _has_bits_[0] |= 0x00000004u; -} -inline void ClientDownloadRequest::clear_has_length() { - _has_bits_[0] &= ~0x00000004u; -} -inline void ClientDownloadRequest::clear_length() { - length_ = GOOGLE_LONGLONG(0); - clear_has_length(); -} -inline ::google::protobuf::int64 ClientDownloadRequest::length() const { - return length_; -} -inline void ClientDownloadRequest::set_length(::google::protobuf::int64 value) { - set_has_length(); - length_ = value; -} - -// repeated .safe_browsing.ClientDownloadRequest.Resource resources = 4; -inline int ClientDownloadRequest::resources_size() const { - return resources_.size(); -} -inline void ClientDownloadRequest::clear_resources() { - resources_.Clear(); -} -inline const ::safe_browsing::ClientDownloadRequest_Resource& ClientDownloadRequest::resources(int index) const { - return resources_.Get(index); -} -inline ::safe_browsing::ClientDownloadRequest_Resource* ClientDownloadRequest::mutable_resources(int index) { - return resources_.Mutable(index); -} -inline ::safe_browsing::ClientDownloadRequest_Resource* ClientDownloadRequest::add_resources() { - return resources_.Add(); -} -inline const ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_Resource >& -ClientDownloadRequest::resources() const { - return resources_; -} -inline ::google::protobuf::RepeatedPtrField< ::safe_browsing::ClientDownloadRequest_Resource >* -ClientDownloadRequest::mutable_resources() { - return &resources_; -} - -// optional .safe_browsing.ClientDownloadRequest.SignatureInfo signature = 5; -inline bool ClientDownloadRequest::has_signature() const { - return (_has_bits_[0] & 0x00000010u) != 0; -} -inline void ClientDownloadRequest::set_has_signature() { - _has_bits_[0] |= 0x00000010u; -} -inline void ClientDownloadRequest::clear_has_signature() { - _has_bits_[0] &= ~0x00000010u; -} -inline void ClientDownloadRequest::clear_signature() { - if (signature_ != NULL) signature_->::safe_browsing::ClientDownloadRequest_SignatureInfo::Clear(); - clear_has_signature(); -} -inline const ::safe_browsing::ClientDownloadRequest_SignatureInfo& ClientDownloadRequest::signature() const { - return signature_ != NULL ? *signature_ : *default_instance_->signature_; -} -inline ::safe_browsing::ClientDownloadRequest_SignatureInfo* ClientDownloadRequest::mutable_signature() { - set_has_signature(); - if (signature_ == NULL) signature_ = new ::safe_browsing::ClientDownloadRequest_SignatureInfo; - return signature_; -} -inline ::safe_browsing::ClientDownloadRequest_SignatureInfo* ClientDownloadRequest::release_signature() { - clear_has_signature(); - ::safe_browsing::ClientDownloadRequest_SignatureInfo* temp = signature_; - signature_ = NULL; - return temp; -} - -// optional bool user_initiated = 6; -inline bool ClientDownloadRequest::has_user_initiated() const { - return (_has_bits_[0] & 0x00000020u) != 0; -} -inline void ClientDownloadRequest::set_has_user_initiated() { - _has_bits_[0] |= 0x00000020u; -} -inline void ClientDownloadRequest::clear_has_user_initiated() { - _has_bits_[0] &= ~0x00000020u; -} -inline void ClientDownloadRequest::clear_user_initiated() { - user_initiated_ = false; - clear_has_user_initiated(); -} -inline bool ClientDownloadRequest::user_initiated() const { - return user_initiated_; -} -inline void ClientDownloadRequest::set_user_initiated(bool value) { - set_has_user_initiated(); - user_initiated_ = value; -} - -// optional string file_basename = 9; -inline bool ClientDownloadRequest::has_file_basename() const { - return (_has_bits_[0] & 0x00000040u) != 0; -} -inline void ClientDownloadRequest::set_has_file_basename() { - _has_bits_[0] |= 0x00000040u; -} -inline void ClientDownloadRequest::clear_has_file_basename() { - _has_bits_[0] &= ~0x00000040u; -} -inline void ClientDownloadRequest::clear_file_basename() { - if (file_basename_ != &::google::protobuf::internal::kEmptyString) { - file_basename_->clear(); - } - clear_has_file_basename(); -} -inline const ::std::string& ClientDownloadRequest::file_basename() const { - return *file_basename_; -} -inline void ClientDownloadRequest::set_file_basename(const ::std::string& value) { - set_has_file_basename(); - if (file_basename_ == &::google::protobuf::internal::kEmptyString) { - file_basename_ = new ::std::string; - } - file_basename_->assign(value); -} -inline void ClientDownloadRequest::set_file_basename(const char* value) { - set_has_file_basename(); - if (file_basename_ == &::google::protobuf::internal::kEmptyString) { - file_basename_ = new ::std::string; - } - file_basename_->assign(value); -} -inline void ClientDownloadRequest::set_file_basename(const char* value, size_t size) { - set_has_file_basename(); - if (file_basename_ == &::google::protobuf::internal::kEmptyString) { - file_basename_ = new ::std::string; - } - file_basename_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest::mutable_file_basename() { - set_has_file_basename(); - if (file_basename_ == &::google::protobuf::internal::kEmptyString) { - file_basename_ = new ::std::string; - } - return file_basename_; -} -inline ::std::string* ClientDownloadRequest::release_file_basename() { - clear_has_file_basename(); - if (file_basename_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = file_basename_; - file_basename_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// optional .safe_browsing.ClientDownloadRequest.DownloadType download_type = 10 [default = WIN_EXECUTABLE]; -inline bool ClientDownloadRequest::has_download_type() const { - return (_has_bits_[0] & 0x00000080u) != 0; -} -inline void ClientDownloadRequest::set_has_download_type() { - _has_bits_[0] |= 0x00000080u; -} -inline void ClientDownloadRequest::clear_has_download_type() { - _has_bits_[0] &= ~0x00000080u; -} -inline void ClientDownloadRequest::clear_download_type() { - download_type_ = 0; - clear_has_download_type(); -} -inline ::safe_browsing::ClientDownloadRequest_DownloadType ClientDownloadRequest::download_type() const { - return static_cast< ::safe_browsing::ClientDownloadRequest_DownloadType >(download_type_); -} -inline void ClientDownloadRequest::set_download_type(::safe_browsing::ClientDownloadRequest_DownloadType value) { - GOOGLE_DCHECK(::safe_browsing::ClientDownloadRequest_DownloadType_IsValid(value)); - set_has_download_type(); - download_type_ = value; -} - -// optional string locale = 11; -inline bool ClientDownloadRequest::has_locale() const { - return (_has_bits_[0] & 0x00000100u) != 0; -} -inline void ClientDownloadRequest::set_has_locale() { - _has_bits_[0] |= 0x00000100u; -} -inline void ClientDownloadRequest::clear_has_locale() { - _has_bits_[0] &= ~0x00000100u; -} -inline void ClientDownloadRequest::clear_locale() { - if (locale_ != &::google::protobuf::internal::kEmptyString) { - locale_->clear(); - } - clear_has_locale(); -} -inline const ::std::string& ClientDownloadRequest::locale() const { - return *locale_; -} -inline void ClientDownloadRequest::set_locale(const ::std::string& value) { - set_has_locale(); - if (locale_ == &::google::protobuf::internal::kEmptyString) { - locale_ = new ::std::string; - } - locale_->assign(value); -} -inline void ClientDownloadRequest::set_locale(const char* value) { - set_has_locale(); - if (locale_ == &::google::protobuf::internal::kEmptyString) { - locale_ = new ::std::string; - } - locale_->assign(value); -} -inline void ClientDownloadRequest::set_locale(const char* value, size_t size) { - set_has_locale(); - if (locale_ == &::google::protobuf::internal::kEmptyString) { - locale_ = new ::std::string; - } - locale_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadRequest::mutable_locale() { - set_has_locale(); - if (locale_ == &::google::protobuf::internal::kEmptyString) { - locale_ = new ::std::string; - } - return locale_; -} -inline ::std::string* ClientDownloadRequest::release_locale() { - clear_has_locale(); - if (locale_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = locale_; - locale_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// optional .safe_browsing.ClientDownloadRequest.ImageHeaders image_headers = 18; -inline bool ClientDownloadRequest::has_image_headers() const { - return (_has_bits_[0] & 0x00000200u) != 0; -} -inline void ClientDownloadRequest::set_has_image_headers() { - _has_bits_[0] |= 0x00000200u; -} -inline void ClientDownloadRequest::clear_has_image_headers() { - _has_bits_[0] &= ~0x00000200u; -} -inline void ClientDownloadRequest::clear_image_headers() { - if (image_headers_ != NULL) image_headers_->::safe_browsing::ClientDownloadRequest_ImageHeaders::Clear(); - clear_has_image_headers(); -} -inline const ::safe_browsing::ClientDownloadRequest_ImageHeaders& ClientDownloadRequest::image_headers() const { - return image_headers_ != NULL ? *image_headers_ : *default_instance_->image_headers_; -} -inline ::safe_browsing::ClientDownloadRequest_ImageHeaders* ClientDownloadRequest::mutable_image_headers() { - set_has_image_headers(); - if (image_headers_ == NULL) image_headers_ = new ::safe_browsing::ClientDownloadRequest_ImageHeaders; - return image_headers_; -} -inline ::safe_browsing::ClientDownloadRequest_ImageHeaders* ClientDownloadRequest::release_image_headers() { - clear_has_image_headers(); - ::safe_browsing::ClientDownloadRequest_ImageHeaders* temp = image_headers_; - image_headers_ = NULL; - return temp; -} - -// ------------------------------------------------------------------- - -// ClientDownloadResponse_MoreInfo - -// optional string description = 1; -inline bool ClientDownloadResponse_MoreInfo::has_description() const { - return (_has_bits_[0] & 0x00000001u) != 0; -} -inline void ClientDownloadResponse_MoreInfo::set_has_description() { - _has_bits_[0] |= 0x00000001u; -} -inline void ClientDownloadResponse_MoreInfo::clear_has_description() { - _has_bits_[0] &= ~0x00000001u; -} -inline void ClientDownloadResponse_MoreInfo::clear_description() { - if (description_ != &::google::protobuf::internal::kEmptyString) { - description_->clear(); - } - clear_has_description(); -} -inline const ::std::string& ClientDownloadResponse_MoreInfo::description() const { - return *description_; -} -inline void ClientDownloadResponse_MoreInfo::set_description(const ::std::string& value) { - set_has_description(); - if (description_ == &::google::protobuf::internal::kEmptyString) { - description_ = new ::std::string; - } - description_->assign(value); -} -inline void ClientDownloadResponse_MoreInfo::set_description(const char* value) { - set_has_description(); - if (description_ == &::google::protobuf::internal::kEmptyString) { - description_ = new ::std::string; - } - description_->assign(value); -} -inline void ClientDownloadResponse_MoreInfo::set_description(const char* value, size_t size) { - set_has_description(); - if (description_ == &::google::protobuf::internal::kEmptyString) { - description_ = new ::std::string; - } - description_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadResponse_MoreInfo::mutable_description() { - set_has_description(); - if (description_ == &::google::protobuf::internal::kEmptyString) { - description_ = new ::std::string; - } - return description_; -} -inline ::std::string* ClientDownloadResponse_MoreInfo::release_description() { - clear_has_description(); - if (description_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = description_; - description_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// optional string url = 2; -inline bool ClientDownloadResponse_MoreInfo::has_url() const { - return (_has_bits_[0] & 0x00000002u) != 0; -} -inline void ClientDownloadResponse_MoreInfo::set_has_url() { - _has_bits_[0] |= 0x00000002u; -} -inline void ClientDownloadResponse_MoreInfo::clear_has_url() { - _has_bits_[0] &= ~0x00000002u; -} -inline void ClientDownloadResponse_MoreInfo::clear_url() { - if (url_ != &::google::protobuf::internal::kEmptyString) { - url_->clear(); - } - clear_has_url(); -} -inline const ::std::string& ClientDownloadResponse_MoreInfo::url() const { - return *url_; -} -inline void ClientDownloadResponse_MoreInfo::set_url(const ::std::string& value) { - set_has_url(); - if (url_ == &::google::protobuf::internal::kEmptyString) { - url_ = new ::std::string; - } - url_->assign(value); -} -inline void ClientDownloadResponse_MoreInfo::set_url(const char* value) { - set_has_url(); - if (url_ == &::google::protobuf::internal::kEmptyString) { - url_ = new ::std::string; - } - url_->assign(value); -} -inline void ClientDownloadResponse_MoreInfo::set_url(const char* value, size_t size) { - set_has_url(); - if (url_ == &::google::protobuf::internal::kEmptyString) { - url_ = new ::std::string; - } - url_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadResponse_MoreInfo::mutable_url() { - set_has_url(); - if (url_ == &::google::protobuf::internal::kEmptyString) { - url_ = new ::std::string; - } - return url_; -} -inline ::std::string* ClientDownloadResponse_MoreInfo::release_url() { - clear_has_url(); - if (url_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = url_; - url_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - -// ------------------------------------------------------------------- - -// ClientDownloadResponse - -// required .safe_browsing.ClientDownloadResponse.Verdict verdict = 1; -inline bool ClientDownloadResponse::has_verdict() const { - return (_has_bits_[0] & 0x00000001u) != 0; -} -inline void ClientDownloadResponse::set_has_verdict() { - _has_bits_[0] |= 0x00000001u; -} -inline void ClientDownloadResponse::clear_has_verdict() { - _has_bits_[0] &= ~0x00000001u; -} -inline void ClientDownloadResponse::clear_verdict() { - verdict_ = 0; - clear_has_verdict(); -} -inline ::safe_browsing::ClientDownloadResponse_Verdict ClientDownloadResponse::verdict() const { - return static_cast< ::safe_browsing::ClientDownloadResponse_Verdict >(verdict_); -} -inline void ClientDownloadResponse::set_verdict(::safe_browsing::ClientDownloadResponse_Verdict value) { - GOOGLE_DCHECK(::safe_browsing::ClientDownloadResponse_Verdict_IsValid(value)); - set_has_verdict(); - verdict_ = value; -} - -// optional .safe_browsing.ClientDownloadResponse.MoreInfo more_info = 2; -inline bool ClientDownloadResponse::has_more_info() const { - return (_has_bits_[0] & 0x00000002u) != 0; -} -inline void ClientDownloadResponse::set_has_more_info() { - _has_bits_[0] |= 0x00000002u; -} -inline void ClientDownloadResponse::clear_has_more_info() { - _has_bits_[0] &= ~0x00000002u; -} -inline void ClientDownloadResponse::clear_more_info() { - if (more_info_ != NULL) more_info_->::safe_browsing::ClientDownloadResponse_MoreInfo::Clear(); - clear_has_more_info(); -} -inline const ::safe_browsing::ClientDownloadResponse_MoreInfo& ClientDownloadResponse::more_info() const { - return more_info_ != NULL ? *more_info_ : *default_instance_->more_info_; -} -inline ::safe_browsing::ClientDownloadResponse_MoreInfo* ClientDownloadResponse::mutable_more_info() { - set_has_more_info(); - if (more_info_ == NULL) more_info_ = new ::safe_browsing::ClientDownloadResponse_MoreInfo; - return more_info_; -} -inline ::safe_browsing::ClientDownloadResponse_MoreInfo* ClientDownloadResponse::release_more_info() { - clear_has_more_info(); - ::safe_browsing::ClientDownloadResponse_MoreInfo* temp = more_info_; - more_info_ = NULL; - return temp; -} - -// optional bytes token = 3; -inline bool ClientDownloadResponse::has_token() const { - return (_has_bits_[0] & 0x00000004u) != 0; -} -inline void ClientDownloadResponse::set_has_token() { - _has_bits_[0] |= 0x00000004u; -} -inline void ClientDownloadResponse::clear_has_token() { - _has_bits_[0] &= ~0x00000004u; -} -inline void ClientDownloadResponse::clear_token() { - if (token_ != &::google::protobuf::internal::kEmptyString) { - token_->clear(); - } - clear_has_token(); -} -inline const ::std::string& ClientDownloadResponse::token() const { - return *token_; -} -inline void ClientDownloadResponse::set_token(const ::std::string& value) { - set_has_token(); - if (token_ == &::google::protobuf::internal::kEmptyString) { - token_ = new ::std::string; - } - token_->assign(value); -} -inline void ClientDownloadResponse::set_token(const char* value) { - set_has_token(); - if (token_ == &::google::protobuf::internal::kEmptyString) { - token_ = new ::std::string; - } - token_->assign(value); -} -inline void ClientDownloadResponse::set_token(const void* value, size_t size) { - set_has_token(); - if (token_ == &::google::protobuf::internal::kEmptyString) { - token_ = new ::std::string; - } - token_->assign(reinterpret_cast(value), size); -} -inline ::std::string* ClientDownloadResponse::mutable_token() { - set_has_token(); - if (token_ == &::google::protobuf::internal::kEmptyString) { - token_ = new ::std::string; - } - return token_; -} -inline ::std::string* ClientDownloadResponse::release_token() { - clear_has_token(); - if (token_ == &::google::protobuf::internal::kEmptyString) { - return NULL; - } else { - ::std::string* temp = token_; - token_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); - return temp; - } -} - - -// @@protoc_insertion_point(namespace_scope) - -} // namespace safe_browsing - -// @@protoc_insertion_point(global_scope) - -#endif // PROTOBUF_csd_2eproto__INCLUDED diff --git a/toolkit/components/downloads/generate_csd.sh b/toolkit/components/downloads/generate_csd.sh deleted file mode 100755 index a0eba711ae..0000000000 --- a/toolkit/components/downloads/generate_csd.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/bash -# A script to generate toolkit/components/downloads/csd.pb.{cc,h} for use in -# nsIApplicationReputationQuery. This script assumes you have downloaded and -# installed the protocol buffer compiler. -# As of June 26 2014, csd.proto contains many protobufs that are currently -# unused by ApplicationReputation. You may want to strip csd.proto of these -# before running the protocol compiler on it. -if [ -n $PROTOC_PATH ]; then - PROTOC_PATH=/usr/local/bin/protoc -fi - -echo "Using $PROTOC_PATH as protocol compiler" - -if [ ! -e $PROTOC_PATH ]; then - echo "You must install the protocol compiler from " \ - "https://code.google.com/p/protobuf/downloads/list" - exit 1 -fi - -# Get the protocol buffer and compile it -CMD='wget http://src.chromium.org/chrome/trunk/src/chrome/common/safe_browsing/csd.proto -O csd.proto' -OUTPUT_PATH=toolkit/components/downloads - -$CMD -$PROTOC_PATH csd.proto --cpp_out=$OUTPUT_PATH diff --git a/toolkit/components/downloads/moz.build b/toolkit/components/downloads/moz.build index 5f8ccbee71..dd522aa18a 100644 --- a/toolkit/components/downloads/moz.build +++ b/toolkit/components/downloads/moz.build @@ -30,12 +30,6 @@ SOURCES += [ 'SQLFunctions.cpp', ] -if CONFIG['MOZ_URL_CLASSIFIER']: - UNIFIED_SOURCES += [ - 'ApplicationReputation.cpp', - 'csd.pb.cc' - ] - if CONFIG['OS_ARCH'] == 'WINNT': # Can't build unified because we need CreateEvent which some IPC code # included in LoadContext ends up undefining. diff --git a/toolkit/components/jsdownloads/src/DownloadIntegration.jsm b/toolkit/components/jsdownloads/src/DownloadIntegration.jsm index e534c47de7..ada168ebb7 100644 --- a/toolkit/components/jsdownloads/src/DownloadIntegration.jsm +++ b/toolkit/components/jsdownloads/src/DownloadIntegration.jsm @@ -136,11 +136,7 @@ this.DownloadIntegration = { dontLoadObservers: false, dontCheckParentalControls: false, shouldBlockInTest: false, -#ifdef MOZ_URL_CLASSIFIER - dontCheckApplicationReputation: false, -#else dontCheckApplicationReputation: true, -#endif shouldBlockInTestForApplicationReputation: false, shouldKeepBlockedDataInTest: false, dontOpenFileAndFolder: false, diff --git a/toolkit/components/moz.build b/toolkit/components/moz.build index 8307504901..29fd63e97a 100644 --- a/toolkit/components/moz.build +++ b/toolkit/components/moz.build @@ -78,9 +78,6 @@ if CONFIG['MOZ_XUL']: if CONFIG['MOZ_TOOLKIT_SEARCH']: DIRS += ['search'] -if CONFIG['MOZ_URL_CLASSIFIER']: - DIRS += ['url-classifier'] - if CONFIG['MOZ_CAPTIVEDETECT']: DIRS += ['captivedetect'] diff --git a/toolkit/components/url-classifier/ChunkSet.cpp b/toolkit/components/url-classifier/ChunkSet.cpp deleted file mode 100644 index 5d48c95ea2..0000000000 --- a/toolkit/components/url-classifier/ChunkSet.cpp +++ /dev/null @@ -1,102 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#include "ChunkSet.h" - -namespace mozilla { -namespace safebrowsing { - -nsresult -ChunkSet::Serialize(nsACString& aChunkStr) -{ - aChunkStr.Truncate(); - - uint32_t i = 0; - while (i < mChunks.Length()) { - if (i != 0) { - aChunkStr.Append(','); - } - aChunkStr.AppendInt((int32_t)mChunks[i]); - - uint32_t first = i; - uint32_t last = first; - i++; - while (i < mChunks.Length() && (mChunks[i] == mChunks[i - 1] + 1 || mChunks[i] == mChunks[i - 1])) { - last = i++; - } - - if (last != first) { - aChunkStr.Append('-'); - aChunkStr.AppendInt((int32_t)mChunks[last]); - } - } - - return NS_OK; -} - -nsresult -ChunkSet::Set(uint32_t aChunk) -{ - size_t idx = mChunks.BinaryIndexOf(aChunk); - if (idx == nsTArray::NoIndex) { - mChunks.InsertElementSorted(aChunk); - } - return NS_OK; -} - -nsresult -ChunkSet::Unset(uint32_t aChunk) -{ - mChunks.RemoveElementSorted(aChunk); - - return NS_OK; -} - -bool -ChunkSet::Has(uint32_t aChunk) const -{ - return mChunks.BinaryIndexOf(aChunk) != nsTArray::NoIndex; -} - -nsresult -ChunkSet::Merge(const ChunkSet& aOther) -{ - const uint32_t *dupIter = aOther.mChunks.Elements(); - const uint32_t *end = aOther.mChunks.Elements() + aOther.mChunks.Length(); - - for (const uint32_t *iter = dupIter; iter != end; iter++) { - nsresult rv = Set(*iter); - NS_ENSURE_SUCCESS(rv, rv); - } - - return NS_OK; -} - -nsresult -ChunkSet::Remove(const ChunkSet& aOther) -{ - uint32_t *addIter = mChunks.Elements(); - uint32_t *end = mChunks.Elements() + mChunks.Length(); - - for (uint32_t *iter = addIter; iter != end; iter++) { - if (!aOther.Has(*iter)) { - *addIter = *iter; - addIter++; - } - } - - mChunks.SetLength(addIter - mChunks.Elements()); - - return NS_OK; -} - -void -ChunkSet::Clear() -{ - mChunks.Clear(); -} - -} -} diff --git a/toolkit/components/url-classifier/ChunkSet.h b/toolkit/components/url-classifier/ChunkSet.h deleted file mode 100644 index a39574c320..0000000000 --- a/toolkit/components/url-classifier/ChunkSet.h +++ /dev/null @@ -1,57 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#ifndef ChunkSet_h__ -#define ChunkSet_h__ - - -#include "Entries.h" -#include "nsString.h" -#include "nsTArray.h" - -namespace mozilla { -namespace safebrowsing { - -/** - * Store the chunk numbers as an array of uint32_t. We need chunk numbers in - * order to ask for incremental updates from the server. - * XXX: We should optimize this further to compress the many consecutive - * numbers. - */ -class ChunkSet { -public: - ChunkSet() {} - ~ChunkSet() {} - - nsresult Serialize(nsACString& aStr); - nsresult Set(uint32_t aChunk); - nsresult Unset(uint32_t aChunk); - void Clear(); - nsresult Merge(const ChunkSet& aOther); - nsresult Remove(const ChunkSet& aOther); - - bool Has(uint32_t chunk) const; - - uint32_t Length() const { return mChunks.Length(); } - - nsresult Write(nsIOutputStream* aOut) { - return WriteTArray(aOut, mChunks); - } - - nsresult Read(nsIInputStream* aIn, uint32_t aNumElements) { - return ReadTArray(aIn, &mChunks, aNumElements); - } - - uint32_t *Begin() { return mChunks.Elements(); } - uint32_t *End() { return mChunks.Elements() + mChunks.Length(); } - -private: - nsTArray mChunks; -}; - -} -} - -#endif diff --git a/toolkit/components/url-classifier/Classifier.cpp b/toolkit/components/url-classifier/Classifier.cpp deleted file mode 100644 index d258e3a5ee..0000000000 --- a/toolkit/components/url-classifier/Classifier.cpp +++ /dev/null @@ -1,755 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#include "Classifier.h" -#include "nsIPrefBranch.h" -#include "nsIPrefService.h" -#include "nsISimpleEnumerator.h" -#include "nsIRandomGenerator.h" -#include "nsIInputStream.h" -#include "nsISeekableStream.h" -#include "nsIFile.h" -#include "nsThreadUtils.h" -#include "mozilla/Telemetry.h" -#include "prlog.h" - -// NSPR_LOG_MODULES=UrlClassifierDbService:5 -extern PRLogModuleInfo *gUrlClassifierDbServiceLog; -#if defined(PR_LOGGING) -#define LOG(args) PR_LOG(gUrlClassifierDbServiceLog, PR_LOG_DEBUG, args) -#define LOG_ENABLED() PR_LOG_TEST(gUrlClassifierDbServiceLog, 4) -#else -#define LOG(args) -#define LOG_ENABLED() (false) -#endif - -#define STORE_DIRECTORY NS_LITERAL_CSTRING("safebrowsing") -#define TO_DELETE_DIR_SUFFIX NS_LITERAL_CSTRING("-to_delete") -#define BACKUP_DIR_SUFFIX NS_LITERAL_CSTRING("-backup") - -namespace mozilla { -namespace safebrowsing { - -void -Classifier::SplitTables(const nsACString& str, nsTArray& tables) -{ - tables.Clear(); - - nsACString::const_iterator begin, iter, end; - str.BeginReading(begin); - str.EndReading(end); - while (begin != end) { - iter = begin; - FindCharInReadable(',', iter, end); - nsDependentCSubstring table = Substring(begin,iter); - if (!table.IsEmpty()) { - tables.AppendElement(Substring(begin, iter)); - } - begin = iter; - if (begin != end) { - begin++; - } - } -} - -Classifier::Classifier() -{ -} - -Classifier::~Classifier() -{ - Close(); -} - -nsresult -Classifier::SetupPathNames() -{ - // Get the root directory where to store all the databases. - nsresult rv = mCacheDirectory->Clone(getter_AddRefs(mStoreDirectory)); - NS_ENSURE_SUCCESS(rv, rv); - - rv = mStoreDirectory->AppendNative(STORE_DIRECTORY); - NS_ENSURE_SUCCESS(rv, rv); - - // Make sure LookupCaches (which are persistent and survive updates) - // are reading/writing in the right place. We will be moving their - // files "underneath" them during backup/restore. - for (uint32_t i = 0; i < mLookupCaches.Length(); i++) { - mLookupCaches[i]->UpdateDirHandle(mStoreDirectory); - } - - // Directory where to move a backup before an update. - rv = mCacheDirectory->Clone(getter_AddRefs(mBackupDirectory)); - NS_ENSURE_SUCCESS(rv, rv); - - rv = mBackupDirectory->AppendNative(STORE_DIRECTORY + BACKUP_DIR_SUFFIX); - NS_ENSURE_SUCCESS(rv, rv); - - // Directory where to move the backup so we can atomically - // delete (really move) it. - rv = mCacheDirectory->Clone(getter_AddRefs(mToDeleteDirectory)); - NS_ENSURE_SUCCESS(rv, rv); - - rv = mToDeleteDirectory->AppendNative(STORE_DIRECTORY + TO_DELETE_DIR_SUFFIX); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -nsresult -Classifier::CreateStoreDirectory() -{ - // Ensure the safebrowsing directory exists. - bool storeExists; - nsresult rv = mStoreDirectory->Exists(&storeExists); - NS_ENSURE_SUCCESS(rv, rv); - - if (!storeExists) { - rv = mStoreDirectory->Create(nsIFile::DIRECTORY_TYPE, 0755); - NS_ENSURE_SUCCESS(rv, rv); - } else { - bool storeIsDir; - rv = mStoreDirectory->IsDirectory(&storeIsDir); - NS_ENSURE_SUCCESS(rv, rv); - if (!storeIsDir) - return NS_ERROR_FILE_DESTINATION_NOT_DIR; - } - - return NS_OK; -} - -nsresult -Classifier::Open(nsIFile& aCacheDirectory) -{ - // Remember the Local profile directory. - nsresult rv = aCacheDirectory.Clone(getter_AddRefs(mCacheDirectory)); - NS_ENSURE_SUCCESS(rv, rv); - - // Create the handles to the update and backup directories. - rv = SetupPathNames(); - NS_ENSURE_SUCCESS(rv, rv); - - // Clean up any to-delete directories that haven't been deleted yet. - rv = CleanToDelete(); - NS_ENSURE_SUCCESS(rv, rv); - - // Check whether we have an incomplete update and recover from the - // backup if so. - rv = RecoverBackups(); - NS_ENSURE_SUCCESS(rv, rv); - - // Make sure the main store directory exists. - rv = CreateStoreDirectory(); - NS_ENSURE_SUCCESS(rv, rv); - - mCryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - // Build the list of know urlclassifier lists - // XXX: Disk IO potentially on the main thread during startup - RegenActiveTables(); - - return NS_OK; -} - -void -Classifier::Close() -{ - DropStores(); -} - -void -Classifier::Reset() -{ - DropStores(); - - mStoreDirectory->Remove(true); - mBackupDirectory->Remove(true); - mToDeleteDirectory->Remove(true); - - CreateStoreDirectory(); - - mTableFreshness.Clear(); - RegenActiveTables(); -} - -void -Classifier::TableRequest(nsACString& aResult) -{ - nsTArray tables; - ActiveTables(tables); - for (uint32_t i = 0; i < tables.Length(); i++) { - HashStore store(tables[i], mStoreDirectory); - - nsresult rv = store.Open(); - if (NS_FAILED(rv)) - continue; - - aResult.Append(store.TableName()); - aResult.Append(';'); - - ChunkSet &adds = store.AddChunks(); - ChunkSet &subs = store.SubChunks(); - - if (adds.Length() > 0) { - aResult.AppendLiteral("a:"); - nsAutoCString addList; - adds.Serialize(addList); - aResult.Append(addList); - } - - if (subs.Length() > 0) { - if (adds.Length() > 0) - aResult.Append(':'); - aResult.AppendLiteral("s:"); - nsAutoCString subList; - subs.Serialize(subList); - aResult.Append(subList); - } - - aResult.Append('\n'); - } -} - -nsresult -Classifier::Check(const nsACString& aSpec, - const nsACString& aTables, - uint32_t aFreshnessGuarantee, - LookupResultArray& aResults) -{ - Telemetry::AutoTimer timer; - - // Get the set of fragments based on the url. This is necessary because we - // only look up at most 5 URLs per aSpec, even if aSpec has more than 5 - // components. - nsTArray fragments; - nsresult rv = LookupCache::GetLookupFragments(aSpec, &fragments); - NS_ENSURE_SUCCESS(rv, rv); - - nsTArray activeTables; - SplitTables(aTables, activeTables); - - nsTArray cacheArray; - for (uint32_t i = 0; i < activeTables.Length(); i++) { - LOG(("Checking table %s", activeTables[i].get())); - LookupCache *cache = GetLookupCache(activeTables[i]); - if (cache) { - cacheArray.AppendElement(cache); - } else { - return NS_ERROR_FAILURE; - } - } - - // Now check each lookup fragment against the entries in the DB. - for (uint32_t i = 0; i < fragments.Length(); i++) { - Completion lookupHash; - lookupHash.FromPlaintext(fragments[i], mCryptoHash); - - // Get list of host keys to look up - Completion hostKey; - rv = LookupCache::GetKey(fragments[i], &hostKey, mCryptoHash); - if (NS_FAILED(rv)) { - // Local host on the network. - continue; - } - -#if DEBUG && defined(PR_LOGGING) - if (LOG_ENABLED()) { - nsAutoCString checking; - lookupHash.ToHexString(checking); - LOG(("Checking fragment %s, hash %s (%X)", fragments[i].get(), - checking.get(), lookupHash.ToUint32())); - } -#endif - for (uint32_t i = 0; i < cacheArray.Length(); i++) { - LookupCache *cache = cacheArray[i]; - bool has, complete; - rv = cache->Has(lookupHash, &has, &complete); - NS_ENSURE_SUCCESS(rv, rv); - if (has) { - LookupResult *result = aResults.AppendElement(); - if (!result) - return NS_ERROR_OUT_OF_MEMORY; - - int64_t age; - bool found = mTableFreshness.Get(cache->TableName(), &age); - if (!found) { - age = 24 * 60 * 60; // just a large number - } else { - int64_t now = (PR_Now() / PR_USEC_PER_SEC); - age = now - age; - } - - LOG(("Found a result in %s: %s (Age: %Lds)", - cache->TableName().get(), - complete ? "complete." : "Not complete.", - age)); - - result->hash.complete = lookupHash; - result->mComplete = complete; - result->mFresh = (age < aFreshnessGuarantee); - result->mTableName.Assign(cache->TableName()); - } - } - - } - - return NS_OK; -} - -nsresult -Classifier::ApplyUpdates(nsTArray* aUpdates) -{ - Telemetry::AutoTimer timer; - -#if defined(PR_LOGGING) - PRIntervalTime clockStart = 0; - if (LOG_ENABLED() || true) { - clockStart = PR_IntervalNow(); - } -#endif - - LOG(("Backup before update.")); - - nsresult rv = BackupTables(); - NS_ENSURE_SUCCESS(rv, rv); - - LOG(("Applying %d table updates.", aUpdates->Length())); - - for (uint32_t i = 0; i < aUpdates->Length(); i++) { - // Previous ApplyTableUpdates() may have consumed this update.. - if ((*aUpdates)[i]) { - // Run all updates for one table - nsCString updateTable(aUpdates->ElementAt(i)->TableName()); - rv = ApplyTableUpdates(aUpdates, updateTable); - if (NS_FAILED(rv)) { - if (rv != NS_ERROR_OUT_OF_MEMORY) { - Reset(); - } - return rv; - } - } - } - aUpdates->Clear(); - - rv = RegenActiveTables(); - NS_ENSURE_SUCCESS(rv, rv); - - LOG(("Cleaning up backups.")); - - // Move the backup directory away (signaling the transaction finished - // successfully). This is atomic. - rv = RemoveBackupTables(); - NS_ENSURE_SUCCESS(rv, rv); - - // Do the actual deletion of the backup files. - rv = CleanToDelete(); - NS_ENSURE_SUCCESS(rv, rv); - - LOG(("Done applying updates.")); - -#if defined(PR_LOGGING) - if (LOG_ENABLED() || true) { - PRIntervalTime clockEnd = PR_IntervalNow(); - LOG(("update took %dms\n", - PR_IntervalToMilliseconds(clockEnd - clockStart))); - } -#endif - - return NS_OK; -} - -nsresult -Classifier::MarkSpoiled(nsTArray& aTables) -{ - for (uint32_t i = 0; i < aTables.Length(); i++) { - LOG(("Spoiling table: %s", aTables[i].get())); - // Spoil this table by marking it as no known freshness - mTableFreshness.Remove(aTables[i]); - // Remove any cached Completes for this table - LookupCache *cache = GetLookupCache(aTables[i]); - if (cache) { - cache->ClearCompleteCache(); - } - } - return NS_OK; -} - -void -Classifier::DropStores() -{ - for (uint32_t i = 0; i < mHashStores.Length(); i++) { - delete mHashStores[i]; - } - mHashStores.Clear(); - for (uint32_t i = 0; i < mLookupCaches.Length(); i++) { - delete mLookupCaches[i]; - } - mLookupCaches.Clear(); -} - -nsresult -Classifier::RegenActiveTables() -{ - mActiveTablesCache.Clear(); - - nsTArray foundTables; - ScanStoreDir(foundTables); - - for (uint32_t i = 0; i < foundTables.Length(); i++) { - nsCString table(foundTables[i]); - HashStore store(table, mStoreDirectory); - - nsresult rv = store.Open(); - if (NS_FAILED(rv)) - continue; - - LookupCache *lookupCache = GetLookupCache(store.TableName()); - if (!lookupCache) { - continue; - } - - if (!lookupCache->IsPrimed()) - continue; - - const ChunkSet &adds = store.AddChunks(); - const ChunkSet &subs = store.SubChunks(); - - if (adds.Length() == 0 && subs.Length() == 0) - continue; - - LOG(("Active table: %s", store.TableName().get())); - mActiveTablesCache.AppendElement(store.TableName()); - } - - return NS_OK; -} - -nsresult -Classifier::ScanStoreDir(nsTArray& aTables) -{ - nsCOMPtr entries; - nsresult rv = mStoreDirectory->GetDirectoryEntries(getter_AddRefs(entries)); - NS_ENSURE_SUCCESS(rv, rv); - - bool hasMore; - while (NS_SUCCEEDED(rv = entries->HasMoreElements(&hasMore)) && hasMore) { - nsCOMPtr supports; - rv = entries->GetNext(getter_AddRefs(supports)); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr file = do_QueryInterface(supports); - - nsCString leafName; - rv = file->GetNativeLeafName(leafName); - NS_ENSURE_SUCCESS(rv, rv); - - nsCString suffix(NS_LITERAL_CSTRING(".sbstore")); - - int32_t dot = leafName.RFind(suffix, 0); - if (dot != -1) { - leafName.Cut(dot, suffix.Length()); - aTables.AppendElement(leafName); - } - } - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -nsresult -Classifier::ActiveTables(nsTArray& aTables) -{ - aTables = mActiveTablesCache; - return NS_OK; -} - -nsresult -Classifier::CleanToDelete() -{ - bool exists; - nsresult rv = mToDeleteDirectory->Exists(&exists); - NS_ENSURE_SUCCESS(rv, rv); - - if (exists) { - rv = mToDeleteDirectory->Remove(true); - NS_ENSURE_SUCCESS(rv, rv); - } - - return NS_OK; -} - -nsresult -Classifier::BackupTables() -{ - // We have to work in reverse here: first move the normal directory - // away to be the backup directory, then copy the files over - // to the normal directory. This ensures that if we crash the backup - // dir always has a valid, complete copy, instead of a partial one, - // because that's the one we will copy over the normal store dir. - - nsCString backupDirName; - nsresult rv = mBackupDirectory->GetNativeLeafName(backupDirName); - NS_ENSURE_SUCCESS(rv, rv); - - nsCString storeDirName; - rv = mStoreDirectory->GetNativeLeafName(storeDirName); - NS_ENSURE_SUCCESS(rv, rv); - - rv = mStoreDirectory->MoveToNative(nullptr, backupDirName); - NS_ENSURE_SUCCESS(rv, rv); - - rv = mStoreDirectory->CopyToNative(nullptr, storeDirName); - NS_ENSURE_SUCCESS(rv, rv); - - // We moved some things to new places, so move the handles around, too. - rv = SetupPathNames(); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -nsresult -Classifier::RemoveBackupTables() -{ - nsCString toDeleteName; - nsresult rv = mToDeleteDirectory->GetNativeLeafName(toDeleteName); - NS_ENSURE_SUCCESS(rv, rv); - - rv = mBackupDirectory->MoveToNative(nullptr, toDeleteName); - NS_ENSURE_SUCCESS(rv, rv); - - // mBackupDirectory now points to toDelete, fix that up. - rv = SetupPathNames(); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -nsresult -Classifier::RecoverBackups() -{ - bool backupExists; - nsresult rv = mBackupDirectory->Exists(&backupExists); - NS_ENSURE_SUCCESS(rv, rv); - - if (backupExists) { - // Remove the safebrowsing dir if it exists - nsCString storeDirName; - rv = mStoreDirectory->GetNativeLeafName(storeDirName); - NS_ENSURE_SUCCESS(rv, rv); - - bool storeExists; - rv = mStoreDirectory->Exists(&storeExists); - NS_ENSURE_SUCCESS(rv, rv); - - if (storeExists) { - rv = mStoreDirectory->Remove(true); - NS_ENSURE_SUCCESS(rv, rv); - } - - // Move the backup to the store location - rv = mBackupDirectory->MoveToNative(nullptr, storeDirName); - NS_ENSURE_SUCCESS(rv, rv); - - // mBackupDirectory now points to storeDir, fix up. - rv = SetupPathNames(); - NS_ENSURE_SUCCESS(rv, rv); - } - - return NS_OK; -} - -/* - * This will consume+delete updates from the passed nsTArray. -*/ -nsresult -Classifier::ApplyTableUpdates(nsTArray* aUpdates, - const nsACString& aTable) -{ - LOG(("Classifier::ApplyTableUpdates(%s)", PromiseFlatCString(aTable).get())); - - HashStore store(aTable, mStoreDirectory); - - // take the quick exit if there is no valid update for us - // (common case) - uint32_t validupdates = 0; - - for (uint32_t i = 0; i < aUpdates->Length(); i++) { - TableUpdate *update = aUpdates->ElementAt(i); - if (!update || !update->TableName().Equals(store.TableName())) - continue; - if (update->Empty()) { - aUpdates->ElementAt(i) = nullptr; - delete update; - continue; - } - validupdates++; - } - - if (!validupdates) { - // This can happen if the update was only valid for one table. - return NS_OK; - } - - nsresult rv = store.Open(); - NS_ENSURE_SUCCESS(rv, rv); - rv = store.BeginUpdate(); - NS_ENSURE_SUCCESS(rv, rv); - - // Read the part of the store that is (only) in the cache - LookupCache *prefixSet = GetLookupCache(store.TableName()); - if (!prefixSet) { - return NS_ERROR_FAILURE; - } - FallibleTArray AddPrefixHashes; - rv = prefixSet->GetPrefixes(AddPrefixHashes); - NS_ENSURE_SUCCESS(rv, rv); - rv = store.AugmentAdds(AddPrefixHashes); - NS_ENSURE_SUCCESS(rv, rv); - AddPrefixHashes.Clear(); - - uint32_t applied = 0; - bool updateFreshness = false; - bool hasCompletes = false; - - for (uint32_t i = 0; i < aUpdates->Length(); i++) { - TableUpdate *update = aUpdates->ElementAt(i); - if (!update || !update->TableName().Equals(store.TableName())) - continue; - - rv = store.ApplyUpdate(*update); - NS_ENSURE_SUCCESS(rv, rv); - - applied++; - - LOG(("Applied update to table %s:", store.TableName().get())); - LOG((" %d add chunks", update->AddChunks().Length())); - LOG((" %d add prefixes", update->AddPrefixes().Length())); - LOG((" %d add completions", update->AddCompletes().Length())); - LOG((" %d sub chunks", update->SubChunks().Length())); - LOG((" %d sub prefixes", update->SubPrefixes().Length())); - LOG((" %d sub completions", update->SubCompletes().Length())); - LOG((" %d add expirations", update->AddExpirations().Length())); - LOG((" %d sub expirations", update->SubExpirations().Length())); - - if (!update->IsLocalUpdate()) { - updateFreshness = true; - LOG(("Remote update, updating freshness")); - } - - if (update->AddCompletes().Length() > 0 - || update->SubCompletes().Length() > 0) { - hasCompletes = true; - LOG(("Contains Completes, keeping cache.")); - } - - aUpdates->ElementAt(i) = nullptr; - delete update; - } - - LOG(("Applied %d update(s) to %s.", applied, store.TableName().get())); - - rv = store.Rebuild(); - NS_ENSURE_SUCCESS(rv, rv); - - // Not an update with Completes, clear all completes data. - if (!hasCompletes) { - store.ClearCompletes(); - } - - LOG(("Table %s now has:", store.TableName().get())); - LOG((" %d add chunks", store.AddChunks().Length())); - LOG((" %d add prefixes", store.AddPrefixes().Length())); - LOG((" %d add completions", store.AddCompletes().Length())); - LOG((" %d sub chunks", store.SubChunks().Length())); - LOG((" %d sub prefixes", store.SubPrefixes().Length())); - LOG((" %d sub completions", store.SubCompletes().Length())); - - rv = store.WriteFile(); - NS_ENSURE_SUCCESS(rv, rv); - - // At this point the store is updated and written out to disk, but - // the data is still in memory. Build our quick-lookup table here. - rv = prefixSet->Build(store.AddPrefixes(), store.AddCompletes()); - NS_ENSURE_SUCCESS(rv, rv); - -#if defined(DEBUG) && defined(PR_LOGGING) - prefixSet->Dump(); -#endif - rv = prefixSet->WriteFile(); - NS_ENSURE_SUCCESS(rv, rv); - - if (updateFreshness) { - int64_t now = (PR_Now() / PR_USEC_PER_SEC); - LOG(("Successfully updated %s", store.TableName().get())); - mTableFreshness.Put(store.TableName(), now); - } - - return NS_OK; -} - -LookupCache * -Classifier::GetLookupCache(const nsACString& aTable) -{ - for (uint32_t i = 0; i < mLookupCaches.Length(); i++) { - if (mLookupCaches[i]->TableName().Equals(aTable)) { - return mLookupCaches[i]; - } - } - - LookupCache *cache = new LookupCache(aTable, mStoreDirectory); - nsresult rv = cache->Init(); - if (NS_FAILED(rv)) { - return nullptr; - } - rv = cache->Open(); - if (NS_FAILED(rv)) { - if (rv == NS_ERROR_FILE_CORRUPTED) { - Reset(); - } - return nullptr; - } - mLookupCaches.AppendElement(cache); - return cache; -} - -nsresult -Classifier::ReadNoiseEntries(const Prefix& aPrefix, - const nsACString& aTableName, - uint32_t aCount, - PrefixArray* aNoiseEntries) -{ - LookupCache *cache = GetLookupCache(aTableName); - if (!cache) { - return NS_ERROR_FAILURE; - } - - FallibleTArray prefixes; - nsresult rv = cache->GetPrefixes(prefixes); - NS_ENSURE_SUCCESS(rv, rv); - - size_t idx = prefixes.BinaryIndexOf(aPrefix.ToUint32()); - - if (idx == nsTArray::NoIndex) { - NS_WARNING("Could not find prefix in PrefixSet during noise lookup"); - return NS_ERROR_FAILURE; - } - - idx -= idx % aCount; - - for (size_t i = 0; (i < aCount) && ((idx+i) < prefixes.Length()); i++) { - Prefix newPref; - newPref.FromUint32(prefixes[idx+i]); - if (newPref != aPrefix) { - aNoiseEntries->AppendElement(newPref); - } - } - - return NS_OK; -} - -} // namespace safebrowsing -} // namespace mozilla diff --git a/toolkit/components/url-classifier/Classifier.h b/toolkit/components/url-classifier/Classifier.h deleted file mode 100644 index f936bdfe85..0000000000 --- a/toolkit/components/url-classifier/Classifier.h +++ /dev/null @@ -1,110 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#ifndef Classifier_h__ -#define Classifier_h__ - -#include "Entries.h" -#include "HashStore.h" -#include "ProtocolParser.h" -#include "LookupCache.h" -#include "nsCOMPtr.h" -#include "nsString.h" -#include "nsIFile.h" -#include "nsICryptoHash.h" -#include "nsDataHashtable.h" - -namespace mozilla { -namespace safebrowsing { - -/** - * Maintains the stores and LookupCaches for the url classifier. - */ -class Classifier { -public: - Classifier(); - ~Classifier(); - - nsresult Open(nsIFile& aCacheDirectory); - void Close(); - void Reset(); - - /** - * Get the list of active tables and their chunks in a format - * suitable for an update request. - */ - void TableRequest(nsACString& aResult); - - /* - * Get all tables that we know about. - */ - nsresult ActiveTables(nsTArray& aTables); - - /** - * Check a URL against the specified tables. - */ - nsresult Check(const nsACString& aSpec, - const nsACString& tables, - uint32_t aFreshnessGuarantee, - LookupResultArray& aResults); - - /** - * Apply the table updates in the array. Takes ownership of - * the updates in the array and clears it. Wacky! - */ - nsresult ApplyUpdates(nsTArray* aUpdates); - /** - * Failed update. Spoil the entries so we don't block hosts - * unnecessarily - */ - nsresult MarkSpoiled(nsTArray& aTables); - nsresult CacheCompletions(const CacheResultArray& aResults); - uint32_t GetHashKey(void) { return mHashKey; } - /* - * Get a bunch of extra prefixes to query for completion - * and mask the real entry being requested - */ - nsresult ReadNoiseEntries(const Prefix& aPrefix, - const nsACString& aTableName, - uint32_t aCount, - PrefixArray* aNoiseEntries); - static void SplitTables(const nsACString& str, nsTArray& tables); - -private: - void DropStores(); - nsresult CreateStoreDirectory(); - nsresult SetupPathNames(); - nsresult RecoverBackups(); - nsresult CleanToDelete(); - nsresult BackupTables(); - nsresult RemoveBackupTables(); - nsresult RegenActiveTables(); - nsresult ScanStoreDir(nsTArray& aTables); - - nsresult ApplyTableUpdates(nsTArray* aUpdates, - const nsACString& aTable); - - LookupCache *GetLookupCache(const nsACString& aTable); - - // Root dir of the Local profile. - nsCOMPtr mCacheDirectory; - // Main directory where to store the databases. - nsCOMPtr mStoreDirectory; - // Used for atomically updating the other dirs. - nsCOMPtr mBackupDirectory; - nsCOMPtr mToDeleteDirectory; - nsCOMPtr mCryptoHash; - nsTArray mHashStores; - nsTArray mLookupCaches; - nsTArray mActiveTablesCache; - uint32_t mHashKey; - // Stores the last time a given table was updated (seconds). - nsDataHashtable mTableFreshness; -}; - -} -} - -#endif diff --git a/toolkit/components/url-classifier/Entries.h b/toolkit/components/url-classifier/Entries.h deleted file mode 100644 index 3f6136ddd1..0000000000 --- a/toolkit/components/url-classifier/Entries.h +++ /dev/null @@ -1,314 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -// This header file defines the storage types of the actual safebrowsing -// chunk data, which may be either 32-bit hashes or complete 256-bit hashes. -// Chunk numbers are represented in ChunkSet.h. - -#ifndef SBEntries_h__ -#define SBEntries_h__ - -#include "nsTArray.h" -#include "nsString.h" -#include "nsICryptoHash.h" -#include "nsNetUtil.h" - -#if DEBUG -#include "plbase64.h" -#endif - -namespace mozilla { -namespace safebrowsing { - -#define PREFIX_SIZE 4 -#define COMPLETE_SIZE 32 - -// This is the struct that contains 4-byte hash prefixes. -template -struct SafebrowsingHash -{ - static const uint32_t sHashSize = S; - typedef SafebrowsingHash self_type; - uint8_t buf[S]; - - nsresult FromPlaintext(const nsACString& aPlainText, nsICryptoHash* aHash) { - // From the protocol doc: - // Each entry in the chunk is composed - // of the SHA 256 hash of a suffix/prefix expression. - - nsresult rv = aHash->Init(nsICryptoHash::SHA256); - NS_ENSURE_SUCCESS(rv, rv); - - rv = aHash->Update - (reinterpret_cast(aPlainText.BeginReading()), - aPlainText.Length()); - NS_ENSURE_SUCCESS(rv, rv); - - nsAutoCString hashed; - rv = aHash->Finish(false, hashed); - NS_ENSURE_SUCCESS(rv, rv); - - NS_ASSERTION(hashed.Length() >= sHashSize, - "not enough characters in the hash"); - - memcpy(buf, hashed.BeginReading(), sHashSize); - - return NS_OK; - } - - void Assign(const nsACString& aStr) { - NS_ASSERTION(aStr.Length() >= sHashSize, - "string must be at least sHashSize characters long"); - memcpy(buf, aStr.BeginReading(), sHashSize); - } - - int Compare(const self_type& aOther) const { - return Comparator::Compare(buf, aOther.buf); - } - - bool operator==(const self_type& aOther) const { - return Comparator::Compare(buf, aOther.buf) == 0; - } - - bool operator!=(const self_type& aOther) const { - return Comparator::Compare(buf, aOther.buf) != 0; - } - - bool operator<(const self_type& aOther) const { - return Comparator::Compare(buf, aOther.buf) < 0; - } - -#ifdef DEBUG - void ToString(nsACString& aStr) const { - uint32_t len = ((sHashSize + 2) / 3) * 4; - aStr.SetCapacity(len + 1); - PL_Base64Encode((char*)buf, sHashSize, aStr.BeginWriting()); - aStr.BeginWriting()[len] = '\0'; - } - - void ToHexString(nsACString& aStr) const { - static const char* const lut = "0123456789ABCDEF"; - // 32 bytes is the longest hash - size_t len = 32; - - aStr.SetCapacity(2 * len); - for (size_t i = 0; i < len; ++i) { - const char c = static_cast(buf[i]); - aStr.Append(lut[(c >> 4) & 0x0F]); - aStr.Append(lut[c & 15]); - } - } -#endif - uint32_t ToUint32() const { - return *((uint32_t*)buf); - } - void FromUint32(uint32_t aHash) { - *((uint32_t*)buf) = aHash; - } -}; - -class PrefixComparator { -public: - static int Compare(const uint8_t* a, const uint8_t* b) { - uint32_t first = *((uint32_t*)a); - uint32_t second = *((uint32_t*)b); - if (first > second) { - return 1; - } else if (first == second) { - return 0; - } else { - return -1; - } - } -}; -// Use this for 4-byte hashes -typedef SafebrowsingHash Prefix; -typedef nsTArray PrefixArray; - -class CompletionComparator { -public: - static int Compare(const uint8_t* a, const uint8_t* b) { - return memcmp(a, b, COMPLETE_SIZE); - } -}; -// Use this for 32-byte hashes -typedef SafebrowsingHash Completion; -typedef nsTArray CompletionArray; - -struct AddPrefix { - // The truncated hash. - Prefix prefix; - // The chunk number to which it belongs. - uint32_t addChunk; - - AddPrefix() : addChunk(0) {} - - // Returns the chunk number. - uint32_t Chunk() const { return addChunk; } - const Prefix &PrefixHash() const { return prefix; } - - template - int Compare(const T& other) const { - int cmp = prefix.Compare(other.PrefixHash()); - if (cmp != 0) { - return cmp; - } - return addChunk - other.addChunk; - } -}; - -struct AddComplete { - Completion complete; - uint32_t addChunk; - - AddComplete() : addChunk(0) {} - - uint32_t Chunk() const { return addChunk; } - // The 4-byte prefix of the sha256 hash. - uint32_t ToUint32() const { return complete.ToUint32(); } - // The 32-byte sha256 hash. - const Completion &CompleteHash() const { return complete; } - - template - int Compare(const T& other) const { - int cmp = complete.Compare(other.CompleteHash()); - if (cmp != 0) { - return cmp; - } - return addChunk - other.addChunk; - } -}; - -struct SubPrefix { - // The hash to subtract. - Prefix prefix; - // The chunk number of the add chunk to which the hash belonged. - uint32_t addChunk; - // The chunk number of this sub chunk. - uint32_t subChunk; - - SubPrefix(): addChunk(0), subChunk(0) {} - - uint32_t Chunk() const { return subChunk; } - uint32_t AddChunk() const { return addChunk; } - const Prefix &PrefixHash() const { return prefix; } - - template - // Returns 0 if and only if the chunks are the same in every way. - int Compare(const T& aOther) const { - int cmp = prefix.Compare(aOther.PrefixHash()); - if (cmp != 0) - return cmp; - if (addChunk != aOther.addChunk) - return addChunk - aOther.addChunk; - return subChunk - aOther.subChunk; - } - - template - int CompareAlt(const T& aOther) const { - Prefix other; - other.FromUint32(aOther.ToUint32()); - int cmp = prefix.Compare(other); - if (cmp != 0) - return cmp; - return addChunk - aOther.addChunk; - } -}; - -struct SubComplete { - Completion complete; - uint32_t addChunk; - uint32_t subChunk; - - SubComplete() : addChunk(0), subChunk(0) {} - - uint32_t Chunk() const { return subChunk; } - uint32_t AddChunk() const { return addChunk; } - const Completion &CompleteHash() const { return complete; } - // The 4-byte prefix of the sha256 hash. - uint32_t ToUint32() const { return complete.ToUint32(); } - - int Compare(const SubComplete& aOther) const { - int cmp = complete.Compare(aOther.complete); - if (cmp != 0) - return cmp; - if (addChunk != aOther.addChunk) - return addChunk - aOther.addChunk; - return subChunk - aOther.subChunk; - } -}; - -typedef FallibleTArray AddPrefixArray; -typedef FallibleTArray AddCompleteArray; -typedef FallibleTArray SubPrefixArray; -typedef FallibleTArray SubCompleteArray; - -/** - * Compares chunks by their add chunk, then their prefix. - */ -template -class EntryCompare { -public: - typedef T elem_type; - static int Compare(const void* e1, const void* e2) { - const elem_type* a = static_cast(e1); - const elem_type* b = static_cast(e2); - return a->Compare(*b); - } -}; - -/** - * Sort an array of store entries. nsTArray::Sort uses Equal/LessThan - * to sort, this does a single Compare so it's a bit quicker over the - * large sorts we do. - */ -template -void -EntrySort(nsTArray_Impl& aArray) -{ - qsort(aArray.Elements(), aArray.Length(), sizeof(T), - EntryCompare::Compare); -} - -template -nsresult -ReadTArray(nsIInputStream* aStream, nsTArray_Impl* aArray, uint32_t aNumElements) -{ - aArray->SetLength(aNumElements); - - void *buffer = aArray->Elements(); - nsresult rv = NS_ReadInputStreamToBuffer(aStream, &buffer, - (aNumElements * sizeof(T))); - NS_ENSURE_SUCCESS(rv, rv); - return NS_OK; -} - -template -nsresult -ReadTArray(nsIInputStream* aStream, FallibleTArray* aArray, uint32_t aNumElements) -{ - if (!aArray->SetLength(aNumElements)) - return NS_ERROR_OUT_OF_MEMORY; - - void *buffer = aArray->Elements(); - nsresult rv = NS_ReadInputStreamToBuffer(aStream, &buffer, - (aNumElements * sizeof(T))); - NS_ENSURE_SUCCESS(rv, rv); - return NS_OK; -} - -template -nsresult -WriteTArray(nsIOutputStream* aStream, nsTArray_Impl& aArray) -{ - uint32_t written; - return aStream->Write(reinterpret_cast(aArray.Elements()), - aArray.Length() * sizeof(T), - &written); -} - -} // namespace safebrowsing -} // namespace mozilla -#endif // SBEntries_h__ diff --git a/toolkit/components/url-classifier/HashStore.cpp b/toolkit/components/url-classifier/HashStore.cpp deleted file mode 100644 index 83d9f73c0d..0000000000 --- a/toolkit/components/url-classifier/HashStore.cpp +++ /dev/null @@ -1,1050 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -// Originally based on Chrome sources: -// Copyright (c) 2010 The Chromium Authors. All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -#include "HashStore.h" -#include "nsICryptoHash.h" -#include "nsISeekableStream.h" -#include "nsIStreamConverterService.h" -#include "nsNetUtil.h" -#include "nsCheckSummedOutputStream.h" -#include "prlog.h" -#include "zlib.h" - -// Main store for SafeBrowsing protocol data. We store -// known add/sub chunks, prefixes and completions in memory -// during an update, and serialize to disk. -// We do not store the add prefixes, those are retrieved by -// decompressing the PrefixSet cache whenever we need to apply -// an update. -// -// byte slicing: Many of the 4-byte values stored here are strongly -// correlated in the upper bytes, and uncorrelated in the lower -// bytes. Because zlib/DEFLATE requires match lengths of at least -// 3 to achieve good compression, and we don't get those if only -// the upper 16-bits are correlated, it is worthwhile to slice 32-bit -// values into 4 1-byte slices and compress the slices individually. -// The slices corresponding to MSBs will compress very well, and the -// slice corresponding to LSB almost nothing. Because of this, we -// only apply DEFLATE to the 3 most significant bytes, and store the -// LSB uncompressed. -// -// byte sliced (numValues) data format: -// uint32_t compressed-size -// compressed-size bytes zlib DEFLATE data -// 0...numValues byte MSB of 4-byte numValues data -// uint32_t compressed-size -// compressed-size bytes zlib DEFLATE data -// 0...numValues byte 2nd byte of 4-byte numValues data -// uint32_t compressed-size -// compressed-size bytes zlib DEFLATE data -// 0...numValues byte 3rd byte of 4-byte numValues data -// 0...numValues byte LSB of 4-byte numValues data -// -// Store data format: -// uint32_t magic -// uint32_t version -// uint32_t numAddChunks -// uint32_t numSubChunks -// uint32_t numAddPrefixes -// uint32_t numSubPrefixes -// uint32_t numAddCompletes -// uint32_t numSubCompletes -// 0...numAddChunks uint32_t addChunk -// 0...numSubChunks uint32_t subChunk -// byte sliced (numAddPrefixes) uint32_t add chunk of AddPrefixes -// byte sliced (numSubPrefixes) uint32_t add chunk of SubPrefixes -// byte sliced (numSubPrefixes) uint32_t sub chunk of SubPrefixes -// byte sliced (numSubPrefixes) uint32_t SubPrefixes -// 0...numAddCompletes 32-byte Completions + uint32_t addChunk -// 0...numSubCompletes 32-byte Completions + uint32_t addChunk -// + uint32_t subChunk -// 16-byte MD5 of all preceding data - -// Name of the SafeBrowsing store -#define STORE_SUFFIX ".sbstore" - -// NSPR_LOG_MODULES=UrlClassifierDbService:5 -extern PRLogModuleInfo *gUrlClassifierDbServiceLog; -#if defined(PR_LOGGING) -#define LOG(args) PR_LOG(gUrlClassifierDbServiceLog, PR_LOG_DEBUG, args) -#define LOG_ENABLED() PR_LOG_TEST(gUrlClassifierDbServiceLog, 4) -#else -#define LOG(args) -#define LOG_ENABLED() (false) -#endif - -// Either the return was successful or we call the Reset function (unless we -// hit an OOM). Used while reading in the store. -#define SUCCESS_OR_RESET(res) \ - do { \ - nsresult __rv = res; /* Don't evaluate |res| more than once */ \ - if (__rv == NS_ERROR_OUT_OF_MEMORY) { \ - NS_WARNING("SafeBrowsing OOM."); \ - return __rv; \ - } \ - if (NS_FAILED(__rv)) { \ - NS_WARNING("SafeBrowsing store corrupted or out of date."); \ - Reset(); \ - return __rv; \ - } \ - } while(0) - -namespace mozilla { -namespace safebrowsing { - -const uint32_t STORE_MAGIC = 0x1231af3b; -const uint32_t CURRENT_VERSION = 3; - -void -TableUpdate::NewAddPrefix(uint32_t aAddChunk, const Prefix& aHash) -{ - AddPrefix *add = mAddPrefixes.AppendElement(); - add->addChunk = aAddChunk; - add->prefix = aHash; -} - -void -TableUpdate::NewSubPrefix(uint32_t aAddChunk, const Prefix& aHash, uint32_t aSubChunk) -{ - SubPrefix *sub = mSubPrefixes.AppendElement(); - sub->addChunk = aAddChunk; - sub->prefix = aHash; - sub->subChunk = aSubChunk; -} - -void -TableUpdate::NewAddComplete(uint32_t aAddChunk, const Completion& aHash) -{ - AddComplete *add = mAddCompletes.AppendElement(); - add->addChunk = aAddChunk; - add->complete = aHash; -} - -void -TableUpdate::NewSubComplete(uint32_t aAddChunk, const Completion& aHash, uint32_t aSubChunk) -{ - SubComplete *sub = mSubCompletes.AppendElement(); - sub->addChunk = aAddChunk; - sub->complete = aHash; - sub->subChunk = aSubChunk; -} - - -HashStore::HashStore(const nsACString& aTableName, nsIFile* aStoreDir) - : mTableName(aTableName) - , mStoreDirectory(aStoreDir) - , mInUpdate(false) -{ -} - -HashStore::~HashStore() -{ -} - -nsresult -HashStore::Reset() -{ - LOG(("HashStore resetting")); - - nsCOMPtr storeFile; - nsresult rv = mStoreDirectory->Clone(getter_AddRefs(storeFile)); - NS_ENSURE_SUCCESS(rv, rv); - - rv = storeFile->AppendNative(mTableName + NS_LITERAL_CSTRING(STORE_SUFFIX)); - NS_ENSURE_SUCCESS(rv, rv); - - rv = storeFile->Remove(false); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -nsresult -HashStore::CheckChecksum(nsIFile* aStoreFile, - uint32_t aFileSize) -{ - // Check for file corruption by - // comparing the stored checksum to actual checksum of data - nsAutoCString hash; - nsAutoCString compareHash; - char *data; - uint32_t read; - - nsresult rv = CalculateChecksum(hash, aFileSize, true); - NS_ENSURE_SUCCESS(rv, rv); - - compareHash.GetMutableData(&data, hash.Length()); - - if (hash.Length() > aFileSize) { - NS_WARNING("SafeBrowing file not long enough to store its hash"); - return NS_ERROR_FAILURE; - } - nsCOMPtr seekIn = do_QueryInterface(mInputStream); - rv = seekIn->Seek(nsISeekableStream::NS_SEEK_SET, aFileSize - hash.Length()); - NS_ENSURE_SUCCESS(rv, rv); - - rv = mInputStream->Read(data, hash.Length(), &read); - NS_ENSURE_SUCCESS(rv, rv); - NS_ASSERTION(read == hash.Length(), "Could not read hash bytes"); - - if (!hash.Equals(compareHash)) { - NS_WARNING("Safebrowing file failed checksum."); - return NS_ERROR_FAILURE; - } - - return NS_OK; -} - -nsresult -HashStore::Open() -{ - nsCOMPtr storeFile; - nsresult rv = mStoreDirectory->Clone(getter_AddRefs(storeFile)); - NS_ENSURE_SUCCESS(rv, rv); - - rv = storeFile->AppendNative(mTableName + NS_LITERAL_CSTRING(".sbstore")); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr origStream; - rv = NS_NewLocalFileInputStream(getter_AddRefs(origStream), storeFile, - PR_RDONLY | nsIFile::OS_READAHEAD); - - if (rv == NS_ERROR_FILE_NOT_FOUND) { - UpdateHeader(); - return NS_OK; - } else { - SUCCESS_OR_RESET(rv); - } - - int64_t fileSize; - rv = storeFile->GetFileSize(&fileSize); - NS_ENSURE_SUCCESS(rv, rv); - - if (fileSize < 0 || fileSize > UINT32_MAX) { - return NS_ERROR_FAILURE; - } - - uint32_t fileSize32 = static_cast(fileSize); - - rv = NS_NewBufferedInputStream(getter_AddRefs(mInputStream), origStream, - fileSize32); - NS_ENSURE_SUCCESS(rv, rv); - - rv = CheckChecksum(storeFile, fileSize32); - SUCCESS_OR_RESET(rv); - - rv = ReadHeader(); - SUCCESS_OR_RESET(rv); - - rv = SanityCheck(); - SUCCESS_OR_RESET(rv); - - rv = ReadChunkNumbers(); - SUCCESS_OR_RESET(rv); - - return NS_OK; -} - -nsresult -HashStore::ReadHeader() -{ - if (!mInputStream) { - UpdateHeader(); - return NS_OK; - } - - nsCOMPtr seekable = do_QueryInterface(mInputStream); - nsresult rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, 0); - NS_ENSURE_SUCCESS(rv, rv); - - void *buffer = &mHeader; - rv = NS_ReadInputStreamToBuffer(mInputStream, - &buffer, - sizeof(Header)); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -nsresult -HashStore::SanityCheck() -{ - if (mHeader.magic != STORE_MAGIC || mHeader.version != CURRENT_VERSION) { - NS_WARNING("Unexpected header data in the store."); - return NS_ERROR_FAILURE; - } - - return NS_OK; -} - -nsresult -HashStore::CalculateChecksum(nsAutoCString& aChecksum, - uint32_t aFileSize, - bool aChecksumPresent) -{ - aChecksum.Truncate(); - - // Reset mInputStream to start - nsCOMPtr seekable = do_QueryInterface(mInputStream); - nsresult rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, 0); - - nsCOMPtr hash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - // Size of MD5 hash in bytes - const uint32_t CHECKSUM_SIZE = 16; - - // MD5 is not a secure hash function, but since this is a filesystem integrity - // check, this usage is ok. - rv = hash->Init(nsICryptoHash::MD5); - NS_ENSURE_SUCCESS(rv, rv); - - if (!aChecksumPresent) { - // Hash entire file - rv = hash->UpdateFromStream(mInputStream, UINT32_MAX); - } else { - // Hash everything but last checksum bytes - if (aFileSize < CHECKSUM_SIZE) { - NS_WARNING("SafeBrowsing file isn't long enough to store its checksum"); - return NS_ERROR_FAILURE; - } - rv = hash->UpdateFromStream(mInputStream, aFileSize - CHECKSUM_SIZE); - } - NS_ENSURE_SUCCESS(rv, rv); - - rv = hash->Finish(false, aChecksum); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -void -HashStore::UpdateHeader() -{ - mHeader.magic = STORE_MAGIC; - mHeader.version = CURRENT_VERSION; - - mHeader.numAddChunks = mAddChunks.Length(); - mHeader.numSubChunks = mSubChunks.Length(); - mHeader.numAddPrefixes = mAddPrefixes.Length(); - mHeader.numSubPrefixes = mSubPrefixes.Length(); - mHeader.numAddCompletes = mAddCompletes.Length(); - mHeader.numSubCompletes = mSubCompletes.Length(); -} - -nsresult -HashStore::ReadChunkNumbers() -{ - NS_ENSURE_STATE(mInputStream); - - nsCOMPtr seekable = do_QueryInterface(mInputStream); - nsresult rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, - sizeof(Header)); - - rv = mAddChunks.Read(mInputStream, mHeader.numAddChunks); - NS_ENSURE_SUCCESS(rv, rv); - NS_ASSERTION(mAddChunks.Length() == mHeader.numAddChunks, "Read the right amount of add chunks."); - - rv = mSubChunks.Read(mInputStream, mHeader.numSubChunks); - NS_ENSURE_SUCCESS(rv, rv); - NS_ASSERTION(mSubChunks.Length() == mHeader.numSubChunks, "Read the right amount of sub chunks."); - - return NS_OK; -} - -nsresult -HashStore::ReadHashes() -{ - if (!mInputStream) { - // BeginUpdate has been called but Open hasn't initialized mInputStream, - // because the existing HashStore is empty. - return NS_OK; - } - - nsCOMPtr seekable = do_QueryInterface(mInputStream); - - uint32_t offset = sizeof(Header); - offset += (mHeader.numAddChunks + mHeader.numSubChunks) * sizeof(uint32_t); - nsresult rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, offset); - - rv = ReadAddPrefixes(); - NS_ENSURE_SUCCESS(rv, rv); - - rv = ReadSubPrefixes(); - NS_ENSURE_SUCCESS(rv, rv); - - rv = ReadTArray(mInputStream, &mAddCompletes, mHeader.numAddCompletes); - NS_ENSURE_SUCCESS(rv, rv); - - rv = ReadTArray(mInputStream, &mSubCompletes, mHeader.numSubCompletes); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -nsresult -HashStore::BeginUpdate() -{ - // Read the rest of the store in memory. - nsresult rv = ReadHashes(); - SUCCESS_OR_RESET(rv); - - // Close input stream, won't be needed any more and - // we will rewrite ourselves. - if (mInputStream) { - rv = mInputStream->Close(); - NS_ENSURE_SUCCESS(rv, rv); - } - - mInUpdate = true; - - return NS_OK; -} - -template -static nsresult -Merge(ChunkSet* aStoreChunks, - FallibleTArray* aStorePrefixes, - ChunkSet& aUpdateChunks, - FallibleTArray& aUpdatePrefixes, - bool aAllowMerging = false) -{ - EntrySort(aUpdatePrefixes); - - T* updateIter = aUpdatePrefixes.Elements(); - T* updateEnd = aUpdatePrefixes.Elements() + aUpdatePrefixes.Length(); - - T* storeIter = aStorePrefixes->Elements(); - T* storeEnd = aStorePrefixes->Elements() + aStorePrefixes->Length(); - - // use a separate array so we can keep the iterators valid - // if the nsTArray grows - nsTArray adds; - - for (; updateIter != updateEnd; updateIter++) { - // skip this chunk if we already have it, unless we're - // merging completions, in which case we'll always already - // have the chunk from the original prefix - if (aStoreChunks->Has(updateIter->Chunk())) - if (!aAllowMerging) - continue; - // XXX: binary search for insertion point might be faster in common - // case? - while (storeIter < storeEnd && (storeIter->Compare(*updateIter) < 0)) { - // skip forward to matching element (or not...) - storeIter++; - } - // no match, add - if (storeIter == storeEnd - || storeIter->Compare(*updateIter) != 0) { - if (!adds.AppendElement(*updateIter)) - return NS_ERROR_OUT_OF_MEMORY; - } - } - - // Chunks can be empty, but we should still report we have them - // to make the chunkranges continuous. - aStoreChunks->Merge(aUpdateChunks); - - aStorePrefixes->AppendElements(adds); - EntrySort(*aStorePrefixes); - - return NS_OK; -} - -nsresult -HashStore::ApplyUpdate(TableUpdate &update) -{ - nsresult rv = mAddExpirations.Merge(update.AddExpirations()); - NS_ENSURE_SUCCESS(rv, rv); - - rv = mSubExpirations.Merge(update.SubExpirations()); - NS_ENSURE_SUCCESS(rv, rv); - - rv = Expire(); - NS_ENSURE_SUCCESS(rv, rv); - - rv = Merge(&mAddChunks, &mAddPrefixes, - update.AddChunks(), update.AddPrefixes()); - NS_ENSURE_SUCCESS(rv, rv); - - rv = Merge(&mAddChunks, &mAddCompletes, - update.AddChunks(), update.AddCompletes(), true); - NS_ENSURE_SUCCESS(rv, rv); - - rv = Merge(&mSubChunks, &mSubPrefixes, - update.SubChunks(), update.SubPrefixes()); - NS_ENSURE_SUCCESS(rv, rv); - - rv = Merge(&mSubChunks, &mSubCompletes, - update.SubChunks(), update.SubCompletes(), true); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -nsresult -HashStore::Rebuild() -{ - NS_ASSERTION(mInUpdate, "Must be in update to rebuild."); - - nsresult rv = ProcessSubs(); - NS_ENSURE_SUCCESS(rv, rv); - - UpdateHeader(); - - return NS_OK; -} - -void -HashStore::ClearCompletes() -{ - NS_ASSERTION(mInUpdate, "Must be in update to clear completes."); - - mAddCompletes.Clear(); - mSubCompletes.Clear(); - - UpdateHeader(); -} - -template -static void -ExpireEntries(FallibleTArray* aEntries, ChunkSet& aExpirations) -{ - T* addIter = aEntries->Elements(); - T* end = aEntries->Elements() + aEntries->Length(); - - for (T *iter = addIter; iter != end; iter++) { - if (!aExpirations.Has(iter->Chunk())) { - *addIter = *iter; - addIter++; - } - } - - aEntries->SetLength(addIter - aEntries->Elements()); -} - -nsresult -HashStore::Expire() -{ - ExpireEntries(&mAddPrefixes, mAddExpirations); - ExpireEntries(&mAddCompletes, mAddExpirations); - ExpireEntries(&mSubPrefixes, mSubExpirations); - ExpireEntries(&mSubCompletes, mSubExpirations); - - mAddChunks.Remove(mAddExpirations); - mSubChunks.Remove(mSubExpirations); - - mAddExpirations.Clear(); - mSubExpirations.Clear(); - - return NS_OK; -} - -template -nsresult DeflateWriteTArray(nsIOutputStream* aStream, nsTArray& aIn) -{ - uLongf insize = aIn.Length() * sizeof(T); - uLongf outsize = compressBound(insize); - FallibleTArray outBuff; - if (!outBuff.SetLength(outsize)) { - return NS_ERROR_OUT_OF_MEMORY; - } - - int zerr = compress(reinterpret_cast(outBuff.Elements()), - &outsize, - reinterpret_cast(aIn.Elements()), - insize); - if (zerr != Z_OK) { - return NS_ERROR_FAILURE; - } - LOG(("DeflateWriteTArray: %d in %d out", insize, outsize)); - - outBuff.TruncateLength(outsize); - - // Length of compressed data stream - uint32_t dataLen = outBuff.Length(); - uint32_t written; - nsresult rv = aStream->Write(reinterpret_cast(&dataLen), sizeof(dataLen), &written); - NS_ENSURE_SUCCESS(rv, rv); - - NS_ASSERTION(written == sizeof(dataLen), "Error writing deflate length"); - - // Store to stream - rv = WriteTArray(aStream, outBuff); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -template -nsresult InflateReadTArray(nsIInputStream* aStream, FallibleTArray* aOut, - uint32_t aExpectedSize) -{ - - uint32_t inLen; - uint32_t read; - nsresult rv = aStream->Read(reinterpret_cast(&inLen), sizeof(inLen), &read); - NS_ENSURE_SUCCESS(rv, rv); - - NS_ASSERTION(read == sizeof(inLen), "Error reading inflate length"); - - FallibleTArray inBuff; - if (!inBuff.SetLength(inLen)) { - return NS_ERROR_OUT_OF_MEMORY; - } - - rv = ReadTArray(aStream, &inBuff, inLen); - NS_ENSURE_SUCCESS(rv, rv); - - uLongf insize = inLen; - uLongf outsize = aExpectedSize * sizeof(T); - if (!aOut->SetLength(aExpectedSize)) { - return NS_ERROR_OUT_OF_MEMORY; - } - - int zerr = uncompress(reinterpret_cast(aOut->Elements()), - &outsize, - reinterpret_cast(inBuff.Elements()), - insize); - if (zerr != Z_OK) { - return NS_ERROR_FAILURE; - } - LOG(("InflateReadTArray: %d in %d out", insize, outsize)); - - NS_ASSERTION(outsize == aExpectedSize * sizeof(T), "Decompression size mismatch"); - - return NS_OK; -} - -static nsresult -ByteSliceWrite(nsIOutputStream* aOut, nsTArray& aData) -{ - nsTArray slice1; - nsTArray slice2; - nsTArray slice3; - nsTArray slice4; - uint32_t count = aData.Length(); - - slice1.SetCapacity(count); - slice2.SetCapacity(count); - slice3.SetCapacity(count); - slice4.SetCapacity(count); - - for (uint32_t i = 0; i < count; i++) { - slice1.AppendElement( aData[i] >> 24); - slice2.AppendElement((aData[i] >> 16) & 0xFF); - slice3.AppendElement((aData[i] >> 8) & 0xFF); - slice4.AppendElement( aData[i] & 0xFF); - } - - nsresult rv = DeflateWriteTArray(aOut, slice1); - NS_ENSURE_SUCCESS(rv, rv); - rv = DeflateWriteTArray(aOut, slice2); - NS_ENSURE_SUCCESS(rv, rv); - rv = DeflateWriteTArray(aOut, slice3); - NS_ENSURE_SUCCESS(rv, rv); - // The LSB slice is generally uncompressible, don't bother - // compressing it. - rv = WriteTArray(aOut, slice4); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -static nsresult -ByteSliceRead(nsIInputStream* aInStream, FallibleTArray* aData, uint32_t count) -{ - FallibleTArray slice1; - FallibleTArray slice2; - FallibleTArray slice3; - FallibleTArray slice4; - - nsresult rv = InflateReadTArray(aInStream, &slice1, count); - NS_ENSURE_SUCCESS(rv, rv); - - rv = InflateReadTArray(aInStream, &slice2, count); - NS_ENSURE_SUCCESS(rv, rv); - - rv = InflateReadTArray(aInStream, &slice3, count); - NS_ENSURE_SUCCESS(rv, rv); - - rv = ReadTArray(aInStream, &slice4, count); - NS_ENSURE_SUCCESS(rv, rv); - - if (!aData->SetCapacity(count)) { - return NS_ERROR_OUT_OF_MEMORY; - } - - for (uint32_t i = 0; i < count; i++) { - aData->AppendElement((slice1[i] << 24) | (slice2[i] << 16) - | (slice3[i] << 8) | (slice4[i])); - } - - return NS_OK; -} - -nsresult -HashStore::ReadAddPrefixes() -{ - FallibleTArray chunks; - uint32_t count = mHeader.numAddPrefixes; - - nsresult rv = ByteSliceRead(mInputStream, &chunks, count); - NS_ENSURE_SUCCESS(rv, rv); - - if (!mAddPrefixes.SetCapacity(count)) { - return NS_ERROR_OUT_OF_MEMORY; - } - for (uint32_t i = 0; i < count; i++) { - AddPrefix *add = mAddPrefixes.AppendElement(); - add->prefix.FromUint32(0); - add->addChunk = chunks[i]; - } - - return NS_OK; -} - -nsresult -HashStore::ReadSubPrefixes() -{ - FallibleTArray addchunks; - FallibleTArray subchunks; - FallibleTArray prefixes; - uint32_t count = mHeader.numSubPrefixes; - - nsresult rv = ByteSliceRead(mInputStream, &addchunks, count); - NS_ENSURE_SUCCESS(rv, rv); - - rv = ByteSliceRead(mInputStream, &subchunks, count); - NS_ENSURE_SUCCESS(rv, rv); - - rv = ByteSliceRead(mInputStream, &prefixes, count); - NS_ENSURE_SUCCESS(rv, rv); - - if (!mSubPrefixes.SetCapacity(count)) { - return NS_ERROR_OUT_OF_MEMORY; - } - for (uint32_t i = 0; i < count; i++) { - SubPrefix *sub = mSubPrefixes.AppendElement(); - sub->addChunk = addchunks[i]; - sub->prefix.FromUint32(prefixes[i]); - sub->subChunk = subchunks[i]; - } - - return NS_OK; -} - -// Split up PrefixArray back into the constituents -nsresult -HashStore::WriteAddPrefixes(nsIOutputStream* aOut) -{ - nsTArray chunks; - uint32_t count = mAddPrefixes.Length(); - chunks.SetCapacity(count); - - for (uint32_t i = 0; i < count; i++) { - chunks.AppendElement(mAddPrefixes[i].Chunk()); - } - - nsresult rv = ByteSliceWrite(aOut, chunks); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -nsresult -HashStore::WriteSubPrefixes(nsIOutputStream* aOut) -{ - nsTArray addchunks; - nsTArray subchunks; - nsTArray prefixes; - uint32_t count = mSubPrefixes.Length(); - addchunks.SetCapacity(count); - subchunks.SetCapacity(count); - prefixes.SetCapacity(count); - - for (uint32_t i = 0; i < count; i++) { - addchunks.AppendElement(mSubPrefixes[i].AddChunk()); - prefixes.AppendElement(mSubPrefixes[i].PrefixHash().ToUint32()); - subchunks.AppendElement(mSubPrefixes[i].Chunk()); - } - - nsresult rv = ByteSliceWrite(aOut, addchunks); - NS_ENSURE_SUCCESS(rv, rv); - - rv = ByteSliceWrite(aOut, subchunks); - NS_ENSURE_SUCCESS(rv, rv); - - rv = ByteSliceWrite(aOut, prefixes); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -nsresult -HashStore::WriteFile() -{ - NS_ASSERTION(mInUpdate, "Must be in update to write database."); - - nsCOMPtr storeFile; - nsresult rv = mStoreDirectory->Clone(getter_AddRefs(storeFile)); - NS_ENSURE_SUCCESS(rv, rv); - rv = storeFile->AppendNative(mTableName + NS_LITERAL_CSTRING(".sbstore")); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr out; - rv = NS_NewCheckSummedOutputStream(getter_AddRefs(out), storeFile, - PR_WRONLY | PR_TRUNCATE | PR_CREATE_FILE); - NS_ENSURE_SUCCESS(rv, rv); - - uint32_t written; - rv = out->Write(reinterpret_cast(&mHeader), sizeof(mHeader), &written); - NS_ENSURE_SUCCESS(rv, rv); - - // Write chunk numbers. - rv = mAddChunks.Write(out); - NS_ENSURE_SUCCESS(rv, rv); - - rv = mSubChunks.Write(out); - NS_ENSURE_SUCCESS(rv, rv); - - // Write hashes. - rv = WriteAddPrefixes(out); - NS_ENSURE_SUCCESS(rv, rv); - - rv = WriteSubPrefixes(out); - NS_ENSURE_SUCCESS(rv, rv); - - rv = WriteTArray(out, mAddCompletes); - NS_ENSURE_SUCCESS(rv, rv); - - rv = WriteTArray(out, mSubCompletes); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr safeOut = do_QueryInterface(out, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - rv = safeOut->Finish(); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -template -static void -Erase(FallibleTArray* array, T* iterStart, T* iterEnd) -{ - uint32_t start = iterStart - array->Elements(); - uint32_t count = iterEnd - iterStart; - - if (count > 0) { - array->RemoveElementsAt(start, count); - } -} - -// Find items matching between |subs| and |adds|, and remove them, -// recording the item from |adds| in |adds_removed|. To minimize -// copies, the inputs are processing in parallel, so |subs| and |adds| -// should be compatibly ordered (either by SBAddPrefixLess or -// SBAddPrefixHashLess). -// -// |predAS| provides add < sub, |predSA| provides sub < add, for the -// tightest compare appropriate (see calls in SBProcessSubs). -template -static void -KnockoutSubs(FallibleTArray* aSubs, FallibleTArray* aAdds) -{ - // Keep a pair of output iterators for writing kept items. Due to - // deletions, these may lag the main iterators. Using erase() on - // individual items would result in O(N^2) copies. Using a list - // would work around that, at double or triple the memory cost. - TAdd* addOut = aAdds->Elements(); - TAdd* addIter = aAdds->Elements(); - - TSub* subOut = aSubs->Elements(); - TSub* subIter = aSubs->Elements(); - - TAdd* addEnd = addIter + aAdds->Length(); - TSub* subEnd = subIter + aSubs->Length(); - - while (addIter != addEnd && subIter != subEnd) { - // additer compare, so it compares on add chunk - int32_t cmp = addIter->Compare(*subIter); - if (cmp > 0) { - // If |*sub_iter| < |*add_iter|, retain the sub. - *subOut = *subIter; - ++subOut; - ++subIter; - } else if (cmp < 0) { - // If |*add_iter| < |*sub_iter|, retain the add. - *addOut = *addIter; - ++addOut; - ++addIter; - } else { - // Drop equal items - ++addIter; - ++subIter; - } - } - - Erase(aAdds, addOut, addIter); - Erase(aSubs, subOut, subIter); -} - -// Remove items in |removes| from |fullHashes|. |fullHashes| and -// |removes| should be ordered by SBAddPrefix component. -template -static void -RemoveMatchingPrefixes(const SubPrefixArray& aSubs, FallibleTArray* aFullHashes) -{ - // Where to store kept items. - T* out = aFullHashes->Elements(); - T* hashIter = out; - T* hashEnd = aFullHashes->Elements() + aFullHashes->Length(); - - SubPrefix const * removeIter = aSubs.Elements(); - SubPrefix const * removeEnd = aSubs.Elements() + aSubs.Length(); - - while (hashIter != hashEnd && removeIter != removeEnd) { - int32_t cmp = removeIter->CompareAlt(*hashIter); - if (cmp > 0) { - // Keep items less than |*removeIter|. - *out = *hashIter; - ++out; - ++hashIter; - } else if (cmp < 0) { - // No hit for |*removeIter|, bump it forward. - ++removeIter; - } else { - // Drop equal items, there may be multiple hits. - do { - ++hashIter; - } while (hashIter != hashEnd && - !(removeIter->CompareAlt(*hashIter) < 0)); - ++removeIter; - } - } - Erase(aFullHashes, out, hashIter); -} - -static void -RemoveDeadSubPrefixes(SubPrefixArray& aSubs, ChunkSet& aAddChunks) -{ - SubPrefix * subIter = aSubs.Elements(); - SubPrefix * subEnd = aSubs.Elements() + aSubs.Length(); - - for (SubPrefix * iter = subIter; iter != subEnd; iter++) { - bool hasChunk = aAddChunks.Has(iter->AddChunk()); - // Keep the subprefix if the chunk it refers to is one - // we haven't seen it yet. - if (!hasChunk) { - *subIter = *iter; - subIter++; - } - } - - LOG(("Removed %u dead SubPrefix entries.", subEnd - subIter)); - aSubs.SetLength(subIter - aSubs.Elements()); -} - -#ifdef DEBUG -template -static void EnsureSorted(FallibleTArray* aArray) -{ - T* start = aArray->Elements(); - T* end = aArray->Elements() + aArray->Length(); - T* iter = start; - T* previous = start; - - while (iter != end) { - previous = iter; - ++iter; - if (iter != end) { - MOZ_ASSERT(iter->Compare(*previous) >= 0); - } - } - - return; -} -#endif - -nsresult -HashStore::ProcessSubs() -{ -#ifdef DEBUG - EnsureSorted(&mAddPrefixes); - EnsureSorted(&mSubPrefixes); - EnsureSorted(&mAddCompletes); - EnsureSorted(&mSubCompletes); - LOG(("All databases seem to have a consistent sort order.")); -#endif - - RemoveMatchingPrefixes(mSubPrefixes, &mAddCompletes); - RemoveMatchingPrefixes(mSubPrefixes, &mSubCompletes); - - // Remove any remaining subbed prefixes from both addprefixes - // and addcompletes. - KnockoutSubs(&mSubPrefixes, &mAddPrefixes); - KnockoutSubs(&mSubCompletes, &mAddCompletes); - - // Remove any remaining subprefixes referring to addchunks that - // we have (and hence have been processed above). - RemoveDeadSubPrefixes(mSubPrefixes, mAddChunks); - -#ifdef DEBUG - EnsureSorted(&mAddPrefixes); - EnsureSorted(&mSubPrefixes); - EnsureSorted(&mAddCompletes); - EnsureSorted(&mSubCompletes); - LOG(("All databases seem to have a consistent sort order.")); -#endif - - return NS_OK; -} - -nsresult -HashStore::AugmentAdds(const nsTArray& aPrefixes) -{ - uint32_t cnt = aPrefixes.Length(); - if (cnt != mAddPrefixes.Length()) { - LOG(("Amount of prefixes in cache not consistent with store (%d vs %d)", - aPrefixes.Length(), mAddPrefixes.Length())); - return NS_ERROR_FAILURE; - } - for (uint32_t i = 0; i < cnt; i++) { - mAddPrefixes[i].prefix.FromUint32(aPrefixes[i]); - } - return NS_OK; -} - -} -} diff --git a/toolkit/components/url-classifier/HashStore.h b/toolkit/components/url-classifier/HashStore.h deleted file mode 100644 index c33323f05c..0000000000 --- a/toolkit/components/url-classifier/HashStore.h +++ /dev/null @@ -1,200 +0,0 @@ -/* 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/. */ - -#ifndef HashStore_h__ -#define HashStore_h__ - -#include "Entries.h" -#include "ChunkSet.h" - -#include "nsString.h" -#include "nsTArray.h" -#include "nsIFile.h" -#include "nsIFileStreams.h" -#include "nsCOMPtr.h" - -namespace mozilla { -namespace safebrowsing { - -// A table update is built from a single update chunk from the server. As the -// protocol parser processes each chunk, it constructs a table update with the -// new hashes. -class TableUpdate { -public: - explicit TableUpdate(const nsACString& aTable) - : mTable(aTable), mLocalUpdate(false) {} - const nsCString& TableName() const { return mTable; } - - bool Empty() const { - return mAddChunks.Length() == 0 && - mSubChunks.Length() == 0 && - mAddExpirations.Length() == 0 && - mSubExpirations.Length() == 0 && - mAddPrefixes.Length() == 0 && - mSubPrefixes.Length() == 0 && - mAddCompletes.Length() == 0 && - mSubCompletes.Length() == 0; - } - - // Throughout, uint32_t aChunk refers only to the chunk number. Chunk data is - // stored in the Prefix structures. - void NewAddChunk(uint32_t aChunk) { mAddChunks.Set(aChunk); } - void NewSubChunk(uint32_t aChunk) { mSubChunks.Set(aChunk); } - - void NewAddExpiration(uint32_t aChunk) { mAddExpirations.Set(aChunk); } - void NewSubExpiration(uint32_t aChunk) { mSubExpirations.Set(aChunk); } - - void NewAddPrefix(uint32_t aAddChunk, const Prefix& aPrefix); - void NewSubPrefix(uint32_t aAddChunk, const Prefix& aPrefix, uint32_t aSubChunk); - - void NewAddComplete(uint32_t aChunk, const Completion& aCompletion); - void NewSubComplete(uint32_t aAddChunk, const Completion& aCompletion, - uint32_t aSubChunk); - void SetLocalUpdate(void) { mLocalUpdate = true; } - bool IsLocalUpdate(void) { return mLocalUpdate; } - - ChunkSet& AddChunks() { return mAddChunks; } - ChunkSet& SubChunks() { return mSubChunks; } - - // Expirations for chunks. - ChunkSet& AddExpirations() { return mAddExpirations; } - ChunkSet& SubExpirations() { return mSubExpirations; } - - // Hashes associated with this chunk. - AddPrefixArray& AddPrefixes() { return mAddPrefixes; } - SubPrefixArray& SubPrefixes() { return mSubPrefixes; } - AddCompleteArray& AddCompletes() { return mAddCompletes; } - SubCompleteArray& SubCompletes() { return mSubCompletes; } - -private: - nsCString mTable; - // Update not from the remote server (no freshness) - bool mLocalUpdate; - - // The list of chunk numbers that we have for each of the type of chunks. - ChunkSet mAddChunks; - ChunkSet mSubChunks; - ChunkSet mAddExpirations; - ChunkSet mSubExpirations; - - // 4-byte sha256 prefixes. - AddPrefixArray mAddPrefixes; - SubPrefixArray mSubPrefixes; - - // 32-byte hashes. - AddCompleteArray mAddCompletes; - SubCompleteArray mSubCompletes; -}; - -// There is one hash store per table. -class HashStore { -public: - HashStore(const nsACString& aTableName, nsIFile* aStoreFile); - ~HashStore(); - - const nsCString& TableName() const { return mTableName; } - - nsresult Open(); - // Add Prefixes are stored partly in the PrefixSet (contains the - // Prefix data organized for fast lookup/low RAM usage) and partly in the - // HashStore (Add Chunk numbers - only used for updates, slow retrieval). - // AugmentAdds function joins the separate datasets into one complete - // prefixes+chunknumbers dataset. - nsresult AugmentAdds(const nsTArray& aPrefixes); - - ChunkSet& AddChunks() { return mAddChunks; } - ChunkSet& SubChunks() { return mSubChunks; } - AddPrefixArray& AddPrefixes() { return mAddPrefixes; } - AddCompleteArray& AddCompletes() { return mAddCompletes; } - SubPrefixArray& SubPrefixes() { return mSubPrefixes; } - SubCompleteArray& SubCompletes() { return mSubCompletes; } - - // ======= - // Updates - // ======= - // Begin the update process. Reads the store into memory. - nsresult BeginUpdate(); - - // Imports the data from a TableUpdate. - nsresult ApplyUpdate(TableUpdate &aUpdate); - - // Process expired chunks - nsresult Expire(); - - // Rebuild the store, Incorporating all the applied updates. - nsresult Rebuild(); - - // Write the current state of the store to disk. - // If you call between ApplyUpdate() and Rebuild(), you'll - // have a mess on your hands. - nsresult WriteFile(); - - // Wipe out all Completes. - void ClearCompletes(); - -private: - nsresult Reset(); - - nsresult ReadHeader(); - nsresult SanityCheck(); - nsresult CalculateChecksum(nsAutoCString& aChecksum, uint32_t aFileSize, - bool aChecksumPresent); - nsresult CheckChecksum(nsIFile* aStoreFile, uint32_t aFileSize); - void UpdateHeader(); - - nsresult ReadChunkNumbers(); - nsresult ReadHashes(); - - nsresult ReadAddPrefixes(); - nsresult ReadSubPrefixes(); - - nsresult WriteAddPrefixes(nsIOutputStream* aOut); - nsresult WriteSubPrefixes(nsIOutputStream* aOut); - - nsresult ProcessSubs(); - - // This is used for checking that the database is correct and for figuring out - // the number of chunks, etc. to read from disk on restart. - struct Header { - uint32_t magic; - uint32_t version; - uint32_t numAddChunks; - uint32_t numSubChunks; - uint32_t numAddPrefixes; - uint32_t numSubPrefixes; - uint32_t numAddCompletes; - uint32_t numSubCompletes; - }; - - Header mHeader; - - // The name of the table (must end in -shavar or -digest256, or evidently - // -simple for unittesting. - nsCString mTableName; - nsCOMPtr mStoreDirectory; - - bool mInUpdate; - - nsCOMPtr mInputStream; - - // Chunk numbers, stored as uint32_t arrays. - ChunkSet mAddChunks; - ChunkSet mSubChunks; - - ChunkSet mAddExpirations; - ChunkSet mSubExpirations; - - // Chunk data for shavar tables. See Entries.h for format. - AddPrefixArray mAddPrefixes; - SubPrefixArray mSubPrefixes; - - // See bug 806422 for background. We must be able to distinguish between - // updates from the completion server and updates from the regular server. - AddCompleteArray mAddCompletes; - SubCompleteArray mSubCompletes; -}; - -} -} -#endif diff --git a/toolkit/components/url-classifier/LookupCache.cpp b/toolkit/components/url-classifier/LookupCache.cpp deleted file mode 100644 index 08adf309e4..0000000000 --- a/toolkit/components/url-classifier/LookupCache.cpp +++ /dev/null @@ -1,699 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#include "LookupCache.h" -#include "HashStore.h" -#include "nsISeekableStream.h" -#include "mozilla/Telemetry.h" -#include "prlog.h" -#include "prprf.h" - -// We act as the main entry point for all the real lookups, -// so note that those are not done to the actual HashStore. -// The latter solely exists to store the data needed to handle -// the updates from the protocol. - -// This module has its own store, which stores the Completions, -// mostly caching lookups that have happened over the net. -// The prefixes are cached/checked by looking them up in the -// PrefixSet. - -// Data format for the ".cache" files: -// uint32_t magic Identify the file type -// uint32_t version Version identifier for file format -// uint32_t numCompletions Amount of completions stored -// 0...numCompletions 256-bit Completions - -// Name of the lookupcomplete cache -#define CACHE_SUFFIX ".cache" - -// Name of the persistent PrefixSet storage -#define PREFIXSET_SUFFIX ".pset" - -// NSPR_LOG_MODULES=UrlClassifierDbService:5 -extern PRLogModuleInfo *gUrlClassifierDbServiceLog; -#if defined(PR_LOGGING) -#define LOG(args) PR_LOG(gUrlClassifierDbServiceLog, PR_LOG_DEBUG, args) -#define LOG_ENABLED() PR_LOG_TEST(gUrlClassifierDbServiceLog, 4) -#else -#define LOG(args) -#define LOG_ENABLED() (false) -#endif - -namespace mozilla { -namespace safebrowsing { - -const uint32_t LOOKUPCACHE_MAGIC = 0x1231af3e; -const uint32_t CURRENT_VERSION = 2; - -LookupCache::LookupCache(const nsACString& aTableName, nsIFile* aStoreDir) - : mPrimed(false) - , mTableName(aTableName) - , mStoreDirectory(aStoreDir) -{ -} - -nsresult -LookupCache::Init() -{ - mPrefixSet = new nsUrlClassifierPrefixSet(); - nsresult rv = mPrefixSet->Init(mTableName); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -LookupCache::~LookupCache() -{ -} - -nsresult -LookupCache::Open() -{ - nsCOMPtr storeFile; - - nsresult rv = mStoreDirectory->Clone(getter_AddRefs(storeFile)); - NS_ENSURE_SUCCESS(rv, rv); - - rv = storeFile->AppendNative(mTableName + NS_LITERAL_CSTRING(CACHE_SUFFIX)); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr inputStream; - rv = NS_NewLocalFileInputStream(getter_AddRefs(inputStream), storeFile, - PR_RDONLY | nsIFile::OS_READAHEAD); - - if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) { - Reset(); - return rv; - } - - if (rv == NS_ERROR_FILE_NOT_FOUND) { - // Simply lacking a .cache file is a recoverable error, - // as unlike the .pset/.sbstore files it is a pure cache. - // Just create a new empty one. - ClearCompleteCache(); - } else { - // Read in the .cache file - rv = ReadHeader(inputStream); - NS_ENSURE_SUCCESS(rv, rv); - LOG(("ReadCompletions")); - rv = ReadCompletions(inputStream); - NS_ENSURE_SUCCESS(rv, rv); - - rv = inputStream->Close(); - NS_ENSURE_SUCCESS(rv, rv); - } - - LOG(("Loading PrefixSet")); - rv = LoadPrefixSet(); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -nsresult -LookupCache::UpdateDirHandle(nsIFile* aStoreDirectory) -{ - return aStoreDirectory->Clone(getter_AddRefs(mStoreDirectory)); -} - -nsresult -LookupCache::Reset() -{ - LOG(("LookupCache resetting")); - - nsCOMPtr storeFile; - nsCOMPtr prefixsetFile; - nsresult rv = mStoreDirectory->Clone(getter_AddRefs(storeFile)); - NS_ENSURE_SUCCESS(rv, rv); - rv = mStoreDirectory->Clone(getter_AddRefs(prefixsetFile)); - NS_ENSURE_SUCCESS(rv, rv); - - rv = storeFile->AppendNative(mTableName + NS_LITERAL_CSTRING(CACHE_SUFFIX)); - NS_ENSURE_SUCCESS(rv, rv); - rv = prefixsetFile->AppendNative(mTableName + NS_LITERAL_CSTRING(PREFIXSET_SUFFIX)); - NS_ENSURE_SUCCESS(rv, rv); - - rv = storeFile->Remove(false); - NS_ENSURE_SUCCESS(rv, rv); - rv = prefixsetFile->Remove(false); - NS_ENSURE_SUCCESS(rv, rv); - - ClearAll(); - - return NS_OK; -} - - -nsresult -LookupCache::Build(AddPrefixArray& aAddPrefixes, - AddCompleteArray& aAddCompletes) -{ - Telemetry::Accumulate(Telemetry::URLCLASSIFIER_LC_COMPLETIONS, - static_cast(aAddCompletes.Length())); - - mCompletions.Clear(); - mCompletions.SetCapacity(aAddCompletes.Length()); - for (uint32_t i = 0; i < aAddCompletes.Length(); i++) { - mCompletions.AppendElement(aAddCompletes[i].CompleteHash()); - } - aAddCompletes.Clear(); - mCompletions.Sort(); - - Telemetry::Accumulate(Telemetry::URLCLASSIFIER_LC_PREFIXES, - static_cast(aAddPrefixes.Length())); - - nsresult rv = ConstructPrefixSet(aAddPrefixes); - NS_ENSURE_SUCCESS(rv, rv); - mPrimed = true; - - return NS_OK; -} - -#if defined(DEBUG) && defined(PR_LOGGING) -void -LookupCache::Dump() -{ - if (!LOG_ENABLED()) - return; - - for (uint32_t i = 0; i < mCompletions.Length(); i++) { - nsAutoCString str; - mCompletions[i].ToHexString(str); - LOG(("Completion: %s", str.get())); - } -} -#endif - -nsresult -LookupCache::Has(const Completion& aCompletion, - bool* aHas, bool* aComplete) -{ - *aHas = *aComplete = false; - - uint32_t prefix = aCompletion.ToUint32(); - - bool found; - nsresult rv = mPrefixSet->Contains(prefix, &found); - NS_ENSURE_SUCCESS(rv, rv); - - LOG(("Probe in %s: %X, found %d", mTableName.get(), prefix, found)); - - if (found) { - *aHas = true; - } - - if (mCompletions.BinaryIndexOf(aCompletion) != nsTArray::NoIndex) { - LOG(("Complete in %s", mTableName.get())); - *aComplete = true; - *aHas = true; - } - - return NS_OK; -} - -nsresult -LookupCache::WriteFile() -{ - nsCOMPtr storeFile; - nsresult rv = mStoreDirectory->Clone(getter_AddRefs(storeFile)); - NS_ENSURE_SUCCESS(rv, rv); - rv = storeFile->AppendNative(mTableName + NS_LITERAL_CSTRING(CACHE_SUFFIX)); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr out; - rv = NS_NewSafeLocalFileOutputStream(getter_AddRefs(out), storeFile, - PR_WRONLY | PR_TRUNCATE | PR_CREATE_FILE); - NS_ENSURE_SUCCESS(rv, rv); - - UpdateHeader(); - LOG(("Writing %d completions", mHeader.numCompletions)); - - uint32_t written; - rv = out->Write(reinterpret_cast(&mHeader), sizeof(mHeader), &written); - NS_ENSURE_SUCCESS(rv, rv); - - rv = WriteTArray(out, mCompletions); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr safeOut = do_QueryInterface(out); - rv = safeOut->Finish(); - NS_ENSURE_SUCCESS(rv, rv); - - rv = EnsureSizeConsistent(); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr psFile; - rv = mStoreDirectory->Clone(getter_AddRefs(psFile)); - NS_ENSURE_SUCCESS(rv, rv); - - rv = psFile->AppendNative(mTableName + NS_LITERAL_CSTRING(PREFIXSET_SUFFIX)); - NS_ENSURE_SUCCESS(rv, rv); - - rv = mPrefixSet->StoreToFile(psFile); - NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "failed to store the prefixset"); - - return NS_OK; -} - -void -LookupCache::ClearAll() -{ - ClearCompleteCache(); - mPrefixSet->SetPrefixes(nullptr, 0); - mPrimed = false; -} - -void -LookupCache::ClearCompleteCache() -{ - mCompletions.Clear(); - UpdateHeader(); -} - -void -LookupCache::UpdateHeader() -{ - mHeader.magic = LOOKUPCACHE_MAGIC; - mHeader.version = CURRENT_VERSION; - mHeader.numCompletions = mCompletions.Length(); -} - -nsresult -LookupCache::EnsureSizeConsistent() -{ - nsCOMPtr storeFile; - nsresult rv = mStoreDirectory->Clone(getter_AddRefs(storeFile)); - NS_ENSURE_SUCCESS(rv, rv); - rv = storeFile->AppendNative(mTableName + NS_LITERAL_CSTRING(CACHE_SUFFIX)); - NS_ENSURE_SUCCESS(rv, rv); - - int64_t fileSize; - rv = storeFile->GetFileSize(&fileSize); - NS_ENSURE_SUCCESS(rv, rv); - - if (fileSize < 0) { - return NS_ERROR_FAILURE; - } - - int64_t expectedSize = sizeof(mHeader) - + mHeader.numCompletions*sizeof(Completion); - if (expectedSize != fileSize) { - NS_WARNING("File length does not match. Probably corrupted."); - Reset(); - return NS_ERROR_FILE_CORRUPTED; - } - - return NS_OK; -} - -nsresult -LookupCache::ReadHeader(nsIInputStream* aInputStream) -{ - if (!aInputStream) { - ClearCompleteCache(); - return NS_OK; - } - - nsCOMPtr seekable = do_QueryInterface(aInputStream); - nsresult rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, 0); - NS_ENSURE_SUCCESS(rv, rv); - - void *buffer = &mHeader; - rv = NS_ReadInputStreamToBuffer(aInputStream, - &buffer, - sizeof(Header)); - NS_ENSURE_SUCCESS(rv, rv); - - if (mHeader.magic != LOOKUPCACHE_MAGIC || mHeader.version != CURRENT_VERSION) { - NS_WARNING("Unexpected header data in the store."); - Reset(); - return NS_ERROR_FILE_CORRUPTED; - } - LOG(("%d completions present", mHeader.numCompletions)); - - rv = EnsureSizeConsistent(); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -nsresult -LookupCache::ReadCompletions(nsIInputStream* aInputStream) -{ - if (!mHeader.numCompletions) { - mCompletions.Clear(); - return NS_OK; - } - - nsCOMPtr seekable = do_QueryInterface(aInputStream); - nsresult rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, sizeof(Header)); - NS_ENSURE_SUCCESS(rv, rv); - - rv = ReadTArray(aInputStream, &mCompletions, mHeader.numCompletions); - NS_ENSURE_SUCCESS(rv, rv); - - LOG(("Read %d completions", mCompletions.Length())); - - return NS_OK; -} - -/* static */ bool -LookupCache::IsCanonicalizedIP(const nsACString& aHost) -{ - // The canonicalization process will have left IP addresses in dotted - // decimal with no surprises. - uint32_t i1, i2, i3, i4; - char c; - if (PR_sscanf(PromiseFlatCString(aHost).get(), "%u.%u.%u.%u%c", - &i1, &i2, &i3, &i4, &c) == 4) { - return (i1 <= 0xFF && i2 <= 0xFF && i3 <= 0xFF && i4 <= 0xFF); - } - - return false; -} - -/* static */ nsresult -LookupCache::GetKey(const nsACString& aSpec, - Completion* aHash, - nsCOMPtr& aCryptoHash) -{ - nsACString::const_iterator begin, end, iter; - aSpec.BeginReading(begin); - aSpec.EndReading(end); - - iter = begin; - if (!FindCharInReadable('/', iter, end)) { - return NS_OK; - } - - const nsCSubstring& host = Substring(begin, iter); - - if (IsCanonicalizedIP(host)) { - nsAutoCString key; - key.Assign(host); - key.Append('/'); - return aHash->FromPlaintext(key, aCryptoHash); - } - - nsTArray hostComponents; - ParseString(PromiseFlatCString(host), '.', hostComponents); - - if (hostComponents.Length() < 2) - return NS_ERROR_FAILURE; - - int32_t last = int32_t(hostComponents.Length()) - 1; - nsAutoCString lookupHost; - - if (hostComponents.Length() > 2) { - lookupHost.Append(hostComponents[last - 2]); - lookupHost.Append('.'); - } - - lookupHost.Append(hostComponents[last - 1]); - lookupHost.Append('.'); - lookupHost.Append(hostComponents[last]); - lookupHost.Append('/'); - - return aHash->FromPlaintext(lookupHost, aCryptoHash); -} - -/* static */ nsresult -LookupCache::GetLookupFragments(const nsACString& aSpec, - nsTArray* aFragments) - -{ - aFragments->Clear(); - - nsACString::const_iterator begin, end, iter; - aSpec.BeginReading(begin); - aSpec.EndReading(end); - - iter = begin; - if (!FindCharInReadable('/', iter, end)) { - return NS_OK; - } - - const nsCSubstring& host = Substring(begin, iter++); - nsAutoCString path; - path.Assign(Substring(iter, end)); - - /** - * From the protocol doc: - * For the hostname, the client will try at most 5 different strings. They - * are: - * a) The exact hostname of the url - * b) The 4 hostnames formed by starting with the last 5 components and - * successivly removing the leading component. The top-level component - * can be skipped. This is not done if the hostname is a numerical IP. - */ - nsTArray hosts; - hosts.AppendElement(host); - - if (!IsCanonicalizedIP(host)) { - host.BeginReading(begin); - host.EndReading(end); - int numHostComponents = 0; - while (RFindInReadable(NS_LITERAL_CSTRING("."), begin, end) && - numHostComponents < MAX_HOST_COMPONENTS) { - // don't bother checking toplevel domains - if (++numHostComponents >= 2) { - host.EndReading(iter); - hosts.AppendElement(Substring(end, iter)); - } - end = begin; - host.BeginReading(begin); - } - } - - /** - * From the protocol doc: - * For the path, the client will also try at most 6 different strings. - * They are: - * a) the exact path of the url, including query parameters - * b) the exact path of the url, without query parameters - * c) the 4 paths formed by starting at the root (/) and - * successively appending path components, including a trailing - * slash. This behavior should only extend up to the next-to-last - * path component, that is, a trailing slash should never be - * appended that was not present in the original url. - */ - nsTArray paths; - nsAutoCString pathToAdd; - - path.BeginReading(begin); - path.EndReading(end); - iter = begin; - if (FindCharInReadable('?', iter, end)) { - pathToAdd = Substring(begin, iter); - paths.AppendElement(pathToAdd); - end = iter; - } - - int numPathComponents = 1; - iter = begin; - while (FindCharInReadable('/', iter, end) && - numPathComponents < MAX_PATH_COMPONENTS) { - iter++; - pathToAdd.Assign(Substring(begin, iter)); - paths.AppendElement(pathToAdd); - numPathComponents++; - } - - // If we haven't already done so, add the full path - if (!pathToAdd.Equals(path)) { - paths.AppendElement(path); - } - // Check an empty path (for whole-domain blacklist entries) - paths.AppendElement(EmptyCString()); - - for (uint32_t hostIndex = 0; hostIndex < hosts.Length(); hostIndex++) { - for (uint32_t pathIndex = 0; pathIndex < paths.Length(); pathIndex++) { - nsCString key; - key.Assign(hosts[hostIndex]); - key.Append('/'); - key.Append(paths[pathIndex]); - LOG(("Checking fragment %s", key.get())); - - aFragments->AppendElement(key); - } - } - - return NS_OK; -} - -/* static */ nsresult -LookupCache::GetHostKeys(const nsACString& aSpec, - nsTArray* aHostKeys) -{ - nsACString::const_iterator begin, end, iter; - aSpec.BeginReading(begin); - aSpec.EndReading(end); - - iter = begin; - if (!FindCharInReadable('/', iter, end)) { - return NS_OK; - } - - const nsCSubstring& host = Substring(begin, iter); - - if (IsCanonicalizedIP(host)) { - nsCString *key = aHostKeys->AppendElement(); - if (!key) - return NS_ERROR_OUT_OF_MEMORY; - - key->Assign(host); - key->Append("/"); - return NS_OK; - } - - nsTArray hostComponents; - ParseString(PromiseFlatCString(host), '.', hostComponents); - - if (hostComponents.Length() < 2) { - // no host or toplevel host, this won't match anything in the db - return NS_OK; - } - - // First check with two domain components - int32_t last = int32_t(hostComponents.Length()) - 1; - nsCString *lookupHost = aHostKeys->AppendElement(); - if (!lookupHost) - return NS_ERROR_OUT_OF_MEMORY; - - lookupHost->Assign(hostComponents[last - 1]); - lookupHost->Append("."); - lookupHost->Append(hostComponents[last]); - lookupHost->Append("/"); - - // Now check with three domain components - if (hostComponents.Length() > 2) { - nsCString *lookupHost2 = aHostKeys->AppendElement(); - if (!lookupHost2) - return NS_ERROR_OUT_OF_MEMORY; - lookupHost2->Assign(hostComponents[last - 2]); - lookupHost2->Append("."); - lookupHost2->Append(*lookupHost); - } - - return NS_OK; -} - -bool LookupCache::IsPrimed() -{ - return mPrimed; -} - -#ifdef DEBUG -template -static void EnsureSorted(T* aArray) -{ - typename T::elem_type* start = aArray->Elements(); - typename T::elem_type* end = aArray->Elements() + aArray->Length(); - typename T::elem_type* iter = start; - typename T::elem_type* previous = start; - - while (iter != end) { - previous = iter; - ++iter; - if (iter != end) { - MOZ_ASSERT(*previous <= *iter); - } - } - return; -} -#endif - -nsresult -LookupCache::ConstructPrefixSet(AddPrefixArray& aAddPrefixes) -{ - Telemetry::AutoTimer timer; - - nsTArray array; - array.SetCapacity(aAddPrefixes.Length()); - - for (uint32_t i = 0; i < aAddPrefixes.Length(); i++) { - array.AppendElement(aAddPrefixes[i].PrefixHash().ToUint32()); - } - aAddPrefixes.Clear(); - -#ifdef DEBUG - // PrefixSet requires sorted order - EnsureSorted(&array); -#endif - - // construct new one, replace old entries - nsresult rv = mPrefixSet->SetPrefixes(array.Elements(), array.Length()); - if (NS_FAILED(rv)) { - goto error_bailout; - } - -#ifdef DEBUG - uint32_t size; - size = mPrefixSet->SizeInMemory(); - LOG(("SB tree done, size = %d bytes\n", size)); -#endif - - mPrimed = true; - - return NS_OK; - - error_bailout: - Telemetry::Accumulate(Telemetry::URLCLASSIFIER_PS_FAILURE, 1); - return rv; -} - -nsresult -LookupCache::LoadPrefixSet() -{ - nsCOMPtr psFile; - nsresult rv = mStoreDirectory->Clone(getter_AddRefs(psFile)); - NS_ENSURE_SUCCESS(rv, rv); - - rv = psFile->AppendNative(mTableName + NS_LITERAL_CSTRING(PREFIXSET_SUFFIX)); - NS_ENSURE_SUCCESS(rv, rv); - - bool exists; - rv = psFile->Exists(&exists); - NS_ENSURE_SUCCESS(rv, rv); - - if (exists) { - LOG(("stored PrefixSet exists, loading from disk")); - rv = mPrefixSet->LoadFromFile(psFile); - if (NS_FAILED(rv)) { - if (rv == NS_ERROR_FILE_CORRUPTED) { - Reset(); - } - return rv; - } - mPrimed = true; - } else { - LOG(("no (usable) stored PrefixSet found")); - } - -#ifdef DEBUG - if (mPrimed) { - uint32_t size = mPrefixSet->SizeInMemory(); - LOG(("SB tree done, size = %d bytes\n", size)); - } -#endif - - return NS_OK; -} - -nsresult -LookupCache::GetPrefixes(FallibleTArray& aAddPrefixes) -{ - if (!mPrimed) { - // This can happen if its a new table, so no error. - LOG(("GetPrefixes from empty LookupCache")); - return NS_OK; - } - return mPrefixSet->GetPrefixesNative(aAddPrefixes); -} - - -} -} diff --git a/toolkit/components/url-classifier/LookupCache.h b/toolkit/components/url-classifier/LookupCache.h deleted file mode 100644 index ad551f6ca6..0000000000 --- a/toolkit/components/url-classifier/LookupCache.h +++ /dev/null @@ -1,142 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#ifndef LookupCache_h__ -#define LookupCache_h__ - -#include "Entries.h" -#include "nsString.h" -#include "nsTArray.h" -#include "nsCOMPtr.h" -#include "nsIFile.h" -#include "nsIFileStreams.h" -#include "nsUrlClassifierPrefixSet.h" -#include "prlog.h" - -namespace mozilla { -namespace safebrowsing { - -#define MAX_HOST_COMPONENTS 5 -#define MAX_PATH_COMPONENTS 4 - -class LookupResult { -public: - LookupResult() : mComplete(false), mNoise(false), mFresh(false), mProtocolConfirmed(false) {} - - // The fragment that matched in the LookupCache - union { - Prefix prefix; - Completion complete; - } hash; - - const Prefix &PrefixHash() { return hash.prefix; } - const Completion &CompleteHash() { return hash.complete; } - - bool Confirmed() const { return (mComplete && mFresh) || mProtocolConfirmed; } - bool Complete() const { return mComplete; } - - // True if we have a complete match for this hash in the table. - bool mComplete; - - // True if this is a noise entry, i.e. an extra entry - // that is inserted to mask the true URL we are requesting - bool mNoise; - - // True if we've updated this table recently-enough. - bool mFresh; - - bool mProtocolConfirmed; - - nsCString mTableName; -}; - -typedef nsTArray LookupResultArray; - -struct CacheResult { - AddComplete entry; - nsCString table; -}; -typedef nsTArray CacheResultArray; - -class LookupCache { -public: - // Check for a canonicalized IP address. - static bool IsCanonicalizedIP(const nsACString& aHost); - - // take a lookup string (www.hostname.com/path/to/resource.html) and - // expand it into the set of fragments that should be searched for in an - // entry - static nsresult GetLookupFragments(const nsACString& aSpec, - nsTArray* aFragments); - // Similar to GetKey(), but if the domain contains three or more components, - // two keys will be returned: - // hostname.com/foo/bar -> [hostname.com] - // mail.hostname.com/foo/bar -> [hostname.com, mail.hostname.com] - // www.mail.hostname.com/foo/bar -> [hostname.com, mail.hostname.com] - static nsresult GetHostKeys(const nsACString& aSpec, - nsTArray* aHostKeys); - // Get the database key for a given URI. This is the top three - // domain components if they exist, otherwise the top two. - // hostname.com/foo/bar -> hostname.com - // mail.hostname.com/foo/bar -> mail.hostname.com - // www.mail.hostname.com/foo/bar -> mail.hostname.com - static nsresult GetKey(const nsACString& aSpec, Completion* aHash, - nsCOMPtr& aCryptoHash); - - LookupCache(const nsACString& aTableName, nsIFile* aStoreFile); - ~LookupCache(); - - const nsCString &TableName() const { return mTableName; } - - nsresult Init(); - nsresult Open(); - // The directory handle where we operate will - // be moved away when a backup is made. - nsresult UpdateDirHandle(nsIFile* aStoreDirectory); - // This will Clear() the passed arrays when done. - nsresult Build(AddPrefixArray& aAddPrefixes, - AddCompleteArray& aAddCompletes); - nsresult GetPrefixes(FallibleTArray& aAddPrefixes); - void ClearCompleteCache(); - -#if DEBUG && defined(PR_LOGGING) - void Dump(); -#endif - nsresult WriteFile(); - nsresult Has(const Completion& aCompletion, - bool* aHas, bool* aComplete); - bool IsPrimed(); - -private: - void ClearAll(); - nsresult Reset(); - void UpdateHeader(); - nsresult ReadHeader(nsIInputStream* aInputStream); - nsresult ReadCompletions(nsIInputStream* aInputStream); - nsresult EnsureSizeConsistent(); - nsresult LoadPrefixSet(); - // Construct a Prefix Set with known prefixes. - // This will Clear() aAddPrefixes when done. - nsresult ConstructPrefixSet(AddPrefixArray& aAddPrefixes); - - struct Header { - uint32_t magic; - uint32_t version; - uint32_t numCompletions; - }; - Header mHeader; - - bool mPrimed; - nsCString mTableName; - nsCOMPtr mStoreDirectory; - CompletionArray mCompletions; - // Set of prefixes known to be in the database - nsRefPtr mPrefixSet; -}; - -} -} - -#endif diff --git a/toolkit/components/url-classifier/ProtocolParser.cpp b/toolkit/components/url-classifier/ProtocolParser.cpp deleted file mode 100644 index 5b77fc9a85..0000000000 --- a/toolkit/components/url-classifier/ProtocolParser.cpp +++ /dev/null @@ -1,632 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#include "ProtocolParser.h" -#include "LookupCache.h" -#include "nsNetCID.h" -#include "prlog.h" -#include "prnetdb.h" -#include "prprf.h" - -#include "nsUrlClassifierUtils.h" - -// NSPR_LOG_MODULES=UrlClassifierDbService:5 -extern PRLogModuleInfo *gUrlClassifierDbServiceLog; -#if defined(PR_LOGGING) -#define LOG(args) PR_LOG(gUrlClassifierDbServiceLog, PR_LOG_DEBUG, args) -#define LOG_ENABLED() PR_LOG_TEST(gUrlClassifierDbServiceLog, 4) -#else -#define LOG(args) -#define LOG_ENABLED() (false) -#endif - -namespace mozilla { -namespace safebrowsing { - -// Updates will fail if fed chunks larger than this -const uint32_t MAX_CHUNK_SIZE = (1024 * 1024); - -const uint32_t DOMAIN_SIZE = 4; - -// Parse one stringified range of chunks of the form "n" or "n-m" from a -// comma-separated list of chunks. Upon return, 'begin' will point to the -// next range of chunks in the list of chunks. -static bool -ParseChunkRange(nsACString::const_iterator& aBegin, - const nsACString::const_iterator& aEnd, - uint32_t* aFirst, uint32_t* aLast) -{ - nsACString::const_iterator iter = aBegin; - FindCharInReadable(',', iter, aEnd); - - nsAutoCString element(Substring(aBegin, iter)); - aBegin = iter; - if (aBegin != aEnd) - aBegin++; - - uint32_t numRead = PR_sscanf(element.get(), "%u-%u", aFirst, aLast); - if (numRead == 2) { - if (*aFirst > *aLast) { - uint32_t tmp = *aFirst; - *aFirst = *aLast; - *aLast = tmp; - } - return true; - } - - if (numRead == 1) { - *aLast = *aFirst; - return true; - } - - return false; -} - -ProtocolParser::ProtocolParser() - : mState(PROTOCOL_STATE_CONTROL) - , mUpdateStatus(NS_OK) - , mUpdateWait(0) - , mResetRequested(false) -{ -} - -ProtocolParser::~ProtocolParser() -{ - CleanupUpdates(); -} - -nsresult -ProtocolParser::Init(nsICryptoHash* aHasher) -{ - mCryptoHash = aHasher; - return NS_OK; -} - -void -ProtocolParser::SetCurrentTable(const nsACString& aTable) -{ - mTableUpdate = GetTableUpdate(aTable); -} - -nsresult -ProtocolParser::AppendStream(const nsACString& aData) -{ - if (NS_FAILED(mUpdateStatus)) - return mUpdateStatus; - - nsresult rv; - mPending.Append(aData); - - bool done = false; - while (!done) { - if (mState == PROTOCOL_STATE_CONTROL) { - rv = ProcessControl(&done); - } else if (mState == PROTOCOL_STATE_CHUNK) { - rv = ProcessChunk(&done); - } else { - NS_ERROR("Unexpected protocol state"); - rv = NS_ERROR_FAILURE; - } - if (NS_FAILED(rv)) { - mUpdateStatus = rv; - return rv; - } - } - return NS_OK; -} - -nsresult -ProtocolParser::ProcessControl(bool* aDone) -{ - nsresult rv; - - nsAutoCString line; - *aDone = true; - while (NextLine(line)) { - //LOG(("Processing %s\n", line.get())); - - if (StringBeginsWith(line, NS_LITERAL_CSTRING("i:"))) { - // Set the table name from the table header line. - SetCurrentTable(Substring(line, 2)); - } else if (StringBeginsWith(line, NS_LITERAL_CSTRING("n:"))) { - if (PR_sscanf(line.get(), "n:%d", &mUpdateWait) != 1) { - LOG(("Error parsing n: '%s' (%d)", line.get(), mUpdateWait)); - mUpdateWait = 0; - } - } else if (line.EqualsLiteral("r:pleasereset")) { - mResetRequested = true; - } else if (StringBeginsWith(line, NS_LITERAL_CSTRING("u:"))) { - rv = ProcessForward(line); - NS_ENSURE_SUCCESS(rv, rv); - } else if (StringBeginsWith(line, NS_LITERAL_CSTRING("a:")) || - StringBeginsWith(line, NS_LITERAL_CSTRING("s:"))) { - rv = ProcessChunkControl(line); - NS_ENSURE_SUCCESS(rv, rv); - *aDone = false; - return NS_OK; - } else if (StringBeginsWith(line, NS_LITERAL_CSTRING("ad:")) || - StringBeginsWith(line, NS_LITERAL_CSTRING("sd:"))) { - rv = ProcessExpirations(line); - NS_ENSURE_SUCCESS(rv, rv); - } - } - - *aDone = true; - return NS_OK; -} - -nsresult -ProtocolParser::ProcessExpirations(const nsCString& aLine) -{ - if (!mTableUpdate) { - NS_WARNING("Got an expiration without a table."); - return NS_ERROR_FAILURE; - } - const nsCSubstring &list = Substring(aLine, 3); - nsACString::const_iterator begin, end; - list.BeginReading(begin); - list.EndReading(end); - while (begin != end) { - uint32_t first, last; - if (ParseChunkRange(begin, end, &first, &last)) { - for (uint32_t num = first; num <= last; num++) { - if (aLine[0] == 'a') - mTableUpdate->NewAddExpiration(num); - else - mTableUpdate->NewSubExpiration(num); - } - } else { - return NS_ERROR_FAILURE; - } - } - return NS_OK; -} - -nsresult -ProtocolParser::ProcessChunkControl(const nsCString& aLine) -{ - if (!mTableUpdate) { - NS_WARNING("Got a chunk before getting a table."); - return NS_ERROR_FAILURE; - } - - mState = PROTOCOL_STATE_CHUNK; - char command; - - mChunkState.Clear(); - - if (PR_sscanf(aLine.get(), - "%c:%d:%d:%d", - &command, - &mChunkState.num, &mChunkState.hashSize, &mChunkState.length) - != 4) - { - return NS_ERROR_FAILURE; - } - - if (mChunkState.length > MAX_CHUNK_SIZE) { - return NS_ERROR_FAILURE; - } - - if (!(mChunkState.hashSize == PREFIX_SIZE || mChunkState.hashSize == COMPLETE_SIZE)) { - NS_WARNING("Invalid hash size specified in update."); - return NS_ERROR_FAILURE; - } - - if (StringEndsWith(mTableUpdate->TableName(), - NS_LITERAL_CSTRING("-shavar")) || - StringEndsWith(mTableUpdate->TableName(), - NS_LITERAL_CSTRING("-simple"))) { - // Accommodate test tables ending in -simple for now. - mChunkState.type = (command == 'a') ? CHUNK_ADD : CHUNK_SUB; - } else if (StringEndsWith(mTableUpdate->TableName(), - NS_LITERAL_CSTRING("-digest256"))) { - LOG(("Processing digest256 data")); - mChunkState.type = (command == 'a') ? CHUNK_ADD_DIGEST : CHUNK_SUB_DIGEST; - } - switch (mChunkState.type) { - case CHUNK_ADD: - mTableUpdate->NewAddChunk(mChunkState.num); - break; - case CHUNK_SUB: - mTableUpdate->NewSubChunk(mChunkState.num); - break; - case CHUNK_ADD_DIGEST: - mTableUpdate->NewAddChunk(mChunkState.num); - break; - case CHUNK_SUB_DIGEST: - mTableUpdate->NewSubChunk(mChunkState.num); - break; - } - - return NS_OK; -} - -nsresult -ProtocolParser::ProcessForward(const nsCString& aLine) -{ - const nsCSubstring &forward = Substring(aLine, 2); - return AddForward(forward); -} - -nsresult -ProtocolParser::AddForward(const nsACString& aUrl) -{ - if (!mTableUpdate) { - NS_WARNING("Forward without a table name."); - return NS_ERROR_FAILURE; - } - - ForwardedUpdate *forward = mForwards.AppendElement(); - forward->table = mTableUpdate->TableName(); - forward->url.Assign(aUrl); - - return NS_OK; -} - -nsresult -ProtocolParser::ProcessChunk(bool* aDone) -{ - if (!mTableUpdate) { - NS_WARNING("Processing chunk without an active table."); - return NS_ERROR_FAILURE; - } - - NS_ASSERTION(mChunkState.num != 0, "Must have a chunk number."); - - if (mPending.Length() < mChunkState.length) { - *aDone = true; - return NS_OK; - } - - // Pull the chunk out of the pending stream data. - nsAutoCString chunk; - chunk.Assign(Substring(mPending, 0, mChunkState.length)); - mPending.Cut(0, mChunkState.length); - - *aDone = false; - mState = PROTOCOL_STATE_CONTROL; - - //LOG(("Handling a %d-byte chunk", chunk.Length())); - if (StringEndsWith(mTableUpdate->TableName(), - NS_LITERAL_CSTRING("-shavar"))) { - return ProcessShaChunk(chunk); - } - if (StringEndsWith(mTableUpdate->TableName(), - NS_LITERAL_CSTRING("-digest256"))) { - return ProcessDigestChunk(chunk); - } - return ProcessPlaintextChunk(chunk); -} - -/** - * Process a plaintext chunk (currently only used in unit tests). - */ -nsresult -ProtocolParser::ProcessPlaintextChunk(const nsACString& aChunk) -{ - if (!mTableUpdate) { - NS_WARNING("Chunk received with no table."); - return NS_ERROR_FAILURE; - } - - nsTArray lines; - ParseString(PromiseFlatCString(aChunk), '\n', lines); - - // non-hashed tables need to be hashed - for (uint32_t i = 0; i < lines.Length(); i++) { - nsCString& line = lines[i]; - - if (mChunkState.type == CHUNK_ADD) { - if (mChunkState.hashSize == COMPLETE_SIZE) { - Completion hash; - hash.FromPlaintext(line, mCryptoHash); - mTableUpdate->NewAddComplete(mChunkState.num, hash); - } else { - NS_ASSERTION(mChunkState.hashSize == 4, "Only 32- or 4-byte hashes can be used for add chunks."); - Prefix hash; - hash.FromPlaintext(line, mCryptoHash); - mTableUpdate->NewAddPrefix(mChunkState.num, hash); - } - } else { - nsCString::const_iterator begin, iter, end; - line.BeginReading(begin); - line.EndReading(end); - iter = begin; - uint32_t addChunk; - if (!FindCharInReadable(':', iter, end) || - PR_sscanf(lines[i].get(), "%d:", &addChunk) != 1) { - NS_WARNING("Received sub chunk without associated add chunk."); - return NS_ERROR_FAILURE; - } - iter++; - - if (mChunkState.hashSize == COMPLETE_SIZE) { - Completion hash; - hash.FromPlaintext(Substring(iter, end), mCryptoHash); - mTableUpdate->NewSubComplete(addChunk, hash, mChunkState.num); - } else { - NS_ASSERTION(mChunkState.hashSize == 4, "Only 32- or 4-byte hashes can be used for add chunks."); - Prefix hash; - hash.FromPlaintext(Substring(iter, end), mCryptoHash); - mTableUpdate->NewSubPrefix(addChunk, hash, mChunkState.num); - } - } - } - - return NS_OK; -} - -nsresult -ProtocolParser::ProcessShaChunk(const nsACString& aChunk) -{ - uint32_t start = 0; - while (start < aChunk.Length()) { - // First four bytes are the domain key. - Prefix domain; - domain.Assign(Substring(aChunk, start, DOMAIN_SIZE)); - start += DOMAIN_SIZE; - - // Then a count of entries. - uint8_t numEntries = static_cast(aChunk[start]); - start++; - - nsresult rv; - if (mChunkState.type == CHUNK_ADD && mChunkState.hashSize == PREFIX_SIZE) { - rv = ProcessHostAdd(domain, numEntries, aChunk, &start); - } else if (mChunkState.type == CHUNK_ADD && mChunkState.hashSize == COMPLETE_SIZE) { - rv = ProcessHostAddComplete(numEntries, aChunk, &start); - } else if (mChunkState.type == CHUNK_SUB && mChunkState.hashSize == PREFIX_SIZE) { - rv = ProcessHostSub(domain, numEntries, aChunk, &start); - } else if (mChunkState.type == CHUNK_SUB && mChunkState.hashSize == COMPLETE_SIZE) { - rv = ProcessHostSubComplete(numEntries, aChunk, &start); - } else { - NS_WARNING("Unexpected chunk type/hash size!"); - LOG(("Got an unexpected chunk type/hash size: %s:%d", - mChunkState.type == CHUNK_ADD ? "add" : "sub", - mChunkState.hashSize)); - return NS_ERROR_FAILURE; - } - NS_ENSURE_SUCCESS(rv, rv); - } - - return NS_OK; -} - -nsresult -ProtocolParser::ProcessDigestChunk(const nsACString& aChunk) -{ - if (mChunkState.type == CHUNK_ADD_DIGEST) { - return ProcessDigestAdd(aChunk); - } - if (mChunkState.type == CHUNK_SUB_DIGEST) { - return ProcessDigestSub(aChunk); - } - return NS_ERROR_UNEXPECTED; -} - -nsresult -ProtocolParser::ProcessDigestAdd(const nsACString& aChunk) -{ - // The ABNF format for add chunks is (HASH)+, where HASH is 32 bytes. - MOZ_ASSERT(aChunk.Length() % 32 == 0, - "Chunk length in bytes must be divisible by 4"); - uint32_t start = 0; - while (start < aChunk.Length()) { - Completion hash; - hash.Assign(Substring(aChunk, start, COMPLETE_SIZE)); - start += COMPLETE_SIZE; - mTableUpdate->NewAddComplete(mChunkState.num, hash); - } - return NS_OK; -} - -nsresult -ProtocolParser::ProcessDigestSub(const nsACString& aChunk) -{ - // The ABNF format for sub chunks is (ADDCHUNKNUM HASH)+, where ADDCHUNKNUM - // is a 4 byte chunk number, and HASH is 32 bytes. - MOZ_ASSERT(aChunk.Length() % 36 == 0, - "Chunk length in bytes must be divisible by 36"); - uint32_t start = 0; - while (start < aChunk.Length()) { - // Read ADDCHUNKNUM - const nsCSubstring& addChunkStr = Substring(aChunk, start, 4); - start += 4; - - uint32_t addChunk; - memcpy(&addChunk, addChunkStr.BeginReading(), 4); - addChunk = PR_ntohl(addChunk); - - // Read the hash - Completion hash; - hash.Assign(Substring(aChunk, start, COMPLETE_SIZE)); - start += COMPLETE_SIZE; - - mTableUpdate->NewSubComplete(addChunk, hash, mChunkState.num); - } - return NS_OK; -} - -nsresult -ProtocolParser::ProcessHostAdd(const Prefix& aDomain, uint8_t aNumEntries, - const nsACString& aChunk, uint32_t* aStart) -{ - NS_ASSERTION(mChunkState.hashSize == PREFIX_SIZE, - "ProcessHostAdd should only be called for prefix hashes."); - - if (aNumEntries == 0) { - mTableUpdate->NewAddPrefix(mChunkState.num, aDomain); - return NS_OK; - } - - if (*aStart + (PREFIX_SIZE * aNumEntries) > aChunk.Length()) { - NS_WARNING("Chunk is not long enough to contain the expected entries."); - return NS_ERROR_FAILURE; - } - - for (uint8_t i = 0; i < aNumEntries; i++) { - Prefix hash; - hash.Assign(Substring(aChunk, *aStart, PREFIX_SIZE)); - mTableUpdate->NewAddPrefix(mChunkState.num, hash); - *aStart += PREFIX_SIZE; - } - - return NS_OK; -} - -nsresult -ProtocolParser::ProcessHostSub(const Prefix& aDomain, uint8_t aNumEntries, - const nsACString& aChunk, uint32_t *aStart) -{ - NS_ASSERTION(mChunkState.hashSize == PREFIX_SIZE, - "ProcessHostSub should only be called for prefix hashes."); - - if (aNumEntries == 0) { - if ((*aStart) + 4 > aChunk.Length()) { - NS_WARNING("Received a zero-entry sub chunk without an associated add."); - return NS_ERROR_FAILURE; - } - - const nsCSubstring& addChunkStr = Substring(aChunk, *aStart, 4); - *aStart += 4; - - uint32_t addChunk; - memcpy(&addChunk, addChunkStr.BeginReading(), 4); - addChunk = PR_ntohl(addChunk); - - mTableUpdate->NewSubPrefix(addChunk, aDomain, mChunkState.num); - return NS_OK; - } - - if (*aStart + ((PREFIX_SIZE + 4) * aNumEntries) > aChunk.Length()) { - NS_WARNING("Chunk is not long enough to contain the expected entries."); - return NS_ERROR_FAILURE; - } - - for (uint8_t i = 0; i < aNumEntries; i++) { - const nsCSubstring& addChunkStr = Substring(aChunk, *aStart, 4); - *aStart += 4; - - uint32_t addChunk; - memcpy(&addChunk, addChunkStr.BeginReading(), 4); - addChunk = PR_ntohl(addChunk); - - Prefix prefix; - prefix.Assign(Substring(aChunk, *aStart, PREFIX_SIZE)); - *aStart += PREFIX_SIZE; - - mTableUpdate->NewSubPrefix(addChunk, prefix, mChunkState.num); - } - - return NS_OK; -} - -nsresult -ProtocolParser::ProcessHostAddComplete(uint8_t aNumEntries, - const nsACString& aChunk, uint32_t* aStart) -{ - NS_ASSERTION(mChunkState.hashSize == COMPLETE_SIZE, - "ProcessHostAddComplete should only be called for complete hashes."); - - if (aNumEntries == 0) { - // this is totally comprehensible. - // My sarcasm detector is going off! - NS_WARNING("Expected > 0 entries for a 32-byte hash add."); - return NS_OK; - } - - if (*aStart + (COMPLETE_SIZE * aNumEntries) > aChunk.Length()) { - NS_WARNING("Chunk is not long enough to contain the expected entries."); - return NS_ERROR_FAILURE; - } - - for (uint8_t i = 0; i < aNumEntries; i++) { - Completion hash; - hash.Assign(Substring(aChunk, *aStart, COMPLETE_SIZE)); - mTableUpdate->NewAddComplete(mChunkState.num, hash); - *aStart += COMPLETE_SIZE; - } - - return NS_OK; -} - -nsresult -ProtocolParser::ProcessHostSubComplete(uint8_t aNumEntries, - const nsACString& aChunk, uint32_t* aStart) -{ - NS_ASSERTION(mChunkState.hashSize == COMPLETE_SIZE, - "ProcessHostSubComplete should only be called for complete hashes."); - - if (aNumEntries == 0) { - // this is totally comprehensible. - NS_WARNING("Expected > 0 entries for a 32-byte hash sub."); - return NS_OK; - } - - if (*aStart + ((COMPLETE_SIZE + 4) * aNumEntries) > aChunk.Length()) { - NS_WARNING("Chunk is not long enough to contain the expected entries."); - return NS_ERROR_FAILURE; - } - - for (uint8_t i = 0; i < aNumEntries; i++) { - Completion hash; - hash.Assign(Substring(aChunk, *aStart, COMPLETE_SIZE)); - *aStart += COMPLETE_SIZE; - - const nsCSubstring& addChunkStr = Substring(aChunk, *aStart, 4); - *aStart += 4; - - uint32_t addChunk; - memcpy(&addChunk, addChunkStr.BeginReading(), 4); - addChunk = PR_ntohl(addChunk); - - mTableUpdate->NewSubComplete(addChunk, hash, mChunkState.num); - } - - return NS_OK; -} - -bool -ProtocolParser::NextLine(nsACString& aLine) -{ - int32_t newline = mPending.FindChar('\n'); - if (newline == kNotFound) { - return false; - } - aLine.Assign(Substring(mPending, 0, newline)); - mPending.Cut(0, newline + 1); - return true; -} - -void -ProtocolParser::CleanupUpdates() -{ - for (uint32_t i = 0; i < mTableUpdates.Length(); i++) { - delete mTableUpdates[i]; - } - mTableUpdates.Clear(); -} - -TableUpdate * -ProtocolParser::GetTableUpdate(const nsACString& aTable) -{ - for (uint32_t i = 0; i < mTableUpdates.Length(); i++) { - if (aTable.Equals(mTableUpdates[i]->TableName())) { - return mTableUpdates[i]; - } - } - - // We free automatically on destruction, ownership of these - // updates can be transferred to DBServiceWorker, which passes - // them back to Classifier when doing the updates, and that - // will free them. - TableUpdate *update = new TableUpdate(aTable); - mTableUpdates.AppendElement(update); - return update; -} - -} // namespace safebrowsing -} // namespace mozilla diff --git a/toolkit/components/url-classifier/ProtocolParser.h b/toolkit/components/url-classifier/ProtocolParser.h deleted file mode 100644 index c44e263ec3..0000000000 --- a/toolkit/components/url-classifier/ProtocolParser.h +++ /dev/null @@ -1,119 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#ifndef ProtocolParser_h__ -#define ProtocolParser_h__ - -#include "HashStore.h" -#include "nsICryptoHMAC.h" - -namespace mozilla { -namespace safebrowsing { - -/** - * Some helpers for parsing the safe - */ -class ProtocolParser { -public: - struct ForwardedUpdate { - nsCString table; - nsCString url; - }; - - ProtocolParser(); - ~ProtocolParser(); - - nsresult Status() const { return mUpdateStatus; } - - nsresult Init(nsICryptoHash* aHasher); - - void SetCurrentTable(const nsACString& aTable); - - nsresult Begin(); - nsresult AppendStream(const nsACString& aData); - - // Forget the table updates that were created by this pass. It - // becomes the caller's responsibility to free them. This is shitty. - TableUpdate *GetTableUpdate(const nsACString& aTable); - void ForgetTableUpdates() { mTableUpdates.Clear(); } - nsTArray &GetTableUpdates() { return mTableUpdates; } - - // Update information. - const nsTArray &Forwards() const { return mForwards; } - int32_t UpdateWait() { return mUpdateWait; } - bool ResetRequested() { return mResetRequested; } - -private: - nsresult ProcessControl(bool* aDone); - nsresult ProcessExpirations(const nsCString& aLine); - nsresult ProcessChunkControl(const nsCString& aLine); - nsresult ProcessForward(const nsCString& aLine); - nsresult AddForward(const nsACString& aUrl); - nsresult ProcessChunk(bool* done); - // Remove this, it's only used for testing - nsresult ProcessPlaintextChunk(const nsACString& aChunk); - nsresult ProcessShaChunk(const nsACString& aChunk); - nsresult ProcessHostAdd(const Prefix& aDomain, uint8_t aNumEntries, - const nsACString& aChunk, uint32_t* aStart); - nsresult ProcessHostSub(const Prefix& aDomain, uint8_t aNumEntries, - const nsACString& aChunk, uint32_t* aStart); - nsresult ProcessHostAddComplete(uint8_t aNumEntries, const nsACString& aChunk, - uint32_t *aStart); - nsresult ProcessHostSubComplete(uint8_t numEntries, const nsACString& aChunk, - uint32_t* start); - // Digest chunks are very similar to shavar chunks, except digest chunks - // always contain the full hash, so there is no need for chunk data to - // contain prefix sizes. - nsresult ProcessDigestChunk(const nsACString& aChunk); - nsresult ProcessDigestAdd(const nsACString& aChunk); - nsresult ProcessDigestSub(const nsACString& aChunk); - bool NextLine(nsACString& aLine); - - void CleanupUpdates(); - - enum ParserState { - PROTOCOL_STATE_CONTROL, - PROTOCOL_STATE_CHUNK - }; - ParserState mState; - - enum ChunkType { - // Types for shavar tables. - CHUNK_ADD, - CHUNK_SUB, - // Types for digest256 tables. digest256 tables differ in format from - // shavar tables since they only contain complete hashes. - CHUNK_ADD_DIGEST, - CHUNK_SUB_DIGEST - }; - - struct ChunkState { - ChunkType type; - uint32_t num; - uint32_t hashSize; - uint32_t length; - void Clear() { num = 0; hashSize = 0; length = 0; } - }; - ChunkState mChunkState; - - nsCOMPtr mCryptoHash; - - nsresult mUpdateStatus; - nsCString mPending; - - uint32_t mUpdateWait; - bool mResetRequested; - - nsTArray mForwards; - // Keep track of updates to apply before passing them to the DBServiceWorkers. - nsTArray mTableUpdates; - // Updates to apply to the current table being parsed. - TableUpdate *mTableUpdate; -}; - -} -} - -#endif diff --git a/toolkit/components/url-classifier/SafeBrowsing.jsm b/toolkit/components/url-classifier/SafeBrowsing.jsm deleted file mode 100644 index 2449ae6c6a..0000000000 --- a/toolkit/components/url-classifier/SafeBrowsing.jsm +++ /dev/null @@ -1,235 +0,0 @@ -/* 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/. */ - -this.EXPORTED_SYMBOLS = ["SafeBrowsing"]; - -const Cc = Components.classes; -const Ci = Components.interfaces; -const Cu = Components.utils; - -Cu.import("resource://gre/modules/Services.jsm"); - -// Skip all the ones containining "test", because we never need to ask for -// updates for them. -function getLists(prefName) { - let pref = Services.prefs.getCharPref(prefName); - // Splitting an empty string returns [''], we really want an empty array. - if (!pref) { - return []; - } - return pref.split(",") - .filter(function(value) { return value.indexOf("test-") == -1; }) - .map(function(value) { return value.trim(); }); -} - -// These may be a comma-separated lists of tables. -const phishingLists = getLists("urlclassifier.phishTable"); -const malwareLists = getLists("urlclassifier.malwareTable"); -const downloadBlockLists = getLists("urlclassifier.downloadBlockTable"); -const downloadAllowLists = getLists("urlclassifier.downloadAllowTable"); -const trackingProtectionLists = getLists("urlclassifier.trackingTable"); - -var debug = false; -function log(...stuff) { - if (!debug) - return; - - var d = new Date(); - let msg = "SafeBrowsing: " + d.toTimeString() + ": " + stuff.join(" "); - Services.console.logStringMessage(msg); - dump(msg + "\n"); -} - -this.SafeBrowsing = { - - init: function() { - if (this.initialized) { - log("Already initialized"); - return; - } - - Services.prefs.addObserver("browser.safebrowsing", this.readPrefs.bind(this), false); - Services.prefs.addObserver("privacy.trackingprotection", this.readPrefs.bind(this), false); - this.readPrefs(); - - // Register our two types of tables, and add custom Mozilla entries - let listManager = Cc["@mozilla.org/url-classifier/listmanager;1"]. - getService(Ci.nsIUrlListManager); - for (let i = 0; i < phishingLists.length; ++i) { - listManager.registerTable(phishingLists[i], this.updateURL, this.gethashURL); - } - for (let i = 0; i < malwareLists.length; ++i) { - listManager.registerTable(malwareLists[i], this.updateURL, this.gethashURL); - } - for (let i = 0; i < downloadBlockLists.length; ++i) { - listManager.registerTable(downloadBlockLists[i], this.updateURL, this.gethashURL); - } - for (let i = 0; i < downloadAllowLists.length; ++i) { - listManager.registerTable(downloadAllowLists[i], this.updateURL, this.gethashURL); - } - for (let i = 0; i < trackingProtectionLists.length; ++i) { - listManager.registerTable(trackingProtectionLists[i], - this.trackingUpdateURL, - this.trackingGethashURL); - } - this.addMozEntries(); - - this.controlUpdateChecking(); - this.initialized = true; - - log("init() finished"); - }, - - - initialized: false, - phishingEnabled: false, - malwareEnabled: false, - - updateURL: null, - gethashURL: null, - - reportURL: null, - reportGenericURL: null, - reportErrorURL: null, - reportPhishURL: null, - reportMalwareURL: null, - reportMalwareErrorURL: null, - - - getReportURL: function(kind) { - return this["report" + kind + "URL"]; - }, - - - readPrefs: function() { - log("reading prefs"); - - debug = Services.prefs.getBoolPref("browser.safebrowsing.debug"); - this.phishingEnabled = Services.prefs.getBoolPref("browser.safebrowsing.enabled"); - this.malwareEnabled = Services.prefs.getBoolPref("browser.safebrowsing.malware.enabled"); - this.trackingEnabled = Services.prefs.getBoolPref("privacy.trackingprotection.enabled"); - this.updateProviderURLs(); - - // XXX The listManager backend gets confused if this is called before the - // lists are registered. So only call it here when a pref changes, and not - // when doing initialization. I expect to refactor this later, so pardon the hack. - if (this.initialized) { - this.controlUpdateChecking(); - } - }, - - - updateProviderURLs: function() { - try { - var clientID = Services.prefs.getCharPref("browser.safebrowsing.id"); - } catch(e) { - var clientID = Services.appinfo.name; - } - - log("initializing safe browsing URLs, client id ", clientID); - let basePref = "browser.safebrowsing."; - - // Urls to HTML report pages - this.reportURL = Services.urlFormatter.formatURLPref(basePref + "reportURL"); - this.reportGenericURL = Services.urlFormatter.formatURLPref(basePref + "reportGenericURL"); - this.reportErrorURL = Services.urlFormatter.formatURLPref(basePref + "reportErrorURL"); - this.reportPhishURL = Services.urlFormatter.formatURLPref(basePref + "reportPhishURL"); - this.reportMalwareURL = Services.urlFormatter.formatURLPref(basePref + "reportMalwareURL"); - this.reportMalwareErrorURL = Services.urlFormatter.formatURLPref(basePref + "reportMalwareErrorURL"); - - // Urls used to update DB - this.updateURL = Services.urlFormatter.formatURLPref(basePref + "updateURL"); - this.gethashURL = Services.urlFormatter.formatURLPref(basePref + "gethashURL"); - - this.updateURL = this.updateURL.replace("SAFEBROWSING_ID", clientID); - this.gethashURL = this.gethashURL.replace("SAFEBROWSING_ID", clientID); - this.trackingUpdateURL = Services.urlFormatter.formatURLPref( - "browser.trackingprotection.updateURL"); - this.trackingGethashURL = Services.urlFormatter.formatURLPref( - "browser.trackingprotection.gethashURL"); - }, - - controlUpdateChecking: function() { - log("phishingEnabled:", this.phishingEnabled, "malwareEnabled:", - this.malwareEnabled, "trackingEnabled:", this.trackingEnabled); - - let listManager = Cc["@mozilla.org/url-classifier/listmanager;1"]. - getService(Ci.nsIUrlListManager); - - for (let i = 0; i < phishingLists.length; ++i) { - if (this.phishingEnabled) { - listManager.enableUpdate(phishingLists[i]); - } else { - listManager.disableUpdate(phishingLists[i]); - } - } - for (let i = 0; i < malwareLists.length; ++i) { - if (this.malwareEnabled) { - listManager.enableUpdate(malwareLists[i]); - } else { - listManager.disableUpdate(malwareLists[i]); - } - } - for (let i = 0; i < downloadBlockLists.length; ++i) { - if (this.malwareEnabled) { - listManager.enableUpdate(downloadBlockLists[i]); - } else { - listManager.disableUpdate(downloadBlockLists[i]); - } - } - for (let i = 0; i < downloadAllowLists.length; ++i) { - if (this.malwareEnabled) { - listManager.enableUpdate(downloadAllowLists[i]); - } else { - listManager.disableUpdate(downloadAllowLists[i]); - } - } - for (let i = 0; i < trackingProtectionLists.length; ++i) { - if (this.trackingEnabled) { - listManager.enableUpdate(trackingProtectionLists[i]); - } else { - listManager.disableUpdate(trackingProtectionLists[i]); - } - } - listManager.maybeToggleUpdateChecking(); - }, - - - addMozEntries: function() { - // Add test entries to the DB. - // XXX bug 779008 - this could be done by DB itself? - const phishURL = "itisatrap.org/firefox/its-a-trap.html"; - const malwareURL = "itisatrap.org/firefox/its-an-attack.html"; - - let update = "n:1000\ni:test-malware-simple\nad:1\n" + - "a:1:32:" + malwareURL.length + "\n" + - malwareURL; - update += "n:1000\ni:test-phish-simple\nad:1\n" + - "a:1:32:" + phishURL.length + "\n" + - phishURL; - log("addMozEntries:", update); - - let db = Cc["@mozilla.org/url-classifier/dbservice;1"]. - getService(Ci.nsIUrlClassifierDBService); - - // nsIUrlClassifierUpdateObserver - let dummyListener = { - updateUrlRequested: function() { }, - streamFinished: function() { }, - updateError: function() { }, - updateSuccess: function() { } - }; - - try { - db.beginUpdate(dummyListener, "test-malware-simple,test-phish-simple", ""); - db.beginStream("", ""); - db.updateStream(update); - db.finishStream(); - db.finishUpdate(); - } catch(ex) { - // beginUpdate will throw harmlessly if there's an existing update in progress, ignore failures. - log("addMozEntries failed!", ex); - } - }, -}; diff --git a/toolkit/components/url-classifier/content/listmanager.js b/toolkit/components/url-classifier/content/listmanager.js deleted file mode 100644 index 2f6cd15177..0000000000 --- a/toolkit/components/url-classifier/content/listmanager.js +++ /dev/null @@ -1,458 +0,0 @@ -# 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/. - -const Cu = Components.utils; -Cu.import("resource://gre/modules/Services.jsm"); - -// This is the only implementation of nsIUrlListManager. -// A class that manages lists, namely white and black lists for -// phishing or malware protection. The ListManager knows how to fetch, -// update, and store lists. -// -// There is a single listmanager for the whole application. -// -// TODO more comprehensive update tests, for example add unittest check -// that the listmanagers tables are properly written on updates - -// Log only if browser.safebrowsing.debug is true -this.log = function log(...stuff) { - var prefs_ = new G_Preferences(); - var debug = prefs_.getPref("browser.safebrowsing.debug"); - if (!debug) { - return; - } - - var d = new Date(); - let msg = "listmanager: " + d.toTimeString() + ": " + stuff.join(" "); - Services.console.logStringMessage(msg); - dump(msg + "\n"); -} - -this.QueryAdapter = function QueryAdapter(callback) { - this.callback_ = callback; -}; - -QueryAdapter.prototype.handleResponse = function(value) { - this.callback_.handleEvent(value); -} - -/** - * A ListManager keeps track of black and white lists and knows - * how to update them. - * - * @constructor - */ -this.PROT_ListManager = function PROT_ListManager() { - log("Initializing list manager"); - this.prefs_ = new G_Preferences(); - this.updateInterval = this.prefs_.getPref("urlclassifier.updateinterval", 30 * 60) * 1000; - - // A map of tableNames to objects of type - // { updateUrl: , gethashUrl: } - this.tablesData = {}; - // A map of updateUrls to maps of tables requiring updates, e.g. - // { safebrowsing-update-url: { goog-phish-shavar: true, - // goog-malware-shavar: true } - this.needsUpdate_ = {}; - - this.observerServiceObserver_ = new G_ObserverServiceObserver( - 'quit-application', - BindToObject(this.shutdown_, this), - true /*only once*/); - - // A map of updateUrls to single-use G_Alarms. An entry exists if and only if - // there is at least one table with updates enabled for that url. G_Alarms - // are reset when enabling/disabling updates or on update callbacks (update - // success, update failure, download error). - this.updateCheckers_ = {}; - this.requestBackoffs_ = {}; - this.dbService_ = Cc["@mozilla.org/url-classifier/dbservice;1"] - .getService(Ci.nsIUrlClassifierDBService); - - - this.hashCompleter_ = Cc["@mozilla.org/url-classifier/hashcompleter;1"] - .getService(Ci.nsIUrlClassifierHashCompleter); -} - -/** - * xpcom-shutdown callback - * Delete all of our data tables which seem to leak otherwise. - */ -PROT_ListManager.prototype.shutdown_ = function() { - for (var name in this.tablesData) { - delete this.tablesData[name]; - } -} - -/** - * Register a new table table - * @param tableName - the name of the table - * @param updateUrl - the url for updating the table - * @param gethashUrl - the url for fetching hash completions - * @returns true if the table could be created; false otherwise - */ -PROT_ListManager.prototype.registerTable = function(tableName, - updateUrl, - gethashUrl) { - log("registering " + tableName + " with " + updateUrl); - if (!updateUrl) { - log("Can't register table " + tableName + " without updateUrl"); - return false; - } - this.tablesData[tableName] = {}; - this.tablesData[tableName].updateUrl = updateUrl; - this.tablesData[tableName].gethashUrl = gethashUrl; - - // Keep track of all of our update URLs. - if (!this.needsUpdate_[updateUrl]) { - this.needsUpdate_[updateUrl] = {}; - /* Backoff interval should be between 30 and 60 minutes. */ - var backoffInterval = 30 * 60 * 1000; - backoffInterval += Math.floor(Math.random() * (30 * 60 * 1000)); - - log("Creating request backoff for " + updateUrl); - this.requestBackoffs_[updateUrl] = new RequestBackoff(2 /* max errors */, - 60*1000 /* retry interval, 1 min */, - 4 /* num requests */, - 60*60*1000 /* request time, 60 min */, - backoffInterval /* backoff interval, 60 min */, - 8*60*60*1000 /* max backoff, 8hr */); - - } - this.needsUpdate_[updateUrl][tableName] = false; - - return true; -} - -PROT_ListManager.prototype.getGethashUrl = function(tableName) { - if (this.tablesData[tableName] && this.tablesData[tableName].gethashUrl) { - return this.tablesData[tableName].gethashUrl; - } - return ""; -} - -/** - * Enable updates for some tables - * @param tables - an array of table names that need updating - */ -PROT_ListManager.prototype.enableUpdate = function(tableName) { - var table = this.tablesData[tableName]; - if (table) { - log("Enabling table updates for " + tableName); - this.needsUpdate_[table.updateUrl][tableName] = true; - } -} - -/** - * Returns true if any table associated with the updateUrl requires updates. - * @param updateUrl - the updateUrl - */ -PROT_ListManager.prototype.updatesNeeded_ = function(updateUrl) { - let updatesNeeded = false; - for (var tableName in this.needsUpdate_[updateUrl]) { - if (this.needsUpdate_[updateUrl][tableName]) { - updatesNeeded = true; - } - } - return updatesNeeded; -} - -/** - * Disables updates for some tables - * @param tables - an array of table names that no longer need updating - */ -PROT_ListManager.prototype.disableUpdate = function(tableName) { - var table = this.tablesData[tableName]; - if (table) { - log("Disabling table updates for " + tableName); - this.needsUpdate_[table.updateUrl][tableName] = false; - if (!this.updatesNeeded_(table.updateUrl) && - this.updateCheckers_[table.updateUrl]) { - this.updateCheckers_[table.updateUrl].cancel(); - this.updateCheckers_[table.updateUrl] = null; - } - } -} - -/** - * Determine if we have some tables that need updating. - */ -PROT_ListManager.prototype.requireTableUpdates = function() { - for (var name in this.tablesData) { - // Tables that need updating even if other tables don't require it - if (this.needsUpdate_[this.tablesData[name].updateUrl][name]) { - return true; - } - } - - return false; -} - -/** - * Acts as a nsIUrlClassifierCallback for getTables. - */ -PROT_ListManager.prototype.kickoffUpdate_ = function (onDiskTableData) -{ - this.startingUpdate_ = false; - var initialUpdateDelay = 3000; - - // If the user has never downloaded tables, do the check now. - log("needsUpdate: " + JSON.stringify(this.needsUpdate_, undefined, 2)); - for (var updateUrl in this.needsUpdate_) { - // If we haven't already kicked off updates for this updateUrl, set a - // non-repeating timer for it. The timer delay will be reset either on - // updateSuccess to this.updateinterval, or backed off on downloadError. - // Don't set the updateChecker unless at least one table has updates - // enabled. - if (this.updatesNeeded_(updateUrl) && !this.updateCheckers_[updateUrl]) { - log("Initializing update checker for " + updateUrl); - this.updateCheckers_[updateUrl] = - new G_Alarm(BindToObject(this.checkForUpdates, this, updateUrl), - initialUpdateDelay, false /* repeating */); - } else { - log("No updates needed or already initialized for " + updateUrl); - } - } -} - -PROT_ListManager.prototype.stopUpdateCheckers = function() { - log("Stopping updates"); - for (var updateUrl in this.updateCheckers_) { - this.updateCheckers_[updateUrl].cancel(); - this.updateCheckers_[updateUrl] = null; - } -} - -/** - * Determine if we have any tables that require updating. Different - * Wardens may call us with new tables that need to be updated. - */ -PROT_ListManager.prototype.maybeToggleUpdateChecking = function() { - // We update tables if we have some tables that want updates. If there - // are no tables that want to be updated - we dont need to check anything. - if (this.requireTableUpdates()) { - log("Starting managing lists"); - - // Get the list of existing tables from the DBService before making any - // update requests. - if (!this.startingUpdate_) { - this.startingUpdate_ = true; - // check the current state of tables in the database - this.dbService_.getTables(BindToObject(this.kickoffUpdate_, this)); - } - } else { - log("Stopping managing lists (if currently active)"); - this.stopUpdateCheckers(); // Cancel pending updates - } -} - -/** - * Provides an exception free way to look up the data in a table. We - * use this because at certain points our tables might not be loaded, - * and querying them could throw. - * - * @param table String Name of the table that we want to consult - * @param key Principal being used to lookup the database - * @param callback nsIUrlListManagerCallback (ie., Function) given false or the - * value in the table corresponding to key. If the table name does not - * exist, we return false, too. - */ -PROT_ListManager.prototype.safeLookup = function(key, callback) { - try { - log("safeLookup: " + key); - var cb = new QueryAdapter(callback); - this.dbService_.lookup(key, - BindToObject(cb.handleResponse, cb), - true); - } catch(e) { - log("safeLookup masked failure for key " + key + ": " + e); - callback.handleEvent(""); - } -} - -/** - * Updates our internal tables from the update server - * - * @param updateUrl: request updates for tables associated with that url, or - * for all tables if the url is empty. - */ -PROT_ListManager.prototype.checkForUpdates = function(updateUrl) { - log("checkForUpdates with " + updateUrl); - // See if we've triggered the request backoff logic. - if (!updateUrl) { - return false; - } - if (!this.requestBackoffs_[updateUrl] || - !this.requestBackoffs_[updateUrl].canMakeRequest()) { - log("Can't make update request"); - return false; - } - // Grab the current state of the tables from the database - this.dbService_.getTables(BindToObject(this.makeUpdateRequest_, this, - updateUrl)); - return true; -} - -/** - * Method that fires the actual HTTP update request. - * First we reset any tables that have disappeared. - * @param tableData List of table data already in the database, in the form - * tablename;\n - */ -PROT_ListManager.prototype.makeUpdateRequest_ = function(updateUrl, tableData) { - log("this.tablesData: " + JSON.stringify(this.tablesData, undefined, 2)); - log("existing chunks: " + tableData + "\n"); - // Disallow blank updateUrls - if (!updateUrl) { - return; - } - // An object of the form - // { tableList: comma-separated list of tables to request, - // tableNames: map of tables that need updating, - // request: list of tables and existing chunk ranges from tableData - // } - var streamerMap = { tableList: null, tableNames: {}, request: "" }; - for (var tableName in this.tablesData) { - // Skip tables not matching this update url - if (this.tablesData[tableName].updateUrl != updateUrl) { - continue; - } - if (this.needsUpdate_[this.tablesData[tableName].updateUrl][tableName]) { - streamerMap.tableNames[tableName] = true; - } - if (!streamerMap.tableList) { - streamerMap.tableList = tableName; - } else { - streamerMap.tableList += "," + tableName; - } - } - // Build the request. For each table already in the database, include the - // chunk data from the database - var lines = tableData.split("\n"); - for (var i = 0; i < lines.length; i++) { - var fields = lines[i].split(";"); - var name = fields[0]; - if (streamerMap.tableNames[name]) { - streamerMap.request += lines[i] + "\n"; - delete streamerMap.tableNames[name]; - } - } - // For each requested table that didn't have chunk data in the database, - // request it fresh - for (let tableName in streamerMap.tableNames) { - streamerMap.request += tableName + ";\n"; - } - - log("update request: " + JSON.stringify(streamerMap, undefined, 2) + "\n"); - - // Don't send an empty request. - if (streamerMap.request.length > 0) { - this.makeUpdateRequestForEntry_(updateUrl, streamerMap.tableList, - streamerMap.request); - } else { - // We were disabled between kicking off getTables and now. - log("Not sending empty request"); - } -} - -PROT_ListManager.prototype.makeUpdateRequestForEntry_ = function(updateUrl, - tableList, - request) { - log("makeUpdateRequestForEntry_: request " + request + - " update: " + updateUrl + " tablelist: " + tableList + "\n"); - var streamer = Cc["@mozilla.org/url-classifier/streamupdater;1"] - .getService(Ci.nsIUrlClassifierStreamUpdater); - - this.requestBackoffs_[updateUrl].noteRequest(); - - if (!streamer.downloadUpdates( - tableList, - request, - updateUrl, - BindToObject(this.updateSuccess_, this, tableList, updateUrl), - BindToObject(this.updateError_, this, tableList, updateUrl), - BindToObject(this.downloadError_, this, tableList, updateUrl))) { - // Our alarm gets reset in one of the 3 callbacks. - log("pending update, queued request until later"); - } -} - -/** - * Callback function if the update request succeeded. - * @param waitForUpdate String The number of seconds that the client should - * wait before requesting again. - */ -PROT_ListManager.prototype.updateSuccess_ = function(tableList, updateUrl, - waitForUpdate) { - log("update success for " + tableList + " from " + updateUrl + ": " + - waitForUpdate + "\n"); - var delay; - if (waitForUpdate) { - delay = parseInt(waitForUpdate, 10); - } - // As long as the delay is something sane (5 minutes or more), update - // our delay time for requesting updates. We always use a non-repeating - // timer since the delay is set differently at every callback. - if (delay >= (5 * 60)) { - log("Waiting " + delay + " seconds"); - delay = delay * 1000; - } else { - log("Ignoring delay from server, waiting " + this.updateInterval / 1000); - delay = this.updateInterval; - } - this.updateCheckers_[updateUrl] = - new G_Alarm(BindToObject(this.checkForUpdates, this, updateUrl), - delay, false); - - // Let the backoff object know that we completed successfully. - this.requestBackoffs_[updateUrl].noteServerResponse(200); -} - -/** - * Callback function if the update request succeeded. - * @param result String The error code of the failure - */ -PROT_ListManager.prototype.updateError_ = function(table, updateUrl, result) { - log("update error for " + table + " from " + updateUrl + ": " + result + "\n"); - // There was some trouble applying the updates. Don't try again for at least - // updateInterval seconds. - this.updateCheckers_[updateUrl] = - new G_Alarm(BindToObject(this.checkForUpdates, this, updateUrl), - this.updateInterval, false); -} - -/** - * Callback function when the download failed - * @param status String http status or an empty string if connection refused. - */ -PROT_ListManager.prototype.downloadError_ = function(table, updateUrl, status) { - log("download error for " + table + ": " + status + "\n"); - // If status is empty, then we assume that we got an NS_CONNECTION_REFUSED - // error. In this case, we treat this is a http 500 error. - if (!status) { - status = 500; - } - status = parseInt(status, 10); - this.requestBackoffs_[updateUrl].noteServerResponse(status); - var delay = this.updateInterval; - if (this.requestBackoffs_[updateUrl].isErrorStatus(status)) { - // Schedule an update for when our backoff is complete - delay = this.requestBackoffs_[updateUrl].nextRequestDelay(); - } else { - log("Got non error status for error callback?!"); - } - this.updateCheckers_[updateUrl] = - new G_Alarm(BindToObject(this.checkForUpdates, this, updateUrl), - delay, false); - -} - -PROT_ListManager.prototype.QueryInterface = function(iid) { - if (iid.equals(Ci.nsISupports) || - iid.equals(Ci.nsIUrlListManager) || - iid.equals(Ci.nsITimerCallback)) - return this; - - throw Components.results.NS_ERROR_NO_INTERFACE; -} diff --git a/toolkit/components/url-classifier/content/moz/alarm.js b/toolkit/components/url-classifier/content/moz/alarm.js deleted file mode 100644 index 7de0675461..0000000000 --- a/toolkit/components/url-classifier/content/moz/alarm.js +++ /dev/null @@ -1,157 +0,0 @@ -# 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/. - - -// An Alarm fires a callback after a certain amount of time, or at -// regular intervals. It's a convenient replacement for -// setTimeout/Interval when you don't want to bind to a specific -// window. -// -// The ConditionalAlarm is an Alarm that cancels itself if its callback -// returns a value that type-converts to true. -// -// Example: -// -// function foo() { dump('hi'); }; -// new G_Alarm(foo, 10*1000); // Fire foo in 10 seconds -// new G_Alarm(foo, 10*1000, true /*repeat*/); // Fire foo every 10 seconds -// new G_Alarm(foo, 10*1000, true, 7); // Fire foo every 10 seconds -// // seven times -// new G_ConditionalAlarm(foo, 1000, true); // Fire every sec until foo()==true -// -// // Fire foo every 10 seconds until foo returns true or until it fires seven -// // times, whichever happens first. -// new G_ConditionalAlarm(foo, 10*1000, true /*repeating*/, 7); -// -// TODO: maybe pass an isFinal flag to the callback if they opted to -// set maxTimes and this is the last iteration? - - -/** - * Set an alarm to fire after a given amount of time, or at specific - * intervals. - * - * @param callback Function to call when the alarm fires - * @param delayMS Number indicating the length of the alarm period in ms - * @param opt_repeating Boolean indicating whether this should fire - * periodically - * @param opt_maxTimes Number indicating a maximum number of times to - * repeat (obviously only useful when opt_repeating==true) - */ -this.G_Alarm = -function G_Alarm(callback, delayMS, opt_repeating, opt_maxTimes) { - this.debugZone = "alarm"; - this.callback_ = callback; - this.repeating_ = !!opt_repeating; - this.timer_ = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); - var type = opt_repeating ? - this.timer_.TYPE_REPEATING_SLACK : - this.timer_.TYPE_ONE_SHOT; - this.maxTimes_ = opt_maxTimes ? opt_maxTimes : null; - this.nTimes_ = 0; - - this.observerServiceObserver_ = new G_ObserverServiceObserver( - 'xpcom-shutdown', - BindToObject(this.cancel, this)); - - // Ask the timer to use nsITimerCallback (.notify()) when ready - this.timer_.initWithCallback(this, delayMS, type); -} - -/** - * Cancel this timer - */ -G_Alarm.prototype.cancel = function() { - if (!this.timer_) { - return; - } - - this.timer_.cancel(); - // Break circular reference created between this.timer_ and the G_Alarm - // instance (this) - this.timer_ = null; - this.callback_ = null; - - // We don't need the shutdown observer anymore - this.observerServiceObserver_.unregister(); -} - -/** - * Invoked by the timer when it fires - * - * @param timer Reference to the nsITimer which fired (not currently - * passed along) - */ -G_Alarm.prototype.notify = function(timer) { - // fire callback and save results - var ret = this.callback_(); - - // If they've given us a max number of times to fire, enforce it - this.nTimes_++; - if (this.repeating_ && - typeof this.maxTimes_ == "number" - && this.nTimes_ >= this.maxTimes_) { - this.cancel(); - } else if (!this.repeating_) { - // Clear out the callback closure for TYPE_ONE_SHOT timers - this.cancel(); - } - // We don't cancel/cleanup timers that repeat forever until either - // xpcom-shutdown occurs or cancel() is called explicitly. - - return ret; -} - -G_Alarm.prototype.setDelay = function(delay) { - this.timer_.delay = delay; -} - -/** - * XPCOM cruft - */ -G_Alarm.prototype.QueryInterface = function(iid) { - if (iid.equals(Components.interfaces.nsISupports) || - iid.equals(Components.interfaces.nsITimerCallback)) - return this; - - throw Components.results.NS_ERROR_NO_INTERFACE; -} - - -/** - * An alarm with the additional property that it cancels itself if its - * callback returns true. - * - * For parameter documentation, see G_Alarm - */ -this.G_ConditionalAlarm = -function G_ConditionalAlarm(callback, delayMS, opt_repeating, opt_maxTimes) { - G_Alarm.call(this, callback, delayMS, opt_repeating, opt_maxTimes); - this.debugZone = "conditionalalarm"; -} - -G_ConditionalAlarm.inherits = function(parentCtor) { - var tempCtor = function(){}; - tempCtor.prototype = parentCtor.prototype; - this.superClass_ = parentCtor.prototype; - this.prototype = new tempCtor(); -} - -G_ConditionalAlarm.inherits(G_Alarm); - -/** - * Invoked by the timer when it fires - * - * @param timer Reference to the nsITimer which fired (not currently - * passed along) - */ -G_ConditionalAlarm.prototype.notify = function(timer) { - // Call G_Alarm::notify - var rv = G_Alarm.prototype.notify.call(this, timer); - - if (this.repeating_ && rv) { - G_Debug(this, "Callback of a repeating alarm returned true; cancelling."); - this.cancel(); - } -} diff --git a/toolkit/components/url-classifier/content/moz/cryptohasher.js b/toolkit/components/url-classifier/content/moz/cryptohasher.js deleted file mode 100644 index a1294aa938..0000000000 --- a/toolkit/components/url-classifier/content/moz/cryptohasher.js +++ /dev/null @@ -1,176 +0,0 @@ -# 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/. - - -// A very thin wrapper around nsICryptoHash. It's not strictly -// necessary, but makes the code a bit cleaner and gives us the -// opportunity to verify that our implementations give the results that -// we expect, for example if we have to interoperate with a server. -// -// The digest* methods reset the state of the hasher, so it's -// necessary to call init() explicitly after them. -// -// Works only in Firefox 1.5+. -// -// IMPORTANT NOTE: Due to https://bugzilla.mozilla.org/show_bug.cgi?id=321024 -// you cannot use the cryptohasher before app-startup. The symptom of doing -// so is a segfault in NSS. - -/** - * Instantiate a new hasher. You must explicitly call init() before use! - */ -this.G_CryptoHasher = -function G_CryptoHasher() { - this.debugZone = "cryptohasher"; - this.hasher_ = null; -} - -G_CryptoHasher.algorithms = { - MD2: Ci.nsICryptoHash.MD2, - MD5: Ci.nsICryptoHash.MD5, - SHA1: Ci.nsICryptoHash.SHA1, - SHA256: Ci.nsICryptoHash.SHA256, - SHA384: Ci.nsICryptoHash.SHA384, - SHA512: Ci.nsICryptoHash.SHA512, -}; - -/** - * Initialize the hasher. This function must be called after every call - * to one of the digest* methods. - * - * @param algorithm Constant from G_CryptoHasher.algorithms specifying the - * algorithm this hasher will use - */ -G_CryptoHasher.prototype.init = function(algorithm) { - var validAlgorithm = false; - for (var alg in G_CryptoHasher.algorithms) - if (algorithm == G_CryptoHasher.algorithms[alg]) - validAlgorithm = true; - - if (!validAlgorithm) - throw new Error("Invalid algorithm: " + algorithm); - - this.hasher_ = Cc["@mozilla.org/security/hash;1"] - .createInstance(Ci.nsICryptoHash); - this.hasher_.init(algorithm); -} - -/** - * Update the hash's internal state with input given in a string. Can be - * called multiple times for incrementeal hash updates. - * - * @param input String containing data to hash. - */ -G_CryptoHasher.prototype.updateFromString = function(input) { - if (!this.hasher_) - throw new Error("You must initialize the hasher first!"); - - var stream = Cc['@mozilla.org/io/string-input-stream;1'] - .createInstance(Ci.nsIStringInputStream); - stream.setData(input, input.length); - this.updateFromStream(stream); -} - -/** - * Update the hash's internal state with input given in an array. Can be - * called multiple times for incremental hash updates. - * - * @param input Array containing data to hash. - */ -G_CryptoHasher.prototype.updateFromArray = function(input) { - if (!this.hasher_) - throw new Error("You must initialize the hasher first!"); - - this.hasher_.update(input, input.length); -} - -/** - * Update the hash's internal state with input given in a stream. Can be - * called multiple times from incremental hash updates. - */ -G_CryptoHasher.prototype.updateFromStream = function(stream) { - if (!this.hasher_) - throw new Error("You must initialize the hasher first!"); - - if (stream.available()) - this.hasher_.updateFromStream(stream, stream.available()); -} - -/** - * @returns The hash value as a string (sequence of 8-bit values) - */ -G_CryptoHasher.prototype.digestRaw = function() { - var digest = this.hasher_.finish(false /* not b64 encoded */); - this.hasher_ = null; - return digest; -} - -/** - * @returns The hash value as a base64-encoded string - */ -G_CryptoHasher.prototype.digestBase64 = function() { - var digest = this.hasher_.finish(true /* b64 encoded */); - this.hasher_ = null; - return digest; -} - -/** - * @returns The hash value as a hex-encoded string - */ -G_CryptoHasher.prototype.digestHex = function() { - var raw = this.digestRaw(); - return this.toHex_(raw); -} - -/** - * Converts a sequence of values to a hex-encoded string. The input is a - * a string, so you can stick 16-bit values in each character. - * - * @param str String to conver to hex. (Often this is just a sequence of - * 16-bit values) - * - * @returns String containing the hex representation of the input - */ -G_CryptoHasher.prototype.toHex_ = function(str) { - var hexchars = '0123456789ABCDEF'; - var hexrep = new Array(str.length * 2); - - for (var i = 0; i < str.length; ++i) { - hexrep[i * 2] = hexchars.charAt((str.charCodeAt(i) >> 4) & 15); - hexrep[i * 2 + 1] = hexchars.charAt(str.charCodeAt(i) & 15); - } - return hexrep.join(''); -} - -#ifdef DEBUG -/** - * Lame unittest function - */ -this.TEST_G_CryptoHasher = function TEST_G_CryptoHasher() { - if (G_GDEBUG) { - var z = "cryptohasher UNITTEST"; - G_debugService.enableZone(z); - - G_Debug(z, "Starting"); - - var md5 = function(str) { - var hasher = new G_CryptoHasher(); - hasher.init(G_CryptoHasher.algorithms.MD5); - hasher.updateFromString(str); - return hasher.digestHex().toLowerCase(); - }; - - // test vectors from: http://www.faqs.org/rfcs/rfc1321.html - var vectors = {"": "d41d8cd98f00b204e9800998ecf8427e", - "a": "0cc175b9c0f1b6a831c399e269772661", - "abc": "900150983cd24fb0d6963f7d28e17f72", - "message digest": "f96b697d7cb7938d525a2f31aaf161d0", - "abcdefghijklmnopqrstuvwxyz": "c3fcd3d76192e4007dfb496cca67e13b", - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789": "d174ab98d277d9f5a5611c2c9f419d9f", - "12345678901234567890123456789012345678901234567890123456789012345678901234567890": "57edf4a22be3c955ac49da2e2107b67a"}; - - G_Debug(z, "PASSED"); - } -} -#endif diff --git a/toolkit/components/url-classifier/content/moz/debug.js b/toolkit/components/url-classifier/content/moz/debug.js deleted file mode 100644 index 3c5cb07ad8..0000000000 --- a/toolkit/components/url-classifier/content/moz/debug.js +++ /dev/null @@ -1,843 +0,0 @@ -# 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/. - -#ifdef DEBUG - -// Generic logging/debugging functionality that: -// -// (*) when disabled compiles to no-ops at worst (for calls to the service) -// and to nothing at best (calls to G_Debug() and similar are compiled -// away when you use a jscompiler that strips dead code) -// -// (*) has dynamically configurable/creatable debugging "zones" enabling -// selective logging -// -// (*) hides its plumbing so that all calls in different zones are uniform, -// so you can drop files using this library into other apps that use it -// without any configuration -// -// (*) can be controlled programmatically or via preferences. The -// preferences that control the service and its zones are under -// the preference branch "safebrowsing-debug-service." -// -// (*) outputs function call traces when the "loggifier" zone is enabled -// -// (*) can write output to logfiles so that you can get a call trace -// from someone who is having a problem -// -// Example: -// -// var G_GDEBUG = true // Enable this module -// var G_debugService = new G_DebugService(); // in global context -// -// // You can use it with arbitrary primitive first arguement -// G_Debug("myzone", "Yo yo yo"); // outputs: [myzone] Yo yo yo\n -// -// // But it's nice to use it with an object; it will probe for the zone name -// function Obj() { -// this.debugZone = "someobj"; -// } -// Obj.prototype.foo = function() { -// G_Debug(this, "foo called"); -// } -// (new Obj).foo(); // outputs: [someobj] foo called\n -// -// G_debugService.loggifier.loggify(Obj.prototype); // enable call tracing -// -// // En/disable specific zones programmatically (you can also use preferences) -// G_debugService.enableZone("somezone"); -// G_debugService.disableZone("someotherzone"); -// G_debugService.enableAllZones(); -// -// // We also have asserts and errors: -// G_Error(this, "Some error occurred"); // will throw -// G_Assert(this, (x > 3), "x not greater than three!"); // will throw -// -// See classes below for more methods. -// -// TODO add code to set prefs when not found to the default value of a tristate -// TODO add error level support -// TODO add ability to turn off console output -// -// -------> TO START DEBUGGING: set G_GDEBUG to true - -// These are the functions code will typically call. Everything is -// wrapped in if's so we can compile it away when G_GDEBUG is false. - - -if (typeof G_GDEBUG == "undefined") { - throw new Error("G_GDEBUG constant must be set before loading debug.js"); -} - - -/** - * Write out a debugging message. - * - * @param who The thingy to convert into a zone name corresponding to the - * zone to which this message belongs - * @param msg Message to output - */ -this.G_Debug = function G_Debug(who, msg) { - if (G_GDEBUG) { - G_GetDebugZone(who).debug(msg); - } -} - -/** - * Debugs loudly - */ -this.G_DebugL = function G_DebugL(who, msg) { - if (G_GDEBUG) { - var zone = G_GetDebugZone(who); - - if (zone.zoneIsEnabled()) { - G_debugService.dump( - "\n************************************************************\n"); - - G_Debug(who, msg); - - G_debugService.dump( - "************************************************************\n\n"); - } - } -} - -/** - * Write out a call tracing message - * - * @param who The thingy to convert into a zone name corresponding to the - * zone to which this message belongs - * @param msg Message to output - */ -this.G_TraceCall = function G_TraceCall(who, msg) { - if (G_GDEBUG) { - if (G_debugService.callTracingEnabled()) { - G_debugService.dump(msg + "\n"); - } - } -} - -/** - * Write out an error (and throw) - * - * @param who The thingy to convert into a zone name corresponding to the - * zone to which this message belongs - * @param msg Message to output - */ -this.G_Error = function G_Error(who, msg) { - if (G_GDEBUG) { - G_GetDebugZone(who).error(msg); - } -} - -/** - * Assert something as true and signal an error if it's not - * - * @param who The thingy to convert into a zone name corresponding to the - * zone to which this message belongs - * @param condition Boolean condition to test - * @param msg Message to output - */ -this.G_Assert = function G_Assert(who, condition, msg) { - if (G_GDEBUG) { - G_GetDebugZone(who).assert(condition, msg); - } -} - -/** - * Helper function that takes input and returns the DebugZone - * corresponding to it. - * - * @param who Arbitrary input that will be converted into a zone name. Most - * likely an object that has .debugZone property, or a string. - * @returns The DebugZone object corresponding to the input - */ -this.G_GetDebugZone = function G_GetDebugZone(who) { - if (G_GDEBUG) { - var zone = "?"; - - if (who && who.debugZone) { - zone = who.debugZone; - } else if (typeof who == "string") { - zone = who; - } - - return G_debugService.getZone(zone); - } -} - -// Classes that implement the functionality. - -/** - * A debug "zone" is a string derived from arbitrary types (but - * typically derived from another string or an object). All debugging - * messages using a particular zone can be enabled or disabled - * independent of other zones. This enables you to turn on/off logging - * of particular objects or modules. This object implements a single - * zone and the methods required to use it. - * - * @constructor - * @param service Reference to the DebugService object we use for - * registration - * @param prefix String indicating the unique prefix we should use - * when creating preferences to control this zone - * @param zone String indicating the name of the zone - */ -this.G_DebugZone = function G_DebugZone(service, prefix, zone) { - if (G_GDEBUG) { - this.debugService_ = service; - this.prefix_ = prefix; - this.zone_ = zone; - this.zoneEnabledPrefName_ = prefix + ".zone." + this.zone_; - this.settings_ = new G_DebugSettings(); - } -} - -/** - * @returns Boolean indicating if this zone is enabled - */ -G_DebugZone.prototype.zoneIsEnabled = function() { - if (G_GDEBUG) { - var explicit = this.settings_.getSetting(this.zoneEnabledPrefName_, null); - - if (explicit !== null) { - return explicit; - } else { - return this.debugService_.allZonesEnabled(); - } - } -} - -/** - * Enable this logging zone - */ -G_DebugZone.prototype.enableZone = function() { - if (G_GDEBUG) { - this.settings_.setDefault(this.zoneEnabledPrefName_, true); - } -} - -/** - * Disable this logging zone - */ -G_DebugZone.prototype.disableZone = function() { - if (G_GDEBUG) { - this.settings_.setDefault(this.zoneEnabledPrefName_, false); - } -} - -/** - * Write a debugging message to this zone - * - * @param msg String of message to write - */ -G_DebugZone.prototype.debug = function(msg) { - if (G_GDEBUG) { - if (this.zoneIsEnabled()) { - this.debugService_.dump("[" + this.zone_ + "] " + msg + "\n"); - } - } -} - -/** - * Write an error to this zone and throw - * - * @param msg String of error to write - */ -G_DebugZone.prototype.error = function(msg) { - if (G_GDEBUG) { - this.debugService_.dump("[" + this.zone_ + "] " + msg + "\n"); - throw new Error(msg); - debugger; - } -} - -/** - * Assert something as true and error if it is not - * - * @param condition Boolean condition to test - * @param msg String of message to write if is false - */ -G_DebugZone.prototype.assert = function(condition, msg) { - if (G_GDEBUG) { - if (condition !== true) { - G_Error(this.zone_, "ASSERT FAILED: " + msg); - } - } -} - - -/** - * The debug service handles auto-registration of zones, namespacing - * the zones preferences, and various global settings such as whether - * all zones are enabled. - * - * @constructor - * @param opt_prefix Optional string indicating the unique prefix we should - * use when creating preferences - */ -this.G_DebugService = function G_DebugService(opt_prefix) { - if (G_GDEBUG) { - this.prefix_ = opt_prefix ? opt_prefix : "safebrowsing-debug-service"; - this.consoleEnabledPrefName_ = this.prefix_ + ".alsologtoconsole"; - this.allZonesEnabledPrefName_ = this.prefix_ + ".enableallzones"; - this.callTracingEnabledPrefName_ = this.prefix_ + ".trace-function-calls"; - this.logFileEnabledPrefName_ = this.prefix_ + ".logfileenabled"; - this.logFileErrorLevelPrefName_ = this.prefix_ + ".logfile-errorlevel"; - this.zones_ = {}; - - this.loggifier = new G_Loggifier(); - this.settings_ = new G_DebugSettings(); - } -} - -// Error levels for reporting console messages to the log. -G_DebugService.ERROR_LEVEL_INFO = "INFO"; -G_DebugService.ERROR_LEVEL_WARNING = "WARNING"; -G_DebugService.ERROR_LEVEL_EXCEPTION = "EXCEPTION"; - - -/** - * @returns Boolean indicating if we should send messages to the jsconsole - */ -G_DebugService.prototype.alsoDumpToConsole = function() { - if (G_GDEBUG) { - return this.settings_.getSetting(this.consoleEnabledPrefName_, false); - } -} - -/** - * @returns whether to log output to a file as well as the console. - */ -G_DebugService.prototype.logFileIsEnabled = function() { - if (G_GDEBUG) { - return this.settings_.getSetting(this.logFileEnabledPrefName_, false); - } -} - -/** - * Turns on file logging. dump() output will also go to the file specified by - * setLogFile() - */ -G_DebugService.prototype.enableLogFile = function() { - if (G_GDEBUG) { - this.settings_.setDefault(this.logFileEnabledPrefName_, true); - } -} - -/** - * Turns off file logging - */ -G_DebugService.prototype.disableLogFile = function() { - if (G_GDEBUG) { - this.settings_.setDefault(this.logFileEnabledPrefName_, false); - } -} - -/** - * @returns an nsIFile instance pointing to the current log file location - */ -G_DebugService.prototype.getLogFile = function() { - if (G_GDEBUG) { - return this.logFile_; - } -} - -/** - * Sets a new log file location - */ -G_DebugService.prototype.setLogFile = function(file) { - if (G_GDEBUG) { - this.logFile_ = file; - } -} - -/** - * Enables sending messages to the jsconsole - */ -G_DebugService.prototype.enableDumpToConsole = function() { - if (G_GDEBUG) { - this.settings_.setDefault(this.consoleEnabledPrefName_, true); - } -} - -/** - * Disables sending messages to the jsconsole - */ -G_DebugService.prototype.disableDumpToConsole = function() { - if (G_GDEBUG) { - this.settings_.setDefault(this.consoleEnabledPrefName_, false); - } -} - -/** - * @param zone Name of the zone to get - * @returns The DebugZone object corresopnding to input. If not such - * zone exists, a new one is created and returned - */ -G_DebugService.prototype.getZone = function(zone) { - if (G_GDEBUG) { - if (!this.zones_[zone]) - this.zones_[zone] = new G_DebugZone(this, this.prefix_, zone); - - return this.zones_[zone]; - } -} - -/** - * @param zone Zone to enable debugging for - */ -G_DebugService.prototype.enableZone = function(zone) { - if (G_GDEBUG) { - var toEnable = this.getZone(zone); - toEnable.enableZone(); - } -} - -/** - * @param zone Zone to disable debugging for - */ -G_DebugService.prototype.disableZone = function(zone) { - if (G_GDEBUG) { - var toDisable = this.getZone(zone); - toDisable.disableZone(); - } -} - -/** - * @returns Boolean indicating whether debugging is enabled for all zones - */ -G_DebugService.prototype.allZonesEnabled = function() { - if (G_GDEBUG) { - return this.settings_.getSetting(this.allZonesEnabledPrefName_, false); - } -} - -/** - * Enables all debugging zones - */ -G_DebugService.prototype.enableAllZones = function() { - if (G_GDEBUG) { - this.settings_.setDefault(this.allZonesEnabledPrefName_, true); - } -} - -/** - * Disables all debugging zones - */ -G_DebugService.prototype.disableAllZones = function() { - if (G_GDEBUG) { - this.settings_.setDefault(this.allZonesEnabledPrefName_, false); - } -} - -/** - * @returns Boolean indicating whether call tracing is enabled - */ -G_DebugService.prototype.callTracingEnabled = function() { - if (G_GDEBUG) { - return this.settings_.getSetting(this.callTracingEnabledPrefName_, false); - } -} - -/** - * Enables call tracing - */ -G_DebugService.prototype.enableCallTracing = function() { - if (G_GDEBUG) { - this.settings_.setDefault(this.callTracingEnabledPrefName_, true); - } -} - -/** - * Disables call tracing - */ -G_DebugService.prototype.disableCallTracing = function() { - if (G_GDEBUG) { - this.settings_.setDefault(this.callTracingEnabledPrefName_, false); - } -} - -/** - * Gets the minimum error that will be reported to the log. - */ -G_DebugService.prototype.getLogFileErrorLevel = function() { - if (G_GDEBUG) { - var level = this.settings_.getSetting(this.logFileErrorLevelPrefName_, - G_DebugService.ERROR_LEVEL_EXCEPTION); - - return level.toUpperCase(); - } -} - -/** - * Sets the minimum error level that will be reported to the log. - */ -G_DebugService.prototype.setLogFileErrorLevel = function(level) { - if (G_GDEBUG) { - // normalize case just to make it slightly easier to not screw up. - level = level.toUpperCase(); - - if (level != G_DebugService.ERROR_LEVEL_INFO && - level != G_DebugService.ERROR_LEVEL_WARNING && - level != G_DebugService.ERROR_LEVEL_EXCEPTION) { - throw new Error("Invalid error level specified: {" + level + "}"); - } - - this.settings_.setDefault(this.logFileErrorLevelPrefName_, level); - } -} - -/** - * Internal dump() method - * - * @param msg String of message to dump - */ -G_DebugService.prototype.dump = function(msg) { - if (G_GDEBUG) { - dump(msg); - - if (this.alsoDumpToConsole()) { - try { - var console = Components.classes['@mozilla.org/consoleservice;1'] - .getService(Components.interfaces.nsIConsoleService); - console.logStringMessage(msg); - } catch(e) { - dump("G_DebugZone ERROR: COULD NOT DUMP TO CONSOLE\n"); - } - } - - this.maybeDumpToFile(msg); - } -} - -/** - * Writes the specified message to the log file, if file logging is enabled. - */ -G_DebugService.prototype.maybeDumpToFile = function(msg) { - if (this.logFileIsEnabled() && this.logFile_) { - - /* try to get the correct line end character for this platform */ - if (!this._LINE_END_CHAR) - this._LINE_END_CHAR = - Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime) - .OS == "WINNT" ? "\r\n" : "\n"; - if (this._LINE_END_CHAR != "\n") - msg = msg.replace(/\n/g, this._LINE_END_CHAR); - - try { - var stream = Cc["@mozilla.org/network/file-output-stream;1"] - .createInstance(Ci.nsIFileOutputStream); - stream.init(this.logFile_, - 0x02 | 0x08 | 0x10 /* PR_WRONLY | PR_CREATE_FILE | PR_APPEND */ - -1 /* default perms */, 0 /* no special behavior */); - stream.write(msg, msg.length); - } finally { - stream.close(); - } - } -} - -/** - * Implements nsIConsoleListener.observe(). Gets called when an error message - * gets reported to the console and sends it to the log file as well. - */ -G_DebugService.prototype.observe = function(consoleMessage) { - if (G_GDEBUG) { - var errorLevel = this.getLogFileErrorLevel(); - - // consoleMessage can be either nsIScriptError or nsIConsoleMessage. The - // latter does not have things like line number, etc. So we special case - // it first. - if (!(consoleMessage instanceof Ci.nsIScriptError)) { - // Only report these messages if the error level is INFO. - if (errorLevel == G_DebugService.ERROR_LEVEL_INFO) { - this.maybeDumpToFile(G_DebugService.ERROR_LEVEL_INFO + ": " + - consoleMessage.message + "\n"); - } - - return; - } - - // We make a local copy of these fields because writing to it doesn't seem - // to work. - var flags = consoleMessage.flags; - var sourceName = consoleMessage.sourceName; - var lineNumber = consoleMessage.lineNumber; - - // Sometimes, a scripterror instance won't have any flags set. We - // default to exception. - if (!flags) { - flags = Ci.nsIScriptError.exceptionFlag; - } - - // Default the filename and line number if they aren't set. - if (!sourceName) { - sourceName = ""; - } - - if (!lineNumber) { - lineNumber = ""; - } - - // Report the error in the log file. - if (flags & Ci.nsIScriptError.warningFlag) { - // Only report warnings if the error level is warning or better. - if (errorLevel == G_DebugService.ERROR_LEVEL_WARNING || - errorLevel == G_DebugService.ERROR_LEVEL_INFO) { - this.reportScriptError_(consoleMessage.message, - sourceName, - lineNumber, - G_DebugService.ERROR_LEVEL_WARNING); - } - } else if (flags & Ci.nsIScriptError.exceptionFlag) { - // Always report exceptions. - this.reportScriptError_(consoleMessage.message, - sourceName, - lineNumber, - G_DebugService.ERROR_LEVEL_EXCEPTION); - } - } -} - -/** - * Private helper to report an nsIScriptError instance to the log/console. - */ -G_DebugService.prototype.reportScriptError_ = function(message, sourceName, - lineNumber, label) { - message = "\n------------------------------------------------------------\n" + - label + ": " + message + - "\nlocation: " + sourceName + ", " + "line: " + lineNumber + - "\n------------------------------------------------------------\n\n"; - - dump(message); - this.maybeDumpToFile(message); -} - - - -/** - * A class that instruments methods so they output a call trace, - * including the values of their actual parameters and return value. - * This code is mostly stolen from Aaron Boodman's original - * implementation in clobber utils. - * - * Note that this class uses the "loggifier" debug zone, so you'll see - * a complete call trace when that zone is enabled. - * - * @constructor - */ -this.G_Loggifier = function G_Loggifier() { - if (G_GDEBUG) { - // Careful not to loggify ourselves! - this.mark_(this); - } -} - -/** - * Marks an object as having been loggified. Loggification is not - * idempotent :) - * - * @param obj Object to be marked - */ -G_Loggifier.prototype.mark_ = function(obj) { - if (G_GDEBUG) { - obj.__loggified_ = true; - } -} - -/** - * @param obj Object to be examined - * @returns Boolean indicating if the object has been loggified - */ -G_Loggifier.prototype.isLoggified = function(obj) { - if (G_GDEBUG) { - return !!obj.__loggified_; - } -} - -/** - * Attempt to extract the class name from the constructor definition. - * Assumes the object was created using new. - * - * @param constructor String containing the definition of a constructor, - * for example what you'd get by examining obj.constructor - * @returns Name of the constructor/object if it could be found, else "???" - */ -G_Loggifier.prototype.getFunctionName_ = function(constructor) { - if (G_GDEBUG) { - return constructor.name || "???"; - } -} - -/** - * Wraps all the methods in an object so that call traces are - * automatically outputted. - * - * @param obj Object to loggify. SHOULD BE THE PROTOTYPE OF A USER-DEFINED - * object. You can get into trouble if you attempt to - * loggify something that isn't, for example the Window. - * - * Any additional parameters are considered method names which should not be - * loggified. - * - * Usage: - * G_debugService.loggifier.loggify(MyClass.prototype, - * "firstMethodNotToLog", - * "secondMethodNotToLog", - * ... etc ...); - */ -G_Loggifier.prototype.loggify = function(obj) { - if (G_GDEBUG) { - if (!G_debugService.callTracingEnabled()) { - return; - } - - if (typeof window != "undefined" && obj == window || - this.isLoggified(obj)) // Don't go berserk! - return; - - var zone = G_GetDebugZone(obj); - if (!zone || !zone.zoneIsEnabled()) { - return; - } - - this.mark_(obj); - - // Helper function returns an instrumented version of - // objName.meth, with "this" bound properly. (BTW, because we're - // in a conditional here, functions will only be defined as - // they're encountered during execution, so declare this helper - // before using it.) - - let wrap = function (meth, objName, methName) { - return function() { - - // First output the call along with actual parameters - var args = new Array(arguments.length); - var argsString = ""; - for (var i = 0; i < args.length; i++) { - args[i] = arguments[i]; - argsString += (i == 0 ? "" : ", "); - - if (typeof args[i] == "function") { - argsString += "[function]"; - } else { - argsString += args[i]; - } - } - - G_TraceCall(this, "> " + objName + "." + methName + "(" + - argsString + ")"); - - // Then run the function, capturing the return value and throws - try { - var retVal = meth.apply(this, arguments); - var reportedRetVal = retVal; - - if (typeof reportedRetVal == "undefined") - reportedRetVal = "void"; - else if (reportedRetVal === "") - reportedRetVal = "\"\" (empty string)"; - } catch (e) { - if (e && !e.__logged) { - G_TraceCall(this, "Error: " + e.message + ". " + - e.fileName + ": " + e.lineNumber); - try { - e.__logged = true; - } catch (e2) { - // Sometimes we can't add the __logged flag because it's an - // XPC wrapper - throw e; - } - } - - throw e; // Re-throw! - } - - // And spit it out already - G_TraceCall( - this, - "< " + objName + "." + methName + ": " + reportedRetVal); - - return retVal; - }; - }; - - var ignoreLookup = {}; - - if (arguments.length > 1) { - for (var i = 1; i < arguments.length; i++) { - ignoreLookup[arguments[i]] = true; - } - } - - // Wrap each method of obj - for (var p in obj) { - // Work around bug in Firefox. In ffox typeof RegExp is "function", - // so make sure this really is a function. Bug as of FFox 1.5b2. - if (typeof obj[p] == "function" && obj[p].call && !ignoreLookup[p]) { - var objName = this.getFunctionName_(obj.constructor); - obj[p] = wrap(obj[p], objName, p); - } - } - } -} - - -/** - * Simple abstraction around debug settings. The thing with debug settings is - * that we want to be able to specify a default in the application's startup, - * but have that default be overridable by the user via their prefs. - * - * To generalize this, we package up a dictionary of defaults with the - * preferences tree. If a setting isn't in the preferences tree, then we grab it - * from the defaults. - */ -this.G_DebugSettings = function G_DebugSettings() { - this.defaults_ = {}; - this.prefs_ = new G_Preferences(); -} - -/** - * Returns the value of a settings, optionally defaulting to a given value if it - * doesn't exist. If no default is specified, the default is |undefined|. - */ -G_DebugSettings.prototype.getSetting = function(name, opt_default) { - var override = this.prefs_.getPref(name, null); - - if (override !== null) { - return override; - } else if (typeof this.defaults_[name] != "undefined") { - return this.defaults_[name]; - } else { - return opt_default; - } -} - -/** - * Sets the default value for a setting. If the user doesn't override it with a - * preference, this is the value which will be returned by getSetting(). - */ -G_DebugSettings.prototype.setDefault = function(name, val) { - this.defaults_[name] = val; -} - -var G_debugService = new G_DebugService(); // Instantiate us! - -if (G_GDEBUG) { - G_debugService.enableAllZones(); -} - -#else - -// Stubs for the debugging aids scattered through this component. -// They will be expanded if you compile yourself a debug build. - -this.G_Debug = function G_Debug(who, msg) { } -this.G_Assert = function G_Assert(who, condition, msg) { } -this.G_Error = function G_Error(who, msg) { } -this.G_debugService = { __noSuchMethod__: function() { } }; - -#endif diff --git a/toolkit/components/url-classifier/content/moz/lang.js b/toolkit/components/url-classifier/content/moz/lang.js deleted file mode 100644 index 804a6e973e..0000000000 --- a/toolkit/components/url-classifier/content/moz/lang.js +++ /dev/null @@ -1,82 +0,0 @@ -# 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/. - - -/** - * lang.js - Some missing JavaScript language features - */ - -/** - * Partially applies a function to a particular "this object" and zero or - * more arguments. The result is a new function with some arguments of the first - * function pre-filled and the value of |this| "pre-specified". - * - * Remaining arguments specified at call-time are appended to the pre- - * specified ones. - * - * Usage: - * var barMethBound = BindToObject(myFunction, myObj, "arg1", "arg2"); - * barMethBound("arg3", "arg4"); - * - * @param fn {string} Reference to the function to be bound - * - * @param self {object} Specifies the object which |this| should point to - * when the function is run. If the value is null or undefined, it will default - * to the global object. - * - * @returns {function} A partially-applied form of the speficied function. - */ -this.BindToObject = function BindToObject(fn, self, opt_args) { - var boundargs = fn.boundArgs_ || []; - boundargs = boundargs.concat(Array.slice(arguments, 2, arguments.length)); - - if (fn.boundSelf_) - self = fn.boundSelf_; - if (fn.boundFn_) - fn = fn.boundFn_; - - var newfn = function() { - // Combine the static args and the new args into one big array - var args = boundargs.concat(Array.slice(arguments)); - return fn.apply(self, args); - } - - newfn.boundArgs_ = boundargs; - newfn.boundSelf_ = self; - newfn.boundFn_ = fn; - - return newfn; -} - -/** - * Inherit the prototype methods from one constructor into another. - * - * Usage: - * - * function ParentClass(a, b) { } - * ParentClass.prototype.foo = function(a) { } - * - * function ChildClass(a, b, c) { - * ParentClass.call(this, a, b); - * } - * - * ChildClass.inherits(ParentClass); - * - * var child = new ChildClass("a", "b", "see"); - * child.foo(); // works - * - * In addition, a superclass' implementation of a method can be invoked - * as follows: - * - * ChildClass.prototype.foo = function(a) { - * ChildClass.superClass_.foo.call(this, a); - * // other code - * }; - */ -Function.prototype.inherits = function(parentCtor) { - var tempCtor = function(){}; - tempCtor.prototype = parentCtor.prototype; - this.superClass_ = parentCtor.prototype; - this.prototype = new tempCtor(); -} diff --git a/toolkit/components/url-classifier/content/moz/observer.js b/toolkit/components/url-classifier/content/moz/observer.js deleted file mode 100644 index 526cf7de28..0000000000 --- a/toolkit/components/url-classifier/content/moz/observer.js +++ /dev/null @@ -1,145 +0,0 @@ -# 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/. - - -// A couple of classes to simplify creating observers. -// -// // Example1: -// -// function doSomething() { ... } -// var observer = new G_ObserverWrapper(topic, doSomething); -// someObj.addObserver(topic, observer); -// -// // Example2: -// -// function doSomething() { ... } -// new G_ObserverServiceObserver("profile-after-change", -// doSomething, -// true /* run only once */); - - -/** - * This class abstracts the admittedly simple boilerplate required of - * an nsIObserver. It saves you the trouble of implementing the - * indirection of your own observe() function. - * - * @param topic String containing the topic the observer will filter for - * - * @param observeFunction Reference to the function to call when the - * observer fires - * - * @constructor - */ -this.G_ObserverWrapper = function G_ObserverWrapper(topic, observeFunction) { - this.debugZone = "observer"; - this.topic_ = topic; - this.observeFunction_ = observeFunction; -} - -/** - * XPCOM - */ -G_ObserverWrapper.prototype.QueryInterface = function(iid) { - if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIObserver)) - return this; - throw Components.results.NS_ERROR_NO_INTERFACE; -} - -/** - * Invoked by the thingy being observed - */ -G_ObserverWrapper.prototype.observe = function(subject, topic, data) { - if (topic == this.topic_) - this.observeFunction_(subject, topic, data); -} - - -/** - * This class abstracts the admittedly simple boilerplate required of - * observing an observerservice topic. It implements the indirection - * required, and automatically registers to hear the topic. - * - * @param topic String containing the topic the observer will filter for - * - * @param observeFunction Reference to the function to call when the - * observer fires - * - * @param opt_onlyOnce Boolean indicating if the observer should unregister - * after it has fired - * - * @constructor - */ -this.G_ObserverServiceObserver = -function G_ObserverServiceObserver(topic, observeFunction, opt_onlyOnce) { - this.debugZone = "observerserviceobserver"; - this.topic_ = topic; - this.observeFunction_ = observeFunction; - this.onlyOnce_ = !!opt_onlyOnce; - - this.observer_ = new G_ObserverWrapper(this.topic_, - BindToObject(this.observe_, this)); - this.observerService_ = Cc["@mozilla.org/observer-service;1"] - .getService(Ci.nsIObserverService); - this.observerService_.addObserver(this.observer_, this.topic_, false); -} - -/** - * Unregister the observer from the observerservice - */ -G_ObserverServiceObserver.prototype.unregister = function() { - this.observerService_.removeObserver(this.observer_, this.topic_); - this.observerService_ = null; -} - -/** - * Invoked by the observerservice - */ -G_ObserverServiceObserver.prototype.observe_ = function(subject, topic, data) { - this.observeFunction_(subject, topic, data); - if (this.onlyOnce_) - this.unregister(); -} - -#ifdef DEBUG -this.TEST_G_Observer = function TEST_G_Observer() { - if (G_GDEBUG) { - - var z = "observer UNITTEST"; - G_debugService.enableZone(z); - - G_Debug(z, "Starting"); - - var regularObserverRan = 0; - var observerServiceObserverRan = 0; - - let regularObserver = function () { - regularObserverRan++; - }; - - let observerServiceObserver = function () { - observerServiceObserverRan++; - }; - - var service = Cc["@mozilla.org/observer-service;1"] - .getService(Ci.nsIObserverService); - var topic = "google-observer-test"; - - var o1 = new G_ObserverWrapper(topic, regularObserver); - service.addObserver(o1, topic, false); - - new G_ObserverServiceObserver(topic, - observerServiceObserver, true /* once */); - - // Notifications happen synchronously, so this is easy - service.notifyObservers(null, topic, null); - service.notifyObservers(null, topic, null); - - G_Assert(z, regularObserverRan == 2, "Regular observer broken"); - G_Assert(z, observerServiceObserverRan == 1, "ObsServObs broken"); - - service.removeObserver(o1, topic); - G_Debug(z, "PASSED"); - } -} -#endif diff --git a/toolkit/components/url-classifier/content/moz/preferences.js b/toolkit/components/url-classifier/content/moz/preferences.js deleted file mode 100644 index b7b3fdcf54..0000000000 --- a/toolkit/components/url-classifier/content/moz/preferences.js +++ /dev/null @@ -1,276 +0,0 @@ -# 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/. - - -// Class for manipulating preferences. Aside from wrapping the pref -// service, useful functionality includes: -// -// - abstracting prefobserving so that you can observe preferences -// without implementing nsIObserver -// -// - getters that return a default value when the pref doesn't exist -// (instead of throwing) -// -// - get-and-set getters -// -// Example: -// -// var p = new PROT_Preferences(); -// dump(p.getPref("some-true-pref")); // shows true -// dump(p.getPref("no-such-pref", true)); // shows true -// dump(p.getPref("no-such-pref", null)); // shows null -// -// function observe(prefThatChanged) { -// dump("Pref changed: " + prefThatChanged); -// }; -// -// p.addObserver("somepref", observe); -// p.setPref("somepref", true); // dumps -// p.removeObserver("somepref", observe); -// -// TODO: should probably have the prefobserver pass in the new and old -// values - -// TODO(tc): Maybe remove this class and just call natively since we're no -// longer an extension. - -/** - * A class that wraps the preferences service. - * - * @param opt_startPoint A starting point on the prefs tree to resolve - * names passed to setPref and getPref. - * - * @param opt_useDefaultPranch Set to true to work against the default - * preferences tree instead of the profile one. - * - * @constructor - */ -this.G_Preferences = -function G_Preferences(opt_startPoint, opt_getDefaultBranch) { - this.debugZone = "prefs"; - this.observers_ = {}; - this.getDefaultBranch_ = !!opt_getDefaultBranch; - - this.startPoint_ = opt_startPoint || null; -} - -G_Preferences.setterMap_ = { "string": "setCharPref", - "boolean": "setBoolPref", - "number": "setIntPref" }; - -G_Preferences.getterMap_ = {}; -G_Preferences.getterMap_[Ci.nsIPrefBranch.PREF_STRING] = "getCharPref"; -G_Preferences.getterMap_[Ci.nsIPrefBranch.PREF_BOOL] = "getBoolPref"; -G_Preferences.getterMap_[Ci.nsIPrefBranch.PREF_INT] = "getIntPref"; - -G_Preferences.prototype.__defineGetter__('prefs_', function() { - var prefs; - var prefSvc = Cc["@mozilla.org/preferences-service;1"] - .getService(Ci.nsIPrefService); - - if (this.getDefaultBranch_) { - prefs = prefSvc.getDefaultBranch(this.startPoint_); - } else { - prefs = prefSvc.getBranch(this.startPoint_); - } - - // QI to prefs in case we want to add observers - prefs.QueryInterface(Ci.nsIPrefBranchInternal); - return prefs; -}); - -/** - * Stores a key/value in a user preference. Valid types for val are string, - * boolean, and number. Complex values are not yet supported (but feel free to - * add them!). - */ -G_Preferences.prototype.setPref = function(key, val) { - var datatype = typeof(val); - - if (datatype == "number" && (val % 1 != 0)) { - throw new Error("Cannot store non-integer numbers in preferences."); - } - - var meth = G_Preferences.setterMap_[datatype]; - - if (!meth) { - throw new Error("Pref datatype {" + datatype + "} not supported."); - } - - return this.prefs_[meth](key, val); -} - -/** - * Retrieves a user preference. Valid types for the value are the same as for - * setPref. If the preference is not found, opt_default will be returned - * instead. - */ -G_Preferences.prototype.getPref = function(key, opt_default) { - var type = this.prefs_.getPrefType(key); - - // zero means that the specified pref didn't exist - if (type == Ci.nsIPrefBranch.PREF_INVALID) { - return opt_default; - } - - var meth = G_Preferences.getterMap_[type]; - - if (!meth) { - throw new Error("Pref datatype {" + type + "} not supported."); - } - - // If a pref has been cleared, it will have a valid type but won't - // be gettable, so this will throw. - try { - return this.prefs_[meth](key); - } catch(e) { - return opt_default; - } -} - -/** - * Delete a preference. - * - * @param which Name of preference to obliterate - */ -G_Preferences.prototype.clearPref = function(which) { - try { - // This throws if the pref doesn't exist, which is fine because a - // nonexistent pref is cleared - this.prefs_.clearUserPref(which); - } catch(e) {} -} - -/** - * Add an observer for a given pref. - * - * @param which String containing the pref to listen to - * @param callback Function to be called when the pref changes. This - * function will receive a single argument, a string - * holding the preference name that changed - */ -G_Preferences.prototype.addObserver = function(which, callback) { - // Need to store the observer we create so we can eventually unregister it - if (!this.observers_[which]) - this.observers_[which] = { callbacks: [], observers: [] }; - - /* only add an observer if the callback hasn't been registered yet */ - if (this.observers_[which].callbacks.indexOf(callback) == -1) { - var observer = new G_PreferenceObserver(callback); - this.observers_[which].callbacks.push(callback); - this.observers_[which].observers.push(observer); - this.prefs_.addObserver(which, observer, false /* strong reference */); - } -} - -/** - * Remove an observer for a given pref. - * - * @param which String containing the pref to stop listening to - * @param callback Function to remove as an observer - */ -G_Preferences.prototype.removeObserver = function(which, callback) { - var ix = this.observers_[which].callbacks.indexOf(callback); - G_Assert(this, ix != -1, "Tried to unregister a nonexistent observer"); - this.observers_[which].callbacks.splice(ix, 1); - var observer = this.observers_[which].observers.splice(ix, 1)[0]; - this.prefs_.removeObserver(which, observer); -} - -/** - * Remove all preference observers registered through this object. - */ -G_Preferences.prototype.removeAllObservers = function() { - for (var which in this.observers_) { - for each (var observer in this.observers_[which].observers) { - this.prefs_.removeObserver(which, observer); - } - } - this.observers_ = {}; -} - -/** - * Helper class that knows how to observe preference changes and - * invoke a callback when they do - * - * @constructor - * @param callback Function to call when the preference changes - */ -this.G_PreferenceObserver = -function G_PreferenceObserver(callback) { - this.debugZone = "prefobserver"; - this.callback_ = callback; -} - -/** - * Invoked by the pref system when a preference changes. Passes the - * message along to the callback. - * - * @param subject The nsIPrefBranch that changed - * @param topic String "nsPref:changed" (aka - * NS_PREFBRANCH_PREFCHANGE_OBSERVER_ID -- but where does it - * live???) - * @param data Name of the pref that changed - */ -G_PreferenceObserver.prototype.observe = function(subject, topic, data) { - G_Debug(this, "Observed pref change: " + data); - this.callback_(data); -} - -/** - * XPCOM cruft - * - * @param iid Interface id of the interface the caller wants - */ -G_PreferenceObserver.prototype.QueryInterface = function(iid) { - if (iid.equals(Ci.nsISupports) || - iid.equals(Ci.nsIObserver) || - iid.equals(Ci.nsISupportsWeakReference)) - return this; - throw Components.results.NS_ERROR_NO_INTERFACE; -} - -#ifdef DEBUG -// UNITTESTS -this.TEST_G_Preferences = function TEST_G_Preferences() { - if (G_GDEBUG) { - var z = "preferences UNITTEST"; - G_debugService.enableZone(z); - G_Debug(z, "Starting"); - - var p = new G_Preferences(); - - var testPref = "test-preferences-unittest"; - var noSuchPref = "test-preferences-unittest-aypabtu"; - - // Used to test observing - var observeCount = 0; - let observe = function (prefChanged) { - G_Assert(z, prefChanged == testPref, "observer broken"); - observeCount++; - }; - - // Test setting, getting, and observing - p.addObserver(testPref, observe); - p.setPref(testPref, true); - G_Assert(z, p.getPref(testPref), "get or set broken"); - G_Assert(z, observeCount == 1, "observer adding not working"); - - p.removeObserver(testPref, observe); - - p.setPref(testPref, false); - G_Assert(z, observeCount == 1, "observer removal not working"); - G_Assert(z, !p.getPref(testPref), "get broken"); - - // Remember to clean up the prefs we've set, and test removing prefs - // while we're at it - p.clearPref(noSuchPref); - G_Assert(z, !p.getPref(noSuchPref, false), "clear broken"); - - p.clearPref(testPref); - - G_Debug(z, "PASSED"); - } -} -#endif diff --git a/toolkit/components/url-classifier/content/moz/protocol4.js b/toolkit/components/url-classifier/content/moz/protocol4.js deleted file mode 100644 index a75f6b531e..0000000000 --- a/toolkit/components/url-classifier/content/moz/protocol4.js +++ /dev/null @@ -1,133 +0,0 @@ -# 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/. - - -// A helper class that knows how to parse from and serialize to -// protocol4. This is a simple, historical format used by some Google -// interfaces, for example the Toolbar (i.e., ancient services). -// -// Protocol4 consists of a newline-separated sequence of name/value -// pairs (strings). Each line consists of the name, the value length, -// and the value itself, all separated by colons. Example: -// -// foo:6:barbaz\n -// fritz:33:issickofdynamicallytypedlanguages\n - - -/** - * This class knows how to serialize/deserialize maps to/from their - * protocol4 representation. - * - * @constructor - */ -this.G_Protocol4Parser = function G_Protocol4Parser() { - this.debugZone = "protocol4"; - - this.protocol4RegExp_ = new RegExp("([^:]+):\\d+:(.*)$"); - this.newlineRegExp_ = new RegExp("(\\r)?\\n"); -} - -/** - * Create a map from a protocol4 string. Silently skips invalid lines. - * - * @param text String holding the protocol4 representation - * - * @returns Object as an associative array with keys and values - * given in text. The empty object is returned if none - * are parsed. - */ -G_Protocol4Parser.prototype.parse = function(text) { - - var response = {}; - if (!text) - return response; - - // Responses are protocol4: (repeated) name:numcontentbytes:content\n - var lines = text.split(this.newlineRegExp_); - for (var i = 0; i < lines.length; i++) - if (this.protocol4RegExp_.exec(lines[i])) - response[RegExp.$1] = RegExp.$2; - - return response; -} - -/** - * Create a protocol4 string from a map (object). Throws an error on - * an invalid input. - * - * @param map Object as an associative array with keys and values - * given as strings. - * - * @returns text String holding the protocol4 representation - */ -G_Protocol4Parser.prototype.serialize = function(map) { - if (typeof map != "object") - throw new Error("map must be an object"); - - var text = ""; - for (var key in map) { - if (typeof map[key] != "string") - throw new Error("Keys and values must be strings"); - - text += key + ":" + map[key].length + ":" + map[key] + "\n"; - } - - return text; -} - -#ifdef DEBUG -/** - * Cheesey unittests - */ -this.TEST_G_Protocol4Parser = function TEST_G_Protocol4Parser() { - if (G_GDEBUG) { - var z = "protocol4 UNITTEST"; - G_debugService.enableZone(z); - - G_Debug(z, "Starting"); - - var p = new G_Protocol4Parser(); - - let isEmpty = function (map) { - for (var key in map) - return false; - return true; - }; - - G_Assert(z, isEmpty(p.parse(null)), "Parsing null broken"); - G_Assert(z, isEmpty(p.parse("")), "Parsing nothing broken"); - - var t = "foo:3:bar"; - G_Assert(z, p.parse(t)["foo"] === "bar", "Parsing one line broken"); - - t = "foo:3:bar\n"; - G_Assert(z, p.parse(t)["foo"] === "bar", "Parsing line with lf broken"); - - t = "foo:3:bar\r\n"; - G_Assert(z, p.parse(t)["foo"] === "bar", "Parsing with crlf broken"); - - - t = "foo:3:bar\nbar:3:baz\r\nbom:3:yaz\n"; - G_Assert(z, p.parse(t)["foo"] === "bar", "First in multiline"); - G_Assert(z, p.parse(t)["bar"] === "baz", "Second in multiline"); - G_Assert(z, p.parse(t)["bom"] === "yaz", "Third in multiline"); - G_Assert(z, p.parse(t)[""] === undefined, "Nonexistent in multiline"); - - // Test serialization - - var original = { - "1": "1", - "2": "2", - "foobar": "baz", - "hello there": "how are you?" , - }; - var deserialized = p.parse(p.serialize(original)); - for (var key in original) - G_Assert(z, original[key] === deserialized[key], - "Trouble (de)serializing " + key); - - G_Debug(z, "PASSED"); - } -} -#endif diff --git a/toolkit/components/url-classifier/content/multi-querier.js b/toolkit/components/url-classifier/content/multi-querier.js deleted file mode 100644 index f79db8154d..0000000000 --- a/toolkit/components/url-classifier/content/multi-querier.js +++ /dev/null @@ -1,137 +0,0 @@ -# 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/. - -/** - * This class helps us batch a series of async calls to the db. - * If any of the tokens is in the database, we fire callback with - * true as a param. If all the tokens are not in the database, - * we fire callback with false as a param. - * This is an "Abstract" base class. Subclasses need to supply - * the condition_ method. - * - * @param tokens Array of strings to lookup in the db - * @param tableName String name of the table - * @param callback Function callback function that takes true if the condition - * passes. - */ -this.MultiQuerier = -function MultiQuerier(tokens, tableName, callback) { - this.tokens_ = tokens; - this.tableName_ = tableName; - this.callback_ = callback; - this.dbservice_ = Cc["@mozilla.org/url-classifier/dbservice;1"] - .getService(Ci.nsIUrlClassifierDBService); - // We put the current token in this variable. - this.key_ = null; -} - -/** - * Run the remaining tokens against the db. - */ -MultiQuerier.prototype.run = function() { - if (this.tokens_.length == 0) { - this.callback_.handleEvent(false); - this.dbservice_ = null; - this.callback_ = null; - return; - } - - this.key_ = this.tokens_.pop(); - G_Debug(this, "Looking up " + this.key_ + " in " + this.tableName_); - this.dbservice_.exists(this.tableName_, this.key_, - BindToObject(this.result_, this)); -} - -/** - * Callback from the db. If the returned value passes the this.condition_ - * test, go ahead and call the main callback. - */ -MultiQuerier.prototype.result_ = function(value) { - if (this.condition_(value)) { - this.callback_.handleEvent(true) - this.dbservice_ = null; - this.callback_ = null; - } else { - this.run(); - } -} - -// Subclasses must override this. -MultiQuerier.prototype.condition_ = function(value) { - throw "MultiQuerier is an abstract base class"; -} - - -/** - * Concrete MultiQuerier that stops if the key exists in the db. - */ -this.ExistsMultiQuerier = -function ExistsMultiQuerier(tokens, tableName, callback) { - MultiQuerier.call(this, tokens, tableName, callback); - this.debugZone = "existsMultiQuerier"; -} - -ExistsMultiQuerier.inherits = function(parentCtor) { - var tempCtor = function(){}; - tempCtor.prototype = parentCtor.prototype; - this.superClass_ = parentCtor.prototype; - this.prototype = new tempCtor(); -} -ExistsMultiQuerier.inherits(MultiQuerier); - -ExistsMultiQuerier.prototype.condition_ = function(value) { - return value.length > 0; -} - - -/** - * Concrete MultiQuerier that looks up a key, decrypts it, then - * checks the the resulting regular expressions for a match. - * @param tokens Array of hosts - */ -this.EnchashMultiQuerier = -function EnchashMultiQuerier(tokens, tableName, callback, url) { - MultiQuerier.call(this, tokens, tableName, callback); - this.url_ = url; - this.enchashDecrypter_ = new PROT_EnchashDecrypter(); - this.debugZone = "enchashMultiQuerier"; -} - -EnchashMultiQuerier.inherits = function(parentCtor) { - var tempCtor = function(){}; - tempCtor.prototype = parentCtor.prototype; - this.superClass_ = parentCtor.prototype; - this.prototype = new tempCtor(); -} -EnchashMultiQuerier.inherits(MultiQuerier); - -EnchashMultiQuerier.prototype.run = function() { - if (this.tokens_.length == 0) { - this.callback_.handleEvent(false); - this.dbservice_ = null; - this.callback_ = null; - return; - } - var host = this.tokens_.pop(); - this.key_ = host; - var lookupKey = this.enchashDecrypter_.getLookupKey(host); - this.dbservice_.exists(this.tableName_, lookupKey, - BindToObject(this.result_, this)); -} - -EnchashMultiQuerier.prototype.condition_ = function(encryptedValue) { - if (encryptedValue.length > 0) { - // We have encrypted regular expressions for this host. Let's - // decrypt them and see if we have a match. - var decrypted = this.enchashDecrypter_.decryptData(encryptedValue, - this.key_); - var res = this.enchashDecrypter_.parseRegExps(decrypted); - for (var j = 0; j < res.length; j++) { - if (res[j].test(this.url_)) { - return true; - } - } - } - return false; -} diff --git a/toolkit/components/url-classifier/content/request-backoff.js b/toolkit/components/url-classifier/content/request-backoff.js deleted file mode 100644 index 17e815cf14..0000000000 --- a/toolkit/components/url-classifier/content/request-backoff.js +++ /dev/null @@ -1,116 +0,0 @@ -/* 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/. */ - -// This implements logic for stopping requests if the server starts to return -// too many errors. If we get MAX_ERRORS errors in ERROR_PERIOD minutes, we -// back off for TIMEOUT_INCREMENT minutes. If we get another error -// immediately after we restart, we double the timeout and add -// TIMEOUT_INCREMENT minutes, etc. -// -// This is similar to the logic used by the search suggestion service. - -// HTTP responses that count as an error. We also include any 5xx response -// as an error. -this.HTTP_FOUND = 302; -this.HTTP_SEE_OTHER = 303; -this.HTTP_TEMPORARY_REDIRECT = 307; - -/** - * @param maxErrors Number of times to request before backing off. - * @param retryIncrement Time (ms) for each retry before backing off. - * @param maxRequests Number the number of requests needed to trigger backoff - * @param requestPeriod Number time (ms) in which maxRequests have to occur to - * trigger the backoff behavior (0 to disable maxRequests) - * @param timeoutIncrement Number time (ms) the starting timeout period - * we double this time for consecutive errors - * @param maxTimeout Number time (ms) maximum timeout period - */ -this.RequestBackoff = -function RequestBackoff(maxErrors, retryIncrement, - maxRequests, requestPeriod, - timeoutIncrement, maxTimeout) { - this.MAX_ERRORS_ = maxErrors; - this.RETRY_INCREMENT_ = retryIncrement; - this.MAX_REQUESTS_ = maxRequests; - this.REQUEST_PERIOD_ = requestPeriod; - this.TIMEOUT_INCREMENT_ = timeoutIncrement; - this.MAX_TIMEOUT_ = maxTimeout; - - // Queue of ints keeping the time of all requests - this.requestTimes_ = []; - - this.numErrors_ = 0; - this.errorTimeout_ = 0; - this.nextRequestTime_ = 0; -} - -/** - * Reset the object for reuse. This deliberately doesn't clear requestTimes_. - */ -RequestBackoff.prototype.reset = function() { - this.numErrors_ = 0; - this.errorTimeout_ = 0; - this.nextRequestTime_ = 0; -} - -/** - * Check to see if we can make a request. - */ -RequestBackoff.prototype.canMakeRequest = function() { - var now = Date.now(); - if (now < this.nextRequestTime_) { - return false; - } - - return (this.requestTimes_.length < this.MAX_REQUESTS_ || - (now - this.requestTimes_[0]) > this.REQUEST_PERIOD_); -} - -RequestBackoff.prototype.noteRequest = function() { - var now = Date.now(); - this.requestTimes_.push(now); - - // We only care about keeping track of MAX_REQUESTS - if (this.requestTimes_.length > this.MAX_REQUESTS_) - this.requestTimes_.shift(); -} - -RequestBackoff.prototype.nextRequestDelay = function() { - return Math.max(0, this.nextRequestTime_ - Date.now()); -} - -/** - * Notify this object of the last server response. If it's an error, - */ -RequestBackoff.prototype.noteServerResponse = function(status) { - if (this.isErrorStatus(status)) { - this.numErrors_++; - - if (this.numErrors_ < this.MAX_ERRORS_) - this.errorTimeout_ = this.RETRY_INCREMENT_; - else if (this.numErrors_ == this.MAX_ERRORS_) - this.errorTimeout_ = this.TIMEOUT_INCREMENT_; - else - this.errorTimeout_ *= 2; - - this.errorTimeout_ = Math.min(this.errorTimeout_, this.MAX_TIMEOUT_); - this.nextRequestTime_ = Date.now() + this.errorTimeout_; - } else { - // Reset error timeout, allow requests to go through. - this.reset(); - } -} - -/** - * We consider 302, 303, 307, 4xx, and 5xx http responses to be errors. - * @param status Number http status - * @return Boolean true if we consider this http status an error - */ -RequestBackoff.prototype.isErrorStatus = function(status) { - return ((400 <= status && status <= 599) || - HTTP_FOUND == status || - HTTP_SEE_OTHER == status || - HTTP_TEMPORARY_REDIRECT == status); -} - diff --git a/toolkit/components/url-classifier/content/trtable.js b/toolkit/components/url-classifier/content/trtable.js deleted file mode 100644 index c58a80c9ad..0000000000 --- a/toolkit/components/url-classifier/content/trtable.js +++ /dev/null @@ -1,169 +0,0 @@ -# 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/. - -// XXX: This should all be moved into the dbservice class so it happens -// in the background thread. - -/** - * Abstract base class for a lookup table. - * @construction - */ -this.UrlClassifierTable = function UrlClassifierTable() { - this.debugZone = "urlclassifier-table"; - this.name = ''; - this.needsUpdate = false; - this.enchashDecrypter_ = new PROT_EnchashDecrypter(); - this.wrappedJSObject = this; -} - -UrlClassifierTable.prototype.QueryInterface = function(iid) { - if (iid.equals(Components.interfaces.nsISupports) || - iid.equals(Components.interfaces.nsIUrlClassifierTable)) - return this; - - throw Components.results.NS_ERROR_NO_INTERFACE; -} - -/** - * Subclasses need to implement this method. - */ -UrlClassifierTable.prototype.exists = function(url, callback) { - throw Components.results.NS_ERROR_NOT_IMPLEMENTED; -} - -///////////////////////////////////////////////////////////////////// -// Url table implementation -this.UrlClassifierTableUrl = function UrlClassifierTableUrl() { - UrlClassifierTable.call(this); -} - -UrlClassifierTableUrl.inherits = function(parentCtor) { - var tempCtor = function(){}; - tempCtor.prototype = parentCtor.prototype; - this.superClass_ = parentCtor.prototype; - this.prototype = new tempCtor(); -} -UrlClassifierTableUrl.inherits(UrlClassifierTable); - -/** - * Look up a URL in a URL table - */ -UrlClassifierTableUrl.prototype.exists = function(url, callback) { - // nsIUrlClassifierUtils.canonicalizeURL is the old way of canonicalizing a - // URL. Unfortunately, it doesn't normalize numeric domains so alternate IP - // formats (hex, octal, etc) won't trigger a match. - // this.enchashDecrypter_.getCanonicalUrl does the right thing and - // normalizes a URL to 4 decimal numbers, but the update server may still be - // giving us encoded IP addresses. So to be safe, we check both cases. - var urlUtils = Cc["@mozilla.org/url-classifier/utils;1"] - .getService(Ci.nsIUrlClassifierUtils); - var oldCanonicalized = urlUtils.canonicalizeURL(url); - var canonicalized = this.enchashDecrypter_.getCanonicalUrl(url); - G_Debug(this, "Looking up: " + url + " (" + oldCanonicalized + " and " + - canonicalized + ")"); - (new ExistsMultiQuerier([oldCanonicalized, canonicalized], - this.name, - callback)).run(); -} - -///////////////////////////////////////////////////////////////////// -// Domain table implementation - -this.UrlClassifierTableDomain = function UrlClassifierTableDomain() { - UrlClassifierTable.call(this); - this.debugZone = "urlclassifier-table-domain"; - this.ioService_ = Cc["@mozilla.org/network/io-service;1"] - .getService(Ci.nsIIOService); -} - -UrlClassifierTableDomain.inherits = function(parentCtor) { - var tempCtor = function(){}; - tempCtor.prototype = parentCtor.prototype; - this.superClass_ = parentCtor.prototype; - this.prototype = new tempCtor(); -} -UrlClassifierTableDomain.inherits(UrlClassifierTable); - -/** - * Look up a URL in a domain table - * We also try to lookup domain + first path component (e.g., - * www.mozilla.org/products). - * - * @returns Boolean true if the url domain is in the table - */ -UrlClassifierTableDomain.prototype.exists = function(url, callback) { - var canonicalized = this.enchashDecrypter_.getCanonicalUrl(url); - var urlObj = this.ioService_.newURI(canonicalized, null, null); - var host = ''; - try { - host = urlObj.host; - } catch (e) { } - var hostComponents = host.split("."); - - // Try to get the path of the URL. Pseudo urls (like wyciwyg:) throw - // errors when trying to convert to an nsIURL so we wrap in a try/catch - // block. - var path = "" - try { - urlObj.QueryInterface(Ci.nsIURL); - path = urlObj.filePath; - } catch (e) { } - - var pathComponents = path.split("/"); - - // We don't have a good way map from hosts to domains, so we instead try - // each possibility. Could probably optimize to start at the second dot? - var possible = []; - for (var i = 0; i < hostComponents.length - 1; i++) { - host = hostComponents.slice(i).join("."); - possible.push(host); - - // The path starts with a "/", so we are interested in the second path - // component if it is available - if (pathComponents.length >= 2 && pathComponents[1].length > 0) { - host = host + "/" + pathComponents[1]; - possible.push(host); - } - } - - // Run the possible domains against the db. - (new ExistsMultiQuerier(possible, this.name, callback)).run(); -} - -///////////////////////////////////////////////////////////////////// -// Enchash table implementation - -this.UrlClassifierTableEnchash = function UrlClassifierTableEnchash() { - UrlClassifierTable.call(this); - this.debugZone = "urlclassifier-table-enchash"; -} - -UrlClassifierTableEnchash.inherits = function(parentCtor) { - var tempCtor = function(){}; - tempCtor.prototype = parentCtor.prototype; - this.superClass_ = parentCtor.prototype; - this.prototype = new tempCtor(); -} -UrlClassifierTableEnchash.inherits(UrlClassifierTable); - -/** - * Look up a URL in an enchashDB. We try all sub domains (up to MAX_DOTS). - */ -UrlClassifierTableEnchash.prototype.exists = function(url, callback) { - url = this.enchashDecrypter_.getCanonicalUrl(url); - var host = this.enchashDecrypter_.getCanonicalHost(url, - PROT_EnchashDecrypter.MAX_DOTS); - - var possible = []; - for (var i = 0; i < PROT_EnchashDecrypter.MAX_DOTS + 1; i++) { - possible.push(host); - - var index = host.indexOf("."); - if (index == -1) - break; - host = host.substring(index + 1); - } - // Run the possible domains against the db. - (new EnchashMultiQuerier(possible, this.name, callback, url)).run(); -} diff --git a/toolkit/components/url-classifier/content/wireformat.js b/toolkit/components/url-classifier/content/wireformat.js deleted file mode 100644 index a24b120e6b..0000000000 --- a/toolkit/components/url-classifier/content/wireformat.js +++ /dev/null @@ -1,230 +0,0 @@ -# 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/. - - -// A class that serializes and deserializes opaque key/value string to -// string maps to/from maps (trtables). It knows how to create -// trtables from the serialized format, so it also understands -// meta-information like the name of the table and the table's -// version. See docs for the protocol description. -// -// TODO: wireformatreader: if you have multiple updates for one table -// in a call to deserialize, the later ones will be merged -// (all but the last will be ignored). To fix, merge instead -// of replace when you have an existing table, and only do so once. -// TODO must have blank line between successive types -- problem? -// TODO doesn't tolerate blank lines very well -// -// Maybe: These classes could use a LOT more cleanup, but it's not a -// priority at the moment. For example, the tablesData/Known -// maps should be combined into a single object, the parser -// for a given type should be separate from the version info, -// and there should be synchronous interfaces for testing. - - -/** - * A class that knows how to serialize and deserialize meta-information. - * This meta information is the table name and version number, and - * in its serialized form looks like the first line below: - * - * [name-of-table X.Y update?] - * ...key/value pairs to add or delete follow... - * - * - * The X.Y is the version number and the optional "update" token means - * that the table is a differential from the curent table the extension - * has. Its absence means that this is a full, new table. - */ -this.PROT_VersionParser = -function PROT_VersionParser(type, opt_major, opt_minor, opt_requireMac) { - this.debugZone = "versionparser"; - this.type = type; - this.major = 0; - this.minor = 0; - - this.badHeader = false; - - // Should the wireformatreader compute a mac? - this.mac = false; - this.macval = ""; - this.macFailed = false; - this.requireMac = !!opt_requireMac; - - this.update = false; - this.needsUpdate = false; // used by ListManager to determine update policy - // Used by ListerManager to see if we have read data for this table from - // disk. Once we read a table from disk, we are not going to do so again - // but instead update remotely if necessary. - this.didRead = false; - if (opt_major) - this.major = parseInt(opt_major); - if (opt_minor) - this.minor = parseInt(opt_minor); -} - -/** Import the version information from another VersionParser - * @params version a version parser object - */ -PROT_VersionParser.prototype.ImportVersion = function(version) { - this.major = version.major; - this.minor = version.minor; - - this.mac = version.mac; - this.macFailed = version.macFailed; - this.macval = version.macval; - // Don't set requireMac, since we create vparsers from scratch and doesn't - // know about it -} - -/** - * Creates a string like [goog-white-black 1.1] from internal information - * - * @returns String - */ -PROT_VersionParser.prototype.toString = function() { - var s = "[" + this.type + " " + this.major + "." + this.minor + "]"; - return s; -} - -/** - * Creates a string like 1.123 with the version number. This is the - * format we store in prefs. - * @return String - */ -PROT_VersionParser.prototype.versionString = function() { - return this.major + "." + this.minor; -} - -/** - * Creates a string like 1:1 from internal information used for - * fetching updates from the server. Called by the listmanager. - * - * @returns String - */ -PROT_VersionParser.prototype.toUrl = function() { - return this.major + ":" + this.minor; -} - -/** - * Process the old format, [type major.minor [update]] - * - * @returns true if the string could be parsed, false otherwise - */ -PROT_VersionParser.prototype.processOldFormat_ = function(line) { - if (line[0] != '[' || line.slice(-1) != ']') - return false; - - var description = line.slice(1, -1); - - // Get the type name and version number of this table - var tokens = description.split(" "); - this.type = tokens[0]; - var majorminor = tokens[1].split("."); - this.major = parseInt(majorminor[0]); - this.minor = parseInt(majorminor[1]); - if (isNaN(this.major) || isNaN(this.minor)) - return false; - - if (tokens.length >= 3) { - this.update = tokens[2] == "update"; - } - - return true; -} - -/** - * Takes a string like [name-of-table 1.1 [update]][mac=MAC] and figures out the - * type and corresponding version numbers. - * @returns true if the string could be parsed, false otherwise - */ -PROT_VersionParser.prototype.fromString = function(line) { - G_Debug(this, "Calling fromString with line: " + line); - if (line[0] != '[' || line.slice(-1) != ']') - return false; - - // There could be two [][], so take care of it - var secondBracket = line.indexOf('[', 1); - var firstPart = null; - var secondPart = null; - - if (secondBracket != -1) { - firstPart = line.substring(0, secondBracket); - secondPart = line.substring(secondBracket); - G_Debug(this, "First part: " + firstPart + " Second part: " + secondPart); - } else { - firstPart = line; - G_Debug(this, "Old format: " + firstPart); - } - - if (!this.processOldFormat_(firstPart)) - return false; - - if (secondPart && !this.processOptTokens_(secondPart)) - return false; - - return true; -} - -/** - * Process optional tokens - * - * @param line A string [token1=val1 token2=val2...] - * @returns true if the string could be parsed, false otherwise - */ -PROT_VersionParser.prototype.processOptTokens_ = function(line) { - if (line[0] != '[' || line.slice(-1) != ']') - return false; - var description = line.slice(1, -1); - // Get the type name and version number of this table - var tokens = description.split(" "); - - for (var i = 0; i < tokens.length; i++) { - G_Debug(this, "Processing optional token: " + tokens[i]); - var tokenparts = tokens[i].split("="); - switch(tokenparts[0]){ - case "mac": - this.mac = true; - if (tokenparts.length < 2) { - G_Debug(this, "Found mac flag but not mac value!"); - return false; - } - // The mac value may have "=" in it, so we can't just use tokenparts[1]. - // Instead, just take the rest of tokens[i] after the first "=" - this.macval = tokens[i].substr(tokens[i].indexOf("=")+1); - break; - default: - G_Debug(this, "Found unrecognized token: " + tokenparts[0]); - break; - } - } - - return true; -} - -#ifdef DEBUG -this.TEST_PROT_WireFormat = function TEST_PROT_WireFormat() { - if (G_GDEBUG) { - var z = "versionparser UNITTEST"; - G_Debug(z, "Starting"); - - var vp = new PROT_VersionParser("dummy"); - G_Assert(z, vp.fromString("[foo-bar-url 1.234]"), - "failed to parse old format"); - G_Assert(z, "foo-bar-url" == vp.type, "failed to parse type"); - G_Assert(z, "1" == vp.major, "failed to parse major"); - G_Assert(z, "234" == vp.minor, "failed to parse minor"); - - vp = new PROT_VersionParser("dummy"); - G_Assert(z, vp.fromString("[foo-bar-url 1.234][mac=567]"), - "failed to parse new format"); - G_Assert(z, "foo-bar-url" == vp.type, "failed to parse type"); - G_Assert(z, "1" == vp.major, "failed to parse major"); - G_Assert(z, "234" == vp.minor, "failed to parse minor"); - G_Assert(z, true == vp.mac, "failed to parse mac"); - G_Assert(z, "567" == vp.macval, "failed to parse macval"); - - G_Debug(z, "PASSED"); - } -} -#endif diff --git a/toolkit/components/url-classifier/content/xml-fetcher.js b/toolkit/components/url-classifier/content/xml-fetcher.js deleted file mode 100644 index 2069db44e2..0000000000 --- a/toolkit/components/url-classifier/content/xml-fetcher.js +++ /dev/null @@ -1,122 +0,0 @@ -# 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/. - -// A simple class that encapsulates a request. You'll notice the -// style here is different from the rest of the extension; that's -// because this was re-used from really old code we had. At some -// point it might be nice to replace this with something better -// (e.g., something that has explicit onerror handler, ability -// to set headers, and so on). - -/** - * Because we might be in a component, we can't just assume that - * XMLHttpRequest exists. So we use this tiny factory function to wrap the - * XPCOM version. - * - * @return XMLHttpRequest object - */ -this.PROT_NewXMLHttpRequest = function PROT_NewXMLHttpRequest() { - var Cc = Components.classes; - var Ci = Components.interfaces; - var request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] - .createInstance(Ci.nsIXMLHttpRequest); - // Need the following so we get onerror/load/progresschange - request.QueryInterface(Ci.nsIJSXMLHttpRequest); - return request; -} - -/** - * A helper class that does HTTP GETs and calls back a function with - * the content it receives. Asynchronous, so uses a closure for the - * callback. - * - * Note, that XMLFetcher is only used for SafeBrowsing, therefore - * we inherit from nsILoadContext, so we can use the callbacks on the - * channel to separate the safebrowsing cookie based on a reserved - * appId. - * @constructor - */ -this.PROT_XMLFetcher = function PROT_XMLFetcher() { - this.debugZone = "xmlfetcher"; - this._request = PROT_NewXMLHttpRequest(); - // implements nsILoadContext - this.appId = Ci.nsIScriptSecurityManager.SAFEBROWSING_APP_ID; - this.isInBrowserElement = false; - this.usePrivateBrowsing = false; - this.isContent = false; -} - -PROT_XMLFetcher.prototype = { - /** - * Function that will be called back upon fetch completion. - */ - _callback: null, - - - /** - * Fetches some content. - * - * @param page URL to fetch - * @param callback Function to call back when complete. - */ - get: function(page, callback) { - this._request.abort(); // abort() is asynchronous, so - this._request = PROT_NewXMLHttpRequest(); - this._callback = callback; - var asynchronous = true; - this._request.open("GET", page, asynchronous); - this._request.channel.notificationCallbacks = this; - - // Create a closure - var self = this; - this._request.addEventListener("readystatechange", function() { - self.readyStateChange(self); - }, false); - - this._request.send(null); - }, - - cancel: function() { - this._request.abort(); - this._request = null; - }, - - /** - * Called periodically by the request to indicate some state change. 4 - * means content has been received. - */ - readyStateChange: function(fetcher) { - if (fetcher._request.readyState != 4) - return; - - // If the request fails, on trunk we get status set to - // NS_ERROR_NOT_AVAILABLE. On 1.8.1 branch we get an exception - // forwarded from nsIHttpChannel::GetResponseStatus. To be consistent - // between branch and trunk, we send back NS_ERROR_NOT_AVAILABLE for - // http failures. - var responseText = null; - var status = Components.results.NS_ERROR_NOT_AVAILABLE; - try { - G_Debug(this, "xml fetch status code: \"" + - fetcher._request.status + "\""); - status = fetcher._request.status; - responseText = fetcher._request.responseText; - } catch(e) { - G_Debug(this, "Caught exception trying to read xmlhttprequest " + - "status/response."); - G_Debug(this, e); - } - if (fetcher._callback) - fetcher._callback(responseText, status); - }, - - // nsIInterfaceRequestor - getInterface: function(iid) { - return this.QueryInterface(iid); - }, - - QueryInterface: XPCOMUtils.generateQI([Ci.nsIInterfaceRequestor, - Ci.nsISupports, - Ci.nsILoadContext]) -}; diff --git a/toolkit/components/url-classifier/moz.build b/toolkit/components/url-classifier/moz.build deleted file mode 100644 index a89ddcfa54..0000000000 --- a/toolkit/components/url-classifier/moz.build +++ /dev/null @@ -1,74 +0,0 @@ -# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*- -# vim: set filetype=python: -# 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/. - -TEST_DIRS += ['tests'] - -XPIDL_SOURCES += [ - 'nsIUrlClassifierDBService.idl', - 'nsIUrlClassifierHashCompleter.idl', - 'nsIUrlClassifierPrefixSet.idl', - 'nsIUrlClassifierStreamUpdater.idl', - 'nsIUrlClassifierUtils.idl', - 'nsIUrlListManager.idl', -] - -XPIDL_MODULE = 'url-classifier' - -UNIFIED_SOURCES += [ - 'ChunkSet.cpp', - 'Classifier.cpp', - 'LookupCache.cpp', - 'nsCheckSummedOutputStream.cpp', - 'nsUrlClassifierDBService.cpp', - 'nsUrlClassifierProxies.cpp', - 'nsUrlClassifierUtils.cpp', - 'ProtocolParser.cpp', -] - -# define conflicting LOG() macros -SOURCES += [ - 'nsUrlClassifierPrefixSet.cpp', - 'nsUrlClassifierStreamUpdater.cpp', -] - -# contains variables that conflict with LookupCache.cpp -SOURCES += [ - 'HashStore.cpp', -] - -EXTRA_COMPONENTS += [ - 'nsURLClassifier.manifest', - 'nsUrlClassifierHashCompleter.js', -] - -# Same as JS components that are run through the pre-processor. -EXTRA_PP_COMPONENTS += [ - 'nsUrlClassifierLib.js', - 'nsUrlClassifierListManager.js', -] - -EXTRA_JS_MODULES += [ - 'SafeBrowsing.jsm', -] - -EXPORTS += [ - 'Entries.h', - 'LookupCache.h', - 'nsUrlClassifierPrefixSet.h', -] - -FAIL_ON_WARNINGS = True - -MSVC_ENABLE_PGO = True - -FINAL_LIBRARY = 'xul' - -LOCAL_INCLUDES += [ - '../build', - '/ipc/chromium/src', -] - -CXXFLAGS += CONFIG['SQLITE_CFLAGS'] diff --git a/toolkit/components/url-classifier/nsCheckSummedOutputStream.cpp b/toolkit/components/url-classifier/nsCheckSummedOutputStream.cpp deleted file mode 100644 index 68f9f1f6f4..0000000000 --- a/toolkit/components/url-classifier/nsCheckSummedOutputStream.cpp +++ /dev/null @@ -1,59 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#include "nsILocalFile.h" -#include "nsCRT.h" -#include "nsIFile.h" -#include "nsISupportsImpl.h" -#include "nsCheckSummedOutputStream.h" - -//////////////////////////////////////////////////////////////////////////////// -// nsCheckSummedOutputStream - -NS_IMPL_ISUPPORTS_INHERITED(nsCheckSummedOutputStream, - nsSafeFileOutputStream, - nsISafeOutputStream, - nsIOutputStream, - nsIFileOutputStream) - -NS_IMETHODIMP -nsCheckSummedOutputStream::Init(nsIFile* file, int32_t ioFlags, int32_t perm, - int32_t behaviorFlags) -{ - nsresult rv; - mHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - rv = mHash->Init(nsICryptoHash::MD5); - NS_ENSURE_SUCCESS(rv, rv); - - return nsSafeFileOutputStream::Init(file, ioFlags, perm, behaviorFlags); -} - -NS_IMETHODIMP -nsCheckSummedOutputStream::Finish() -{ - nsresult rv = mHash->Finish(false, mCheckSum); - NS_ENSURE_SUCCESS(rv, rv); - - uint32_t written; - rv = nsSafeFileOutputStream::Write(reinterpret_cast(mCheckSum.BeginReading()), - mCheckSum.Length(), &written); - NS_ASSERTION(written == mCheckSum.Length(), "Error writing stream checksum"); - NS_ENSURE_SUCCESS(rv, rv); - - return nsSafeFileOutputStream::Finish(); -} - -NS_IMETHODIMP -nsCheckSummedOutputStream::Write(const char *buf, uint32_t count, uint32_t *result) -{ - nsresult rv = mHash->Update(reinterpret_cast(buf), count); - NS_ENSURE_SUCCESS(rv, rv); - - return nsSafeFileOutputStream::Write(buf, count, result); -} - -//////////////////////////////////////////////////////////////////////////////// diff --git a/toolkit/components/url-classifier/nsCheckSummedOutputStream.h b/toolkit/components/url-classifier/nsCheckSummedOutputStream.h deleted file mode 100644 index b6ded8f53a..0000000000 --- a/toolkit/components/url-classifier/nsCheckSummedOutputStream.h +++ /dev/null @@ -1,54 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#ifndef nsCheckSummedOutputStream_h__ -#define nsCheckSummedOutputStream_h__ - -#include "nsILocalFile.h" -#include "nsIFile.h" -#include "nsIOutputStream.h" -#include "nsICryptoHash.h" -#include "nsNetCID.h" -#include "nsString.h" -#include "../../../netwerk/base/nsFileStreams.h" -#include "nsToolkitCompsCID.h" - -class nsCheckSummedOutputStream : public nsSafeFileOutputStream -{ -public: - NS_DECL_ISUPPORTS_INHERITED - - // Size of MD5 hash in bytes - static const uint32_t CHECKSUM_SIZE = 16; - - nsCheckSummedOutputStream() {} - - NS_IMETHOD Finish() override; - NS_IMETHOD Write(const char *buf, uint32_t count, uint32_t *result) override; - NS_IMETHOD Init(nsIFile* file, int32_t ioFlags, int32_t perm, int32_t behaviorFlags) override; - -protected: - virtual ~nsCheckSummedOutputStream() { nsSafeFileOutputStream::Close(); } - - nsCOMPtr mHash; - nsAutoCString mCheckSum; -}; - -// returns a file output stream which can be QI'ed to nsIFileOutputStream. -inline nsresult -NS_NewCheckSummedOutputStream(nsIOutputStream **result, - nsIFile *file, - int32_t ioFlags = -1, - int32_t perm = -1, - int32_t behaviorFlags = 0) -{ - nsCOMPtr out = new nsCheckSummedOutputStream(); - nsresult rv = out->Init(file, ioFlags, perm, behaviorFlags); - if (NS_SUCCEEDED(rv)) - NS_ADDREF(*result = out); // cannot use nsCOMPtr::swap - return rv; -} - -#endif diff --git a/toolkit/components/url-classifier/nsIUrlClassifierDBService.idl b/toolkit/components/url-classifier/nsIUrlClassifierDBService.idl deleted file mode 100644 index efe508e7bd..0000000000 --- a/toolkit/components/url-classifier/nsIUrlClassifierDBService.idl +++ /dev/null @@ -1,219 +0,0 @@ -/* 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/. */ - -#include "nsISupports.idl" - -%{C++ -#include "Entries.h" -#include "LookupCache.h" -class nsUrlClassifierLookupResult; -%} -[ptr] native ResultArray(nsTArray); -[ptr] native CacheCompletionArray(nsTArray); -[ptr] native PrefixArray(mozilla::safebrowsing::PrefixArray); - -interface nsIUrlClassifierHashCompleter; -interface nsIPrincipal; - -// Interface for JS function callbacks -[scriptable, function, uuid(4ca27b6b-a674-4b3d-ab30-d21e2da2dffb)] -interface nsIUrlClassifierCallback : nsISupports { - void handleEvent(in ACString value); -}; - -/** - * The nsIUrlClassifierUpdateObserver interface is implemented by - * clients streaming updates to the url-classifier (usually - * nsUrlClassifierStreamUpdater. - */ -[scriptable, uuid(9fa11561-5816-4e1b-bcc9-b629ca05cce6)] -interface nsIUrlClassifierUpdateObserver : nsISupports { - /** - * The update requested a new URL whose contents should be downloaded - * and sent to the classifier as a new stream. - * - * @param url The url that was requested. - * @param table The table name that this URL's contents will be associated - * with. This should be passed back to beginStream(). - */ - void updateUrlRequested(in ACString url, - in ACString table); - - /** - * A stream update has completed. - * - * @param status The state of the update process. - * @param delay The amount of time the updater should wait to fetch the - * next URL in ms. - */ - void streamFinished(in nsresult status, in unsigned long delay); - - /* The update has encountered an error and should be cancelled */ - void updateError(in nsresult error); - - /** - * The update has completed successfully. - * - * @param requestedTimeout The number of seconds that the caller should - * wait before trying to update again. - **/ - void updateSuccess(in unsigned long requestedTimeout); -}; - -/** - * This is a proxy class that is instantiated and called from the JS thread. - * It provides async methods for querying and updating the database. As the - * methods complete, they call the callback function. - */ -[scriptable, uuid(3f9e61e5-01bd-45d0-8dd2-f1abcd20dbb7)] -interface nsIUrlClassifierDBService : nsISupports -{ - /** - * Looks up a URI in the specified tables. - * - * @param principal: The principal containing the URI to search. - * @param c: The callback will be called with a comma-separated list - * of tables to which the key belongs. - */ - void lookup(in nsIPrincipal principal, - in ACString tables, - in nsIUrlClassifierCallback c); - - /** - * Lists the tables along with which chunks are available in each table. - * This list is in the format of the request body: - * tablename;chunkdata\n - * tablename2;chunkdata2\n - * - * For example: - * goog-phish-regexp;a:10,14,30-40s:56,67 - * goog-white-regexp;a:1-3,5 - */ - void getTables(in nsIUrlClassifierCallback c); - - /** - * Set the nsIUrlClassifierCompleter object for a given table. This - * object will be used to request complete versions of partial - * hashes. - */ - void setHashCompleter(in ACString tableName, - in nsIUrlClassifierHashCompleter completer); - - //////////////////////////////////////////////////////////////////////////// - // Incremental update methods. - // - // An update to the database has the following steps: - // - // 1) The update process is started with beginUpdate(). The client - // passes an nsIUrlClassifierUpdateObserver object which will be - // notified as the update is processed by the dbservice. - // 2) The client sends an initial update stream to the dbservice, - // using beginStream/updateStream/finishStream. - // 3) While reading this initial update stream, the dbservice may - // request additional streams from the client as requested by the - // update stream. - // 4) For each additional update stream, the client feeds the - // contents to the dbservice using beginStream/updateStream/endStream. - // 5) Once all streams have been processed, the client calls - // finishUpdate. When the dbservice has finished processing - // all streams, it will notify the observer that the update process - // is complete. - - /** - * Begin an update process. Will throw NS_ERROR_NOT_AVAILABLE if there - * is already an update in progress. - * - * @param updater The update observer tied to this update. - * @param tables A comma-separated list of tables included in this update. - */ - void beginUpdate(in nsIUrlClassifierUpdateObserver updater, - in ACString tables); - - /** - * Begin a stream update. This should be called once per url being - * fetched. - * - * @param table The table the contents of this stream will be associated - * with, or empty for the initial stream. - */ - void beginStream(in ACString table); - - /** - * Update the table incrementally. - */ - void updateStream(in ACString updateChunk); - - // It would be nice to have an updateFromStream method to round out the - // interface, but it's tricky because of XPCOM proxies. - - /** - * Finish an individual stream update. Must be called for every - * beginStream() call, before the next beginStream() or finishUpdate(). - * - * The update observer's streamFinished will be called once the - * stream has been processed. - */ - void finishStream(); - - /** - * Finish an incremental update. This will attempt to commit any - * pending changes and resets the update interface. - * - * The update observer's updateSucceeded or updateError methods - * will be called when the update has been processed. - */ - void finishUpdate(); - - /** - * Cancel an incremental update. This rolls back any pending changes. - * and resets the update interface. - * - * The update observer's updateError method will be called when the - * update has been rolled back. - */ - void cancelUpdate(); - - /** - * Reset the url-classifier database. This call will delete the existing - * database, emptying all tables. Mostly intended for use in unit tests. - */ - void resetDatabase(); -}; - -/** - * Interface for the actual worker thread. Implementations of this need not - * be thread aware and just work on the database. - */ -[scriptable, uuid(b7b505d0-bfa2-44db-abf8-6e2bfc25bbab)] -interface nsIUrlClassifierDBServiceWorker : nsIUrlClassifierDBService -{ - // Open the DB connection - void openDb(); - // Provide a way to forcibly close the db connection. - void closeDb(); - - [noscript]void cacheCompletions(in CacheCompletionArray completions); - [noscript]void cacheMisses(in PrefixArray misses); -}; - -/** - * This is an internal helper interface for communication between the - * main thread and the dbservice worker thread. It is called for each - * lookup to provide a set of possible results, which the main thread - * may need to expand using an nsIUrlClassifierCompleter. - */ -[uuid(b903dc8f-dff1-42fe-894b-36e7a59bb801)] -interface nsIUrlClassifierLookupCallback : nsISupports -{ - /** - * The lookup process is complete. - * - * @param results - * If this parameter is null, there were no results found. - * If not, it contains an array of nsUrlClassifierEntry objects - * with possible matches. The callee is responsible for freeing - * this array. - */ - void lookupComplete(in ResultArray results); -}; diff --git a/toolkit/components/url-classifier/nsIUrlClassifierHashCompleter.idl b/toolkit/components/url-classifier/nsIUrlClassifierHashCompleter.idl deleted file mode 100644 index a3a8ab6171..0000000000 --- a/toolkit/components/url-classifier/nsIUrlClassifierHashCompleter.idl +++ /dev/null @@ -1,65 +0,0 @@ -/* 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/. */ - -#include "nsISupports.idl" - -/** - * This interface is implemented by nsIUrlClassifierHashCompleter clients. - */ -[scriptable, uuid(da16de40-df26-414d-bde7-c4faf4504868)] -interface nsIUrlClassifierHashCompleterCallback : nsISupports -{ - /** - * A complete hash has been found that matches the partial hash. - * This method may be called 0-n times for a given - * nsIUrlClassifierCompleter::complete() call. - * - * @param hash - * The 128-bit hash that was discovered. - * @param table - * The name of the table that this hash belongs to. - * @param chunkId - * The database chunk that this hash belongs to. - */ - void completion(in ACString hash, - in ACString table, - in uint32_t chunkId); - - /** - * The completion is complete. This method is called once per - * nsIUrlClassifierCompleter::complete() call, after all completion() - * calls are finished. - * - * @param status - * NS_OK if the request completed successfully, or an error code. - */ - void completionFinished(in nsresult status); -}; - -/** - * Clients updating the url-classifier database have the option of sending - * partial (32-bit) hashes of URL fragments to be blacklisted. If the - * url-classifier encounters one of these truncated hashes, it will ask an - * nsIUrlClassifierCompleter instance to asynchronously provide the complete - * hash, along with some associated metadata. - * This is only ever used for testing and should absolutely be deleted (I - * think). - */ -[scriptable, uuid(231fb2ad-ea8a-4e63-a331-eafc3b434811)] -interface nsIUrlClassifierHashCompleter : nsISupports -{ - /** - * Request a completed hash from the given gethash url. - * - * @param partialHash - * The 32-bit hash encountered by the url-classifier. - * @param gethashUrl - * The gethash url to use. - * @param callback - * An nsIUrlClassifierCompleterCallback instance. - */ - void complete(in ACString partialHash, - in ACString gethashUrl, - in nsIUrlClassifierHashCompleterCallback callback); -}; diff --git a/toolkit/components/url-classifier/nsIUrlClassifierPrefixSet.idl b/toolkit/components/url-classifier/nsIUrlClassifierPrefixSet.idl deleted file mode 100644 index 7e1a527d76..0000000000 --- a/toolkit/components/url-classifier/nsIUrlClassifierPrefixSet.idl +++ /dev/null @@ -1,29 +0,0 @@ -/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#include "nsISupports.idl" -#include "nsIFile.idl" - -// Note that the PrefixSet name is historical and we do properly support -// duplicated values, so it's really a Prefix Trie. -// All methods are thread-safe. -[scriptable, uuid(3d8579f0-75fa-4e00-ba41-38661d5b5d17)] -interface nsIUrlClassifierPrefixSet : nsISupports -{ - // Initialize the PrefixSet. Give it a name for memory reporting. - void init(in ACString aName); - // Fills the PrefixSet with the given array of prefixes. - // Can send an empty Array to clear the tree. - // Requires array to be sorted. - void setPrefixes([const, array, size_is(aLength)] in unsigned long aPrefixes, - in unsigned long aLength); - void getPrefixes(out unsigned long aCount, - [array, size_is(aCount), retval] out unsigned long aPrefixes); - // Do a lookup in the PrefixSet, return whether the value is present. - boolean contains(in unsigned long aPrefix); - boolean isEmpty(); - void loadFromFile(in nsIFile aFile); - void storeToFile(in nsIFile aFile); -}; diff --git a/toolkit/components/url-classifier/nsIUrlClassifierStreamUpdater.idl b/toolkit/components/url-classifier/nsIUrlClassifierStreamUpdater.idl deleted file mode 100644 index ff80b856f0..0000000000 --- a/toolkit/components/url-classifier/nsIUrlClassifierStreamUpdater.idl +++ /dev/null @@ -1,36 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* 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/. */ - -#include "nsISupports.idl" -#include "nsIUrlClassifierDBService.idl" - -/** - * This is a class to manage large table updates from the server. Rather than - * downloading the whole update and then updating the sqlite database, we - * update tables as the data is streaming in. - */ -[scriptable, uuid(e1797597-f4d6-4dd3-a1e1-745ad352cd80)] -interface nsIUrlClassifierStreamUpdater : nsISupports -{ - /** - * Try to download updates from updateUrl. If an update is already in - * progress, queues the requested update. This is used in nsIUrlListManager - * as well as in testing. - * @param aRequestTables Comma-separated list of tables included in this - * update. - * @param aRequestBody The body for the request. - * @param aUpdateUrl The plaintext url from which to request updates. - * @param aSuccessCallback Called after a successful update. - * @param aUpdateErrorCallback Called for problems applying the update - * @param aDownloadErrorCallback Called if we get an http error or a - * connection refused error. - */ - boolean downloadUpdates(in ACString aRequestTables, - in ACString aRequestBody, - in ACString aUpdateUrl, - in nsIUrlClassifierCallback aSuccessCallback, - in nsIUrlClassifierCallback aUpdateErrorCallback, - in nsIUrlClassifierCallback aDownloadErrorCallback); -}; diff --git a/toolkit/components/url-classifier/nsIUrlClassifierTable.idl b/toolkit/components/url-classifier/nsIUrlClassifierTable.idl deleted file mode 100644 index 123069556e..0000000000 --- a/toolkit/components/url-classifier/nsIUrlClassifierTable.idl +++ /dev/null @@ -1,31 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* 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/. */ - -#include "nsISupports.idl" -#include "nsIUrlListManager.idl" - -// A map that contains a string keys mapped to string values. - -[scriptable, uuid(fd1f8334-1859-472d-b01f-4ac6b1121ce4)] -interface nsIUrlClassifierTable : nsISupports -{ - /** - * The name used to identify this table - */ - attribute ACString name; - - /** - * Set to false if we don't want to update this table. - */ - attribute boolean needsUpdate; - - /** - * In the simple case, exists just looks up the string in the - * table and call the callback after the query returns with true or - * false. It's possible that something more complex happens - * (e.g., canonicalize the url). - */ - void exists(in ACString key, in nsIUrlListManagerCallback cb); -}; diff --git a/toolkit/components/url-classifier/nsIUrlClassifierUtils.idl b/toolkit/components/url-classifier/nsIUrlClassifierUtils.idl deleted file mode 100644 index 56fe17195c..0000000000 --- a/toolkit/components/url-classifier/nsIUrlClassifierUtils.idl +++ /dev/null @@ -1,24 +0,0 @@ -/* 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/. */ - -#include "nsISupports.idl" -/** - * Some utility methods used by the url classifier. - */ - -interface nsIURI; - -[scriptable, uuid(e4f0e59c-b922-48b0-a7b6-1735c1f96fed)] -interface nsIUrlClassifierUtils : nsISupports -{ - /** - * Get the lookup string for a given URI. This normalizes the hostname, - * url-decodes the string, and strips off the protocol. - * - * @param uri URI to get the lookup key for. - * - * @returns String containing the canonicalized URI. - */ - ACString getKeyForURI(in nsIURI uri); -}; diff --git a/toolkit/components/url-classifier/nsIUrlListManager.idl b/toolkit/components/url-classifier/nsIUrlListManager.idl deleted file mode 100644 index 3c5beb6a24..0000000000 --- a/toolkit/components/url-classifier/nsIUrlListManager.idl +++ /dev/null @@ -1,65 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* 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/. */ - -#include "nsISupports.idl" - -interface nsIPrincipal; - -/** - * Interface for a class that manages updates of the url classifier database. - */ - -// Interface for JS function callbacks -[scriptable, function, uuid(fa4caf12-d057-4e7e-81e9-ce066ceee90b)] -interface nsIUrlListManagerCallback : nsISupports { - void handleEvent(in ACString value); -}; - - -[scriptable, uuid(5d5ed98f-72cd-46b6-a9fe-76418adfdfeb)] -interface nsIUrlListManager : nsISupports -{ - /** - * Get the gethash url for this table - */ - ACString getGethashUrl(in ACString tableName); - - /** - * Add a table to the list of tables we are managing. The name is a - * string of the format provider_name-semantic_type-table_type. For - * @param tableName A string of the format - * provider_name-semantic_type-table_type. For example, - * goog-white-enchash or goog-black-url. - * @param updateUrl The URL from which to fetch updates. - * @param gethashUrl The URL from which to fetch hash completions. - */ - boolean registerTable(in ACString tableName, - in ACString updateUrl, - in ACString gethashUrl); - - /** - * Turn on update checking for a table. I.e., during the next server - * check, download updates for this table. - */ - void enableUpdate(in ACString tableName); - - /** - * Turn off update checking for a table. - */ - void disableUpdate(in ACString tableName); - - /** - * Toggle update checking, if necessary. - */ - void maybeToggleUpdateChecking(); - - /** - * Lookup a key. Should not raise exceptions. Calls the callback - * function with a comma-separated list of tables to which the key - * belongs. - */ - void safeLookup(in nsIPrincipal key, - in nsIUrlListManagerCallback cb); -}; diff --git a/toolkit/components/url-classifier/nsURLClassifier.manifest b/toolkit/components/url-classifier/nsURLClassifier.manifest deleted file mode 100644 index f035dea809..0000000000 --- a/toolkit/components/url-classifier/nsURLClassifier.manifest +++ /dev/null @@ -1,6 +0,0 @@ -component {26a4a019-2827-4a89-a85c-5931a678823a} nsUrlClassifierLib.js -contract @mozilla.org/url-classifier/jslib;1 {26a4a019-2827-4a89-a85c-5931a678823a} -component {ca168834-cc00-48f9-b83c-fd018e58cae3} nsUrlClassifierListManager.js -contract @mozilla.org/url-classifier/listmanager;1 {ca168834-cc00-48f9-b83c-fd018e58cae3} -component {9111de73-9322-4bfc-8b65-2b727f3e6ec8} nsUrlClassifierHashCompleter.js -contract @mozilla.org/url-classifier/hashcompleter;1 {9111de73-9322-4bfc-8b65-2b727f3e6ec8} diff --git a/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp b/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp deleted file mode 100644 index b49f4b6896..0000000000 --- a/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp +++ /dev/null @@ -1,1593 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#include "nsAutoPtr.h" -#include "nsCOMPtr.h" -#include "nsAppDirectoryServiceDefs.h" -#include "nsCRT.h" -#include "nsICryptoHash.h" -#include "nsICryptoHMAC.h" -#include "nsIDirectoryService.h" -#include "nsIKeyModule.h" -#include "nsIObserverService.h" -#include "nsIPermissionManager.h" -#include "nsIPrefBranch.h" -#include "nsIPrefService.h" -#include "nsIProperties.h" -#include "nsToolkitCompsCID.h" -#include "nsIUrlClassifierUtils.h" -#include "nsUrlClassifierDBService.h" -#include "nsUrlClassifierUtils.h" -#include "nsUrlClassifierProxies.h" -#include "nsURILoader.h" -#include "nsString.h" -#include "nsReadableUtils.h" -#include "nsTArray.h" -#include "nsNetUtil.h" -#include "nsNetCID.h" -#include "nsThreadUtils.h" -#include "nsXPCOMStrings.h" -#include "nsProxyRelease.h" -#include "nsString.h" -#include "mozilla/Atomics.h" -#include "mozilla/DebugOnly.h" -#include "mozilla/Mutex.h" -#include "mozilla/Preferences.h" -#include "mozilla/TimeStamp.h" -#include "mozilla/Telemetry.h" -#include "prlog.h" -#include "prprf.h" -#include "prnetdb.h" -#include "Entries.h" -#include "HashStore.h" -#include "Classifier.h" -#include "ProtocolParser.h" -#include "mozilla/Attributes.h" -#include "nsIPrincipal.h" -#include "Classifier.h" -#include "ProtocolParser.h" -#include "nsContentUtils.h" - -using namespace mozilla; -using namespace mozilla::safebrowsing; - -// NSPR_LOG_MODULES=UrlClassifierDbService:5 -#if defined(PR_LOGGING) -PRLogModuleInfo *gUrlClassifierDbServiceLog = nullptr; -#define LOG(args) PR_LOG(gUrlClassifierDbServiceLog, PR_LOG_DEBUG, args) -#define LOG_ENABLED() PR_LOG_TEST(gUrlClassifierDbServiceLog, 4) -#else -#define LOG(args) -#define LOG_ENABLED() (false) -#endif - -// Prefs for implementing nsIURIClassifier to block page loads -#define CHECK_MALWARE_PREF "browser.safebrowsing.malware.enabled" -#define CHECK_MALWARE_DEFAULT false - -#define CHECK_PHISHING_PREF "browser.safebrowsing.enabled" -#define CHECK_PHISHING_DEFAULT false - -#define CHECK_TRACKING_PREF "privacy.trackingprotection.enabled" -#define CHECK_TRACKING_DEFAULT false - -#define GETHASH_NOISE_PREF "urlclassifier.gethashnoise" -#define GETHASH_NOISE_DEFAULT 4 - -// Comma-separated lists -#define MALWARE_TABLE_PREF "urlclassifier.malwareTable" -#define PHISH_TABLE_PREF "urlclassifier.phishTable" -#define TRACKING_TABLE_PREF "urlclassifier.trackingTable" -#define DOWNLOAD_BLOCK_TABLE_PREF "urlclassifier.downloadBlockTable" -#define DOWNLOAD_ALLOW_TABLE_PREF "urlclassifier.downloadAllowTable" -#define DISALLOW_COMPLETION_TABLE_PREF "urlclassifier.disallow_completions" - -#define CONFIRM_AGE_PREF "urlclassifier.max-complete-age" -#define CONFIRM_AGE_DEFAULT_SEC (45 * 60) - -class nsUrlClassifierDBServiceWorker; - -// Singleton instance. -static nsUrlClassifierDBService* sUrlClassifierDBService; - -nsIThread* nsUrlClassifierDBService::gDbBackgroundThread = nullptr; - -// Once we've committed to shutting down, don't do work in the background -// thread. -static bool gShuttingDownThread = false; - -static mozilla::Atomic gFreshnessGuarantee(CONFIRM_AGE_DEFAULT_SEC); - -NS_IMPL_ISUPPORTS(nsUrlClassifierDBServiceWorker, - nsIUrlClassifierDBServiceWorker, - nsIUrlClassifierDBService) - -nsUrlClassifierDBServiceWorker::nsUrlClassifierDBServiceWorker() - : mInStream(false) - , mGethashNoise(0) - , mPendingLookupLock("nsUrlClassifierDBServerWorker.mPendingLookupLock") -{ -} - -nsUrlClassifierDBServiceWorker::~nsUrlClassifierDBServiceWorker() -{ - NS_ASSERTION(!mClassifier, - "Db connection not closed, leaking memory! Call CloseDb " - "to close the connection."); -} - -nsresult -nsUrlClassifierDBServiceWorker::Init(uint32_t aGethashNoise, - nsCOMPtr aCacheDir) -{ - mGethashNoise = aGethashNoise; - mCacheDir = aCacheDir; - - ResetUpdate(); - - return NS_OK; -} - -nsresult -nsUrlClassifierDBServiceWorker::QueueLookup(const nsACString& spec, - const nsACString& tables, - nsIUrlClassifierLookupCallback* callback) -{ - MutexAutoLock lock(mPendingLookupLock); - - PendingLookup* lookup = mPendingLookups.AppendElement(); - if (!lookup) return NS_ERROR_OUT_OF_MEMORY; - - lookup->mStartTime = TimeStamp::Now(); - lookup->mKey = spec; - lookup->mCallback = callback; - lookup->mTables = tables; - - return NS_OK; -} - -nsresult -nsUrlClassifierDBServiceWorker::DoLocalLookup(const nsACString& spec, - const nsACString& tables, - LookupResultArray* results) -{ - MOZ_ASSERT(!NS_IsMainThread(), "DoLocalLookup must be on background thread"); - if (!results) { - return NS_ERROR_FAILURE; - } - // Bail if we haven't been initialized on the background thread. - if (!mClassifier) { - return NS_ERROR_NOT_AVAILABLE; - } - - // We ignore failures from Check because we'd rather return the - // results that were found than fail. - mClassifier->Check(spec, tables, gFreshnessGuarantee, *results); - - LOG(("Found %d results.", results->Length())); - return NS_OK; -} - -static nsresult -TablesToResponse(const nsACString& tables, - bool checkMalware, - bool checkPhishing, - bool checkTracking) -{ - if (checkMalware && - FindInReadable(NS_LITERAL_CSTRING("-malware-"), tables)) { - return NS_ERROR_MALWARE_URI; - } - if (checkPhishing && - FindInReadable(NS_LITERAL_CSTRING("-phish-"), tables)) { - return NS_ERROR_PHISHING_URI; - } - if (checkTracking && - FindInReadable(NS_LITERAL_CSTRING("-track-"), tables)) { - return NS_ERROR_TRACKING_URI; - } - return NS_OK; -} - -static nsresult -ProcessLookupResults(LookupResultArray* results, - bool checkMalware, - bool checkPhishing, - bool checkTracking) -{ - // Build a stringified list of result tables. - nsTArray tables; - for (uint32_t i = 0; i < results->Length(); i++) { - LookupResult& result = results->ElementAt(i); - MOZ_ASSERT(!result.mNoise, "Lookup results should not have noise added"); - LOG(("Found result from table %s", result.mTableName.get())); - if (tables.IndexOf(result.mTableName) == nsTArray::NoIndex) { - tables.AppendElement(result.mTableName); - } - } - nsAutoCString tableStr; - for (uint32_t i = 0; i < tables.Length(); i++) { - if (i != 0) - tableStr.Append(','); - tableStr.Append(tables[i]); - } - return TablesToResponse(tableStr, checkMalware, checkPhishing, checkTracking); -} - -/** - * Lookup up a key in the database is a two step process: - * - * a) First we look for any Entries in the database that might apply to this - * url. For each URL there are one or two possible domain names to check: - * the two-part domain name (example.com) and the three-part name - * (www.example.com). We check the database for both of these. - * b) If we find any entries, we check the list of fragments for that entry - * against the possible subfragments of the URL as described in the - * "Simplified Regular Expression Lookup" section of the protocol doc. - */ -nsresult -nsUrlClassifierDBServiceWorker::DoLookup(const nsACString& spec, - const nsACString& tables, - nsIUrlClassifierLookupCallback* c) -{ - if (gShuttingDownThread) { - c->LookupComplete(nullptr); - return NS_ERROR_NOT_INITIALIZED; - } - -#if defined(PR_LOGGING) - PRIntervalTime clockStart = 0; - if (LOG_ENABLED()) { - clockStart = PR_IntervalNow(); - } -#endif - - nsAutoPtr results(new LookupResultArray()); - if (!results) { - c->LookupComplete(nullptr); - return NS_ERROR_OUT_OF_MEMORY; - } - - nsresult rv = DoLocalLookup(spec, tables, results); - if (NS_FAILED(rv)) { - c->LookupComplete(nullptr); - return rv; - } - - LOG(("Found %d results.", results->Length())); - - -#if defined(PR_LOGGING) - if (LOG_ENABLED()) { - PRIntervalTime clockEnd = PR_IntervalNow(); - LOG(("query took %dms\n", - PR_IntervalToMilliseconds(clockEnd - clockStart))); - } -#endif - - nsAutoPtr completes(new LookupResultArray()); - - for (uint32_t i = 0; i < results->Length(); i++) { - if (!mMissCache.Contains(results->ElementAt(i).hash.prefix)) { - completes->AppendElement(results->ElementAt(i)); - } - } - - for (uint32_t i = 0; i < completes->Length(); i++) { - if (!completes->ElementAt(i).Confirmed()) { - // We're going to be doing a gethash request, add some extra entries. - // Note that we cannot pass the first two by reference, because we - // add to completes, whicah can cause completes to reallocate and move. - AddNoise(completes->ElementAt(i).hash.prefix, - completes->ElementAt(i).mTableName, - mGethashNoise, *completes); - break; - } - } - - // At this point ownership of 'results' is handed to the callback. - c->LookupComplete(completes.forget()); - - return NS_OK; -} - -nsresult -nsUrlClassifierDBServiceWorker::HandlePendingLookups() -{ - MutexAutoLock lock(mPendingLookupLock); - while (mPendingLookups.Length() > 0) { - PendingLookup lookup = mPendingLookups[0]; - mPendingLookups.RemoveElementAt(0); - { - MutexAutoUnlock unlock(mPendingLookupLock); - DoLookup(lookup.mKey, lookup.mTables, lookup.mCallback); - } - double lookupTime = (TimeStamp::Now() - lookup.mStartTime).ToMilliseconds(); - Telemetry::Accumulate(Telemetry::URLCLASSIFIER_LOOKUP_TIME, - static_cast(lookupTime)); - } - - return NS_OK; -} - -nsresult -nsUrlClassifierDBServiceWorker::AddNoise(const Prefix aPrefix, - const nsCString tableName, - uint32_t aCount, - LookupResultArray& results) -{ - if (aCount < 1) { - return NS_OK; - } - - PrefixArray noiseEntries; - nsresult rv = mClassifier->ReadNoiseEntries(aPrefix, tableName, - aCount, &noiseEntries); - NS_ENSURE_SUCCESS(rv, rv); - - for (uint32_t i = 0; i < noiseEntries.Length(); i++) { - LookupResult *result = results.AppendElement(); - if (!result) - return NS_ERROR_OUT_OF_MEMORY; - - result->hash.prefix = noiseEntries[i]; - result->mNoise = true; - - result->mTableName.Assign(tableName); - } - - return NS_OK; -} - -// Lookup a key in the db. -NS_IMETHODIMP -nsUrlClassifierDBServiceWorker::Lookup(nsIPrincipal* aPrincipal, - const nsACString& aTables, - nsIUrlClassifierCallback* c) -{ - return HandlePendingLookups(); -} - -NS_IMETHODIMP -nsUrlClassifierDBServiceWorker::GetTables(nsIUrlClassifierCallback* c) -{ - if (gShuttingDownThread) - return NS_ERROR_NOT_INITIALIZED; - - nsresult rv = OpenDb(); - if (NS_FAILED(rv)) { - NS_ERROR("Unable to open SafeBrowsing database"); - return NS_ERROR_FAILURE; - } - - NS_ENSURE_SUCCESS(rv, rv); - - nsAutoCString response; - mClassifier->TableRequest(response); - LOG(("GetTables: %s", response.get())); - c->HandleEvent(response); - - return rv; -} - -void -nsUrlClassifierDBServiceWorker::ResetStream() -{ - LOG(("ResetStream")); - mInStream = false; - mProtocolParser = nullptr; -} - -void -nsUrlClassifierDBServiceWorker::ResetUpdate() -{ - LOG(("ResetUpdate")); - mUpdateWait = 0; - mUpdateStatus = NS_OK; - mUpdateObserver = nullptr; -} - -NS_IMETHODIMP -nsUrlClassifierDBServiceWorker::SetHashCompleter(const nsACString &tableName, - nsIUrlClassifierHashCompleter *completer) -{ - return NS_ERROR_NOT_IMPLEMENTED; -} - -NS_IMETHODIMP -nsUrlClassifierDBServiceWorker::BeginUpdate(nsIUrlClassifierUpdateObserver *observer, - const nsACString &tables) -{ - LOG(("nsUrlClassifierDBServiceWorker::BeginUpdate [%s]", PromiseFlatCString(tables).get())); - - if (gShuttingDownThread) { - return NS_ERROR_NOT_INITIALIZED; - } - - NS_ENSURE_STATE(!mUpdateObserver); - - nsresult rv = OpenDb(); - if (NS_FAILED(rv)) { - NS_ERROR("Unable to open SafeBrowsing database"); - return NS_ERROR_FAILURE; - } - - mUpdateStatus = NS_OK; - mUpdateObserver = observer; - Classifier::SplitTables(tables, mUpdateTables); - - return NS_OK; -} - -// Called from the stream updater. -NS_IMETHODIMP -nsUrlClassifierDBServiceWorker::BeginStream(const nsACString &table) -{ - LOG(("nsUrlClassifierDBServiceWorker::BeginStream")); - MOZ_ASSERT(!NS_IsMainThread(), "Streaming must be on the background thread"); - - if (gShuttingDownThread) - return NS_ERROR_NOT_INITIALIZED; - - NS_ENSURE_STATE(mUpdateObserver); - NS_ENSURE_STATE(!mInStream); - - mInStream = true; - - NS_ASSERTION(!mProtocolParser, "Should not have a protocol parser."); - - mProtocolParser = new ProtocolParser(); - if (!mProtocolParser) - return NS_ERROR_OUT_OF_MEMORY; - - mProtocolParser->Init(mCryptoHash); - - if (!table.IsEmpty()) { - mProtocolParser->SetCurrentTable(table); - } - - return NS_OK; -} - -/** - * Updating the database: - * - * The Update() method takes a series of chunks separated with control data, - * as described in - * http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec - * - * It will iterate through the control data until it reaches a chunk. By - * the time it reaches a chunk, it should have received - * a) the table to which this chunk applies - * b) the type of chunk (add, delete, expire add, expire delete). - * c) the chunk ID - * d) the length of the chunk. - * - * For add and subtract chunks, it needs to read the chunk data (expires - * don't have any data). Chunk data is a list of URI fragments whose - * encoding depends on the type of table (which is indicated by the end - * of the table name): - * a) tables ending with -exp are a zlib-compressed list of URI fragments - * separated by newlines. - * b) tables ending with -sha128 have the form - * [domain][N][frag0]...[fragN] - * 16 1 16 16 - * If N is 0, the domain is reused as a fragment. - * c) any other tables are assumed to be a plaintext list of URI fragments - * separated by newlines. - * - * Update() can be fed partial data; It will accumulate data until there is - * enough to act on. Finish() should be called when there will be no more - * data. - */ -NS_IMETHODIMP -nsUrlClassifierDBServiceWorker::UpdateStream(const nsACString& chunk) -{ - if (gShuttingDownThread) - return NS_ERROR_NOT_INITIALIZED; - - NS_ENSURE_STATE(mInStream); - - HandlePendingLookups(); - - // Feed the chunk to the parser. - return mProtocolParser->AppendStream(chunk); -} - -NS_IMETHODIMP -nsUrlClassifierDBServiceWorker::FinishStream() -{ - if (gShuttingDownThread) { - LOG(("shutting down")); - return NS_ERROR_NOT_INITIALIZED; - } - - NS_ENSURE_STATE(mInStream); - NS_ENSURE_STATE(mUpdateObserver); - - mInStream = false; - - if (NS_SUCCEEDED(mProtocolParser->Status())) { - if (mProtocolParser->UpdateWait()) { - mUpdateWait = mProtocolParser->UpdateWait(); - } - // XXX: Only allow forwards from the initial update? - const nsTArray &forwards = - mProtocolParser->Forwards(); - for (uint32_t i = 0; i < forwards.Length(); i++) { - const ProtocolParser::ForwardedUpdate &forward = forwards[i]; - mUpdateObserver->UpdateUrlRequested(forward.url, forward.table); - } - // Hold on to any TableUpdate objects that were created by the - // parser. - mTableUpdates.AppendElements(mProtocolParser->GetTableUpdates()); - mProtocolParser->ForgetTableUpdates(); - } else { - mUpdateStatus = mProtocolParser->Status(); - } - mUpdateObserver->StreamFinished(mProtocolParser->Status(), 0); - - if (NS_SUCCEEDED(mUpdateStatus)) { - if (mProtocolParser->ResetRequested()) { - mClassifier->Reset(); - } - } - - mProtocolParser = nullptr; - - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierDBServiceWorker::FinishUpdate() -{ - if (gShuttingDownThread) - return NS_ERROR_NOT_INITIALIZED; - NS_ENSURE_STATE(mUpdateObserver); - - if (NS_SUCCEEDED(mUpdateStatus)) { - mUpdateStatus = ApplyUpdate(); - } - - mMissCache.Clear(); - - if (NS_SUCCEEDED(mUpdateStatus)) { - LOG(("Notifying success: %d", mUpdateWait)); - mUpdateObserver->UpdateSuccess(mUpdateWait); - } else { - LOG(("Notifying error: %d", mUpdateStatus)); - mUpdateObserver->UpdateError(mUpdateStatus); - /* - * mark the tables as spoiled, we don't want to block hosts - * longer than normal because our update failed - */ - mClassifier->MarkSpoiled(mUpdateTables); - } - mUpdateObserver = nullptr; - - return NS_OK; -} - -nsresult -nsUrlClassifierDBServiceWorker::ApplyUpdate() -{ - LOG(("nsUrlClassifierDBServiceWorker::ApplyUpdate()")); - return mClassifier->ApplyUpdates(&mTableUpdates); -} - -NS_IMETHODIMP -nsUrlClassifierDBServiceWorker::ResetDatabase() -{ - nsresult rv = OpenDb(); - - if (NS_SUCCEEDED(rv)) { - mClassifier->Reset(); - } - - rv = CloseDb(); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierDBServiceWorker::CancelUpdate() -{ - LOG(("nsUrlClassifierDBServiceWorker::CancelUpdate")); - - if (mUpdateObserver) { - LOG(("UpdateObserver exists, cancelling")); - - mUpdateStatus = NS_BINDING_ABORTED; - - mUpdateObserver->UpdateError(mUpdateStatus); - - /* - * mark the tables as spoiled, we don't want to block hosts - * longer than normal because our update failed - */ - mClassifier->MarkSpoiled(mUpdateTables); - - ResetStream(); - ResetUpdate(); - } else { - LOG(("No UpdateObserver, nothing to cancel")); - } - - return NS_OK; -} - -// Allows the main thread to delete the connection which may be in -// a background thread. -// XXX This could be turned into a single shutdown event so the logic -// is simpler in nsUrlClassifierDBService::Shutdown. -NS_IMETHODIMP -nsUrlClassifierDBServiceWorker::CloseDb() -{ - if (mClassifier) { - mClassifier->Close(); - mClassifier = nullptr; - } - - mCryptoHash = nullptr; - LOG(("urlclassifier db closed\n")); - - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierDBServiceWorker::CacheCompletions(CacheResultArray *results) -{ - LOG(("nsUrlClassifierDBServiceWorker::CacheCompletions [%p]", this)); - if (!mClassifier) - return NS_OK; - - // Ownership is transferred in to us - nsAutoPtr resultsPtr(results); - - nsAutoPtr pParse(new ProtocolParser()); - nsTArray updates; - - // Only cache results for tables that we have, don't take - // in tables we might accidentally have hit during a completion. - // This happens due to goog vs googpub lists existing. - nsTArray tables; - nsresult rv = mClassifier->ActiveTables(tables); - NS_ENSURE_SUCCESS(rv, rv); - - for (uint32_t i = 0; i < resultsPtr->Length(); i++) { - bool activeTable = false; - for (uint32_t table = 0; table < tables.Length(); table++) { - if (tables[table].Equals(resultsPtr->ElementAt(i).table)) { - activeTable = true; - break; - } - } - if (activeTable) { - TableUpdate * tu = pParse->GetTableUpdate(resultsPtr->ElementAt(i).table); - LOG(("CacheCompletion Addchunk %d hash %X", resultsPtr->ElementAt(i).entry.addChunk, - resultsPtr->ElementAt(i).entry.ToUint32())); - tu->NewAddComplete(resultsPtr->ElementAt(i).entry.addChunk, - resultsPtr->ElementAt(i).entry.complete); - tu->NewAddChunk(resultsPtr->ElementAt(i).entry.addChunk); - tu->SetLocalUpdate(); - updates.AppendElement(tu); - pParse->ForgetTableUpdates(); - } else { - LOG(("Completion received, but table is not active, so not caching.")); - } - } - - mClassifier->ApplyUpdates(&updates); - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierDBServiceWorker::CacheMisses(PrefixArray *results) -{ - LOG(("nsUrlClassifierDBServiceWorker::CacheMisses [%p] %d", - this, results->Length())); - - // Ownership is transferred in to us - nsAutoPtr resultsPtr(results); - - for (uint32_t i = 0; i < resultsPtr->Length(); i++) { - mMissCache.AppendElement(resultsPtr->ElementAt(i)); - } - return NS_OK; -} - -nsresult -nsUrlClassifierDBServiceWorker::OpenDb() -{ - MOZ_ASSERT(!NS_IsMainThread(), "Must initialize DB on background thread"); - // Connection already open, don't do anything. - if (mClassifier) { - return NS_OK; - } - - nsresult rv; - mCryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - nsAutoPtr classifier(new Classifier()); - if (!classifier) { - return NS_ERROR_OUT_OF_MEMORY; - } - - rv = classifier->Open(*mCacheDir); - NS_ENSURE_SUCCESS(rv, rv); - - mClassifier = classifier; - - return NS_OK; -} - -// ------------------------------------------------------------------------- -// nsUrlClassifierLookupCallback -// -// This class takes the results of a lookup found on the worker thread -// and handles any necessary partial hash expansions before calling -// the client callback. - -class nsUrlClassifierLookupCallback final : public nsIUrlClassifierLookupCallback - , public nsIUrlClassifierHashCompleterCallback -{ -public: - NS_DECL_THREADSAFE_ISUPPORTS - NS_DECL_NSIURLCLASSIFIERLOOKUPCALLBACK - NS_DECL_NSIURLCLASSIFIERHASHCOMPLETERCALLBACK - - nsUrlClassifierLookupCallback(nsUrlClassifierDBService *dbservice, - nsIUrlClassifierCallback *c) - : mDBService(dbservice) - , mResults(nullptr) - , mPendingCompletions(0) - , mCallback(c) - {} - -private: - ~nsUrlClassifierLookupCallback(); - - nsresult HandleResults(); - - nsRefPtr mDBService; - nsAutoPtr mResults; - - // Completed results to send back to the worker for caching. - nsAutoPtr mCacheResults; - - uint32_t mPendingCompletions; - nsCOMPtr mCallback; -}; - -NS_IMPL_ISUPPORTS(nsUrlClassifierLookupCallback, - nsIUrlClassifierLookupCallback, - nsIUrlClassifierHashCompleterCallback) - -nsUrlClassifierLookupCallback::~nsUrlClassifierLookupCallback() -{ - nsCOMPtr thread; - (void)NS_GetMainThread(getter_AddRefs(thread)); - - if (mCallback) { - (void)NS_ProxyRelease(thread, mCallback, false); - } -} - -NS_IMETHODIMP -nsUrlClassifierLookupCallback::LookupComplete(nsTArray* results) -{ - NS_ASSERTION(mResults == nullptr, - "Should only get one set of results per nsUrlClassifierLookupCallback!"); - - if (!results) { - HandleResults(); - return NS_OK; - } - - mResults = results; - - // Check the results entries that need to be completed. - for (uint32_t i = 0; i < results->Length(); i++) { - LookupResult& result = results->ElementAt(i); - - // We will complete partial matches and matches that are stale. - if (!result.Confirmed()) { - nsCOMPtr completer; - nsCString gethashUrl; - nsresult rv; - nsCOMPtr listManager = do_GetService( - "@mozilla.org/url-classifier/listmanager;1", &rv); - NS_ENSURE_SUCCESS(rv, rv); - rv = listManager->GetGethashUrl(result.mTableName, gethashUrl); - NS_ENSURE_SUCCESS(rv, rv); - // gethashUrls may be empty in 2 cases: test tables, and on startup where - // we may have found a prefix in an existing table before the listmanager - // has registered the table. In the second case we should not call - // complete. - if ((!gethashUrl.IsEmpty() || - StringBeginsWith(result.mTableName, NS_LITERAL_CSTRING("test-"))) && - mDBService->GetCompleter(result.mTableName, - getter_AddRefs(completer))) { - nsAutoCString partialHash; - partialHash.Assign(reinterpret_cast(&result.hash.prefix), - PREFIX_SIZE); - - nsresult rv = completer->Complete(partialHash, gethashUrl, this); - if (NS_SUCCEEDED(rv)) { - mPendingCompletions++; - } - } else { - // For tables with no hash completer, a complete hash match is - // good enough, we'll consider it fresh, even if it hasn't been updated - // in 45 minutes. - if (result.Complete()) { - result.mFresh = true; - } else { - NS_WARNING("Partial match in a table without a valid completer, ignoring partial match."); - } - } - } - } - - if (mPendingCompletions == 0) { - // All results were complete, we're ready! - HandleResults(); - } - - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierLookupCallback::CompletionFinished(nsresult status) -{ - LOG(("nsUrlClassifierLookupCallback::CompletionFinished [%p, %08x]", - this, status)); - if (NS_FAILED(status)) { - NS_WARNING("gethash response failed."); - } - - mPendingCompletions--; - if (mPendingCompletions == 0) { - HandleResults(); - } - - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierLookupCallback::Completion(const nsACString& completeHash, - const nsACString& tableName, - uint32_t chunkId) -{ - LOG(("nsUrlClassifierLookupCallback::Completion [%p, %s, %d]", - this, PromiseFlatCString(tableName).get(), chunkId)); - mozilla::safebrowsing::Completion hash; - hash.Assign(completeHash); - - // Send this completion to the store for caching. - if (!mCacheResults) { - mCacheResults = new CacheResultArray(); - if (!mCacheResults) - return NS_ERROR_OUT_OF_MEMORY; - } - - CacheResult result; - result.entry.addChunk = chunkId; - result.entry.complete = hash; - result.table = tableName; - - // OK if this fails, we just won't cache the item. - mCacheResults->AppendElement(result); - - // Check if this matched any of our results. - for (uint32_t i = 0; i < mResults->Length(); i++) { - LookupResult& result = mResults->ElementAt(i); - - // Now, see if it verifies a lookup - if (result.CompleteHash() == hash && result.mTableName.Equals(tableName)) { - result.mProtocolConfirmed = true; - } - } - - return NS_OK; -} - -nsresult -nsUrlClassifierLookupCallback::HandleResults() -{ - if (!mResults) { - // No results, this URI is clean. - return mCallback->HandleEvent(NS_LITERAL_CSTRING("")); - } - - nsTArray tables; - // Build a stringified list of result tables. - for (uint32_t i = 0; i < mResults->Length(); i++) { - LookupResult& result = mResults->ElementAt(i); - - // Leave out results that weren't confirmed, as their existence on - // the list can't be verified. Also leave out randomly-generated - // noise. - if (!result.Confirmed() || result.mNoise) { - LOG(("Skipping result from table %s", result.mTableName.get())); - continue; - } - - LOG(("Confirmed result from table %s", result.mTableName.get())); - - if (tables.IndexOf(result.mTableName) == nsTArray::NoIndex) { - tables.AppendElement(result.mTableName); - } - } - - // Some parts of this gethash request generated no hits at all. - // Prefixes must have been removed from the database since our last update. - // Save the prefixes we checked to prevent repeated requests - // until the next update. - nsAutoPtr cacheMisses(new PrefixArray()); - if (cacheMisses) { - for (uint32_t i = 0; i < mResults->Length(); i++) { - LookupResult &result = mResults->ElementAt(i); - if (!result.Confirmed() && !result.mNoise) { - cacheMisses->AppendElement(result.PrefixHash()); - } - } - // Hands ownership of the miss array back to the worker thread. - mDBService->CacheMisses(cacheMisses.forget()); - } - - if (mCacheResults) { - // This hands ownership of the cache results array back to the worker - // thread. - mDBService->CacheCompletions(mCacheResults.forget()); - } - - nsAutoCString tableStr; - for (uint32_t i = 0; i < tables.Length(); i++) { - if (i != 0) - tableStr.Append(','); - tableStr.Append(tables[i]); - } - - return mCallback->HandleEvent(tableStr); -} - - -// ------------------------------------------------------------------------- -// Helper class for nsIURIClassifier implementation, translates table names -// to nsIURIClassifier enums. - -class nsUrlClassifierClassifyCallback final : public nsIUrlClassifierCallback -{ -public: - NS_DECL_THREADSAFE_ISUPPORTS - NS_DECL_NSIURLCLASSIFIERCALLBACK - - nsUrlClassifierClassifyCallback(nsIURIClassifierCallback *c, - bool checkMalware, - bool checkPhishing, - bool checkTracking) - : mCallback(c) - , mCheckMalware(checkMalware) - , mCheckPhishing(checkPhishing) - , mCheckTracking(checkTracking) - {} - -private: - ~nsUrlClassifierClassifyCallback() {} - - nsCOMPtr mCallback; - bool mCheckMalware; - bool mCheckPhishing; - bool mCheckTracking; -}; - -NS_IMPL_ISUPPORTS(nsUrlClassifierClassifyCallback, - nsIUrlClassifierCallback) - -NS_IMETHODIMP -nsUrlClassifierClassifyCallback::HandleEvent(const nsACString& tables) -{ - nsresult response = TablesToResponse(tables, mCheckMalware, - mCheckPhishing, mCheckTracking); - mCallback->OnClassifyComplete(response); - return NS_OK; -} - - -// ------------------------------------------------------------------------- -// Proxy class implementation - -NS_IMPL_ISUPPORTS(nsUrlClassifierDBService, - nsIUrlClassifierDBService, - nsIURIClassifier, - nsIObserver) - -/* static */ nsUrlClassifierDBService* -nsUrlClassifierDBService::GetInstance(nsresult *result) -{ - *result = NS_OK; - if (!sUrlClassifierDBService) { - sUrlClassifierDBService = new nsUrlClassifierDBService(); - if (!sUrlClassifierDBService) { - *result = NS_ERROR_OUT_OF_MEMORY; - return nullptr; - } - - NS_ADDREF(sUrlClassifierDBService); // addref the global - - *result = sUrlClassifierDBService->Init(); - if (NS_FAILED(*result)) { - NS_RELEASE(sUrlClassifierDBService); - return nullptr; - } - } else { - // Already exists, just add a ref - NS_ADDREF(sUrlClassifierDBService); // addref the return result - } - return sUrlClassifierDBService; -} - - -nsUrlClassifierDBService::nsUrlClassifierDBService() - : mCheckMalware(CHECK_MALWARE_DEFAULT) - , mCheckPhishing(CHECK_PHISHING_DEFAULT) - , mCheckTracking(CHECK_TRACKING_DEFAULT) - , mInUpdate(false) -{ -} - -nsUrlClassifierDBService::~nsUrlClassifierDBService() -{ - sUrlClassifierDBService = nullptr; -} - -nsresult -nsUrlClassifierDBService::ReadTablesFromPrefs() -{ - nsCString allTables; - nsCString tables; - Preferences::GetCString(PHISH_TABLE_PREF, &allTables); - - Preferences::GetCString(MALWARE_TABLE_PREF, &tables); - if (!tables.IsEmpty()) { - allTables.Append(','); - allTables.Append(tables); - } - - Preferences::GetCString(DOWNLOAD_BLOCK_TABLE_PREF, &tables); - if (!tables.IsEmpty()) { - allTables.Append(','); - allTables.Append(tables); - } - - Preferences::GetCString(DOWNLOAD_ALLOW_TABLE_PREF, &tables); - if (!tables.IsEmpty()) { - allTables.Append(','); - allTables.Append(tables); - } - - Preferences::GetCString(TRACKING_TABLE_PREF, &tables); - if (!tables.IsEmpty()) { - allTables.Append(','); - allTables.Append(tables); - } - - Classifier::SplitTables(allTables, mGethashTables); - - Preferences::GetCString(DISALLOW_COMPLETION_TABLE_PREF, &tables); - Classifier::SplitTables(tables, mDisallowCompletionsTables); - - return NS_OK; -} - -nsresult -nsUrlClassifierDBService::Init() -{ -#if defined(PR_LOGGING) - if (!gUrlClassifierDbServiceLog) - gUrlClassifierDbServiceLog = PR_NewLogModule("UrlClassifierDbService"); -#endif - MOZ_ASSERT(NS_IsMainThread(), "Must initialize DB service on main thread"); - - // Retrieve all the preferences. - mCheckMalware = Preferences::GetBool(CHECK_MALWARE_PREF, - CHECK_MALWARE_DEFAULT); - mCheckPhishing = Preferences::GetBool(CHECK_PHISHING_PREF, - CHECK_PHISHING_DEFAULT); - mCheckTracking = Preferences::GetBool(CHECK_TRACKING_PREF, - CHECK_TRACKING_DEFAULT); - uint32_t gethashNoise = Preferences::GetUint(GETHASH_NOISE_PREF, - GETHASH_NOISE_DEFAULT); - gFreshnessGuarantee = Preferences::GetInt(CONFIRM_AGE_PREF, - CONFIRM_AGE_DEFAULT_SEC); - ReadTablesFromPrefs(); - - // Do we *really* need to be able to change all of these at runtime? - Preferences::AddStrongObserver(this, CHECK_MALWARE_PREF); - Preferences::AddStrongObserver(this, CHECK_PHISHING_PREF); - Preferences::AddStrongObserver(this, CHECK_TRACKING_PREF); - Preferences::AddStrongObserver(this, GETHASH_NOISE_PREF); - Preferences::AddStrongObserver(this, CONFIRM_AGE_PREF); - Preferences::AddStrongObserver(this, PHISH_TABLE_PREF); - Preferences::AddStrongObserver(this, MALWARE_TABLE_PREF); - Preferences::AddStrongObserver(this, TRACKING_TABLE_PREF); - Preferences::AddStrongObserver(this, DOWNLOAD_BLOCK_TABLE_PREF); - Preferences::AddStrongObserver(this, DOWNLOAD_ALLOW_TABLE_PREF); - Preferences::AddStrongObserver(this, DISALLOW_COMPLETION_TABLE_PREF); - - // Force PSM loading on main thread - nsresult rv; - nsCOMPtr dummy = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - // Directory providers must also be accessed on the main thread. - nsCOMPtr cacheDir; - rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_LOCAL_50_DIR, - getter_AddRefs(cacheDir)); - if (NS_FAILED(rv)) { - rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, - getter_AddRefs(cacheDir)); - if (NS_FAILED(rv)) { - return rv; - } - } - - // Start the background thread. - rv = NS_NewNamedThread("URL Classifier", &gDbBackgroundThread); - if (NS_FAILED(rv)) - return rv; - - mWorker = new nsUrlClassifierDBServiceWorker(); - if (!mWorker) - return NS_ERROR_OUT_OF_MEMORY; - - rv = mWorker->Init(gethashNoise, cacheDir); - if (NS_FAILED(rv)) { - mWorker = nullptr; - return rv; - } - - // Proxy for calling the worker on the background thread - mWorkerProxy = new UrlClassifierDBServiceWorkerProxy(mWorker); - rv = mWorkerProxy->OpenDb(); - if (NS_FAILED(rv)) { - return rv; - } - - // Add an observer for shutdown - nsCOMPtr observerService = - mozilla::services::GetObserverService(); - if (!observerService) - return NS_ERROR_FAILURE; - - observerService->AddObserver(this, "profile-before-change", false); - observerService->AddObserver(this, "xpcom-shutdown-threads", false); - - return NS_OK; -} - -void -nsUrlClassifierDBService::BuildTables(bool aTrackingProtectionEnabled, - nsCString &tables) -{ - nsAutoCString malware; - // LookupURI takes a comma-separated list already. - Preferences::GetCString(MALWARE_TABLE_PREF, &malware); - if (mCheckMalware && !malware.IsEmpty()) { - tables.Append(malware); - } - nsAutoCString phishing; - Preferences::GetCString(PHISH_TABLE_PREF, &phishing); - if (mCheckPhishing && !phishing.IsEmpty()) { - tables.Append(','); - tables.Append(phishing); - } - nsAutoCString tracking; - Preferences::GetCString(TRACKING_TABLE_PREF, &tracking); - if (aTrackingProtectionEnabled && !tracking.IsEmpty()) { - tables.Append(','); - tables.Append(tracking); - } -} - -// nsChannelClassifier is the only consumer of this interface. -NS_IMETHODIMP -nsUrlClassifierDBService::Classify(nsIPrincipal* aPrincipal, - bool aTrackingProtectionEnabled, - nsIURIClassifierCallback* c, - bool* result) -{ - NS_ENSURE_ARG(aPrincipal); - NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED); - - if (!(mCheckMalware || mCheckPhishing)) { - *result = false; - return NS_OK; - } - - nsRefPtr callback = - new nsUrlClassifierClassifyCallback(c, mCheckMalware, mCheckPhishing, - mCheckTracking); - if (!callback) return NS_ERROR_OUT_OF_MEMORY; - - nsAutoCString tables; - BuildTables(aTrackingProtectionEnabled, tables); - - nsresult rv = LookupURI(aPrincipal, tables, callback, false, result); - if (rv == NS_ERROR_MALFORMED_URI) { - *result = false; - // The URI had no hostname, don't try to classify it. - return NS_OK; - } - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierDBService::ClassifyLocal(nsIPrincipal* aPrincipal, - bool aTrackingProtectionEnabled, - nsresult* aResponse) -{ - MOZ_ASSERT(NS_IsMainThread(), "ClassifyLocal must be on main thread"); - *aResponse = NS_OK; - nsAutoCString tables; - BuildTables(aTrackingProtectionEnabled, tables); - - nsCOMPtr uri; - nsresult rv = aPrincipal->GetURI(getter_AddRefs(uri)); - NS_ENSURE_SUCCESS(rv, rv); - NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE); - - uri = NS_GetInnermostURI(uri); - NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE); - - nsAutoCString key; - // Canonicalize the url - nsCOMPtr utilsService = - do_GetService(NS_URLCLASSIFIERUTILS_CONTRACTID); - rv = utilsService->GetKeyForURI(uri, key); - NS_ENSURE_SUCCESS(rv, rv); - - nsAutoPtr results(new LookupResultArray()); - if (!results) { - return NS_ERROR_OUT_OF_MEMORY; - } - - // In unittests, we may not have been initalized, so don't crash. - rv = mWorkerProxy->DoLocalLookup(key, tables, results); - if (NS_SUCCEEDED(rv)) { - rv = ProcessLookupResults(results, mCheckMalware, mCheckPhishing, - mCheckTracking); - *aResponse = rv; - } - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierDBService::Lookup(nsIPrincipal* aPrincipal, - const nsACString& tables, - nsIUrlClassifierCallback* c) -{ - NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED); - - bool dummy; - return LookupURI(aPrincipal, tables, c, true, &dummy); -} - -nsresult -nsUrlClassifierDBService::LookupURI(nsIPrincipal* aPrincipal, - const nsACString& tables, - nsIUrlClassifierCallback* c, - bool forceLookup, - bool *didLookup) -{ - NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED); - NS_ENSURE_ARG(aPrincipal); - - if (nsContentUtils::IsSystemPrincipal(aPrincipal)) { - *didLookup = false; - return NS_OK; - } - - nsCOMPtr uri; - nsresult rv = aPrincipal->GetURI(getter_AddRefs(uri)); - NS_ENSURE_SUCCESS(rv, rv); - NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE); - - uri = NS_GetInnermostURI(uri); - NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE); - - nsAutoCString key; - // Canonicalize the url - nsCOMPtr utilsService = - do_GetService(NS_URLCLASSIFIERUTILS_CONTRACTID); - rv = utilsService->GetKeyForURI(uri, key); - if (NS_FAILED(rv)) - return rv; - - if (forceLookup) { - *didLookup = true; - } else { - bool clean = false; - - if (!clean) { - nsCOMPtr permissionManager = - services::GetPermissionManager(); - - if (permissionManager) { - uint32_t perm; - rv = permissionManager->TestPermissionFromPrincipal(aPrincipal, - "safe-browsing", &perm); - NS_ENSURE_SUCCESS(rv, rv); - - clean |= (perm == nsIPermissionManager::ALLOW_ACTION); - } - } - - *didLookup = !clean; - if (clean) { - return NS_OK; - } - } - - // Create an nsUrlClassifierLookupCallback object. This object will - // take care of confirming partial hash matches if necessary before - // calling the client's callback. - nsCOMPtr callback = - new nsUrlClassifierLookupCallback(this, c); - if (!callback) - return NS_ERROR_OUT_OF_MEMORY; - - nsCOMPtr proxyCallback = - new UrlClassifierLookupCallbackProxy(callback); - - // Queue this lookup and call the lookup function to flush the queue if - // necessary. - rv = mWorker->QueueLookup(key, tables, proxyCallback); - NS_ENSURE_SUCCESS(rv, rv); - - // This seems to just call HandlePendingLookups. - nsAutoCString dummy; - return mWorkerProxy->Lookup(nullptr, dummy, nullptr); -} - -NS_IMETHODIMP -nsUrlClassifierDBService::GetTables(nsIUrlClassifierCallback* c) -{ - NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED); - - // The proxy callback uses the current thread. - nsCOMPtr proxyCallback = - new UrlClassifierCallbackProxy(c); - - return mWorkerProxy->GetTables(proxyCallback); -} - -NS_IMETHODIMP -nsUrlClassifierDBService::SetHashCompleter(const nsACString &tableName, - nsIUrlClassifierHashCompleter *completer) -{ - if (completer) { - mCompleters.Put(tableName, completer); - } else { - mCompleters.Remove(tableName); - } - - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierDBService::BeginUpdate(nsIUrlClassifierUpdateObserver *observer, - const nsACString &updateTables) -{ - NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED); - - if (mInUpdate) { - LOG(("Already updating, not available")); - return NS_ERROR_NOT_AVAILABLE; - } - - mInUpdate = true; - - // The proxy observer uses the current thread - nsCOMPtr proxyObserver = - new UrlClassifierUpdateObserverProxy(observer); - - return mWorkerProxy->BeginUpdate(proxyObserver, updateTables); -} - -NS_IMETHODIMP -nsUrlClassifierDBService::BeginStream(const nsACString &table) -{ - NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED); - - return mWorkerProxy->BeginStream(table); -} - -NS_IMETHODIMP -nsUrlClassifierDBService::UpdateStream(const nsACString& aUpdateChunk) -{ - NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED); - - return mWorkerProxy->UpdateStream(aUpdateChunk); -} - -NS_IMETHODIMP -nsUrlClassifierDBService::FinishStream() -{ - NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED); - - return mWorkerProxy->FinishStream(); -} - -NS_IMETHODIMP -nsUrlClassifierDBService::FinishUpdate() -{ - NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED); - - mInUpdate = false; - - return mWorkerProxy->FinishUpdate(); -} - - -NS_IMETHODIMP -nsUrlClassifierDBService::CancelUpdate() -{ - NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED); - - mInUpdate = false; - - return mWorkerProxy->CancelUpdate(); -} - -NS_IMETHODIMP -nsUrlClassifierDBService::ResetDatabase() -{ - NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED); - - return mWorkerProxy->ResetDatabase(); -} - -nsresult -nsUrlClassifierDBService::CacheCompletions(CacheResultArray *results) -{ - NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED); - - return mWorkerProxy->CacheCompletions(results); -} - -nsresult -nsUrlClassifierDBService::CacheMisses(PrefixArray *results) -{ - NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED); - - return mWorkerProxy->CacheMisses(results); -} - -bool -nsUrlClassifierDBService::GetCompleter(const nsACString &tableName, - nsIUrlClassifierHashCompleter **completer) -{ - // If we have specified a completer, go ahead and query it. This is only - // used by tests. - if (mCompleters.Get(tableName, completer)) { - return true; - } - - // If we don't know about this table at all, or are disallowing completions - // for it, skip completion checks. - if (!mGethashTables.Contains(tableName) || - mDisallowCompletionsTables.Contains(tableName)) { - return false; - } - - MOZ_ASSERT(!StringBeginsWith(tableName, NS_LITERAL_CSTRING("test-")), - "We should never fetch hash completions for test tables"); - - // Otherwise, call gethash to find the hash completions. - return NS_SUCCEEDED(CallGetService(NS_URLCLASSIFIERHASHCOMPLETER_CONTRACTID, - completer)); -} - -NS_IMETHODIMP -nsUrlClassifierDBService::Observe(nsISupports *aSubject, const char *aTopic, - const char16_t *aData) -{ - if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) { - nsresult rv; - nsCOMPtr prefs(do_QueryInterface(aSubject, &rv)); - NS_ENSURE_SUCCESS(rv, rv); - if (NS_LITERAL_STRING(CHECK_MALWARE_PREF).Equals(aData)) { - mCheckMalware = Preferences::GetBool(CHECK_MALWARE_PREF, - CHECK_MALWARE_DEFAULT); - } else if (NS_LITERAL_STRING(CHECK_PHISHING_PREF).Equals(aData)) { - mCheckPhishing = Preferences::GetBool(CHECK_PHISHING_PREF, - CHECK_PHISHING_DEFAULT); - } else if (NS_LITERAL_STRING(CHECK_TRACKING_PREF).Equals(aData)) { - mCheckTracking = Preferences::GetBool(CHECK_TRACKING_PREF, - CHECK_TRACKING_DEFAULT); - } else if ( - NS_LITERAL_STRING(PHISH_TABLE_PREF).Equals(aData) || - NS_LITERAL_STRING(MALWARE_TABLE_PREF).Equals(aData) || - NS_LITERAL_STRING(TRACKING_TABLE_PREF).Equals(aData) || - NS_LITERAL_STRING(DOWNLOAD_BLOCK_TABLE_PREF).Equals(aData) || - NS_LITERAL_STRING(DOWNLOAD_ALLOW_TABLE_PREF).Equals(aData) || - NS_LITERAL_STRING(DISALLOW_COMPLETION_TABLE_PREF).Equals(aData)) { - // Just read everything again. - ReadTablesFromPrefs(); - } else if (NS_LITERAL_STRING(CONFIRM_AGE_PREF).Equals(aData)) { - gFreshnessGuarantee = Preferences::GetInt(CONFIRM_AGE_PREF, - CONFIRM_AGE_DEFAULT_SEC); - } - } else if (!strcmp(aTopic, "profile-before-change") || - !strcmp(aTopic, "xpcom-shutdown-threads")) { - Shutdown(); - } else { - return NS_ERROR_UNEXPECTED; - } - - return NS_OK; -} - -// Join the background thread if it exists. -nsresult -nsUrlClassifierDBService::Shutdown() -{ - LOG(("shutting down db service\n")); - - if (!gDbBackgroundThread) - return NS_OK; - - mCompleters.Clear(); - - nsCOMPtr prefs = do_GetService(NS_PREFSERVICE_CONTRACTID); - if (prefs) { - prefs->RemoveObserver(CHECK_MALWARE_PREF, this); - prefs->RemoveObserver(CHECK_PHISHING_PREF, this); - prefs->RemoveObserver(CHECK_TRACKING_PREF, this); - prefs->RemoveObserver(PHISH_TABLE_PREF, this); - prefs->RemoveObserver(MALWARE_TABLE_PREF, this); - prefs->RemoveObserver(TRACKING_TABLE_PREF, this); - prefs->RemoveObserver(DOWNLOAD_BLOCK_TABLE_PREF, this); - prefs->RemoveObserver(DOWNLOAD_ALLOW_TABLE_PREF, this); - prefs->RemoveObserver(DISALLOW_COMPLETION_TABLE_PREF, this); - prefs->RemoveObserver(CONFIRM_AGE_PREF, this); - } - - DebugOnly rv; - // First close the db connection. - if (mWorker) { - rv = mWorkerProxy->CancelUpdate(); - NS_ASSERTION(NS_SUCCEEDED(rv), "failed to post cancel update event"); - - rv = mWorkerProxy->CloseDb(); - NS_ASSERTION(NS_SUCCEEDED(rv), "failed to post close db event"); - } - - mWorkerProxy = nullptr; - - LOG(("joining background thread")); - - gShuttingDownThread = true; - - nsIThread *backgroundThread = gDbBackgroundThread; - gDbBackgroundThread = nullptr; - backgroundThread->Shutdown(); - NS_RELEASE(backgroundThread); - - return NS_OK; -} - -nsIThread* -nsUrlClassifierDBService::BackgroundThread() -{ - return gDbBackgroundThread; -} - diff --git a/toolkit/components/url-classifier/nsUrlClassifierDBService.h b/toolkit/components/url-classifier/nsUrlClassifierDBService.h deleted file mode 100644 index 4939f36b02..0000000000 --- a/toolkit/components/url-classifier/nsUrlClassifierDBService.h +++ /dev/null @@ -1,238 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-/ -/* 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/. */ - -#ifndef nsUrlClassifierDBService_h_ -#define nsUrlClassifierDBService_h_ - -#include - -#include "nsID.h" -#include "nsInterfaceHashtable.h" -#include "nsIObserver.h" -#include "nsUrlClassifierPrefixSet.h" -#include "nsIUrlClassifierHashCompleter.h" -#include "nsIUrlListManager.h" -#include "nsIUrlClassifierDBService.h" -#include "nsIURIClassifier.h" -#include "nsToolkitCompsCID.h" -#include "nsICryptoHMAC.h" -#include "mozilla/Attributes.h" -#include "mozilla/Mutex.h" -#include "mozilla/TimeStamp.h" - -#include "Entries.h" -#include "LookupCache.h" - -// The hash length for a domain key. -#define DOMAIN_LENGTH 4 - -// The hash length of a partial hash entry. -#define PARTIAL_LENGTH 4 - -// The hash length of a complete hash entry. -#define COMPLETE_LENGTH 32 - -using namespace mozilla::safebrowsing; - -class nsUrlClassifierDBServiceWorker; -class nsIThread; -class nsIURI; -class UrlClassifierDBServiceWorkerProxy; -namespace mozilla { -namespace safebrowsing { -class Classifier; -class ProtocolParser; -class TableUpdate; -} -} - -// This is a proxy class that just creates a background thread and delagates -// calls to the background thread. -class nsUrlClassifierDBService final : public nsIUrlClassifierDBService, - public nsIURIClassifier, - public nsIObserver -{ -public: - // This is thread safe. It throws an exception if the thread is busy. - nsUrlClassifierDBService(); - - nsresult Init(); - - static nsUrlClassifierDBService* GetInstance(nsresult *result); - - NS_DECLARE_STATIC_IID_ACCESSOR(NS_URLCLASSIFIERDBSERVICE_CID) - - NS_DECL_THREADSAFE_ISUPPORTS - NS_DECL_NSIURLCLASSIFIERDBSERVICE - NS_DECL_NSIURICLASSIFIER - NS_DECL_NSIOBSERVER - - bool GetCompleter(const nsACString& tableName, - nsIUrlClassifierHashCompleter** completer); - nsresult CacheCompletions(mozilla::safebrowsing::CacheResultArray *results); - nsresult CacheMisses(mozilla::safebrowsing::PrefixArray *results); - - static nsIThread* BackgroundThread(); - -private: - // No subclassing - ~nsUrlClassifierDBService(); - - // Disallow copy constructor - nsUrlClassifierDBService(nsUrlClassifierDBService&); - - nsresult LookupURI(nsIPrincipal* aPrincipal, - const nsACString& tables, - nsIUrlClassifierCallback* c, - bool forceCheck, bool *didCheck); - - // Close db connection and join the background thread if it exists. - nsresult Shutdown(); - - // Check if the key is on a known-clean host. - nsresult CheckClean(const nsACString &lookupKey, - bool *clean); - - // Read everything into mGethashTables and mDisallowCompletionTables - nsresult ReadTablesFromPrefs(); - - // Build a comma-separated list of tables to check - void BuildTables(bool trackingProtectionEnabled, nsCString& tables); - - nsRefPtr mWorker; - nsRefPtr mWorkerProxy; - - nsInterfaceHashtable mCompleters; - - // TRUE if the nsURIClassifier implementation should check for malware - // uris on document loads. - bool mCheckMalware; - - // TRUE if the nsURIClassifier implementation should check for phishing - // uris on document loads. - bool mCheckPhishing; - - // TRUE if the nsURIClassifier implementation should check for tracking - // uris on document loads. - bool mCheckTracking; - - // TRUE if a BeginUpdate() has been called without an accompanying - // CancelUpdate()/FinishUpdate(). This is used to prevent competing - // updates, not to determine whether an update is still being - // processed. - bool mInUpdate; - - // The list of tables that can use the default hash completer object. - nsTArray mGethashTables; - - // The list of tables that should never be hash completed. - nsTArray mDisallowCompletionsTables; - - // Thread that we do the updates on. - static nsIThread* gDbBackgroundThread; -}; - -class nsUrlClassifierDBServiceWorker final : - public nsIUrlClassifierDBServiceWorker -{ -public: - nsUrlClassifierDBServiceWorker(); - - NS_DECL_THREADSAFE_ISUPPORTS - NS_DECL_NSIURLCLASSIFIERDBSERVICE - NS_DECL_NSIURLCLASSIFIERDBSERVICEWORKER - - nsresult Init(uint32_t aGethashNoise, nsCOMPtr aCacheDir); - - // Queue a lookup for the worker to perform, called in the main thread. - // tables is a comma-separated list of tables to query - nsresult QueueLookup(const nsACString& lookupKey, - const nsACString& tables, - nsIUrlClassifierLookupCallback* callback); - - // Handle any queued-up lookups. We call this function during long-running - // update operations to prevent lookups from blocking for too long. - nsresult HandlePendingLookups(); - - // Perform a blocking classifier lookup for a given url. Can be called on - // either the main thread or the worker thread. - nsresult DoLocalLookup(const nsACString& spec, - const nsACString& tables, - LookupResultArray* results); - -private: - // No subclassing - ~nsUrlClassifierDBServiceWorker(); - - // Disallow copy constructor - nsUrlClassifierDBServiceWorker(nsUrlClassifierDBServiceWorker&); - - // Applies the current transaction and resets the update/working times. - nsresult ApplyUpdate(); - - // Reset the in-progress update stream - void ResetStream(); - - // Reset the in-progress update - void ResetUpdate(); - - // Perform a classifier lookup for a given url. - nsresult DoLookup(const nsACString& spec, - const nsACString& tables, - nsIUrlClassifierLookupCallback* c); - - nsresult AddNoise(const Prefix aPrefix, - const nsCString tableName, - uint32_t aCount, - LookupResultArray& results); - - // Can only be used on the background thread - nsCOMPtr mCryptoHash; - - nsAutoPtr mClassifier; - // The class that actually parses the update chunks. - nsAutoPtr mProtocolParser; - - // Directory where to store the SB databases. - nsCOMPtr mCacheDir; - - // XXX: maybe an array of autoptrs. Or maybe a class specifically - // storing a series of updates. - nsTArray mTableUpdates; - - int32_t mUpdateWait; - - // Entries that cannot be completed. We expect them to die at - // the next update - PrefixArray mMissCache; - - nsresult mUpdateStatus; - nsTArray mUpdateTables; - - nsCOMPtr mUpdateObserver; - bool mInStream; - - // The number of noise entries to add to the set of lookup results. - uint32_t mGethashNoise; - - // Pending lookups are stored in a queue for processing. The queue - // is protected by mPendingLookupLock. - mozilla::Mutex mPendingLookupLock; - - class PendingLookup { - public: - mozilla::TimeStamp mStartTime; - nsCString mKey; - nsCString mTables; - nsCOMPtr mCallback; - }; - - // list of pending lookups - nsTArray mPendingLookups; -}; - -NS_DEFINE_STATIC_IID_ACCESSOR(nsUrlClassifierDBService, NS_URLCLASSIFIERDBSERVICE_CID) - -#endif // nsUrlClassifierDBService_h_ diff --git a/toolkit/components/url-classifier/nsUrlClassifierHashCompleter.js b/toolkit/components/url-classifier/nsUrlClassifierHashCompleter.js deleted file mode 100644 index 8d62c0c3b4..0000000000 --- a/toolkit/components/url-classifier/nsUrlClassifierHashCompleter.js +++ /dev/null @@ -1,466 +0,0 @@ -/* 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/. */ - -const Cc = Components.classes; -const Ci = Components.interfaces; -const Cr = Components.results; -const Cu = Components.utils; - -// COMPLETE_LENGTH and PARTIAL_LENGTH copied from nsUrlClassifierDBService.h, -// they correspond to the length, in bytes, of a hash prefix and the total -// hash. -const COMPLETE_LENGTH = 32; -const PARTIAL_LENGTH = 4; - -// These backoff related constants are taken from v2 of the Google Safe Browsing -// API. All times are in milliseconds. -// BACKOFF_ERRORS: the number of errors incurred until we start to back off. -// BACKOFF_INTERVAL: the initial time to wait once we start backing -// off. -// BACKOFF_MAX: as the backoff time doubles after each failure, this is a -// ceiling on the time to wait. - -const BACKOFF_ERRORS = 2; -const BACKOFF_INTERVAL = 30 * 60 * 1000; -const BACKOFF_MAX = 8 * 60 * 60 * 1000; - -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); -Cu.import("resource://gre/modules/Services.jsm"); - -function HashCompleter() { - // The current HashCompleterRequest in flight. Once it is started, it is set - // to null. It may be used by multiple calls to |complete| in succession to - // avoid creating multiple requests to the same gethash URL. - this._currentRequest = null; - // A map of gethashUrls to HashCompleterRequests that haven't yet begun. - this._pendingRequests = {}; - - // A map of gethash URLs to RequestBackoff objects. - this._backoffs = {}; - - // Whether we have been informed of a shutdown by the xpcom-shutdown event. - this._shuttingDown = false; - - Services.obs.addObserver(this, "xpcom-shutdown", true); -} - -HashCompleter.prototype = { - classID: Components.ID("{9111de73-9322-4bfc-8b65-2b727f3e6ec8}"), - QueryInterface: XPCOMUtils.generateQI([Ci.nsIUrlClassifierHashCompleter, - Ci.nsIRunnable, - Ci.nsIObserver, - Ci.nsISupportsWeakReference, - Ci.nsITimerCallback, - Ci.nsISupports]), - - // This is mainly how the HashCompleter interacts with other components. - // Even though it only takes one partial hash and callback, subsequent - // calls are made into the same HTTP request by using a thread dispatch. - complete: function HC_complete(aPartialHash, aGethashUrl, aCallback) { - if (!aGethashUrl) { - throw Cr.NS_ERROR_NOT_INITIALIZED; - } - - if (!this._currentRequest) { - this._currentRequest = new HashCompleterRequest(this, aGethashUrl); - } - if (this._currentRequest.gethashUrl == aGethashUrl) { - this._currentRequest.add(aPartialHash, aCallback); - } else { - if (!this._pendingRequests[aGethashUrl]) { - this._pendingRequests[aGethashUrl] = - new HashCompleterRequest(this, aGethashUrl); - } - this._pendingRequests[aGethashUrl].add(aPartialHash, aCallback); - } - - if (!this._backoffs[aGethashUrl]) { - // Initialize request backoffs separately, since requests are deleted - // after they are dispatched. - var jslib = Cc["@mozilla.org/url-classifier/jslib;1"] - .getService().wrappedJSObject; - this._backoffs[aGethashUrl] = new jslib.RequestBackoff( - BACKOFF_ERRORS /* max errors */, - 60*1000 /* retry interval, 1 min */, - 10 /* keep track of max requests */, - 0 /* don't throttle on successful requests per time period */, - BACKOFF_INTERVAL /* backoff interval, 60 min */, - BACKOFF_MAX /* max backoff, 8hr */); - } - // Start off this request. Without dispatching to a thread, every call to - // complete makes an individual HTTP request. - Services.tm.currentThread.dispatch(this, Ci.nsIThread.DISPATCH_NORMAL); - }, - - // This is called after several calls to |complete|, or after the - // currentRequest has finished. It starts off the HTTP request by making a - // |begin| call to the HashCompleterRequest. - run: function() { - // Clear everything on shutdown - if (this._shuttingDown) { - this._currentRequest = null; - this._pendingRequests = null; - for (var url in this._backoffs) { - this._backoffs[url] = null; - } - throw Cr.NS_ERROR_NOT_INITIALIZED; - } - - // If we don't have an in-flight request, make one - let pendingUrls = Object.keys(this._pendingRequests); - if (!this._currentRequest && (pendingUrls.length > 0)) { - let nextUrl = pendingUrls[0]; - this._currentRequest = this._pendingRequests[nextUrl]; - delete this._pendingRequests[nextUrl]; - } - - if (this._currentRequest) { - try { - this._currentRequest.begin(); - } finally { - // If |begin| fails, we should get rid of our request. - this._currentRequest = null; - } - } - }, - - // Pass the server response status to the RequestBackoff for the given - // gethashUrl and fetch the next pending request, if there is one. - finishRequest: function(url, aStatus) { - this._backoffs[url].noteServerResponse(aStatus); - Services.tm.currentThread.dispatch(this, Ci.nsIThread.DISPATCH_NORMAL); - }, - - // Returns true if we can make a request from the given url, false otherwise. - canMakeRequest: function(aGethashUrl) { - return this._backoffs[aGethashUrl].canMakeRequest(); - }, - - // Notifies the RequestBackoff of a new request so we can throttle based on - // max requests/time period. This must be called before a channel is opened, - // and finishRequest must be called once the response is received. - noteRequest: function(aGethashUrl) { - return this._backoffs[aGethashUrl].noteRequest(); - }, - - observe: function HC_observe(aSubject, aTopic, aData) { - if (aTopic == "xpcom-shutdown") { - this._shuttingDown = true; - } - }, -}; - -function HashCompleterRequest(aCompleter, aGethashUrl) { - // HashCompleter object that created this HashCompleterRequest. - this._completer = aCompleter; - // The internal set of hashes and callbacks that this request corresponds to. - this._requests = []; - // nsIChannel that the hash completion query is transmitted over. - this._channel = null; - // Response body of hash completion. Created in onDataAvailable. - this._response = ""; - // Whether we have been informed of a shutdown by the xpcom-shutdown event. - this._shuttingDown = false; - this.gethashUrl = aGethashUrl; -} -HashCompleterRequest.prototype = { - QueryInterface: XPCOMUtils.generateQI([Ci.nsIRequestObserver, - Ci.nsIStreamListener, - Ci.nsIObserver, - Ci.nsISupports]), - - // This is called by the HashCompleter to add a hash and callback to the - // HashCompleterRequest. It must be called before calling |begin|. - add: function HCR_add(aPartialHash, aCallback) { - this._requests.push({ - partialHash: aPartialHash, - callback: aCallback, - responses: [] - }); - }, - - // This initiates the HTTP request. It can fail due to backoff timings and - // will notify all callbacks as necessary. We notify the backoff object on - // begin. - begin: function HCR_begin() { - if (!this._completer.canMakeRequest(this.gethashUrl)) { - dump("hashcompleter: Can't make request to " + this.gethashUrl + "\n"); - this.notifyFailure(Cr.NS_ERROR_ABORT); - return; - } - - Services.obs.addObserver(this, "xpcom-shutdown", false); - - try { - this.openChannel(); - // Notify the RequestBackoff if opening the channel succeeded. At this - // point, finishRequest must be called. - this._completer.noteRequest(this.gethashUrl); - } - catch (err) { - this.notifyFailure(err); - throw err; - } - }, - - notify: function HCR_notify() { - // If we haven't gotten onStopRequest, just cancel. This will call us - // with onStopRequest since we implement nsIStreamListener on the - // channel. - if (this._channel && this._channel.isPending()) { - dump("hashcompleter: cancelling request to " + this.gethashUrl + "\n"); - this._channel.cancel(Cr.NS_BINDING_ABORTED); - } - }, - - // Creates an nsIChannel for the request and fills the body. - openChannel: function HCR_openChannel() { - let loadFlags = Ci.nsIChannel.INHIBIT_CACHING | - Ci.nsIChannel.LOAD_BYPASS_CACHE; - - let uri = Services.io.newURI(this.gethashUrl, null, null); - let channel = Services.io.newChannelFromURI2(uri, - null, // aLoadingNode - Services.scriptSecurityManager.getSystemPrincipal(), - null, // aTriggeringPrincipal - Ci.nsILoadInfo.SEC_NORMAL, - Ci.nsIContentPolicy.TYPE_OTHER); - channel.loadFlags = loadFlags; - - // Disable keepalive. - let httpChannel = channel.QueryInterface(Ci.nsIHttpChannel); - httpChannel.setRequestHeader("Connection", "close", false); - - this._channel = channel; - - let body = this.buildRequest(); - this.addRequestBody(body); - - // Set a timer that cancels the channel after timeout_ms in case we - // don't get a gethash response. - this.timer_ = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); - // Ask the timer to use nsITimerCallback (.notify()) when ready - let timeout = Services.prefs.getIntPref( - "urlclassifier.gethash.timeout_ms"); - this.timer_.initWithCallback(this, timeout, this.timer_.TYPE_ONE_SHOT); - channel.asyncOpen(this, null); - }, - - // Returns a string for the request body based on the contents of - // this._requests. - buildRequest: function HCR_buildRequest() { - // Sometimes duplicate entries are sent to HashCompleter but we do not need - // to propagate these to the server. (bug 633644) - let prefixes = []; - - for (let i = 0; i < this._requests.length; i++) { - let request = this._requests[i]; - if (prefixes.indexOf(request.partialHash) == -1) { - prefixes.push(request.partialHash); - } - } - - // Randomize the order to obscure the original request from noise - // unbiased Fisher-Yates shuffle - let i = prefixes.length; - while (i--) { - let j = Math.floor(Math.random() * (i + 1)); - let temp = prefixes[i]; - prefixes[i] = prefixes[j]; - prefixes[j] = temp; - } - - let body; - body = PARTIAL_LENGTH + ":" + (PARTIAL_LENGTH * prefixes.length) + - "\n" + prefixes.join(""); - - return body; - }, - - // Sets the request body of this._channel. - addRequestBody: function HCR_addRequestBody(aBody) { - let inputStream = Cc["@mozilla.org/io/string-input-stream;1"]. - createInstance(Ci.nsIStringInputStream); - - inputStream.setData(aBody, aBody.length); - - let uploadChannel = this._channel.QueryInterface(Ci.nsIUploadChannel); - uploadChannel.setUploadStream(inputStream, "text/plain", -1); - - let httpChannel = this._channel.QueryInterface(Ci.nsIHttpChannel); - httpChannel.requestMethod = "POST"; - }, - - // Parses the response body and eventually adds items to the |responses| array - // for elements of |this._requests|. - handleResponse: function HCR_handleResponse() { - if (this._response == "") { - return; - } - - let start = 0; - - let length = this._response.length; - while (start != length) { - start = this.handleTable(start); - } - }, - - // This parses a table entry in the response body and calls |handleItem| - // for complete hash in the table entry. - handleTable: function HCR_handleTable(aStart) { - let body = this._response.substring(aStart); - - // deal with new line indexes as there could be - // new line characters in the data parts. - let newlineIndex = body.indexOf("\n"); - if (newlineIndex == -1) { - throw errorWithStack(); - } - let header = body.substring(0, newlineIndex); - let entries = header.split(":"); - if (entries.length != 3) { - throw errorWithStack(); - } - - let list = entries[0]; - let addChunk = parseInt(entries[1]); - let dataLength = parseInt(entries[2]); - - if (dataLength % COMPLETE_LENGTH != 0 || - dataLength == 0 || - dataLength > body.length - (newlineIndex + 1)) { - throw errorWithStack(); - } - - let data = body.substr(newlineIndex + 1, dataLength); - for (let i = 0; i < (dataLength / COMPLETE_LENGTH); i++) { - this.handleItem(data.substr(i * COMPLETE_LENGTH, COMPLETE_LENGTH), list, - addChunk); - } - - return aStart + newlineIndex + 1 + dataLength; - }, - - // This adds a complete hash to any entry in |this._requests| that matches - // the hash. - handleItem: function HCR_handleItem(aData, aTableName, aChunkId) { - for (let i = 0; i < this._requests.length; i++) { - let request = this._requests[i]; - if (aData.substring(0,4) == request.partialHash) { - request.responses.push({ - completeHash: aData, - tableName: aTableName, - chunkId: aChunkId, - }); - } - } - }, - - // notifySuccess and notifyFailure are used to alert the callbacks with - // results. notifySuccess makes |completion| and |completionFinished| calls - // while notifyFailure only makes a |completionFinished| call with the error - // code. - notifySuccess: function HCR_notifySuccess() { - for (let i = 0; i < this._requests.length; i++) { - let request = this._requests[i]; - for (let j = 0; j < request.responses.length; j++) { - let response = request.responses[j]; - request.callback.completion(response.completeHash, response.tableName, - response.chunkId); - } - - request.callback.completionFinished(Cr.NS_OK); - } - }, - - notifyFailure: function HCR_notifyFailure(aStatus) { - dump("hashcompleter: notifying failure\n"); - for (let i = 0; i < this._requests.length; i++) { - let request = this._requests[i]; - request.callback.completionFinished(aStatus); - } - }, - - onDataAvailable: function HCR_onDataAvailable(aRequest, aContext, - aInputStream, aOffset, aCount) { - let sis = Cc["@mozilla.org/scriptableinputstream;1"]. - createInstance(Ci.nsIScriptableInputStream); - sis.init(aInputStream); - this._response += sis.readBytes(aCount); - }, - - onStartRequest: function HCR_onStartRequest(aRequest, aContext) { - // At this point no data is available for us and we have no reason to - // terminate the connection, so we do nothing until |onStopRequest|. - }, - - onStopRequest: function HCR_onStopRequest(aRequest, aContext, aStatusCode) { - Services.obs.removeObserver(this, "xpcom-shutdown"); - - if (this._shuttingDown) { - throw Cr.NS_ERROR_ABORT; - } - - // Default HTTP status to service unavailable, in case we can't retrieve - // the true status from the channel. - let httpStatus = 503; - if (Components.isSuccessCode(aStatusCode)) { - let channel = aRequest.QueryInterface(Ci.nsIHttpChannel); - let success = channel.requestSucceeded; - httpStatus = channel.responseStatus; - if (!success) { - aStatusCode = Cr.NS_ERROR_ABORT; - } - } - - let success = Components.isSuccessCode(aStatusCode); - // Notify the RequestBackoff once a response is received. - this._completer.finishRequest(this.gethashUrl, httpStatus); - - if (success) { - try { - this.handleResponse(); - } - catch (err) { - dump(err.stack); - aStatusCode = err.value; - success = false; - } - } - - if (success) { - this.notifySuccess(); - } else { - this.notifyFailure(aStatusCode); - } - }, - - observe: function HCR_observe(aSubject, aTopic, aData) { - if (aTopic != "xpcom-shutdown") { - return; - } - - this._shuttingDown = true; - if (this._channel) { - this._channel.cancel(Cr.NS_ERROR_ABORT); - } - }, -}; - -// Converts a URL safe base64 string to a normal base64 string. Will not change -// normal base64 strings. This is modelled after the same function in -// nsUrlClassifierUtils.h. -function unUrlsafeBase64(aStr) { - return !aStr ? "" : aStr.replace(/-/g, "+") - .replace(/_/g, "/"); -} - -function errorWithStack() { - let err = new Error(); - err.value = Cr.NS_ERROR_FAILURE; - return err; -} - -this.NSGetFactory = XPCOMUtils.generateNSGetFactory([HashCompleter]); diff --git a/toolkit/components/url-classifier/nsUrlClassifierLib.js b/toolkit/components/url-classifier/nsUrlClassifierLib.js deleted file mode 100644 index 2126dc755e..0000000000 --- a/toolkit/components/url-classifier/nsUrlClassifierLib.js +++ /dev/null @@ -1,35 +0,0 @@ -# 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/. - -// We wastefully reload the same JS files across components. This puts all -// the common JS files used by safebrowsing and url-classifier into a -// single component. - -const Cc = Components.classes; -const Ci = Components.interfaces; -const G_GDEBUG = false; - -Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); - -#include ./content/moz/lang.js -#include ./content/moz/preferences.js -#include ./content/moz/debug.js -#include ./content/moz/alarm.js -#include ./content/moz/cryptohasher.js -#include ./content/moz/observer.js -#include ./content/moz/protocol4.js - -#include ./content/request-backoff.js -#include ./content/xml-fetcher.js - -// Expose this whole component. -var lib = this; - -function UrlClassifierLib() { - this.wrappedJSObject = lib; -} -UrlClassifierLib.prototype.classID = Components.ID("{26a4a019-2827-4a89-a85c-5931a678823a}"); -UrlClassifierLib.prototype.QueryInterface = XPCOMUtils.generateQI([]); - -this.NSGetFactory = XPCOMUtils.generateNSGetFactory([UrlClassifierLib]); diff --git a/toolkit/components/url-classifier/nsUrlClassifierListManager.js b/toolkit/components/url-classifier/nsUrlClassifierListManager.js deleted file mode 100644 index 815a143680..0000000000 --- a/toolkit/components/url-classifier/nsUrlClassifierListManager.js +++ /dev/null @@ -1,53 +0,0 @@ -# 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/. - -const Cc = Components.classes; -const Ci = Components.interfaces; - -Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); - -#include ./content/listmanager.js - -var modScope = this; -function Init() { - // Pull the library in. - var jslib = Cc["@mozilla.org/url-classifier/jslib;1"] - .getService().wrappedJSObject; - Function.prototype.inherits = function(parentCtor) { - var tempCtor = function(){}; - tempCtor.prototype = parentCtor.prototype; - this.superClass_ = parentCtor.prototype; - this.prototype = new tempCtor(); - }, - modScope.G_Preferences = jslib.G_Preferences; - modScope.G_PreferenceObserver = jslib.G_PreferenceObserver; - modScope.G_ObserverServiceObserver = jslib.G_ObserverServiceObserver; - modScope.G_Debug = jslib.G_Debug; - modScope.G_Assert = jslib.G_Assert; - modScope.G_debugService = jslib.G_debugService; - modScope.G_Alarm = jslib.G_Alarm; - modScope.BindToObject = jslib.BindToObject; - modScope.PROT_XMLFetcher = jslib.PROT_XMLFetcher; - modScope.RequestBackoff = jslib.RequestBackoff; - - // We only need to call Init once. - modScope.Init = function() {}; -} - -function RegistrationData() -{ -} -RegistrationData.prototype = { - classID: Components.ID("{ca168834-cc00-48f9-b83c-fd018e58cae3}"), - _xpcom_factory: { - createInstance: function(outer, iid) { - if (outer != null) - throw Components.results.NS_ERROR_NO_AGGREGATION; - Init(); - return (new PROT_ListManager()).QueryInterface(iid); - } - }, -}; - -this.NSGetFactory = XPCOMUtils.generateNSGetFactory([RegistrationData]); diff --git a/toolkit/components/url-classifier/nsUrlClassifierPrefixSet.cpp b/toolkit/components/url-classifier/nsUrlClassifierPrefixSet.cpp deleted file mode 100644 index 9604fec4dd..0000000000 --- a/toolkit/components/url-classifier/nsUrlClassifierPrefixSet.cpp +++ /dev/null @@ -1,437 +0,0 @@ -/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set ts=8 sts=2 et sw=2 tw=80: */ -/* 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/. */ - -#include "nsCOMPtr.h" -#include "nsDebug.h" -#include "nsPrintfCString.h" -#include "nsTArray.h" -#include "nsString.h" -#include "nsUrlClassifierPrefixSet.h" -#include "nsIUrlClassifierPrefixSet.h" -#include "nsIFile.h" -#include "nsToolkitCompsCID.h" -#include "nsTArray.h" -#include "nsThreadUtils.h" -#include "mozilla/MemoryReporting.h" -#include "mozilla/Telemetry.h" -#include "mozilla/FileUtils.h" -#include "prlog.h" - -using namespace mozilla; - -// NSPR_LOG_MODULES=UrlClassifierPrefixSet:5 -#if defined(PR_LOGGING) -static const PRLogModuleInfo *gUrlClassifierPrefixSetLog = nullptr; -#define LOG(args) PR_LOG(gUrlClassifierPrefixSetLog, PR_LOG_DEBUG, args) -#define LOG_ENABLED() PR_LOG_TEST(gUrlClassifierPrefixSetLog, 4) -#else -#define LOG(args) -#define LOG_ENABLED() (false) -#endif - -NS_IMPL_ISUPPORTS( - nsUrlClassifierPrefixSet, nsIUrlClassifierPrefixSet, nsIMemoryReporter) - -MOZ_DEFINE_MALLOC_SIZE_OF(UrlClassifierMallocSizeOf) - -nsUrlClassifierPrefixSet::nsUrlClassifierPrefixSet() - : mTotalPrefixes(0) - , mMemoryInUse(0) - , mMemoryReportPath() -{ -#if defined(PR_LOGGING) - if (!gUrlClassifierPrefixSetLog) - gUrlClassifierPrefixSetLog = PR_NewLogModule("UrlClassifierPrefixSet"); -#endif -} - -NS_IMETHODIMP -nsUrlClassifierPrefixSet::Init(const nsACString& aName) -{ - mMemoryReportPath = - nsPrintfCString( - "explicit/storage/prefix-set/%s", - (!aName.IsEmpty() ? PromiseFlatCString(aName).get() : "?!") - ); - - RegisterWeakMemoryReporter(this); - - return NS_OK; -} - -nsUrlClassifierPrefixSet::~nsUrlClassifierPrefixSet() -{ - UnregisterWeakMemoryReporter(this); -} - -NS_IMETHODIMP -nsUrlClassifierPrefixSet::SetPrefixes(const uint32_t* aArray, uint32_t aLength) -{ - nsresult rv = NS_OK; - - if (aLength <= 0) { - if (mIndexPrefixes.Length() > 0) { - LOG(("Clearing PrefixSet")); - mIndexDeltas.Clear(); - mIndexPrefixes.Clear(); - mTotalPrefixes = 0; - } - } else { - rv = MakePrefixSet(aArray, aLength); - } - - mMemoryInUse = SizeOfIncludingThis(UrlClassifierMallocSizeOf); - - return rv; -} - -nsresult -nsUrlClassifierPrefixSet::MakePrefixSet(const uint32_t* aPrefixes, uint32_t aLength) -{ - if (aLength == 0) { - return NS_OK; - } - -#ifdef DEBUG - for (uint32_t i = 1; i < aLength; i++) { - MOZ_ASSERT(aPrefixes[i] >= aPrefixes[i-1]); - } -#endif - - mIndexPrefixes.Clear(); - mIndexDeltas.Clear(); - mTotalPrefixes = aLength; - - mIndexPrefixes.AppendElement(aPrefixes[0]); - mIndexDeltas.AppendElement(); - - uint32_t numOfDeltas = 0; - uint32_t totalDeltas = 0; - uint32_t currentItem = aPrefixes[0]; - for (uint32_t i = 1; i < aLength; i++) { - if ((numOfDeltas >= DELTAS_LIMIT) || - (aPrefixes[i] - currentItem >= MAX_INDEX_DIFF)) { - mIndexDeltas.AppendElement(); - mIndexDeltas[mIndexDeltas.Length() - 1].Compact(); - mIndexPrefixes.AppendElement(aPrefixes[i]); - numOfDeltas = 0; - } else { - uint16_t delta = aPrefixes[i] - currentItem; - mIndexDeltas[mIndexDeltas.Length() - 1].AppendElement(delta); - numOfDeltas++; - totalDeltas++; - } - currentItem = aPrefixes[i]; - } - - mIndexPrefixes.Compact(); - mIndexDeltas.Compact(); - - LOG(("Total number of indices: %d", aLength)); - LOG(("Total number of deltas: %d", totalDeltas)); - LOG(("Total number of delta chunks: %d", mIndexDeltas.Length())); - - return NS_OK; -} - -nsresult -nsUrlClassifierPrefixSet::GetPrefixesNative(FallibleTArray& outArray) -{ - if (!outArray.SetLength(mTotalPrefixes)) { - return NS_ERROR_OUT_OF_MEMORY; - } - - uint32_t prefixIdxLength = mIndexPrefixes.Length(); - uint32_t prefixCnt = 0; - - for (uint32_t i = 0; i < prefixIdxLength; i++) { - uint32_t prefix = mIndexPrefixes[i]; - - outArray[prefixCnt++] = prefix; - for (uint32_t j = 0; j < mIndexDeltas[i].Length(); j++) { - prefix += mIndexDeltas[i][j]; - outArray[prefixCnt++] = prefix; - } - } - - NS_ASSERTION(mTotalPrefixes == prefixCnt, "Lengths are inconsistent"); - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierPrefixSet::GetPrefixes(uint32_t* aCount, - uint32_t** aPrefixes) -{ - NS_ENSURE_ARG_POINTER(aCount); - *aCount = 0; - NS_ENSURE_ARG_POINTER(aPrefixes); - *aPrefixes = nullptr; - - FallibleTArray prefixes; - nsresult rv = GetPrefixesNative(prefixes); - if (NS_FAILED(rv)) { - return rv; - } - - uint64_t itemCount = prefixes.Length(); - uint32_t* prefixArray = static_cast(nsMemory::Alloc(itemCount * sizeof(uint32_t))); - NS_ENSURE_TRUE(prefixArray, NS_ERROR_OUT_OF_MEMORY); - - memcpy(prefixArray, prefixes.Elements(), sizeof(uint32_t) * itemCount); - - *aCount = itemCount; - *aPrefixes = prefixArray; - - return NS_OK; -} - -uint32_t nsUrlClassifierPrefixSet::BinSearch(uint32_t start, - uint32_t end, - uint32_t target) -{ - while (start != end && end >= start) { - uint32_t i = start + ((end - start) >> 1); - uint32_t value = mIndexPrefixes[i]; - if (value < target) { - start = i + 1; - } else if (value > target) { - end = i - 1; - } else { - return i; - } - } - return end; -} - -NS_IMETHODIMP -nsUrlClassifierPrefixSet::Contains(uint32_t aPrefix, bool* aFound) -{ - *aFound = false; - - if (mIndexPrefixes.Length() == 0) { - return NS_OK; - } - - uint32_t target = aPrefix; - - // We want to do a "Price is Right" binary search, that is, we want to find - // the index of the value either equal to the target or the closest value - // that is less than the target. - // - if (target < mIndexPrefixes[0]) { - return NS_OK; - } - - // |binsearch| does not necessarily return the correct index (when the - // target is not found) but rather it returns an index at least one away - // from the correct index. - // Because of this, we need to check if the target lies before the beginning - // of the indices. - - uint32_t i = BinSearch(0, mIndexPrefixes.Length() - 1, target); - if (mIndexPrefixes[i] > target && i > 0) { - i--; - } - - // Now search through the deltas for the target. - uint32_t diff = target - mIndexPrefixes[i]; - uint32_t deltaSize = mIndexDeltas[i].Length(); - uint32_t deltaIndex = 0; - - while (diff > 0 && deltaIndex < deltaSize) { - diff -= mIndexDeltas[i][deltaIndex]; - deltaIndex++; - } - - if (diff == 0) { - *aFound = true; - } - - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierPrefixSet::CollectReports(nsIHandleReportCallback* aHandleReport, - nsISupports* aData, bool aAnonymize) -{ - return aHandleReport->Callback( - EmptyCString(), mMemoryReportPath, KIND_HEAP, UNITS_BYTES, - mMemoryInUse, - NS_LITERAL_CSTRING("Memory used by the prefix set for a URL classifier."), - aData); -} - -size_t -nsUrlClassifierPrefixSet::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) -{ - size_t n = 0; - n += aMallocSizeOf(this); - n += mIndexDeltas.SizeOfExcludingThis(aMallocSizeOf); - for (uint32_t i = 0; i < mIndexDeltas.Length(); i++) { - n += mIndexDeltas[i].SizeOfExcludingThis(aMallocSizeOf); - } - n += mIndexPrefixes.SizeOfExcludingThis(aMallocSizeOf); - return n; -} - -NS_IMETHODIMP -nsUrlClassifierPrefixSet::IsEmpty(bool * aEmpty) -{ - *aEmpty = (mIndexPrefixes.Length() == 0); - return NS_OK; -} - -nsresult -nsUrlClassifierPrefixSet::LoadFromFd(AutoFDClose& fileFd) -{ - uint32_t magic; - int32_t read; - - read = PR_Read(fileFd, &magic, sizeof(uint32_t)); - NS_ENSURE_TRUE(read == sizeof(uint32_t), NS_ERROR_FAILURE); - - if (magic == PREFIXSET_VERSION_MAGIC) { - uint32_t indexSize; - uint32_t deltaSize; - - read = PR_Read(fileFd, &indexSize, sizeof(uint32_t)); - NS_ENSURE_TRUE(read == sizeof(uint32_t), NS_ERROR_FILE_CORRUPTED); - read = PR_Read(fileFd, &deltaSize, sizeof(uint32_t)); - NS_ENSURE_TRUE(read == sizeof(uint32_t), NS_ERROR_FILE_CORRUPTED); - - if (indexSize == 0) { - LOG(("stored PrefixSet is empty!")); - return NS_OK; - } - - if (deltaSize > (indexSize * DELTAS_LIMIT)) { - return NS_ERROR_FILE_CORRUPTED; - } - - nsTArray indexStarts; - indexStarts.SetLength(indexSize); - mIndexPrefixes.SetLength(indexSize); - mIndexDeltas.SetLength(indexSize); - - mTotalPrefixes = indexSize; - - int32_t toRead = indexSize*sizeof(uint32_t); - read = PR_Read(fileFd, mIndexPrefixes.Elements(), toRead); - NS_ENSURE_TRUE(read == toRead, NS_ERROR_FILE_CORRUPTED); - read = PR_Read(fileFd, indexStarts.Elements(), toRead); - NS_ENSURE_TRUE(read == toRead, NS_ERROR_FILE_CORRUPTED); - if (indexSize != 0 && indexStarts[0] != 0) { - return NS_ERROR_FILE_CORRUPTED; - } - for (uint32_t i = 0; i < indexSize; i++) { - uint32_t numInDelta = i == indexSize - 1 ? deltaSize - indexStarts[i] - : indexStarts[i + 1] - indexStarts[i]; - if (numInDelta > 0) { - mIndexDeltas[i].SetLength(numInDelta); - mTotalPrefixes += numInDelta; - toRead = numInDelta * sizeof(uint16_t); - read = PR_Read(fileFd, mIndexDeltas[i].Elements(), toRead); - NS_ENSURE_TRUE(read == toRead, NS_ERROR_FILE_CORRUPTED); - } - } - } else { - LOG(("Version magic mismatch, not loading")); - return NS_ERROR_FILE_CORRUPTED; - } - - MOZ_ASSERT(mIndexPrefixes.Length() == mIndexDeltas.Length()); - LOG(("Loading PrefixSet successful")); - - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierPrefixSet::LoadFromFile(nsIFile* aFile) -{ - Telemetry::AutoTimer timer; - - nsresult rv; - AutoFDClose fileFd; - rv = aFile->OpenNSPRFileDesc(PR_RDONLY | nsIFile::OS_READAHEAD, - 0, &fileFd.rwget()); - if (!NS_FAILED(rv)) { - rv = LoadFromFd(fileFd); - mMemoryInUse = SizeOfIncludingThis(UrlClassifierMallocSizeOf); - } - - return rv; -} - -nsresult -nsUrlClassifierPrefixSet::StoreToFd(AutoFDClose& fileFd) -{ - { - Telemetry::AutoTimer timer; - int64_t size = 4 * sizeof(uint32_t); - uint32_t deltas = mTotalPrefixes - mIndexPrefixes.Length(); - size += 2 * mIndexPrefixes.Length() * sizeof(uint32_t); - size += deltas * sizeof(uint16_t); - - mozilla::fallocate(fileFd, size); - } - - int32_t written; - int32_t writelen = sizeof(uint32_t); - uint32_t magic = PREFIXSET_VERSION_MAGIC; - written = PR_Write(fileFd, &magic, writelen); - NS_ENSURE_TRUE(written == writelen, NS_ERROR_FAILURE); - - uint32_t indexSize = mIndexPrefixes.Length(); - uint32_t indexDeltaSize = mIndexDeltas.Length(); - uint32_t totalDeltas = 0; - - // Store the shape of mIndexDeltas by noting at which "count" of total - // indexes a new subarray starts. This is slightly cumbersome but keeps - // file format compatibility. - // If we ever update the format, we can gain space by storing the delta - // subarray sizes, which fit in bytes. - nsTArray indexStarts; - indexStarts.AppendElement(0); - - for (uint32_t i = 0; i < indexDeltaSize; i++) { - uint32_t deltaLength = mIndexDeltas[i].Length(); - totalDeltas += deltaLength; - indexStarts.AppendElement(totalDeltas); - } - - written = PR_Write(fileFd, &indexSize, writelen); - NS_ENSURE_TRUE(written == writelen, NS_ERROR_FAILURE); - written = PR_Write(fileFd, &totalDeltas, writelen); - NS_ENSURE_TRUE(written == writelen, NS_ERROR_FAILURE); - - writelen = indexSize * sizeof(uint32_t); - written = PR_Write(fileFd, mIndexPrefixes.Elements(), writelen); - NS_ENSURE_TRUE(written == writelen, NS_ERROR_FAILURE); - written = PR_Write(fileFd, indexStarts.Elements(), writelen); - NS_ENSURE_TRUE(written == writelen, NS_ERROR_FAILURE); - if (totalDeltas > 0) { - for (uint32_t i = 0; i < indexDeltaSize; i++) { - writelen = mIndexDeltas[i].Length() * sizeof(uint16_t); - written = PR_Write(fileFd, mIndexDeltas[i].Elements(), writelen); - NS_ENSURE_TRUE(written == writelen, NS_ERROR_FAILURE); - } - } - - LOG(("Saving PrefixSet successful\n")); - - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierPrefixSet::StoreToFile(nsIFile* aFile) -{ - AutoFDClose fileFd; - nsresult rv = aFile->OpenNSPRFileDesc(PR_RDWR | PR_TRUNCATE | PR_CREATE_FILE, - 0644, &fileFd.rwget()); - NS_ENSURE_SUCCESS(rv, rv); - - return StoreToFd(fileFd); -} diff --git a/toolkit/components/url-classifier/nsUrlClassifierPrefixSet.h b/toolkit/components/url-classifier/nsUrlClassifierPrefixSet.h deleted file mode 100644 index bd476a79cf..0000000000 --- a/toolkit/components/url-classifier/nsUrlClassifierPrefixSet.h +++ /dev/null @@ -1,75 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set ts=2 et sw=2 tw=80: */ -/* 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/. */ - -#ifndef nsUrlClassifierPrefixSet_h_ -#define nsUrlClassifierPrefixSet_h_ - -#include "nsISupportsUtils.h" -#include "nsID.h" -#include "nsIFile.h" -#include "nsIMemoryReporter.h" -#include "nsIMutableArray.h" -#include "nsIUrlClassifierPrefixSet.h" -#include "nsTArray.h" -#include "nsToolkitCompsCID.h" -#include "mozilla/MemoryReporting.h" -#include "mozilla/FileUtils.h" -#include "mozilla/Atomics.h" - -class nsUrlClassifierPrefixSet final - : public nsIUrlClassifierPrefixSet - , public nsIMemoryReporter -{ -public: - nsUrlClassifierPrefixSet(); - - NS_IMETHOD Init(const nsACString& aName) override; - NS_IMETHOD SetPrefixes(const uint32_t* aArray, uint32_t aLength) override; - NS_IMETHOD GetPrefixes(uint32_t* aCount, uint32_t** aPrefixes) override; - NS_IMETHOD Contains(uint32_t aPrefix, bool* aFound) override; - NS_IMETHOD IsEmpty(bool* aEmpty) override; - NS_IMETHOD LoadFromFile(nsIFile* aFile) override; - NS_IMETHOD StoreToFile(nsIFile* aFile) override; - - nsresult GetPrefixesNative(FallibleTArray& outArray); - size_t SizeInMemory() { return mMemoryInUse; }; - - NS_DECL_THREADSAFE_ISUPPORTS - NS_DECL_NSIMEMORYREPORTER - -protected: - virtual ~nsUrlClassifierPrefixSet(); - - static const uint32_t DELTAS_LIMIT = 120; - static const uint32_t MAX_INDEX_DIFF = (1 << 16); - static const uint32_t PREFIXSET_VERSION_MAGIC = 1; - - nsresult MakePrefixSet(const uint32_t* aArray, uint32_t aLength); - uint32_t BinSearch(uint32_t start, uint32_t end, uint32_t target); - nsresult LoadFromFd(mozilla::AutoFDClose& fileFd); - nsresult StoreToFd(mozilla::AutoFDClose& fileFd); - - // Return the estimated size of the set on disk and in memory, in bytes. - size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf); - - // list of fully stored prefixes, that also form the - // start of a run of deltas in mIndexDeltas. - nsTArray mIndexPrefixes; - // array containing arrays of deltas from indices. - // Index to the place that matches the closest lower - // prefix from mIndexPrefix. Then every "delta" corresponds - // to a prefix in the PrefixSet. - nsTArray > mIndexDeltas; - // how many prefixes we have. - uint32_t mTotalPrefixes; - - // memory report collection might happen while we're updating the prefixset - // on another thread, so pre-compute and remember the size (bug 1050108). - mozilla::Atomic mMemoryInUse; - nsCString mMemoryReportPath; -}; - -#endif diff --git a/toolkit/components/url-classifier/nsUrlClassifierProxies.cpp b/toolkit/components/url-classifier/nsUrlClassifierProxies.cpp deleted file mode 100644 index 9c34eb5c6c..0000000000 --- a/toolkit/components/url-classifier/nsUrlClassifierProxies.cpp +++ /dev/null @@ -1,319 +0,0 @@ -/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#include "nsUrlClassifierProxies.h" -#include "nsUrlClassifierDBService.h" - -#include "mozilla/SyncRunnable.h" - -using namespace mozilla::safebrowsing; - -static nsresult -DispatchToWorkerThread(nsIRunnable* r) -{ - nsIThread* t = nsUrlClassifierDBService::BackgroundThread(); - if (!t) - return NS_ERROR_FAILURE; - - return t->Dispatch(r, NS_DISPATCH_NORMAL); -} - -NS_IMPL_ISUPPORTS(UrlClassifierDBServiceWorkerProxy, - nsIUrlClassifierDBServiceWorker) - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::Lookup(nsIPrincipal* aPrincipal, - const nsACString& aTables, - nsIUrlClassifierCallback* aCB) -{ - nsCOMPtr r = new LookupRunnable(mTarget, aPrincipal, aTables, - aCB); - return DispatchToWorkerThread(r); -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::LookupRunnable::Run() -{ - (void) mTarget->Lookup(mPrincipal, mLookupTables, mCB); - return NS_OK; -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::GetTables(nsIUrlClassifierCallback* aCB) -{ - nsCOMPtr r = new GetTablesRunnable(mTarget, aCB); - return DispatchToWorkerThread(r); -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::GetTablesRunnable::Run() -{ - mTarget->GetTables(mCB); - return NS_OK; -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::SetHashCompleter - (const nsACString&, nsIUrlClassifierHashCompleter*) -{ - NS_NOTREACHED("This method should not be called!"); - return NS_ERROR_NOT_IMPLEMENTED; -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::BeginUpdate - (nsIUrlClassifierUpdateObserver* aUpdater, - const nsACString& aTables) -{ - nsCOMPtr r = new BeginUpdateRunnable(mTarget, aUpdater, - aTables); - return DispatchToWorkerThread(r); -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::BeginUpdateRunnable::Run() -{ - mTarget->BeginUpdate(mUpdater, mTables); - return NS_OK; -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::BeginStream(const nsACString& aTable) -{ - nsCOMPtr r = - new BeginStreamRunnable(mTarget, aTable); - return DispatchToWorkerThread(r); -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::BeginStreamRunnable::Run() -{ - mTarget->BeginStream(mTable); - return NS_OK; -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::UpdateStream(const nsACString& aUpdateChunk) -{ - nsCOMPtr r = - new UpdateStreamRunnable(mTarget, aUpdateChunk); - return DispatchToWorkerThread(r); -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::UpdateStreamRunnable::Run() -{ - mTarget->UpdateStream(mUpdateChunk); - return NS_OK; -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::FinishStream() -{ - nsCOMPtr r = - NS_NewRunnableMethod(mTarget, - &nsIUrlClassifierDBServiceWorker::FinishStream); - return DispatchToWorkerThread(r); -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::DoLocalLookupRunnable::Run() -{ - mTarget->DoLocalLookup(mSpec, mTables, mResults); - return NS_OK; -} - -nsresult -UrlClassifierDBServiceWorkerProxy::DoLocalLookup(const nsACString& spec, - const nsACString& tables, - LookupResultArray* results) - -{ - // Run synchronously on background thread. NS_DISPATCH_SYNC does *not* do - // what we want -- it continues processing events on the main thread loop - // before the Dispatch returns. - nsCOMPtr r = new DoLocalLookupRunnable(mTarget, spec, tables, results); - nsIThread* t = nsUrlClassifierDBService::BackgroundThread(); - if (!t) - return NS_ERROR_FAILURE; - - mozilla::SyncRunnable::DispatchToThread(t, r); - return NS_OK; -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::FinishUpdate() -{ - nsCOMPtr r = - NS_NewRunnableMethod(mTarget, - &nsIUrlClassifierDBServiceWorker::FinishUpdate); - return DispatchToWorkerThread(r); -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::CancelUpdate() -{ - nsCOMPtr r = - NS_NewRunnableMethod(mTarget, - &nsIUrlClassifierDBServiceWorker::CancelUpdate); - return DispatchToWorkerThread(r); -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::ResetDatabase() -{ - nsCOMPtr r = - NS_NewRunnableMethod(mTarget, - &nsIUrlClassifierDBServiceWorker::ResetDatabase); - return DispatchToWorkerThread(r); -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::OpenDb() -{ - nsCOMPtr r = - NS_NewRunnableMethod(mTarget, - &nsIUrlClassifierDBServiceWorker::OpenDb); - return DispatchToWorkerThread(r); -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::CloseDb() -{ - nsCOMPtr r = - NS_NewRunnableMethod(mTarget, - &nsIUrlClassifierDBServiceWorker::CloseDb); - return DispatchToWorkerThread(r); -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::CacheCompletions(CacheResultArray * aEntries) -{ - nsCOMPtr r = new CacheCompletionsRunnable(mTarget, aEntries); - return DispatchToWorkerThread(r); -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::CacheCompletionsRunnable::Run() -{ - mTarget->CacheCompletions(mEntries); - return NS_OK; -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::CacheMisses(PrefixArray * aEntries) -{ - nsCOMPtr r = new CacheMissesRunnable(mTarget, aEntries); - return DispatchToWorkerThread(r); -} - -NS_IMETHODIMP -UrlClassifierDBServiceWorkerProxy::CacheMissesRunnable::Run() -{ - mTarget->CacheMisses(mEntries); - return NS_OK; -} - - -NS_IMPL_ISUPPORTS(UrlClassifierLookupCallbackProxy, - nsIUrlClassifierLookupCallback) - -NS_IMETHODIMP -UrlClassifierLookupCallbackProxy::LookupComplete - (LookupResultArray * aResults) -{ - nsCOMPtr r = new LookupCompleteRunnable(mTarget, aResults); - return NS_DispatchToMainThread(r); -} - -NS_IMETHODIMP -UrlClassifierLookupCallbackProxy::LookupCompleteRunnable::Run() -{ - mTarget->LookupComplete(mResults); - return NS_OK; -} - -NS_IMPL_ISUPPORTS(UrlClassifierCallbackProxy, - nsIUrlClassifierCallback) - -NS_IMETHODIMP -UrlClassifierCallbackProxy::HandleEvent(const nsACString& aValue) -{ - nsCOMPtr r = new HandleEventRunnable(mTarget, aValue); - return NS_DispatchToMainThread(r); -} - -NS_IMETHODIMP -UrlClassifierCallbackProxy::HandleEventRunnable::Run() -{ - mTarget->HandleEvent(mValue); - return NS_OK; -} - -NS_IMPL_ISUPPORTS(UrlClassifierUpdateObserverProxy, - nsIUrlClassifierUpdateObserver) - -NS_IMETHODIMP -UrlClassifierUpdateObserverProxy::UpdateUrlRequested - (const nsACString& aURL, - const nsACString& aTable) -{ - nsCOMPtr r = - new UpdateUrlRequestedRunnable(mTarget, aURL, aTable); - return NS_DispatchToMainThread(r); -} - -NS_IMETHODIMP -UrlClassifierUpdateObserverProxy::UpdateUrlRequestedRunnable::Run() -{ - mTarget->UpdateUrlRequested(mURL, mTable); - return NS_OK; -} - -NS_IMETHODIMP -UrlClassifierUpdateObserverProxy::StreamFinished(nsresult aStatus, - uint32_t aDelay) -{ - nsCOMPtr r = - new StreamFinishedRunnable(mTarget, aStatus, aDelay); - return NS_DispatchToMainThread(r); -} - -NS_IMETHODIMP -UrlClassifierUpdateObserverProxy::StreamFinishedRunnable::Run() -{ - mTarget->StreamFinished(mStatus, mDelay); - return NS_OK; -} - -NS_IMETHODIMP -UrlClassifierUpdateObserverProxy::UpdateError(nsresult aError) -{ - nsCOMPtr r = - new UpdateErrorRunnable(mTarget, aError); - return NS_DispatchToMainThread(r); -} - -NS_IMETHODIMP -UrlClassifierUpdateObserverProxy::UpdateErrorRunnable::Run() -{ - mTarget->UpdateError(mError); - return NS_OK; -} - -NS_IMETHODIMP -UrlClassifierUpdateObserverProxy::UpdateSuccess(uint32_t aRequestedTimeout) -{ - nsCOMPtr r = - new UpdateSuccessRunnable(mTarget, aRequestedTimeout); - return NS_DispatchToMainThread(r); -} - -NS_IMETHODIMP -UrlClassifierUpdateObserverProxy::UpdateSuccessRunnable::Run() -{ - mTarget->UpdateSuccess(mRequestedTimeout); - return NS_OK; -} diff --git a/toolkit/components/url-classifier/nsUrlClassifierProxies.h b/toolkit/components/url-classifier/nsUrlClassifierProxies.h deleted file mode 100644 index 583d2401e6..0000000000 --- a/toolkit/components/url-classifier/nsUrlClassifierProxies.h +++ /dev/null @@ -1,339 +0,0 @@ -/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#ifndef nsUrlClassifierProxies_h -#define nsUrlClassifierProxies_h - -#include "nsIUrlClassifierDBService.h" -#include "nsUrlClassifierDBService.h" -#include "nsProxyRelease.h" -#include "nsThreadUtils.h" -#include "mozilla/Attributes.h" -#include "nsIPrincipal.h" -#include "LookupCache.h" - - -/** - * Thread proxy from the main thread to the worker thread. - */ -class UrlClassifierDBServiceWorkerProxy final : - public nsIUrlClassifierDBServiceWorker -{ -public: - explicit UrlClassifierDBServiceWorkerProxy(nsUrlClassifierDBServiceWorker* aTarget) - : mTarget(aTarget) - { } - - NS_DECL_THREADSAFE_ISUPPORTS - NS_DECL_NSIURLCLASSIFIERDBSERVICE - NS_DECL_NSIURLCLASSIFIERDBSERVICEWORKER - - class LookupRunnable : public nsRunnable - { - public: - LookupRunnable(nsUrlClassifierDBServiceWorker* aTarget, - nsIPrincipal* aPrincipal, - const nsACString& aTables, - nsIUrlClassifierCallback* aCB) - : mTarget(aTarget) - , mPrincipal(aPrincipal) - , mLookupTables(aTables) - , mCB(aCB) - { } - - NS_DECL_NSIRUNNABLE - - private: - nsRefPtr mTarget; - nsCOMPtr mPrincipal; - nsCString mLookupTables; - nsCOMPtr mCB; - }; - - class GetTablesRunnable : public nsRunnable - { - public: - GetTablesRunnable(nsUrlClassifierDBServiceWorker* aTarget, - nsIUrlClassifierCallback* aCB) - : mTarget(aTarget) - , mCB(aCB) - { } - - NS_DECL_NSIRUNNABLE - - private: - nsRefPtr mTarget; - nsCOMPtr mCB; - }; - - class BeginUpdateRunnable : public nsRunnable - { - public: - BeginUpdateRunnable(nsUrlClassifierDBServiceWorker* aTarget, - nsIUrlClassifierUpdateObserver* aUpdater, - const nsACString& aTables) - : mTarget(aTarget) - , mUpdater(aUpdater) - , mTables(aTables) - { } - - NS_DECL_NSIRUNNABLE - - private: - nsRefPtr mTarget; - nsCOMPtr mUpdater; - nsCString mTables; - }; - - class BeginStreamRunnable : public nsRunnable - { - public: - BeginStreamRunnable(nsUrlClassifierDBServiceWorker* aTarget, - const nsACString& aTable) - : mTarget(aTarget) - , mTable(aTable) - { } - - NS_DECL_NSIRUNNABLE - - private: - nsRefPtr mTarget; - nsCString mTable; - }; - - class UpdateStreamRunnable : public nsRunnable - { - public: - UpdateStreamRunnable(nsUrlClassifierDBServiceWorker* aTarget, - const nsACString& aUpdateChunk) - : mTarget(aTarget) - , mUpdateChunk(aUpdateChunk) - { } - - NS_DECL_NSIRUNNABLE - - private: - nsRefPtr mTarget; - nsCString mUpdateChunk; - }; - - class CacheCompletionsRunnable : public nsRunnable - { - public: - CacheCompletionsRunnable(nsUrlClassifierDBServiceWorker* aTarget, - mozilla::safebrowsing::CacheResultArray *aEntries) - : mTarget(aTarget) - , mEntries(aEntries) - { } - - NS_DECL_NSIRUNNABLE - - private: - nsRefPtr mTarget; - mozilla::safebrowsing::CacheResultArray *mEntries; - }; - - class CacheMissesRunnable : public nsRunnable - { - public: - CacheMissesRunnable(nsUrlClassifierDBServiceWorker* aTarget, - mozilla::safebrowsing::PrefixArray *aEntries) - : mTarget(aTarget) - , mEntries(aEntries) - { } - - NS_DECL_NSIRUNNABLE - - private: - nsRefPtr mTarget; - mozilla::safebrowsing::PrefixArray *mEntries; - }; - - class DoLocalLookupRunnable : public nsRunnable - { - public: - DoLocalLookupRunnable(nsUrlClassifierDBServiceWorker* aTarget, - const nsACString& spec, - const nsACString& tables, - mozilla::safebrowsing::LookupResultArray* results) - : mTarget(aTarget) - , mSpec(spec) - , mTables(tables) - , mResults(results) - { } - - NS_DECL_NSIRUNNABLE - private: - nsRefPtr mTarget; - - nsCString mSpec; - nsCString mTables; - mozilla::safebrowsing::LookupResultArray* mResults; - }; - -public: - nsresult DoLocalLookup(const nsACString& spec, - const nsACString& tables, - mozilla::safebrowsing::LookupResultArray* results); - -private: - ~UrlClassifierDBServiceWorkerProxy() {} - - nsRefPtr mTarget; -}; - -// The remaining classes here are all proxies to the main thread - -class UrlClassifierLookupCallbackProxy final : - public nsIUrlClassifierLookupCallback -{ -public: - explicit UrlClassifierLookupCallbackProxy(nsIUrlClassifierLookupCallback* aTarget) - : mTarget(new nsMainThreadPtrHolder(aTarget)) - { } - - NS_DECL_THREADSAFE_ISUPPORTS - NS_DECL_NSIURLCLASSIFIERLOOKUPCALLBACK - - class LookupCompleteRunnable : public nsRunnable - { - public: - LookupCompleteRunnable(const nsMainThreadPtrHandle& aTarget, - mozilla::safebrowsing::LookupResultArray *aResults) - : mTarget(aTarget) - , mResults(aResults) - { } - - NS_DECL_NSIRUNNABLE - - private: - nsMainThreadPtrHandle mTarget; - mozilla::safebrowsing::LookupResultArray * mResults; - }; - -private: - ~UrlClassifierLookupCallbackProxy() {} - - nsMainThreadPtrHandle mTarget; -}; - -class UrlClassifierCallbackProxy final : public nsIUrlClassifierCallback -{ -public: - explicit UrlClassifierCallbackProxy(nsIUrlClassifierCallback* aTarget) - : mTarget(new nsMainThreadPtrHolder(aTarget)) - { } - - NS_DECL_THREADSAFE_ISUPPORTS - NS_DECL_NSIURLCLASSIFIERCALLBACK - - class HandleEventRunnable : public nsRunnable - { - public: - HandleEventRunnable(const nsMainThreadPtrHandle& aTarget, - const nsACString& aValue) - : mTarget(aTarget) - , mValue(aValue) - { } - - NS_DECL_NSIRUNNABLE - - private: - nsMainThreadPtrHandle mTarget; - nsCString mValue; - }; - -private: - ~UrlClassifierCallbackProxy() {} - - nsMainThreadPtrHandle mTarget; -}; - -class UrlClassifierUpdateObserverProxy final : - public nsIUrlClassifierUpdateObserver -{ -public: - explicit UrlClassifierUpdateObserverProxy(nsIUrlClassifierUpdateObserver* aTarget) - : mTarget(new nsMainThreadPtrHolder(aTarget)) - { } - - NS_DECL_THREADSAFE_ISUPPORTS - NS_DECL_NSIURLCLASSIFIERUPDATEOBSERVER - - class UpdateUrlRequestedRunnable : public nsRunnable - { - public: - UpdateUrlRequestedRunnable(const nsMainThreadPtrHandle& aTarget, - const nsACString& aURL, - const nsACString& aTable) - : mTarget(aTarget) - , mURL(aURL) - , mTable(aTable) - { } - - NS_DECL_NSIRUNNABLE - - private: - nsMainThreadPtrHandle mTarget; - nsCString mURL, mTable; - }; - - class StreamFinishedRunnable : public nsRunnable - { - public: - StreamFinishedRunnable(const nsMainThreadPtrHandle& aTarget, - nsresult aStatus, uint32_t aDelay) - : mTarget(aTarget) - , mStatus(aStatus) - , mDelay(aDelay) - { } - - NS_DECL_NSIRUNNABLE - - private: - nsMainThreadPtrHandle mTarget; - nsresult mStatus; - uint32_t mDelay; - }; - - class UpdateErrorRunnable : public nsRunnable - { - public: - UpdateErrorRunnable(const nsMainThreadPtrHandle& aTarget, - nsresult aError) - : mTarget(aTarget) - , mError(aError) - { } - - NS_DECL_NSIRUNNABLE - - private: - nsMainThreadPtrHandle mTarget; - nsresult mError; - }; - - class UpdateSuccessRunnable : public nsRunnable - { - public: - UpdateSuccessRunnable(const nsMainThreadPtrHandle& aTarget, - uint32_t aRequestedTimeout) - : mTarget(aTarget) - , mRequestedTimeout(aRequestedTimeout) - { } - - NS_DECL_NSIRUNNABLE - - private: - nsMainThreadPtrHandle mTarget; - uint32_t mRequestedTimeout; - }; - -private: - ~UrlClassifierUpdateObserverProxy() {} - - nsMainThreadPtrHandle mTarget; -}; - -#endif // nsUrlClassifierProxies_h diff --git a/toolkit/components/url-classifier/nsUrlClassifierStreamUpdater.cpp b/toolkit/components/url-classifier/nsUrlClassifierStreamUpdater.cpp deleted file mode 100644 index 7556d672fd..0000000000 --- a/toolkit/components/url-classifier/nsUrlClassifierStreamUpdater.cpp +++ /dev/null @@ -1,629 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#include "nsCRT.h" -#include "nsIHttpChannel.h" -#include "nsIObserverService.h" -#include "nsIStringStream.h" -#include "nsIUploadChannel.h" -#include "nsIURI.h" -#include "nsIUrlClassifierDBService.h" -#include "nsStreamUtils.h" -#include "nsStringStream.h" -#include "nsToolkitCompsCID.h" -#include "nsUrlClassifierStreamUpdater.h" -#include "prlog.h" -#include "nsIInterfaceRequestor.h" -#include "mozilla/LoadContext.h" -#include "nsContentUtils.h" - -static const char* gQuitApplicationMessage = "quit-application"; - -#undef LOG - -// NSPR_LOG_MODULES=UrlClassifierStreamUpdater:5 -#if defined(PR_LOGGING) -static const PRLogModuleInfo *gUrlClassifierStreamUpdaterLog = nullptr; -#define LOG(args) PR_LOG(gUrlClassifierStreamUpdaterLog, PR_LOG_DEBUG, args) -#else -#define LOG(args) -#endif - - -// This class does absolutely nothing, except pass requests onto the DBService. - -/////////////////////////////////////////////////////////////////////////////// -// nsIUrlClassiferStreamUpdater implementation -// Handles creating/running the stream listener - -nsUrlClassifierStreamUpdater::nsUrlClassifierStreamUpdater() - : mIsUpdating(false), mInitialized(false), mDownloadError(false), - mBeganStream(false), mChannel(nullptr) -{ -#if defined(PR_LOGGING) - if (!gUrlClassifierStreamUpdaterLog) - gUrlClassifierStreamUpdaterLog = PR_NewLogModule("UrlClassifierStreamUpdater"); -#endif - LOG(("nsUrlClassifierStreamUpdater init [this=%p]", this)); -} - -NS_IMPL_ISUPPORTS(nsUrlClassifierStreamUpdater, - nsIUrlClassifierStreamUpdater, - nsIUrlClassifierUpdateObserver, - nsIRequestObserver, - nsIStreamListener, - nsIObserver, - nsIInterfaceRequestor, - nsITimerCallback) - -/** - * Clear out the update. - */ -void -nsUrlClassifierStreamUpdater::DownloadDone() -{ - LOG(("nsUrlClassifierStreamUpdater::DownloadDone [this=%p]", this)); - mIsUpdating = false; - - mPendingUpdates.Clear(); - mDownloadError = false; - mSuccessCallback = nullptr; - mUpdateErrorCallback = nullptr; - mDownloadErrorCallback = nullptr; -} - -/////////////////////////////////////////////////////////////////////////////// -// nsIUrlClassifierStreamUpdater implementation - -nsresult -nsUrlClassifierStreamUpdater::FetchUpdate(nsIURI *aUpdateUrl, - const nsACString & aRequestBody, - const nsACString & aStreamTable) -{ - -#ifdef DEBUG - { - nsCString spec; - aUpdateUrl->GetSpec(spec); - LOG(("Fetching update %s from %s", aRequestBody.Data(), spec.get())); - } -#endif - - nsresult rv; - uint32_t loadFlags = nsIChannel::INHIBIT_CACHING | - nsIChannel::LOAD_BYPASS_CACHE; - rv = NS_NewChannel(getter_AddRefs(mChannel), - aUpdateUrl, - nsContentUtils::GetSystemPrincipal(), - nsILoadInfo::SEC_NORMAL, - nsIContentPolicy::TYPE_OTHER, - nullptr, // aLoadGroup - this, // aInterfaceRequestor - loadFlags); - - NS_ENSURE_SUCCESS(rv, rv); - - mBeganStream = false; - - // If aRequestBody is empty, construct it for the test. - if (!aRequestBody.IsEmpty()) { - rv = AddRequestBody(aRequestBody); - NS_ENSURE_SUCCESS(rv, rv); - } - - // Set the appropriate content type for file/data URIs, for unit testing - // purposes. - // This is only used for testing and should be deleted. - bool match; - if ((NS_SUCCEEDED(aUpdateUrl->SchemeIs("file", &match)) && match) || - (NS_SUCCEEDED(aUpdateUrl->SchemeIs("data", &match)) && match)) { - mChannel->SetContentType(NS_LITERAL_CSTRING("application/vnd.google.safebrowsing-update")); - } else { - // We assume everything else is an HTTP request. - - // Disable keepalive. - nsCOMPtr httpChannel = do_QueryInterface(mChannel, &rv); - NS_ENSURE_SUCCESS(rv, rv); - rv = httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Connection"), NS_LITERAL_CSTRING("close"), false); - NS_ENSURE_SUCCESS(rv, rv); - } - - // Create a custom LoadContext for SafeBrowsing, so we can use callbacks on - // the channel to query the appId which allows separation of safebrowsing - // cookies in a separate jar. - nsCOMPtr sbContext = - new mozilla::LoadContext(NECKO_SAFEBROWSING_APP_ID); - rv = mChannel->SetNotificationCallbacks(sbContext); - NS_ENSURE_SUCCESS(rv, rv); - - // Make the request. - rv = mChannel->AsyncOpen(this, nullptr); - NS_ENSURE_SUCCESS(rv, rv); - - mStreamTable = aStreamTable; - - return NS_OK; -} - -nsresult -nsUrlClassifierStreamUpdater::FetchUpdate(const nsACString & aUpdateUrl, - const nsACString & aRequestBody, - const nsACString & aStreamTable) -{ - LOG(("(pre) Fetching update from %s\n", PromiseFlatCString(aUpdateUrl).get())); - - nsCOMPtr uri; - nsresult rv = NS_NewURI(getter_AddRefs(uri), aUpdateUrl); - NS_ENSURE_SUCCESS(rv, rv); - - nsAutoCString urlSpec; - uri->GetAsciiSpec(urlSpec); - - LOG(("(post) Fetching update from %s\n", urlSpec.get())); - - return FetchUpdate(uri, aRequestBody, aStreamTable); -} - -NS_IMETHODIMP -nsUrlClassifierStreamUpdater::DownloadUpdates( - const nsACString &aRequestTables, - const nsACString &aRequestBody, - const nsACString &aUpdateUrl, - nsIUrlClassifierCallback *aSuccessCallback, - nsIUrlClassifierCallback *aUpdateErrorCallback, - nsIUrlClassifierCallback *aDownloadErrorCallback, - bool *_retval) -{ - NS_ENSURE_ARG(aSuccessCallback); - NS_ENSURE_ARG(aUpdateErrorCallback); - NS_ENSURE_ARG(aDownloadErrorCallback); - - if (mIsUpdating) { - LOG(("Already updating, queueing update %s from %s", aRequestBody.Data(), - aUpdateUrl.Data())); - *_retval = false; - PendingRequest *request = mPendingRequests.AppendElement(); - request->mTables = aRequestTables; - request->mRequest = aRequestBody; - request->mUrl = aUpdateUrl; - request->mSuccessCallback = aSuccessCallback; - request->mUpdateErrorCallback = aUpdateErrorCallback; - request->mDownloadErrorCallback = aDownloadErrorCallback; - return NS_OK; - } - - if (aUpdateUrl.IsEmpty()) { - NS_ERROR("updateUrl not set"); - return NS_ERROR_NOT_INITIALIZED; - } - - nsresult rv; - - if (!mInitialized) { - // Add an observer for shutdown so we can cancel any pending list - // downloads. quit-application is the same event that the download - // manager listens for and uses to cancel pending downloads. - nsCOMPtr observerService = - mozilla::services::GetObserverService(); - if (!observerService) - return NS_ERROR_FAILURE; - - observerService->AddObserver(this, gQuitApplicationMessage, false); - - mDBService = do_GetService(NS_URLCLASSIFIERDBSERVICE_CONTRACTID, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - mInitialized = true; - } - - rv = mDBService->BeginUpdate(this, aRequestTables); - if (rv == NS_ERROR_NOT_AVAILABLE) { - LOG(("Service busy, already updating, queuing update %s from %s", - aRequestBody.Data(), aUpdateUrl.Data())); - *_retval = false; - PendingRequest *request = mPendingRequests.AppendElement(); - request->mTables = aRequestTables; - request->mRequest = aRequestBody; - request->mUrl = aUpdateUrl; - request->mSuccessCallback = aSuccessCallback; - request->mUpdateErrorCallback = aUpdateErrorCallback; - request->mDownloadErrorCallback = aDownloadErrorCallback; - return NS_OK; - } - - if (NS_FAILED(rv)) { - return rv; - } - - mSuccessCallback = aSuccessCallback; - mUpdateErrorCallback = aUpdateErrorCallback; - mDownloadErrorCallback = aDownloadErrorCallback; - - mIsUpdating = true; - *_retval = true; - - LOG(("FetchUpdate: %s", aUpdateUrl.Data())); - //LOG(("requestBody: %s", aRequestBody.Data())); - - return FetchUpdate(aUpdateUrl, aRequestBody, EmptyCString()); -} - -/////////////////////////////////////////////////////////////////////////////// -// nsIUrlClassifierUpdateObserver implementation - -NS_IMETHODIMP -nsUrlClassifierStreamUpdater::UpdateUrlRequested(const nsACString &aUrl, - const nsACString &aTable) -{ - LOG(("Queuing requested update from %s\n", PromiseFlatCString(aUrl).get())); - - PendingUpdate *update = mPendingUpdates.AppendElement(); - if (!update) - return NS_ERROR_OUT_OF_MEMORY; - - // Allow data: and file: urls for unit testing purposes, otherwise assume http - if (StringBeginsWith(aUrl, NS_LITERAL_CSTRING("data:")) || - StringBeginsWith(aUrl, NS_LITERAL_CSTRING("file:"))) { - update->mUrl = aUrl; - } else { - // For unittesting update urls to localhost should use http, not https - // (otherwise the connection will fail silently, since there will be no - // cert available). - if (!StringBeginsWith(aUrl, NS_LITERAL_CSTRING("localhost"))) { - update->mUrl = NS_LITERAL_CSTRING("https://") + aUrl; - } else { - update->mUrl = NS_LITERAL_CSTRING("http://") + aUrl; - } - } - update->mTable = aTable; - - return NS_OK; -} - -nsresult -nsUrlClassifierStreamUpdater::FetchNext() -{ - if (mPendingUpdates.Length() == 0) { - return NS_OK; - } - - PendingUpdate &update = mPendingUpdates[0]; - LOG(("Fetching update url: %s\n", update.mUrl.get())); - nsresult rv = FetchUpdate(update.mUrl, EmptyCString(), - update.mTable); - if (NS_FAILED(rv)) { - LOG(("Error fetching update url: %s\n", update.mUrl.get())); - // We can commit the urls that we've applied so far. This is - // probably a transient server problem, so trigger backoff. - mDownloadErrorCallback->HandleEvent(EmptyCString()); - mDownloadError = true; - mDBService->FinishUpdate(); - return rv; - } - - mPendingUpdates.RemoveElementAt(0); - - return NS_OK; -} - -nsresult -nsUrlClassifierStreamUpdater::FetchNextRequest() -{ - if (mPendingRequests.Length() == 0) { - LOG(("No more requests, returning")); - return NS_OK; - } - - PendingRequest &request = mPendingRequests[0]; - LOG(("Stream updater: fetching next request: %s, %s", - request.mTables.get(), request.mUrl.get())); - bool dummy; - DownloadUpdates( - request.mTables, - request.mRequest, - request.mUrl, - request.mSuccessCallback, - request.mUpdateErrorCallback, - request.mDownloadErrorCallback, - &dummy); - request.mSuccessCallback = nullptr; - request.mUpdateErrorCallback = nullptr; - request.mDownloadErrorCallback = nullptr; - mPendingRequests.RemoveElementAt(0); - - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierStreamUpdater::StreamFinished(nsresult status, - uint32_t requestedDelay) -{ - // We are a service and may not be reset with Init between calls, so reset - // mBeganStream manually. - mBeganStream = false; - LOG(("nsUrlClassifierStreamUpdater::StreamFinished [%x, %d]", status, requestedDelay)); - if (NS_FAILED(status) || mPendingUpdates.Length() == 0) { - // We're done. - LOG(("nsUrlClassifierStreamUpdater::Done [this=%p]", this)); - mDBService->FinishUpdate(); - return NS_OK; - } - - // Wait the requested amount of time before starting a new stream. - // This appears to be a duplicate timer (see bug 1110891) - nsresult rv; - mTimer = do_CreateInstance("@mozilla.org/timer;1", &rv); - if (NS_SUCCEEDED(rv)) { - rv = mTimer->InitWithCallback(this, requestedDelay, - nsITimer::TYPE_ONE_SHOT); - } - - if (NS_FAILED(rv)) { - NS_WARNING("Unable to initialize timer, fetching next safebrowsing item immediately"); - return FetchNext(); - } - - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierStreamUpdater::UpdateSuccess(uint32_t requestedTimeout) -{ - LOG(("nsUrlClassifierStreamUpdater::UpdateSuccess [this=%p]", this)); - if (mPendingUpdates.Length() != 0) { - NS_WARNING("Didn't fetch all safebrowsing update redirects"); - } - - // DownloadDone() clears mSuccessCallback, so we save it off here. - nsCOMPtr successCallback = mDownloadError ? nullptr : mSuccessCallback.get(); - DownloadDone(); - - nsAutoCString strTimeout; - strTimeout.AppendInt(requestedTimeout); - if (successCallback) { - LOG(("nsUrlClassifierStreamUpdater::UpdateSuccess callback [this=%p]", - this)); - successCallback->HandleEvent(strTimeout); - } else { - LOG(("nsUrlClassifierStreamUpdater::UpdateSuccess skipping callback [this=%p]", - this)); - } - // Now fetch the next request - LOG(("stream updater: calling into fetch next request")); - FetchNextRequest(); - - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierStreamUpdater::UpdateError(nsresult result) -{ - LOG(("nsUrlClassifierStreamUpdater::UpdateError [this=%p]", this)); - - // DownloadDone() clears mUpdateErrorCallback, so we save it off here. - nsCOMPtr errorCallback = mDownloadError ? nullptr : mUpdateErrorCallback.get(); - - DownloadDone(); - - nsAutoCString strResult; - strResult.AppendInt(static_cast(result)); - if (errorCallback) { - errorCallback->HandleEvent(strResult); - } - - return NS_OK; -} - -nsresult -nsUrlClassifierStreamUpdater::AddRequestBody(const nsACString &aRequestBody) -{ - nsresult rv; - nsCOMPtr strStream = - do_CreateInstance(NS_STRINGINPUTSTREAM_CONTRACTID, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - rv = strStream->SetData(aRequestBody.BeginReading(), - aRequestBody.Length()); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr uploadChannel = do_QueryInterface(mChannel, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - rv = uploadChannel->SetUploadStream(strStream, - NS_LITERAL_CSTRING("text/plain"), - -1); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr httpChannel = do_QueryInterface(mChannel, &rv); - NS_ENSURE_SUCCESS(rv, rv); - - rv = httpChannel->SetRequestMethod(NS_LITERAL_CSTRING("POST")); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - - -/////////////////////////////////////////////////////////////////////////////// -// nsIStreamListenerObserver implementation - -NS_IMETHODIMP -nsUrlClassifierStreamUpdater::OnStartRequest(nsIRequest *request, - nsISupports* context) -{ - nsresult rv; - bool downloadError = false; - nsAutoCString strStatus; - nsresult status = NS_OK; - - // Only update if we got http success header - nsCOMPtr httpChannel = do_QueryInterface(request); - if (httpChannel) { - rv = httpChannel->GetStatus(&status); - LOG(("nsUrlClassifierStreamUpdater::OnStartRequest (status=%x, this=%p)", - status, this)); - NS_ENSURE_SUCCESS(rv, rv); - - if (NS_FAILED(status)) { - // Assume we're overloading the server and trigger backoff. - downloadError = true; - } else { - bool succeeded = false; - rv = httpChannel->GetRequestSucceeded(&succeeded); - NS_ENSURE_SUCCESS(rv, rv); - - LOG(("nsUrlClassifierStreamUpdater::OnStartRequest (%s)", succeeded ? - "succeeded" : "failed")); - if (!succeeded) { - // 404 or other error, pass error status back - LOG(("HTTP request returned failure code.")); - - uint32_t requestStatus; - rv = httpChannel->GetResponseStatus(&requestStatus); - LOG(("HTTP request returned failure code: %d.", requestStatus)); - NS_ENSURE_SUCCESS(rv, rv); - - strStatus.AppendInt(requestStatus); - downloadError = true; - } - } - } - - if (downloadError) { - LOG(("nsUrlClassifierStreamUpdater::Download error [this=%p]", this)); - - // It's possible for mDownloadErrorCallback to be null on shutdown. - if (mDownloadErrorCallback) { - mDownloadErrorCallback->HandleEvent(strStatus); - } - - mDownloadError = true; - status = NS_ERROR_ABORT; - } else if (NS_SUCCEEDED(status)) { - MOZ_ASSERT(mDownloadErrorCallback); - mBeganStream = true; - LOG(("nsUrlClassifierStreamUpdater::Beginning stream [this=%p]", this)); - rv = mDBService->BeginStream(mStreamTable); - NS_ENSURE_SUCCESS(rv, rv); - } - - mStreamTable.Truncate(); - - return status; -} - -NS_IMETHODIMP -nsUrlClassifierStreamUpdater::OnDataAvailable(nsIRequest *request, - nsISupports* context, - nsIInputStream *aIStream, - uint64_t aSourceOffset, - uint32_t aLength) -{ - if (!mDBService) - return NS_ERROR_NOT_INITIALIZED; - - LOG(("OnDataAvailable (%d bytes)", aLength)); - - nsresult rv; - - // Copy the data into a nsCString - nsCString chunk; - rv = NS_ConsumeStream(aIStream, aLength, chunk); - NS_ENSURE_SUCCESS(rv, rv); - - //LOG(("Chunk (%d): %s\n\n", chunk.Length(), chunk.get())); - rv = mDBService->UpdateStream(chunk); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; -} - -NS_IMETHODIMP -nsUrlClassifierStreamUpdater::OnStopRequest(nsIRequest *request, nsISupports* context, - nsresult aStatus) -{ - if (!mDBService) - return NS_ERROR_NOT_INITIALIZED; - - LOG(("OnStopRequest (status %x, beganStream %s, this=%p)", aStatus, - mBeganStream ? "true" : "false", this)); - - nsresult rv; - - if (NS_SUCCEEDED(aStatus)) { - // Success, finish this stream and move on to the next. - rv = mDBService->FinishStream(); - } else if (mBeganStream) { - LOG(("OnStopRequest::Canceling update [this=%p]", this)); - // We began this stream and couldn't finish it. We have to cancel the - // update, it's not in a consistent state. - rv = mDBService->CancelUpdate(); - } else { - LOG(("OnStopRequest::Finishing update [this=%p]", this)); - // The fetch failed, but we didn't start the stream (probably a - // server or connection error). We can commit what we've applied - // so far, and request again later. - rv = mDBService->FinishUpdate(); - } - - mChannel = nullptr; - - // If the fetch failed, return the network status rather than NS_OK, the - // result of finishing a possibly-empty update - if (NS_SUCCEEDED(aStatus)) { - return rv; - } - return aStatus; -} - -/////////////////////////////////////////////////////////////////////////////// -// nsIObserver implementation - -NS_IMETHODIMP -nsUrlClassifierStreamUpdater::Observe(nsISupports *aSubject, const char *aTopic, - const char16_t *aData) -{ - if (nsCRT::strcmp(aTopic, gQuitApplicationMessage) == 0) { - if (mIsUpdating && mChannel) { - LOG(("Cancel download")); - nsresult rv; - rv = mChannel->Cancel(NS_ERROR_ABORT); - NS_ENSURE_SUCCESS(rv, rv); - mIsUpdating = false; - mChannel = nullptr; - } - if (mTimer) { - mTimer->Cancel(); - mTimer = nullptr; - } - } - return NS_OK; -} - -/////////////////////////////////////////////////////////////////////////////// -// nsIInterfaceRequestor implementation - -NS_IMETHODIMP -nsUrlClassifierStreamUpdater::GetInterface(const nsIID & eventSinkIID, void* *_retval) -{ - return QueryInterface(eventSinkIID, _retval); -} - - -/////////////////////////////////////////////////////////////////////////////// -// nsITimerCallback implementation -NS_IMETHODIMP -nsUrlClassifierStreamUpdater::Notify(nsITimer *timer) -{ - LOG(("nsUrlClassifierStreamUpdater::Notify [%p]", this)); - - mTimer = nullptr; - - // Start the update process up again. - FetchNext(); - - return NS_OK; -} - diff --git a/toolkit/components/url-classifier/nsUrlClassifierStreamUpdater.h b/toolkit/components/url-classifier/nsUrlClassifierStreamUpdater.h deleted file mode 100644 index c568674c2f..0000000000 --- a/toolkit/components/url-classifier/nsUrlClassifierStreamUpdater.h +++ /dev/null @@ -1,100 +0,0 @@ -//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-/ -/* 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/. */ - -#ifndef nsUrlClassifierStreamUpdater_h_ -#define nsUrlClassifierStreamUpdater_h_ - -#include - -#include "nsCOMPtr.h" -#include "nsIObserver.h" -#include "nsIUrlClassifierStreamUpdater.h" -#include "nsIStreamListener.h" -#include "nsNetUtil.h" -#include "nsTArray.h" -#include "nsITimer.h" -#include "mozilla/Attributes.h" - -// Forward declare pointers -class nsIURI; - -class nsUrlClassifierStreamUpdater final : public nsIUrlClassifierStreamUpdater, - public nsIUrlClassifierUpdateObserver, - public nsIStreamListener, - public nsIObserver, - public nsIInterfaceRequestor, - public nsITimerCallback -{ -public: - nsUrlClassifierStreamUpdater(); - - NS_DECL_THREADSAFE_ISUPPORTS - NS_DECL_NSIURLCLASSIFIERSTREAMUPDATER - NS_DECL_NSIURLCLASSIFIERUPDATEOBSERVER - NS_DECL_NSIINTERFACEREQUESTOR - NS_DECL_NSIREQUESTOBSERVER - NS_DECL_NSISTREAMLISTENER - NS_DECL_NSIOBSERVER - NS_DECL_NSITIMERCALLBACK - -private: - // No subclassing - ~nsUrlClassifierStreamUpdater() {} - - // When the dbservice sends an UpdateComplete or UpdateFailure, we call this - // to reset the stream updater. - void DownloadDone(); - - // Disallow copy constructor - nsUrlClassifierStreamUpdater(nsUrlClassifierStreamUpdater&); - - nsresult AddRequestBody(const nsACString &aRequestBody); - - // Fetches an update for a single table. - nsresult FetchUpdate(nsIURI *aURI, - const nsACString &aRequestBody, - const nsACString &aTable); - // Dumb wrapper so we don't have to create URIs. - nsresult FetchUpdate(const nsACString &aURI, - const nsACString &aRequestBody, - const nsACString &aTable); - - // Fetches the next table, from mPendingUpdates. - nsresult FetchNext(); - // Fetches the next request, from mPendingRequests - nsresult FetchNextRequest(); - - - bool mIsUpdating; - bool mInitialized; - bool mDownloadError; - bool mBeganStream; - nsCString mStreamTable; - nsCOMPtr mChannel; - nsCOMPtr mDBService; - nsCOMPtr mTimer; - - struct PendingRequest { - nsCString mTables; - nsCString mRequest; - nsCString mUrl; - nsCOMPtr mSuccessCallback; - nsCOMPtr mUpdateErrorCallback; - nsCOMPtr mDownloadErrorCallback; - }; - nsTArray mPendingRequests; - - struct PendingUpdate { - nsCString mUrl; - nsCString mTable; - }; - nsTArray mPendingUpdates; - - nsCOMPtr mSuccessCallback; - nsCOMPtr mUpdateErrorCallback; - nsCOMPtr mDownloadErrorCallback; -}; - -#endif // nsUrlClassifierStreamUpdater_h_ diff --git a/toolkit/components/url-classifier/nsUrlClassifierUtils.cpp b/toolkit/components/url-classifier/nsUrlClassifierUtils.cpp deleted file mode 100644 index e81992944a..0000000000 --- a/toolkit/components/url-classifier/nsUrlClassifierUtils.cpp +++ /dev/null @@ -1,364 +0,0 @@ -/* 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/. */ - -#include "nsEscape.h" -#include "nsString.h" -#include "nsIURI.h" -#include "nsNetUtil.h" -#include "nsUrlClassifierUtils.h" -#include "nsTArray.h" -#include "nsReadableUtils.h" -#include "plbase64.h" -#include "prprf.h" - -static char int_to_hex_digit(int32_t i) -{ - NS_ASSERTION((i >= 0) && (i <= 15), "int too big in int_to_hex_digit"); - return static_cast(((i < 10) ? (i + '0') : ((i - 10) + 'A'))); -} - -static bool -IsDecimal(const nsACString & num) -{ - for (uint32_t i = 0; i < num.Length(); i++) { - if (!isdigit(num[i])) { - return false; - } - } - - return true; -} - -static bool -IsHex(const nsACString & num) -{ - if (num.Length() < 3) { - return false; - } - - if (num[0] != '0' || !(num[1] == 'x' || num[1] == 'X')) { - return false; - } - - for (uint32_t i = 2; i < num.Length(); i++) { - if (!isxdigit(num[i])) { - return false; - } - } - - return true; -} - -static bool -IsOctal(const nsACString & num) -{ - if (num.Length() < 2) { - return false; - } - - if (num[0] != '0') { - return false; - } - - for (uint32_t i = 1; i < num.Length(); i++) { - if (!isdigit(num[i]) || num[i] == '8' || num[i] == '9') { - return false; - } - } - - return true; -} - -nsUrlClassifierUtils::nsUrlClassifierUtils() : mEscapeCharmap(nullptr) -{ -} - -nsresult -nsUrlClassifierUtils::Init() -{ - // Everything but alpha numerics, - and . - mEscapeCharmap = new Charmap(0xffffffff, 0xfc009fff, 0xf8000001, 0xf8000001, - 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff); - if (!mEscapeCharmap) - return NS_ERROR_OUT_OF_MEMORY; - return NS_OK; -} - -NS_IMPL_ISUPPORTS(nsUrlClassifierUtils, nsIUrlClassifierUtils) - -///////////////////////////////////////////////////////////////////////////// -// nsIUrlClassifierUtils - -NS_IMETHODIMP -nsUrlClassifierUtils::GetKeyForURI(nsIURI * uri, nsACString & _retval) -{ - nsCOMPtr innerURI = NS_GetInnermostURI(uri); - if (!innerURI) - innerURI = uri; - - nsAutoCString host; - innerURI->GetAsciiHost(host); - - if (host.IsEmpty()) { - return NS_ERROR_MALFORMED_URI; - } - - nsresult rv = CanonicalizeHostname(host, _retval); - NS_ENSURE_SUCCESS(rv, rv); - - nsAutoCString path; - rv = innerURI->GetPath(path); - NS_ENSURE_SUCCESS(rv, rv); - - // strip out anchors - int32_t ref = path.FindChar('#'); - if (ref != kNotFound) - path.SetLength(ref); - - nsAutoCString temp; - rv = CanonicalizePath(path, temp); - NS_ENSURE_SUCCESS(rv, rv); - - _retval.Append(temp); - - return NS_OK; -} - -///////////////////////////////////////////////////////////////////////////// -// non-interface methods - -nsresult -nsUrlClassifierUtils::CanonicalizeHostname(const nsACString & hostname, - nsACString & _retval) -{ - nsAutoCString unescaped; - if (!NS_UnescapeURL(PromiseFlatCString(hostname).get(), - PromiseFlatCString(hostname).Length(), - 0, unescaped)) { - unescaped.Assign(hostname); - } - - nsAutoCString cleaned; - CleanupHostname(unescaped, cleaned); - - nsAutoCString temp; - ParseIPAddress(cleaned, temp); - if (!temp.IsEmpty()) { - cleaned.Assign(temp); - } - - ToLowerCase(cleaned); - SpecialEncode(cleaned, false, _retval); - - return NS_OK; -} - - -nsresult -nsUrlClassifierUtils::CanonicalizePath(const nsACString & path, - nsACString & _retval) -{ - _retval.Truncate(); - - nsAutoCString decodedPath(path); - nsAutoCString temp; - while (NS_UnescapeURL(decodedPath.get(), decodedPath.Length(), 0, temp)) { - decodedPath.Assign(temp); - temp.Truncate(); - } - - SpecialEncode(decodedPath, true, _retval); - // XXX: lowercase the path? - - return NS_OK; -} - -void -nsUrlClassifierUtils::CleanupHostname(const nsACString & hostname, - nsACString & _retval) -{ - _retval.Truncate(); - - const char* curChar = hostname.BeginReading(); - const char* end = hostname.EndReading(); - char lastChar = '\0'; - while (curChar != end) { - unsigned char c = static_cast(*curChar); - if (c == '.' && (lastChar == '\0' || lastChar == '.')) { - // skip - } else { - _retval.Append(*curChar); - } - lastChar = c; - ++curChar; - } - - // cut off trailing dots - while (_retval.Length() > 0 && _retval[_retval.Length() - 1] == '.') { - _retval.SetLength(_retval.Length() - 1); - } -} - -void -nsUrlClassifierUtils::ParseIPAddress(const nsACString & host, - nsACString & _retval) -{ - _retval.Truncate(); - nsACString::const_iterator iter, end; - host.BeginReading(iter); - host.EndReading(end); - - if (host.Length() <= 15) { - // The Windows resolver allows a 4-part dotted decimal IP address to - // have a space followed by any old rubbish, so long as the total length - // of the string doesn't get above 15 characters. So, "10.192.95.89 xy" - // is resolved to 10.192.95.89. - // If the string length is greater than 15 characters, e.g. - // "10.192.95.89 xy.wildcard.example.com", it will be resolved through - // DNS. - - if (FindCharInReadable(' ', iter, end)) { - end = iter; - } - } - - for (host.BeginReading(iter); iter != end; iter++) { - if (!(isxdigit(*iter) || *iter == 'x' || *iter == 'X' || *iter == '.')) { - // not an IP - return; - } - } - - host.BeginReading(iter); - nsTArray parts; - ParseString(PromiseFlatCString(Substring(iter, end)), '.', parts); - if (parts.Length() > 4) { - return; - } - - // If any potentially-octal numbers (start with 0 but not hex) have - // non-octal digits, no part of the ip can be in octal - // XXX: this came from the old javascript implementation, is it really - // supposed to be like this? - bool allowOctal = true; - uint32_t i; - - for (i = 0; i < parts.Length(); i++) { - const nsCString& part = parts[i]; - if (part[0] == '0') { - for (uint32_t j = 1; j < part.Length(); j++) { - if (part[j] == 'x') { - break; - } - if (part[j] == '8' || part[j] == '9') { - allowOctal = false; - break; - } - } - } - } - - for (i = 0; i < parts.Length(); i++) { - nsAutoCString canonical; - - if (i == parts.Length() - 1) { - CanonicalNum(parts[i], 5 - parts.Length(), allowOctal, canonical); - } else { - CanonicalNum(parts[i], 1, allowOctal, canonical); - } - - if (canonical.IsEmpty()) { - _retval.Truncate(); - return; - } - - if (_retval.IsEmpty()) { - _retval.Assign(canonical); - } else { - _retval.Append('.'); - _retval.Append(canonical); - } - } - return; -} - -void -nsUrlClassifierUtils::CanonicalNum(const nsACString& num, - uint32_t bytes, - bool allowOctal, - nsACString& _retval) -{ - _retval.Truncate(); - - if (num.Length() < 1) { - return; - } - - uint32_t val; - if (allowOctal && IsOctal(num)) { - if (PR_sscanf(PromiseFlatCString(num).get(), "%o", &val) != 1) { - return; - } - } else if (IsDecimal(num)) { - if (PR_sscanf(PromiseFlatCString(num).get(), "%u", &val) != 1) { - return; - } - } else if (IsHex(num)) { - if (PR_sscanf(PromiseFlatCString(num).get(), num[1] == 'X' ? "0X%x" : "0x%x", - &val) != 1) { - return; - } - } else { - return; - } - - while (bytes--) { - char buf[20]; - PR_snprintf(buf, sizeof(buf), "%u", val & 0xff); - if (_retval.IsEmpty()) { - _retval.Assign(buf); - } else { - _retval = nsDependentCString(buf) + NS_LITERAL_CSTRING(".") + _retval; - } - val >>= 8; - } -} - -// This function will encode all "special" characters in typical url -// encoding, that is %hh where h is a valid hex digit. It will also fold -// any duplicated slashes. -bool -nsUrlClassifierUtils::SpecialEncode(const nsACString & url, - bool foldSlashes, - nsACString & _retval) -{ - bool changed = false; - const char* curChar = url.BeginReading(); - const char* end = url.EndReading(); - - unsigned char lastChar = '\0'; - while (curChar != end) { - unsigned char c = static_cast(*curChar); - if (ShouldURLEscape(c)) { - _retval.Append('%'); - _retval.Append(int_to_hex_digit(c / 16)); - _retval.Append(int_to_hex_digit(c % 16)); - - changed = true; - } else if (foldSlashes && (c == '/' && lastChar == '/')) { - // skip - } else { - _retval.Append(*curChar); - } - lastChar = c; - curChar++; - } - return changed; -} - -bool -nsUrlClassifierUtils::ShouldURLEscape(const unsigned char c) const -{ - return c <= 32 || c == '%' || c >=127; -} diff --git a/toolkit/components/url-classifier/nsUrlClassifierUtils.h b/toolkit/components/url-classifier/nsUrlClassifierUtils.h deleted file mode 100644 index 3a689b9e7a..0000000000 --- a/toolkit/components/url-classifier/nsUrlClassifierUtils.h +++ /dev/null @@ -1,89 +0,0 @@ -/* 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/. */ - -#ifndef nsUrlClassifierUtils_h_ -#define nsUrlClassifierUtils_h_ - -#include "nsAutoPtr.h" -#include "nsIUrlClassifierUtils.h" -#include "nsTArray.h" -#include "nsDataHashtable.h" -#include "mozilla/Attributes.h" - -class nsUrlClassifierUtils final : public nsIUrlClassifierUtils -{ -private: - /** - * A fast, bit-vector map for ascii characters. - * - * Internally stores 256 bits in an array of 8 ints. - * Does quick bit-flicking to lookup needed characters. - */ - class Charmap - { - public: - Charmap(uint32_t b0, uint32_t b1, uint32_t b2, uint32_t b3, - uint32_t b4, uint32_t b5, uint32_t b6, uint32_t b7) - { - mMap[0] = b0; mMap[1] = b1; mMap[2] = b2; mMap[3] = b3; - mMap[4] = b4; mMap[5] = b5; mMap[6] = b6; mMap[7] = b7; - } - - /** - * Do a quick lookup to see if the letter is in the map. - */ - bool Contains(unsigned char c) const - { - return mMap[c >> 5] & (1 << (c & 31)); - } - - private: - // Store the 256 bits in an 8 byte array. - uint32_t mMap[8]; - }; - - -public: - nsUrlClassifierUtils(); - - NS_DECL_ISUPPORTS - NS_DECL_NSIURLCLASSIFIERUTILS - - nsresult Init(); - - nsresult CanonicalizeHostname(const nsACString & hostname, - nsACString & _retval); - nsresult CanonicalizePath(const nsACString & url, nsACString & _retval); - - // This function will encode all "special" characters in typical url encoding, - // that is %hh where h is a valid hex digit. The characters which are encoded - // by this function are any ascii characters under 32(control characters and - // space), 37(%), and anything 127 or above (special characters). Url is the - // string to encode, ret is the encoded string. Function returns true if - // ret != url. - bool SpecialEncode(const nsACString & url, - bool foldSlashes, - nsACString & _retval); - - void ParseIPAddress(const nsACString & host, nsACString & _retval); - void CanonicalNum(const nsACString & num, - uint32_t bytes, - bool allowOctal, - nsACString & _retval); - -private: - ~nsUrlClassifierUtils() {} - - // Disallow copy constructor - nsUrlClassifierUtils(const nsUrlClassifierUtils&); - - // Function to tell if we should encode a character. - bool ShouldURLEscape(const unsigned char c) const; - - void CleanupHostname(const nsACString & host, nsACString & _retval); - - nsAutoPtr mEscapeCharmap; -}; - -#endif // nsUrlClassifierUtils_h_ diff --git a/toolkit/components/url-classifier/tests/TestUrlClassifierUtils.cpp b/toolkit/components/url-classifier/tests/TestUrlClassifierUtils.cpp deleted file mode 100644 index 7fec2ebeb5..0000000000 --- a/toolkit/components/url-classifier/tests/TestUrlClassifierUtils.cpp +++ /dev/null @@ -1,326 +0,0 @@ -/* 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/. */ - -#include -#include -#include "nsEscape.h" -#include "nsString.h" -#include "nsUrlClassifierUtils.h" -#include "nsNetUtil.h" -#include "stdlib.h" -#include "TestHarness.h" - -static int gTotalTests = 0; -static int gPassedTests = 0; - -static char int_to_hex_digit(int32_t i) { - NS_ASSERTION((i >= 0) && (i <= 15), "int too big in int_to_hex_digit"); - return static_cast(((i < 10) ? (i + '0') : ((i - 10) + 'A'))); -} - -static void CheckEquals(nsCString & expected, nsCString & actual) -{ - if (!(expected).Equals((actual))) { - fail("expected |%s| but got |%s|", (expected).get(), (actual).get()); - } else { - gPassedTests++; - } - gTotalTests++; -} - -void TestUnescapeHelper(const char* in, const char* expected) -{ - nsCString out, strIn(in), strExp(expected); - nsUrlClassifierUtils utils; - - NS_UnescapeURL(strIn.get(), strIn.Length(), esc_AlwaysCopy, out); - CheckEquals(strExp, out); -} - -// Make sure Unescape from nsEncode.h's unescape does what the server does. -void TestUnescape() -{ - // test empty string - TestUnescapeHelper("\0", "\0"); - - // Test docoding of all characters. - nsCString allCharsEncoded, allCharsEncodedLowercase, allCharsAsString; - for (int32_t i = 1; i < 256; ++i) { - allCharsEncoded.Append('%'); - allCharsEncoded.Append(int_to_hex_digit(i / 16)); - allCharsEncoded.Append((int_to_hex_digit(i % 16))); - - allCharsEncodedLowercase.Append('%'); - allCharsEncodedLowercase.Append(tolower(int_to_hex_digit(i / 16))); - allCharsEncodedLowercase.Append(tolower(int_to_hex_digit(i % 16))); - - allCharsAsString.Append(static_cast(i)); - } - - nsUrlClassifierUtils utils; - nsCString out; - NS_UnescapeURL(allCharsEncoded.get(), allCharsEncoded.Length(), esc_AlwaysCopy, out); - CheckEquals(allCharsAsString, out); - - out.Truncate(); - NS_UnescapeURL(allCharsEncodedLowercase.get(), allCharsEncodedLowercase.Length(), esc_AlwaysCopy, out); - CheckEquals(allCharsAsString, out); - - // Test %-related edge cases - TestUnescapeHelper("%", "%"); - TestUnescapeHelper("%xx", "%xx"); - TestUnescapeHelper("%%", "%%"); - TestUnescapeHelper("%%%", "%%%"); - TestUnescapeHelper("%%%%", "%%%%"); - TestUnescapeHelper("%1", "%1"); - TestUnescapeHelper("%1z", "%1z"); - TestUnescapeHelper("a%1z", "a%1z"); - TestUnescapeHelper("abc%d%e%fg%hij%klmno%", "abc%d%e%fg%hij%klmno%"); - - // A few more tests - TestUnescapeHelper("%25", "%"); - TestUnescapeHelper("%25%32%35", "%25"); -} - -void TestEncodeHelper(const char* in, const char* expected) -{ - nsCString out, strIn(in), strExp(expected); - nsUrlClassifierUtils utils; - - utils.SpecialEncode(strIn, true, out); - CheckEquals(strExp, out); -} - -void TestEnc() -{ - // Test empty string - TestEncodeHelper("", ""); - - // Test that all characters we shouldn't encode ([33-36],[38,126]) are not. - nsCString noenc; - for (int32_t i = 33; i < 127; i++) { - if (i != 37) { // skip % - noenc.Append(static_cast(i)); - } - } - nsUrlClassifierUtils utils; - nsCString out; - utils.SpecialEncode(noenc, false, out); - CheckEquals(noenc, out); - - // Test that all the chars that we should encode [0,32],37,[127,255] are - nsCString yesAsString, yesExpectedString; - for (int32_t i = 1; i < 256; i++) { - if (i < 33 || i == 37 || i > 126) { - yesAsString.Append(static_cast(i)); - yesExpectedString.Append('%'); - yesExpectedString.Append(int_to_hex_digit(i / 16)); - yesExpectedString.Append(int_to_hex_digit(i % 16)); - } - } - - out.Truncate(); - utils.SpecialEncode(yesAsString, false, out); - CheckEquals(yesExpectedString, out); - - TestEncodeHelper("blah//blah", "blah/blah"); -} - -void TestCanonicalizeHelper(const char* in, const char* expected) -{ - nsCString out, strIn(in), strExp(expected); - nsUrlClassifierUtils utils; - - utils.CanonicalizePath(strIn, out); - CheckEquals(strExp, out); -} - -void TestCanonicalize() -{ - // Test repeated %-decoding. Note: %25 --> %, %32 --> 2, %35 --> 5 - TestCanonicalizeHelper("%25", "%25"); - TestCanonicalizeHelper("%25%32%35", "%25"); - TestCanonicalizeHelper("asdf%25%32%35asd", "asdf%25asd"); - TestCanonicalizeHelper("%%%25%32%35asd%%", "%25%25%25asd%25%25"); - TestCanonicalizeHelper("%25%32%35%25%32%35%25%32%35", "%25%25%25"); - TestCanonicalizeHelper("%25", "%25"); - TestCanonicalizeHelper("%257Ea%2521b%2540c%2523d%2524e%25f%255E00%252611%252A22%252833%252944_55%252B", - "~a!b@c#d$e%25f^00&11*22(33)44_55+"); - - TestCanonicalizeHelper("", ""); - TestCanonicalizeHelper("%31%36%38%2e%31%38%38%2e%39%39%2e%32%36/%2E%73%65%63%75%72%65/%77%77%77%2E%65%62%61%79%2E%63%6F%6D/", - "168.188.99.26/.secure/www.ebay.com/"); - TestCanonicalizeHelper("195.127.0.11/uploads/%20%20%20%20/.verify/.eBaysecure=updateuserdataxplimnbqmn-xplmvalidateinfoswqpcmlx=hgplmcx/", - "195.127.0.11/uploads/%20%20%20%20/.verify/.eBaysecure=updateuserdataxplimnbqmn-xplmvalidateinfoswqpcmlx=hgplmcx/"); - // Added in bug 489455. %00 should no longer be changed to %01. - TestCanonicalizeHelper("%00", "%00"); -} - -void TestParseIPAddressHelper(const char *in, const char *expected) -{ - nsCString out, strIn(in), strExp(expected); - nsUrlClassifierUtils utils; - utils.Init(); - - utils.ParseIPAddress(strIn, out); - CheckEquals(strExp, out); -} - -void TestParseIPAddress() -{ - TestParseIPAddressHelper("123.123.0.0.1", ""); - TestParseIPAddressHelper("255.0.0.1", "255.0.0.1"); - TestParseIPAddressHelper("12.0x12.01234", "12.18.2.156"); - TestParseIPAddressHelper("276.2.3", "20.2.0.3"); - TestParseIPAddressHelper("012.034.01.055", "10.28.1.45"); - TestParseIPAddressHelper("0x12.0x43.0x44.0x01", "18.67.68.1"); - TestParseIPAddressHelper("167838211", "10.1.2.3"); - TestParseIPAddressHelper("3279880203", "195.127.0.11"); - TestParseIPAddressHelper("0x12434401", "18.67.68.1"); - TestParseIPAddressHelper("413960661", "24.172.137.213"); - TestParseIPAddressHelper("03053104725", "24.172.137.213"); - TestParseIPAddressHelper("030.0254.0x89d5", "24.172.137.213"); - TestParseIPAddressHelper("1.234.4.0377", "1.234.4.255"); - TestParseIPAddressHelper("1.2.3.00x0", ""); - TestParseIPAddressHelper("10.192.95.89 xy", "10.192.95.89"); - TestParseIPAddressHelper("10.192.95.89 xyz", ""); - TestParseIPAddressHelper("1.2.3.0x0", "1.2.3.0"); - TestParseIPAddressHelper("1.2.3.4", "1.2.3.4"); -} - -void TestCanonicalNumHelper(const char *in, uint32_t bytes, - bool allowOctal, const char *expected) -{ - nsCString out, strIn(in), strExp(expected); - nsUrlClassifierUtils utils; - utils.Init(); - - utils.CanonicalNum(strIn, bytes, allowOctal, out); - CheckEquals(strExp, out); -} - -void TestCanonicalNum() -{ - TestCanonicalNumHelper("", 1, true, ""); - TestCanonicalNumHelper("10", 0, true, ""); - TestCanonicalNumHelper("45", 1, true, "45"); - TestCanonicalNumHelper("0x10", 1, true, "16"); - TestCanonicalNumHelper("367", 2, true, "1.111"); - TestCanonicalNumHelper("012345", 3, true, "0.20.229"); - TestCanonicalNumHelper("0173", 1, true, "123"); - TestCanonicalNumHelper("09", 1, false, "9"); - TestCanonicalNumHelper("0x120x34", 2, true, ""); - TestCanonicalNumHelper("0x12fc", 2, true, "18.252"); - TestCanonicalNumHelper("3279880203", 4, true, "195.127.0.11"); - TestCanonicalNumHelper("0x0000059", 1, true, "89"); - TestCanonicalNumHelper("0x00000059", 1, true, "89"); - TestCanonicalNumHelper("0x0000067", 1, true, "103"); -} - -void TestHostnameHelper(const char *in, const char *expected) -{ - nsCString out, strIn(in), strExp(expected); - nsUrlClassifierUtils utils; - utils.Init(); - - utils.CanonicalizeHostname(strIn, out); - CheckEquals(strExp, out); -} - -void TestHostname() -{ - TestHostnameHelper("abcd123;[]", "abcd123;[]"); - TestHostnameHelper("abc.123", "abc.123"); - TestHostnameHelper("abc..123", "abc.123"); - TestHostnameHelper("trailing.", "trailing"); - TestHostnameHelper("i love trailing dots....", "i%20love%20trailing%20dots"); - TestHostnameHelper(".leading", "leading"); - TestHostnameHelper("..leading", "leading"); - TestHostnameHelper(".dots.", "dots"); - TestHostnameHelper(".both.", "both"); - TestHostnameHelper(".both..", "both"); - TestHostnameHelper("..both.", "both"); - TestHostnameHelper("..both..", "both"); - TestHostnameHelper("..a.b.c.d..", "a.b.c.d"); - TestHostnameHelper("..127.0.0.1..", "127.0.0.1"); - TestHostnameHelper("asdf!@#$a", "asdf!@#$a"); - TestHostnameHelper("AB CD 12354", "ab%20cd%2012354"); - TestHostnameHelper("\1\2\3\4\112\177", "%01%02%03%04j%7F"); - TestHostnameHelper("<>.AS/-+", "<>.as/-+"); - // Added in bug 489455. %00 should no longer be changed to %01. - TestHostnameHelper("%00", "%00"); -} - -void TestLongHostname() -{ - static const int kTestSize = 1024 * 150; - char *str = static_cast(malloc(kTestSize + 1)); - memset(str, 'x', kTestSize); - str[kTestSize] = '\0'; - - nsUrlClassifierUtils utils; - utils.Init(); - - nsAutoCString out; - nsDependentCString in(str); - PRIntervalTime clockStart = PR_IntervalNow(); - utils.CanonicalizeHostname(in, out); - PRIntervalTime clockEnd = PR_IntervalNow(); - - CheckEquals(in, out); - - printf("CanonicalizeHostname on long string (%dms)\n", - PR_IntervalToMilliseconds(clockEnd - clockStart)); -} - -void TestFragmentSet() -{ - nsUrlClassifierFragmentSet set; - set.Init(3); - - set.Put(NS_LITERAL_CSTRING("a")); - set.Put(NS_LITERAL_CSTRING("b")); - set.Put(NS_LITERAL_CSTRING("c")); - - // At this point, adding a fourth element would push "a" off. - // Make sure that set.Has("a") moves it to the front of the list - set.Has(NS_LITERAL_CSTRING("a")); - - // Now add a new item. This should now push "b" off the list, - // but leave "a" - set.Put(NS_LITERAL_CSTRING("d")); - - gTotalTests++; - if (set.Has(NS_LITERAL_CSTRING("a"))) - gPassedTests++; - else - fail("set.Has(\"a\") failed."); - - gTotalTests++; - if (!set.Has(NS_LITERAL_CSTRING("b"))) - gPassedTests++; - else - fail("!set.Has(\"b\") failed."); -} - -int main(int argc, char **argv) -{ - ScopedXPCOM xpcom("URLClassiferUtils"); - - TestUnescape(); - TestEnc(); - TestCanonicalize(); - TestCanonicalNum(); - TestParseIPAddress(); - TestHostname(); - TestLongHostname(); - TestFragmentSet(); - - if (gPassedTests == gTotalTests) - passed(__FILE__); - printf("%d of %d tests passed\n", gPassedTests, gTotalTests); - // Non-zero return status signals test failure to build system. - - return (gPassedTests != gTotalTests); -} diff --git a/toolkit/components/url-classifier/tests/jar.mn b/toolkit/components/url-classifier/tests/jar.mn deleted file mode 100644 index 749c0890ba..0000000000 --- a/toolkit/components/url-classifier/tests/jar.mn +++ /dev/null @@ -1,2 +0,0 @@ -toolkit.jar: -+ content/global/url-classifier/unittests.xul (unittests.xul) diff --git a/toolkit/components/url-classifier/tests/mochitest/allowlistAnnotatedFrame.html b/toolkit/components/url-classifier/tests/mochitest/allowlistAnnotatedFrame.html deleted file mode 100644 index 2259ce94bc..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/allowlistAnnotatedFrame.html +++ /dev/null @@ -1,72 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/toolkit/components/url-classifier/tests/mochitest/chrome.ini b/toolkit/components/url-classifier/tests/mochitest/chrome.ini deleted file mode 100644 index ed0a30f1b7..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/chrome.ini +++ /dev/null @@ -1,8 +0,0 @@ -[DEFAULT] -support-files = - allowlistAnnotatedFrame.html - classifiedAnnotatedFrame.html - -[test_lookup_system_principal.html] -[test_classified_annotations.html] -[test_allowlisted_annotations.html] diff --git a/toolkit/components/url-classifier/tests/mochitest/classifiedAnnotatedFrame.html b/toolkit/components/url-classifier/tests/mochitest/classifiedAnnotatedFrame.html deleted file mode 100644 index 3793a38cee..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/classifiedAnnotatedFrame.html +++ /dev/null @@ -1,135 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - -The following should not be hidden: -
STYLE TEST
- - - diff --git a/toolkit/components/url-classifier/tests/mochitest/classifierFrame.html b/toolkit/components/url-classifier/tests/mochitest/classifierFrame.html deleted file mode 100644 index 1a624ac530..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/classifierFrame.html +++ /dev/null @@ -1,48 +0,0 @@ - - - - - - - - - - - - - - - - - - -The following should not be hidden: -
STYLE TEST
- - diff --git a/toolkit/components/url-classifier/tests/mochitest/cleanWorker.js b/toolkit/components/url-classifier/tests/mochitest/cleanWorker.js deleted file mode 100644 index 6856483730..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/cleanWorker.js +++ /dev/null @@ -1,10 +0,0 @@ -onmessage = function() { - try { - importScripts("evilWorker.js"); - } catch(ex) { - postMessage("success"); - return; - } - - postMessage("failure"); -}; diff --git a/toolkit/components/url-classifier/tests/mochitest/evil.css b/toolkit/components/url-classifier/tests/mochitest/evil.css deleted file mode 100644 index f6f08d7c5c..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/evil.css +++ /dev/null @@ -1 +0,0 @@ -#styleCheck { visibility: hidden; } \ No newline at end of file diff --git a/toolkit/components/url-classifier/tests/mochitest/evil.js b/toolkit/components/url-classifier/tests/mochitest/evil.js deleted file mode 100644 index 27f2e8c43d..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/evil.js +++ /dev/null @@ -1 +0,0 @@ -scriptItem = "loaded malware javascript!"; diff --git a/toolkit/components/url-classifier/tests/mochitest/evilWorker.js b/toolkit/components/url-classifier/tests/mochitest/evilWorker.js deleted file mode 100644 index ac34977d71..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/evilWorker.js +++ /dev/null @@ -1,3 +0,0 @@ -onmessage = function() { - postMessage("loaded bad file"); -} diff --git a/toolkit/components/url-classifier/tests/mochitest/import.css b/toolkit/components/url-classifier/tests/mochitest/import.css deleted file mode 100644 index 4b5803a0a3..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/import.css +++ /dev/null @@ -1,3 +0,0 @@ -/* malware.example.com is in the malware database. - classifierBad.css does not actually exist. */ -@import url("http://malware.example.com/tests/docshell/test/classifierBad.css"); diff --git a/toolkit/components/url-classifier/tests/mochitest/mochitest.ini b/toolkit/components/url-classifier/tests/mochitest/mochitest.ini deleted file mode 100644 index 6a0c32e809..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/mochitest.ini +++ /dev/null @@ -1,15 +0,0 @@ -[DEFAULT] -skip-if = buildapp == 'b2g' || e10s -support-files = - classifierFrame.html - cleanWorker.js - evil.css - evil.js - evilWorker.js - import.css - raptor.jpg - track.html - workerFrame.html - -[test_classifier.html] -[test_classifier_worker.html] diff --git a/toolkit/components/url-classifier/tests/mochitest/raptor.jpg b/toolkit/components/url-classifier/tests/mochitest/raptor.jpg deleted file mode 100644 index 243ba9e2d4..0000000000 Binary files a/toolkit/components/url-classifier/tests/mochitest/raptor.jpg and /dev/null differ diff --git a/toolkit/components/url-classifier/tests/mochitest/test_allowlisted_annotations.html b/toolkit/components/url-classifier/tests/mochitest/test_allowlisted_annotations.html deleted file mode 100644 index 207cb114ba..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/test_allowlisted_annotations.html +++ /dev/null @@ -1,86 +0,0 @@ - - - - Test the URI Classifier - - - - - -

- -
-
-
-
-
- - - diff --git a/toolkit/components/url-classifier/tests/mochitest/test_classified_annotations.html b/toolkit/components/url-classifier/tests/mochitest/test_classified_annotations.html deleted file mode 100644 index d29c8eea87..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/test_classified_annotations.html +++ /dev/null @@ -1,81 +0,0 @@ - - - - Test the URI Classifier - - - - - -

- -
-
-
-
-
- - - diff --git a/toolkit/components/url-classifier/tests/mochitest/test_classifier.html b/toolkit/components/url-classifier/tests/mochitest/test_classifier.html deleted file mode 100644 index 27b6407520..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/test_classifier.html +++ /dev/null @@ -1,78 +0,0 @@ - - - - Test the URI Classifier - - - - -

- -
-
-
-
-
- - - diff --git a/toolkit/components/url-classifier/tests/mochitest/test_classifier_worker.html b/toolkit/components/url-classifier/tests/mochitest/test_classifier_worker.html deleted file mode 100644 index f88084a83c..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/test_classifier_worker.html +++ /dev/null @@ -1,89 +0,0 @@ - - - - Test the URI Classifier - - - - -

- -
-
-
-
-
- - - diff --git a/toolkit/components/url-classifier/tests/mochitest/test_lookup_system_principal.html b/toolkit/components/url-classifier/tests/mochitest/test_lookup_system_principal.html deleted file mode 100644 index fa61e6a00b..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/test_lookup_system_principal.html +++ /dev/null @@ -1,29 +0,0 @@ - - - - Test that lookup() on a system principal doesn't crash - - - - - - -
-
-
-
- - diff --git a/toolkit/components/url-classifier/tests/mochitest/track.html b/toolkit/components/url-classifier/tests/mochitest/track.html deleted file mode 100644 index 8785e7c5b1..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/track.html +++ /dev/null @@ -1,7 +0,0 @@ - - - - -

Tracking Works!

- - diff --git a/toolkit/components/url-classifier/tests/mochitest/workerFrame.html b/toolkit/components/url-classifier/tests/mochitest/workerFrame.html deleted file mode 100644 index 23f0a25f8a..0000000000 --- a/toolkit/components/url-classifier/tests/mochitest/workerFrame.html +++ /dev/null @@ -1,49 +0,0 @@ - - - - - - - - - - - diff --git a/toolkit/components/url-classifier/tests/moz.build b/toolkit/components/url-classifier/tests/moz.build deleted file mode 100644 index 96d5d3b2fb..0000000000 --- a/toolkit/components/url-classifier/tests/moz.build +++ /dev/null @@ -1,21 +0,0 @@ -# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*- -# vim: set filetype=python: -# 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/. - -MOCHITEST_MANIFESTS += ['mochitest/mochitest.ini'] -MOCHITEST_CHROME_MANIFESTS += ['mochitest/chrome.ini'] -XPCSHELL_TESTS_MANIFESTS += ['unit/xpcshell.ini'] - -JAR_MANIFESTS += ['jar.mn'] - -#DEFINES['MOZILLA_INTERNAL_API'] = True - -# XXX Get this to work in libxul builds. -## simple c++ tests (no xpcom) -#CPP_UNIT_TESTS += [ \ -# 'TestUrlClassifierUtils', \ -#] -# -#SOURCES += sorted('%s.cpp' % t for t in CPP_UNIT_TESTS) diff --git a/toolkit/components/url-classifier/tests/unit/data/digest1.chunk b/toolkit/components/url-classifier/tests/unit/data/digest1.chunk deleted file mode 100644 index 3850373c19..0000000000 Binary files a/toolkit/components/url-classifier/tests/unit/data/digest1.chunk and /dev/null differ diff --git a/toolkit/components/url-classifier/tests/unit/data/digest2.chunk b/toolkit/components/url-classifier/tests/unit/data/digest2.chunk deleted file mode 100644 index 738c96f6ba..0000000000 --- a/toolkit/components/url-classifier/tests/unit/data/digest2.chunk +++ /dev/null @@ -1,2 +0,0 @@ -a:5:32:32 -“Ê_Há^˜aÍ7ÂÙ]´=#ÌnmåÃøún‹æo—ÌQ‰ \ No newline at end of file diff --git a/toolkit/components/url-classifier/tests/unit/head_urlclassifier.js b/toolkit/components/url-classifier/tests/unit/head_urlclassifier.js deleted file mode 100644 index 69ecf2fda6..0000000000 --- a/toolkit/components/url-classifier/tests/unit/head_urlclassifier.js +++ /dev/null @@ -1,370 +0,0 @@ -//* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- * -function dumpn(s) { - dump(s + "\n"); -} - -const NS_APP_USER_PROFILE_50_DIR = "ProfD"; -const NS_APP_USER_PROFILE_LOCAL_50_DIR = "ProfLD"; - -const Cc = Components.classes; -const Ci = Components.interfaces; -const Cu = Components.utils; -const Cr = Components.results; - -Cu.import("resource://testing-common/httpd.js"); - -do_get_profile(); - -var dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties); - -var iosvc = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); - -var secMan = Cc["@mozilla.org/scriptsecuritymanager;1"] - .getService(Ci.nsIScriptSecurityManager); - -// Disable hashcompleter noise for tests -var prefBranch = Cc["@mozilla.org/preferences-service;1"]. - getService(Ci.nsIPrefBranch); -prefBranch.setIntPref("urlclassifier.gethashnoise", 0); - -// Enable malware/phishing checking for tests -prefBranch.setBoolPref("browser.safebrowsing.malware.enabled", true); -prefBranch.setBoolPref("browser.safebrowsing.enabled", true); - -// Enable all completions for tests -prefBranch.setCharPref("urlclassifier.disallow_completions", ""); - -// Hash completion timeout -prefBranch.setIntPref("urlclassifier.gethash.timeout_ms", 5000); - -function delFile(name) { - try { - // Delete a previously created sqlite file - var file = dirSvc.get('ProfLD', Ci.nsIFile); - file.append(name); - if (file.exists()) - file.remove(false); - } catch(e) { - } -} - -function cleanUp() { - delFile("urlclassifier3.sqlite"); - delFile("safebrowsing/classifier.hashkey"); - delFile("safebrowsing/test-phish-simple.sbstore"); - delFile("safebrowsing/test-malware-simple.sbstore"); - delFile("safebrowsing/test-phish-simple.cache"); - delFile("safebrowsing/test-malware-simple.cache"); - delFile("safebrowsing/test-phish-simple.pset"); - delFile("safebrowsing/test-malware-simple.pset"); - delFile("testLarge.pset"); - delFile("testNoDelta.pset"); -} - -var allTables = "test-phish-simple,test-malware-simple"; - -var dbservice = Cc["@mozilla.org/url-classifier/dbservice;1"].getService(Ci.nsIUrlClassifierDBService); -var streamUpdater = Cc["@mozilla.org/url-classifier/streamupdater;1"] - .getService(Ci.nsIUrlClassifierStreamUpdater); - - -/* - * Builds an update from an object that looks like: - *{ "test-phish-simple" : [{ - * "chunkType" : "a", // 'a' is assumed if not specified - * "chunkNum" : 1, // numerically-increasing chunk numbers are assumed - * // if not specified - * "urls" : [ "foo.com/a", "foo.com/b", "bar.com/" ] - * } - */ - -function buildUpdate(update, hashSize) { - if (!hashSize) { - hashSize = 32; - } - var updateStr = "n:1000\n"; - - for (var tableName in update) { - if (tableName != "") - updateStr += "i:" + tableName + "\n"; - var chunks = update[tableName]; - for (var j = 0; j < chunks.length; j++) { - var chunk = chunks[j]; - var chunkType = chunk.chunkType ? chunk.chunkType : 'a'; - var chunkNum = chunk.chunkNum ? chunk.chunkNum : j; - updateStr += chunkType + ':' + chunkNum + ':' + hashSize; - - if (chunk.urls) { - var chunkData = chunk.urls.join("\n"); - updateStr += ":" + chunkData.length + "\n" + chunkData; - } - - updateStr += "\n"; - } - } - - return updateStr; -} - -function buildPhishingUpdate(chunks, hashSize) { - return buildUpdate({"test-phish-simple" : chunks}, hashSize); -} - -function buildMalwareUpdate(chunks, hashSize) { - return buildUpdate({"test-malware-simple" : chunks}, hashSize); -} - -function buildBareUpdate(chunks, hashSize) { - return buildUpdate({"" : chunks}, hashSize); -} - -/** - * Performs an update of the dbservice manually, bypassing the stream updater - */ -function doSimpleUpdate(updateText, success, failure) { - var listener = { - QueryInterface: function(iid) - { - if (iid.equals(Ci.nsISupports) || - iid.equals(Ci.nsIUrlClassifierUpdateObserver)) - return this; - throw Cr.NS_ERROR_NO_INTERFACE; - }, - - updateUrlRequested: function(url) { }, - streamFinished: function(status) { }, - updateError: function(errorCode) { failure(errorCode); }, - updateSuccess: function(requestedTimeout) { success(requestedTimeout); } - }; - - dbservice.beginUpdate(listener, - "test-phish-simple,test-malware-simple"); - dbservice.beginStream("", ""); - dbservice.updateStream(updateText); - dbservice.finishStream(); - dbservice.finishUpdate(); -} - -/** - * Simulates a failed database update. - */ -function doErrorUpdate(tables, success, failure) { - var listener = { - QueryInterface: function(iid) - { - if (iid.equals(Ci.nsISupports) || - iid.equals(Ci.nsIUrlClassifierUpdateObserver)) - return this; - throw Cr.NS_ERROR_NO_INTERFACE; - }, - - updateUrlRequested: function(url) { }, - streamFinished: function(status) { }, - updateError: function(errorCode) { success(errorCode); }, - updateSuccess: function(requestedTimeout) { failure(requestedTimeout); } - }; - - dbservice.beginUpdate(listener, tables, null); - dbservice.beginStream("", ""); - dbservice.cancelUpdate(); -} - -/** - * Performs an update of the dbservice using the stream updater and a - * data: uri - */ -function doStreamUpdate(updateText, success, failure, downloadFailure) { - var dataUpdate = "data:," + encodeURIComponent(updateText); - - if (!downloadFailure) { - downloadFailure = failure; - } - - streamUpdater.downloadUpdates("test-phish-simple,test-malware-simple", "", - dataUpdate, success, failure, downloadFailure); -} - -var gAssertions = { - -tableData : function(expectedTables, cb) -{ - dbservice.getTables(function(tables) { - // rebuild the tables in a predictable order. - var parts = tables.split("\n"); - while (parts[parts.length - 1] == '') { - parts.pop(); - } - parts.sort(); - tables = parts.join("\n"); - - do_check_eq(tables, expectedTables); - cb(); - }); -}, - -checkUrls: function(urls, expected, cb) -{ - // work with a copy of the list. - urls = urls.slice(0); - var doLookup = function() { - if (urls.length > 0) { - var fragment = urls.shift(); - var principal = secMan.getNoAppCodebasePrincipal(iosvc.newURI("http://" + fragment, null, null)); - dbservice.lookup(principal, allTables, - function(arg) { - do_check_eq(expected, arg); - doLookup(); - }, true); - } else { - cb(); - } - }; - doLookup(); -}, - -urlsDontExist: function(urls, cb) -{ - this.checkUrls(urls, '', cb); -}, - -urlsExist: function(urls, cb) -{ - this.checkUrls(urls, 'test-phish-simple', cb); -}, - -malwareUrlsExist: function(urls, cb) -{ - this.checkUrls(urls, 'test-malware-simple', cb); -}, - -subsDontExist: function(urls, cb) -{ - // XXX: there's no interface for checking items in the subs table - cb(); -}, - -subsExist: function(urls, cb) -{ - // XXX: there's no interface for checking items in the subs table - cb(); -} - -}; - -/** - * Check a set of assertions against the gAssertions table. - */ -function checkAssertions(assertions, doneCallback) -{ - var checkAssertion = function() { - for (var i in assertions) { - var data = assertions[i]; - delete assertions[i]; - gAssertions[i](data, checkAssertion); - return; - } - - doneCallback(); - } - - checkAssertion(); -} - -function updateError(arg) -{ - do_throw(arg); -} - -// Runs a set of updates, and then checks a set of assertions. -function doUpdateTest(updates, assertions, successCallback, errorCallback) { - var errorUpdate = function() { - checkAssertions(assertions, errorCallback); - } - - var runUpdate = function() { - if (updates.length > 0) { - var update = updates.shift(); - doStreamUpdate(update, runUpdate, errorUpdate, null); - } else { - checkAssertions(assertions, successCallback); - } - } - - runUpdate(); -} - -var gTests; -var gNextTest = 0; - -function runNextTest() -{ - if (gNextTest >= gTests.length) { - do_test_finished(); - return; - } - - dbservice.resetDatabase(); - dbservice.setHashCompleter('test-phish-simple', null); - - let test = gTests[gNextTest++]; - dump("running " + test.name + "\n"); - test(); -} - -function runTests(tests) -{ - gTests = tests; - runNextTest(); -} - -var timerArray = []; - -function Timer(delay, cb) { - this.cb = cb; - var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); - timer.initWithCallback(this, delay, timer.TYPE_ONE_SHOT); - timerArray.push(timer); -} - -Timer.prototype = { -QueryInterface: function(iid) { - if (!iid.equals(Ci.nsISupports) && !iid.equals(Ci.nsITimerCallback)) { - throw Cr.NS_ERROR_NO_INTERFACE; - } - return this; - }, -notify: function(timer) { - this.cb(); - } -} - -// LFSRgenerator is a 32-bit linear feedback shift register random number -// generator. It is highly predictable and is not intended to be used for -// cryptography but rather to allow easier debugging than a test that uses -// Math.random(). -function LFSRgenerator(seed) { - // Force |seed| to be a number. - seed = +seed; - // LFSR generators do not work with a value of 0. - if (seed == 0) - seed = 1; - - this._value = seed; -} -LFSRgenerator.prototype = { - // nextNum returns a random unsigned integer of in the range [0,2^|bits|]. - nextNum: function(bits) { - if (!bits) - bits = 32; - - let val = this._value; - // Taps are 32, 22, 2 and 1. - let bit = ((val >>> 0) ^ (val >>> 10) ^ (val >>> 30) ^ (val >>> 31)) & 1; - val = (val >>> 1) | (bit << 31); - this._value = val; - - return (val >>> (32 - bits)); - }, -}; - -cleanUp(); diff --git a/toolkit/components/url-classifier/tests/unit/tail_urlclassifier.js b/toolkit/components/url-classifier/tests/unit/tail_urlclassifier.js deleted file mode 100644 index 37f39d1a8d..0000000000 --- a/toolkit/components/url-classifier/tests/unit/tail_urlclassifier.js +++ /dev/null @@ -1 +0,0 @@ -cleanUp(); diff --git a/toolkit/components/url-classifier/tests/unit/test_addsub.js b/toolkit/components/url-classifier/tests/unit/test_addsub.js deleted file mode 100644 index 1ed65c7baa..0000000000 --- a/toolkit/components/url-classifier/tests/unit/test_addsub.js +++ /dev/null @@ -1,488 +0,0 @@ - -function doTest(updates, assertions) -{ - doUpdateTest(updates, assertions, runNextTest, updateError); -} - -// Test an add of two urls to a fresh database -function testSimpleAdds() { - var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ]; - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls - }]); - - var assertions = { - "tableData" : "test-phish-simple;a:1", - "urlsExist" : addUrls - }; - - doTest([update], assertions); -} - -// Same as testSimpleAdds, but make the same-domain URLs come from different -// chunks. -function testMultipleAdds() { - var add1Urls = [ "foo.com/a", "bar.com/c" ]; - var add2Urls = [ "foo.com/b" ]; - - var update = buildPhishingUpdate( - [{ "chunkNum" : 1, - "urls" : add1Urls }, - { "chunkNum" : 2, - "urls" : add2Urls }]); - var assertions = { - "tableData" : "test-phish-simple;a:1-2", - "urlsExist" : add1Urls.concat(add2Urls) - }; - - doTest([update], assertions); -} - -// Test that a sub will remove an existing add -function testSimpleSub() -{ - var addUrls = ["foo.com/a", "bar.com/b"]; - var subUrls = ["1:foo.com/a"]; - - var addUpdate = buildPhishingUpdate( - [{ "chunkNum" : 1, // adds and subtracts don't share a chunk numbering space - "urls": addUrls }]); - - var subUpdate = buildPhishingUpdate( - [{ "chunkNum" : 50, - "chunkType" : "s", - "urls": subUrls }]); - - - var assertions = { - "tableData" : "test-phish-simple;a:1:s:50", - "urlsExist" : [ "bar.com/b" ], - "urlsDontExist": ["foo.com/a" ], - "subsDontExist" : [ "foo.com/a" ] - } - - doTest([addUpdate, subUpdate], assertions); - -} - -// Same as testSimpleSub(), but the sub comes in before the add. -function testSubEmptiesAdd() -{ - var subUrls = ["1:foo.com/a"]; - var addUrls = ["foo.com/a", "bar.com/b"]; - - var subUpdate = buildPhishingUpdate( - [{ "chunkNum" : 50, - "chunkType" : "s", - "urls": subUrls }]); - - var addUpdate = buildPhishingUpdate( - [{ "chunkNum" : 1, - "urls": addUrls }]); - - var assertions = { - "tableData" : "test-phish-simple;a:1:s:50", - "urlsExist" : [ "bar.com/b" ], - "urlsDontExist": ["foo.com/a" ], - "subsDontExist" : [ "foo.com/a" ] // this sub was found, it shouldn't exist anymore - } - - doTest([subUpdate, addUpdate], assertions); -} - -// Very similar to testSubEmptiesAdd, except that the domain entry will -// still have an item left over that needs to be synced. -function testSubPartiallyEmptiesAdd() -{ - var subUrls = ["1:foo.com/a"]; - var addUrls = ["foo.com/a", "foo.com/b", "bar.com/b"]; - - var subUpdate = buildPhishingUpdate( - [{ "chunkNum" : 1, - "chunkType" : "s", - "urls": subUrls }]); - - var addUpdate = buildPhishingUpdate( - [{ "chunkNum" : 1, // adds and subtracts don't share a chunk numbering space - "urls": addUrls }]); - - var assertions = { - "tableData" : "test-phish-simple;a:1:s:1", - "urlsExist" : [ "foo.com/b", "bar.com/b" ], - "urlsDontExist" : ["foo.com/a" ], - "subsDontExist" : [ "foo.com/a" ] // this sub was found, it shouldn't exist anymore - } - - doTest([subUpdate, addUpdate], assertions); -} - -// We SHOULD be testing that pending subs are removed using -// subsDontExist assertions. Since we don't have a good interface for getting -// at sub entries, we'll verify it by side-effect. Subbing a url once -// then adding it twice should leave the url intact. -function testPendingSubRemoved() -{ - var subUrls = ["1:foo.com/a", "2:foo.com/b"]; - var addUrls = ["foo.com/a", "foo.com/b"]; - - var subUpdate = buildPhishingUpdate( - [{ "chunkNum" : 1, - "chunkType" : "s", - "urls": subUrls }]); - - var addUpdate1 = buildPhishingUpdate( - [{ "chunkNum" : 1, // adds and subtracts don't share a chunk numbering space - "urls": addUrls }]); - - var addUpdate2 = buildPhishingUpdate( - [{ "chunkNum" : 2, - "urls": addUrls }]); - - var assertions = { - "tableData" : "test-phish-simple;a:1-2:s:1", - "urlsExist" : [ "foo.com/a", "foo.com/b" ], - "subsDontExist" : [ "foo.com/a", "foo.com/b" ] // this sub was found, it shouldn't exist anymore - } - - doTest([subUpdate, addUpdate1, addUpdate2], assertions); -} - -// Make sure that a saved sub is removed when the sub chunk is expired. -function testPendingSubExpire() -{ - var subUrls = ["1:foo.com/a", "1:foo.com/b"]; - var addUrls = ["foo.com/a", "foo.com/b"]; - - var subUpdate = buildPhishingUpdate( - [{ "chunkNum" : 1, - "chunkType" : "s", - "urls": subUrls }]); - - var expireUpdate = buildPhishingUpdate( - [{ "chunkNum" : 1, - "chunkType" : "sd" }]); - - var addUpdate = buildPhishingUpdate( - [{ "chunkNum" : 1, // adds and subtracts don't share a chunk numbering space - "urls": addUrls }]); - - var assertions = { - "tableData" : "test-phish-simple;a:1", - "urlsExist" : [ "foo.com/a", "foo.com/b" ], - "subsDontExist" : [ "foo.com/a", "foo.com/b" ] // this sub was expired - } - - doTest([subUpdate, expireUpdate, addUpdate], assertions); -} - -// Make sure that the sub url removes from only the chunk that it specifies -function testDuplicateAdds() -{ - var urls = ["foo.com/a"]; - - var addUpdate1 = buildPhishingUpdate( - [{ "chunkNum" : 1, - "urls": urls }]); - var addUpdate2 = buildPhishingUpdate( - [{ "chunkNum" : 2, - "urls": urls }]); - var subUpdate = buildPhishingUpdate( - [{ "chunkNum" : 3, - "chunkType" : "s", - "urls": ["2:foo.com/a"]}]); - - var assertions = { - "tableData" : "test-phish-simple;a:1-2:s:3", - "urlsExist" : [ "foo.com/a"], - "subsDontExist" : [ "foo.com/a"] - } - - doTest([addUpdate1, addUpdate2, subUpdate], assertions); -} - -// Tests a sub which matches some existing adds but leaves others. -function testSubPartiallyMatches() -{ - var subUrls = ["foo.com/a"]; - var addUrls = ["1:foo.com/a", "2:foo.com/b"]; - - var addUpdate = buildPhishingUpdate( - [{ "chunkNum" : 1, - "urls" : addUrls }]); - - var subUpdate = buildPhishingUpdate( - [{ "chunkNum" : 1, - "chunkType" : "s", - "urls" : addUrls }]); - - var assertions = { - "tableData" : "test-phish-simple;a:1:s:1", - "urlsDontExist" : ["foo.com/a"], - "subsDontExist" : ["foo.com/a"], - "subsExist" : ["foo.com/b"] - }; - - doTest([addUpdate, subUpdate], assertions); -} - -// XXX: because subsExist isn't actually implemented, this is the same -// test as above but with a second add chunk that should fail to be added -// because of a pending sub chunk. -function testSubPartiallyMatches2() -{ - var addUrls = ["foo.com/a"]; - var subUrls = ["1:foo.com/a", "2:foo.com/b"]; - var addUrls2 = ["foo.com/b"]; - - var addUpdate = buildPhishingUpdate( - [{ "chunkNum" : 1, - "urls" : addUrls }]); - - var subUpdate = buildPhishingUpdate( - [{ "chunkNum" : 1, - "chunkType" : "s", - "urls" : subUrls }]); - - var addUpdate2 = buildPhishingUpdate( - [{ "chunkNum" : 2, - "urls" : addUrls2 }]); - - var assertions = { - "tableData" : "test-phish-simple;a:1-2:s:1", - "urlsDontExist" : ["foo.com/a", "foo.com/b"], - "subsDontExist" : ["foo.com/a", "foo.com/b"] - }; - - doTest([addUpdate, subUpdate, addUpdate2], assertions); -} - -// Verify that two subs for the same domain but from different chunks -// match (tests that existing sub entries are properly updated) -function testSubsDifferentChunks() { - var subUrls1 = [ "3:foo.com/a" ]; - var subUrls2 = [ "3:foo.com/b" ]; - - var addUrls = [ "foo.com/a", "foo.com/b", "foo.com/c" ]; - - var subUpdate1 = buildPhishingUpdate( - [{ "chunkNum" : 1, - "chunkType" : "s", - "urls": subUrls1 }]); - var subUpdate2 = buildPhishingUpdate( - [{ "chunkNum" : 2, - "chunkType" : "s", - "urls" : subUrls2 }]); - var addUpdate = buildPhishingUpdate( - [{ "chunkNum" : 3, - "urls" : addUrls }]); - - var assertions = { - "tableData" : "test-phish-simple;a:3:s:1-2", - "urlsExist" : [ "foo.com/c" ], - "urlsDontExist" : [ "foo.com/a", "foo.com/b" ], - "subsDontExist" : [ "foo.com/a", "foo.com/b" ] - }; - - doTest([subUpdate1, subUpdate2, addUpdate], assertions); -} - -// for bug 534079 -function testSubsDifferentChunksSameHostId() { - var subUrls1 = [ "1:foo.com/a" ]; - var subUrls2 = [ "1:foo.com/b", "2:foo.com/c" ]; - - var addUrls = [ "foo.com/a", "foo.com/b" ]; - var addUrls2 = [ "foo.com/c" ]; - - var subUpdate1 = buildPhishingUpdate( - [{ "chunkNum" : 1, - "chunkType" : "s", - "urls": subUrls1 }]); - var subUpdate2 = buildPhishingUpdate( - [{ "chunkNum" : 2, - "chunkType" : "s", - "urls" : subUrls2 }]); - - var addUpdate = buildPhishingUpdate( - [{ "chunkNum" : 1, - "urls" : addUrls }]); - var addUpdate2 = buildPhishingUpdate( - [{ "chunkNum" : 2, - "urls" : addUrls2 }]); - - var assertions = { - "tableData" : "test-phish-simple;a:1-2:s:1-2", - "urlsDontExist" : [ "foo.com/c", "foo.com/b", "foo.com/a", ], - }; - - doTest([addUpdate, addUpdate2, subUpdate1, subUpdate2], assertions); -} - -// Test lists of expired chunks -function testExpireLists() { - var addUpdate = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : [ "foo.com/a" ] - }, - { "chunkNum" : 3, - "urls" : [ "bar.com/a" ] - }, - { "chunkNum" : 4, - "urls" : [ "baz.com/a" ] - }, - { "chunkNum" : 5, - "urls" : [ "blah.com/a" ] - }, - ]); - var subUpdate = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "chunkType" : "s", - "urls" : [ "50:foo.com/1" ] - }, - { "chunkNum" : 2, - "chunkType" : "s", - "urls" : [ "50:bar.com/1" ] - }, - { "chunkNum" : 3, - "chunkType" : "s", - "urls" : [ "50:baz.com/1" ] - }, - { "chunkNum" : 5, - "chunkType" : "s", - "urls" : [ "50:blah.com/1" ] - }, - ]); - - var expireUpdate = buildPhishingUpdate( - [ { "chunkType" : "ad:1,3-5" }, - { "chunkType" : "sd:1-3,5" }]); - - var assertions = { - // "tableData" : "test-phish-simple;" - "tableData": "" - }; - - doTest([addUpdate, subUpdate, expireUpdate], assertions); -} - -// Test a duplicate add chunk. -function testDuplicateAddChunks() { - var addUrls1 = [ "foo.com/a" ]; - var addUrls2 = [ "bar.com/b" ]; - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls1 - }, - { "chunkNum" : 1, - "urls" : addUrls2 - }]); - - var assertions = { - "tableData" : "test-phish-simple;a:1", - "urlsExist" : addUrls1, - "urlsDontExist" : addUrls2 - }; - - doTest([update], assertions); -} - -// This test is a bit tricky. We want to test that an add removes all -// subs with the same add chunk id, even if there is no match. To do -// that we need to add the same add chunk twice, with an expiration -// in the middle. This would be easier if subsDontExist actually -// worked... -function testExpireWholeSub() -{ - var subUrls = ["1:foo.com/a"]; - - var update = buildPhishingUpdate( - [{ "chunkNum" : 5, - "chunkType" : "s", - "urls" : subUrls - }, - // empty add chunk should still cause foo.com/a to go away. - { "chunkNum" : 1, - "urls" : [] - }, - // and now adding chunk 1 again with foo.com/a should succeed, - // because the sub should have been expired with the empty - // add chunk. - - // we need to expire this chunk to let us add chunk 1 again. - { - "chunkType" : "ad:1" - }, - { "chunkNum" : 1, - "urls" : [ "foo.com/a" ] - }]); - - var assertions = { - "tableData" : "test-phish-simple;a:1:s:5", - "urlsExist" : ["foo.com/a"] - }; - - doTest([update], assertions); -} - - -// This test is roughly the opposite of testExpireWholeSub(). We add -// the empty add first, and make sure that it prevents a sub for that -// add from being applied. -function testPreventWholeSub() -{ - var subUrls = ["1:foo.com/a"]; - - var update = buildPhishingUpdate( - [ // empty add chunk should cause foo.com/a to not be saved - { "chunkNum" : 1, - "urls" : [] - }, - { "chunkNum" : 5, - "chunkType" : "s", - "urls" : subUrls - }, - // and now adding chunk 1 again with foo.com/a should succeed, - // because the sub should have been expired with the empty - // add chunk. - - // we need to expire this chunk to let us add chunk 1 again. - { - "chunkType" : "ad:1" - }, - { "chunkNum" : 1, - "urls" : [ "foo.com/a" ] - }]); - - var assertions = { - "tableData" : "test-phish-simple;a:1:s:5", - "urlsExist" : ["foo.com/a"] - }; - - doTest([update], assertions); -} - -function run_test() -{ - runTests([ - testSimpleAdds, - testMultipleAdds, - testSimpleSub, - testSubEmptiesAdd, - testSubPartiallyEmptiesAdd, - testPendingSubRemoved, - testPendingSubExpire, - testDuplicateAdds, - testSubPartiallyMatches, - testSubPartiallyMatches2, - testSubsDifferentChunks, - testSubsDifferentChunksSameHostId, - testExpireLists - ]); -} - -do_test_pending(); diff --git a/toolkit/components/url-classifier/tests/unit/test_backoff.js b/toolkit/components/url-classifier/tests/unit/test_backoff.js deleted file mode 100644 index 365568c479..0000000000 --- a/toolkit/components/url-classifier/tests/unit/test_backoff.js +++ /dev/null @@ -1,89 +0,0 @@ -// Some unittests (e.g., paste into JS shell) -var jslib = Cc["@mozilla.org/url-classifier/jslib;1"]. - getService().wrappedJSObject; -var _Datenow = jslib.Date.now; -function setNow(time) { - jslib.Date.now = function() { - return time; - } -} - -function run_test() { - // 3 errors, 1ms retry period, max 3 requests per ten milliseconds, - // 5ms backoff interval, 19ms max delay - var rb = new jslib.RequestBackoff(3, 1, 3, 10, 5, 19); - setNow(1); - rb.noteServerResponse(200); - do_check_true(rb.canMakeRequest()); - setNow(2); - do_check_true(rb.canMakeRequest()); - - // First error should trigger a 1ms delay - rb.noteServerResponse(500); - do_check_false(rb.canMakeRequest()); - do_check_eq(rb.nextRequestTime_, 3); - setNow(3); - do_check_true(rb.canMakeRequest()); - - // Second error should also trigger a 1ms delay - rb.noteServerResponse(500); - do_check_false(rb.canMakeRequest()); - do_check_eq(rb.nextRequestTime_, 4); - setNow(4); - do_check_true(rb.canMakeRequest()); - - // Third error should trigger a 5ms backoff - rb.noteServerResponse(500); - do_check_false(rb.canMakeRequest()); - do_check_eq(rb.nextRequestTime_, 9); - setNow(9); - do_check_true(rb.canMakeRequest()); - - // Trigger backoff again - rb.noteServerResponse(503); - do_check_false(rb.canMakeRequest()); - do_check_eq(rb.nextRequestTime_, 19); - setNow(19); - do_check_true(rb.canMakeRequest()); - - // Trigger backoff a third time and hit max timeout - rb.noteServerResponse(302); - do_check_false(rb.canMakeRequest()); - do_check_eq(rb.nextRequestTime_, 38); - setNow(38); - do_check_true(rb.canMakeRequest()); - - // One more backoff, should still be at the max timeout - rb.noteServerResponse(400); - do_check_false(rb.canMakeRequest()); - do_check_eq(rb.nextRequestTime_, 57); - setNow(57); - do_check_true(rb.canMakeRequest()); - - // Request goes through - rb.noteServerResponse(200); - do_check_true(rb.canMakeRequest()); - do_check_eq(rb.nextRequestTime_, 0); - setNow(58); - rb.noteServerResponse(500); - - // Another error, should trigger a 1ms backoff - do_check_false(rb.canMakeRequest()); - do_check_eq(rb.nextRequestTime_, 59); - - setNow(59); - do_check_true(rb.canMakeRequest()); - - setNow(200); - rb.noteRequest(); - setNow(201); - rb.noteRequest(); - setNow(202); - do_check_true(rb.canMakeRequest()); - rb.noteRequest(); - do_check_false(rb.canMakeRequest()); - setNow(211); - do_check_true(rb.canMakeRequest()); - - jslib.Date.now = _Datenow; -} diff --git a/toolkit/components/url-classifier/tests/unit/test_dbservice.js b/toolkit/components/url-classifier/tests/unit/test_dbservice.js deleted file mode 100644 index 0f9108be83..0000000000 --- a/toolkit/components/url-classifier/tests/unit/test_dbservice.js +++ /dev/null @@ -1,260 +0,0 @@ -var checkUrls = []; -var checkExpect; - -var chunk1Urls = [ - "test.com/aba", - "test.com/foo/bar", - "foo.bar.com/a/b/c" -]; -var chunk1 = chunk1Urls.join("\n"); - -var chunk2Urls = [ - "blah.com/a", - "baz.com/", - "255.255.0.1/", - "www.foo.com/test2?param=1" -]; -var chunk2 = chunk2Urls.join("\n"); - -var chunk3Urls = [ - "test.com/a", - "foo.bar.com/a", - "blah.com/a", - ]; -var chunk3 = chunk3Urls.join("\n"); - -var chunk3SubUrls = [ - "1:test.com/a", - "1:foo.bar.com/a", - "2:blah.com/a" ]; -var chunk3Sub = chunk3SubUrls.join("\n"); - -var chunk4Urls = [ - "a.com/b", - "b.com/c", - ]; -var chunk4 = chunk4Urls.join("\n"); - -var chunk5Urls = [ - "d.com/e", - "f.com/g", - ]; -var chunk5 = chunk5Urls.join("\n"); - -var chunk6Urls = [ - "h.com/i", - "j.com/k", - ]; -var chunk6 = chunk6Urls.join("\n"); - -// we are going to add chunks 1, 2, 4, 5, and 6 to phish-simple, and -// chunk 2 to malware-simple. Then we'll remove the urls in chunk3 -// from phish-simple, then expire chunk 1 and chunks 4-6 from -// phish-simple. -var phishExpected = {}; -var phishUnexpected = {}; -var malwareExpected = {}; -for (var i = 0; i < chunk2Urls.length; i++) { - phishExpected[chunk2Urls[i]] = true; - malwareExpected[chunk2Urls[i]] = true; -} -for (var i = 0; i < chunk3Urls.length; i++) { - delete phishExpected[chunk3Urls[i]]; - phishUnexpected[chunk3Urls[i]] = true; -} -for (var i = 0; i < chunk1Urls.length; i++) { - // chunk1 urls are expired - phishUnexpected[chunk1Urls[i]] = true; -} -for (var i = 0; i < chunk4Urls.length; i++) { - // chunk4 urls are expired - phishUnexpected[chunk4Urls[i]] = true; -} -for (var i = 0; i < chunk5Urls.length; i++) { - // chunk5 urls are expired - phishUnexpected[chunk5Urls[i]] = true; -} -for (var i = 0; i < chunk6Urls.length; i++) { - // chunk6 urls are expired - phishUnexpected[chunk6Urls[i]] = true; -} - -// Check that the entries hit based on sub-parts -phishExpected["baz.com/foo/bar"] = true; -phishExpected["foo.bar.baz.com/foo"] = true; -phishExpected["bar.baz.com/"] = true; - -var numExpecting; - -function testFailure(arg) { - do_throw(arg); -} - -function checkNoHost() -{ - // Looking up a no-host uri such as a data: uri should throw an exception. - var exception; - try { - var principal = secMan.getNoAppCodebasePrincipal(iosvc.newURI("data:text/html,test", null, null)); - dbservice.lookup(principal, allTables); - - exception = false; - } catch(e) { - exception = true; - } - do_check_true(exception); - - do_test_finished(); -} - -function tablesCallbackWithoutSub(tables) -{ - var parts = tables.split("\n"); - parts.sort(); - - // there's a leading \n here because splitting left an empty string - // after the trailing newline, which will sort first - do_check_eq(parts.join("\n"), - "\ntest-malware-simple;a:1\ntest-phish-simple;a:2"); - - checkNoHost(); -} - - -function expireSubSuccess(result) { - dbservice.getTables(tablesCallbackWithoutSub); -} - -function tablesCallbackWithSub(tables) -{ - var parts = tables.split("\n"); - parts.sort(); - - // there's a leading \n here because splitting left an empty string - // after the trailing newline, which will sort first - do_check_eq(parts.join("\n"), - "\ntest-malware-simple;a:1\ntest-phish-simple;a:2:s:3"); - - // verify that expiring a sub chunk removes its name from the list - var data = - "n:1000\n" + - "i:test-phish-simple\n" + - "sd:3\n"; - - doSimpleUpdate(data, expireSubSuccess, testFailure); -} - -function checkChunksWithSub() -{ - dbservice.getTables(tablesCallbackWithSub); -} - -function checkDone() { - if (--numExpecting == 0) - checkChunksWithSub(); -} - -function phishExists(result) { - dumpn("phishExists: " + result); - try { - do_check_true(result.indexOf("test-phish-simple") != -1); - } finally { - checkDone(); - } -} - -function phishDoesntExist(result) { - dumpn("phishDoesntExist: " + result); - try { - do_check_true(result.indexOf("test-phish-simple") == -1); - } finally { - checkDone(); - } -} - -function malwareExists(result) { - dumpn("malwareExists: " + result); - - try { - do_check_true(result.indexOf("test-malware-simple") != -1); - } finally { - checkDone(); - } -} - -function checkState() -{ - numExpecting = 0; - - for (var key in phishExpected) { - var principal = secMan.getNoAppCodebasePrincipal(iosvc.newURI("http://" + key, null, null)); - dbservice.lookup(principal, allTables, phishExists, true); - numExpecting++; - } - - for (var key in phishUnexpected) { - var principal = secMan.getNoAppCodebasePrincipal(iosvc.newURI("http://" + key, null, null)); - dbservice.lookup(principal, allTables, phishDoesntExist, true); - numExpecting++; - } - - for (var key in malwareExpected) { - var principal = secMan.getNoAppCodebasePrincipal(iosvc.newURI("http://" + key, null, null)); - dbservice.lookup(principal, allTables, malwareExists, true); - numExpecting++; - } -} - -function testSubSuccess(result) -{ - do_check_eq(result, "1000"); - checkState(); -} - -function do_subs() { - var data = - "n:1000\n" + - "i:test-phish-simple\n" + - "s:3:32:" + chunk3Sub.length + "\n" + - chunk3Sub + "\n" + - "ad:1\n" + - "ad:4-6\n"; - - doSimpleUpdate(data, testSubSuccess, testFailure); -} - -function testAddSuccess(arg) { - do_check_eq(arg, "1000"); - - do_subs(); -} - -function do_adds() { - // This test relies on the fact that only -regexp tables are ungzipped, - // and only -hash tables are assumed to be pre-md5'd. So we use - // a 'simple' table type to get simple hostname-per-line semantics. - - var data = - "n:1000\n" + - "i:test-phish-simple\n" + - "a:1:32:" + chunk1.length + "\n" + - chunk1 + "\n" + - "a:2:32:" + chunk2.length + "\n" + - chunk2 + "\n" + - "a:4:32:" + chunk4.length + "\n" + - chunk4 + "\n" + - "a:5:32:" + chunk5.length + "\n" + - chunk5 + "\n" + - "a:6:32:" + chunk6.length + "\n" + - chunk6 + "\n" + - "i:test-malware-simple\n" + - "a:1:32:" + chunk2.length + "\n" + - chunk2 + "\n"; - - doSimpleUpdate(data, testAddSuccess, testFailure); -} - -function run_test() { - do_adds(); - do_test_pending(); -} diff --git a/toolkit/components/url-classifier/tests/unit/test_digest256.js b/toolkit/components/url-classifier/tests/unit/test_digest256.js deleted file mode 100644 index 74f8bc8d0d..0000000000 --- a/toolkit/components/url-classifier/tests/unit/test_digest256.js +++ /dev/null @@ -1,146 +0,0 @@ -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); - -XPCOMUtils.defineLazyModuleGetter(this, "NetUtil", - "resource://gre/modules/NetUtil.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "Promise", - "resource://gre/modules/Promise.jsm"); -// Global test server for serving safebrowsing updates. -let gHttpServ = null; -// Global nsIUrlClassifierDBService -let gDbService = Cc["@mozilla.org/url-classifier/dbservice;1"] - .getService(Ci.nsIUrlClassifierDBService); -// Security manager for creating nsIPrincipals from URIs -let gSecMan = Cc["@mozilla.org/scriptsecuritymanager;1"] - .getService(Ci.nsIScriptSecurityManager); - -// A map of tables to arrays of update redirect urls. -let gTables = {}; - -// Construct an update from a file. -function readFileToString(aFilename) { - let f = do_get_file(aFilename); - let stream = Cc["@mozilla.org/network/file-input-stream;1"] - .createInstance(Ci.nsIFileInputStream); - stream.init(f, -1, 0, 0); - let buf = NetUtil.readInputStreamToString(stream, stream.available()); - return buf; -} - -// Registers a table for which to serve update chunks. Returns a promise that -// resolves when that chunk has been downloaded. -function registerTableUpdate(aTable, aFilename) { - let deferred = Promise.defer(); - // If we haven't been given an update for this table yet, add it to the map - if (!(aTable in gTables)) { - gTables[aTable] = []; - } - - // The number of chunks associated with this table. - let numChunks = gTables[aTable].length + 1; - let redirectPath = "/" + aTable + "-" + numChunks; - let redirectUrl = "localhost:4444" + redirectPath; - - // Store redirect url for that table so we can return it later when we - // process an update request. - gTables[aTable].push(redirectUrl); - - gHttpServ.registerPathHandler(redirectPath, function(request, response) { - do_print("Mock safebrowsing server handling request for " + redirectPath); - let contents = readFileToString(aFilename); - response.setHeader("Content-Type", - "application/vnd.google.safebrowsing-update", false); - response.setStatusLine(request.httpVersion, 200, "OK"); - response.bodyOutputStream.write(contents, contents.length); - deferred.resolve(contents); - }); - return deferred.promise; -} - -// Construct a response with redirect urls. -function processUpdateRequest() { - let response = "n:1000\n"; - for (let table in gTables) { - response += "i:" + table + "\n"; - for (let i = 0; i < gTables[table].length; ++i) { - response += "u:" + gTables[table][i] + "\n"; - } - } - do_print("Returning update response: " + response); - return response; -} - -// Set up our test server to handle update requests. -function run_test() { - gHttpServ = new HttpServer(); - gHttpServ.registerDirectory("/", do_get_cwd()); - - gHttpServ.registerPathHandler("/downloads", function(request, response) { - let buf = NetUtil.readInputStreamToString(request.bodyInputStream, - request.bodyInputStream.available()); - let blob = processUpdateRequest(); - response.setHeader("Content-Type", - "application/vnd.google.safebrowsing-update", false); - response.setStatusLine(request.httpVersion, 200, "OK"); - response.bodyOutputStream.write(blob, blob.length); - }); - - gHttpServ.start(4444); - run_next_test(); -} - -function createURI(s) { - let service = Cc["@mozilla.org/network/io-service;1"] - .getService(Ci.nsIIOService); - return service.newURI(s, null, null); -} - -// Just throw if we ever get an update or download error. -function handleError(aEvent) { - do_throw("We didn't download or update correctly: " + aEvent); -} - -add_test(function test_update() { - let streamUpdater = Cc["@mozilla.org/url-classifier/streamupdater;1"] - .getService(Ci.nsIUrlClassifierStreamUpdater); - - // Load up some update chunks for the safebrowsing server to serve. - registerTableUpdate("goog-downloadwhite-digest256", "data/digest1.chunk"); - registerTableUpdate("goog-downloadwhite-digest256", "data/digest2.chunk"); - - // Download some updates, and don't continue until the downloads are done. - function updateSuccess(aEvent) { - // Timeout of n:1000 is constructed in processUpdateRequest above and - // passed back in the callback in nsIUrlClassifierStreamUpdater on success. - do_check_eq("1000", aEvent); - do_print("All data processed"); - run_next_test(); - } - streamUpdater.downloadUpdates( - "goog-downloadwhite-digest256", - "goog-downloadwhite-digest256;\n", - "http://localhost:4444/downloads", - updateSuccess, handleError, handleError); -}); - -add_test(function test_url_not_whitelisted() { - let uri = createURI("http://example.com"); - let principal = gSecMan.getNoAppCodebasePrincipal(uri); - gDbService.lookup(principal, "goog-downloadwhite-digest256", - function handleEvent(aEvent) { - // This URI is not on any lists. - do_check_eq("", aEvent); - run_next_test(); - }); -}); - -add_test(function test_url_whitelisted() { - // Hash of "whitelisted.com/" (canonicalized URL) is: - // 93CA5F48E15E9861CD37C2D95DB43D23CC6E6DE5C3F8FA6E8BE66F97CC518907 - let uri = createURI("http://whitelisted.com"); - let principal = gSecMan.getNoAppCodebasePrincipal(uri); - gDbService.lookup(principal, "goog-downloadwhite-digest256", - function handleEvent(aEvent) { - do_check_eq("goog-downloadwhite-digest256", aEvent); - run_next_test(); - }); -}); diff --git a/toolkit/components/url-classifier/tests/unit/test_hashcompleter.js b/toolkit/components/url-classifier/tests/unit/test_hashcompleter.js deleted file mode 100644 index 982aea5ea1..0000000000 --- a/toolkit/components/url-classifier/tests/unit/test_hashcompleter.js +++ /dev/null @@ -1,346 +0,0 @@ -/* Any copyright is dedicated to the Public Domain. - http://creativecommons.org/publicdomain/zero/1.0/ */ - -// This test ensures that the nsIUrlClassifierHashCompleter works as expected -// and simulates an HTTP server to provide completions. -// -// In order to test completions, each group of completions sent as one request -// to the HTTP server is called a completion set. There is currently not -// support for multiple requests being sent to the server at once, in this test. -// This tests makes a request for each element of |completionSets|, waits for -// a response and then moves to the next element. -// Each element of |completionSets| is an array of completions, and each -// completion is an object with the properties: -// hash: complete hash for the completion. Automatically right-padded -// to be COMPLETE_LENGTH. -// expectCompletion: boolean indicating whether the server should respond -// with a full hash. -// forceServerError: boolean indicating whether the server should respond -// with a 503. -// table: name of the table that the hash corresponds to. Only needs to be set -// if a completion is expected. -// chunkId: positive integer corresponding to the chunk that the hash belongs -// to. Only needs to be set if a completion is expected. -// multipleCompletions: boolean indicating whether the server should respond -// with more than one full hash. If this is set to true -// then |expectCompletion| must also be set to true and -// |hash| must have the same prefix as all |completions|. -// completions: an array of completions (objects with a hash, table and -// chunkId property as described above). This property is only -// used when |multipleCompletions| is set to true. - -// Basic prefixes with 2/3 completions. -let basicCompletionSet = [ - { - hash: "abcdefgh", - expectCompletion: true, - table: "test", - chunkId: 1234, - }, - { - hash: "1234", - expectCompletion: false, - }, - { - hash: "\u0000\u0000\u000012312", - expectCompletion: true, - table: "test", - chunkId: 1234, - } -]; - -// 3 prefixes with 0 completions to test HashCompleter handling a 204 status. -let falseCompletionSet = [ - { - hash: "1234", - expectCompletion: false, - }, - { - hash: "", - expectCompletion: false, - }, - { - hash: "abc", - expectCompletion: false, - } -]; - -// The current implementation (as of Mar 2011) sometimes sends duplicate -// entries to HashCompleter and even expects responses for duplicated entries. -let dupedCompletionSet = [ - { - hash: "1234", - expectCompletion: true, - table: "test", - chunkId: 1, - }, - { - hash: "5678", - expectCompletion: false, - table: "test2", - chunkId: 2, - }, - { - hash: "1234", - expectCompletion: true, - table: "test", - chunkId: 1, - }, - { - hash: "5678", - expectCompletion: false, - table: "test2", - chunkId: 2 - } -]; - -// It is possible for a hash completion request to return with multiple -// completions, the HashCompleter should return all of these. -let multipleResponsesCompletionSet = [ - { - hash: "1234", - expectCompletion: true, - multipleCompletions: true, - completions: [ - { - hash: "123456", - table: "test1", - chunkId: 3, - }, - { - hash: "123478", - table: "test2", - chunkId: 4, - } - ], - } -]; - -// The fifth completion set is added at runtime by getRandomCompletionSet. -// Each completion in the set only has one response and its purpose is to -// provide an easy way to test the HashCompleter handling an arbitrarily large -// completion set (determined by SIZE_OF_RANDOM_SET). -const SIZE_OF_RANDOM_SET = 16; -function getRandomCompletionSet(forceServerError) { - let completionSet = []; - let hashPrefixes = []; - - let seed = Math.floor(Math.random() * Math.pow(2, 32)); - dump("Using seed of " + seed + " for random completion set.\n"); - let rand = new LFSRgenerator(seed); - - for (let i = 0; i < SIZE_OF_RANDOM_SET; i++) { - let completion = { expectCompletion: false, forceServerError: false, _finished: false }; - - // Generate a random 256 bit hash. First we get a random number and then - // convert it to a string. - let hash; - let prefix; - do { - hash = ""; - let length = 1 + rand.nextNum(5); - for (let i = 0; i < length; i++) - hash += String.fromCharCode(rand.nextNum(8)); - prefix = hash.substring(0,4); - } while (hashPrefixes.indexOf(prefix) != -1); - - hashPrefixes.push(prefix); - completion.hash = hash; - - if (!forceServerError) { - completion.expectCompletion = rand.nextNum(1) == 1; - } else { - completion.forceServerError = true; - } - if (completion.expectCompletion) { - // Generate a random alpha-numeric string of length at most 6 for the - // table name. - completion.table = (rand.nextNum(31)).toString(36); - - completion.chunkId = rand.nextNum(16); - } - completionSet.push(completion); - } - - return completionSet; -} - -let completionSets = [basicCompletionSet, falseCompletionSet, - dupedCompletionSet, multipleResponsesCompletionSet]; -let currentCompletionSet = -1; -let finishedCompletions = 0; - -const SERVER_PORT = 8080; -const SERVER_PATH = "/hash-completer"; -let server; - -// Completion hashes are automatically right-padded with null chars to have a -// length of COMPLETE_LENGTH. -// Taken from nsUrlClassifierDBService.h -const COMPLETE_LENGTH = 32; - -let completer = Cc["@mozilla.org/url-classifier/hashcompleter;1"]. - getService(Ci.nsIUrlClassifierHashCompleter); - -let gethashUrl; - -// Expected highest completion set for which the server sends a response. -let expectedMaxServerCompletionSet = 0; -let maxServerCompletionSet = 0; - -function run_test() { - // Generate a random completion set that return successful responses. - completionSets.push(getRandomCompletionSet(false)); - // We backoff after receiving an error, so requests shouldn't reach the - // server after that. - expectedMaxServerCompletionSet = completionSets.length; - // Generate some completion sets that return 503s. - for (let j = 0; j < 10; ++j) { - completionSets.push(getRandomCompletionSet(true)); - } - - // Fix up the completions before running the test. - for each (let completionSet in completionSets) { - for each (let completion in completionSet) { - // Pad the right of each |hash| so that the length is COMPLETE_LENGTH. - if (completion.multipleCompletions) { - for each (let responseCompletion in completion.completions) { - let numChars = COMPLETE_LENGTH - responseCompletion.hash.length; - responseCompletion.hash += (new Array(numChars + 1)).join("\u0000"); - } - } - else { - let numChars = COMPLETE_LENGTH - completion.hash.length; - completion.hash += (new Array(numChars + 1)).join("\u0000"); - } - } - } - do_test_pending(); - - server = new HttpServer(); - server.registerPathHandler(SERVER_PATH, hashCompleterServer); - - server.start(-1); - const SERVER_PORT = server.identity.primaryPort; - - gethashUrl = "http://localhost:" + SERVER_PORT + SERVER_PATH; - - runNextCompletion(); -} - -function runNextCompletion() { - // The server relies on currentCompletionSet to send the correct response, so - // don't increment it until we start the new set of callbacks. - currentCompletionSet++; - if (currentCompletionSet >= completionSets.length) { - finish(); - return; - } - - dump("Now on completion set index " + currentCompletionSet + ", length " + - completionSets[currentCompletionSet].length + "\n"); - // Number of finished completions for this set. - finishedCompletions = 0; - for each (let completion in completionSets[currentCompletionSet]) { - completer.complete(completion.hash.substring(0,4), gethashUrl, - (new callback(completion))); - } -} - -function hashCompleterServer(aRequest, aResponse) { - let stream = aRequest.bodyInputStream; - let wrapperStream = Cc["@mozilla.org/binaryinputstream;1"]. - createInstance(Ci.nsIBinaryInputStream); - wrapperStream.setInputStream(stream); - - let len = stream.available(); - let data = wrapperStream.readBytes(len); - - // To avoid a response with duplicate hash completions, we keep track of all - // completed hash prefixes so far. - let completedHashes = []; - let responseText = ""; - - function responseForCompletion(x) { - return x.table + ":" + x.chunkId + ":" + x.hash.length + "\n" + x.hash; - } - // As per the spec, a server should response with a 204 if there are no - // full-length hashes that match the prefixes. - let httpStatus = 204; - for each (let completion in completionSets[currentCompletionSet]) { - if (completion.expectCompletion && - (completedHashes.indexOf(completion.hash) == -1)) { - completedHashes.push(completion.hash); - - if (completion.multipleCompletions) - responseText += completion.completions.map(responseForCompletion).join(""); - else - responseText += responseForCompletion(completion); - } - if (completion.forceServerError) { - httpStatus = 503; - } - } - - dump("Server sending response for " + currentCompletionSet + "\n"); - maxServerCompletionSet = currentCompletionSet; - if (responseText && httpStatus != 503) { - aResponse.write(responseText); - } else { - aResponse.setStatusLine(null, httpStatus, null); - } -} - - -function callback(completion) { - this._completion = completion; -} - -callback.prototype = { - completion: function completion(hash, table, chunkId, trusted) { - do_check_true(this._completion.expectCompletion); - if (this._completion.multipleCompletions) { - for each (let completion in this._completion.completions) { - if (completion.hash == hash) { - do_check_eq(JSON.stringify(hash), JSON.stringify(completion.hash)); - do_check_eq(table, completion.table); - do_check_eq(chunkId, completion.chunkId); - - completion._completed = true; - - if (this._completion.completions.every(function(x) x._completed)) - this._completed = true; - - break; - } - } - } - else { - // Hashes are not actually strings and can contain arbitrary data. - do_check_eq(JSON.stringify(hash), JSON.stringify(this._completion.hash)); - do_check_eq(table, this._completion.table); - do_check_eq(chunkId, this._completion.chunkId); - - this._completed = true; - } - }, - - completionFinished: function completionFinished(status) { - finishedCompletions++; - do_check_eq(!!this._completion.expectCompletion, !!this._completed); - this._completion._finished = true; - - // currentCompletionSet can mutate before all of the callbacks are complete. - if (currentCompletionSet < completionSets.length && - finishedCompletions == completionSets[currentCompletionSet].length) { - runNextCompletion(); - } - }, -}; - -function finish() { - do_check_eq(expectedMaxServerCompletionSet, maxServerCompletionSet); - server.stop(function() { - do_test_finished(); - }); -} diff --git a/toolkit/components/url-classifier/tests/unit/test_partial.js b/toolkit/components/url-classifier/tests/unit/test_partial.js deleted file mode 100644 index 83243fb4e7..0000000000 --- a/toolkit/components/url-classifier/tests/unit/test_partial.js +++ /dev/null @@ -1,825 +0,0 @@ - -/** - * DummyCompleter() lets tests easily specify the results of a partial - * hash completion request. - */ -function DummyCompleter() { - this.fragments = {}; - this.queries = []; - this.tableName = "test-phish-simple"; -} - -DummyCompleter.prototype = -{ -QueryInterface: function(iid) -{ - if (!iid.equals(Ci.nsISupports) && - !iid.equals(Ci.nsIUrlClassifierHashCompleter)) { - throw Cr.NS_ERROR_NO_INTERFACE; - } - return this; -}, - -complete: function(partialHash, gethashUrl, cb) -{ - this.queries.push(partialHash); - var fragments = this.fragments; - var self = this; - var doCallback = function() { - if (self.alwaysFail) { - cb.completionFinished(1); - return; - } - var results; - if (fragments[partialHash]) { - for (var i = 0; i < fragments[partialHash].length; i++) { - var chunkId = fragments[partialHash][i][0]; - var hash = fragments[partialHash][i][1]; - cb.completion(hash, self.tableName, chunkId); - } - } - cb.completionFinished(0); - } - var timer = new Timer(0, doCallback); -}, - -getHash: function(fragment) -{ - var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]. - createInstance(Ci.nsIScriptableUnicodeConverter); - converter.charset = "UTF-8"; - var data = converter.convertToByteArray(fragment); - var ch = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash); - ch.init(ch.SHA256); - ch.update(data, data.length); - var hash = ch.finish(false); - return hash.slice(0, 32); -}, - -addFragment: function(chunkId, fragment) -{ - this.addHash(chunkId, this.getHash(fragment)); -}, - -// This method allows the caller to generate complete hashes that match the -// prefix of a real fragment, but have different complete hashes. -addConflict: function(chunkId, fragment) -{ - var realHash = this.getHash(fragment); - var invalidHash = this.getHash("blah blah blah blah blah"); - this.addHash(chunkId, realHash.slice(0, 4) + invalidHash.slice(4, 32)); -}, - -addHash: function(chunkId, hash) -{ - var partial = hash.slice(0, 4); - if (this.fragments[partial]) { - this.fragments[partial].push([chunkId, hash]); - } else { - this.fragments[partial] = [[chunkId, hash]]; - } -}, - -compareQueries: function(fragments) -{ - var expectedQueries = []; - for (var i = 0; i < fragments.length; i++) { - expectedQueries.push(this.getHash(fragments[i]).slice(0, 4)); - } - do_check_eq(this.queries.length, expectedQueries.length); - expectedQueries.sort(); - this.queries.sort(); - for (var i = 0; i < this.queries.length; i++) { - do_check_eq(this.queries[i], expectedQueries[i]); - } -} -}; - -function setupCompleter(table, hits, conflicts) -{ - var completer = new DummyCompleter(); - completer.tableName = table; - for (var i = 0; i < hits.length; i++) { - var chunkId = hits[i][0]; - var fragments = hits[i][1]; - for (var j = 0; j < fragments.length; j++) { - completer.addFragment(chunkId, fragments[j]); - } - } - for (var i = 0; i < conflicts.length; i++) { - var chunkId = conflicts[i][0]; - var fragments = conflicts[i][1]; - for (var j = 0; j < fragments.length; j++) { - completer.addConflict(chunkId, fragments[j]); - } - } - - dbservice.setHashCompleter(table, completer); - - return completer; -} - -function installCompleter(table, fragments, conflictFragments) -{ - return setupCompleter(table, fragments, conflictFragments); -} - -function installFailingCompleter(table) { - var completer = setupCompleter(table, [], []); - completer.alwaysFail = true; - return completer; -} - -// Helper assertion for checking dummy completer queries -gAssertions.completerQueried = function(data, cb) -{ - var completer = data[0]; - completer.compareQueries(data[1]); - cb(); -} - -function doTest(updates, assertions) -{ - doUpdateTest(updates, assertions, runNextTest, updateError); -} - -// Test an add of two partial urls to a fresh database -function testPartialAdds() { - var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ]; - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls - }], - 4); - - - var completer = installCompleter('test-phish-simple', [[1, addUrls]], []); - - var assertions = { - "tableData" : "test-phish-simple;a:1", - "urlsExist" : addUrls, - "completerQueried" : [completer, addUrls] - }; - - - doTest([update], assertions); -} - -function testPartialAddsWithConflicts() { - var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ]; - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls - }], - 4); - - // Each result will have both a real match and a conflict - var completer = installCompleter('test-phish-simple', - [[1, addUrls]], - [[1, addUrls]]); - - var assertions = { - "tableData" : "test-phish-simple;a:1", - "urlsExist" : addUrls, - "completerQueried" : [completer, addUrls] - }; - - doTest([update], assertions); -} - -// Test whether the fragmenting code does not cause duplicated completions -function testFragments() { - var addUrls = [ "foo.com/a/b/c", "foo.net/", "foo.com/c/" ]; - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls - }], - 4); - - - var completer = installCompleter('test-phish-simple', [[1, addUrls]], []); - - var assertions = { - "tableData" : "test-phish-simple;a:1", - "urlsExist" : addUrls, - "completerQueried" : [completer, addUrls] - }; - - - doTest([update], assertions); -} - -// Test http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec -// section 6.2 example 1 -function testSpecFragments() { - var probeUrls = [ "a.b.c/1/2.html?param=1" ]; - - var addUrls = [ "a.b.c/1/2.html", - "a.b.c/", - "a.b.c/1/", - "b.c/1/2.html?param=1", - "b.c/1/2.html", - "b.c/", - "b.c/1/", - "a.b.c/1/2.html?param=1" ]; - - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls - }], - 4); - - - var completer = installCompleter('test-phish-simple', [[1, addUrls]], []); - - var assertions = { - "tableData" : "test-phish-simple;a:1", - "urlsExist" : probeUrls, - "completerQueried" : [completer, addUrls] - }; - - doTest([update], assertions); - -} - -// Test http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec -// section 6.2 example 2 -function testMoreSpecFragments() { - var probeUrls = [ "a.b.c.d.e.f.g/1.html" ]; - - var addUrls = [ "a.b.c.d.e.f.g/1.html", - "a.b.c.d.e.f.g/", - "c.d.e.f.g/1.html", - "c.d.e.f.g/", - "d.e.f.g/1.html", - "d.e.f.g/", - "e.f.g/1.html", - "e.f.g/", - "f.g/1.html", - "f.g/" ]; - - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls - }], - 4); - - var completer = installCompleter('test-phish-simple', [[1, addUrls]], []); - - var assertions = { - "tableData" : "test-phish-simple;a:1", - "urlsExist" : probeUrls, - "completerQueried" : [completer, addUrls] - }; - - doTest([update], assertions); - -} - -function testFalsePositives() { - var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ]; - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls - }], - 4); - - // Each result will have no matching complete hashes and a non-matching - // conflict - var completer = installCompleter('test-phish-simple', [], [[1, addUrls]]); - - var assertions = { - "tableData" : "test-phish-simple;a:1", - "urlsDontExist" : addUrls, - "completerQueried" : [completer, addUrls] - }; - - doTest([update], assertions); -} - -function testEmptyCompleter() { - var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ]; - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls - }], - 4); - - // Completer will never return full hashes - var completer = installCompleter('test-phish-simple', [], []); - - var assertions = { - "tableData" : "test-phish-simple;a:1", - "urlsDontExist" : addUrls, - "completerQueried" : [completer, addUrls] - }; - - doTest([update], assertions); -} - -function testCompleterFailure() { - var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ]; - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls - }], - 4); - - // Completer will never return full hashes - var completer = installFailingCompleter('test-phish-simple'); - - var assertions = { - "tableData" : "test-phish-simple;a:1", - "urlsDontExist" : addUrls, - "completerQueried" : [completer, addUrls] - }; - - doTest([update], assertions); -} - -function testMixedSizesSameDomain() { - var add1Urls = [ "foo.com/a" ]; - var add2Urls = [ "foo.com/b" ]; - - var update1 = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : add1Urls }], - 4); - var update2 = buildPhishingUpdate( - [ - { "chunkNum" : 2, - "urls" : add2Urls }], - 32); - - // We should only need to complete the partial hashes - var completer = installCompleter('test-phish-simple', [[1, add1Urls]], []); - - var assertions = { - "tableData" : "test-phish-simple;a:1-2", - // both urls should match... - "urlsExist" : add1Urls.concat(add2Urls), - // ... but the completer should only be queried for the partial entry - "completerQueried" : [completer, add1Urls] - }; - - doTest([update1, update2], assertions); -} - -function testMixedSizesDifferentDomains() { - var add1Urls = [ "foo.com/a" ]; - var add2Urls = [ "bar.com/b" ]; - - var update1 = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : add1Urls }], - 4); - var update2 = buildPhishingUpdate( - [ - { "chunkNum" : 2, - "urls" : add2Urls }], - 32); - - // We should only need to complete the partial hashes - var completer = installCompleter('test-phish-simple', [[1, add1Urls]], []); - - var assertions = { - "tableData" : "test-phish-simple;a:1-2", - // both urls should match... - "urlsExist" : add1Urls.concat(add2Urls), - // ... but the completer should only be queried for the partial entry - "completerQueried" : [completer, add1Urls] - }; - - doTest([update1, update2], assertions); -} - -function testInvalidHashSize() -{ - var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ]; - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls - }], - 12); // only 4 and 32 are legal hash sizes - - var addUrls2 = [ "zaz.com/a", "xyz.com/b" ]; - var update2 = buildPhishingUpdate( - [ - { "chunkNum" : 2, - "urls" : addUrls2 - }], - 4); - - var completer = installCompleter('test-phish-simple', [[1, addUrls]], []); - - var assertions = { - "tableData" : "test-phish-simple;a:2", - "urlsDontExist" : addUrls - }; - - // A successful update will trigger an error - doUpdateTest([update2, update], assertions, updateError, runNextTest); -} - -function testWrongTable() -{ - var addUrls = [ "foo.com/a" ]; - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls - }], - 4); - var completer = installCompleter('test-malware-simple', // wrong table - [[1, addUrls]], []); - - // The above installCompleter installs the completer for test-malware-simple, - // we want it to be used for test-phish-simple too. - dbservice.setHashCompleter("test-phish-simple", completer); - - - var assertions = { - "tableData" : "test-phish-simple;a:1", - // The urls were added as phishing urls, but the completer is claiming - // that they are malware urls, and we trust the completer in this case. - // The result will be discarded, so we can only check for non-existence. - "urlsDontExist" : addUrls, - // Make sure the completer was actually queried. - "completerQueried" : [completer, addUrls] - }; - - doUpdateTest([update], assertions, - function() { - // Give the dbservice a chance to (not) cache the result. - var timer = new Timer(3000, function() { - // The miss earlier will have caused a miss to be cached. - // Resetting the completer does not count as an update, - // so we will not be probed again. - var newCompleter = installCompleter('test-malware-simple', [[1, addUrls]], []); dbservice.setHashCompleter("test-phish-simple", - newCompleter); - - var assertions = { - "urlsDontExist" : addUrls - }; - checkAssertions(assertions, runNextTest); - }); - }, updateError); -} - -function setupCachedResults(addUrls, part2) -{ - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls - }], - 4); - - var completer = installCompleter('test-phish-simple', [[1, addUrls]], []); - - var assertions = { - "tableData" : "test-phish-simple;a:1", - // Request the add url. This should cause the completion to be cached. - "urlsExist" : addUrls, - // Make sure the completer was actually queried. - "completerQueried" : [completer, addUrls] - }; - - doUpdateTest([update], assertions, - function() { - // Give the dbservice a chance to cache the result. - var timer = new Timer(3000, part2); - }, updateError); -} - -function testCachedResults() -{ - setupCachedResults(["foo.com/a"], function(add) { - // This is called after setupCachedResults(). Verify that - // checking the url again does not cause a completer request. - - // install a new completer, this one should never be queried. - var newCompleter = installCompleter('test-phish-simple', [[1, []]], []); - - var assertions = { - "urlsExist" : ["foo.com/a"], - "completerQueried" : [newCompleter, []] - }; - checkAssertions(assertions, runNextTest); - }); -} - -function testCachedResultsWithSub() { - setupCachedResults(["foo.com/a"], function() { - // install a new completer, this one should never be queried. - var newCompleter = installCompleter('test-phish-simple', [[1, []]], []); - - var removeUpdate = buildPhishingUpdate( - [ { "chunkNum" : 2, - "chunkType" : "s", - "urls": ["1:foo.com/a"] }], - 4); - - var assertions = { - "urlsDontExist" : ["foo.com/a"], - "completerQueried" : [newCompleter, []] - } - - doTest([removeUpdate], assertions); - }); -} - -function testCachedResultsWithExpire() { - setupCachedResults(["foo.com/a"], function() { - // install a new completer, this one should never be queried. - var newCompleter = installCompleter('test-phish-simple', [[1, []]], []); - - var expireUpdate = - "n:1000\n" + - "i:test-phish-simple\n" + - "ad:1\n"; - - var assertions = { - "urlsDontExist" : ["foo.com/a"], - "completerQueried" : [newCompleter, []] - } - doTest([expireUpdate], assertions); - }); -} - -function testCachedResultsUpdate() -{ - var existUrls = ["foo.com/a"]; - setupCachedResults(existUrls, function() { - // This is called after setupCachedResults(). Verify that - // checking the url again does not cause a completer request. - - // install a new completer, this one should never be queried. - var newCompleter = installCompleter('test-phish-simple', [[1, []]], []); - - var assertions = { - "urlsExist" : existUrls, - "completerQueried" : [newCompleter, []] - }; - - var addUrls = ["foobar.org/a"]; - - var update2 = buildPhishingUpdate( - [ - { "chunkNum" : 2, - "urls" : addUrls - }], - 4); - - checkAssertions(assertions, function () { - // Apply the update. The cached completes should be gone. - doStreamUpdate(update2, function() { - // Now the completer gets queried again. - var newCompleter2 = installCompleter('test-phish-simple', [[1, existUrls]], []); - var assertions2 = { - "tableData" : "test-phish-simple;a:1-2", - "urlsExist" : existUrls, - "completerQueried" : [newCompleter2, existUrls] - }; - checkAssertions(assertions2, runNextTest); - }, updateError); - }); - }); -} - -function testCachedResultsFailure() -{ - var existUrls = ["foo.com/a"]; - setupCachedResults(existUrls, function() { - // This is called after setupCachedResults(). Verify that - // checking the url again does not cause a completer request. - - // install a new completer, this one should never be queried. - var newCompleter = installCompleter('test-phish-simple', [[1, []]], []); - - var assertions = { - "urlsExist" : existUrls, - "completerQueried" : [newCompleter, []] - }; - - var addUrls = ["foobar.org/a"]; - - var update2 = buildPhishingUpdate( - [ - { "chunkNum" : 2, - "urls" : addUrls - }], - 4); - - checkAssertions(assertions, function() { - // Apply the update. The cached completes should be gone. - doErrorUpdate("test-phish-simple,test-malware-simple", function() { - // Now the completer gets queried again. - var newCompleter2 = installCompleter('test-phish-simple', [[1, existUrls]], []); - var assertions2 = { - "tableData" : "test-phish-simple;a:1", - "urlsExist" : existUrls, - "completerQueried" : [newCompleter2, existUrls] - }; - checkAssertions(assertions2, runNextTest); - }, updateError); - }); - }); -} - -function testErrorList() -{ - var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ]; - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls - }], - 4); - // The update failure should will kill the completes, so the above - // must be a prefix to get any hit at all past the update failure. - - var completer = installCompleter('test-phish-simple', [[1, addUrls]], []); - - var assertions = { - "tableData" : "test-phish-simple;a:1", - "urlsExist" : addUrls, - // These are complete urls, and will only be completed if the - // list is stale. - "completerQueried" : [completer, addUrls] - }; - - // Apply the update. - doStreamUpdate(update, function() { - // Now the test-phish-simple and test-malware-simple tables are marked - // as fresh. Fake an update failure to mark them stale. - doErrorUpdate("test-phish-simple,test-malware-simple", function() { - // Now the lists should be marked stale. Check assertions. - checkAssertions(assertions, runNextTest); - }, updateError); - }, updateError); -} - - -function testStaleList() -{ - var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ]; - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls - }], - 32); - - var completer = installCompleter('test-phish-simple', [[1, addUrls]], []); - - var assertions = { - "tableData" : "test-phish-simple;a:1", - "urlsExist" : addUrls, - // These are complete urls, and will only be completed if the - // list is stale. - "completerQueried" : [completer, addUrls] - }; - - // Consider a match stale after one second. - prefBranch.setIntPref("urlclassifier.max-complete-age", 1); - - // Apply the update. - doStreamUpdate(update, function() { - // Now the test-phish-simple and test-malware-simple tables are marked - // as fresh. Wait three seconds to make sure the list is marked stale. - new Timer(3000, function() { - // Now the lists should be marked stale. Check assertions. - checkAssertions(assertions, function() { - prefBranch.setIntPref("urlclassifier.max-complete-age", 2700); - runNextTest(); - }); - }, updateError); - }, updateError); -} - -// Same as testStaleList, but verifies that an empty response still -// unconfirms the entry. -function testStaleListEmpty() -{ - var addUrls = [ "foo.com/a", "foo.com/b", "bar.com/c" ]; - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls - }], - 32); - - var completer = installCompleter('test-phish-simple', [], []); - - var assertions = { - "tableData" : "test-phish-simple;a:1", - // None of these should match, because they won't be completed - "urlsDontExist" : addUrls, - // These are complete urls, and will only be completed if the - // list is stale. - "completerQueried" : [completer, addUrls] - }; - - // Consider a match stale after one second. - prefBranch.setIntPref("urlclassifier.max-complete-age", 1); - - // Apply the update. - doStreamUpdate(update, function() { - // Now the test-phish-simple and test-malware-simple tables are marked - // as fresh. Wait three seconds to make sure the list is marked stale. - new Timer(3000, function() { - // Now the lists should be marked stale. Check assertions. - checkAssertions(assertions, function() { - prefBranch.setIntPref("urlclassifier.max-complete-age", 2700); - runNextTest(); - }); - }, updateError); - }, updateError); -} - - -// Verify that different lists (test-phish-simple, -// test-malware-simple) maintain their freshness separately. -function testErrorListIndependent() -{ - var phishUrls = [ "phish.com/a" ]; - var malwareUrls = [ "attack.com/a" ]; - var update = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : phishUrls - }], - 4); - // These have to persist past the update failure, so they must be prefixes, - // not completes. - - update += buildMalwareUpdate( - [ - { "chunkNum" : 2, - "urls" : malwareUrls - }], - 32); - - var completer = installCompleter('test-phish-simple', [[1, phishUrls]], []); - - var assertions = { - "tableData" : "test-malware-simple;a:2\ntest-phish-simple;a:1", - "urlsExist" : phishUrls, - "malwareUrlsExist" : malwareUrls, - // Only this phishing urls should be completed, because only the phishing - // urls will be stale. - "completerQueried" : [completer, phishUrls] - }; - - // Apply the update. - doStreamUpdate(update, function() { - // Now the test-phish-simple and test-malware-simple tables are - // marked as fresh. Fake an update failure to mark *just* - // phishing data as stale. - doErrorUpdate("test-phish-simple", function() { - // Now the lists should be marked stale. Check assertions. - checkAssertions(assertions, runNextTest); - }, updateError); - }, updateError); -} - -function run_test() -{ - runTests([ - testPartialAdds, - testPartialAddsWithConflicts, - testFragments, - testSpecFragments, - testMoreSpecFragments, - testFalsePositives, - testEmptyCompleter, - testCompleterFailure, - testMixedSizesSameDomain, - testMixedSizesDifferentDomains, - testInvalidHashSize, - testWrongTable, - testCachedResults, - testCachedResultsWithSub, - testCachedResultsWithExpire, - testCachedResultsUpdate, - testCachedResultsFailure, - testStaleList, - testStaleListEmpty, - testErrorList, - testErrorListIndependent - ]); -} - -do_test_pending(); diff --git a/toolkit/components/url-classifier/tests/unit/test_prefixset.js b/toolkit/components/url-classifier/tests/unit/test_prefixset.js deleted file mode 100644 index ca2c8a5058..0000000000 --- a/toolkit/components/url-classifier/tests/unit/test_prefixset.js +++ /dev/null @@ -1,232 +0,0 @@ -// newPset: returns an empty nsIUrlClassifierPrefixSet. -function newPset() { - let pset = Cc["@mozilla.org/url-classifier/prefixset;1"] - .createInstance(Ci.nsIUrlClassifierPrefixSet); - pset.init("all"); - return pset; -} - -// arrContains: returns true if |arr| contains the element |target|. Uses binary -// search and requires |arr| to be sorted. -function arrContains(arr, target) { - let start = 0; - let end = arr.length - 1; - let i = 0; - - while (end > start) { - i = start + (end - start >> 1); - let value = arr[i]; - - if (value < target) - start = i+1; - else if (value > target) - end = i-1; - else - break; - } - if (start == end) - i = start; - - return (!(i < 0 || i >= arr.length) && arr[i] == target); -} - -// checkContents: Check whether the PrefixSet pset contains -// the prefixes in the passed array. -function checkContents(pset, prefixes) { - var outcount = {}, outset = {}; - outset = pset.getPrefixes(outcount); - let inset = prefixes; - do_check_eq(inset.length, outset.length); - inset.sort(function(x,y) x - y); - for (let i = 0; i < inset.length; i++) { - do_check_eq(inset[i], outset[i]); - } -} - -function wrappedProbe(pset, prefix) { - return pset.contains(prefix); -}; - -// doRandomLookups: we use this to test for false membership with random input -// over the range of prefixes (unsigned 32-bits integers). -// pset: a nsIUrlClassifierPrefixSet to test. -// prefixes: an array of prefixes supposed to make up the prefix set. -// N: number of random lookups to make. -function doRandomLookups(pset, prefixes, N) { - for (let i = 0; i < N; i++) { - let randInt = prefixes[0]; - while (arrContains(prefixes, randInt)) - randInt = Math.floor(Math.random() * Math.pow(2, 32)); - - do_check_false(wrappedProbe(pset, randInt)); - } -} - -// doExpectedLookups: we use this to test expected membership. -// pset: a nsIUrlClassifierPrefixSet to test. -// prefixes: -function doExpectedLookups(pset, prefixes, N) { - for (let i = 0; i < N; i++) { - prefixes.forEach(function (x) { - dump("Checking " + x + "\n"); - do_check_true(wrappedProbe(pset, x)); - }); - } -} - -// testBasicPset: A very basic test of the prefix set to make sure that it -// exists and to give a basic example of its use. -function testBasicPset() { - let pset = Cc["@mozilla.org/url-classifier/prefixset;1"] - .createInstance(Ci.nsIUrlClassifierPrefixSet); - let prefixes = [2,50,100,2000,78000,1593203]; - pset.setPrefixes(prefixes, prefixes.length); - - do_check_true(wrappedProbe(pset, 100)); - do_check_false(wrappedProbe(pset, 100000)); - do_check_true(wrappedProbe(pset, 1593203)); - do_check_false(wrappedProbe(pset, 999)); - do_check_false(wrappedProbe(pset, 0)); - - - checkContents(pset, prefixes); -} - -function testDuplicates() { - let pset = Cc["@mozilla.org/url-classifier/prefixset;1"] - .createInstance(Ci.nsIUrlClassifierPrefixSet); - let prefixes = [1,1,2,2,2,3,3,3,3,3,3,5,6,6,7,7,9,9,9]; - pset.setPrefixes(prefixes, prefixes.length); - - do_check_true(wrappedProbe(pset, 1)); - do_check_true(wrappedProbe(pset, 2)); - do_check_true(wrappedProbe(pset, 5)); - do_check_true(wrappedProbe(pset, 9)); - do_check_false(wrappedProbe(pset, 4)); - do_check_false(wrappedProbe(pset, 8)); - - - checkContents(pset, prefixes); -} - -function testSimplePset() { - let pset = newPset(); - let prefixes = [1,2,100,400,123456789]; - pset.setPrefixes(prefixes, prefixes.length); - - doRandomLookups(pset, prefixes, 100); - doExpectedLookups(pset, prefixes, 1); - - - checkContents(pset, prefixes); -} - -function testReSetPrefixes() { - let pset = newPset(); - let prefixes = [1, 5, 100, 1000, 150000]; - pset.setPrefixes(prefixes, prefixes.length); - - doExpectedLookups(pset, prefixes, 1); - - let secondPrefixes = [12, 50, 300, 2000, 5000, 200000]; - pset.setPrefixes(secondPrefixes, secondPrefixes.length); - - doExpectedLookups(pset, secondPrefixes, 1); - for (let i = 0; i < prefixes.length; i++) { - do_check_false(wrappedProbe(pset, prefixes[i])); - } - - - checkContents(pset, secondPrefixes); -} - -function testLoadSaveLargeSet() { - let N = 1000; - let arr = []; - - for (let i = 0; i < N; i++) { - let randInt = Math.floor(Math.random() * Math.pow(2, 32)); - arr.push(randInt); - } - - arr.sort(function(x,y) x - y); - - let pset = newPset(); - pset.setPrefixes(arr, arr.length); - - doExpectedLookups(pset, arr, 1); - doRandomLookups(pset, arr, 1000); - - checkContents(pset, arr); - - // Now try to save, restore, and redo the lookups - var file = dirSvc.get('ProfLD', Ci.nsIFile); - file.append("testLarge.pset"); - - pset.storeToFile(file); - - let psetLoaded = newPset(); - psetLoaded.loadFromFile(file); - - doExpectedLookups(psetLoaded, arr, 1); - doRandomLookups(psetLoaded, arr, 1000); - - checkContents(psetLoaded, arr); -} - -function testTinySet() { - let pset = Cc["@mozilla.org/url-classifier/prefixset;1"] - .createInstance(Ci.nsIUrlClassifierPrefixSet); - let prefixes = [1]; - pset.setPrefixes(prefixes, prefixes.length); - - do_check_true(wrappedProbe(pset, 1)); - do_check_false(wrappedProbe(pset, 100000)); - checkContents(pset, prefixes); - - prefixes = []; - pset.setPrefixes(prefixes, prefixes.length); - do_check_false(wrappedProbe(pset, 1)); - checkContents(pset, prefixes); -} - -function testLoadSaveNoDelta() { - let N = 100; - let arr = []; - - for (let i = 0; i < N; i++) { - // construct a tree without deltas by making the distance - // between entries larger than 16 bits - arr.push(((1 << 16) + 1) * i); - } - - let pset = newPset(); - pset.setPrefixes(arr, arr.length); - - doExpectedLookups(pset, arr, 1); - - var file = dirSvc.get('ProfLD', Ci.nsIFile); - file.append("testNoDelta.pset"); - - pset.storeToFile(file); - pset.loadFromFile(file); - - doExpectedLookups(pset, arr, 1); -} - -let tests = [testBasicPset, - testSimplePset, - testReSetPrefixes, - testLoadSaveLargeSet, - testDuplicates, - testTinySet, - testLoadSaveNoDelta]; - -function run_test() { - // None of the tests use |executeSoon| or any sort of callbacks, so we can - // just run them in succession. - for (let i = 0; i < tests.length; i++) { - dump("Running " + tests[i].name + "\n"); - tests[i](); - } -} diff --git a/toolkit/components/url-classifier/tests/unit/test_streamupdater.js b/toolkit/components/url-classifier/tests/unit/test_streamupdater.js deleted file mode 100644 index 9ba73a17e0..0000000000 --- a/toolkit/components/url-classifier/tests/unit/test_streamupdater.js +++ /dev/null @@ -1,220 +0,0 @@ -function doTest(updates, assertions, expectError) -{ - if (expectError) { - doUpdateTest(updates, assertions, updateError, runNextTest); - } else { - doUpdateTest(updates, assertions, runNextTest, updateError); - } -} - -// Never use the same URLs for multiple tests, because we aren't guaranteed -// to reset the database between tests. -function testFillDb() { - var add1Urls = [ "zaz.com/a", "yxz.com/c" ]; - - var update = "n:1000\n"; - update += "i:test-phish-simple\n"; - - var update1 = buildBareUpdate( - [{ "chunkNum" : 1, - "urls" : add1Urls }]); - update += "u:data:," + encodeURIComponent(update1) + "\n"; - - var assertions = { - "tableData" : "test-phish-simple;a:1", - "urlsExist" : add1Urls - }; - - doTest([update], assertions, false); -} - -function testSimpleForward() { - var add1Urls = [ "foo-simple.com/a", "bar-simple.com/c" ]; - var add2Urls = [ "foo-simple.com/b" ]; - var add3Urls = [ "bar-simple.com/d" ]; - - var update = "n:1000\n"; - update += "i:test-phish-simple\n"; - - var update1 = buildBareUpdate( - [{ "chunkNum" : 1, - "urls" : add1Urls }]); - update += "u:data:," + encodeURIComponent(update1) + "\n"; - - var update2 = buildBareUpdate( - [{ "chunkNum" : 2, - "urls" : add2Urls }]); - update += "u:data:," + encodeURIComponent(update2) + "\n"; - - var update3 = buildBareUpdate( - [{ "chunkNum" : 3, - "urls" : add3Urls }]); - update += "u:data:," + encodeURIComponent(update3) + "\n"; - - var assertions = { - "tableData" : "test-phish-simple;a:1-3", - "urlsExist" : add1Urls.concat(add2Urls).concat(add3Urls) - }; - - doTest([update], assertions, false); -} - -// Make sure that a nested forward (a forward within a forward) causes -// the update to fail. -function testNestedForward() { - var add1Urls = [ "foo-nested.com/a", "bar-nested.com/c" ]; - var add2Urls = [ "foo-nested.com/b" ]; - - var update = "n:1000\n"; - update += "i:test-phish-simple\n"; - - var update1 = buildBareUpdate( - [{ "chunkNum" : 1, - "urls" : add1Urls }]); - update += "u:data:," + encodeURIComponent(update1) + "\n"; - - var update2 = buildBareUpdate( - [{ "chunkNum" : 2 }]); - var update3 = buildBareUpdate( - [{ "chunkNum" : 3, - "urls" : add1Urls }]); - - update2 += "u:data:," + encodeURIComponent(update3) + "\n"; - - update += "u:data:," + encodeURIComponent(update2) + "\n"; - - var assertions = { - "tableData" : "", - "urlsDontExist" : add1Urls.concat(add2Urls) - }; - - doTest([update], assertions, true); -} - -// An invalid URL forward causes the update to fail. -function testInvalidUrlForward() { - var add1Urls = [ "foo-invalid.com/a", "bar-invalid.com/c" ]; - - var update = buildPhishingUpdate( - [{ "chunkNum" : 1, - "urls" : add1Urls }]); - update += "u:asdf://blah/blah\n"; // invalid URL scheme - - // add1Urls is present, but that is an artifact of the way we do the test. - var assertions = { - "tableData" : "", - "urlsExist" : add1Urls - }; - - doTest([update], assertions, true); -} - -// A failed network request causes the update to fail. -function testErrorUrlForward() { - var add1Urls = [ "foo-forward.com/a", "bar-forward.com/c" ]; - - var update = buildPhishingUpdate( - [{ "chunkNum" : 1, - "urls" : add1Urls }]); - update += "u:http://test.invalid/asdf/asdf\n"; // invalid URL scheme - - // add1Urls is present, but that is an artifact of the way we do the test. - var assertions = { - "tableData" : "", - "urlsExist" : add1Urls - }; - - doTest([update], assertions, true); -} - -function testMultipleTables() { - var add1Urls = [ "foo-multiple.com/a", "bar-multiple.com/c" ]; - var add2Urls = [ "foo-multiple.com/b" ]; - var add3Urls = [ "bar-multiple.com/d" ]; - - var update = "n:1000\n"; - update += "i:test-phish-simple\n"; - - var update1 = buildBareUpdate( - [{ "chunkNum" : 1, - "urls" : add1Urls }]); - update += "u:data:," + encodeURIComponent(update1) + "\n"; - - var update2 = buildBareUpdate( - [{ "chunkNum" : 2, - "urls" : add2Urls }]); - update += "u:data:," + encodeURIComponent(update2) + "\n"; - - update += "i:test-malware-simple\n"; - - var update3 = buildBareUpdate( - [{ "chunkNum" : 3, - "urls" : add3Urls }]); - update += "u:data:," + encodeURIComponent(update3) + "\n"; - - var assertions = { - "tableData" : "test-malware-simple;a:3\ntest-phish-simple;a:1-2", - "urlsExist" : add1Urls.concat(add2Urls), - "malwareUrlsExist" : add3Urls - }; - - doTest([update], assertions, false); -} - -function Observer(callback) { - this.observe = callback; -} - -Observer.prototype = -{ -QueryInterface: function(iid) -{ - if (!iid.equals(Ci.nsISupports) && - !iid.equals(Ci.nsIObserver)) { - throw Cr.NS_ERROR_NO_INTERFACE; - } - return this; -} -}; - -// Tests a database reset request. -function testReset() { - var addUrls1 = [ "foo-reset.com/a", "foo-reset.com/b" ]; - var update1 = buildPhishingUpdate( - [ - { "chunkNum" : 1, - "urls" : addUrls1 - }]); - - var update2 = "n:1000\nr:pleasereset\n"; - - var addUrls3 = [ "bar-reset.com/a", "bar-reset.com/b" ]; - var update3 = buildPhishingUpdate( - [ - { "chunkNum" : 3, - "urls" : addUrls3 - }]); - - var assertions = { - "tableData" : "test-phish-simple;a:3", - "urlsExist" : addUrls3, - "urlsDontExist" : addUrls1 - }; - - doTest([update1, update2, update3], assertions, false); -} - - -function run_test() -{ - runTests([ - testSimpleForward, - testNestedForward, - testInvalidUrlForward, - testErrorUrlForward, - testMultipleTables, - testReset - ]); -} - -do_test_pending(); diff --git a/toolkit/components/url-classifier/tests/unit/xpcshell.ini b/toolkit/components/url-classifier/tests/unit/xpcshell.ini deleted file mode 100644 index 1c923b277a..0000000000 --- a/toolkit/components/url-classifier/tests/unit/xpcshell.ini +++ /dev/null @@ -1,18 +0,0 @@ -[DEFAULT] -head = head_urlclassifier.js -tail = tail_urlclassifier.js -skip-if = toolkit == 'android' || toolkit == 'gonk' -support-files = - data/digest1.chunk - data/digest2.chunk - -[test_addsub.js] -[test_backoff.js] -[test_dbservice.js] -[test_hashcompleter.js] -# Bug 752243: Profile cleanup frequently fails -#skip-if = os == "mac" || os == "linux" -[test_partial.js] -[test_prefixset.js] -[test_streamupdater.js] -[test_digest256.js] diff --git a/toolkit/components/url-classifier/tests/unittests.xul b/toolkit/components/url-classifier/tests/unittests.xul deleted file mode 100644 index 0c9ce898bc..0000000000 --- a/toolkit/components/url-classifier/tests/unittests.xul +++ /dev/null @@ -1,188 +0,0 @@ - - - - - diff --git a/xulrunner/confvars.sh b/xulrunner/confvars.sh index ce3feae6fa..a115bda166 100755 --- a/xulrunner/confvars.sh +++ b/xulrunner/confvars.sh @@ -11,7 +11,6 @@ MOZ_CHROME_FILE_FORMAT=omni MOZ_APP_VERSION=$MOZILLA_VERSION MOZ_PLACES=1 MOZ_EXTENSIONS_DEFAULT=" gio" -MOZ_URL_CLASSIFIER=1 MOZ_SERVICES_COMMON=1 MOZ_SERVICES_CRYPTO=1 MOZ_SERVICES_METRICS=1