blob: 95f554f8ed310f97f92b136920f0c41dc0e38e9c [file] [log] [blame]
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -07001// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef METRICS_UPLOADER_SYSTEM_PROFILE_CACHE_H_
6#define METRICS_UPLOADER_SYSTEM_PROFILE_CACHE_H_
7
Ben Chanf05ab402014-08-07 00:54:59 -07008#include <stdint.h>
9
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070010#include <string>
11
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070012#include "base/compiler_specific.h"
13#include "base/gtest_prod_util.h"
14#include "base/memory/scoped_ptr.h"
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -070015#include "chromeos/osrelease_reader.h"
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070016#include "components/metrics/proto/system_profile.pb.h"
17#include "metrics/persistent_integer.h"
18#include "metrics/uploader/system_profile_setter.h"
19
20namespace metrics {
21class ChromeUserMetricsExtension;
22}
23
24struct SystemProfile {
25 std::string os_name;
26 std::string os_version;
27 metrics::SystemProfileProto::Channel channel;
28 std::string app_version;
29 std::string hardware_class;
30 std::string client_id;
Ben Chanf05ab402014-08-07 00:54:59 -070031 int32_t session_id;
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -070032 int32_t product_id;
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070033};
34
35// Retrieves general system informations needed by the protobuf for context and
36// remembers them to avoid expensive calls.
37//
38// The cache is populated lazily. The only method needed is Populate.
39class SystemProfileCache : public SystemProfileSetter {
40 public:
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -070041 SystemProfileCache();
42
43 SystemProfileCache(bool testing, const std::string& config_root);
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070044
45 // Populates the ProfileSystem protobuf with system information.
Alex Vakulenkoe8a8e302014-08-14 12:56:21 -070046 void Populate(metrics::ChromeUserMetricsExtension* profile_proto) override;
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070047
48 // Converts a string representation of the channel (|channel|-channel) to a
49 // SystemProfileProto_Channel
50 static metrics::SystemProfileProto_Channel ProtoChannelFromString(
51 const std::string& channel);
52
53 // Gets the persistent GUID and create it if it has not been created yet.
54 static std::string GetPersistentGUID(const std::string& filename);
55
56 private:
57 friend class UploadServiceTest;
58 FRIEND_TEST(UploadServiceTest, ExtractChannelFromDescription);
59 FRIEND_TEST(UploadServiceTest, ReadKeyValueFromFile);
60 FRIEND_TEST(UploadServiceTest, SessionIdIncrementedAtInitialization);
61 FRIEND_TEST(UploadServiceTest, ValuesInConfigFileAreSent);
62
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070063 // Fetches all informations and populates |profile_|
64 bool Initialize();
65
66 // Initializes |profile_| only if it has not been yet initialized.
67 bool InitializeOrCheck();
68
69 // Gets the hardware ID using crossystem
70 bool GetHardwareId(std::string* hwid);
71
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -070072 // Gets the product ID from the GOOGLE_METRICS_PRODUCT_ID field.
73 bool GetProductId(int* product_id) const;
74
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070075 bool initialized_;
Bertrand SIMONNETeb815be2014-09-11 07:38:17 -070076 bool testing_;
Bertrand SIMONNET71a62ef2014-10-07 11:26:25 -070077 std::string config_root_;
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070078 scoped_ptr<chromeos_metrics::PersistentInteger> session_id_;
79 SystemProfile profile_;
80};
81
82#endif // METRICS_UPLOADER_SYSTEM_PROFILE_CACHE_H_