Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: chrome/browser/chromeos/usb_mount_observer.cc

Issue 6674043: Rewritten MountLibrary to work with non-blocking mount API calls in libcros.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/usb_mount_observer.h" 5 #include "chrome/browser/chromeos/usb_mount_observer.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/json/json_writer.h"
8 #include "base/singleton.h" 9 #include "base/singleton.h"
10 #include "chrome/browser/chromeos/cros/cros_library.h"
9 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h" 12 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_list.h" 13 #include "chrome/browser/ui/browser_list.h"
12 #include "chrome/browser/ui/browser_window.h" 14 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/webui/filebrowse_ui.h" 15 #include "chrome/browser/ui/webui/filebrowse_ui.h"
14 #include "chrome/common/chrome_switches.h" 16 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/url_constants.h" 17 #include "chrome/common/url_constants.h"
16 #include "content/browser/tab_contents/tab_contents.h" 18 #include "content/browser/tab_contents/tab_contents.h"
17 19
20
18 namespace chromeos { 21 namespace chromeos {
19 22
20 const char* kFilebrowseURLHash = "chrome://filebrowse#"; 23 const char* kFilebrowseURLHash = "chrome://filebrowse#";
21 const char* kFilebrowseScanning = "scanningdevice"; 24 const char* kFilebrowseScanning = "scanningdevice";
22 const int kPopupLeft = 0; 25 const int kPopupLeft = 0;
23 const int kPopupTop = 0; 26 const int kPopupTop = 0;
24 const int kPopupWidth = 250; 27 const int kPopupWidth = 250;
25 const int kPopupHeight = 300; 28 const int kPopupHeight = 300;
26 29
27 // static 30 // static
(...skipping 10 matching lines...) Expand all
38 if (Source<Browser>(source).ptr() == i->browser) { 41 if (Source<Browser>(source).ptr() == i->browser) {
39 i->browser = NULL; 42 i->browser = NULL;
40 registrar_.Remove(this, 43 registrar_.Remove(this,
41 NotificationType::BROWSER_CLOSED, 44 NotificationType::BROWSER_CLOSED,
42 source); 45 source);
43 return; 46 return;
44 } 47 }
45 } 48 }
46 } 49 }
47 50
48 void USBMountObserver::ScanForDevices(chromeos::MountLibrary* obj) {
49 const chromeos::MountLibrary::DiskVector& disks = obj->disks();
50 for (size_t i = 0; i < disks.size(); ++i) {
51 chromeos::MountLibrary::Disk disk = disks[i];
52 if (!disk.is_parent && !disk.device_path.empty()) {
53 obj->MountPath(disk.device_path.c_str());
54 }
55 }
56 }
57
58 void USBMountObserver::OpenFileBrowse(const std::string& url, 51 void USBMountObserver::OpenFileBrowse(const std::string& url,
59 const std::string& device_path, 52 const std::string& device_path,
60 bool small) { 53 bool small) {
61 Browser* browser; 54 Browser* browser;
62 Profile* profile; 55 Profile* profile;
63 browser = BrowserList::GetLastActive(); 56 browser = BrowserList::GetLastActive();
64 if (browser == NULL) { 57 if (browser == NULL) {
65 return; 58 return;
66 } 59 }
67 profile = browser->profile(); 60 profile = browser->profile();
(...skipping 19 matching lines...) Expand all
87 if (iter == browsers_.end()) { 80 if (iter == browsers_.end()) {
88 BrowserWithPath new_browser; 81 BrowserWithPath new_browser;
89 new_browser.browser = browser; 82 new_browser.browser = browser;
90 new_browser.device_path = device_path; 83 new_browser.device_path = device_path;
91 browsers_.push_back(new_browser); 84 browsers_.push_back(new_browser);
92 } else { 85 } else {
93 iter->browser = browser; 86 iter->browser = browser;
94 } 87 }
95 } 88 }
96 89
97 void USBMountObserver::MountChanged(chromeos::MountLibrary* obj, 90 void USBMountObserver::DiskChanged(MountLibraryEventType event,
98 chromeos::MountEventType evt, 91 const MountLibrary::Disk* disk) {
99 const std::string& path) { 92 if (event == MOUNT_DISK_ADDED) {
100 if (evt == chromeos::DISK_ADDED) { 93 OnDiskAdded(disk);
101 const chromeos::MountLibrary::DiskVector& disks = obj->disks(); 94 } else if (event == MOUNT_DISK_REMOVED) {
102 for (size_t i = 0; i < disks.size(); ++i) { 95 OnDiskRemoved(disk);
103 chromeos::MountLibrary::Disk disk = disks[i]; 96 } else if (event == MOUNT_DISK_CHANGED) {
104 if (disk.device_path == path) { 97 OnDiskChanged(disk);
105 if (disk.is_parent) { 98 }
106 if (!disk.has_media) { 99 }
107 RemoveBrowserFromVector(disk.system_path); 100
108 } 101 void USBMountObserver::DeviceChanged(MountLibraryEventType event,
109 } else if (!obj->MountPath(path.c_str())) { 102 const std::string& device_path) {
110 RemoveBrowserFromVector(disk.system_path); 103 if (event == MOUNT_DEVICE_ADDED) {
111 } 104 OnDeviceAdded(device_path);
112 } 105 }else if (event == MOUNT_DEVICE_REMOVED) {
106 OnDeviceRemoved(device_path);
107 } else if (event == MOUNT_DEVICE_SCANNED) {
108 OnDeviceScanned(device_path);
109 }
110 }
111
112 void USBMountObserver::FireFileSystemChanged(
113 const std::string& web_path) {
114 // TODO(zelidrag): Send message to all extensions that file system has
115 // changed.
116 return;
117 }
118
119 void USBMountObserver::OnDiskAdded(const MountLibrary::Disk* disk) {
120 LOG(WARNING) << "Disk added: " << disk->device_path();
stevenjb 2011/03/16 22:52:55 -> VLOG(1)
zel 2011/03/17 01:00:30 Done.
121 if (disk->device_path().empty()) {
122 LOG(WARNING) << "Empty system path for " << disk->device_path();
123 return;
124 }
125 if (disk->is_parent()) {
126 if (!disk->has_media()) {
127 RemoveBrowserFromVector(disk->system_path());
113 } 128 }
stevenjb 2011/03/16 22:52:55 nit: unneeded {}
zel 2011/03/17 01:00:30 Done.
114 VLOG(1) << "Got added mount: " << path; 129 return;
115 } else if (evt == chromeos::DISK_REMOVED || 130 }
116 evt == chromeos::DEVICE_REMOVED) { 131
117 RemoveBrowserFromVector(path); 132 // If disk is not mounted yet, give it a try.
118 } else if (evt == chromeos::DISK_CHANGED) { 133 if (disk->mount_path().empty()) {
119 BrowserIterator iter = FindBrowserForPath(path); 134 // Initiate disk mount operation.
120 VLOG(1) << "Got changed mount: " << path; 135 chromeos::MountLibrary* lib =
121 if (iter == browsers_.end()) { 136 chromeos::CrosLibrary::Get()->GetMountLibrary();
122 // We don't currently have this one, so it must have been 137 lib->MountPath(disk->device_path().c_str());
123 // mounted 138 }
124 const chromeos::MountLibrary::DiskVector& disks = obj->disks(); 139 }
125 for (size_t i = 0; i < disks.size(); ++i) { 140
126 if (disks[i].device_path == path) { 141 void USBMountObserver::OnDiskRemoved(const MountLibrary::Disk* disk) {
127 if (!disks[i].mount_path.empty()) { 142 LOG(WARNING) << "Disk removed: " << disk->device_path();
stevenjb 2011/03/16 22:52:55 VLOG(1)
zel 2011/03/17 01:00:30 Done.
128 // Doing second search to see if the current disk has already 143 RemoveBrowserFromVector(disk->device_path());
129 // been popped up due to its parent device being plugged in. 144 MountPointMap::iterator iter = mounted_devices_.find(disk->device_path());
130 iter = FindBrowserForPath(disks[i].system_path); 145 if (iter == mounted_devices_.end())
131 if (iter != browsers_.end() && iter->browser) { 146 return;
132 std::string url = kFilebrowseURLHash; 147
133 url += disks[i].mount_path; 148 chromeos::MountLibrary* lib =
134 TabContents* tab = iter->browser->GetSelectedTabContents(); 149 chromeos::CrosLibrary::Get()->GetMountLibrary();
135 iter->browser->window()->SetBounds(gfx::Rect( 150 // TODO(zelidrag): This for some reason does not work as advertized.
136 0, 0, FileBrowseUI::kPopupWidth, FileBrowseUI::kPopupHeight)); 151 // we might need to clean up mount directory on FILE thread here as well.
137 tab->OpenURL(GURL(url), GURL(), CURRENT_TAB, 152 lib->UnmountPath(disk->device_path().c_str());
138 PageTransition::LINK); 153
139 tab->NavigateToPendingEntry(NavigationController::RELOAD); 154 FireFileSystemChanged(iter->second);
140 iter->device_path = path; 155 mounted_devices_.erase(iter);
141 iter->mount_path = disks[i].mount_path; 156 }
142 } else { 157
143 OpenFileBrowse(disks[i].mount_path, disks[i].device_path, false); 158 void USBMountObserver::OnDiskChanged(const MountLibrary::Disk* disk) {
144 } 159 LOG(WARNING) << "Disk changed : " << disk->device_path();
stevenjb 2011/03/16 22:52:55 VLOG(1)
zel 2011/03/17 01:00:30 Done.
145 } 160 if (!disk->mount_path().empty()) {
146 return; 161 // Remember this mount point.
147 } 162 mounted_devices_.insert(
148 } 163 std::pair<std::string, std::string>(disk->device_path(),
164 disk->mount_path()));
165 FireFileSystemChanged(disk->mount_path());
166
167 // TODO(zelidrag): We should remove old file browser stuff later.
168 // Doing second search to see if the current disk has already
169 // been popped up due to its parent device being plugged in.
170 BrowserIterator iter = FindBrowserForPath(disk->system_path());
171 if (iter != browsers_.end() && iter->browser) {
172 std::string url = kFilebrowseURLHash;
173 url += disk->mount_path();
174 TabContents* tab = iter->browser->GetSelectedTabContents();
175 iter->browser->window()->SetBounds(gfx::Rect(
176 0, 0, FileBrowseUI::kPopupWidth, FileBrowseUI::kPopupHeight));
177 tab->OpenURL(GURL(url), GURL(), CURRENT_TAB,
178 PageTransition::LINK);
179 tab->NavigateToPendingEntry(NavigationController::RELOAD);
180 iter->device_path = disk->device_path();
181 iter->mount_path = disk->mount_path();
182 } else {
183 OpenFileBrowse(disk->mount_path(), disk->device_path(), false);
149 } 184 }
150 } else if (evt == chromeos::DEVICE_ADDED) {
151 VLOG(1) << "Got device added: " << path;
152 OpenFileBrowse(kFilebrowseScanning, path, true);
153 } else if (evt == chromeos::DEVICE_SCANNED) {
154 VLOG(1) << "Got device scanned: " << path;
155 } 185 }
156 } 186 }
157 187
188 void USBMountObserver::OnDeviceAdded(const std::string& device_path) {
189 LOG(WARNING) << "Device added : " << device_path;
stevenjb 2011/03/16 22:52:55 VLOG(1)
zel 2011/03/17 01:00:30 Done.
190 OpenFileBrowse(kFilebrowseScanning, device_path, true);
191 }
192
193 void USBMountObserver::OnDeviceRemoved(const std::string& device_path) {
194 // New device is added, initiate disk rescan.
195 RemoveBrowserFromVector(device_path);
196 }
197
198 void USBMountObserver::OnDeviceScanned(const std::string& device_path) {
199 LOG(WARNING) << "Device scanned : " << device_path;
stevenjb 2011/03/16 22:52:55 VLOG(1)
zel 2011/03/17 01:00:30 Done.
200 }
201
158 USBMountObserver::BrowserIterator USBMountObserver::FindBrowserForPath( 202 USBMountObserver::BrowserIterator USBMountObserver::FindBrowserForPath(
159 const std::string& path) { 203 const std::string& path) {
160 for (BrowserIterator i = browsers_.begin();i != browsers_.end(); 204 for (BrowserIterator i = browsers_.begin();i != browsers_.end();
161 ++i) { 205 ++i) {
162 // Doing a substring match so that we find if this new one is a subdevice 206 // Doing a substring match so that we find if this new one is a subdevice
163 // of another already inserted device. 207 // of another already inserted device.
164 if (path.find(i->device_path) != std::string::npos) { 208 if (path.find(i->device_path) != std::string::npos) {
165 return i; 209 return i;
166 } 210 }
167 } 211 }
(...skipping 27 matching lines...) Expand all
195 } 239 }
196 } 240 }
197 for (size_t x = 0; x < close_these.size(); x++) { 241 for (size_t x = 0; x < close_these.size(); x++) {
198 if (close_these[x]->window()) { 242 if (close_these[x]->window()) {
199 close_these[x]->window()->Close(); 243 close_these[x]->window()->Close();
200 } 244 }
201 } 245 }
202 } 246 }
203 247
204 } // namespace chromeos 248 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698