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