Chromium Code Reviews| 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/logging.h" | |
| 9 #include "base/string16.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "chrome/browser/intents/intent_service_host.h" | |
| 12 #include "chrome/browser/intents/native_services.h" | |
| 13 #include "chrome/browser/intents/web_intents_util.h" | |
| 14 #include "chrome/browser/platform_util.h" | |
| 15 #include "content/public/browser/web_contents.h" | |
| 16 #include "content/public/browser/web_intents_dispatcher.h" | |
| 17 #include "googleurl/src/gurl.h" | |
| 18 #include "grit/generated_resources.h" | |
| 19 #include "net/base/mime_util.h" | |
| 20 #include "ui/base/dialogs/select_file_dialog.h" | |
| 21 #include "ui/base/dialogs/selected_file_info.h" | |
| 22 #include "ui/base/l10n/l10n_util.h" | |
| 23 #include "webkit/glue/web_intent_data.h" | |
| 24 #include "webkit/glue/web_intent_service_data.h" | |
| 25 | |
| 26 namespace web_intents { | |
| 27 namespace { | |
| 28 | |
| 29 // static | |
| 30 void AddTypeInfo( | |
| 31 const std::string& mime_type, | |
| 32 ui::SelectFileDialog::FileTypeInfo* info) { | |
| 33 | |
| 34 info->include_all_files = true; | |
| 35 info->extensions.resize(1); | |
| 36 std::vector<FilePath::StringType>* extensions = &info->extensions.back(); | |
| 37 | |
| 38 // Provide a "helpful" description when possible. | |
| 39 int description_id = 0; | |
| 40 if (mime_type == "image/*") | |
| 41 description_id = IDS_IMAGE_FILES; | |
| 42 else if (mime_type == "audio/*") | |
| 43 description_id = IDS_AUDIO_FILES; | |
| 44 else if (mime_type == "video/*") | |
| 45 description_id = IDS_VIDEO_FILES; | |
| 46 | |
| 47 if (description_id) | |
| 48 info->extension_description_overrides.push_back( | |
| 49 l10n_util::GetStringUTF16(description_id)); | |
| 50 | |
| 51 net::GetExtensionsForMimeType(mime_type, extensions); | |
| 52 } | |
| 53 | |
| 54 // FilePicker service allowing a native file picker to handle | |
| 55 // pick+*/* intents. | |
|
Greg Billock
2012/10/08 20:36:12
"pick + */*"
| |
| 56 class NativeFilePickerService : public IntentServiceHost { | |
| 57 public: | |
| 58 explicit NativeFilePickerService(content::WebContents* web_contents); | |
| 59 virtual void HandleIntent(content::WebIntentsDispatcher* dispatcher) OVERRIDE; | |
| 60 | |
| 61 private: | |
| 62 virtual ~NativeFilePickerService(); | |
| 63 // Weak pointer to the web contents on which we'll be displayed. | |
|
Greg Billock
2012/10/08 20:36:12
we'll --> the selector
| |
| 64 content::WebContents* web_contents_; | |
| 65 | |
| 66 DISALLOW_COPY_AND_ASSIGN(NativeFilePickerService); | |
| 67 }; | |
| 68 | |
| 69 class FileSelectedListener : public ui::SelectFileDialog::Listener { | |
|
Greg Billock
2012/10/08 20:36:12
It looks like this should be an inner class on Nat
| |
| 70 public: | |
| 71 explicit FileSelectedListener(content::WebIntentsDispatcher* dispatcher); | |
| 72 // SelectFileDialog::Listener | |
| 73 virtual void FileSelected( | |
| 74 const FilePath& path, int index, void* params) OVERRIDE; | |
| 75 | |
| 76 virtual void MultiFilesSelected( | |
| 77 const std::vector<FilePath>& files, void* params) OVERRIDE; | |
| 78 | |
| 79 virtual void FileSelectionCanceled(void* params) OVERRIDE; | |
| 80 private: | |
| 81 virtual ~FileSelectedListener(); | |
| 82 content::WebIntentsDispatcher* dispatcher_; | |
| 83 | |
| 84 DISALLOW_COPY_AND_ASSIGN(FileSelectedListener); | |
| 85 }; | |
| 86 | |
| 87 } // namespace | |
| 88 | |
| 89 FileSelectedListener::FileSelectedListener( | |
| 90 content::WebIntentsDispatcher* dispatcher) : dispatcher_(dispatcher) { | |
| 91 } | |
| 92 | |
| 93 void FileSelectedListener::FileSelected( | |
| 94 const FilePath& path, int index, void* params) { | |
| 95 dispatcher_->SendReplyMessage( | |
| 96 webkit_glue::WEB_INTENT_REPLY_SUCCESS, string16()); | |
| 97 delete this; | |
| 98 } | |
| 99 | |
| 100 void FileSelectedListener::MultiFilesSelected( | |
| 101 const std::vector<FilePath>& files, void* params) { | |
| 102 NOTREACHED(); | |
| 103 } | |
| 104 | |
| 105 void FileSelectedListener::FileSelectionCanceled(void* params) { | |
| 106 dispatcher_->SendReplyMessage( | |
| 107 webkit_glue::WEB_INTENT_REPLY_FAILURE, string16()); | |
| 108 delete this; | |
| 109 } | |
| 110 | |
| 111 FileSelectedListener::~FileSelectedListener() {} | |
| 112 | |
| 113 NativeFilePickerService::NativeFilePickerService( | |
| 114 content::WebContents* web_contents) : web_contents_(web_contents) { | |
| 115 } | |
| 116 | |
| 117 void NativeFilePickerService::HandleIntent( | |
| 118 content::WebIntentsDispatcher* dispatcher) { | |
| 119 const webkit_glue::WebIntentData& intent = dispatcher->GetIntent(); | |
| 120 | |
| 121 std::string ascii_type = UTF16ToASCII(intent.type); | |
| 122 DCHECK(!(net::GetIANAMediaType(ascii_type).empty())); | |
| 123 | |
| 124 FileSelectedListener* listener = new FileSelectedListener(dispatcher); | |
| 125 ui::SelectFileDialog* dialog = ui::SelectFileDialog::Create(listener, NULL); | |
|
Greg Billock
2012/10/08 20:36:12
Where does this get deleted? Do we need to hang on
Steve McKay
2012/10/08 22:39:53
Now ref counted.
| |
| 126 | |
| 127 ui::SelectFileDialog::FileTypeInfo type_info; | |
| 128 AddTypeInfo(ascii_type, &type_info); | |
| 129 | |
| 130 const FilePath default_path(FILE_PATH_LITERAL(".")); | |
| 131 const FilePath::StringType default_extension = FILE_PATH_LITERAL(""); | |
| 132 const string16 title = FilePickerFactory::GetServiceTitle(); | |
| 133 | |
| 134 dialog->SelectFile( | |
| 135 ui::SelectFileDialog::SELECT_OPEN_FILE, | |
| 136 title, | |
| 137 default_path, | |
| 138 &type_info, | |
| 139 1, // index of which file description to show | |
| 140 default_extension, | |
| 141 platform_util::GetTopLevel(web_contents_->GetNativeView()), | |
| 142 NULL); | |
| 143 | |
| 144 delete this; | |
|
Greg Billock
2012/10/08 20:36:12
Let's not have service hosts be self-deleting. The
Steve McKay
2012/10/08 22:39:53
For the time being, let's keep this self-deleting.
| |
| 145 } | |
| 146 | |
| 147 NativeFilePickerService::~NativeFilePickerService() {} | |
| 148 | |
| 149 // static | |
| 150 IntentServiceHost* FilePickerFactory::CreateServiceInstance( | |
| 151 content::WebContents* web_contents, | |
| 152 const webkit_glue::WebIntentData& intent) { | |
| 153 return new NativeFilePickerService(web_contents); | |
|
Greg Billock
2012/10/08 20:36:12
This leaves us without a good hook for testing. Pu
groby-ooo-7-16
2012/10/08 20:58:21
If we need to inject here for testing, I suggest l
Steve McKay
2012/10/08 22:39:53
Talked this over, can use test factory for the dia
| |
| 154 } | |
| 155 | |
| 156 // Returns the action-specific string for |action|. | |
| 157 string16 FilePickerFactory::GetServiceTitle() { | |
| 158 return l10n_util::GetStringUTF16(IDS_WEB_INTENTS_FILE_PICKER_SERVICE_TITLE); | |
| 159 } | |
| 160 | |
| 161 } // web_intents namespace | |
| OLD | NEW |