OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/browser/chromeos/system/udev_info_provider.h" | 5 #include "chrome/browser/chromeos/system/udev_info_provider.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/process_util.h" | 10 #include "base/process_util.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "chrome/browser/chromeos/system/name_value_pairs_parser.h" | 12 #include "chrome/browser/chromeos/system/name_value_pairs_parser.h" |
13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
14 | 14 |
15 using content::BrowserThread; | 15 using content::BrowserThread; |
16 | 16 |
17 namespace chromeos { | 17 namespace chromeos { |
18 namespace system { | 18 namespace system { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 // Absolute path of the udevadm tool. | 22 // Absolute path of the udevadm tool. |
23 const char kUdevadmToolPath[] = "/sbin/udevadm"; | 23 const char kUdevadmToolPath[] = "/sbin/udevadm"; |
24 // Args to pass to udevadm (followed by the device path). | |
25 const char* kUdevadmToolArgs[] = { "info", "--query=property", "--path" }; | |
26 // Key-value separator and string delimiter in output of "udevadm info". | 24 // Key-value separator and string delimiter in output of "udevadm info". |
27 const char kUdevadmEq[] = "="; | 25 const char kUdevadmEq[] = "="; |
28 const char kUdevadmDelim[] = "\n"; | 26 const char kUdevadmDelim[] = "\n"; |
| 27 const char kUdevadmCommentDelim[] = ""; |
29 | 28 |
30 } // namespace | 29 } // namespace |
31 | 30 |
32 class UdevInfoProviderImpl : public UdevInfoProvider { | 31 class UdevInfoProviderImpl : public UdevInfoProvider { |
33 public: | 32 public: |
34 UdevInfoProviderImpl() {} | 33 UdevInfoProviderImpl() {} |
35 | 34 |
36 // UdevInfoProvider implementation. | 35 // UdevInfoProvider implementation. |
37 virtual bool QueryDeviceProperty(const std::string& sys_path, | 36 virtual bool QueryDeviceProperty(const std::string& sys_path, |
38 const std::string& property_name, | 37 const std::string& property_name, |
39 std::string* result) const OVERRIDE; | 38 std::string* result) const OVERRIDE; |
40 | 39 |
41 static UdevInfoProvider* GetInstance(); | 40 static UdevInfoProvider* GetInstance(); |
42 | 41 |
43 private: | 42 private: |
44 DISALLOW_COPY_AND_ASSIGN(UdevInfoProviderImpl); | 43 DISALLOW_COPY_AND_ASSIGN(UdevInfoProviderImpl); |
45 }; | 44 }; |
46 | 45 |
47 base::LazyInstance<UdevInfoProviderImpl> g_udev_info_provider = | 46 base::LazyInstance<UdevInfoProviderImpl> g_udev_info_provider = |
48 LAZY_INSTANCE_INITIALIZER; | 47 LAZY_INSTANCE_INITIALIZER; |
49 | 48 |
50 bool UdevInfoProviderImpl::QueryDeviceProperty(const std::string& sys_path, | 49 bool UdevInfoProviderImpl::QueryDeviceProperty(const std::string& sys_path, |
51 const std::string& property_name, | 50 const std::string& property_name, |
52 std::string* result) const { | 51 std::string* result) const { |
53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
54 | 53 |
55 FilePath udevadm_path(kUdevadmToolPath); | 54 const char* kUdevadmTool[] = { |
56 CommandLine cmd_line(udevadm_path); | 55 kUdevadmToolPath, |
57 | 56 "info", |
58 // TODO(ivankr): manually appending switches with AppendArg is a workaround | 57 "--query=property", |
59 // to pass "info" argument before switches like "--path" (CommandLine forces | 58 "--path", |
60 // arguments after switches). | 59 sys_path.c_str(), |
61 for (size_t i = 0; i < arraysize(kUdevadmToolArgs); ++i) | 60 }; |
62 cmd_line.AppendArg(kUdevadmToolArgs[i]); | |
63 cmd_line.AppendArg(sys_path); | |
64 | |
65 std::string output_string; | |
66 if (!base::GetAppOutput(cmd_line, &output_string)) { | |
67 LOG(ERROR) << "Error executing: " << cmd_line.GetCommandLineString(); | |
68 return false; | |
69 } | |
70 TrimWhitespaceASCII(output_string, TRIM_ALL, &output_string); | |
71 | 61 |
72 NameValuePairsParser::NameValueMap device_info; | 62 NameValuePairsParser::NameValueMap device_info; |
73 NameValuePairsParser parser(&device_info); | 63 NameValuePairsParser parser(&device_info); |
74 | 64 |
75 if (!parser.ParseNameValuePairs(output_string, kUdevadmEq, kUdevadmDelim)) | 65 if (!parser.ParseNameValuePairsFromTool( |
76 return false; | 66 arraysize(kUdevadmTool), kUdevadmTool, kUdevadmEq, |
| 67 kUdevadmDelim, kUdevadmCommentDelim)) { |
| 68 LOG(WARNING) << "There were errors parsing the output of " |
| 69 << kUdevadmToolPath << "."; |
| 70 } |
77 | 71 |
78 NameValuePairsParser::NameValueMap::const_iterator it = | 72 NameValuePairsParser::NameValueMap::const_iterator it = |
79 device_info.find(property_name); | 73 device_info.find(property_name); |
80 if (it == device_info.end()) | 74 if (it == device_info.end()) |
81 return false; | 75 return false; |
82 | 76 |
83 *result = it->second; | 77 *result = it->second; |
84 return true; | 78 return true; |
85 } | 79 } |
86 | 80 |
87 UdevInfoProvider* UdevInfoProviderImpl::GetInstance() { | 81 UdevInfoProvider* UdevInfoProviderImpl::GetInstance() { |
88 return &g_udev_info_provider.Get(); | 82 return &g_udev_info_provider.Get(); |
89 } | 83 } |
90 | 84 |
91 UdevInfoProvider* UdevInfoProvider::GetInstance() { | 85 UdevInfoProvider* UdevInfoProvider::GetInstance() { |
92 return UdevInfoProviderImpl::GetInstance(); | 86 return UdevInfoProviderImpl::GetInstance(); |
93 } | 87 } |
94 | 88 |
95 } // namespace system | 89 } // namespace system |
96 } // namespace chromeos | 90 } // namespace chromeos |
OLD | NEW |