| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/intents/device_attached_intent_source.h" | 5 #include "chrome/browser/intents/device_attached_intent_source.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| 11 #include "base/string16.h" | 11 #include "base/string16.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/intents/web_intents_registry.h" | 14 #include "chrome/browser/intents/web_intents_registry.h" |
| 15 #include "chrome/browser/intents/web_intents_registry_factory.h" | 15 #include "chrome/browser/intents/web_intents_registry_factory.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/system_monitor/media_storage_util.h" | 17 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 18 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/browser/ui/browser_window.h" | 19 #include "chrome/browser/ui/browser_window.h" |
| 20 #include "content/public/browser/web_intents_dispatcher.h" | 20 #include "content/public/browser/web_intents_dispatcher.h" |
| 21 #include "content/public/browser/web_contents_delegate.h" | 21 #include "content/public/browser/web_contents_delegate.h" |
| 22 #include "webkit/fileapi/file_system_types.h" | 22 #include "webkit/fileapi/file_system_types.h" |
| 23 #include "webkit/fileapi/isolated_context.h" | 23 #include "webkit/fileapi/isolated_context.h" |
| 24 #include "webkit/glue/web_intent_data.h" | 24 #include "webkit/glue/web_intent_data.h" |
| 25 #include "webkit/glue/web_intent_service_data.h" | 25 #include "webkit/glue/web_intent_service_data.h" |
| 26 | 26 |
| 27 using base::SystemMonitor; | |
| 28 using chrome::MediaStorageUtil; | 27 using chrome::MediaStorageUtil; |
| 29 using content::WebContentsDelegate; | 28 using content::WebContentsDelegate; |
| 30 using webkit_glue::WebIntentServiceData; | 29 using webkit_glue::WebIntentServiceData; |
| 31 | 30 |
| 32 namespace { | 31 namespace { |
| 33 | 32 |
| 34 // Specifies device attached web intent kAction. | 33 // Specifies device attached web intent kAction. |
| 35 const char kAction[] = "chrome-extension://attach"; | 34 const char kAction[] = "chrome-extension://attach"; |
| 36 | 35 |
| 37 // Specifies device attached web intent type. | 36 // Specifies device attached web intent type. |
| 38 const char kIntentType[] = "chrome-extension://filesystem"; | 37 const char kIntentType[] = "chrome-extension://filesystem"; |
| 39 | 38 |
| 40 // Dispatch intent only when there is a list of registered services. This is a | 39 // Dispatch intent only when there is a list of registered services. This is a |
| 41 // helper class that stores the attached media device information and decides | 40 // helper class that stores the attached media device information and decides |
| 42 // whether to dispatch an intent or not. | 41 // whether to dispatch an intent or not. |
| 43 class DispatchIntentTaskHelper | 42 class DispatchIntentTaskHelper |
| 44 : public base::RefCountedThreadSafe<DispatchIntentTaskHelper> { | 43 : public base::RefCountedThreadSafe<DispatchIntentTaskHelper> { |
| 45 public: | 44 public: |
| 46 DispatchIntentTaskHelper( | 45 DispatchIntentTaskHelper( |
| 47 const base::WeakPtr<DeviceAttachedIntentSource> source, | 46 const base::WeakPtr<DeviceAttachedIntentSource> source, |
| 48 SystemMonitor::RemovableStorageInfo device_info) | 47 chrome::RemovableStorageNotifications::RemovableStorageInfo device_info) |
| 49 : source_(source), | 48 : source_(source), |
| 50 device_info_(device_info) { | 49 device_info_(device_info) { |
| 51 } | 50 } |
| 52 | 51 |
| 53 // Query callback for WebIntentsRegistry::GetIntentServices function. | 52 // Query callback for WebIntentsRegistry::GetIntentServices function. |
| 54 void MayDispatchIntentForService( | 53 void MayDispatchIntentForService( |
| 55 const std::vector<WebIntentServiceData>& services) { | 54 const std::vector<WebIntentServiceData>& services) { |
| 56 if (!services.empty() && source_) | 55 if (!services.empty() && source_) |
| 57 source_->DispatchIntentsForService(device_info_); | 56 source_->DispatchIntentsForService(device_info_); |
| 58 } | 57 } |
| 59 | 58 |
| 60 private: | 59 private: |
| 61 friend class base::RefCountedThreadSafe<DispatchIntentTaskHelper>; | 60 friend class base::RefCountedThreadSafe<DispatchIntentTaskHelper>; |
| 62 | 61 |
| 63 ~DispatchIntentTaskHelper() {} | 62 ~DispatchIntentTaskHelper() {} |
| 64 | 63 |
| 65 // A weak pointer to |DeviceAttachedIntentSource| object to dispatch a | 64 // A weak pointer to |DeviceAttachedIntentSource| object to dispatch a |
| 66 // web intent. | 65 // web intent. |
| 67 base::WeakPtr<DeviceAttachedIntentSource> source_; | 66 base::WeakPtr<DeviceAttachedIntentSource> source_; |
| 68 | 67 |
| 69 // Store the device info. This is used while registering the device as file | 68 // Store the device info. This is used while registering the device as file |
| 70 // system. | 69 // system. |
| 71 const SystemMonitor::RemovableStorageInfo device_info_; | 70 const chrome::RemovableStorageNotifications::RemovableStorageInfo |
| 71 device_info_; |
| 72 | 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(DispatchIntentTaskHelper); | 73 DISALLOW_COPY_AND_ASSIGN(DispatchIntentTaskHelper); |
| 74 }; | 74 }; |
| 75 | 75 |
| 76 } // namespace | 76 } // namespace |
| 77 | 77 |
| 78 DeviceAttachedIntentSource::DeviceAttachedIntentSource( | 78 DeviceAttachedIntentSource::DeviceAttachedIntentSource( |
| 79 Browser* browser, WebContentsDelegate* delegate) | 79 Browser* browser, WebContentsDelegate* delegate) |
| 80 : browser_(browser), delegate_(delegate) { | 80 : browser_(browser), delegate_(delegate) { |
| 81 SystemMonitor* sys_monitor = SystemMonitor::Get(); | 81 chrome::RemovableStorageNotifications* notifications = |
| 82 if (sys_monitor) | 82 chrome::RemovableStorageNotifications::GetInstance(); |
| 83 sys_monitor->AddDevicesChangedObserver(this); | 83 if (notifications) |
| 84 notifications->AddRemovableStorageObserver(this); |
| 84 } | 85 } |
| 85 | 86 |
| 86 DeviceAttachedIntentSource::~DeviceAttachedIntentSource() { | 87 DeviceAttachedIntentSource::~DeviceAttachedIntentSource() { |
| 87 SystemMonitor* sys_monitor = SystemMonitor::Get(); | 88 chrome::RemovableStorageNotifications* notifications = |
| 88 if (sys_monitor) | 89 chrome::RemovableStorageNotifications::GetInstance(); |
| 89 sys_monitor->RemoveDevicesChangedObserver(this); | 90 if (notifications) |
| 91 notifications->RemoveRemovableStorageObserver(this); |
| 90 } | 92 } |
| 91 | 93 |
| 92 void DeviceAttachedIntentSource::OnRemovableStorageAttached( | 94 void DeviceAttachedIntentSource::OnRemovableStorageAttached( |
| 93 const std::string& id, | 95 const std::string& id, |
| 94 const string16& name, | 96 const string16& name, |
| 95 const FilePath::StringType& location) { | 97 const FilePath::StringType& location) { |
| 96 if (!browser_->window()->IsActive()) | 98 if (!browser_->window()->IsActive()) |
| 97 return; | 99 return; |
| 98 | 100 |
| 99 // TODO(kmadhusu): Dispatch intents on incognito window. | 101 // TODO(kmadhusu): Dispatch intents on incognito window. |
| 100 if (browser_->profile()->IsOffTheRecord()) | 102 if (browser_->profile()->IsOffTheRecord()) |
| 101 return; | 103 return; |
| 102 | 104 |
| 103 // Only handle mass storage for now. | 105 // Only handle mass storage for now. |
| 104 // TODO(kmadhusu): Handle all device types. http://crbug.com/140353. | 106 // TODO(kmadhusu): Handle all device types. http://crbug.com/140353. |
| 105 if (!MediaStorageUtil::IsMassStorageDevice(id)) | 107 if (!MediaStorageUtil::IsMassStorageDevice(id)) |
| 106 return; | 108 return; |
| 107 DCHECK(MediaStorageUtil::IsRemovableDevice(id)); | 109 DCHECK(MediaStorageUtil::IsRemovableDevice(id)); |
| 108 | 110 |
| 109 // Sanity checks for |device_path|. | 111 // Sanity checks for |device_path|. |
| 110 const FilePath device_path(location); | 112 const FilePath device_path(location); |
| 111 if (!device_path.IsAbsolute() || device_path.ReferencesParent()) | 113 if (!device_path.IsAbsolute() || device_path.ReferencesParent()) |
| 112 return; | 114 return; |
| 113 | 115 |
| 114 SystemMonitor::RemovableStorageInfo device_info(id, name, location); | 116 chrome::RemovableStorageNotifications::RemovableStorageInfo device_info( |
| 117 id, name, location); |
| 115 scoped_refptr<DispatchIntentTaskHelper> task = new DispatchIntentTaskHelper( | 118 scoped_refptr<DispatchIntentTaskHelper> task = new DispatchIntentTaskHelper( |
| 116 AsWeakPtr(), device_info); | 119 AsWeakPtr(), device_info); |
| 117 WebIntentsRegistryFactory::GetForProfile(browser_->profile())-> | 120 WebIntentsRegistryFactory::GetForProfile(browser_->profile())-> |
| 118 GetIntentServices( | 121 GetIntentServices( |
| 119 UTF8ToUTF16(kAction), UTF8ToUTF16(kIntentType), | 122 UTF8ToUTF16(kAction), UTF8ToUTF16(kIntentType), |
| 120 base::Bind(&DispatchIntentTaskHelper::MayDispatchIntentForService, | 123 base::Bind(&DispatchIntentTaskHelper::MayDispatchIntentForService, |
| 121 task.get())); | 124 task.get())); |
| 122 } | 125 } |
| 123 | 126 |
| 124 void DeviceAttachedIntentSource::DispatchIntentsForService( | 127 void DeviceAttachedIntentSource::DispatchIntentsForService( |
| 125 const base::SystemMonitor::RemovableStorageInfo& device_info) { | 128 const chrome::RemovableStorageNotifications::RemovableStorageInfo& |
| 129 device_info) { |
| 126 // Store the media device info locally. | 130 // Store the media device info locally. |
| 127 device_id_map_.insert(std::make_pair(device_info.device_id, device_info)); | 131 device_id_map_.insert(std::make_pair(device_info.device_id, device_info)); |
| 128 | 132 |
| 129 std::string device_name(UTF16ToUTF8(device_info.name)); | 133 std::string device_name(UTF16ToUTF8(device_info.name)); |
| 130 const FilePath device_path(device_info.location); | 134 const FilePath device_path(device_info.location); |
| 131 | 135 |
| 132 // TODO(kinuko, kmadhusu): Use a different file system type for MTP. | 136 // TODO(kinuko, kmadhusu): Use a different file system type for MTP. |
| 133 // TODO(kmadhusu): To manage the registered file systems efficiently, register | 137 // TODO(kmadhusu): To manage the registered file systems efficiently, register |
| 134 // the attached device media file system using MediaFileSystemRegistry. | 138 // the attached device media file system using MediaFileSystemRegistry. |
| 135 const std::string fs_id = fileapi::IsolatedContext::GetInstance()-> | 139 const std::string fs_id = fileapi::IsolatedContext::GetInstance()-> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 147 void DeviceAttachedIntentSource::OnRemovableStorageDetached( | 151 void DeviceAttachedIntentSource::OnRemovableStorageDetached( |
| 148 const std::string& id) { | 152 const std::string& id) { |
| 149 DeviceIdToInfoMap::iterator it = device_id_map_.find(id); | 153 DeviceIdToInfoMap::iterator it = device_id_map_.find(id); |
| 150 if (it == device_id_map_.end()) | 154 if (it == device_id_map_.end()) |
| 151 return; | 155 return; |
| 152 | 156 |
| 153 FilePath path(it->second.location); | 157 FilePath path(it->second.location); |
| 154 fileapi::IsolatedContext::GetInstance()->RevokeFileSystemByPath(path); | 158 fileapi::IsolatedContext::GetInstance()->RevokeFileSystemByPath(path); |
| 155 device_id_map_.erase(it); | 159 device_id_map_.erase(it); |
| 156 } | 160 } |
| OLD | NEW |