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), |
| 59 | metrics_directory_(metrics::kMetricsDirectory), |
| 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(); |
| 83 | if (!client->GetChannel(&channel)) { |
| 84 | LOG(ERROR) << "failed to read the current channel from update engine."; |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 85 | return false; |
Bertrand SIMONNET | eb697ab | 2015-10-14 13:26:42 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
Bertrand SIMONNET | 2699362 | 2015-08-20 14:08:41 -0700 | [diff] [blame] | 88 | |
Bertrand SIMONNET | 1d15d46 | 2015-11-17 13:20:06 -0800 | [diff] [blame] | 89 | if (!reader.GetString(metrics::kProductId, &profile_.product_id) |
| 90 | || profile_.product_id.empty()) { |
Bertrand SIMONNET | eb697ab | 2015-10-14 13:26:42 -0700 | [diff] [blame] | 91 | LOG(ERROR) << "product_id is not set."; |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | |
Bertrand SIMONNET | eb697ab | 2015-10-14 13:26:42 -0700 | [diff] [blame] | 95 | if (!reader.GetString(metrics::kProductVersion, &profile_.version)) { |
| 96 | LOG(ERROR) << "failed to read the product version"; |
| 97 | } |
Bertrand SIMONNET | 2699362 | 2015-08-20 14:08:41 -0700 | [diff] [blame] | 98 | |
| 99 | if (channel.empty() || profile_.version.empty()) { |
Bertrand SIMONNET | bae5dcc | 2015-08-04 14:12:10 -0700 | [diff] [blame] | 100 | // If the channel or version is missing, the image is not official. |
| 101 | // In this case, set the channel to unknown and the version to 0.0.0.0 to |
| 102 | // avoid polluting the production data. |
| 103 | channel = ""; |
| 104 | profile_.version = metrics::kDefaultVersion; |
Bertrand SIMONNET | bae5dcc | 2015-08-04 14:12:10 -0700 | [diff] [blame] | 105 | } |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 106 | std::string guid_path = metrics_directory_.Append( |
| 107 | metrics::kMetricsGUIDFileName).value(); |
| 108 | profile_.client_id = testing_ ? |
| 109 | "client_id_test" : |
| 110 | GetPersistentGUID(guid_path); |
Bertrand SIMONNET | e6b96d6 | 2015-10-30 13:04:40 -0700 | [diff] [blame] | 111 | profile_.model_manifest_id = "unknown"; |
| 112 | if (!testing_) { |
| 113 | brillo::KeyValueStore weave_config; |
| 114 | if (!weave_config.Load(base::FilePath(metrics::kWeaveConfigurationFile))) { |
| 115 | LOG(ERROR) << "Failed to load the weave configuration file."; |
| 116 | } else if (!weave_config.GetString(metrics::kModelManifestId, |
| 117 | &profile_.model_manifest_id)) { |
| 118 | LOG(ERROR) << "The model manifest id (model_id) is undefined in " |
| 119 | << metrics::kWeaveConfigurationFile; |
| 120 | } |
| 121 | } |
| 122 | |
Bertrand SIMONNET | 52e1d55 | 2015-08-04 15:06:21 -0700 | [diff] [blame] | 123 | profile_.channel = ProtoChannelFromString(channel); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 124 | |
| 125 | // Increment the session_id everytime we initialize this. If metrics_daemon |
| 126 | // does not crash, this should correspond to the number of reboots of the |
| 127 | // system. |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 128 | session_id_->Add(1); |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 129 | profile_.session_id = static_cast<int32_t>(session_id_->Get()); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 130 | |
| 131 | initialized_ = true; |
| 132 | return initialized_; |
| 133 | } |
| 134 | |
| 135 | bool SystemProfileCache::InitializeOrCheck() { |
| 136 | return initialized_ || Initialize(); |
| 137 | } |
| 138 | |
Bertrand SIMONNET | 1f14655 | 2015-08-19 18:13:02 -0700 | [diff] [blame] | 139 | bool SystemProfileCache::Populate( |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 140 | metrics::ChromeUserMetricsExtension* metrics_proto) { |
| 141 | CHECK(metrics_proto); |
Bertrand SIMONNET | 1f14655 | 2015-08-19 18:13:02 -0700 | [diff] [blame] | 142 | if (not InitializeOrCheck()) { |
| 143 | return false; |
| 144 | } |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 145 | |
| 146 | // The client id is hashed before being sent. |
| 147 | metrics_proto->set_client_id( |
| 148 | metrics::MetricsLogBase::Hash(profile_.client_id)); |
| 149 | metrics_proto->set_session_id(profile_.session_id); |
| 150 | |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 151 | // Sets the product id. |
Bertrand SIMONNET | 52e1d55 | 2015-08-04 15:06:21 -0700 | [diff] [blame] | 152 | metrics_proto->set_product(9); |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 153 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 154 | metrics::SystemProfileProto* profile_proto = |
| 155 | metrics_proto->mutable_system_profile(); |
| 156 | profile_proto->mutable_hardware()->set_hardware_class( |
Bertrand SIMONNET | e6b96d6 | 2015-10-30 13:04:40 -0700 | [diff] [blame] | 157 | profile_.model_manifest_id); |
Bertrand SIMONNET | 52e1d55 | 2015-08-04 15:06:21 -0700 | [diff] [blame] | 158 | profile_proto->set_app_version(profile_.version); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 159 | profile_proto->set_channel(profile_.channel); |
Bertrand SIMONNET | 52e1d55 | 2015-08-04 15:06:21 -0700 | [diff] [blame] | 160 | metrics::SystemProfileProto_BrilloDeviceData* device_data = |
| 161 | profile_proto->mutable_brillo(); |
Bertrand SIMONNET | 7dc7827 | 2015-10-06 15:27:37 -0700 | [diff] [blame] | 162 | device_data->set_product_id(profile_.product_id); |
Bertrand SIMONNET | 1f14655 | 2015-08-19 18:13:02 -0700 | [diff] [blame] | 163 | |
| 164 | return true; |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Bertrand SIMONNET | 52e1d55 | 2015-08-04 15:06:21 -0700 | [diff] [blame] | 167 | std::string SystemProfileCache::GetPersistentGUID( |
| 168 | const std::string& filename) { |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 169 | std::string guid; |
| 170 | base::FilePath filepath(filename); |
| 171 | if (!base::ReadFileToString(filepath, &guid)) { |
| 172 | guid = base::GenerateGUID(); |
| 173 | // If we can't read or write the file, the guid will not be preserved during |
| 174 | // the next reboot. Crash. |
| 175 | CHECK(base::WriteFile(filepath, guid.c_str(), guid.size())); |
| 176 | } |
| 177 | return guid; |
| 178 | } |
| 179 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 180 | metrics::SystemProfileProto_Channel SystemProfileCache::ProtoChannelFromString( |
| 181 | const std::string& channel) { |
Bertrand SIMONNET | dc225c8 | 2015-11-12 14:04:03 -0800 | [diff] [blame] | 182 | if (channel == "stable-channel") { |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 183 | return metrics::SystemProfileProto::CHANNEL_STABLE; |
Bertrand SIMONNET | dc225c8 | 2015-11-12 14:04:03 -0800 | [diff] [blame] | 184 | } else if (channel == "dev-channel") { |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 185 | return metrics::SystemProfileProto::CHANNEL_DEV; |
Bertrand SIMONNET | dc225c8 | 2015-11-12 14:04:03 -0800 | [diff] [blame] | 186 | } else if (channel == "beta-channel") { |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 187 | return metrics::SystemProfileProto::CHANNEL_BETA; |
Bertrand SIMONNET | dc225c8 | 2015-11-12 14:04:03 -0800 | [diff] [blame] | 188 | } else if (channel == "canary-channel") { |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 189 | return metrics::SystemProfileProto::CHANNEL_CANARY; |
| 190 | } |
| 191 | |
| 192 | DLOG(INFO) << "unknown channel: " << channel; |
| 193 | return metrics::SystemProfileProto::CHANNEL_UNKNOWN; |
| 194 | } |