blob: 8384b31214ebbd197e99d3cfac7ab500e5024e51 [file] [log] [blame]
grte29f8ab2016-11-15 22:26:301// 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 Graham332af9c2017-07-28 15:49:517#include <assert.h>
grte29f8ab2016-11-15 22:26:308#include <string.h>
9
10#include <type_traits>
11
12#include "chrome/install_static/install_modes.h"
Scott Graham332af9c2017-07-28 15:49:5113#include "chrome/install_static/install_util.h"
grte29f8ab2016-11-15 22:26:3014#include "chrome_elf/nt_registry/nt_registry.h"
15#include "components/version_info/version_info_values.h"
16
17namespace install_static {
18
19namespace {
20
21// The version is set at compile-time to a W.X.Y.Z value.
22constexpr char kProductVersion[] = PRODUCT_VERSION;
23
24// This module's instance (intentionally leaked at shutdown).
grt97674ee2017-02-13 15:57:0625const InstallDetails* g_module_details = nullptr;
grte29f8ab2016-11-15 22:26:3026
27} // namespace
28
grt8b090302017-01-11 11:55:3829std::wstring InstallDetails::GetClientStateKeyPath() const {
30 return install_static::GetClientStateKeyPath(app_guid());
grte29f8ab2016-11-15 22:26:3031}
32
grt8b090302017-01-11 11:55:3833std::wstring InstallDetails::GetClientStateMediumKeyPath() const {
34 return install_static::GetClientStateMediumKeyPath(app_guid());
grte29f8ab2016-11-15 22:26:3035}
36
37bool 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
45const InstallDetails& InstallDetails::Get() {
46 assert(g_module_details);
47 return *g_module_details;
48}
49
50// static
51void 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
61const 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
grta47e8652017-01-18 23:45:0670void InstallDetails::InitializeFromPayload(
71 const InstallDetails::Payload* payload) {
grte29f8ab2016-11-15 22:26:3072 assert(!g_module_details);
grte29f8ab2016-11-15 22:26:3073 // Intentionally leaked at shutdown.
grta47e8652017-01-18 23:45:0674 g_module_details = new InstallDetails(payload);
grte29f8ab2016-11-15 22:26:3075}
76
grt97674ee2017-02-13 15:57:0677// static
78std::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
grte29f8ab2016-11-15 22:26:3085PrimaryInstallDetails::PrimaryInstallDetails() : InstallDetails(&payload_) {
86 payload_.size = sizeof(payload_);
87 payload_.product_version = &kProductVersion[0];
88}
89
grte29f8ab2016-11-15 22:26:3090} // namespace install_static