- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / music_manager_private / device_id_linux.cc
1 // Copyright 2013 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 "chrome/browser/extensions/api/music_manager_private/device_id.h"
6
7 #include <map>
8
9 #include "base/bind.h"
10 #include "base/file_util.h"
11 #include "base/files/file_enumerator.h"
12 #include "base/files/file_path.h"
13 #include "content/public/browser/browser_thread.h"
14
15 namespace {
16
17 const char kDiskByUuidDirectoryName[] = "/dev/disk/by-uuid";
18 const char* kDeviceNames[] = { "sda1", "hda1", "dm-0" };
19
20 // Map from device name to disk uuid
21 typedef std::map<base::FilePath, base::FilePath> DiskEntries;
22
23 void GetDiskUuid(const extensions::api::DeviceId::IdCallback& callback) {
24   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
25
26   DiskEntries disk_uuids;
27
28   base::FileEnumerator files(base::FilePath(kDiskByUuidDirectoryName),
29                              false,  // Recursive.
30                              base::FileEnumerator::FILES);
31   do {
32     base::FilePath file_path = files.Next();
33     if (file_path.empty())
34       break;
35
36     base::FilePath target_path;
37     if (!file_util::ReadSymbolicLink(file_path, &target_path))
38       continue;
39
40     base::FilePath device_name = target_path.BaseName();
41     base::FilePath disk_uuid = file_path.BaseName();
42     disk_uuids[device_name] = disk_uuid;
43   } while (true);
44
45   // Look for first device name matching an entry of |kDeviceNames|.
46   std::string result;
47   for (size_t i = 0; i < arraysize(kDeviceNames); i++) {
48     DiskEntries::iterator it =
49         disk_uuids.find(base::FilePath(kDeviceNames[i]));
50     if (it != disk_uuids.end()) {
51       DVLOG(1) << "Returning uuid: \"" << it->second.value()
52                << "\" for device \"" << it->first.value() << "\"";
53       result = it->second.value();
54       break;
55     }
56   }
57
58   // Log failure (at most once) for diagnostic purposes.
59   static bool error_logged = false;
60   if (result.empty() && !error_logged) {
61     error_logged = true;
62     LOG(ERROR) << "Could not find appropriate disk uuid.";
63     for (DiskEntries::iterator it = disk_uuids.begin();
64         it != disk_uuids.end(); ++it) {
65       LOG(ERROR) << "  DeviceID=" << it->first.value() << ", uuid="
66                  << it->second.value();
67     }
68   }
69
70   content::BrowserThread::PostTask(
71       content::BrowserThread::UI,
72       FROM_HERE,
73       base::Bind(callback, result));
74 }
75
76 }  // namespace
77
78 namespace extensions {
79 namespace api {
80
81 // Linux: Look for disk uuid
82 /* static */
83 void DeviceId::GetMachineId(const IdCallback& callback) {
84   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
85
86   content::BrowserThread::PostTask(
87       content::BrowserThread::FILE,
88       FROM_HERE,
89       base::Bind(GetDiskUuid, callback));
90 }
91
92 }  // namespace api
93 }  // namespace extensions