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

content / test / mock_background_sync_controller.cc [blame]

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

#include "content/test/mock_background_sync_controller.h"
#include "base/containers/contains.h"
#include "base/metrics/field_trial_params.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"

namespace {

const char kFieldTrialName[] = "BackgroundSync";
const char kMaxAttemptsParameterName[] = "max_sync_attempts";
const char kMinPeriodicSyncEventsInterval[] =
    "min_periodic_sync_events_interval_sec";

}  // namespace

namespace content {

MockBackgroundSyncController::MockBackgroundSyncController() = default;

MockBackgroundSyncController::~MockBackgroundSyncController() = default;

void MockBackgroundSyncController::NotifyOneShotBackgroundSyncRegistered(
    const url::Origin& origin,
    bool can_fire,
    bool is_reregistered) {
  registration_count_ += 1;
  registration_origin_ = origin;
}

void MockBackgroundSyncController::ScheduleBrowserWakeUpWithDelay(
    blink::mojom::BackgroundSyncType sync_type,
    base::TimeDelta delay) {
  if (sync_type == blink::mojom::BackgroundSyncType::PERIODIC) {
    run_in_background_for_periodic_sync_count_ += 1;
    periodic_sync_browser_wakeup_delay_ = delay;
    return;
  }
  run_in_background_for_one_shot_sync_count_ += 1;
  one_shot_sync_browser_wakeup_delay_ = delay;
}

void MockBackgroundSyncController::CancelBrowserWakeup(
    blink::mojom::BackgroundSyncType sync_type) {
  if (sync_type == blink::mojom::BackgroundSyncType::PERIODIC) {
    periodic_sync_browser_wakeup_delay_ = base::TimeDelta::Max();
  } else {
    one_shot_sync_browser_wakeup_delay_ = base::TimeDelta::Max();
  }
}

void MockBackgroundSyncController::ApplyFieldTrialParamsOverrides() {
  std::map<std::string, std::string> field_params;
  if (!base::GetFieldTrialParams(kFieldTrialName, &field_params))
    return;

  if (base::Contains(field_params, kMaxAttemptsParameterName)) {
    int max_attempts;
    if (base::StringToInt(field_params[kMaxAttemptsParameterName],
                          &max_attempts)) {
      background_sync_parameters_.max_sync_attempts = max_attempts;
    }
  }

  if (base::Contains(field_params, kMinPeriodicSyncEventsInterval)) {
    int min_periodic_sync_events_interval_sec;
    if (base::StringToInt(field_params[kMinPeriodicSyncEventsInterval],
                          &min_periodic_sync_events_interval_sec)) {
      background_sync_parameters_.min_periodic_sync_events_interval =
          base::Seconds(min_periodic_sync_events_interval_sec);
    }
  }
}

void MockBackgroundSyncController::GetParameterOverrides(
    BackgroundSyncParameters* parameters) {
  ApplyFieldTrialParamsOverrides();
  *parameters = background_sync_parameters_;
}

base::TimeDelta MockBackgroundSyncController::GetNextEventDelay(
    const BackgroundSyncRegistration& registration,
    BackgroundSyncParameters* parameters,
    base::TimeDelta time_till_soonest_scheduled_event_for_origin) {
  DCHECK(parameters);

  if (suspended_periodic_sync_origins_.count(registration.origin()))
    return base::TimeDelta::Max();

  int num_attempts = registration.num_attempts();

  if (!num_attempts) {
    // First attempt.
    switch (registration.sync_type()) {
      case blink::mojom::BackgroundSyncType::ONE_SHOT:
        return base::TimeDelta();
      case blink::mojom::BackgroundSyncType::PERIODIC:
        int64_t effective_gap_ms =
            parameters->min_periodic_sync_events_interval.InMilliseconds();
        return base::Milliseconds(
            std::max(registration.options()->min_interval, effective_gap_ms));
    }
  }

  // After a sync event has been fired.
  DCHECK_LT(num_attempts, parameters->max_sync_attempts);
  return parameters->initial_retry_delay *
         pow(parameters->retry_delay_factor, num_attempts - 1);
}

std::unique_ptr<BackgroundSyncController::BackgroundSyncEventKeepAlive>
MockBackgroundSyncController::CreateBackgroundSyncEventKeepAlive() {
  return nullptr;
}

void MockBackgroundSyncController::NoteSuspendedPeriodicSyncOrigins(
    std::set<url::Origin> suspended_origins) {
  for (auto& origin : suspended_origins)
    suspended_periodic_sync_origins_.insert(std::move(origin));
}

void MockBackgroundSyncController::NoteRegisteredPeriodicSyncOrigins(
    std::set<url::Origin> registered_origins) {
  for (auto& origin : registered_origins)
    suspended_periodic_sync_origins_.insert(std::move(origin));
}

void MockBackgroundSyncController::AddToTrackedOrigins(
    const url::Origin& origin) {
  periodic_sync_origins_.insert(origin);
}

void MockBackgroundSyncController::RemoveFromTrackedOrigins(
    const url::Origin& origin) {
  periodic_sync_origins_.erase(origin);
}

}  // namespace content