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