blob: e6f66170244ab14f0b9a175fa04a7b637bdaa56e [file] [log] [blame]
Bertrand SIMONNET52e5b992015-08-10 15:18:00 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070016
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -070017#include "uploader/system_profile_cache.h"
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070018
Bertrand SIMONNETbd3505e2015-08-04 14:04:51 -070019#include <base/files/file_util.h>
20#include <base/guid.h>
21#include <base/logging.h>
22#include <base/strings/string_number_conversions.h>
23#include <base/strings/string_util.h>
Bertrand SIMONNETeb697ab2015-10-14 13:26:42 -070024#include <brillo/osrelease_reader.h>
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070025#include <string>
Bertrand SIMONNETeb697ab2015-10-14 13:26:42 -070026#include <update_engine/client.h>
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070027#include <vector>
28
Bertrand SIMONNETbd3505e2015-08-04 14:04:51 -070029#include "constants.h"
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -070030#include "persistent_integer.h"
31#include "uploader/metrics_log_base.h"
32#include "uploader/proto/chrome_user_metrics_extension.pb.h"
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070033
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -070034namespace {
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070035
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -070036const char kPersistentSessionIdFilename[] = "Sysinfo.SessionId";
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -070037
38} // namespace
39
Bertrand SIMONNETa8bcc182014-12-19 09:35:15 -080040std::string ChannelToString(
41 const metrics::SystemProfileProto_Channel& channel) {
42 switch (channel) {
43 case metrics::SystemProfileProto::CHANNEL_STABLE:
44 return "STABLE";
45 case metrics::SystemProfileProto::CHANNEL_DEV:
46 return "DEV";
47 case metrics::SystemProfileProto::CHANNEL_BETA:
48 return "BETA";
49 case metrics::SystemProfileProto::CHANNEL_CANARY:
50 return "CANARY";
51 default:
52 return "UNKNOWN";
53 }
54}
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -070055
56SystemProfileCache::SystemProfileCache()
57 : initialized_(false),
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080058 testing_(false),
Bertrand SIMONNET9d3a4ae2015-11-25 13:49:12 -080059 metrics_directory_(metrics::kMetricsdDirectory),
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080060 session_id_(new chromeos_metrics::PersistentInteger(
61 kPersistentSessionIdFilename, metrics_directory_)) {}
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -070062
63SystemProfileCache::SystemProfileCache(bool testing,
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -070064 const base::FilePath& metrics_directory)
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070065 : initialized_(false),
Bertrand SIMONNETeb815be2014-09-11 07:38:17 -070066 testing_(testing),
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -070067 metrics_directory_(metrics_directory),
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070068 session_id_(new chromeos_metrics::PersistentInteger(
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080069 kPersistentSessionIdFilename, metrics_directory)) {}
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070070
71bool SystemProfileCache::Initialize() {
72 CHECK(!initialized_)
73 << "this should be called only once in the metrics_daemon lifetime.";
74
Bertrand SIMONNETeb697ab2015-10-14 13:26:42 -070075 brillo::OsReleaseReader reader;
76 std::string channel;
77 if (testing_) {
78 reader.LoadTestingOnly(metrics_directory_);
79 channel = "unknown";
80 } else {
81 reader.Load();
82 auto client = update_engine::UpdateEngineClient::CreateInstance();
Bertrand SIMONNETf1aa3722016-01-22 13:15:54 -080083 if (!client) {
84 LOG(ERROR) << "failed to create the update engine client";
85 return false;
86 }
Bertrand SIMONNETeb697ab2015-10-14 13:26:42 -070087 if (!client->GetChannel(&channel)) {
88 LOG(ERROR) << "failed to read the current channel from update engine.";
Bertrand SIMONNET608e4282015-11-12 17:52:17 -080089 return false;
Bertrand SIMONNETeb697ab2015-10-14 13:26:42 -070090 }
91 }
Bertrand SIMONNET26993622015-08-20 14:08:41 -070092
Bertrand SIMONNET1d15d462015-11-17 13:20:06 -080093 if (!reader.GetString(metrics::kProductId, &profile_.product_id)
94 || profile_.product_id.empty()) {
Bertrand SIMONNETeb697ab2015-10-14 13:26:42 -070095 LOG(ERROR) << "product_id is not set.";
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070096 return false;
97 }
98
Bertrand SIMONNETeb697ab2015-10-14 13:26:42 -070099 if (!reader.GetString(metrics::kProductVersion, &profile_.version)) {
100 LOG(ERROR) << "failed to read the product version";
101 }
Bertrand SIMONNET26993622015-08-20 14:08:41 -0700102
103 if (channel.empty() || profile_.version.empty()) {
Bertrand SIMONNETbae5dcc2015-08-04 14:12:10 -0700104 // If the channel or version is missing, the image is not official.
105 // In this case, set the channel to unknown and the version to 0.0.0.0 to
106 // avoid polluting the production data.
107 channel = "";
108 profile_.version = metrics::kDefaultVersion;
Bertrand SIMONNETbae5dcc2015-08-04 14:12:10 -0700109 }
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -0700110 std::string guid_path = metrics_directory_.Append(
111 metrics::kMetricsGUIDFileName).value();
112 profile_.client_id = testing_ ?
113 "client_id_test" :
114 GetPersistentGUID(guid_path);
Bertrand SIMONNETe6b96d62015-10-30 13:04:40 -0700115 profile_.model_manifest_id = "unknown";
116 if (!testing_) {
117 brillo::KeyValueStore weave_config;
118 if (!weave_config.Load(base::FilePath(metrics::kWeaveConfigurationFile))) {
119 LOG(ERROR) << "Failed to load the weave configuration file.";
120 } else if (!weave_config.GetString(metrics::kModelManifestId,
121 &profile_.model_manifest_id)) {
122 LOG(ERROR) << "The model manifest id (model_id) is undefined in "
123 << metrics::kWeaveConfigurationFile;
124 }
125 }
126
Bertrand SIMONNET52e1d552015-08-04 15:06:21 -0700127 profile_.channel = ProtoChannelFromString(channel);
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700128
129 // Increment the session_id everytime we initialize this. If metrics_daemon
130 // does not crash, this should correspond to the number of reboots of the
131 // system.
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700132 session_id_->Add(1);
Ben Chanf05ab402014-08-07 00:54:59 -0700133 profile_.session_id = static_cast<int32_t>(session_id_->Get());
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700134
135 initialized_ = true;
136 return initialized_;
137}
138
139bool SystemProfileCache::InitializeOrCheck() {
140 return initialized_ || Initialize();
141}
142
Bertrand SIMONNET1f146552015-08-19 18:13:02 -0700143bool SystemProfileCache::Populate(
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700144 metrics::ChromeUserMetricsExtension* metrics_proto) {
145 CHECK(metrics_proto);
Bertrand SIMONNET1f146552015-08-19 18:13:02 -0700146 if (not InitializeOrCheck()) {
147 return false;
148 }
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700149
150 // The client id is hashed before being sent.
151 metrics_proto->set_client_id(
152 metrics::MetricsLogBase::Hash(profile_.client_id));
153 metrics_proto->set_session_id(profile_.session_id);
154
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -0700155 // Sets the product id.
Bertrand SIMONNET52e1d552015-08-04 15:06:21 -0700156 metrics_proto->set_product(9);
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -0700157
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700158 metrics::SystemProfileProto* profile_proto =
159 metrics_proto->mutable_system_profile();
160 profile_proto->mutable_hardware()->set_hardware_class(
Bertrand SIMONNETe6b96d62015-10-30 13:04:40 -0700161 profile_.model_manifest_id);
Bertrand SIMONNET52e1d552015-08-04 15:06:21 -0700162 profile_proto->set_app_version(profile_.version);
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700163 profile_proto->set_channel(profile_.channel);
Bertrand SIMONNET52e1d552015-08-04 15:06:21 -0700164 metrics::SystemProfileProto_BrilloDeviceData* device_data =
165 profile_proto->mutable_brillo();
Bertrand SIMONNET7dc78272015-10-06 15:27:37 -0700166 device_data->set_product_id(profile_.product_id);
Bertrand SIMONNET1f146552015-08-19 18:13:02 -0700167
168 return true;
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700169}
170
Bertrand SIMONNET52e1d552015-08-04 15:06:21 -0700171std::string SystemProfileCache::GetPersistentGUID(
172 const std::string& filename) {
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700173 std::string guid;
174 base::FilePath filepath(filename);
175 if (!base::ReadFileToString(filepath, &guid)) {
176 guid = base::GenerateGUID();
177 // If we can't read or write the file, the guid will not be preserved during
178 // the next reboot. Crash.
179 CHECK(base::WriteFile(filepath, guid.c_str(), guid.size()));
180 }
181 return guid;
182}
183
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700184metrics::SystemProfileProto_Channel SystemProfileCache::ProtoChannelFromString(
185 const std::string& channel) {
Bertrand SIMONNETdc225c82015-11-12 14:04:03 -0800186 if (channel == "stable-channel") {
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700187 return metrics::SystemProfileProto::CHANNEL_STABLE;
Bertrand SIMONNETdc225c82015-11-12 14:04:03 -0800188 } else if (channel == "dev-channel") {
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700189 return metrics::SystemProfileProto::CHANNEL_DEV;
Bertrand SIMONNETdc225c82015-11-12 14:04:03 -0800190 } else if (channel == "beta-channel") {
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700191 return metrics::SystemProfileProto::CHANNEL_BETA;
Bertrand SIMONNETdc225c82015-11-12 14:04:03 -0800192 } else if (channel == "canary-channel") {
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -0700193 return metrics::SystemProfileProto::CHANNEL_CANARY;
194 }
195
196 DLOG(INFO) << "unknown channel: " << channel;
197 return metrics::SystemProfileProto::CHANNEL_UNKNOWN;
198}