1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323

content / browser / attribution_reporting / sql_queries.h [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_ATTRIBUTION_REPORTING_SQL_QUERIES_H_
#define CONTENT_BROWSER_ATTRIBUTION_REPORTING_SQL_QUERIES_H_

#include "content/browser/attribution_reporting/attribution_reporting.mojom.h"
#include "content/browser/attribution_reporting/rate_limit_table.h"

namespace content::attribution_queries {

static_assert(static_cast<int>(
                  attribution_reporting::mojom::ReportType::kEventLevel) == 0,
              "update `report_type=0` clause below");
inline constexpr const char kMinPrioritySql[] =
    "SELECT metadata,report_id FROM reports "
    "WHERE source_id=? AND initial_report_time=? AND report_type=0";

// Rows are ordered by source_id instead of source_time because the former is
// strictly increasing while the latter is subject to clock adjustments. This
// property is only guaranteed because of the use of AUTOINCREMENT on the
// source_id column, which prevents reuse upon row deletion.
inline constexpr const char kGetMatchingSourcesSql[] =
    "SELECT I.priority,I.source_id,I.num_attributions>0 OR "
    "I.num_aggregatable_attribution_reports>0,"
    "I.attribution_scopes_data,I.source_time "
    "FROM sources I "
    "WHERE I.reporting_origin=? "
    "AND(I.event_level_active=1 OR I.aggregatable_active=1)"
    "AND I.expiry_time>? "
    "AND I.source_id IN("
    "SELECT source_id FROM source_destinations D "
    "WHERE D.destination_site IN(?,?,?)"
    ")";

inline constexpr const char kSelectExpiredSourcesSql[] =
    "SELECT source_id FROM sources "
    "WHERE expiry_time<=? AND "
    "source_id NOT IN("
    "SELECT source_id FROM reports"
    ")LIMIT ?";

inline constexpr const char kSelectInactiveSourcesSql[] =
    "SELECT source_id FROM sources "
    "WHERE event_level_active=0 AND aggregatable_active=0 AND "
    "source_id NOT IN("
    "SELECT source_id FROM reports"
    ")LIMIT ?";

inline constexpr const char kScanSourcesData[] =
    "SELECT I.reporting_origin,I.source_id "
    "FROM sources I WHERE "
    "I.source_time BETWEEN ? AND ?";

inline constexpr const char kScanReportsData[] =
    "SELECT R.reporting_origin,R.source_id,R.report_id,R.report_type "
    "FROM reports R WHERE "
    "R.trigger_time BETWEEN ? AND ?";

inline constexpr const char kDeleteVestigialConversionSql[] =
    "DELETE FROM reports WHERE source_id=? RETURNING report_type";

inline constexpr const char kCountActiveSourcesFromSourceOriginSql[] =
    "SELECT COUNT(*)FROM sources "
    "WHERE source_origin=? "
    "AND(event_level_active=1 OR aggregatable_active=1)"
    "AND expiry_time>?";

inline constexpr const char kCountSourcesSql[] = "SELECT COUNT(*)FROM sources";

inline constexpr const char kDedupKeySql[] =
    "SELECT dedup_key,report_type FROM dedup_keys WHERE source_id=?";

inline constexpr const char kGetSourcesDataKeysSql[] =
    "SELECT reporting_origin FROM sources";

static_assert(
    static_cast<int>(
        attribution_reporting::mojom::ReportType::kNullAggregatable) == 2,
    "update `report_type=2` clause below");
inline constexpr const char kGetNullReportsDataKeysSql[] =
    "SELECT reporting_origin FROM reports WHERE report_type=2";

inline constexpr const char kGetRateLimitDataKeysSql[] =
    "SELECT reporting_origin FROM rate_limits";

inline constexpr const char kCountReportsForDestinationSql[] =
    "SELECT COUNT(*)FROM source_destinations D "
    "JOIN reports R "
    "ON R.source_id=D.source_id "
    "WHERE D.destination_site=? AND R.report_type=?";

inline constexpr char kNextReportTimeSql[] =
    "SELECT MIN(report_time)FROM reports WHERE report_time>?";

// Set the report time for all reports that should have been sent before now
// to now + a random number of microseconds between `min_delay` and
// `max_delay`, both inclusive. We use RANDOM, instead of a method on the
// delegate, to avoid having to pull all reports into memory and update them
// one by one. We use ABS because RANDOM may return a negative integer. We add
// 1 to the difference between `max_delay` and `min_delay` to ensure that the
// range of generated values is inclusive. If `max_delay == min_delay`, we
// take the remainder modulo 1, which is always 0.
inline constexpr const char kSetReportTimeSql[] =
    "UPDATE reports "
    "SET report_time=?+ABS(RANDOM()%?)"
    "WHERE report_time<?";

// clang-format off

#define ATTRIBUTION_SOURCE_COLUMNS_SQL(prefix) \
  prefix "source_id,"                          \
  prefix "source_event_id,"                    \
  prefix "source_origin,"                      \
  prefix "reporting_origin,"                   \
  prefix "source_time,"                        \
  prefix "expiry_time,"                        \
  prefix "aggregatable_report_window_time,"    \
  prefix "source_type,"                        \
  prefix "attribution_logic,"                  \
  prefix "priority,"                           \
  prefix "debug_key,"                          \
  prefix "num_attributions,"                   \
  prefix "remaining_aggregatable_attribution_budget," \
  prefix "num_aggregatable_attribution_reports," \
  prefix "remaining_aggregatable_debug_budget," \
  prefix "aggregatable_source,"                \
  prefix "filter_data,"                        \
  prefix "attribution_scopes_data,"            \
  prefix "event_level_active,"                 \
  prefix "aggregatable_active,"                \
  prefix "read_only_source_data"

inline constexpr const char kReadSourceToAttributeSql[] =
    "SELECT " ATTRIBUTION_SOURCE_COLUMNS_SQL("")
    " FROM sources "
    "WHERE source_id=?";

inline constexpr const char kGetActiveSourcesSql[] =
      "SELECT " ATTRIBUTION_SOURCE_COLUMNS_SQL("")
      " FROM sources "
      "WHERE(event_level_active=1 OR aggregatable_active=1)AND "
      "expiry_time>? LIMIT ?";

#define ATTRIBUTION_SELECT_REPORT_AND_SOURCE_COLUMNS_SQL                      \
  "SELECT "                                                                   \
  ATTRIBUTION_SOURCE_COLUMNS_SQL("I.")                                        \
  ",R.report_id,R.trigger_time,R.report_time,R.initial_report_time,"          \
  "R.failed_send_attempts,R.external_report_id,R.debug_key,R.context_origin," \
  "R.reporting_origin,R.report_type,R.metadata "                              \
  "FROM reports R "                                                           \
  "LEFT JOIN sources I ON R.source_id=I.source_id "

inline constexpr const char kGetReportsSql[] =
    ATTRIBUTION_SELECT_REPORT_AND_SOURCE_COLUMNS_SQL
    "WHERE R.report_time<=? LIMIT ?";

inline constexpr const char kGetReportSql[] =
    ATTRIBUTION_SELECT_REPORT_AND_SOURCE_COLUMNS_SQL
    "WHERE R.report_id=?";

#undef ATTRIBUTION_SELECT_REPORT_AND_SOURCE_COLUMNS_SQL

inline constexpr const char kUpdateFailedReportSql[] =
  "UPDATE reports "
  "SET report_time=?,"
  "failed_send_attempts=failed_send_attempts+1 "
  "WHERE report_id=?";

static_assert(static_cast<int>(
                  attribution_reporting::mojom::ReportType::kEventLevel) == 0,
              "update `report_type=0` clause below");
inline constexpr const char kDeletePendingEventLevelReportsForSourceSql[] =
  "DELETE FROM reports "
  "WHERE report_type=0 AND source_id=? AND trigger_time>=? "
  "RETURNING report_id";

static_assert(
    static_cast<int>(
        attribution_reporting::mojom::ReportType::kAggregatableAttribution) ==
        1,
    "update `report_type=1` clause below");
inline constexpr char kDeleteAggregatableReportsForDestinationLimitSql[] =
  "DELETE FROM reports "
  "WHERE report_type=1 AND source_id=? "
  "RETURNING report_id";

// clang-format on

inline constexpr const char kRateLimitAttributionAllowedSql[] =
    "SELECT COUNT(*)FROM rate_limits "
    "WHERE scope=? "
    "AND destination_site=? "
    "AND source_site=? "
    "AND reporting_site=? "
    "AND time>?";

static_assert(static_cast<int>(RateLimitTable::Scope::kSource) == 0,
              "update `scope=0` query below");
#define RATE_LIMIT_SOURCE_CONDITION "scope=0"

static_assert(static_cast<int>(RateLimitTable::Scope::kEventLevelAttribution) ==
                  1,
              "update `scope=1` query below");
static_assert(
    static_cast<int>(RateLimitTable::Scope::kAggregatableAttribution) == 2,
    "update `scope=2` query below");
#define RATE_LIMIT_ATTRIBUTION_CONDITION "(scope=1 OR scope=2)"

inline constexpr const char kRateLimitSourceAllowedSql[] =
    "SELECT destination_site,time,destination_limit_priority,source_id "
    "FROM rate_limits "
    "WHERE " RATE_LIMIT_SOURCE_CONDITION
    " AND source_site=?"
    " AND reporting_site=?"
    " AND source_expiry_or_attribution_time>?"
    " AND deactivated_for_source_destination_limit=0";

inline constexpr const char kRateLimitSourceAllowedDestinationRateLimitSql[] =
    "SELECT destination_site,reporting_site FROM rate_limits "
    "WHERE " RATE_LIMIT_SOURCE_CONDITION
    " AND source_site=?"
    " AND source_expiry_or_attribution_time>?"
    " AND time>?";

inline constexpr const char
    kRateLimitSourceAllowedDestinationPerDayRateLimitSql[] =
        "SELECT destination_site,reporting_site FROM rate_limits "
        "WHERE " RATE_LIMIT_SOURCE_CONDITION
        " AND source_site=?"
        " AND reporting_site=?"
        " AND source_expiry_or_attribution_time>?"
        " AND time>?";

#define RATE_LIMIT_SELECT_REPORTING_ORIGINS_QUERY \
  "SELECT reporting_origin FROM rate_limits "     \
  "WHERE source_site=? "                          \
  "AND destination_site=? "                       \
  "AND time>? "                                   \
  "AND "

inline constexpr const char kRateLimitSelectSourceReportingOriginsSql[] =
    RATE_LIMIT_SELECT_REPORTING_ORIGINS_QUERY RATE_LIMIT_SOURCE_CONDITION;

inline constexpr const char kRateLimitSelectAttributionReportingOriginsSql[] =
    RATE_LIMIT_SELECT_REPORTING_ORIGINS_QUERY RATE_LIMIT_ATTRIBUTION_CONDITION;

#undef RATE_LIMIT_SELECT_REPORTING_ORIGINS_QUERY

inline constexpr const char kRateLimitSelectSourceReportingOriginsBySiteSql[] =
    "SELECT reporting_origin FROM rate_limits "
    "WHERE " RATE_LIMIT_SOURCE_CONDITION
    " AND source_site=?"
    " AND reporting_site=?"
    " AND time>?";

inline constexpr const char
    kRateLimitCountUniqueReportingOriginsPerSiteForAttributionSql[] =
        "SELECT COUNT(DISTINCT reporting_origin)FROM rate_limits "
        "WHERE " RATE_LIMIT_ATTRIBUTION_CONDITION
        " AND destination_site=?"
        " AND reporting_site=?"
        " AND source_expiry_or_attribution_time>?";

static_assert(RateLimitTable::kUnsetRecordId == -1,
              "update `report_id!=-1` query below");
#define RATE_LIMIT_REPORT_ID_SET_CONDITION "report_id!=-1"

inline constexpr const char kDeleteAttributionRateLimitByReportIdSql[] =
    "DELETE FROM rate_limits "
    "WHERE " RATE_LIMIT_ATTRIBUTION_CONDITION
    " AND scope=? AND " RATE_LIMIT_REPORT_ID_SET_CONDITION " AND report_id=?";

inline constexpr const char kDeleteRateLimitRangeSql[] =
    "DELETE FROM rate_limits WHERE"
    "(time BETWEEN ?1 AND ?2)OR"
    "(" RATE_LIMIT_ATTRIBUTION_CONDITION
    "AND source_expiry_or_attribution_time BETWEEN ?1 AND ?2)";

inline constexpr const char kSelectRateLimitsForDeletionSql[] =
    "SELECT id,reporting_origin "
    "FROM rate_limits WHERE"
    "(time BETWEEN ?1 AND ?2)OR"
    "(" RATE_LIMIT_ATTRIBUTION_CONDITION
    "AND source_expiry_or_attribution_time BETWEEN ?1 AND ?2)";

inline constexpr const char kDeleteExpiredRateLimitsSql[] =
    "DELETE FROM rate_limits "
    "WHERE time<=? AND(" RATE_LIMIT_ATTRIBUTION_CONDITION
    "OR source_expiry_or_attribution_time<=?)";

inline constexpr const char kDeleteRateLimitsBySourceIdSql[] =
    "DELETE FROM rate_limits WHERE source_id=?";

inline constexpr const char kDeactivateForSourceDestinationLimitSql[] =
    "UPDATE rate_limits "
    "SET deactivated_for_source_destination_limit=1 "
    "WHERE " RATE_LIMIT_SOURCE_CONDITION " AND source_id=?";

#undef RATE_LIMIT_SOURCE_CONDITION

inline constexpr const char kAggregatableDebugReportAllowedForRateLimitSql[] =
    "SELECT reporting_site,consumed_budget "
    "FROM aggregatable_debug_rate_limits "
    "WHERE context_site=? AND time>?";

inline constexpr const char kDeleteExpiredAggregatableDebugRateLimitsSql[] =
    "DELETE FROM aggregatable_debug_rate_limits "
    "WHERE time<=?";

inline constexpr const char kSelectAggregatableDebugRateLimitsForDeletionSql[] =
    "SELECT id,reporting_origin "
    "FROM aggregatable_debug_rate_limits "
    "WHERE time BETWEEN ?1 AND ?2";

inline constexpr const char kDeleteAggregatableDebugRateLimitRangeSql[] =
    "DELETE FROM aggregatable_debug_rate_limits "
    "WHERE time BETWEEN ?1 AND ?2";

}  // namespace content::attribution_queries

#endif  // CONTENT_BROWSER_ATTRIBUTION_REPORTING_SQL_QUERIES_H_