summaryrefslogtreecommitdiff
path: root/chromium/components/bookmarks/browser/model_loader.cc
blob: 12fe8d32547f3a5ac96c8b11041b2229801c871d (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
// Copyright 2018 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/bookmarks/browser/model_loader.h"

#include "base/bind.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/bookmarks/browser/bookmark_storage.h"
#include "components/bookmarks/browser/url_index.h"

namespace bookmarks {

namespace {

// TODO(mastiz): Remove this kill switch asap since the UMA metrics entail
// negligible risks for stability or performance overhead.
const base::Feature kEmitExperimentalBookmarkLoadUma{
    "EmitExperimentalBookmarkLoadUma", base::FEATURE_ENABLED_BY_DEFAULT};

}  // namespace

// static
scoped_refptr<ModelLoader> ModelLoader::Create(
    const base::FilePath& profile_path,
    base::SequencedTaskRunner* load_sequenced_task_runner,
    std::unique_ptr<BookmarkLoadDetails> details,
    LoadCallback callback) {
  // Note: base::MakeRefCounted is not available here, as ModelLoader's
  // constructor is private.
  auto model_loader = base::WrapRefCounted(new ModelLoader());
  // We plumb the value for kEmitExperimentalBookmarkLoadUma as retrieved on
  // the UI thread to avoid issues with TSAN bots (in case there are tests that
  // override feature toggles -not necessarily this one- while bookmark loading
  // is ongoing, which is problematic due to how feature overriding for tests is
  // implemented).
  load_sequenced_task_runner->PostTask(
      FROM_HERE,
      base::BindOnce(
          &ModelLoader::DoLoadOnBackgroundThread, model_loader, profile_path,
          base::FeatureList::IsEnabled(kEmitExperimentalBookmarkLoadUma),
          base::ThreadTaskRunnerHandle::Get(), std::move(details),
          std::move(callback)));
  return model_loader;
}

void ModelLoader::BlockTillLoaded() {
  loaded_signal_.Wait();
}

ModelLoader::ModelLoader()
    : loaded_signal_(base::WaitableEvent::ResetPolicy::MANUAL,
                     base::WaitableEvent::InitialState::NOT_SIGNALED) {}

ModelLoader::~ModelLoader() = default;

void ModelLoader::DoLoadOnBackgroundThread(
    const base::FilePath& profile_path,
    bool emit_experimental_uma,
    scoped_refptr<base::SequencedTaskRunner> main_sequenced_task_runner,
    std::unique_ptr<BookmarkLoadDetails> details,
    LoadCallback callback) {
  LoadBookmarks(profile_path, emit_experimental_uma, details.get());
  history_bookmark_model_ = details->url_index();
  loaded_signal_.Signal();
  main_sequenced_task_runner->PostTask(
      FROM_HERE, base::BindOnce(&ModelLoader::OnFinishedLoad, this,
                                std::move(details), std::move(callback)));
}

void ModelLoader::OnFinishedLoad(std::unique_ptr<BookmarkLoadDetails> details,
                                 LoadCallback callback) {
  std::move(callback).Run(std::move(details));
}

}  // namespace bookmarks