Bertrand SIMONNET | 52e5b99 | 2015-08-10 15:18:00 -0700 | [diff] [blame] | 1 | /* |
| 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 SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 16 | |
Bertrand SIMONNET | 4b915ae | 2015-07-28 15:38:14 -0700 | [diff] [blame] | 17 | #include "uploader/system_profile_cache.h" |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 18 | |
Bertrand SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame] | 19 | #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 SIMONNET | eb697ab | 2015-10-14 13:26:42 -0700 | [diff] [blame] | 24 | #include <brillo/osrelease_reader.h> |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 25 | #include <string> |
Bertrand SIMONNET | eb697ab | 2015-10-14 13:26:42 -0700 | [diff] [blame] | 26 | #include <update_engine/client.h> |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
Bertrand SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame] | 29 | #include "constants.h" |
Bertrand SIMONNET | 4b915ae | 2015-07-28 15:38:14 -0700 | [diff] [blame] | 30 | #include "persistent_integer.h" |
| 31 | #include "uploader/metrics_log_base.h" |
| 32 | #include "uploader/proto/chrome_user_metrics_extension.pb.h" |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 33 | |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 34 | namespace { |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 35 | |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 36 | const char kPersistentSessionIdFilename[] = "Sysinfo.SessionId"; |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 37 | |
| 38 | } // namespace |
| 39 | |
Bertrand SIMONNET | a8bcc18 | 2014-12-19 09:35:15 -0800 | [diff] [blame] | 40 | std::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 SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 55 | |
| 56 | SystemProfileCache::SystemProfileCache() |
| 57 | : initialized_(false), |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 58 | testing_(false), |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 59 | metrics_directory_(metrics::kMetricsdDirectory), |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 60 | session_id_(new chromeos_metrics::PersistentInteger( |
| 61 | kPersistentSessionIdFilename, metrics_directory_)) {} |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 62 | |
| 63 | SystemProfileCache::SystemProfileCache(bool testing, |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 64 | const base::FilePath& metrics_directory) |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 65 | : initialized_(false), |
Bertrand SIMONNET | eb815be | 2014-09-11 07:38:17 -0700 | [diff] [blame] | 66 | testing_(testing), |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 67 | metrics_directory_(metrics_directory), |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 68 | session_id_(new chromeos_metrics::PersistentInteger( |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 69 | kPersistentSessionIdFilename, metrics_directory)) {} |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 70 | |
| 71 | bool SystemProfileCache::Initialize() { |
| 72 | CHECK(!initialized_) |
| 73 | << "this should be called only once in the metrics_daemon lifetime."; |
| 74 | |
Bertrand SIMONNET | eb697ab | 2015-10-14 13:26:42 -0700 | [diff] [blame] | 75 | 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 SIMONNET | f1aa372 | 2016-01-22 13:15:54 -0800 | [diff] [blame] | 83 | if (!client) { |
| 84 | LOG(ERROR) << "failed to create the update engine client"; |
| 85 | return false; |
| 86 | } |
Bertrand SIMONNET | eb697ab | 2015-10-14 13:26:42 -0700 | [diff] [blame] | 87 | if (!client->GetChannel(&channel)) { |
| 88 | LOG(ERROR) << "failed to read the current channel from update engine."; |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 89 | return false; |
Bertrand SIMONNET | eb697ab | 2015-10-14 13:26:42 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
Bertrand SIMONNET | 2699362 | 2015-08-20 14:08:41 -0700 | [diff] [blame] | 92 | |
Bertrand SIMONNET | 1d15d46 | 2015-11-17 13:20:06 -0800 | [diff] [blame] | 93 | if (!reader.GetString(metrics::kProductId, &profile_.product_id) |
| 94 | || profile_.product_id.empty()) { |
Bertrand SIMONNET | eb697ab | 2015-10-14 13:26:42 -0700 | [diff] [blame] | 95 | LOG(ERROR) << "product_id is not set."; |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | |
Bertrand SIMONNET | eb697ab | 2015-10-14 13:26:42 -0700 | [diff] [blame] | 99 | if (!reader.GetString(metrics::kProductVersion, &profile_.version)) { |
| 100 | LOG(ERROR) << "failed to read the product version"; |
| 101 | } |
Bertrand SIMONNET | 2699362 | 2015-08-20 14:08:41 -0700 | [diff] [blame] | 102 | |
| 103 | if (channel.empty() || profile_.version.empty()) { |
Bertrand SIMONNET | bae5dcc | 2015-08-04 14:12:10 -0700 | [diff] [blame] | 104 | // 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 SIMONNET | bae5dcc | 2015-08-04 14:12:10 -0700 | [diff] [blame] | 109 | } |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 110 | 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 SIMONNET | e6b96d6 | 2015-10-30 13:04:40 -0700 | [diff] [blame] | 115 | 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 SIMONNET | 52e1d55 | 2015-08-04 15:06:21 -0700 | [diff] [blame] | 127 | profile_.channel = ProtoChannelFromString(channel); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 128 | |
| 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 SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 132 | session_id_->Add(1); |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 133 | profile_.session_id = static_cast<int32_t>(session_id_->Get()); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 134 | |
| 135 | initialized_ = true; |
| 136 | return initialized_; |
| 137 | } |
| 138 | |
| 139 | bool SystemProfileCache::InitializeOrCheck() { |
| 140 | return initialized_ || Initialize(); |
| 141 | } |
| 142 | |
Bertrand SIMONNET | 1f14655 | 2015-08-19 18:13:02 -0700 | [diff] [blame] | 143 | bool SystemProfileCache::Populate( |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 144 | metrics::ChromeUserMetricsExtension* metrics_proto) { |
| 145 | CHECK(metrics_proto); |
Bertrand SIMONNET | 1f14655 | 2015-08-19 18:13:02 -0700 | [diff] [blame] | 146 | if (not InitializeOrCheck()) { |
| 147 | return false; |
| 148 | } |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 149 | |
| 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 SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 155 | // Sets the product id. |
Bertrand SIMONNET | 52e1d55 | 2015-08-04 15:06:21 -0700 | [diff] [blame] | 156 | metrics_proto->set_product(9); |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 157 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 158 | metrics::SystemProfileProto* profile_proto = |
| 159 | metrics_proto->mutable_system_profile(); |
| 160 | profile_proto->mutable_hardware()->set_hardware_class( |
Bertrand SIMONNET | e6b96d6 | 2015-10-30 13:04:40 -0700 | [diff] [blame] | 161 | profile_.model_manifest_id); |
Bertrand SIMONNET | 52e1d55 | 2015-08-04 15:06:21 -0700 | [diff] [blame] | 162 | profile_proto->set_app_version(profile_.version); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 163 | profile_proto->set_channel(profile_.channel); |
Bertrand SIMONNET | 52e1d55 | 2015-08-04 15:06:21 -0700 | [diff] [blame] | 164 | metrics::SystemProfileProto_BrilloDeviceData* device_data = |
| 165 | profile_proto->mutable_brillo(); |
Bertrand SIMONNET | 7dc7827 | 2015-10-06 15:27:37 -0700 | [diff] [blame] | 166 | device_data->set_product_id(profile_.product_id); |
Bertrand SIMONNET | 1f14655 | 2015-08-19 18:13:02 -0700 | [diff] [blame] | 167 | |
| 168 | return true; |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Bertrand SIMONNET | 52e1d55 | 2015-08-04 15:06:21 -0700 | [diff] [blame] | 171 | std::string SystemProfileCache::GetPersistentGUID( |
| 172 | const std::string& filename) { |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 173 | 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 SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 184 | metrics::SystemProfileProto_Channel SystemProfileCache::ProtoChannelFromString( |
| 185 | const std::string& channel) { |
Bertrand SIMONNET | dc225c8 | 2015-11-12 14:04:03 -0800 | [diff] [blame] | 186 | if (channel == "stable-channel") { |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 187 | return metrics::SystemProfileProto::CHANNEL_STABLE; |
Bertrand SIMONNET | dc225c8 | 2015-11-12 14:04:03 -0800 | [diff] [blame] | 188 | } else if (channel == "dev-channel") { |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 189 | return metrics::SystemProfileProto::CHANNEL_DEV; |
Bertrand SIMONNET | dc225c8 | 2015-11-12 14:04:03 -0800 | [diff] [blame] | 190 | } else if (channel == "beta-channel") { |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 191 | return metrics::SystemProfileProto::CHANNEL_BETA; |
Bertrand SIMONNET | dc225c8 | 2015-11-12 14:04:03 -0800 | [diff] [blame] | 192 | } else if (channel == "canary-channel") { |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 193 | return metrics::SystemProfileProto::CHANNEL_CANARY; |
| 194 | } |
| 195 | |
| 196 | DLOG(INFO) << "unknown channel: " << channel; |
| 197 | return metrics::SystemProfileProto::CHANNEL_UNKNOWN; |
| 198 | } |