Chromium Code Reviews| 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/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 Loading... | |
| 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 Loading... | |
| 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); |
|
xiyuan
2011/03/17 00:23:32
nit: one less indent space.
| |
| 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 VLOG(1) << "Disk added: " << disk->device_path(); | |
| 121 if (disk->device_path().empty()) { | |
| 122 VLOG(1) << "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()); | |
| 128 return; | |
| 129 } | |
| 130 | |
| 131 // If disk is not mounted yet, give it a try. | |
| 132 if (disk->mount_path().empty()) { | |
| 133 // Initiate disk mount operation. | |
| 134 chromeos::MountLibrary* lib = | |
| 135 chromeos::CrosLibrary::Get()->GetMountLibrary(); | |
| 136 lib->MountPath(disk->device_path().c_str()); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 void USBMountObserver::OnDiskRemoved(const MountLibrary::Disk* disk) { | |
| 141 VLOG(1) << "Disk removed: " << disk->device_path(); | |
| 142 RemoveBrowserFromVector(disk->device_path()); | |
| 143 MountPointMap::iterator iter = mounted_devices_.find(disk->device_path()); | |
| 144 if (iter == mounted_devices_.end()) | |
| 145 return; | |
| 146 | |
| 147 chromeos::MountLibrary* lib = | |
| 148 chromeos::CrosLibrary::Get()->GetMountLibrary(); | |
| 149 // TODO(zelidrag): This for some reason does not work as advertized. | |
| 150 // we might need to clean up mount directory on FILE thread here as well. | |
| 151 lib->UnmountPath(disk->device_path().c_str()); | |
| 152 | |
| 153 FireFileSystemChanged(iter->second); | |
| 154 mounted_devices_.erase(iter); | |
| 155 } | |
| 156 | |
| 157 void USBMountObserver::OnDiskChanged(const MountLibrary::Disk* disk) { | |
| 158 VLOG(1) << "Disk changed : " << disk->device_path(); | |
| 159 if (!disk->mount_path().empty()) { | |
| 160 // Remember this mount point. | |
| 161 mounted_devices_.insert( | |
| 162 std::pair<std::string, std::string>(disk->device_path(), | |
| 163 disk->mount_path())); | |
| 164 FireFileSystemChanged(disk->mount_path()); | |
| 165 | |
| 166 // TODO(zelidrag): We should remove old file browser stuff later. | |
| 167 // Doing second search to see if the current disk has already | |
| 168 // been popped up due to its parent device being plugged in. | |
| 169 BrowserIterator iter = FindBrowserForPath(disk->system_path()); | |
| 170 if (iter != browsers_.end() && iter->browser) { | |
| 171 std::string url = kFilebrowseURLHash; | |
| 172 url += disk->mount_path(); | |
| 173 TabContents* tab = iter->browser->GetSelectedTabContents(); | |
| 174 iter->browser->window()->SetBounds(gfx::Rect( | |
| 175 0, 0, FileBrowseUI::kPopupWidth, FileBrowseUI::kPopupHeight)); | |
| 176 tab->OpenURL(GURL(url), GURL(), CURRENT_TAB, | |
| 177 PageTransition::LINK); | |
| 178 tab->NavigateToPendingEntry(NavigationController::RELOAD); | |
| 179 iter->device_path = disk->device_path(); | |
| 180 iter->mount_path = disk->mount_path(); | |
| 181 } else { | |
| 182 OpenFileBrowse(disk->mount_path(), disk->device_path(), false); | |
| 113 } | 183 } |
| 114 VLOG(1) << "Got added mount: " << path; | |
| 115 } else if (evt == chromeos::DISK_REMOVED || | |
| 116 evt == chromeos::DEVICE_REMOVED) { | |
| 117 RemoveBrowserFromVector(path); | |
| 118 } else if (evt == chromeos::DISK_CHANGED) { | |
| 119 BrowserIterator iter = FindBrowserForPath(path); | |
| 120 VLOG(1) << "Got changed mount: " << path; | |
| 121 if (iter == browsers_.end()) { | |
| 122 // We don't currently have this one, so it must have been | |
| 123 // mounted | |
| 124 const chromeos::MountLibrary::DiskVector& disks = obj->disks(); | |
| 125 for (size_t i = 0; i < disks.size(); ++i) { | |
| 126 if (disks[i].device_path == path) { | |
| 127 if (!disks[i].mount_path.empty()) { | |
| 128 // Doing second search to see if the current disk has already | |
| 129 // been popped up due to its parent device being plugged in. | |
| 130 iter = FindBrowserForPath(disks[i].system_path); | |
| 131 if (iter != browsers_.end() && iter->browser) { | |
| 132 std::string url = kFilebrowseURLHash; | |
| 133 url += disks[i].mount_path; | |
| 134 TabContents* tab = iter->browser->GetSelectedTabContents(); | |
| 135 iter->browser->window()->SetBounds(gfx::Rect( | |
| 136 0, 0, FileBrowseUI::kPopupWidth, FileBrowseUI::kPopupHeight)); | |
| 137 tab->OpenURL(GURL(url), GURL(), CURRENT_TAB, | |
| 138 PageTransition::LINK); | |
| 139 tab->NavigateToPendingEntry(NavigationController::RELOAD); | |
| 140 iter->device_path = path; | |
| 141 iter->mount_path = disks[i].mount_path; | |
| 142 } else { | |
| 143 OpenFileBrowse(disks[i].mount_path, disks[i].device_path, false); | |
| 144 } | |
| 145 } | |
| 146 return; | |
| 147 } | |
| 148 } | |
| 149 } | |
| 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 } | 184 } |
| 156 } | 185 } |
| 157 | 186 |
| 187 void USBMountObserver::OnDeviceAdded(const std::string& device_path) { | |
| 188 VLOG(1) << "Device added : " << device_path; | |
| 189 OpenFileBrowse(kFilebrowseScanning, device_path, true); | |
| 190 } | |
| 191 | |
| 192 void USBMountObserver::OnDeviceRemoved(const std::string& device_path) { | |
| 193 // New device is added, initiate disk rescan. | |
| 194 RemoveBrowserFromVector(device_path); | |
| 195 } | |
| 196 | |
| 197 void USBMountObserver::OnDeviceScanned(const std::string& device_path) { | |
| 198 VLOG(1) << "Device scanned : " << device_path; | |
| 199 } | |
| 200 | |
| 158 USBMountObserver::BrowserIterator USBMountObserver::FindBrowserForPath( | 201 USBMountObserver::BrowserIterator USBMountObserver::FindBrowserForPath( |
| 159 const std::string& path) { | 202 const std::string& path) { |
| 160 for (BrowserIterator i = browsers_.begin();i != browsers_.end(); | 203 for (BrowserIterator i = browsers_.begin();i != browsers_.end(); |
| 161 ++i) { | 204 ++i) { |
| 162 // Doing a substring match so that we find if this new one is a subdevice | 205 // Doing a substring match so that we find if this new one is a subdevice |
| 163 // of another already inserted device. | 206 // of another already inserted device. |
| 164 if (path.find(i->device_path) != std::string::npos) { | 207 if (path.find(i->device_path) != std::string::npos) { |
| 165 return i; | 208 return i; |
| 166 } | 209 } |
| 167 } | 210 } |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 195 } | 238 } |
| 196 } | 239 } |
| 197 for (size_t x = 0; x < close_these.size(); x++) { | 240 for (size_t x = 0; x < close_these.size(); x++) { |
| 198 if (close_these[x]->window()) { | 241 if (close_these[x]->window()) { |
| 199 close_these[x]->window()->Close(); | 242 close_these[x]->window()->Close(); |
| 200 } | 243 } |
| 201 } | 244 } |
| 202 } | 245 } |
| 203 | 246 |
| 204 } // namespace chromeos | 247 } // namespace chromeos |
| OLD | NEW |