| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <windows.h> |
| 6 |
| 7 #include "chrome/installer/util/module_util_win.h" |
| 8 |
| 9 #include "base/file_version_info.h" |
| 10 #include "base/files/file.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/version.h" |
| 15 |
| 16 namespace installer { |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Returns the directory in which the currently running executable resides. |
| 21 base::FilePath GetExecutableDir() { |
| 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( |
| 31 CREATE_FILE_VERSION_INFO_FOR_CURRENT_MODULE()); |
| 32 if (file_version_info.get()) { |
| 33 base::string16 version_string(file_version_info->file_version()); |
| 34 if (Version(base::UTF16ToASCII(version_string)).IsValid()) |
| 35 return version_string; |
| 36 } |
| 37 return base::string16(); |
| 38 } |
| 39 |
| 40 // Indicates whether a file can be opened using the same flags that |
| 41 // ::LoadLibrary() uses to open modules. |
| 42 bool ModuleCanBeRead(const base::FilePath file_path) { |
| 43 return base::File(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ) |
| 44 .IsValid(); |
| 45 } |
| 46 |
| 47 } // namespace |
| 48 |
| 49 base::FilePath GetModulePath(base::StringPiece16 module_name, |
| 50 base::string16* version) { |
| 51 DCHECK(version); |
| 52 |
| 53 base::FilePath module_dir = GetExecutableDir(); |
| 54 base::FilePath module = module_dir.Append(module_name); |
| 55 if (ModuleCanBeRead(module)) |
| 56 return module; |
| 57 |
| 58 base::string16 version_string(GetCurrentModuleVersion()); |
| 59 if (version_string.empty()) { |
| 60 LOG(ERROR) << "No valid Chrome version found"; |
| 61 return base::FilePath(); |
| 62 } |
| 63 *version = version_string; |
| 64 return module_dir.Append(version_string).Append(module_name); |
| 65 } |
| 66 |
| 67 } // namespace installer |
| OLD | NEW |