| 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) { |
| 54 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 55 switches::kEnableAdvancedFileSystem)) { |
| 56 return; |
| 57 } |
| 61 Browser* browser; | 58 Browser* browser; |
| 62 Profile* profile; | 59 Profile* profile; |
| 63 browser = BrowserList::GetLastActive(); | 60 browser = BrowserList::GetLastActive(); |
| 64 if (browser == NULL) { | 61 if (browser == NULL) { |
| 65 return; | 62 return; |
| 66 } | 63 } |
| 67 profile = browser->profile(); | 64 profile = browser->profile(); |
| 68 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
| 69 switches::kEnableAdvancedFileSystem)) { | |
| 70 return; | |
| 71 } | |
| 72 if (small) { | 65 if (small) { |
| 73 browser = FileBrowseUI::OpenPopup(profile, | 66 browser = FileBrowseUI::OpenPopup(profile, |
| 74 url, | 67 url, |
| 75 FileBrowseUI::kSmallPopupWidth, | 68 FileBrowseUI::kSmallPopupWidth, |
| 76 FileBrowseUI::kSmallPopupHeight); | 69 FileBrowseUI::kSmallPopupHeight); |
| 77 } else { | 70 } else { |
| 78 browser = FileBrowseUI::OpenPopup(profile, | 71 browser = FileBrowseUI::OpenPopup(profile, |
| 79 url, | 72 url, |
| 80 FileBrowseUI::kPopupWidth, | 73 FileBrowseUI::kPopupWidth, |
| 81 FileBrowseUI::kPopupHeight); | 74 FileBrowseUI::kPopupHeight); |
| 82 } | 75 } |
| 83 registrar_.Add(this, | 76 |
| 84 NotificationType::BROWSER_CLOSED, | |
| 85 Source<Browser>(browser)); | |
| 86 BrowserIterator iter = FindBrowserForPath(device_path); | 77 BrowserIterator iter = FindBrowserForPath(device_path); |
| 87 if (iter == browsers_.end()) { | 78 if (iter == browsers_.end()) { |
| 79 registrar_.Add(this, |
| 80 NotificationType::BROWSER_CLOSED, |
| 81 Source<Browser>(browser)); |
| 88 BrowserWithPath new_browser; | 82 BrowserWithPath new_browser; |
| 89 new_browser.browser = browser; | 83 new_browser.browser = browser; |
| 90 new_browser.device_path = device_path; | 84 new_browser.device_path = device_path; |
| 91 browsers_.push_back(new_browser); | 85 browsers_.push_back(new_browser); |
| 92 } else { | 86 } else { |
| 93 iter->browser = browser; | 87 iter->browser = browser; |
| 94 } | 88 } |
| 95 } | 89 } |
| 96 | 90 |
| 97 void USBMountObserver::MountChanged(chromeos::MountLibrary* obj, | 91 void USBMountObserver::DiskChanged(MountLibraryEventType event, |
| 98 chromeos::MountEventType evt, | 92 const MountLibrary::Disk* disk) { |
| 99 const std::string& path) { | 93 if (event == MOUNT_DISK_ADDED) { |
| 100 if (evt == chromeos::DISK_ADDED) { | 94 OnDiskAdded(disk); |
| 101 const chromeos::MountLibrary::DiskVector& disks = obj->disks(); | 95 } else if (event == MOUNT_DISK_REMOVED) { |
| 102 for (size_t i = 0; i < disks.size(); ++i) { | 96 OnDiskRemoved(disk); |
| 103 chromeos::MountLibrary::Disk disk = disks[i]; | 97 } else if (event == MOUNT_DISK_CHANGED) { |
| 104 if (disk.device_path == path) { | 98 OnDiskChanged(disk); |
| 105 if (disk.is_parent) { | 99 } |
| 106 if (!disk.has_media) { | 100 } |
| 107 RemoveBrowserFromVector(disk.system_path); | 101 |
| 108 } | 102 void USBMountObserver::DeviceChanged(MountLibraryEventType event, |
| 109 } else if (!obj->MountPath(path.c_str())) { | 103 const std::string& device_path) { |
| 110 RemoveBrowserFromVector(disk.system_path); | 104 if (event == MOUNT_DEVICE_ADDED) { |
| 111 } | 105 OnDeviceAdded(device_path); |
| 112 } | 106 }else if (event == MOUNT_DEVICE_REMOVED) { |
| 107 OnDeviceRemoved(device_path); |
| 108 } else if (event == MOUNT_DEVICE_SCANNED) { |
| 109 OnDeviceScanned(device_path); |
| 110 } |
| 111 } |
| 112 |
| 113 void USBMountObserver::FireFileSystemChanged( |
| 114 const std::string& web_path) { |
| 115 // TODO(zelidrag): Send message to all extensions that file system has |
| 116 // changed. |
| 117 return; |
| 118 } |
| 119 |
| 120 void USBMountObserver::OnDiskAdded(const MountLibrary::Disk* disk) { |
| 121 VLOG(1) << "Disk added: " << disk->device_path(); |
| 122 if (disk->device_path().empty()) { |
| 123 VLOG(1) << "Empty system path for " << disk->device_path(); |
| 124 return; |
| 125 } |
| 126 if (disk->is_parent()) { |
| 127 if (!disk->has_media()) |
| 128 RemoveBrowserFromVector(disk->system_path()); |
| 129 return; |
| 130 } |
| 131 |
| 132 // If disk is not mounted yet, give it a try. |
| 133 if (disk->mount_path().empty()) { |
| 134 // Initiate disk mount operation. |
| 135 chromeos::MountLibrary* lib = |
| 136 chromeos::CrosLibrary::Get()->GetMountLibrary(); |
| 137 lib->MountPath(disk->device_path().c_str()); |
| 138 } |
| 139 } |
| 140 |
| 141 void USBMountObserver::OnDiskRemoved(const MountLibrary::Disk* disk) { |
| 142 VLOG(1) << "Disk removed: " << disk->device_path(); |
| 143 RemoveBrowserFromVector(disk->system_path()); |
| 144 MountPointMap::iterator iter = mounted_devices_.find(disk->device_path()); |
| 145 if (iter == mounted_devices_.end()) |
| 146 return; |
| 147 |
| 148 chromeos::MountLibrary* lib = |
| 149 chromeos::CrosLibrary::Get()->GetMountLibrary(); |
| 150 // TODO(zelidrag): This for some reason does not work as advertized. |
| 151 // we might need to clean up mount directory on FILE thread here as well. |
| 152 lib->UnmountPath(disk->device_path().c_str()); |
| 153 |
| 154 FireFileSystemChanged(iter->second); |
| 155 mounted_devices_.erase(iter); |
| 156 } |
| 157 |
| 158 void USBMountObserver::OnDiskChanged(const MountLibrary::Disk* disk) { |
| 159 VLOG(1) << "Disk changed : " << disk->device_path(); |
| 160 if (!disk->mount_path().empty()) { |
| 161 // Remember this mount point. |
| 162 mounted_devices_.insert( |
| 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); |
| 113 } | 184 } |
| 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 } | 185 } |
| 156 } | 186 } |
| 157 | 187 |
| 188 void USBMountObserver::OnDeviceAdded(const std::string& device_path) { |
| 189 VLOG(1) << "Device added : " << device_path; |
| 190 OpenFileBrowse(kFilebrowseScanning, device_path, true); |
| 191 } |
| 192 |
| 193 void USBMountObserver::OnDeviceRemoved(const std::string& system_path) { |
| 194 // New device is added, initiate disk rescan. |
| 195 RemoveBrowserFromVector(system_path); |
| 196 } |
| 197 |
| 198 void USBMountObserver::OnDeviceScanned(const std::string& device_path) { |
| 199 VLOG(1) << "Device scanned : " << device_path; |
| 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) { |
| 206 const std::string& device_path = i->device_path; |
| 162 // Doing a substring match so that we find if this new one is a subdevice | 207 // Doing a substring match so that we find if this new one is a subdevice |
| 163 // of another already inserted device. | 208 // of another already inserted device. |
| 164 if (path.find(i->device_path) != std::string::npos) { | 209 if (path.find(device_path) != std::string::npos) { |
| 165 return i; | 210 return i; |
| 166 } | 211 } |
| 167 } | 212 } |
| 168 return browsers_.end(); | 213 return browsers_.end(); |
| 169 } | 214 } |
| 170 | 215 |
| 171 void USBMountObserver::RemoveBrowserFromVector(const std::string& path) { | 216 void USBMountObserver::RemoveBrowserFromVector(const std::string& system_path) { |
| 172 BrowserIterator i = FindBrowserForPath(path); | 217 BrowserIterator i = FindBrowserForPath(system_path); |
| 173 std::string mount_path; | 218 std::string mount_path; |
| 174 if (i != browsers_.end()) { | 219 if (i != browsers_.end()) { |
| 175 registrar_.Remove(this, | 220 registrar_.Remove(this, |
| 176 NotificationType::BROWSER_CLOSED, | 221 NotificationType::BROWSER_CLOSED, |
| 177 Source<Browser>(i->browser)); | 222 Source<Browser>(i->browser)); |
| 178 mount_path = i->mount_path; | 223 mount_path = i->mount_path; |
| 179 browsers_.erase(i); | 224 browsers_.erase(i); |
| 180 } | 225 } |
| 181 std::vector<Browser*> close_these; | 226 std::vector<Browser*> close_these; |
| 182 for (BrowserList::const_iterator it = BrowserList::begin(); | 227 for (BrowserList::const_iterator it = BrowserList::begin(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 195 } | 240 } |
| 196 } | 241 } |
| 197 for (size_t x = 0; x < close_these.size(); x++) { | 242 for (size_t x = 0; x < close_these.size(); x++) { |
| 198 if (close_these[x]->window()) { | 243 if (close_these[x]->window()) { |
| 199 close_these[x]->window()->Close(); | 244 close_these[x]->window()->Close(); |
| 200 } | 245 } |
| 201 } | 246 } |
| 202 } | 247 } |
| 203 | 248 |
| 204 } // namespace chromeos | 249 } // namespace chromeos |
| OLD | NEW |