OLD | NEW |
---|---|
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/installer/util/module_util_win.h" | 5 #include "chrome/installer/util/module_util_win.h" |
6 | 6 |
7 #include <memory> | 7 #include <Windows.h> |
8 | |
9 #include <string.h> | |
8 | 10 |
9 #include "base/base_paths.h" | 11 #include "base/base_paths.h" |
10 #include "base/file_version_info.h" | |
11 #include "base/files/file.h" | 12 #include "base/files/file.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/path_service.h" | 14 #include "base/path_service.h" |
14 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
16 #include "base/strings/stringprintf.h" | |
15 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
16 #include "base/version.h" | 18 #include "base/version.h" |
17 #include "base/win/current_module.h" | 19 #include "base/win/current_module.h" |
20 #include "base/win/resource_util.h" | |
18 | 21 |
19 namespace installer { | 22 namespace installer { |
20 | 23 |
21 namespace { | 24 namespace { |
22 | 25 |
26 // This structure is not declared anywhere in the SDK, but is documented at | |
fdoray
2016/06/06 14:40:58
I copied this from third_party/crashpad/crashpad/s
grt (UTC plus 2)
2016/06/06 15:42:56
Many current uses of CreateFileVersionInfoForModul
fdoray
2016/06/17 20:18:45
Done.
The documentation of ::VerQueryValue() [1]
| |
27 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms647001.aspx. | |
28 struct VS_VERSIONINFO { | |
29 WORD wLength; | |
30 WORD wValueLength; | |
31 WORD wType; | |
32 | |
33 // The structure documentation on MSDN doesn’t show the [16], but it does | |
34 // say that it’s supposed to be L"VS_VERSION_INFO", which is is in fact a | |
35 // 16-character string (including its NUL terminator). | |
36 WCHAR szKey[16]; | |
37 | |
38 WORD Padding1; | |
39 VS_FIXEDFILEINFO Value; | |
40 | |
41 // Don’t include Children or the Padding2 that precedes it, because they may | |
42 // not be present. | |
43 // WORD Padding2; | |
44 // WORD Children; | |
45 }; | |
46 | |
23 // Returns the version in the current executable's version resource. | 47 // Returns the version in the current executable's version resource. |
24 base::string16 GetCurrentExecutableVersion() { | 48 base::string16 GetCurrentExecutableVersion() { |
25 std::unique_ptr<FileVersionInfo> file_version_info( | 49 VS_VERSIONINFO* version_info; |
26 FileVersionInfo::CreateFileVersionInfoForModule(CURRENT_MODULE())); | 50 size_t version_info_length; |
27 DCHECK(file_version_info.get()); | 51 const bool has_version_resource = base::win::GetResourceFromModule( |
28 base::string16 version_string(file_version_info->file_version()); | 52 CURRENT_MODULE(), VS_VERSION_INFO, RT_VERSION, |
53 reinterpret_cast<void**>(&version_info), &version_info_length); | |
54 DCHECK(has_version_resource); | |
55 DCHECK_LE(sizeof(*version_info), version_info_length); | |
56 DCHECK_LE(sizeof(*version_info), version_info->wLength); | |
57 DCHECK_EQ(version_info_length, version_info->wLength); | |
58 DCHECK_EQ(version_info->wValueLength, sizeof(version_info->Value)); | |
59 DCHECK_EQ(version_info->wType, 0); | |
60 DCHECK_EQ(wcsncmp(version_info->szKey, L"VS_VERSION_INFO", | |
61 arraysize(version_info->szKey)), | |
62 0); | |
63 | |
64 const uint16_t version_0 = version_info->Value.dwFileVersionMS >> 16; | |
65 const uint16_t version_1 = version_info->Value.dwFileVersionMS & 0xffff; | |
66 const uint16_t version_2 = version_info->Value.dwFileVersionLS >> 16; | |
67 const uint16_t version_3 = version_info->Value.dwFileVersionLS & 0xffff; | |
68 | |
69 const base::string16 version_string(base::StringPrintf( | |
70 L"%hu.%hu.%hu.%hu", version_0, version_1, version_2, version_3)); | |
29 DCHECK(base::Version(base::UTF16ToASCII(version_string)).IsValid()); | 71 DCHECK(base::Version(base::UTF16ToASCII(version_string)).IsValid()); |
30 return version_string; | 72 return version_string; |
31 } | 73 } |
32 | 74 |
33 // Indicates whether a file can be opened using the same flags that | 75 // Indicates whether a file can be opened using the same flags that |
34 // ::LoadLibrary() uses to open modules. | 76 // ::LoadLibrary() uses to open modules. |
35 bool ModuleCanBeRead(const base::FilePath file_path) { | 77 bool ModuleCanBeRead(const base::FilePath& file_path) { |
36 return base::File(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ) | 78 return base::File(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ) |
37 .IsValid(); | 79 .IsValid(); |
38 } | 80 } |
39 | 81 |
40 } // namespace | 82 } // namespace |
41 | 83 |
42 base::FilePath GetModulePath(base::StringPiece16 module_name) { | 84 base::FilePath GetModulePath(base::StringPiece16 module_name) { |
43 base::FilePath exe_dir; | 85 base::FilePath exe_dir; |
44 const bool has_path = base::PathService::Get(base::DIR_EXE, &exe_dir); | 86 const bool has_path = base::PathService::Get(base::DIR_EXE, &exe_dir); |
45 DCHECK(has_path); | 87 DCHECK(has_path); |
46 | 88 |
47 // Look for the module in the current executable's directory and return the | 89 // Look for the module in the current executable's directory and return the |
48 // path if it can be read. This is the expected location of modules for dev | 90 // path if it can be read. This is the expected location of modules for dev |
49 // builds. | 91 // builds. |
50 const base::FilePath module_path = exe_dir.Append(module_name); | 92 const base::FilePath module_path = exe_dir.Append(module_name); |
51 if (ModuleCanBeRead(module_path)) | 93 if (ModuleCanBeRead(module_path)) |
52 return module_path; | 94 return module_path; |
53 | 95 |
54 // Othwerwise, return the path to the module in a versioned sub-directory of | 96 // Othwerwise, return the path to the module in a versioned sub-directory of |
55 // the current executable's directory. This is the expected location of | 97 // the current executable's directory. This is the expected location of |
56 // modules for proper installs. | 98 // modules for proper installs. |
57 const base::string16 version = GetCurrentExecutableVersion(); | 99 const base::string16 version = GetCurrentExecutableVersion(); |
58 DCHECK(!version.empty()); | 100 DCHECK(!version.empty()); |
59 return exe_dir.Append(version).Append(module_name); | 101 return exe_dir.Append(version).Append(module_name); |
60 } | 102 } |
61 | 103 |
62 } // namespace installer | 104 } // namespace installer |
OLD | NEW |