Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: chrome/browser/ui/intents/native_file_picker_service.cc

Issue 11071005: Add native file picker impl using SelectFileDialog. (Closed) Base URL: http://git.chromium.org/chromium/src.git@filePicker
Patch Set: Add functioning browser tests. Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
Greg Billock 2012/10/10 17:12:51 I don't think you need this for an anonymous metho
Steve McKay 2012/10/10 23:12:57 Done.
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)
Greg Billock 2012/10/10 17:12:51 braces around multi-line statement
Steve McKay 2012/10/10 23:12:57 Done.
48 info->extension_description_overrides.push_back(
49 l10n_util::GetStringUTF16(description_id));
50
51 net::GetExtensionsForMimeType(mime_type, extensions);
Greg Billock 2012/10/10 17:12:51 Can this be moved above the description stuff? It
Steve McKay 2012/10/10 23:12:57 Done.
52 }
53
54 // FilePicker service allowing a native file picker to handle
55 // pick + */* intents.
Greg Billock 2012/10/10 17:12:51 It'll be the creator's responsibility to make sure
Steve McKay 2012/10/10 23:12:57 Done. Just simply stated that the creator should b
56 class NativeFilePickerService
57 : public IntentServiceHost, public ui::SelectFileDialog::Listener {
58 public:
59 explicit NativeFilePickerService(content::WebContents* web_contents);
60 virtual void HandleIntent(content::WebIntentsDispatcher* dispatcher) OVERRIDE;
61
62 // SelectFileDialog::Listener
63 virtual void FileSelected(
Greg Billock 2012/10/10 17:12:51 Eliminate whitespace between all these overrides.
Steve McKay 2012/10/10 23:12:57 Done.
Steve McKay 2012/10/10 23:12:57 Done.
64 const FilePath& path, int index, void* params) OVERRIDE;
65
66 virtual void MultiFilesSelected(
67 const std::vector<FilePath>& files, void* params) OVERRIDE;
68
69 virtual void FileSelectionCanceled(void* params) OVERRIDE;
70 private:
Greg Billock 2012/10/10 17:12:51 add blank line above "private:"
Steve McKay 2012/10/10 23:12:57 Done.
71 virtual ~NativeFilePickerService();
Greg Billock 2012/10/10 17:12:51 Destructor should now be public, no?
Steve McKay 2012/10/10 23:12:57 Done.
72 // Weak pointer to the web contents on which the selector will be displayed.
73 content::WebContents* web_contents_;
74
75 // Weak pointer to the dispatcher for the current intent. Only
76 // set at the time the intent request is delivered to HandleIntent.
77 content::WebIntentsDispatcher* dispatcher_;
78
79 scoped_refptr<ui::SelectFileDialog> dialog_;
80
81 DISALLOW_COPY_AND_ASSIGN(NativeFilePickerService);
82 };
83
84 } // namespace
85
86 NativeFilePickerService::NativeFilePickerService(
87 content::WebContents* web_contents)
88 : web_contents_(web_contents), dispatcher_(NULL) {
89 }
90
91 void NativeFilePickerService::HandleIntent(
92 content::WebIntentsDispatcher* dispatcher) {
93 DCHECK(dispatcher);
94 dispatcher_ = dispatcher;
95
96 const webkit_glue::WebIntentData& intent = dispatcher_->GetIntent();
97
98 std::string ascii_type = UTF16ToASCII(intent.type);
99 DCHECK(!(net::GetIANAMediaType(ascii_type).empty()));
100
101 dialog_ = ui::SelectFileDialog::Create(this, NULL);
102
103 ui::SelectFileDialog::FileTypeInfo type_info;
104 AddTypeInfo(ascii_type, &type_info);
105
106 const FilePath default_path(FILE_PATH_LITERAL("."));
107 const FilePath::StringType default_extension = FILE_PATH_LITERAL("");
108 const string16 title = FilePickerFactory::GetServiceTitle();
Greg Billock 2012/10/10 17:12:51 Will this be the same as the title in the picker?
Steve McKay 2012/10/10 23:12:57 Up to the implementation, but that is how it works
109
110 dialog_->SelectFile(
111 ui::SelectFileDialog::SELECT_OPEN_FILE,
112 title,
113 default_path,
114 &type_info,
115 1, // index of which file description to show
116 default_extension,
117 platform_util::GetTopLevel(web_contents_->GetNativeView()),
118 NULL);
119 }
120
121 void NativeFilePickerService::FileSelected(
122 const FilePath& path, int index, void* params) {
123 DCHECK(dispatcher_);
124 // Return the readable path until we can return a real blob.
Greg Billock 2012/10/10 17:12:51 Add a TODO that we want to return the FilePath. (C
Steve McKay 2012/10/10 23:12:57 Done.
125 string16 url = path.LossyDisplayName();
126 dispatcher_->SendReplyMessage(webkit_glue::WEB_INTENT_REPLY_SUCCESS, url);
127 }
128
129 void NativeFilePickerService::MultiFilesSelected(
Greg Billock 2012/10/10 17:12:51 This is an empty impl in the superclass. Just leav
Steve McKay 2012/10/10 23:12:57 Done.
130 const std::vector<FilePath>& files, void* params) {
131 NOTREACHED();
132 }
133
134 void NativeFilePickerService::FileSelectionCanceled(void* params) {
135 DCHECK(dispatcher_);
136 dispatcher_->SendReplyMessage(
137 webkit_glue::WEB_INTENT_REPLY_FAILURE, string16());
138 }
139
140 NativeFilePickerService::~NativeFilePickerService() {}
Greg Billock 2012/10/10 17:12:51 Move under constructor
Steve McKay 2012/10/10 23:12:57 Done.
141
142 // static
143 IntentServiceHost* FilePickerFactory::CreateServiceInstance(
144 const webkit_glue::WebIntentData& intent,
145 content::WebContents* web_contents) {
146 return new NativeFilePickerService(web_contents);
147 }
148
149 // Returns the action-specific string for |action|.
150 string16 FilePickerFactory::GetServiceTitle() {
151 return l10n_util::GetStringUTF16(IDS_WEB_INTENTS_FILE_PICKER_SERVICE_TITLE);
152 }
153
154 } // web_intents namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698