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 | |
| 17 | #ifndef METRICS_UPLOADER_SYSTEM_PROFILE_CACHE_H_ |
| 18 | #define METRICS_UPLOADER_SYSTEM_PROFILE_CACHE_H_ |
| 19 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | |
Bertrand SIMONNET | 6c9fbb9 | 2015-12-21 14:56:40 -0800 | [diff] [blame] | 22 | #include <memory> |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 25 | #include "base/compiler_specific.h" |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 26 | #include "base/files/file_path.h" |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 27 | #include "base/gtest_prod_util.h" |
Bertrand SIMONNET | 4b915ae | 2015-07-28 15:38:14 -0700 | [diff] [blame] | 28 | #include "persistent_integer.h" |
| 29 | #include "uploader/proto/system_profile.pb.h" |
| 30 | #include "uploader/system_profile_setter.h" |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 31 | |
| 32 | namespace metrics { |
| 33 | class ChromeUserMetricsExtension; |
| 34 | } |
| 35 | |
| 36 | struct SystemProfile { |
Bertrand SIMONNET | 52e1d55 | 2015-08-04 15:06:21 -0700 | [diff] [blame] | 37 | std::string version; |
Bertrand SIMONNET | e6b96d6 | 2015-10-30 13:04:40 -0700 | [diff] [blame] | 38 | std::string model_manifest_id; |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 39 | std::string client_id; |
Bertrand SIMONNET | 52e1d55 | 2015-08-04 15:06:21 -0700 | [diff] [blame] | 40 | int session_id; |
| 41 | metrics::SystemProfileProto::Channel channel; |
Bertrand SIMONNET | 7beaf89 | 2015-09-24 11:22:27 -0700 | [diff] [blame] | 42 | std::string product_id; |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 43 | }; |
| 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. |
| 49 | class SystemProfileCache : public SystemProfileSetter { |
| 50 | public: |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 51 | SystemProfileCache(); |
| 52 | |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 53 | SystemProfileCache(bool testing, const base::FilePath& metrics_directory); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 54 | |
| 55 | // Populates the ProfileSystem protobuf with system information. |
Bertrand SIMONNET | 1f14655 | 2015-08-19 18:13:02 -0700 | [diff] [blame] | 56 | bool Populate(metrics::ChromeUserMetricsExtension* metrics_proto) override; |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 57 | |
Bertrand SIMONNET | 52e1d55 | 2015-08-04 15:06:21 -0700 | [diff] [blame] | 58 | // Converts a string representation of the channel to a |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 59 | // 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 SIMONNET | 1d15d46 | 2015-11-17 13:20:06 -0800 | [diff] [blame] | 72 | FRIEND_TEST(UploadServiceTest, ProductIdMandatory); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 73 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 74 | // 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 SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 80 | bool initialized_; |
Bertrand SIMONNET | eb815be | 2014-09-11 07:38:17 -0700 | [diff] [blame] | 81 | bool testing_; |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 82 | base::FilePath metrics_directory_; |
Bertrand SIMONNET | 6c9fbb9 | 2015-12-21 14:56:40 -0800 | [diff] [blame] | 83 | std::unique_ptr<chromeos_metrics::PersistentInteger> session_id_; |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 84 | SystemProfile profile_; |
| 85 | }; |
| 86 | |
| 87 | #endif // METRICS_UPLOADER_SYSTEM_PROFILE_CACHE_H_ |