grt | e29f8ab | 2016-11-15 22:26:30 | [diff] [blame] | 1 | // Copyright 2016 The Chromium 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 | #include "chrome/install_static/install_details.h" |
| 6 | |
Scott Graham | 332af9c | 2017-07-28 15:49:51 | [diff] [blame] | 7 | #include <assert.h> |
grt | e29f8ab | 2016-11-15 22:26:30 | [diff] [blame] | 8 | #include <string.h> |
| 9 | |
| 10 | #include <type_traits> |
| 11 | |
| 12 | #include "chrome/install_static/install_modes.h" |
Scott Graham | 332af9c | 2017-07-28 15:49:51 | [diff] [blame] | 13 | #include "chrome/install_static/install_util.h" |
grt | e29f8ab | 2016-11-15 22:26:30 | [diff] [blame] | 14 | #include "chrome_elf/nt_registry/nt_registry.h" |
| 15 | #include "components/version_info/version_info_values.h" |
| 16 | |
| 17 | namespace install_static { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // The version is set at compile-time to a W.X.Y.Z value. |
| 22 | constexpr char kProductVersion[] = PRODUCT_VERSION; |
| 23 | |
| 24 | // This module's instance (intentionally leaked at shutdown). |
grt | 97674ee | 2017-02-13 15:57:06 | [diff] [blame] | 25 | const InstallDetails* g_module_details = nullptr; |
grt | e29f8ab | 2016-11-15 22:26:30 | [diff] [blame] | 26 | |
| 27 | } // namespace |
| 28 | |
grt | 8b09030 | 2017-01-11 11:55:38 | [diff] [blame] | 29 | std::wstring InstallDetails::GetClientStateKeyPath() const { |
| 30 | return install_static::GetClientStateKeyPath(app_guid()); |
grt | e29f8ab | 2016-11-15 22:26:30 | [diff] [blame] | 31 | } |
| 32 | |
grt | 8b09030 | 2017-01-11 11:55:38 | [diff] [blame] | 33 | std::wstring InstallDetails::GetClientStateMediumKeyPath() const { |
| 34 | return install_static::GetClientStateMediumKeyPath(app_guid()); |
grt | e29f8ab | 2016-11-15 22:26:30 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | bool InstallDetails::VersionMismatch() const { |
| 38 | // Check the product version and the size of the mode structure. |
| 39 | return payload_->size != sizeof(Payload) || |
| 40 | strcmp(payload_->product_version, &kProductVersion[0]) != 0 || |
| 41 | payload_->mode->size != sizeof(InstallConstants); |
| 42 | } |
| 43 | |
| 44 | // static |
| 45 | const InstallDetails& InstallDetails::Get() { |
| 46 | assert(g_module_details); |
| 47 | return *g_module_details; |
| 48 | } |
| 49 | |
| 50 | // static |
| 51 | void InstallDetails::SetForProcess( |
| 52 | std::unique_ptr<PrimaryInstallDetails> details) { |
| 53 | assert(!details || !g_module_details); |
| 54 | // Tests may set then reset via null. In this case, delete the old instance. |
| 55 | delete g_module_details; |
| 56 | // Intentionally leaked at shutdown. |
| 57 | g_module_details = details.release(); |
| 58 | } |
| 59 | |
| 60 | // static |
| 61 | const InstallDetails::Payload* InstallDetails::GetPayload() { |
| 62 | assert(g_module_details); |
| 63 | static_assert(std::is_pod<Payload>::value, "Payload must be a POD-struct"); |
| 64 | static_assert(std::is_pod<InstallConstants>::value, |
| 65 | "InstallConstants must be a POD-struct"); |
| 66 | return g_module_details->payload_; |
| 67 | } |
| 68 | |
| 69 | // static |
grt | a47e865 | 2017-01-18 23:45:06 | [diff] [blame] | 70 | void InstallDetails::InitializeFromPayload( |
| 71 | const InstallDetails::Payload* payload) { |
grt | e29f8ab | 2016-11-15 22:26:30 | [diff] [blame] | 72 | assert(!g_module_details); |
grt | e29f8ab | 2016-11-15 22:26:30 | [diff] [blame] | 73 | // Intentionally leaked at shutdown. |
grt | a47e865 | 2017-01-18 23:45:06 | [diff] [blame] | 74 | g_module_details = new InstallDetails(payload); |
grt | e29f8ab | 2016-11-15 22:26:30 | [diff] [blame] | 75 | } |
| 76 | |
grt | 97674ee | 2017-02-13 15:57:06 | [diff] [blame] | 77 | // static |
| 78 | std::unique_ptr<const InstallDetails> InstallDetails::Swap( |
| 79 | std::unique_ptr<const InstallDetails> install_details) { |
| 80 | std::unique_ptr<const InstallDetails> previous_value(g_module_details); |
| 81 | g_module_details = install_details.release(); |
| 82 | return previous_value; |
| 83 | } |
| 84 | |
grt | e29f8ab | 2016-11-15 22:26:30 | [diff] [blame] | 85 | PrimaryInstallDetails::PrimaryInstallDetails() : InstallDetails(&payload_) { |
| 86 | payload_.size = sizeof(payload_); |
| 87 | payload_.product_version = &kProductVersion[0]; |
| 88 | } |
| 89 | |
grt | e29f8ab | 2016-11-15 22:26:30 | [diff] [blame] | 90 | } // namespace install_static |