blob: f9c484cd719eacd4a9f35dca58c0e07851e55593 [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
17#ifndef METRICS_UPLOADER_SYSTEM_PROFILE_CACHE_H_
18#define METRICS_UPLOADER_SYSTEM_PROFILE_CACHE_H_
19
Ben Chanf05ab402014-08-07 00:54:59 -070020#include <stdint.h>
21
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -080022#include <memory>
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070023#include <string>
24
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070025#include "base/compiler_specific.h"
Bertrand SIMONNET12531862015-08-31 11:11:57 -070026#include "base/files/file_path.h"
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070027#include "base/gtest_prod_util.h"
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -070028#include "persistent_integer.h"
29#include "uploader/proto/system_profile.pb.h"
30#include "uploader/system_profile_setter.h"
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070031
32namespace metrics {
33class ChromeUserMetricsExtension;
34}
35
36struct SystemProfile {
Bertrand SIMONNET52e1d552015-08-04 15:06:21 -070037 std::string version;
Bertrand SIMONNETe6b96d62015-10-30 13:04:40 -070038 std::string model_manifest_id;
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070039 std::string client_id;
Bertrand SIMONNET52e1d552015-08-04 15:06:21 -070040 int session_id;
41 metrics::SystemProfileProto::Channel channel;
Bertrand SIMONNET7beaf892015-09-24 11:22:27 -070042 std::string product_id;
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070043};
44
45// Retrieves general system informations needed by the protobuf for context and
46// remembers them to avoid expensive calls.
47//
48// The cache is populated lazily. The only method needed is Populate.
49class SystemProfileCache : public SystemProfileSetter {
50 public:
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -070051 SystemProfileCache();
52
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -070053 SystemProfileCache(bool testing, const base::FilePath& metrics_directory);
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070054
55 // Populates the ProfileSystem protobuf with system information.
Bertrand SIMONNET1f146552015-08-19 18:13:02 -070056 bool Populate(metrics::ChromeUserMetricsExtension* metrics_proto) override;
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070057
Bertrand SIMONNET52e1d552015-08-04 15:06:21 -070058 // Converts a string representation of the channel to a
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070059 // SystemProfileProto_Channel
60 static metrics::SystemProfileProto_Channel ProtoChannelFromString(
61 const std::string& channel);
62
63 // Gets the persistent GUID and create it if it has not been created yet.
64 static std::string GetPersistentGUID(const std::string& filename);
65
66 private:
67 friend class UploadServiceTest;
68 FRIEND_TEST(UploadServiceTest, ExtractChannelFromDescription);
69 FRIEND_TEST(UploadServiceTest, ReadKeyValueFromFile);
70 FRIEND_TEST(UploadServiceTest, SessionIdIncrementedAtInitialization);
71 FRIEND_TEST(UploadServiceTest, ValuesInConfigFileAreSent);
Bertrand SIMONNET1d15d462015-11-17 13:20:06 -080072 FRIEND_TEST(UploadServiceTest, ProductIdMandatory);
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070073
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070074 // Fetches all informations and populates |profile_|
75 bool Initialize();
76
77 // Initializes |profile_| only if it has not been yet initialized.
78 bool InitializeOrCheck();
79
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070080 bool initialized_;
Bertrand SIMONNETeb815be2014-09-11 07:38:17 -070081 bool testing_;
Bertrand SIMONNET2765d0a2015-09-09 10:38:20 -070082 base::FilePath metrics_directory_;
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -080083 std::unique_ptr<chromeos_metrics::PersistentInteger> session_id_;
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070084 SystemProfile profile_;
85};
86
87#endif // METRICS_UPLOADER_SYSTEM_PROFILE_CACHE_H_