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 <string> |
8 | 8 |
9 #include "base/bind.h" | |
9 #include "base/file_path.h" | 10 #include "base/file_path.h" |
10 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "chrome/browser/intents/web_intents_registry.h" | |
13 #include "chrome/browser/intents/web_intents_registry_factory.h" | |
11 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/browser_window.h" | 15 #include "chrome/browser/ui/browser_window.h" |
16 #include "content/public/browser/browser_thread.h" | |
13 #include "content/public/browser/web_intents_dispatcher.h" | 17 #include "content/public/browser/web_intents_dispatcher.h" |
14 #include "content/public/browser/web_contents_delegate.h" | 18 #include "content/public/browser/web_contents_delegate.h" |
15 #include "webkit/fileapi/file_system_types.h" | 19 #include "webkit/fileapi/file_system_types.h" |
16 #include "webkit/fileapi/isolated_context.h" | 20 #include "webkit/fileapi/isolated_context.h" |
17 #include "webkit/glue/web_intent_data.h" | 21 #include "webkit/glue/web_intent_data.h" |
22 #include "webkit/glue/web_intent_service_data.h" | |
18 | 23 |
19 using base::SystemMonitor; | 24 using base::SystemMonitor; |
25 using content::BrowserThread; | |
20 using content::WebContentsDelegate; | 26 using content::WebContentsDelegate; |
27 using webkit_glue::WebIntentServiceData; | |
28 | |
29 namespace { | |
30 | |
31 // Helper class to get the intent services. | |
James Hawkins
2012/08/03 17:44:59
nit: Expand this documentation.
kmadhusu
2012/08/04 01:14:47
Done.
| |
32 class DispatchIntentTaskHelper { | |
33 public: | |
34 DispatchIntentTaskHelper(DeviceAttachedIntentSource* source, | |
35 Browser* browser, | |
36 const string16& action, | |
37 const string16& intent_type, | |
38 SystemMonitor::MediaDeviceType device_type, | |
39 const FilePath& device_path) | |
40 : source_(source), | |
41 browser_(browser), | |
42 action_(action), | |
43 intent_type_(intent_type), | |
44 device_type_(device_type), | |
45 device_path_(device_path) { | |
46 } | |
47 | |
48 // Destruction happens on DidGetServicesForIntent function. | |
49 ~DispatchIntentTaskHelper() { | |
James Hawkins
2012/08/03 17:44:59
nit: {}
kmadhusu
2012/08/04 01:14:47
Done.
| |
50 } | |
51 | |
52 // Helper function to get intent services. | |
53 void GetServicesForIntent(); | |
54 | |
55 private: | |
56 void DidGetServicesForIntent( | |
James Hawkins
2012/08/03 17:44:59
Need to document that this method deletes this obj
kmadhusu
2012/08/04 01:14:47
Done.
| |
57 const std::vector<webkit_glue::WebIntentServiceData>& services); | |
58 | |
59 // Weak pointers. | |
James Hawkins
2012/08/03 17:44:59
More importantly, document what they are used for.
kmadhusu
2012/08/04 01:14:47
Done.
| |
60 DeviceAttachedIntentSource* source_; | |
61 Browser* browser_; | |
62 | |
63 const string16 action_; | |
James Hawkins
2012/08/03 17:44:59
Document member variables.
kmadhusu
2012/08/04 01:14:47
Done.
| |
64 const string16 intent_type_; | |
65 SystemMonitor::MediaDeviceType device_type_; | |
66 const FilePath device_path_; | |
67 | |
68 DISALLOW_COPY_AND_ASSIGN(DispatchIntentTaskHelper); | |
69 }; | |
70 | |
71 } // namespace | |
21 | 72 |
22 DeviceAttachedIntentSource::DeviceAttachedIntentSource( | 73 DeviceAttachedIntentSource::DeviceAttachedIntentSource( |
23 Browser* browser, WebContentsDelegate* delegate) | 74 Browser* browser, WebContentsDelegate* delegate) |
24 : browser_(browser), delegate_(delegate) { | 75 : browser_(browser), delegate_(delegate) { |
25 SystemMonitor* sys_monitor = SystemMonitor::Get(); | 76 SystemMonitor* sys_monitor = SystemMonitor::Get(); |
26 if (sys_monitor) | 77 if (sys_monitor) |
27 sys_monitor->AddDevicesChangedObserver(this); | 78 sys_monitor->AddDevicesChangedObserver(this); |
28 } | 79 } |
29 | 80 |
30 DeviceAttachedIntentSource::~DeviceAttachedIntentSource() { | 81 DeviceAttachedIntentSource::~DeviceAttachedIntentSource() { |
31 SystemMonitor* sys_monitor = SystemMonitor::Get(); | 82 SystemMonitor* sys_monitor = SystemMonitor::Get(); |
32 if (sys_monitor) | 83 if (sys_monitor) |
33 sys_monitor->RemoveDevicesChangedObserver(this); | 84 sys_monitor->RemoveDevicesChangedObserver(this); |
34 } | 85 } |
35 | 86 |
36 void DeviceAttachedIntentSource::OnMediaDeviceAttached( | 87 void DeviceAttachedIntentSource::OnMediaDeviceAttached( |
37 const std::string& id, | 88 const std::string& id, |
38 const string16& name, | 89 const string16& name, |
39 base::SystemMonitor::MediaDeviceType type, | 90 base::SystemMonitor::MediaDeviceType device_type, |
40 const FilePath::StringType& location) { | 91 const FilePath::StringType& location) { |
41 if (!browser_->window()->IsActive()) | 92 if (!browser_->window()->IsActive()) |
42 return; | 93 return; |
43 | 94 |
44 // Only handle FilePaths for now. | 95 // Only handle FilePaths for now. |
45 if (type != SystemMonitor::TYPE_PATH) | 96 if (device_type != SystemMonitor::TYPE_PATH) |
46 return; | 97 return; |
47 | 98 |
48 // Sanity checks for |device_path|. | 99 // Sanity checks for |device_path|. |
49 const FilePath device_path(location); | 100 const FilePath device_path(location); |
50 if (!device_path.IsAbsolute() || device_path.ReferencesParent()) | 101 if (!device_path.IsAbsolute() || device_path.ReferencesParent()) |
51 return; | 102 return; |
52 | 103 |
104 const string16 action = ASCIIToUTF16("chrome-extension://attach"); | |
James Hawkins
2012/08/03 17:44:59
Pull this out of this method into the unnamed name
kmadhusu
2012/08/04 01:14:47
Done.
| |
105 const string16 intent_type = ASCIIToUTF16("chrome-extension://filesystem"); | |
106 DispatchIntentTaskHelper* task = new DispatchIntentTaskHelper( | |
107 this, browser_, action, intent_type, device_type, device_path); | |
108 task->GetServicesForIntent(); | |
groby-ooo-7-16
2012/08/03 17:37:20
Please don't use a self-destructing class unless a
kmadhusu
2012/08/04 01:14:47
I need to store the device info in between calls.
| |
109 } | |
110 | |
111 void DeviceAttachedIntentSource::DispatchIntentsForService( | |
112 const string16& action, | |
113 const string16& intent_type, | |
114 base::SystemMonitor::MediaDeviceType device_type, | |
115 const FilePath& device_path) { | |
53 std::string device_name; | 116 std::string device_name; |
54 | 117 |
55 // Register device path as an isolated file system. | 118 // Register device path as an isolated file system. |
56 // TODO(kinuko, kmadhusu): Use a different file system type for MTP. | 119 // TODO(kinuko, kmadhusu): Use a different file system type for MTP. |
57 const std::string filesystem_id = | 120 const std::string fs_id = fileapi::IsolatedContext::GetInstance()-> |
58 fileapi::IsolatedContext::GetInstance()->RegisterFileSystemForPath( | 121 RegisterFileSystemForPath(fileapi::kFileSystemTypeNativeMedia, |
59 fileapi::kFileSystemTypeNativeMedia, device_path, &device_name); | 122 device_path, &device_name); |
60 | 123 |
61 CHECK(!filesystem_id.empty()); | 124 CHECK(!fs_id.empty()); |
James Hawkins
2012/08/03 17:44:59
Don't use CHECKs unless you're debugging a crash i
kmadhusu
2012/08/04 01:14:47
Done.
| |
62 webkit_glue::WebIntentData intent( | 125 webkit_glue::WebIntentData intent(action, intent_type, device_name, fs_id); |
63 ASCIIToUTF16("chrome-extension://attach"), | |
64 ASCIIToUTF16("chrome-extension://filesystem"), | |
65 device_name, | |
66 filesystem_id); | |
67 | 126 |
68 delegate_->WebIntentDispatch(NULL /* no WebContents */, | 127 delegate_->WebIntentDispatch(NULL /* no WebContents */, |
69 content::WebIntentsDispatcher::Create(intent)); | 128 content::WebIntentsDispatcher::Create(intent)); |
70 } | 129 } |
130 | |
131 void DispatchIntentTaskHelper::GetServicesForIntent() { | |
132 WebIntentsRegistryFactory::GetForProfile(browser_->profile())-> | |
133 GetIntentServices( | |
134 action_, intent_type_, | |
135 base::Bind(&DispatchIntentTaskHelper::DidGetServicesForIntent, | |
136 base::Unretained(this))); | |
137 } | |
138 | |
139 void DispatchIntentTaskHelper::DidGetServicesForIntent( | |
140 const std::vector<WebIntentServiceData>& services) { | |
141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
142 if (!services.empty()) { | |
143 BrowserThread::PostTask( | |
144 BrowserThread::UI, | |
145 FROM_HERE, | |
James Hawkins
2012/08/03 17:44:59
Optional nit: Condense parameters to save a line.
kmadhusu
2012/08/04 01:14:47
Done.
| |
146 base::Bind(&DeviceAttachedIntentSource::DispatchIntentsForService, | |
147 base::Unretained(source_), action_, intent_type_, | |
148 device_type_, device_path_)); | |
149 } | |
150 delete this; | |
James Hawkins
2012/08/03 17:44:59
What if this method is never called? Will we leak
kmadhusu
2012/08/04 01:14:47
This code will not leak. Just to be on the safer s
| |
151 } | |
OLD | NEW |