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 <string> | 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/media_gallery/media_storage_util.h" | 16 #include "chrome/browser/media_gallery/media_storage_util.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
(...skipping 27 matching lines...) Expand all Loading... |
45 const char kIntentType[] = "chrome-extension://filesystem"; | 45 const char kIntentType[] = "chrome-extension://filesystem"; |
46 | 46 |
47 // Dispatch intent only when there is a list of registered services. This is a | 47 // Dispatch intent only when there is a list of registered services. This is a |
48 // helper class that stores the attached media device information and decides | 48 // helper class that stores the attached media device information and decides |
49 // whether to dispatch an intent or not. | 49 // whether to dispatch an intent or not. |
50 class DispatchIntentTaskHelper | 50 class DispatchIntentTaskHelper |
51 : public base::RefCountedThreadSafe<DispatchIntentTaskHelper> { | 51 : public base::RefCountedThreadSafe<DispatchIntentTaskHelper> { |
52 public: | 52 public: |
53 DispatchIntentTaskHelper( | 53 DispatchIntentTaskHelper( |
54 const base::WeakPtr<DeviceAttachedIntentSource> source, | 54 const base::WeakPtr<DeviceAttachedIntentSource> source, |
55 SystemMonitor::MediaDeviceInfo device_info) | 55 SystemMonitor::RemovableStorageInfo device_info) |
56 : source_(source), | 56 : source_(source), |
57 device_info_(device_info) { | 57 device_info_(device_info) { |
58 } | 58 } |
59 | 59 |
60 // Query callback for WebIntentsRegistry::GetIntentServices function. | 60 // Query callback for WebIntentsRegistry::GetIntentServices function. |
61 void MayDispatchIntentForService( | 61 void MayDispatchIntentForService( |
62 const std::vector<WebIntentServiceData>& services) { | 62 const std::vector<WebIntentServiceData>& services) { |
63 if (!services.empty() && source_) | 63 if (!services.empty() && source_) |
64 source_->DispatchIntentsForService(device_info_); | 64 source_->DispatchIntentsForService(device_info_); |
65 } | 65 } |
66 | 66 |
67 private: | 67 private: |
68 friend class base::RefCountedThreadSafe<DispatchIntentTaskHelper>; | 68 friend class base::RefCountedThreadSafe<DispatchIntentTaskHelper>; |
69 | 69 |
70 ~DispatchIntentTaskHelper() {} | 70 ~DispatchIntentTaskHelper() {} |
71 | 71 |
72 // A weak pointer to |DeviceAttachedIntentSource| object to dispatch a | 72 // A weak pointer to |DeviceAttachedIntentSource| object to dispatch a |
73 // web intent. | 73 // web intent. |
74 base::WeakPtr<DeviceAttachedIntentSource> source_; | 74 base::WeakPtr<DeviceAttachedIntentSource> source_; |
75 | 75 |
76 // Store the device info. This is used while registering the device as file | 76 // Store the device info. This is used while registering the device as file |
77 // system. | 77 // system. |
78 const SystemMonitor::MediaDeviceInfo device_info_; | 78 const SystemMonitor::RemovableStorageInfo device_info_; |
79 | 79 |
80 DISALLOW_COPY_AND_ASSIGN(DispatchIntentTaskHelper); | 80 DISALLOW_COPY_AND_ASSIGN(DispatchIntentTaskHelper); |
81 }; | 81 }; |
82 | 82 |
83 } // namespace | 83 } // namespace |
84 | 84 |
85 DeviceAttachedIntentSource::DeviceAttachedIntentSource( | 85 DeviceAttachedIntentSource::DeviceAttachedIntentSource( |
86 Browser* browser, WebContentsDelegate* delegate) | 86 Browser* browser, WebContentsDelegate* delegate) |
87 : browser_(browser), delegate_(delegate) { | 87 : browser_(browser), delegate_(delegate) { |
88 SystemMonitor* sys_monitor = SystemMonitor::Get(); | 88 SystemMonitor* sys_monitor = SystemMonitor::Get(); |
89 if (sys_monitor) | 89 if (sys_monitor) |
90 sys_monitor->AddDevicesChangedObserver(this); | 90 sys_monitor->AddDevicesChangedObserver(this); |
91 } | 91 } |
92 | 92 |
93 DeviceAttachedIntentSource::~DeviceAttachedIntentSource() { | 93 DeviceAttachedIntentSource::~DeviceAttachedIntentSource() { |
94 SystemMonitor* sys_monitor = SystemMonitor::Get(); | 94 SystemMonitor* sys_monitor = SystemMonitor::Get(); |
95 if (sys_monitor) | 95 if (sys_monitor) |
96 sys_monitor->RemoveDevicesChangedObserver(this); | 96 sys_monitor->RemoveDevicesChangedObserver(this); |
97 } | 97 } |
98 | 98 |
99 void DeviceAttachedIntentSource::OnMediaDeviceAttached( | 99 void DeviceAttachedIntentSource::OnRemovableStorageAttached( |
100 const std::string& id, | 100 const std::string& id, |
101 const string16& name, | 101 const string16& name, |
102 const FilePath::StringType& location) { | 102 const FilePath::StringType& location) { |
103 if (!browser_->window()->IsActive()) | 103 if (!browser_->window()->IsActive()) |
104 return; | 104 return; |
105 | 105 |
106 // TODO(kmadhusu): Dispatch intents on incognito window. | 106 // TODO(kmadhusu): Dispatch intents on incognito window. |
107 if (browser_->profile()->IsOffTheRecord()) | 107 if (browser_->profile()->IsOffTheRecord()) |
108 return; | 108 return; |
109 | 109 |
110 // Only handle mass storage for now. | 110 // Only handle mass storage for now. |
111 // TODO(kmadhusu): Handle all device types. http://crbug.com/140353. | 111 // TODO(kmadhusu): Handle all device types. http://crbug.com/140353. |
112 MediaStorageUtil::Type type; | 112 MediaStorageUtil::Type type; |
113 MediaStorageUtil::CrackDeviceId(id, &type, NULL); | 113 MediaStorageUtil::CrackDeviceId(id, &type, NULL); |
114 if (type == MediaStorageUtil::USB_MTP) | 114 if (type == MediaStorageUtil::USB_MTP) |
115 return; | 115 return; |
116 DCHECK(MediaStorageUtil::IsRemovableDevice(id)); | 116 DCHECK(MediaStorageUtil::IsRemovableDevice(id)); |
117 | 117 |
118 // Sanity checks for |device_path|. | 118 // Sanity checks for |device_path|. |
119 const FilePath device_path(location); | 119 const FilePath device_path(location); |
120 if (!device_path.IsAbsolute() || device_path.ReferencesParent()) | 120 if (!device_path.IsAbsolute() || device_path.ReferencesParent()) |
121 return; | 121 return; |
122 | 122 |
123 SystemMonitor::MediaDeviceInfo device_info(id, name, location); | 123 SystemMonitor::RemovableStorageInfo device_info(id, name, location); |
124 scoped_refptr<DispatchIntentTaskHelper> task = new DispatchIntentTaskHelper( | 124 scoped_refptr<DispatchIntentTaskHelper> task = new DispatchIntentTaskHelper( |
125 AsWeakPtr(), device_info); | 125 AsWeakPtr(), device_info); |
126 WebIntentsRegistryFactory::GetForProfile(browser_->profile())-> | 126 WebIntentsRegistryFactory::GetForProfile(browser_->profile())-> |
127 GetIntentServices( | 127 GetIntentServices( |
128 UTF8ToUTF16(kAction), UTF8ToUTF16(kIntentType), | 128 UTF8ToUTF16(kAction), UTF8ToUTF16(kIntentType), |
129 base::Bind(&DispatchIntentTaskHelper::MayDispatchIntentForService, | 129 base::Bind(&DispatchIntentTaskHelper::MayDispatchIntentForService, |
130 task.get())); | 130 task.get())); |
131 } | 131 } |
132 | 132 |
133 void DeviceAttachedIntentSource::DispatchIntentsForService( | 133 void DeviceAttachedIntentSource::DispatchIntentsForService( |
134 const base::SystemMonitor::MediaDeviceInfo& device_info) { | 134 const base::SystemMonitor::RemovableStorageInfo& device_info) { |
135 // Store the media device info locally. | 135 // Store the media device info locally. |
136 device_id_map_.insert(std::make_pair(device_info.unique_id, device_info)); | 136 device_id_map_.insert(std::make_pair(device_info.device_id, device_info)); |
137 | 137 |
138 std::string device_name(UTF16ToUTF8(device_info.name)); | 138 std::string device_name(UTF16ToUTF8(device_info.name)); |
139 const FilePath device_path(device_info.location); | 139 const FilePath device_path(device_info.location); |
140 | 140 |
141 // TODO(kinuko, kmadhusu): Use a different file system type for MTP. | 141 // TODO(kinuko, kmadhusu): Use a different file system type for MTP. |
142 const std::string fs_id = fileapi::IsolatedContext::GetInstance()-> | 142 const std::string fs_id = fileapi::IsolatedContext::GetInstance()-> |
143 RegisterFileSystemForPath(fileapi::kFileSystemTypeNativeMedia, | 143 RegisterFileSystemForPath(fileapi::kFileSystemTypeNativeMedia, |
144 device_path, &device_name); | 144 device_path, &device_name); |
145 | 145 |
146 DCHECK(!fs_id.empty()); | 146 DCHECK(!fs_id.empty()); |
147 webkit_glue::WebIntentData intent( | 147 webkit_glue::WebIntentData intent( |
148 UTF8ToUTF16(kAction), UTF8ToUTF16(kIntentType), device_name, fs_id); | 148 UTF8ToUTF16(kAction), UTF8ToUTF16(kIntentType), device_name, fs_id); |
149 | 149 |
150 delegate_->WebIntentDispatch(NULL /* no WebContents */, | 150 delegate_->WebIntentDispatch(NULL /* no WebContents */, |
151 content::WebIntentsDispatcher::Create(intent)); | 151 content::WebIntentsDispatcher::Create(intent)); |
152 } | 152 } |
153 | 153 |
154 void DeviceAttachedIntentSource::OnMediaDeviceDetached(const std::string& id) { | 154 void DeviceAttachedIntentSource::OnRemovableStorageDetached( |
| 155 const std::string& id) { |
155 DeviceIdToInfoMap::iterator it = device_id_map_.find(id); | 156 DeviceIdToInfoMap::iterator it = device_id_map_.find(id); |
156 if (it == device_id_map_.end()) | 157 if (it == device_id_map_.end()) |
157 return; | 158 return; |
158 | 159 |
159 // TODO(kmadhusu) This should be something like | 160 // TODO(kmadhusu) This should be something like |
160 // RevokeFileSystemByDevice(std::string) | 161 // RevokeFileSystemByDevice(std::string) |
161 FilePath path(it->second.location); | 162 FilePath path(it->second.location); |
162 fileapi::IsolatedContext::GetInstance()->RevokeFileSystemByPath(path); | 163 fileapi::IsolatedContext::GetInstance()->RevokeFileSystemByPath(path); |
163 | 164 |
164 #if defined(SUPPORT_MEDIA_FILESYSTEM) | 165 #if defined(SUPPORT_MEDIA_FILESYSTEM) |
165 // TODO(kmadhusu, vandebo): Clean up this code. http://crbug.com/140340. | 166 // TODO(kmadhusu, vandebo): Clean up this code. http://crbug.com/140340. |
166 MediaStorageUtil::Type type; | 167 MediaStorageUtil::Type type; |
167 MediaStorageUtil::CrackDeviceId(it->second.unique_id, &type, NULL); | 168 MediaStorageUtil::CrackDeviceId(it->second.device_id, &type, NULL); |
168 if (type == MediaStorageUtil::USB_MTP) { | 169 if (type == MediaStorageUtil::USB_MTP) { |
169 MediaDeviceMapService::GetInstance()->RemoveMediaDevice( | 170 MediaDeviceMapService::GetInstance()->RemoveMediaDevice( |
170 it->second.location); | 171 it->second.location); |
171 } | 172 } |
172 #endif | 173 #endif |
173 device_id_map_.erase(it); | 174 device_id_map_.erase(it); |
174 } | 175 } |
OLD | NEW |