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 "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/system_monitor/system_monitor.h" | |
Lei Zhang
2012/08/01 23:49:05
nit: It's very unlikely you'll ever remove this in
kmadhusu
2012/08/02 16:59:03
Done.
| |
8 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
10 #include "chrome/browser/ui/browser_window.h" | 11 #include "chrome/browser/ui/browser_window.h" |
11 #include "content/public/browser/web_intents_dispatcher.h" | 12 #include "content/public/browser/web_intents_dispatcher.h" |
12 #include "content/public/browser/web_contents_delegate.h" | 13 #include "content/public/browser/web_contents_delegate.h" |
13 #include "webkit/fileapi/file_system_types.h" | 14 #include "webkit/fileapi/file_system_types.h" |
14 #include "webkit/fileapi/isolated_context.h" | 15 #include "webkit/fileapi/isolated_context.h" |
15 #include "webkit/glue/web_intent_data.h" | 16 #include "webkit/glue/web_intent_data.h" |
17 #include "webkit/fileapi/media/media_file_system_config.h" | |
18 | |
19 #if defined(SUPPORT_MEDIA_FILESYSTEM) | |
20 #include "webkit/fileapi/media/media_device_map_service.h" | |
21 | |
22 using fileapi::MediaDeviceMapService; | |
23 #endif | |
16 | 24 |
17 using base::SystemMonitor; | 25 using base::SystemMonitor; |
18 using content::WebContentsDelegate; | 26 using content::WebContentsDelegate; |
19 | 27 |
20 DeviceAttachedIntentSource::DeviceAttachedIntentSource( | 28 DeviceAttachedIntentSource::DeviceAttachedIntentSource( |
21 Browser* browser, WebContentsDelegate* delegate) | 29 Browser* browser, WebContentsDelegate* delegate) |
22 : browser_(browser), delegate_(delegate) { | 30 : browser_(browser), delegate_(delegate) { |
23 SystemMonitor* sys_monitor = SystemMonitor::Get(); | 31 SystemMonitor* sys_monitor = SystemMonitor::Get(); |
24 if (sys_monitor) | 32 if (sys_monitor) |
25 sys_monitor->AddDevicesChangedObserver(this); | 33 sys_monitor->AddDevicesChangedObserver(this); |
26 } | 34 } |
27 | 35 |
28 DeviceAttachedIntentSource::~DeviceAttachedIntentSource() { | 36 DeviceAttachedIntentSource::~DeviceAttachedIntentSource() { |
29 SystemMonitor* sys_monitor = SystemMonitor::Get(); | 37 SystemMonitor* sys_monitor = SystemMonitor::Get(); |
30 if (sys_monitor) | 38 if (sys_monitor) |
31 sys_monitor->RemoveDevicesChangedObserver(this); | 39 sys_monitor->RemoveDevicesChangedObserver(this); |
32 } | 40 } |
33 | 41 |
34 void DeviceAttachedIntentSource::OnMediaDeviceAttached( | 42 void DeviceAttachedIntentSource::OnMediaDeviceAttached( |
35 const std::string& id, | 43 const std::string& id, |
36 const string16& name, | 44 const string16& name, |
37 base::SystemMonitor::MediaDeviceType type, | 45 base::SystemMonitor::MediaDeviceType type, |
38 const FilePath::StringType& location) { | 46 const FilePath::StringType& location) { |
39 if (!browser_->window()->IsActive()) | 47 if (!browser_->window()->IsActive()) |
40 return; | 48 return; |
41 | 49 |
42 // Only handle FilePaths for now. | 50 // Only handle FilePaths for now. |
51 // TODO(kmadhusu): Handle all device types. | |
43 if (type != SystemMonitor::TYPE_PATH) | 52 if (type != SystemMonitor::TYPE_PATH) |
44 return; | 53 return; |
45 | 54 |
46 // Sanity checks for |device_path|. | 55 // Sanity checks for |device_path|. |
47 const FilePath device_path(location); | 56 const FilePath device_path(location); |
48 if (!device_path.IsAbsolute() || device_path.ReferencesParent()) | 57 if (!device_path.IsAbsolute() || device_path.ReferencesParent()) |
49 return; | 58 return; |
50 | 59 |
60 // Store the media device info locally. | |
61 SystemMonitor::MediaDeviceInfo device_info(id, name, type, location); | |
62 device_id_map_.insert(std::make_pair(id, device_info)); | |
63 | |
51 std::string device_name; | 64 std::string device_name; |
52 | 65 |
53 // Register device path as an isolated file system. | 66 // Register device path as an isolated file system. |
54 // TODO(kinuko, kmadhusu): Use a different file system type for MTP. | 67 // TODO(kinuko, kmadhusu): Use a different file system type for MTP. |
55 const std::string filesystem_id = | 68 const std::string filesystem_id = |
56 fileapi::IsolatedContext::GetInstance()->RegisterFileSystemForPath( | 69 fileapi::IsolatedContext::GetInstance()->RegisterFileSystemForPath( |
57 fileapi::kFileSystemTypeIsolated, device_path, &device_name); | 70 fileapi::kFileSystemTypeIsolated, device_path, &device_name); |
58 | 71 |
59 CHECK(!filesystem_id.empty()); | 72 CHECK(!filesystem_id.empty()); |
60 webkit_glue::WebIntentData intent( | 73 webkit_glue::WebIntentData intent( |
61 ASCIIToUTF16("chrome-extension://attach"), | 74 ASCIIToUTF16("chrome-extension://attach"), |
62 ASCIIToUTF16("chrome-extension://filesystem"), | 75 ASCIIToUTF16("chrome-extension://filesystem"), |
63 device_name, | 76 device_name, |
64 filesystem_id); | 77 filesystem_id); |
65 | 78 |
66 delegate_->WebIntentDispatch(NULL /* no WebContents */, | 79 delegate_->WebIntentDispatch(NULL /* no WebContents */, |
67 content::WebIntentsDispatcher::Create(intent)); | 80 content::WebIntentsDispatcher::Create(intent)); |
68 } | 81 } |
82 | |
83 void DeviceAttachedIntentSource::OnMediaDeviceDetached(const std::string& id) { | |
84 DeviceIdToInfoMap::iterator it = device_id_map_.find(id); | |
85 if (it == device_id_map_.end()) | |
86 return; | |
87 | |
88 FilePath path(it->second.location); | |
89 // TODO(kinuko, kmadhusu): Revoke file system by path. | |
90 // fileapi::IsolatedContext::GetInstance()->RevokeFileSystemByPath(path); | |
91 switch (it->second.type) { | |
92 case SystemMonitor::TYPE_MTP: | |
Lei Zhang
2012/08/01 23:49:05
This is currently dead code, since you've returned
kmadhusu
2012/08/02 16:59:03
I understand that this is a dead code now, but I a
| |
93 #if defined(SUPPORT_MEDIA_FILESYSTEM) | |
94 MediaDeviceMapService::GetInstance()->RemoveMediaDevice( | |
Lei Zhang
2012/08/01 23:49:05
Also, why do this here? Don't you already call Rem
kmadhusu
2012/08/02 16:59:03
MediaFileSystemRegistry::RevokeMediaFileSystem han
| |
95 it->second.location); | |
96 #endif | |
97 break; | |
98 default: | |
Lei Zhang
2012/08/01 23:49:05
You should use TYPE_PATH here, so if anyone ever a
kmadhusu
2012/08/02 16:59:03
Done.
| |
99 break; | |
100 } | |
101 device_id_map_.erase(it); | |
102 } | |
OLD | NEW |