Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 1 | // 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 Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 10 | #include <string> |
| 11 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
| 13 | #include "base/gtest_prod_util.h" |
| 14 | #include "base/memory/scoped_ptr.h" |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 15 | #include "chromeos/osrelease_reader.h" |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 16 | #include "components/metrics/proto/system_profile.pb.h" |
| 17 | #include "metrics/persistent_integer.h" |
| 18 | #include "metrics/uploader/system_profile_setter.h" |
| 19 | |
| 20 | namespace metrics { |
| 21 | class ChromeUserMetricsExtension; |
| 22 | } |
| 23 | |
| 24 | struct 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 Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 31 | int32_t session_id; |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 32 | int32_t product_id; |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 33 | }; |
| 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. |
| 39 | class SystemProfileCache : public SystemProfileSetter { |
| 40 | public: |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 41 | SystemProfileCache(); |
| 42 | |
| 43 | SystemProfileCache(bool testing, const std::string& config_root); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 44 | |
| 45 | // Populates the ProfileSystem protobuf with system information. |
Alex Vakulenko | e8a8e30 | 2014-08-14 12:56:21 -0700 | [diff] [blame] | 46 | void Populate(metrics::ChromeUserMetricsExtension* profile_proto) override; |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 47 | |
| 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 SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 63 | // 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 SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 72 | // Gets the product ID from the GOOGLE_METRICS_PRODUCT_ID field. |
| 73 | bool GetProductId(int* product_id) const; |
| 74 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 75 | bool initialized_; |
Bertrand SIMONNET | eb815be | 2014-09-11 07:38:17 -0700 | [diff] [blame] | 76 | bool testing_; |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 77 | std::string config_root_; |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 78 | scoped_ptr<chromeos_metrics::PersistentInteger> session_id_; |
| 79 | SystemProfile profile_; |
| 80 | }; |
| 81 | |
| 82 | #endif // METRICS_UPLOADER_SYSTEM_PROFILE_CACHE_H_ |