summaryrefslogtreecommitdiff
path: root/chromium/components/reporting/storage_selector/storage_selector.cc
blob: 98867c6fa8a08f59862750b1af08211cd82d9a8d (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
// Copyright 2020 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/reporting/storage_selector/storage_selector.h"

#include <memory>
#include <utility>

#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "base/path_service.h"
#include "base/task/bind_post_task.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "components/reporting/compression/compression_module.h"
#include "components/reporting/encryption/encryption_module.h"
#include "components/reporting/storage/storage_module_interface.h"
#include "components/reporting/storage/storage_uploader_interface.h"
#include "components/reporting/util/status.h"
#include "components/reporting/util/status_macros.h"
#include "components/reporting/util/statusor.h"

#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
#include "chromeos/dbus/missive/missive_client.h"
#include "components/reporting/storage/missive_storage_module.h"
#include "components/reporting/storage/missive_storage_module_delegate_impl.h"

using ::chromeos::MissiveClient;

#endif  // BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)

namespace reporting {

namespace {

#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
// Features settings for storage and uploader.
// Use `missived` by all browsers.
const base::Feature kUseMissiveDaemonFeature{StorageSelector::kUseMissiveDaemon,
                                             base::FEATURE_ENABLED_BY_DEFAULT};

// Receive `missived` uploads by ASH/primary browser only.
const base::Feature kProvideUploaderFeature {
  StorageSelector::kProvideUploader,
#if BUILDFLAG(IS_CHROMEOS_ASH)
      base::FEATURE_ENABLED_BY_DEFAULT
#else   // BUILDFLAG(IS_CHROMEOS_LACROS)
      base::FEATURE_DISABLED_BY_DEFAULT
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)
};
#endif  // BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)

}  // namespace

#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
// static
const char StorageSelector::kUseMissiveDaemon[] = "ConnectMissiveDaemon";
// static
const char StorageSelector::kProvideUploader[] = "ProvideUploader";
#endif  // BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)

// static
bool StorageSelector::is_uploader_required() {
#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
  return base::FeatureList::IsEnabled(kProvideUploaderFeature);
#else   // Not ChromeOS
  return true;   // Local storage must have an uploader.
#endif  // BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
}

// static
bool StorageSelector::is_use_missive() {
#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
  return base::FeatureList::IsEnabled(kUseMissiveDaemonFeature);
#else   // Not ChromeOS
  return false;  // Use Local storage.
#endif  // BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
}

#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
// static
void StorageSelector::CreateMissiveStorageModule(
    base::OnceCallback<void(StatusOr<scoped_refptr<StorageModuleInterface>>)>
        cb) {
  MissiveClient* const missive_client = MissiveClient::Get();
  if (!missive_client) {
    std::move(cb).Run(Status(
        error::FAILED_PRECONDITION,
        "Missive Client unavailable, probably has not been initialized"));
    return;
  }
  // Refer to the storage module.
  auto missive_storage_module_delegate =
      std::make_unique<MissiveStorageModuleDelegateImpl>(
          base::BindPostTask(missive_client->origin_task_runner(),
                             base::BindRepeating(&MissiveClient::EnqueueRecord,
                                                 missive_client->GetWeakPtr())),
          base::BindPostTask(
              missive_client->origin_task_runner(),
              base::BindRepeating(&MissiveClient::Flush,
                                  missive_client->GetWeakPtr())));
  auto missive_storage_module =
      MissiveStorageModule::Create(std::move(missive_storage_module_delegate));
  if (!missive_storage_module) {
    std::move(cb).Run(Status(error::FAILED_PRECONDITION,
                             "Missive Storage Module failed to create"));
    return;
  }
  LOG(WARNING) << "Store reporting data by a Missive daemon";
  std::move(cb).Run(missive_storage_module);
  return;
}
#endif  // BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
}  // namespace reporting