summaryrefslogtreecommitdiff
path: root/chromium/components/offline_pages/core/prefetch/prefetch_dispatcher_impl.cc
blob: 591627820bd9bc06e034e501a5f63aab154ac15a (plain)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/offline_pages/core/prefetch/prefetch_dispatcher_impl.h"

#include <algorithm>
#include <utility>

#include "base/bind.h"
#include "base/guid.h"
#include "base/task_runner.h"
#include "base/task_scheduler/post_task.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/offline_pages/core/client_namespace_constants.h"
#include "components/offline_pages/core/offline_event_logger.h"
#include "components/offline_pages/core/offline_page_feature.h"
#include "components/offline_pages/core/offline_page_model.h"
#include "components/offline_pages/core/prefetch/add_unique_urls_task.h"
#include "components/offline_pages/core/prefetch/download_archives_task.h"
#include "components/offline_pages/core/prefetch/download_cleanup_task.h"
#include "components/offline_pages/core/prefetch/download_completed_task.h"
#include "components/offline_pages/core/prefetch/finalize_dismissed_url_suggestion_task.h"
#include "components/offline_pages/core/prefetch/generate_page_bundle_reconcile_task.h"
#include "components/offline_pages/core/prefetch/generate_page_bundle_task.h"
#include "components/offline_pages/core/prefetch/get_operation_task.h"
#include "components/offline_pages/core/prefetch/import_archives_task.h"
#include "components/offline_pages/core/prefetch/import_cleanup_task.h"
#include "components/offline_pages/core/prefetch/import_completed_task.h"
#include "components/offline_pages/core/prefetch/mark_operation_done_task.h"
#include "components/offline_pages/core/prefetch/metrics_finalization_task.h"
#include "components/offline_pages/core/prefetch/offline_metrics_collector.h"
#include "components/offline_pages/core/prefetch/page_bundle_update_task.h"
#include "components/offline_pages/core/prefetch/prefetch_background_task.h"
#include "components/offline_pages/core/prefetch/prefetch_background_task_handler.h"
#include "components/offline_pages/core/prefetch/prefetch_configuration.h"
#include "components/offline_pages/core/prefetch/prefetch_downloader.h"
#include "components/offline_pages/core/prefetch/prefetch_gcm_handler.h"
#include "components/offline_pages/core/prefetch/prefetch_importer.h"
#include "components/offline_pages/core/prefetch/prefetch_network_request_factory.h"
#include "components/offline_pages/core/prefetch/prefetch_service.h"
#include "components/offline_pages/core/prefetch/prefetch_types.h"
#include "components/offline_pages/core/prefetch/sent_get_operation_cleanup_task.h"
#include "components/offline_pages/core/prefetch/stale_entry_finalizer_task.h"
#include "components/offline_pages/core/prefetch/suggested_articles_observer.h"
#include "components/offline_pages/core/prefetch/thumbnail_fetcher.h"
#include "components/offline_pages/core/task.h"
#include "url/gurl.h"

namespace offline_pages {

namespace {

void DeleteBackgroundTaskHelper(std::unique_ptr<PrefetchBackgroundTask> task) {
  task.reset();
}

}  // namespace

PrefetchDispatcherImpl::PrefetchDispatcherImpl()
    : task_queue_(this), weak_factory_(this) {}

PrefetchDispatcherImpl::~PrefetchDispatcherImpl() = default;

void PrefetchDispatcherImpl::SetService(PrefetchService* service) {
  CHECK(service);
  service_ = service;
}

void PrefetchDispatcherImpl::SchedulePipelineProcessing() {
  needs_pipeline_processing_ = true;
  service_->GetLogger()->RecordActivity(
      "Dispatcher: Scheduled more pipeline processing.");
}

void PrefetchDispatcherImpl::EnsureTaskScheduled() {
  if (background_task_) {
    background_task_->SetReschedule(
        PrefetchBackgroundTaskRescheduleType::RESCHEDULE_WITHOUT_BACKOFF);
  } else {
    service_->GetPrefetchBackgroundTaskHandler()->EnsureTaskScheduled();
  }
}

void PrefetchDispatcherImpl::AddCandidatePrefetchURLs(
    const std::string& name_space,
    const std::vector<PrefetchURL>& prefetch_urls) {
  if (!service_->GetPrefetchConfiguration()->IsPrefetchingEnabled())
    return;

  service_->GetLogger()->RecordActivity("Dispatcher: Received " +
                                        std::to_string(prefetch_urls.size()) +
                                        " suggested URLs.");

  PrefetchStore* prefetch_store = service_->GetPrefetchStore();

  // Run 2 pipeline expiration tasks first to ensure there is no buildup of URLs
  // in the pipeline if the new ones are coming but NOW can't be entered (for
  // example, if the user is never on WiFi with enough battery charge).
  // First, detect stale entries and move them to FINISHED.
  task_queue_.AddTask(
      std::make_unique<StaleEntryFinalizerTask>(this, prefetch_store));

  // Second, move FINISHED to ZOMBIE.
  task_queue_.AddTask(
      std::make_unique<MetricsFinalizationTask>(prefetch_store));

  // Third, add new unique URLs and remove unneeded ZOMBIEs.
  std::unique_ptr<Task> add_task = std::make_unique<AddUniqueUrlsTask>(
      this, prefetch_store, name_space, prefetch_urls);
  task_queue_.AddTask(std::move(add_task));

  // Report the 'enabled' day if we receive URLs and Prefetch is enabled.
  service_->GetOfflineMetricsCollector()->OnPrefetchEnabled();
}

void PrefetchDispatcherImpl::RemoveAllUnprocessedPrefetchURLs(
    const std::string& name_space) {
  if (!service_->GetPrefetchConfiguration()->IsPrefetchingEnabled())
    return;

  NOTIMPLEMENTED();
}

void PrefetchDispatcherImpl::RemovePrefetchURLsByClientId(
    const ClientId& client_id) {
  if (!service_->GetPrefetchConfiguration()->IsPrefetchingEnabled())
    return;
  PrefetchStore* prefetch_store = service_->GetPrefetchStore();
  task_queue_.AddTask(std::make_unique<FinalizeDismissedUrlSuggestionTask>(
      prefetch_store, client_id));
}

void PrefetchDispatcherImpl::BeginBackgroundTask(
    std::unique_ptr<PrefetchBackgroundTask> background_task) {
  if (!service_->GetPrefetchConfiguration()->IsPrefetchingEnabled())
    return;
  service_->GetLogger()->RecordActivity(
      "Dispatcher: Beginning background task.");

  background_task_ = std::move(background_task);
  service_->GetPrefetchBackgroundTaskHandler()->RemoveSuspension();

  // Reset suspended state in case that it was set last time and Chrome is still
  // running till new background task starts after the suspension period.
  suspended_ = false;

  QueueReconcileTasks();
  QueueActionTasks();
}

void PrefetchDispatcherImpl::QueueReconcileTasks() {
  if (suspended_)
    return;

  service_->GetLogger()->RecordActivity("Dispatcher: Adding reconcile tasks.");
  // Note: For optimal results StaleEntryFinalizerTask should be executed before
  // other reconciler tasks that deal with external systems so that entries
  // finalized by it will promptly effect any external processing they relate
  // to.
  task_queue_.AddTask(std::make_unique<StaleEntryFinalizerTask>(
      this, service_->GetPrefetchStore()));

  task_queue_.AddTask(std::make_unique<GeneratePageBundleReconcileTask>(
      service_->GetPrefetchStore(),
      service_->GetPrefetchNetworkRequestFactory()));

  task_queue_.AddTask(std::make_unique<SentGetOperationCleanupTask>(
      service_->GetPrefetchStore(),
      service_->GetPrefetchNetworkRequestFactory()));

  // Notifies the downloader that the download cleanup can proceed when the
  // download service is up. The prefetch service and download service are two
  // separate services which can start up on their own. The download cleanup
  // should only kick in when both services are ready.
  service_->GetPrefetchDownloader()->CleanupDownloadsWhenReady();

  task_queue_.AddTask(std::make_unique<ImportCleanupTask>(
      service_->GetPrefetchStore(), service_->GetPrefetchImporter()));

  // This task should be last, because it is least important for correct
  // operation of the system, and because any reconciliation tasks might
  // generate more entries in the FINISHED state that the finalization task
  // could pick up.
  task_queue_.AddTask(
      std::make_unique<MetricsFinalizationTask>(service_->GetPrefetchStore()));
}

void PrefetchDispatcherImpl::QueueActionTasks() {
  service_->GetLogger()->RecordActivity("Dispatcher: Adding action tasks.");

  // Import should be run first to minimize time to import after download
  // finishes, during the download background task.
  std::unique_ptr<Task> import_archives_task =
      std::make_unique<ImportArchivesTask>(service_->GetPrefetchStore(),
                                           service_->GetPrefetchImporter());
  task_queue_.AddTask(std::move(import_archives_task));

  std::unique_ptr<Task> download_archives_task =
      std::make_unique<DownloadArchivesTask>(service_->GetPrefetchStore(),
                                             service_->GetPrefetchDownloader());
  task_queue_.AddTask(std::move(download_archives_task));

  // The following tasks should not be run unless we are in the background task,
  // as we need to ensure WiFi access at that time.
  if (!background_task_)
    return;

  std::unique_ptr<Task> get_operation_task = std::make_unique<GetOperationTask>(
      service_->GetPrefetchStore(),
      service_->GetPrefetchNetworkRequestFactory(),
      base::Bind(
          &PrefetchDispatcherImpl::DidGenerateBundleOrGetOperationRequest,
          weak_factory_.GetWeakPtr(), "GetOperationRequest"));
  task_queue_.AddTask(std::move(get_operation_task));

  std::unique_ptr<Task> generate_page_bundle_task =
      std::make_unique<GeneratePageBundleTask>(
          this, service_->GetPrefetchStore(), service_->GetPrefetchGCMHandler(),
          service_->GetPrefetchNetworkRequestFactory(),
          base::Bind(
              &PrefetchDispatcherImpl::DidGenerateBundleOrGetOperationRequest,
              weak_factory_.GetWeakPtr(), "GeneratePageBundleRequest"));
  task_queue_.AddTask(std::move(generate_page_bundle_task));
}

void PrefetchDispatcherImpl::StopBackgroundTask() {
  if (!service_->GetPrefetchConfiguration()->IsPrefetchingEnabled())
    return;

  service_->GetLogger()->RecordActivity(
      "Dispatcher: Stopping background task.");

  DisposeTask();
}

void PrefetchDispatcherImpl::OnTaskQueueIsIdle() {
  if (!suspended_ && needs_pipeline_processing_) {
    needs_pipeline_processing_ = false;
    QueueActionTasks();
  } else {
    PrefetchNetworkRequestFactory* request_factory =
        service_->GetPrefetchNetworkRequestFactory();
    if (!request_factory->HasOutstandingRequests())
      DisposeTask();
  }
}

void PrefetchDispatcherImpl::DisposeTask() {
  if (!background_task_)
    return;

  // Delay the deletion till the caller finishes.
  base::ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE,
      base::BindOnce(&DeleteBackgroundTaskHelper, std::move(background_task_)));
}

void PrefetchDispatcherImpl::GCMOperationCompletedMessageReceived(
    const std::string& operation_name) {
  if (!service_->GetPrefetchConfiguration()->IsPrefetchingEnabled())
    return;

  service_->GetLogger()->RecordActivity("Dispatcher: Received GCM message.");

  PrefetchStore* prefetch_store = service_->GetPrefetchStore();
  task_queue_.AddTask(std::make_unique<MarkOperationDoneTask>(
      this, prefetch_store, operation_name));
}

void PrefetchDispatcherImpl::DidGenerateBundleOrGetOperationRequest(
    const std::string& request_name_for_logging,
    PrefetchRequestStatus status,
    const std::string& operation_name,
    const std::vector<RenderPageInfo>& pages) {
  LogRequestResult(request_name_for_logging, status, operation_name, pages);

  // Note that we still want to trigger PageBundleUpdateTask even if the request
  // fails and no page is returned. This is because currently we only check for
  // the empty task queue and no outstanding request in order to decide whether
  // to dispose th background task upon the completion of a task.
  PrefetchStore* prefetch_store = service_->GetPrefetchStore();
  task_queue_.AddTask(std::make_unique<PageBundleUpdateTask>(
      prefetch_store, this, operation_name, pages));

  if (background_task_ && status != PrefetchRequestStatus::SUCCESS) {
    PrefetchBackgroundTaskRescheduleType reschedule_type =
        PrefetchBackgroundTaskRescheduleType::NO_RESCHEDULE;
    switch (status) {
      case PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF:
        reschedule_type =
            PrefetchBackgroundTaskRescheduleType::RESCHEDULE_WITH_BACKOFF;
        break;
      case PrefetchRequestStatus::SHOULD_RETRY_WITHOUT_BACKOFF:
        reschedule_type =
            PrefetchBackgroundTaskRescheduleType::RESCHEDULE_WITHOUT_BACKOFF;
        break;
      case PrefetchRequestStatus::SHOULD_SUSPEND:
        reschedule_type = PrefetchBackgroundTaskRescheduleType::SUSPEND;
        break;
      default:
        NOTREACHED();
        break;
    }
    background_task_->SetReschedule(reschedule_type);

    if (reschedule_type == PrefetchBackgroundTaskRescheduleType::SUSPEND)
      suspended_ = true;
  }
}

void PrefetchDispatcherImpl::CleanupDownloads(
    const std::set<std::string>& outstanding_download_ids,
    const std::map<std::string, std::pair<base::FilePath, int64_t>>&
        success_downloads) {
  task_queue_.AddTask(std::make_unique<DownloadCleanupTask>(
      this, service_->GetPrefetchStore(), outstanding_download_ids,
      success_downloads));
}

void PrefetchDispatcherImpl::GeneratePageBundleRequested(
    std::unique_ptr<PrefetchDispatcher::IdsVector> ids) {
  // Reverse the order so that the fresher items are last. This is done because
  // the ids are popped from the end of the vector.
  std::reverse(ids->begin(), ids->end());
  FetchThumbnails(std::move(ids), /* is_first_attempt= */ true);
}

void PrefetchDispatcherImpl::DownloadCompleted(
    const PrefetchDownloadResult& download_result) {
  if (!service_->GetPrefetchConfiguration()->IsPrefetchingEnabled())
    return;

  service_->GetLogger()->RecordActivity(
      "Download " + download_result.download_id +
      (download_result.success ? "succeeded" : "failed"));
  if (download_result.success) {
    service_->GetLogger()->RecordActivity(
        "Download size: " + std::to_string(download_result.file_size));
  }

  task_queue_.AddTask(std::make_unique<DownloadCompletedTask>(
      this, service_->GetPrefetchStore(), download_result));
  task_queue_.AddTask(std::make_unique<ImportArchivesTask>(
      service_->GetPrefetchStore(), service_->GetPrefetchImporter()));
}

void PrefetchDispatcherImpl::ItemDownloaded(int64_t offline_id,
                                            const ClientId& client_id) {
  auto ids = std::make_unique<IdsVector>();
  ids->emplace_back(offline_id, client_id);
  FetchThumbnails(std::move(ids), /* is_first_attempt= */ false);
}

void PrefetchDispatcherImpl::ArchiveImported(int64_t offline_id, bool success) {
  DCHECK_NE(OfflinePageModel::kInvalidOfflineId, offline_id);

  if (!service_->GetPrefetchConfiguration()->IsPrefetchingEnabled())
    return;

  service_->GetLogger()->RecordActivity("Importing archive " +
                                        std::to_string(offline_id) +
                                        (success ? "succeeded" : "failed"));

  if (success)
    service_->GetOfflineMetricsCollector()->OnSuccessfulPagePrefetch();

  task_queue_.AddTask(std::make_unique<ImportCompletedTask>(
      this, service_->GetPrefetchStore(), service_->GetPrefetchImporter(),
      offline_id, success));
}

void PrefetchDispatcherImpl::LogRequestResult(
    const std::string& request_name_for_logging,
    PrefetchRequestStatus status,
    const std::string& operation_name,
    const std::vector<RenderPageInfo>& pages) {
  service_->GetLogger()->RecordActivity(
      "Finished " + request_name_for_logging +
      " for operation: " + operation_name +
      " with status: " + std::to_string(static_cast<int>(status)) +
      "; included " + std::to_string(pages.size()) + " pages in result.");
  for (const RenderPageInfo& page : pages) {
    service_->GetLogger()->RecordActivity(
        "Response for page: " + page.url +
        "; status=" + std::to_string(static_cast<int>(page.status)));
  }
}

void PrefetchDispatcherImpl::FetchThumbnails(
    std::unique_ptr<PrefetchDispatcher::IdsVector> remaining_ids,
    bool is_first_attempt) {
  if (remaining_ids->empty())
    return;

  int64_t offline_id = remaining_ids->back().first;
  ClientId client_id = std::move(remaining_ids->back().second);
  DCHECK(client_id.name_space == kSuggestedArticlesNamespace);
  remaining_ids->pop_back();

  service_->GetOfflinePageModel()->HasThumbnailForOfflineId(
      offline_id,
      base::BindOnce(&PrefetchDispatcherImpl::ThumbnailExistenceChecked,
                     base::Unretained(this), offline_id, std::move(client_id),
                     std::move(remaining_ids), is_first_attempt));
}

void PrefetchDispatcherImpl::ThumbnailExistenceChecked(
    const int64_t offline_id,
    ClientId client_id,
    std::unique_ptr<PrefetchDispatcher::IdsVector> remaining_ids,
    bool is_first_attempt,
    bool thumbnail_exists) {
  if (thumbnail_exists) {
    FetchThumbnails(std::move(remaining_ids), is_first_attempt);
  } else {
    auto complete_callback = base::BindOnce(
        &PrefetchDispatcherImpl::ThumbnailFetchComplete, base::Unretained(this),
        offline_id, std::move(remaining_ids), is_first_attempt);
    service_->GetThumbnailFetcher()->FetchSuggestionImageData(
        client_id, is_first_attempt, std::move(complete_callback));
  }
}

void PrefetchDispatcherImpl::ThumbnailFetchComplete(
    const int64_t offline_id,
    std::unique_ptr<PrefetchDispatcher::IdsVector> remaining_ids,
    bool is_first_attempt,
    const std::string& image_data) {
  // Thumbnails are marked to expire after this delta. Expired thumbnails are
  // eventually deleted if their offline_id does not correspond to an offline
  // item. Two days gives us plenty of time so that the prefetched item can be
  // imported into the offline item database.
  const base::TimeDelta kThumbnailExpirationDelta =
      base::TimeDelta::FromDays(2);

  if (!image_data.empty()) {
    service_->GetOfflinePageModel()->StoreThumbnail(OfflinePageThumbnail(
        offline_id, base::Time::Now() + kThumbnailExpirationDelta, image_data));
  }
  FetchThumbnails(std::move(remaining_ids), is_first_attempt);
}

}  // namespace offline_pages