Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsPlacesTables.h" |
michael@0 | 8 | |
michael@0 | 9 | #ifndef __nsPlacesTriggers_h__ |
michael@0 | 10 | #define __nsPlacesTriggers_h__ |
michael@0 | 11 | |
michael@0 | 12 | /** |
michael@0 | 13 | * Exclude these visit types: |
michael@0 | 14 | * 0 - invalid |
michael@0 | 15 | * 4 - EMBED |
michael@0 | 16 | * 7 - DOWNLOAD |
michael@0 | 17 | * 7 - FRAMED_LINK |
michael@0 | 18 | **/ |
michael@0 | 19 | #define EXCLUDED_VISIT_TYPES "0, 4, 7, 8" |
michael@0 | 20 | |
michael@0 | 21 | /** |
michael@0 | 22 | * This triggers update visit_count and last_visit_date based on historyvisits |
michael@0 | 23 | * table changes. |
michael@0 | 24 | */ |
michael@0 | 25 | #define CREATE_HISTORYVISITS_AFTERINSERT_TRIGGER NS_LITERAL_CSTRING( \ |
michael@0 | 26 | "CREATE TEMP TRIGGER moz_historyvisits_afterinsert_v2_trigger " \ |
michael@0 | 27 | "AFTER INSERT ON moz_historyvisits FOR EACH ROW " \ |
michael@0 | 28 | "BEGIN " \ |
michael@0 | 29 | "UPDATE moz_places SET " \ |
michael@0 | 30 | "visit_count = visit_count + (SELECT NEW.visit_type NOT IN (" EXCLUDED_VISIT_TYPES ")), "\ |
michael@0 | 31 | "last_visit_date = MAX(IFNULL(last_visit_date, 0), NEW.visit_date) " \ |
michael@0 | 32 | "WHERE id = NEW.place_id;" \ |
michael@0 | 33 | "END" \ |
michael@0 | 34 | ) |
michael@0 | 35 | |
michael@0 | 36 | #define CREATE_HISTORYVISITS_AFTERDELETE_TRIGGER NS_LITERAL_CSTRING( \ |
michael@0 | 37 | "CREATE TEMP TRIGGER moz_historyvisits_afterdelete_v2_trigger " \ |
michael@0 | 38 | "AFTER DELETE ON moz_historyvisits FOR EACH ROW " \ |
michael@0 | 39 | "BEGIN " \ |
michael@0 | 40 | "UPDATE moz_places SET " \ |
michael@0 | 41 | "visit_count = visit_count - (SELECT OLD.visit_type NOT IN (" EXCLUDED_VISIT_TYPES ")), "\ |
michael@0 | 42 | "last_visit_date = (SELECT visit_date FROM moz_historyvisits " \ |
michael@0 | 43 | "WHERE place_id = OLD.place_id " \ |
michael@0 | 44 | "ORDER BY visit_date DESC LIMIT 1) " \ |
michael@0 | 45 | "WHERE id = OLD.place_id;" \ |
michael@0 | 46 | "END" \ |
michael@0 | 47 | ) |
michael@0 | 48 | |
michael@0 | 49 | /** |
michael@0 | 50 | * A predicate matching pages on rev_host, based on a given host value. |
michael@0 | 51 | * 'host' may be either the moz_hosts.host column or an alias representing an |
michael@0 | 52 | * equivalent value. |
michael@0 | 53 | */ |
michael@0 | 54 | #define HOST_TO_REVHOST_PREDICATE \ |
michael@0 | 55 | "rev_host = get_unreversed_host(host || '.') || '.' " \ |
michael@0 | 56 | "OR rev_host = get_unreversed_host(host || '.') || '.www.'" |
michael@0 | 57 | |
michael@0 | 58 | /** |
michael@0 | 59 | * Select the best prefix for a host, based on existing pages registered for it. |
michael@0 | 60 | * Prefixes have a priority, from the top to the bottom, so that secure pages |
michael@0 | 61 | * have higher priority, and more generically "www." prefixed hosts come before |
michael@0 | 62 | * unprefixed ones. |
michael@0 | 63 | * Given a host, examine associated pages and: |
michael@0 | 64 | * - if all of the typed pages start with https://www. return https://www. |
michael@0 | 65 | * - if all of the typed pages start with https:// return https:// |
michael@0 | 66 | * - if all of the typed pages start with ftp: return ftp:// |
michael@0 | 67 | * - if all of the typed pages start with www. return www. |
michael@0 | 68 | * - otherwise don't use any prefix |
michael@0 | 69 | */ |
michael@0 | 70 | #define HOSTS_PREFIX_PRIORITY_FRAGMENT \ |
michael@0 | 71 | "SELECT CASE " \ |
michael@0 | 72 | "WHEN 1 = ( " \ |
michael@0 | 73 | "SELECT min(substr(url,1,12) = 'https://www.') FROM moz_places h " \ |
michael@0 | 74 | "WHERE (" HOST_TO_REVHOST_PREDICATE ") AND +h.typed = 1 " \ |
michael@0 | 75 | ") THEN 'https://www.' " \ |
michael@0 | 76 | "WHEN 1 = ( " \ |
michael@0 | 77 | "SELECT min(substr(url,1,8) = 'https://') FROM moz_places h " \ |
michael@0 | 78 | "WHERE (" HOST_TO_REVHOST_PREDICATE ") AND +h.typed = 1 " \ |
michael@0 | 79 | ") THEN 'https://' " \ |
michael@0 | 80 | "WHEN 1 = ( " \ |
michael@0 | 81 | "SELECT min(substr(url,1,4) = 'ftp:') FROM moz_places h " \ |
michael@0 | 82 | "WHERE (" HOST_TO_REVHOST_PREDICATE ") AND +h.typed = 1 " \ |
michael@0 | 83 | ") THEN 'ftp://' " \ |
michael@0 | 84 | "WHEN 1 = ( " \ |
michael@0 | 85 | "SELECT min(substr(url,1,11) = 'http://www.') FROM moz_places h " \ |
michael@0 | 86 | "WHERE (" HOST_TO_REVHOST_PREDICATE ") AND +h.typed = 1 " \ |
michael@0 | 87 | ") THEN 'www.' " \ |
michael@0 | 88 | "END " |
michael@0 | 89 | |
michael@0 | 90 | /** |
michael@0 | 91 | * These triggers update the hostnames table whenever moz_places changes. |
michael@0 | 92 | */ |
michael@0 | 93 | #define CREATE_PLACES_AFTERINSERT_TRIGGER NS_LITERAL_CSTRING( \ |
michael@0 | 94 | "CREATE TEMP TRIGGER moz_places_afterinsert_trigger " \ |
michael@0 | 95 | "AFTER INSERT ON moz_places FOR EACH ROW " \ |
michael@0 | 96 | "WHEN LENGTH(NEW.rev_host) > 1 " \ |
michael@0 | 97 | "BEGIN " \ |
michael@0 | 98 | "INSERT OR REPLACE INTO moz_hosts (id, host, frecency, typed, prefix) " \ |
michael@0 | 99 | "VALUES (" \ |
michael@0 | 100 | "(SELECT id FROM moz_hosts WHERE host = fixup_url(get_unreversed_host(NEW.rev_host))), " \ |
michael@0 | 101 | "fixup_url(get_unreversed_host(NEW.rev_host)), " \ |
michael@0 | 102 | "MAX(IFNULL((SELECT frecency FROM moz_hosts WHERE host = fixup_url(get_unreversed_host(NEW.rev_host))), -1), NEW.frecency), " \ |
michael@0 | 103 | "MAX(IFNULL((SELECT typed FROM moz_hosts WHERE host = fixup_url(get_unreversed_host(NEW.rev_host))), 0), NEW.typed), " \ |
michael@0 | 104 | "(" HOSTS_PREFIX_PRIORITY_FRAGMENT \ |
michael@0 | 105 | "FROM ( " \ |
michael@0 | 106 | "SELECT fixup_url(get_unreversed_host(NEW.rev_host)) AS host " \ |
michael@0 | 107 | ") AS match " \ |
michael@0 | 108 | ") " \ |
michael@0 | 109 | "); " \ |
michael@0 | 110 | "END" \ |
michael@0 | 111 | ) |
michael@0 | 112 | |
michael@0 | 113 | #define CREATE_PLACES_AFTERDELETE_TRIGGER NS_LITERAL_CSTRING( \ |
michael@0 | 114 | "CREATE TEMP TRIGGER moz_places_afterdelete_trigger " \ |
michael@0 | 115 | "AFTER DELETE ON moz_places FOR EACH ROW " \ |
michael@0 | 116 | "BEGIN " \ |
michael@0 | 117 | "DELETE FROM moz_hosts " \ |
michael@0 | 118 | "WHERE host = fixup_url(get_unreversed_host(OLD.rev_host)) " \ |
michael@0 | 119 | "AND NOT EXISTS(" \ |
michael@0 | 120 | "SELECT 1 FROM moz_places " \ |
michael@0 | 121 | "WHERE rev_host = get_unreversed_host(host || '.') || '.' " \ |
michael@0 | 122 | "OR rev_host = get_unreversed_host(host || '.') || '.www.' " \ |
michael@0 | 123 | "); " \ |
michael@0 | 124 | "UPDATE moz_hosts " \ |
michael@0 | 125 | "SET prefix = (" HOSTS_PREFIX_PRIORITY_FRAGMENT ") " \ |
michael@0 | 126 | "WHERE host = fixup_url(get_unreversed_host(OLD.rev_host)); " \ |
michael@0 | 127 | "END" \ |
michael@0 | 128 | ) |
michael@0 | 129 | |
michael@0 | 130 | // For performance reasons the host frecency is updated only when the page |
michael@0 | 131 | // frecency changes by a meaningful percentage. This is because the frecency |
michael@0 | 132 | // decay algorithm requires to update all the frecencies at once, causing a |
michael@0 | 133 | // too high overhead, while leaving the ordering unchanged. |
michael@0 | 134 | #define CREATE_PLACES_AFTERUPDATE_FRECENCY_TRIGGER NS_LITERAL_CSTRING( \ |
michael@0 | 135 | "CREATE TEMP TRIGGER moz_places_afterupdate_frecency_trigger " \ |
michael@0 | 136 | "AFTER UPDATE OF frecency ON moz_places FOR EACH ROW " \ |
michael@0 | 137 | "WHEN NEW.frecency >= 0 " \ |
michael@0 | 138 | "AND ABS(" \ |
michael@0 | 139 | "IFNULL((NEW.frecency - OLD.frecency) / CAST(NEW.frecency AS REAL), " \ |
michael@0 | 140 | "(NEW.frecency - OLD.frecency))" \ |
michael@0 | 141 | ") > .05 " \ |
michael@0 | 142 | "BEGIN " \ |
michael@0 | 143 | "UPDATE moz_hosts " \ |
michael@0 | 144 | "SET frecency = (SELECT MAX(frecency) FROM moz_places " \ |
michael@0 | 145 | "WHERE rev_host = get_unreversed_host(host || '.') || '.' " \ |
michael@0 | 146 | "OR rev_host = get_unreversed_host(host || '.') || '.www.') " \ |
michael@0 | 147 | "WHERE host = fixup_url(get_unreversed_host(NEW.rev_host)); " \ |
michael@0 | 148 | "END" \ |
michael@0 | 149 | ) |
michael@0 | 150 | |
michael@0 | 151 | #define CREATE_PLACES_AFTERUPDATE_TYPED_TRIGGER NS_LITERAL_CSTRING( \ |
michael@0 | 152 | "CREATE TEMP TRIGGER moz_places_afterupdate_typed_trigger " \ |
michael@0 | 153 | "AFTER UPDATE OF typed ON moz_places FOR EACH ROW " \ |
michael@0 | 154 | "WHEN NEW.typed = 1 " \ |
michael@0 | 155 | "BEGIN " \ |
michael@0 | 156 | "UPDATE moz_hosts " \ |
michael@0 | 157 | "SET typed = 1 " \ |
michael@0 | 158 | "WHERE host = fixup_url(get_unreversed_host(NEW.rev_host)); " \ |
michael@0 | 159 | "END" \ |
michael@0 | 160 | ) |
michael@0 | 161 | |
michael@0 | 162 | /** |
michael@0 | 163 | * This trigger removes a row from moz_openpages_temp when open_count reaches 0. |
michael@0 | 164 | * |
michael@0 | 165 | * @note this should be kept up-to-date with the definition in |
michael@0 | 166 | * nsPlacesAutoComplete.js |
michael@0 | 167 | */ |
michael@0 | 168 | #define CREATE_REMOVEOPENPAGE_CLEANUP_TRIGGER NS_LITERAL_CSTRING( \ |
michael@0 | 169 | "CREATE TEMPORARY TRIGGER moz_openpages_temp_afterupdate_trigger " \ |
michael@0 | 170 | "AFTER UPDATE OF open_count ON moz_openpages_temp FOR EACH ROW " \ |
michael@0 | 171 | "WHEN NEW.open_count = 0 " \ |
michael@0 | 172 | "BEGIN " \ |
michael@0 | 173 | "DELETE FROM moz_openpages_temp " \ |
michael@0 | 174 | "WHERE url = NEW.url;" \ |
michael@0 | 175 | "END" \ |
michael@0 | 176 | ) |
michael@0 | 177 | |
michael@0 | 178 | #endif // __nsPlacesTriggers_h__ |