File: download_offline_content_provider.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (636 lines) | stat: -rw-r--r-- 22,190 bytes parent folder | download | duplicates (3)
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/download/download_offline_content_provider.h"

#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/notimplemented.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/download/offline_item_utils.h"
#include "chrome/browser/offline_items_collection/offline_content_aggregator_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/thumbnail/generator/image_thumbnail_request.h"
#include "components/download/public/common/download_danger_type.h"
#include "components/download/public/common/download_features.h"
#include "components/download/public/common/download_item.h"
#include "components/safe_browsing/buildflags.h"
#include "content/public/browser/browser_context.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"

#if BUILDFLAG(IS_ANDROID)
#include "base/android/build_info.h"
#include "chrome/browser/download/android/download_controller.h"
#include "chrome/browser/download/android/download_manager_bridge.h"
#include "chrome/browser/download/android/download_manager_service.h"
#include "chrome/browser/download/android/download_utils.h"
#include "chrome/browser/download/android/open_download_dialog_bridge_delegate.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/flags/android/chrome_feature_list.h"
#include "components/pdf/common/constants.h"
#include "content/public/browser/download_item_utils.h"
#include "content/public/browser/download_manager_delegate.h"
#include "content/public/common/content_features.h"
#include "ui/base/device_form_factor.h"

#if BUILDFLAG(SAFE_BROWSING_DOWNLOAD_PROTECTION)
#include "chrome/browser/download/download_ui_safe_browsing_util.h"
#include "chrome/browser/safe_browsing/download_protection/download_protection_util.h"
#endif  // BUILDFLAG(SAFE_BROWSING_DOWNLOAD_PROTECTION)
#endif  // BUILDFLAG(IS_ANDROID)

using OfflineItemFilter = offline_items_collection::OfflineItemFilter;
using OfflineItemState = offline_items_collection::OfflineItemState;
using OfflineItemProgressUnit =
    offline_items_collection::OfflineItemProgressUnit;
using offline_items_collection::OfflineItemShareInfo;
using OfflineItemVisuals = offline_items_collection::OfflineItemVisuals;
using UpdateDelta = offline_items_collection::UpdateDelta;

namespace {

// Thumbnail size used for generating thumbnails for image files.
const int kThumbnailSizeInDP = 64;

// The delay to wait after loading history and before starting the check for
// externally removed downloads.
const base::TimeDelta kCheckExternallyRemovedDownloadsDelay =
    base::Milliseconds(100);

#if BUILDFLAG(IS_ANDROID)
// Invalid system download Id.
const int kInvalidSystemDownloadId = -1;

#endif

bool ShouldShowDownloadItem(const DownloadItem* item) {
  return !item->IsTemporary() && !item->IsTransient() &&
         !item->GetTargetFilePath().empty();
}

std::unique_ptr<OfflineItemShareInfo> CreateShareInfo(
    const DownloadItem* item) {
  auto share_info = std::make_unique<OfflineItemShareInfo>();
#if BUILDFLAG(IS_ANDROID)
  if (item) {
    share_info->uri =
        DownloadUtils::GetUriStringForPath(item->GetTargetFilePath());
  }
#else
  NOTIMPLEMENTED();
#endif
  return share_info;
}

// Observes the all downloads, primrarily responsible for cleaning up the
// externally removed downloads, and notifying the provider about download
// deletions. Only used for android.
class AllDownloadObserver
    : public download::AllDownloadEventNotifier::Observer {
 public:
  explicit AllDownloadObserver(DownloadOfflineContentProvider* provider);

  AllDownloadObserver(const AllDownloadObserver&) = delete;
  AllDownloadObserver& operator=(const AllDownloadObserver&) = delete;

  ~AllDownloadObserver() override;

  void OnDownloadUpdated(SimpleDownloadManagerCoordinator* manager,
                         DownloadItem* item) override;
  void OnDownloadRemoved(SimpleDownloadManagerCoordinator* manager,
                         DownloadItem* item) override;

 private:
  void DeleteDownloadItem(SimpleDownloadManagerCoordinator* manager,
                          const std::string& guid);

  raw_ptr<DownloadOfflineContentProvider> provider_;
  base::WeakPtrFactory<AllDownloadObserver> weak_ptr_factory_{this};
};

AllDownloadObserver::AllDownloadObserver(
    DownloadOfflineContentProvider* provider)
    : provider_(provider) {}

AllDownloadObserver::~AllDownloadObserver() = default;

void AllDownloadObserver::OnDownloadUpdated(
    SimpleDownloadManagerCoordinator* manager,
    DownloadItem* item) {
  if (item->GetFileExternallyRemoved()) {
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(&AllDownloadObserver::DeleteDownloadItem,
                                  weak_ptr_factory_.GetWeakPtr(), manager,
                                  item->GetGuid()));
  }
}

void AllDownloadObserver::OnDownloadRemoved(
    SimpleDownloadManagerCoordinator* manager,
    DownloadItem* item) {
  provider_->OnDownloadRemoved(item);
}

void AllDownloadObserver::DeleteDownloadItem(
    SimpleDownloadManagerCoordinator* manager,
    const std::string& guid) {
  DownloadItem* item = manager->GetDownloadByGuid(guid);
  if (item)
    item->Remove();
}

}  // namespace

DownloadOfflineContentProvider::DownloadOfflineContentProvider(
    OfflineContentAggregator* aggregator,
    const std::string& name_space)
    : aggregator_(aggregator),
      name_space_(name_space),
      manager_(nullptr),
      checked_for_externally_removed_downloads_(false),
      state_(State::UNINITIALIZED),
      profile_(nullptr) {
  aggregator_->RegisterProvider(name_space_, this);
#if BUILDFLAG(IS_ANDROID)
  all_download_observer_ = std::make_unique<AllDownloadObserver>(this);
#endif
}

DownloadOfflineContentProvider::~DownloadOfflineContentProvider() {
  aggregator_->UnregisterProvider(name_space_);
  if (manager_) {
    manager_->RemoveObserver(this);
    if (all_download_observer_)
      manager_->GetNotifier()->RemoveObserver(all_download_observer_.get());
  }
}

void DownloadOfflineContentProvider::SetSimpleDownloadManagerCoordinator(
    SimpleDownloadManagerCoordinator* manager) {
  DCHECK(manager);
  if (manager_ == manager)
    return;

  manager_ = manager;
  manager_->AddObserver(this);

  if (all_download_observer_)
    manager_->GetNotifier()->AddObserver(all_download_observer_.get());
}

void DownloadOfflineContentProvider::OnDownloadsInitialized(
    bool active_downloads_only) {
  state_ = active_downloads_only ? State::ACTIVE_DOWNLOADS_ONLY
                                 : State::HISTORY_LOADED;

  while (!pending_actions_for_reduced_mode_.empty()) {
    auto callback = std::move(pending_actions_for_reduced_mode_.front());
    pending_actions_for_reduced_mode_.pop_front();
    std::move(callback).Run();
  }

  if (state_ != State::HISTORY_LOADED)
    return;

  while (!pending_actions_for_full_browser_.empty()) {
    auto callback = std::move(pending_actions_for_full_browser_.front());
    pending_actions_for_full_browser_.pop_front();
    std::move(callback).Run();
  }

  base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce(
          &DownloadOfflineContentProvider::CheckForExternallyRemovedDownloads,
          weak_ptr_factory_.GetWeakPtr()),
      kCheckExternallyRemovedDownloadsDelay);
}

// TODO(shaktisahu) : Pass DownloadOpenSource.
void DownloadOfflineContentProvider::OpenItem(const OpenParams& open_params,
                                              const ContentId& id) {
  EnsureDownloadCoreServiceStarted();
  if (state_ != State::HISTORY_LOADED) {
    pending_actions_for_full_browser_.push_back(
        base::BindOnce(&DownloadOfflineContentProvider::OpenItem,
                       weak_ptr_factory_.GetWeakPtr(), open_params, id));
    return;
  }

  DownloadItem* item = GetDownload(id.id);
  if (item)
    item->OpenDownload();
}

void DownloadOfflineContentProvider::RemoveItem(const ContentId& id) {
  EnsureDownloadCoreServiceStarted();
  if (state_ != State::HISTORY_LOADED) {
    pending_actions_for_full_browser_.push_back(
        base::BindOnce(&DownloadOfflineContentProvider::RemoveItem,
                       weak_ptr_factory_.GetWeakPtr(), id));
    return;
  }

  DownloadItem* item = GetDownload(id.id);
  if (item) {
    item->DeleteFile(base::DoNothing());
    item->Remove();
  }
}

void DownloadOfflineContentProvider::CancelDownload(const ContentId& id) {
  if (state_ == State::UNINITIALIZED) {
    pending_actions_for_reduced_mode_.push_back(
        base::BindOnce(&DownloadOfflineContentProvider::CancelDownload,
                       weak_ptr_factory_.GetWeakPtr(), id));
    return;
  }

  DownloadItem* item = GetDownload(id.id);
  if (item)
    item->Cancel(true);
}

void DownloadOfflineContentProvider::PauseDownload(const ContentId& id) {
  if (state_ == State::UNINITIALIZED) {
    pending_actions_for_reduced_mode_.push_back(
        base::BindOnce(&DownloadOfflineContentProvider::PauseDownload,
                       weak_ptr_factory_.GetWeakPtr(), id));
    return;
  }

  DownloadItem* item = GetDownload(id.id);
  if (item)
    item->Pause();
}

void DownloadOfflineContentProvider::ResumeDownload(const ContentId& id) {
  if (state_ == State::UNINITIALIZED) {
    pending_actions_for_reduced_mode_.push_back(
        base::BindOnce(&DownloadOfflineContentProvider::ResumeDownload,
                       weak_ptr_factory_.GetWeakPtr(), id));
    return;
  }

  DownloadItem* item = GetDownload(id.id);
  if (item)
    item->Resume(true /* user_resume */);
}

void DownloadOfflineContentProvider::ValidateDangerousDownload(
    const ContentId& id) {
  if (state_ == State::UNINITIALIZED) {
    pending_actions_for_reduced_mode_.push_back(base::BindOnce(
        &DownloadOfflineContentProvider::ValidateDangerousDownload,
        weak_ptr_factory_.GetWeakPtr(), id));
    return;
  }

  DownloadItem* item = GetDownload(id.id);
  if (item && !item->IsDone() && item->IsDangerous()) {
    item->ValidateDangerousDownload();
#if BUILDFLAG(SAFE_BROWSING_DOWNLOAD_PROTECTION) && BUILDFLAG(IS_ANDROID)
    SendSafeBrowsingDownloadReport(
        safe_browsing::ClientSafeBrowsingReportRequest::
            DANGEROUS_DOWNLOAD_WARNING_ANDROID,
        /*did_proceed=*/true, item);
#endif
  }
}

void DownloadOfflineContentProvider::GetItemById(
    const ContentId& id,
    OfflineContentProvider::SingleItemCallback callback) {
  EnsureDownloadCoreServiceStarted();
  base::OnceClosure run_get_item_callback =
      base::BindOnce(&DownloadOfflineContentProvider::RunGetItemByIdCallback,
                     weak_ptr_factory_.GetWeakPtr(), id, std::move(callback));
  if (state_ != State::HISTORY_LOADED) {
    pending_actions_for_full_browser_.push_back(
        std::move(run_get_item_callback));
    return;
  }

  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, std::move(run_get_item_callback));
}

void DownloadOfflineContentProvider::GetAllItems(
    OfflineContentProvider::MultipleItemCallback callback) {
  EnsureDownloadCoreServiceStarted();
  base::OnceClosure run_get_all_items_callback =
      base::BindOnce(&DownloadOfflineContentProvider::RunGetAllItemsCallback,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback));
  if (state_ != State::HISTORY_LOADED) {
    pending_actions_for_full_browser_.push_back(
        std::move(run_get_all_items_callback));
    return;
  }

  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, std::move(run_get_all_items_callback));
}

void DownloadOfflineContentProvider::GetVisualsForItem(
    const ContentId& id,
    GetVisualsOptions options,
    VisualsCallback callback) {
  // TODO(crbug.com/40581903) Supply thumbnail if item is visible.
  DownloadItem* item = GetDownload(id.id);
  display::Screen* screen = display::Screen::GetScreen();
  if (!item || !options.get_icon || !screen) {
    // No favicon is available; run the callback without visuals.
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(callback), id, nullptr));
    return;
  }

  display::Display display = screen->GetPrimaryDisplay();
  int icon_size = kThumbnailSizeInDP * display.device_scale_factor();

  auto request = std::make_unique<ImageThumbnailRequest>(
      icon_size,
      base::BindOnce(&DownloadOfflineContentProvider::OnThumbnailRetrieved,
                     weak_ptr_factory_.GetWeakPtr(), id, std::move(callback)));
  request->Start(item->GetTargetFilePath());

  // Dropping ownership of |request| here because it will clean itself up once
  // the started request finishes.
  request.release();
}

void DownloadOfflineContentProvider::GetShareInfoForItem(
    const ContentId& id,
    ShareCallback callback) {
  EnsureDownloadCoreServiceStarted();
  if (state_ != State::HISTORY_LOADED) {
    pending_actions_for_full_browser_.push_back(base::BindOnce(
        &DownloadOfflineContentProvider::GetShareInfoForItem,
        weak_ptr_factory_.GetWeakPtr(), id, std::move(callback)));
    return;
  }

  DownloadItem* item = GetDownload(id.id);
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(std::move(callback), id, CreateShareInfo(item)));
}

void DownloadOfflineContentProvider::OnThumbnailRetrieved(
    const ContentId& id,
    VisualsCallback callback,
    const SkBitmap& bitmap) {
  auto visuals = std::make_unique<OfflineItemVisuals>();
  visuals->icon = gfx::Image::CreateFrom1xBitmap(bitmap);
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(std::move(callback), id, std::move(visuals)));
}

void DownloadOfflineContentProvider::RenameItem(const ContentId& id,
                                                const std::string& name,
                                                RenameCallback callback) {
  EnsureDownloadCoreServiceStarted();
  if (state_ != State::HISTORY_LOADED) {
    pending_actions_for_full_browser_.push_back(base::BindOnce(
        &DownloadOfflineContentProvider::RenameItem,
        weak_ptr_factory_.GetWeakPtr(), id, name, std::move(callback)));
    return;
  }

  DownloadItem* item = GetDownload(id.id);
  if (!item) {
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE,
        base::BindOnce(std::move(callback), RenameResult::FAILURE_UNAVAILABLE));
    return;
  }
  download::DownloadItem::RenameDownloadCallback download_callback =
      base::BindOnce(
          &DownloadOfflineContentProvider::OnRenameDownloadCallbackDone,
          weak_ptr_factory_.GetWeakPtr(), std::move(callback), item);
  base::FilePath::StringType filename;
#if BUILDFLAG(IS_WIN)
  filename = base::UTF8ToWide(name);
#else
  filename = name;
#endif
  item->Rename(base::FilePath(filename), std::move(download_callback));
}

void DownloadOfflineContentProvider::OnRenameDownloadCallbackDone(
    RenameCallback callback,
    DownloadItem* item,
    DownloadItem::DownloadRenameResult result) {
  if (result == DownloadItem::DownloadRenameResult::SUCCESS) {
    auto offline_item = OfflineItemUtils::CreateOfflineItem(name_space_, item);
    UpdateDelta update_delta;
    update_delta.state_changed = false;
    update_delta.visuals_changed = false;
    UpdateObservers(offline_item, update_delta);
  }

  std::move(callback).Run(
      OfflineItemUtils::ConvertDownloadRenameResultToRenameResult(result));
}

void DownloadOfflineContentProvider::OnManagerGoingDown(
    SimpleDownloadManagerCoordinator* manager) {
  std::vector<raw_ptr<DownloadItem, VectorExperimental>> all_items;
  GetAllDownloads(&all_items);

  for (download::DownloadItem* item : all_items) {
    if (!ShouldShowDownloadItem(item))
      continue;
    NotifyItemRemoved(ContentId(name_space_, item->GetGuid()));
  }

  manager_ = nullptr;
}

void DownloadOfflineContentProvider::OnDownloadStarted(DownloadItem* item) {
  item->RemoveObserver(this);
  item->AddObserver(this);

  OnDownloadUpdated(item);
}

void DownloadOfflineContentProvider::OnDownloadUpdated(DownloadItem* item) {
  // Notify user if this download is blocked.
  bool should_notify =
      item->GetState() == DownloadItem::INTERRUPTED &&
      item->GetLastReason() ==
          download::DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED &&
      item->GetInsecureDownloadStatus() !=
          download::DownloadItem::InsecureDownloadStatus::SILENT_BLOCK;
  // Wait until the target path is determined or the download is canceled.
  if (!should_notify && item->GetTargetFilePath().empty() &&
      item->GetState() != DownloadItem::CANCELLED) {
    return;
  }

  if (!should_notify && !ShouldShowDownloadItem(item)) {
    return;
  }

  UpdateDelta update_delta;
  auto offline_item = OfflineItemUtils::CreateOfflineItem(name_space_, item);
  if (offline_item.state == OfflineItemState::COMPLETE ||
      offline_item.state == OfflineItemState::FAILED ||
      offline_item.state == OfflineItemState::CANCELLED) {
    // TODO(crbug.com/40616574): May be move this to DownloadItem.
    // Never call this for completed downloads from history.
    item->RemoveObserver(this);

    update_delta.state_changed = true;
    if (item->GetState() == DownloadItem::COMPLETE)
      AddCompletedDownload(item);
  }

  // If the download was newly marked dangerous or validated, then visuals may
  // change (e.g. new icon to denote dangerous status).
  if (offline_item.state != OfflineItemState::CANCELLED &&
      (item->IsDangerous() ||
       item->GetDangerType() ==
           download::DOWNLOAD_DANGER_TYPE_USER_VALIDATED)) {
    update_delta.visuals_changed = true;
  }

  UpdateObservers(offline_item, update_delta);
}

void DownloadOfflineContentProvider::OnDownloadRemoved(DownloadItem* item) {
  if (!ShouldShowDownloadItem(item))
    return;

#if BUILDFLAG(IS_ANDROID)
  DownloadManagerBridge::RemoveCompletedDownload(item);
#endif

  ContentId contentId(name_space_, item->GetGuid());
  NotifyItemRemoved(contentId);
}

void DownloadOfflineContentProvider::OnProfileCreated(Profile* profile) {
  profile_ = profile;
}

void DownloadOfflineContentProvider::AddCompletedDownload(DownloadItem* item) {
#if BUILDFLAG(IS_ANDROID)
  base::OnceCallback<void(int64_t)> cb =
      base::BindOnce(&DownloadOfflineContentProvider::AddCompletedDownloadDone,
                     weak_ptr_factory_.GetWeakPtr(), item->GetGuid());
  if (base::android::BuildInfo::GetInstance()->sdk_int() <
      base::android::SDK_VERSION_Q) {
    DownloadManagerBridge::AddCompletedDownload(item, std::move(cb));
  } else {
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(cb), kInvalidSystemDownloadId));
  }
#endif
}

void DownloadOfflineContentProvider::AddCompletedDownloadDone(
    const std::string& download_guid,
    int64_t system_download_id) {
#if BUILDFLAG(IS_ANDROID)
  DownloadItem* item = GetDownload(download_guid);
  if (!item)
    return;
  if (DownloadUtils::IsOmaDownloadDescription(item->GetMimeType())) {
    DownloadManagerService::GetInstance()->HandleOMADownload(
        item, system_download_id);
    return;
  }

  if (profile_ && profile_->GetDownloadManagerDelegate() &&
      profile_->GetDownloadManagerDelegate()->ShouldOpenPdfInline() &&
      item->GetMimeType() == pdf::kPDFMimeType) {
    return;
  }

  if (DownloadUtils::ShouldAutoOpenDownload(item)) {
    item->OpenDownload();
  } else if (item->IsFromExternalApp()) {
    if (item->GetMimeType() == pdf::kPDFMimeType) {
      if (profile_ &&
          DownloadPrefs::FromBrowserContext(profile_)->IsAutoOpenPdfEnabled()) {
        item->OpenDownload();
      } else {
        open_download_dialog_delegate_.CreateDialog(item);
      }
    }
  }
#endif
}

DownloadItem* DownloadOfflineContentProvider::GetDownload(
    const std::string& download_guid) {
  return manager_ ? manager_->GetDownloadByGuid(download_guid) : nullptr;
}

void DownloadOfflineContentProvider::GetAllDownloads(
    std::vector<raw_ptr<DownloadItem, VectorExperimental>>* all_items) {
  if (manager_)
    manager_->GetAllDownloads(all_items);
}

void DownloadOfflineContentProvider::UpdateObservers(
    const OfflineItem& item,
    const std::optional<UpdateDelta>& update_delta) {
  NotifyItemUpdated(item, update_delta);
}

void DownloadOfflineContentProvider::CheckForExternallyRemovedDownloads() {
  if (checked_for_externally_removed_downloads_ || !manager_)
    return;

  checked_for_externally_removed_downloads_ = true;

#if BUILDFLAG(IS_ANDROID)
  manager_->CheckForExternallyRemovedDownloads();
#endif
}

void DownloadOfflineContentProvider::EnsureDownloadCoreServiceStarted() {
  DCHECK(profile_);
  CHECK(profile_->GetDownloadManager());
}

void DownloadOfflineContentProvider::RunGetAllItemsCallback(
    OfflineContentProvider::MultipleItemCallback callback) {
  std::vector<raw_ptr<DownloadItem, VectorExperimental>> all_items;
  GetAllDownloads(&all_items);

  std::vector<OfflineItem> items;
  for (download::DownloadItem* item : all_items) {
    if (!ShouldShowDownloadItem(item))
      continue;
    items.push_back(OfflineItemUtils::CreateOfflineItem(name_space_, item));
  }
  std::move(callback).Run(std::move(items));
}

void DownloadOfflineContentProvider::RunGetItemByIdCallback(
    const ContentId& id,
    OfflineContentProvider::SingleItemCallback callback) {
  DownloadItem* item = GetDownload(id.id);
  auto offline_item =
      item && ShouldShowDownloadItem(item)
          ? std::make_optional(
                OfflineItemUtils::CreateOfflineItem(name_space_, item))
          : std::nullopt;

  std::move(callback).Run(offline_item);
}