OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/cros/mount_library.h" | 5 #include "chrome/browser/chromeos/cros/mount_library.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "chrome/browser/chrome_thread.h" | 9 #include "chrome/browser/chrome_thread.h" |
10 #include "chrome/browser/chromeos/cros/cros_library.h" | 10 #include "chrome/browser/chromeos/cros/cros_library.h" |
11 | 11 |
12 // Allows InvokeLater without adding refcounting. This class is a Singleton and | 12 // Allows InvokeLater without adding refcounting. This class is a Singleton and |
13 // won't be deleted until it's last InvokeLater is run. | 13 // won't be deleted until it's last InvokeLater is run. |
14 template <> | 14 template <> |
15 struct RunnableMethodTraits<chromeos::MountLibrary> { | 15 struct RunnableMethodTraits<chromeos::MountLibraryImpl> { |
16 void RetainCallee(chromeos::MountLibrary* obj) {} | 16 void RetainCallee(chromeos::MountLibraryImpl* obj) {} |
17 void ReleaseCallee(chromeos::MountLibrary* obj) {} | 17 void ReleaseCallee(chromeos::MountLibraryImpl* obj) {} |
18 }; | 18 }; |
19 | 19 |
20 namespace chromeos { | 20 namespace chromeos { |
21 | 21 |
22 // static | 22 void MountLibraryImpl::AddObserver(Observer* observer) { |
23 MountLibrary* MountLibrary::Get() { | |
24 return Singleton<MountLibrary>::get(); | |
25 } | |
26 | |
27 void MountLibrary::AddObserver(Observer* observer) { | |
28 observers_.AddObserver(observer); | 23 observers_.AddObserver(observer); |
29 } | 24 } |
30 | 25 |
31 void MountLibrary::RemoveObserver(Observer* observer) { | 26 void MountLibraryImpl::RemoveObserver(Observer* observer) { |
32 observers_.RemoveObserver(observer); | 27 observers_.RemoveObserver(observer); |
33 } | 28 } |
34 | 29 |
35 void MountLibrary::ParseDisks(const MountStatus& status) { | 30 void MountLibraryImpl::ParseDisks(const MountStatus& status) { |
36 disks_.clear(); | 31 disks_.clear(); |
37 for (int i = 0; i < status.size; i++) { | 32 for (int i = 0; i < status.size; i++) { |
38 std::string path; | 33 std::string path; |
39 std::string mountpath; | 34 std::string mountpath; |
40 std::string systempath; | 35 std::string systempath; |
41 if (status.disks[i].path != NULL) { | 36 if (status.disks[i].path != NULL) { |
42 path = status.disks[i].path; | 37 path = status.disks[i].path; |
43 } | 38 } |
44 if (status.disks[i].mountpath != NULL) { | 39 if (status.disks[i].mountpath != NULL) { |
45 mountpath = status.disks[i].mountpath; | 40 mountpath = status.disks[i].mountpath; |
46 } | 41 } |
47 if (status.disks[i].systempath != NULL) { | 42 if (status.disks[i].systempath != NULL) { |
48 systempath = status.disks[i].systempath; | 43 systempath = status.disks[i].systempath; |
49 } | 44 } |
50 disks_.push_back(Disk(path, mountpath, systempath)); | 45 disks_.push_back(Disk(path, mountpath, systempath)); |
51 } | 46 } |
52 } | 47 } |
53 | 48 |
54 MountLibrary::MountLibrary() { | 49 MountLibraryImpl::MountLibraryImpl() : mount_status_connection_(NULL) { |
55 if (CrosLibrary::EnsureLoaded()) { | 50 if (CrosLibrary::Get()->EnsureLoaded()) { |
56 Init(); | 51 Init(); |
57 } else { | 52 } else { |
58 LOG(ERROR) << "Cros Library has not been loaded"; | 53 LOG(ERROR) << "Cros Library has not been loaded"; |
59 } | 54 } |
60 } | 55 } |
61 | 56 |
62 MountLibrary::~MountLibrary() { | 57 MountLibraryImpl::~MountLibraryImpl() { |
63 if (CrosLibrary::EnsureLoaded()) { | 58 if (mount_status_connection_) { |
64 DisconnectMountStatus(mount_status_connection_); | 59 DisconnectMountStatus(mount_status_connection_); |
65 } | 60 } |
66 } | 61 } |
67 | 62 |
68 // static | 63 // static |
69 void MountLibrary::MountStatusChangedHandler(void* object, | 64 void MountLibraryImpl::MountStatusChangedHandler(void* object, |
70 const MountStatus& status, | 65 const MountStatus& status, |
71 MountEventType evt, | 66 MountEventType evt, |
72 const char* path) { | 67 const char* path) { |
73 MountLibrary* mount = static_cast<MountLibrary*>(object); | 68 MountLibraryImpl* mount = static_cast<MountLibraryImpl*>(object); |
74 std::string devicepath = path; | 69 std::string devicepath = path; |
75 mount->ParseDisks(status); | 70 mount->ParseDisks(status); |
76 mount->UpdateMountStatus(status, evt, devicepath); | 71 mount->UpdateMountStatus(status, evt, devicepath); |
77 } | 72 } |
78 | 73 |
79 void MountLibrary::Init() { | 74 void MountLibraryImpl::Init() { |
80 // Getting the monitor status so that the daemon starts up. | 75 // Getting the monitor status so that the daemon starts up. |
81 MountStatus* mount = RetrieveMountInformation(); | 76 MountStatus* mount = RetrieveMountInformation(); |
82 FreeMountStatus(mount); | 77 FreeMountStatus(mount); |
83 | 78 |
84 mount_status_connection_ = MonitorMountStatus( | 79 mount_status_connection_ = MonitorMountStatus( |
85 &MountStatusChangedHandler, this); | 80 &MountStatusChangedHandler, this); |
86 } | 81 } |
87 | 82 |
88 void MountLibrary::UpdateMountStatus(const MountStatus& status, | 83 void MountLibraryImpl::UpdateMountStatus(const MountStatus& status, |
89 MountEventType evt, | 84 MountEventType evt, |
90 const std::string& path) { | 85 const std::string& path) { |
91 // Make sure we run on UI thread. | 86 // Make sure we run on UI thread. |
92 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 87 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
93 | 88 |
94 FOR_EACH_OBSERVER(Observer, observers_, MountChanged(this, evt, path)); | 89 FOR_EACH_OBSERVER(Observer, observers_, MountChanged(this, evt, path)); |
95 } | 90 } |
96 | 91 |
97 } // namespace chromeos | 92 } // namespace chromeos |
OLD | NEW |