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 // This file provides the UI implementation of the classes declared | |
6 // in chrome/browser/intents/native_services.h and partially defined in | |
7 // chrome/browser/intents/native_services.cc. | |
8 | |
9 #include <vector> | |
10 | |
11 #include "base/file_path.h" | |
12 #include "base/file_util.h" | |
13 #include "base/logging.h" | |
14 #include "base/platform_file.h" | |
15 #include "base/string16.h" | |
16 #include "base/utf_string_conversions.h" | |
17 #include "chrome/browser/intents/intent_service_host.h" | |
18 #include "chrome/browser/intents/native_services.h" | |
19 #include "chrome/browser/intents/web_intents_util.h" | |
20 #include "chrome/browser/platform_util.h" | |
21 #include "content/public/browser/browser_thread.h" | |
22 #include "content/public/browser/child_process_security_policy.h" | |
23 #include "content/public/browser/render_process_host.h" | |
24 #include "content/public/browser/web_contents.h" | |
25 #include "content/public/browser/web_intents_dispatcher.h" | |
26 #include "googleurl/src/gurl.h" | |
27 #include "grit/generated_resources.h" | |
28 #include "net/base/mime_util.h" | |
29 #include "ui/base/l10n/l10n_util.h" | |
30 #include "ui/shell_dialogs/select_file_dialog.h" | |
31 #include "ui/shell_dialogs/selected_file_info.h" | |
32 #include "webkit/glue/web_intent_data.h" | |
33 #include "webkit/glue/web_intent_service_data.h" | |
34 | |
35 namespace web_intents { | |
36 namespace { | |
37 | |
38 const int kInvalidFileSize = -1; | |
39 | |
40 // Returns a FileTypeInfo instance representative of |mime_type|. | |
41 ui::SelectFileDialog::FileTypeInfo TypeInfoFromMimeType( | |
42 const std::string& mime_type) { | |
43 ui::SelectFileDialog::FileTypeInfo info; | |
44 info.include_all_files = true; | |
45 info.extensions.resize(1); | |
46 net::GetExtensionsForMimeType(mime_type, &info.extensions.back()); | |
47 | |
48 // Provide a "helpful" description when possible. | |
49 int description_id = 0; | |
50 if (mime_type == "image/*") { | |
51 description_id = IDS_IMAGE_FILES; | |
52 } else if (mime_type == "audio/*") { | |
53 description_id = IDS_AUDIO_FILES; | |
54 } else if (mime_type == "video/*") { | |
55 description_id = IDS_VIDEO_FILES; | |
56 } | |
57 | |
58 if (description_id != 0) { | |
59 info.extension_description_overrides.push_back( | |
60 l10n_util::GetStringUTF16(description_id)); | |
61 } | |
62 | |
63 return info; | |
64 } | |
65 | |
66 // FilePicker service allowing a native file picker to handle | |
67 // pick + */* intents. | |
68 class NativeFilePickerService | |
69 : public IntentServiceHost, public ui::SelectFileDialog::Listener { | |
70 public: | |
71 explicit NativeFilePickerService(content::WebContents* web_contents); | |
72 virtual ~NativeFilePickerService(); | |
73 | |
74 // Reads the length of the file on the FILE thread, then returns | |
75 // the file and the length to the UI thread. | |
76 void ReadFileLength(const base::FilePath& path); | |
77 | |
78 // Handles sending of data back to dispatcher | |
79 void PostDataFileReply(const base::FilePath& path, int64 length); | |
80 | |
81 // Implements IntentServiceHost: | |
82 virtual void HandleIntent(content::WebIntentsDispatcher* dispatcher) OVERRIDE; | |
83 | |
84 // Implements SelectFileDialog::Listener | |
85 virtual void FileSelected( | |
86 const base::FilePath& path, int index, void* params) OVERRIDE; | |
87 virtual void FileSelectionCanceled(void* params) OVERRIDE; | |
88 | |
89 private: | |
90 // The web contents on which the selector will be displayed. Not owned. | |
91 content::WebContents* web_contents_; | |
92 | |
93 // The dispatcher for the current intent. Only set at the time the intent | |
94 // request is delivered to HandleIntent. Not owned. | |
95 content::WebIntentsDispatcher* dispatcher_; | |
96 | |
97 scoped_refptr<ui::SelectFileDialog> dialog_; | |
98 | |
99 DISALLOW_COPY_AND_ASSIGN(NativeFilePickerService); | |
100 }; | |
101 | |
102 } // namespace | |
103 | |
104 NativeFilePickerService::NativeFilePickerService( | |
105 content::WebContents* web_contents) | |
106 : web_contents_(web_contents), dispatcher_(NULL) { | |
107 } | |
108 | |
109 NativeFilePickerService::~NativeFilePickerService() {} | |
110 | |
111 void NativeFilePickerService::HandleIntent( | |
112 content::WebIntentsDispatcher* dispatcher) { | |
113 DCHECK(dispatcher); | |
114 dispatcher_ = dispatcher; | |
115 | |
116 dialog_ = ui::SelectFileDialog::Create(this, NULL); | |
117 | |
118 ui::SelectFileDialog::FileTypeInfo type_info = | |
119 TypeInfoFromMimeType(UTF16ToASCII(dispatcher_->GetIntent().type)); | |
120 | |
121 dialog_->SelectFile( | |
122 ui::SelectFileDialog::SELECT_OPEN_FILE, | |
123 FilePickerFactory::GetServiceTitle(), | |
124 base::FilePath(FILE_PATH_LITERAL(".")), | |
125 &type_info, | |
126 1, // Index of which file description to show. | |
127 FILE_PATH_LITERAL(""), | |
128 platform_util::GetTopLevel(web_contents_->GetNativeView()), | |
129 NULL); | |
130 } | |
131 | |
132 void NativeFilePickerService::ReadFileLength(const base::FilePath& path) { | |
133 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
134 | |
135 int64 file_size; | |
136 if (!file_util::GetFileSize(path, &file_size)) | |
137 file_size = kInvalidFileSize; | |
138 | |
139 content::BrowserThread::PostTask( | |
140 content::BrowserThread::UI, FROM_HERE, | |
141 base::Bind(&NativeFilePickerService::PostDataFileReply, | |
142 base::Unretained(this), path, file_size)); | |
143 } | |
144 | |
145 void NativeFilePickerService::PostDataFileReply( | |
146 const base::FilePath& path, int64 length) { | |
147 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
148 | |
149 if (length <= kInvalidFileSize) { | |
150 DLOG(WARNING) << "Unable to determine file size."; | |
151 dispatcher_->SendReply( | |
152 webkit_glue::WebIntentReply( | |
153 webkit_glue::WEB_INTENT_REPLY_FAILURE, string16())); | |
154 return; | |
155 } | |
156 | |
157 content::ChildProcessSecurityPolicy::GetInstance()->GrantReadFile( | |
158 web_contents_->GetRenderProcessHost()->GetID(), | |
159 path); | |
160 | |
161 dispatcher_->SendReply(webkit_glue::WebIntentReply( | |
162 webkit_glue::WEB_INTENT_REPLY_SUCCESS, path, length)); | |
163 } | |
164 | |
165 void NativeFilePickerService::FileSelected( | |
166 const base::FilePath& path, int index, void* params) { | |
167 DCHECK(dispatcher_); | |
168 content::BrowserThread::PostTask( | |
169 content::BrowserThread::FILE, | |
170 FROM_HERE, | |
171 base::Bind( | |
172 &NativeFilePickerService::ReadFileLength, | |
173 base::Unretained(this), path)); | |
174 } | |
175 | |
176 void NativeFilePickerService::FileSelectionCanceled(void* params) { | |
177 DCHECK(dispatcher_); | |
178 dispatcher_->SendReply( | |
179 webkit_glue::WebIntentReply( | |
180 webkit_glue::WEB_INTENT_REPLY_FAILURE, string16())); | |
181 } | |
182 | |
183 // static | |
184 IntentServiceHost* FilePickerFactory::CreateServiceInstance( | |
185 const webkit_glue::WebIntentData& intent, | |
186 content::WebContents* web_contents) { | |
187 return new NativeFilePickerService(web_contents); | |
188 } | |
189 | |
190 string16 FilePickerFactory::GetServiceTitle() { | |
191 return l10n_util::GetStringUTF16(IDS_WEB_INTENTS_FILE_PICKER_SERVICE_TITLE); | |
192 } | |
193 | |
194 } // namespace web_intents | |
OLD | NEW |