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 <windows.h> | |
6 | |
7 #include "chrome/installer/util/module_util_win.h" | 5 #include "chrome/installer/util/module_util_win.h" |
8 | 6 |
| 7 #include "base/base_paths.h" |
9 #include "base/file_version_info.h" | 8 #include "base/file_version_info.h" |
10 #include "base/files/file.h" | 9 #include "base/files/file.h" |
11 #include "base/logging.h" | 10 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/path_service.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "base/version.h" | 14 #include "base/version.h" |
15 | 15 |
16 namespace installer { | 16 namespace installer { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // Returns the directory in which the currently running executable resides. | 20 // Returns the version in the current executable's version resource. |
21 base::FilePath GetExecutableDir() { | 21 base::string16 GetCurrentExecutableVersion() { |
22 base::char16 path[MAX_PATH]; | |
23 ::GetModuleFileNameW(nullptr, path, MAX_PATH); | |
24 return base::FilePath(path).DirName(); | |
25 } | |
26 | |
27 // Returns the version in the current module's version resource or the empty | |
28 // string if none found. | |
29 base::string16 GetCurrentModuleVersion() { | |
30 scoped_ptr<FileVersionInfo> file_version_info( | 22 scoped_ptr<FileVersionInfo> file_version_info( |
31 CREATE_FILE_VERSION_INFO_FOR_CURRENT_MODULE()); | 23 CREATE_FILE_VERSION_INFO_FOR_CURRENT_MODULE()); |
32 if (file_version_info.get()) { | 24 DCHECK(file_version_info.get()); |
33 base::string16 version_string(file_version_info->file_version()); | 25 base::string16 version_string(file_version_info->file_version()); |
34 if (Version(base::UTF16ToASCII(version_string)).IsValid()) | 26 DCHECK(base::Version(base::UTF16ToASCII(version_string)).IsValid()); |
35 return version_string; | 27 return version_string; |
36 } | |
37 return base::string16(); | |
38 } | 28 } |
39 | 29 |
40 // Indicates whether a file can be opened using the same flags that | 30 // Indicates whether a file can be opened using the same flags that |
41 // ::LoadLibrary() uses to open modules. | 31 // ::LoadLibrary() uses to open modules. |
42 bool ModuleCanBeRead(const base::FilePath file_path) { | 32 bool ModuleCanBeRead(const base::FilePath file_path) { |
43 return base::File(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ) | 33 return base::File(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ) |
44 .IsValid(); | 34 .IsValid(); |
45 } | 35 } |
46 | 36 |
47 } // namespace | 37 } // namespace |
48 | 38 |
49 base::FilePath GetModulePath(base::StringPiece16 module_name, | 39 base::FilePath GetModulePath(base::StringPiece16 module_name, |
50 base::string16* version) { | 40 base::string16* version) { |
51 DCHECK(version); | 41 DCHECK(version); |
52 | 42 |
53 base::FilePath module_dir = GetExecutableDir(); | 43 base::FilePath exe_dir; |
54 base::FilePath module = module_dir.Append(module_name); | 44 const bool has_path = base::PathService::Get(base::DIR_EXE, &exe_dir); |
55 if (ModuleCanBeRead(module)) | 45 DCHECK(has_path); |
56 return module; | |
57 | 46 |
58 base::string16 version_string(GetCurrentModuleVersion()); | 47 // Look for the module in the current executable's directory and return the |
59 if (version_string.empty()) { | 48 // path if it can be read. This is the expected location of modules for dev |
60 LOG(ERROR) << "No valid Chrome version found"; | 49 // builds. |
61 return base::FilePath(); | 50 const base::FilePath module_path = exe_dir.Append(module_name); |
62 } | 51 if (ModuleCanBeRead(module_path)) |
63 *version = version_string; | 52 return module_path; |
64 return module_dir.Append(version_string).Append(module_name); | 53 |
| 54 // 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 |
| 56 // modules for proper installs. |
| 57 *version = GetCurrentExecutableVersion(); |
| 58 DCHECK(!version->empty()); |
| 59 |
| 60 return exe_dir.Append(*version).Append(module_name); |
65 } | 61 } |
66 | 62 |
67 } // namespace installer | 63 } // namespace installer |
OLD | NEW |