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> | |
| 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 or the empty |
| 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. | 21 // string if none found. |
|
gab
2015/11/18 23:01:40
Is this comment still correct?
fdoray
2015/11/18 23:26:36
Done. Removed.
| |
| 29 base::string16 GetCurrentModuleVersion() { | 22 base::string16 GetCurrentExecutableVersion() { |
| 30 scoped_ptr<FileVersionInfo> file_version_info( | 23 scoped_ptr<FileVersionInfo> file_version_info( |
| 31 CREATE_FILE_VERSION_INFO_FOR_CURRENT_MODULE()); | 24 CREATE_FILE_VERSION_INFO_FOR_CURRENT_MODULE()); |
| 32 if (file_version_info.get()) { | 25 DCHECK(file_version_info.get()); |
| 33 base::string16 version_string(file_version_info->file_version()); | 26 base::string16 version_string(file_version_info->file_version()); |
| 34 if (Version(base::UTF16ToASCII(version_string)).IsValid()) | 27 DCHECK(base::Version(base::UTF16ToASCII(version_string)).IsValid()); |
| 35 return version_string; | 28 return version_string; |
| 36 } | |
| 37 return base::string16(); | |
| 38 } | 29 } |
| 39 | 30 |
| 40 // Indicates whether a file can be opened using the same flags that | 31 // Indicates whether a file can be opened using the same flags that |
| 41 // ::LoadLibrary() uses to open modules. | 32 // ::LoadLibrary() uses to open modules. |
| 42 bool ModuleCanBeRead(const base::FilePath file_path) { | 33 bool ModuleCanBeRead(const base::FilePath file_path) { |
| 43 return base::File(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ) | 34 return base::File(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ) |
| 44 .IsValid(); | 35 .IsValid(); |
| 45 } | 36 } |
| 46 | 37 |
| 47 } // namespace | 38 } // namespace |
| 48 | 39 |
| 49 base::FilePath GetModulePath(base::StringPiece16 module_name, | 40 base::FilePath GetModulePath(base::StringPiece16 module_name, |
| 50 base::string16* version) { | 41 base::string16* version) { |
| 51 DCHECK(version); | 42 DCHECK(version); |
| 52 | 43 |
| 53 base::FilePath module_dir = GetExecutableDir(); | 44 base::FilePath exe_dir; |
| 54 base::FilePath module = module_dir.Append(module_name); | 45 const bool has_path = base::PathService::Get(base::DIR_EXE, &exe_dir); |
| 55 if (ModuleCanBeRead(module)) | 46 DCHECK(has_path); |
| 56 return module; | |
| 57 | 47 |
| 58 base::string16 version_string(GetCurrentModuleVersion()); | 48 // Look for the module in the current executable's directory and return the |
| 59 if (version_string.empty()) { | 49 // path if it can be read. This is the expected location of modules for dev |
| 60 LOG(ERROR) << "No valid Chrome version found"; | 50 // builds. |
| 61 return base::FilePath(); | 51 base::FilePath module_path = exe_dir.Append(module_name); |
|
gab
2015/11/18 23:01:40
const
fdoray
2015/11/18 23:26:36
Done.
| |
| 62 } | 52 if (ModuleCanBeRead(module_path)) |
| 53 return module_path; | |
| 54 | |
| 55 // Othwerwise, return the path to the module in a versioned sub-directory of | |
| 56 // the current executable's directory. This is the expected location of | |
| 57 // modules for proper installs. | |
| 58 base::string16 version_string(GetCurrentExecutableVersion()); | |
| 59 DCHECK(!version_string.empty()); | |
|
grt (UTC plus 2)
2015/11/17 23:22:58
This will be the failure mode if we ever botch the
fdoray
2015/11/18 15:37:03
Before this CL, when the version string was empty
| |
| 60 | |
| 63 *version = version_string; | 61 *version = version_string; |
| 64 return module_dir.Append(version_string).Append(module_name); | 62 return exe_dir.Append(version_string).Append(module_name); |
| 65 } | 63 } |
| 66 | 64 |
| 67 } // namespace installer | 65 } // namespace installer |
| OLD | NEW |