| 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(); |
| 68 if (!CommandLine::ForCurrentProcess()->HasSwitch( | 61 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 69 switches::kEnableAdvancedFileSystem)) { | 62 switches::kEnableAdvancedFileSystem)) { |
| 70 return; | 63 return; |
| 71 } | 64 } |
| 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, | 77 static bool registered = false; |
| 85 Source<Browser>(browser)); | 78 if (!registered) { |
| 79 registrar_.Add(this, |
| 80 NotificationType::BROWSER_CLOSED, |
| 81 Source<Browser>(browser)); |
| 82 registered =-true; |
| 83 } |
| 84 |
| 86 BrowserIterator iter = FindBrowserForPath(device_path); | 85 BrowserIterator iter = FindBrowserForPath(device_path); |
| 87 if (iter == browsers_.end()) { | 86 if (iter == browsers_.end()) { |
| 88 BrowserWithPath new_browser; | 87 BrowserWithPath new_browser; |
| 89 new_browser.browser = browser; | 88 new_browser.browser = browser; |
| 90 new_browser.device_path = device_path; | 89 new_browser.device_path = device_path; |
| 91 browsers_.push_back(new_browser); | 90 browsers_.push_back(new_browser); |
| 92 } else { | 91 } else { |
| 93 iter->browser = browser; | 92 iter->browser = browser; |
| 94 } | 93 } |
| 95 } | 94 } |
| 96 | 95 |
| 97 void USBMountObserver::MountChanged(chromeos::MountLibrary* obj, | 96 void USBMountObserver::DiskChanged(MountLibraryEventType event, |
| 98 chromeos::MountEventType evt, | 97 const MountLibrary::Disk* disk) { |
| 99 const std::string& path) { | 98 if (event == MOUNT_DISK_ADDED) { |
| 100 if (evt == chromeos::DISK_ADDED) { | 99 OnDiskAdded(disk); |
| 101 const chromeos::MountLibrary::DiskVector& disks = obj->disks(); | 100 } else if (event == MOUNT_DISK_REMOVED) { |
| 102 for (size_t i = 0; i < disks.size(); ++i) { | 101 OnDiskRemoved(disk); |
| 103 chromeos::MountLibrary::Disk disk = disks[i]; | 102 } else if (event == MOUNT_DISK_CHANGED) { |
| 104 if (disk.device_path == path) { | 103 OnDiskChanged(disk); |
| 105 if (disk.is_parent) { | 104 } |
| 106 if (!disk.has_media) { | 105 } |
| 107 RemoveBrowserFromVector(disk.system_path); | 106 |
| 108 } | 107 void USBMountObserver::DeviceChanged(MountLibraryEventType event, |
| 109 } else if (!obj->MountPath(path.c_str())) { | 108 const std::string& device_path) { |
| 110 RemoveBrowserFromVector(disk.system_path); | 109 if (event == MOUNT_DEVICE_ADDED) { |
| 111 } | 110 OnDeviceAdded(device_path); |
| 112 } | 111 }else if (event == MOUNT_DEVICE_REMOVED) { |
| 112 OnDeviceRemoved(device_path); |
| 113 } else if (event == MOUNT_DEVICE_SCANNED) { |
| 114 OnDeviceScanned(device_path); |
| 115 } |
| 116 } |
| 117 |
| 118 void USBMountObserver::FireFileSystemChanged( |
| 119 const std::string& web_path) { |
| 120 // TODO(zelidrag): Send message to all extensions that file system has |
| 121 // changed. |
| 122 return; |
| 123 } |
| 124 |
| 125 void USBMountObserver::OnDiskAdded(const MountLibrary::Disk* disk) { |
| 126 VLOG(1) << "Disk added: " << disk->device_path(); |
| 127 if (disk->device_path().empty()) { |
| 128 VLOG(1) << "Empty system path for " << disk->device_path(); |
| 129 return; |
| 130 } |
| 131 if (disk->is_parent()) { |
| 132 if (!disk->has_media()) |
| 133 RemoveBrowserFromVector(disk->system_path()); |
| 134 return; |
| 135 } |
| 136 |
| 137 // If disk is not mounted yet, give it a try. |
| 138 if (disk->mount_path().empty()) { |
| 139 // Initiate disk mount operation. |
| 140 chromeos::MountLibrary* lib = |
| 141 chromeos::CrosLibrary::Get()->GetMountLibrary(); |
| 142 lib->MountPath(disk->device_path().c_str()); |
| 143 } |
| 144 } |
| 145 |
| 146 void USBMountObserver::OnDiskRemoved(const MountLibrary::Disk* disk) { |
| 147 VLOG(1) << "Disk removed: " << disk->device_path(); |
| 148 RemoveBrowserFromVector(disk->device_path()); |
| 149 MountPointMap::iterator iter = mounted_devices_.find(disk->device_path()); |
| 150 if (iter == mounted_devices_.end()) |
| 151 return; |
| 152 |
| 153 chromeos::MountLibrary* lib = |
| 154 chromeos::CrosLibrary::Get()->GetMountLibrary(); |
| 155 // TODO(zelidrag): This for some reason does not work as advertized. |
| 156 // we might need to clean up mount directory on FILE thread here as well. |
| 157 lib->UnmountPath(disk->device_path().c_str()); |
| 158 |
| 159 FireFileSystemChanged(iter->second); |
| 160 mounted_devices_.erase(iter); |
| 161 } |
| 162 |
| 163 void USBMountObserver::OnDiskChanged(const MountLibrary::Disk* disk) { |
| 164 VLOG(1) << "Disk changed : " << disk->device_path(); |
| 165 if (!disk->mount_path().empty()) { |
| 166 // Remember this mount point. |
| 167 mounted_devices_.insert( |
| 168 std::pair<std::string, std::string>(disk->device_path(), |
| 169 disk->mount_path())); |
| 170 FireFileSystemChanged(disk->mount_path()); |
| 171 |
| 172 // TODO(zelidrag): We should remove old file browser stuff later. |
| 173 // Doing second search to see if the current disk has already |
| 174 // been popped up due to its parent device being plugged in. |
| 175 BrowserIterator iter = FindBrowserForPath(disk->system_path()); |
| 176 if (iter != browsers_.end() && iter->browser) { |
| 177 std::string url = kFilebrowseURLHash; |
| 178 url += disk->mount_path(); |
| 179 TabContents* tab = iter->browser->GetSelectedTabContents(); |
| 180 iter->browser->window()->SetBounds(gfx::Rect( |
| 181 0, 0, FileBrowseUI::kPopupWidth, FileBrowseUI::kPopupHeight)); |
| 182 tab->OpenURL(GURL(url), GURL(), CURRENT_TAB, |
| 183 PageTransition::LINK); |
| 184 tab->NavigateToPendingEntry(NavigationController::RELOAD); |
| 185 iter->device_path = disk->device_path(); |
| 186 iter->mount_path = disk->mount_path(); |
| 187 } else { |
| 188 OpenFileBrowse(disk->mount_path(), disk->device_path(), false); |
| 113 } | 189 } |
| 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 } | 190 } |
| 156 } | 191 } |
| 157 | 192 |
| 193 void USBMountObserver::OnDeviceAdded(const std::string& device_path) { |
| 194 VLOG(1) << "Device added : " << device_path; |
| 195 OpenFileBrowse(kFilebrowseScanning, device_path, true); |
| 196 } |
| 197 |
| 198 void USBMountObserver::OnDeviceRemoved(const std::string& device_path) { |
| 199 // New device is added, initiate disk rescan. |
| 200 RemoveBrowserFromVector(device_path); |
| 201 } |
| 202 |
| 203 void USBMountObserver::OnDeviceScanned(const std::string& device_path) { |
| 204 VLOG(1) << "Device scanned : " << device_path; |
| 205 } |
| 206 |
| 158 USBMountObserver::BrowserIterator USBMountObserver::FindBrowserForPath( | 207 USBMountObserver::BrowserIterator USBMountObserver::FindBrowserForPath( |
| 159 const std::string& path) { | 208 const std::string& path) { |
| 160 for (BrowserIterator i = browsers_.begin();i != browsers_.end(); | 209 for (BrowserIterator i = browsers_.begin();i != browsers_.end(); |
| 161 ++i) { | 210 ++i) { |
| 162 // Doing a substring match so that we find if this new one is a subdevice | 211 // Doing a substring match so that we find if this new one is a subdevice |
| 163 // of another already inserted device. | 212 // of another already inserted device. |
| 164 if (path.find(i->device_path) != std::string::npos) { | 213 if (path.find(i->device_path) != std::string::npos) { |
| 165 return i; | 214 return i; |
| 166 } | 215 } |
| 167 } | 216 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 195 } | 244 } |
| 196 } | 245 } |
| 197 for (size_t x = 0; x < close_these.size(); x++) { | 246 for (size_t x = 0; x < close_these.size(); x++) { |
| 198 if (close_these[x]->window()) { | 247 if (close_these[x]->window()) { |
| 199 close_these[x]->window()->Close(); | 248 close_these[x]->window()->Close(); |
| 200 } | 249 } |
| 201 } | 250 } |
| 202 } | 251 } |
| 203 | 252 |
| 204 } // namespace chromeos | 253 } // namespace chromeos |
| OLD | NEW |