Chromium Code Reviews| 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> | 5 #include <windows.h> |
| 6 | 6 |
| 7 #include "chrome/installer/util/module_util_win.h" | 7 #include "chrome/installer/util/module_util_win.h" |
| 8 | 8 |
| 9 #include "base/file_version_info.h" | 9 #include "base/file_version_info.h" |
| 10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
| 11 #include "base/files/file_util.h" | |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/version.h" | 15 #include "base/version.h" |
| 15 | 16 |
| 16 namespace installer { | 17 namespace installer { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // Returns the directory in which the currently running executable resides. | 21 // Returns the directory in which the currently running executable resides. |
| 21 base::FilePath GetExecutableDir() { | 22 base::FilePath GetExecutableDir() { |
| 22 base::char16 path[MAX_PATH]; | 23 base::char16 path_buffer[MAX_PATH]; |
| 23 ::GetModuleFileNameW(nullptr, path, MAX_PATH); | 24 ::GetModuleFileNameW(nullptr, path_buffer, MAX_PATH); |
| 24 return base::FilePath(path).DirName(); | 25 base::FilePath path(path_buffer); |
| 26 | |
| 27 // When debugging Chrome from the gyp-generated Visual Studio solution, the | |
|
gab
2015/11/12 18:10:30
You mean non-ninja builds? Those are not supported
fdoray
2015/11/12 18:27:23
When GYP_GENERATORS is set to "ninja,msvs-ninja",
| |
| 28 // value returned by ::GetModuleFileNameW can contain a path traversal '..' | |
| 29 // component, which isn't supported by base::File. | |
| 30 if (path.ReferencesParent()) | |
| 31 path = base::MakeAbsoluteFilePath(path); | |
| 32 | |
| 33 return path.DirName(); | |
| 25 } | 34 } |
| 26 | 35 |
| 27 // Returns the version in the current module's version resource or the empty | 36 // Returns the version in the current module's version resource or the empty |
| 28 // string if none found. | 37 // string if none found. |
| 29 base::string16 GetCurrentModuleVersion() { | 38 base::string16 GetCurrentModuleVersion() { |
| 30 scoped_ptr<FileVersionInfo> file_version_info( | 39 scoped_ptr<FileVersionInfo> file_version_info( |
| 31 CREATE_FILE_VERSION_INFO_FOR_CURRENT_MODULE()); | 40 CREATE_FILE_VERSION_INFO_FOR_CURRENT_MODULE()); |
| 32 if (file_version_info.get()) { | 41 if (file_version_info.get()) { |
| 33 base::string16 version_string(file_version_info->file_version()); | 42 base::string16 version_string(file_version_info->file_version()); |
| 34 if (Version(base::UTF16ToASCII(version_string)).IsValid()) | 43 if (Version(base::UTF16ToASCII(version_string)).IsValid()) |
| 35 return version_string; | 44 return version_string; |
| 36 } | 45 } |
| 37 return base::string16(); | 46 return base::string16(); |
| 38 } | 47 } |
| 39 | 48 |
| 40 // Indicates whether a file can be opened using the same flags that | 49 // Indicates whether a file can be opened using the same flags that |
| 41 // ::LoadLibrary() uses to open modules. | 50 // ::LoadLibrary() uses to open modules. |
| 42 bool ModuleCanBeRead(const base::FilePath file_path) { | 51 bool ModuleCanBeRead(const base::FilePath file_path) { |
| 43 return base::File(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ) | 52 return base::File(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ) |
| 44 .IsValid(); | 53 .IsValid(); |
| 45 } | 54 } |
| 46 | 55 |
| 47 } // namespace | 56 } // namespace |
| 48 | 57 |
| 49 base::FilePath GetModulePath(base::StringPiece16 module_name, | 58 base::FilePath GetModulePath(base::StringPiece16 module_name, |
| 50 base::string16* version) { | 59 base::string16* version) { |
| 51 DCHECK(version); | 60 DCHECK(version); |
| 52 | 61 |
| 53 base::FilePath module_dir = GetExecutableDir(); | 62 base::FilePath module_dir = GetExecutableDir(); |
|
Peter Kasting
2015/11/16 20:25:43
I think if we just replace this with something lik
fdoray
2015/11/16 21:31:36
Done.
| |
| 54 base::FilePath module = module_dir.Append(module_name); | 63 base::FilePath module = module_dir.Append(module_name); |
| 55 if (ModuleCanBeRead(module)) | 64 if (ModuleCanBeRead(module)) |
| 56 return module; | 65 return module; |
| 57 | 66 |
| 58 base::string16 version_string(GetCurrentModuleVersion()); | 67 base::string16 version_string(GetCurrentModuleVersion()); |
| 59 if (version_string.empty()) { | 68 if (version_string.empty()) { |
| 60 LOG(ERROR) << "No valid Chrome version found"; | 69 LOG(ERROR) << "No valid Chrome version found"; |
| 61 return base::FilePath(); | 70 return base::FilePath(); |
| 62 } | 71 } |
| 63 *version = version_string; | 72 *version = version_string; |
| 64 return module_dir.Append(version_string).Append(module_name); | 73 return module_dir.Append(version_string).Append(module_name); |
| 65 } | 74 } |
| 66 | 75 |
| 67 } // namespace installer | 76 } // namespace installer |
| OLD | NEW |