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

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: Don't use "DeleteService" as a method name since Windows munges it at compile time. 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 void AddTypeInfo(
30 const std::string& mime_type,
31 ui::SelectFileDialog::FileTypeInfo* info) {
32
33 info->include_all_files = true;
34 info->extensions.resize(1);
35 net::GetExtensionsForMimeType(mime_type, &info->extensions.back());
36
37 // Provide a "helpful" description when possible.
38 int description_id = 0;
39 if (mime_type == "image/*")
40 description_id = IDS_IMAGE_FILES;
41 else if (mime_type == "audio/*")
42 description_id = IDS_AUDIO_FILES;
43 else if (mime_type == "video/*")
44 description_id = IDS_VIDEO_FILES;
45
46 if (description_id) {
47 info->extension_description_overrides.push_back(
48 l10n_util::GetStringUTF16(description_id));
49 }
50 }
51
52 // FilePicker service allowing a native file picker to handle
53 // pick + */* intents.
54 class NativeFilePickerService
55 : public IntentServiceHost, public ui::SelectFileDialog::Listener {
56 public:
57 explicit NativeFilePickerService(content::WebContents* web_contents);
58 virtual ~NativeFilePickerService();
59 virtual void HandleIntent(content::WebIntentsDispatcher* dispatcher) OVERRIDE;
60
61 // SelectFileDialog::Listener
62 virtual void FileSelected(
63 const FilePath& path, int index, void* params) OVERRIDE;
64 virtual void FileSelectionCanceled(void* params) OVERRIDE;
65
66 private:
67 // Weak pointer to the web contents on which the selector will be displayed.
68 content::WebContents* web_contents_;
69
70 // Weak pointer to the dispatcher for the current intent. Only
71 // set at the time the intent request is delivered to HandleIntent.
72 content::WebIntentsDispatcher* dispatcher_;
73
74 scoped_refptr<ui::SelectFileDialog> dialog_;
75
76 DISALLOW_COPY_AND_ASSIGN(NativeFilePickerService);
77 };
78
79 } // namespace
80
81 NativeFilePickerService::NativeFilePickerService(
82 content::WebContents* web_contents)
83 : web_contents_(web_contents), dispatcher_(NULL) {
84 }
85
86 NativeFilePickerService::~NativeFilePickerService() {}
87
88 void NativeFilePickerService::HandleIntent(
89 content::WebIntentsDispatcher* dispatcher) {
90 DCHECK(dispatcher);
91 dispatcher_ = dispatcher;
92
93 const webkit_glue::WebIntentData& intent = dispatcher_->GetIntent();
94
95 std::string ascii_type = UTF16ToASCII(intent.type);
96 DCHECK(!(net::GetIANAMediaType(ascii_type).empty()));
97
98 dialog_ = ui::SelectFileDialog::Create(this, NULL);
99
100 ui::SelectFileDialog::FileTypeInfo type_info;
101 AddTypeInfo(ascii_type, &type_info);
102
103 const FilePath default_path(FILE_PATH_LITERAL("."));
104 const FilePath::StringType default_extension = FILE_PATH_LITERAL("");
105 const string16 title = FilePickerFactory::GetServiceTitle();
106
107 dialog_->SelectFile(
108 ui::SelectFileDialog::SELECT_OPEN_FILE,
109 title,
110 default_path,
111 &type_info,
112 1, // index of which file description to show
113 default_extension,
114 platform_util::GetTopLevel(web_contents_->GetNativeView()),
115 NULL);
116 }
117
118 void NativeFilePickerService::FileSelected(
119 const FilePath& path, int index, void* params) {
120 DCHECK(dispatcher_);
121 // TODO(smckay): once we've worked out the RPCs, we'll return the FilePath.
122 string16 url = path.LossyDisplayName();
123 dispatcher_->SendReplyMessage(webkit_glue::WEB_INTENT_REPLY_SUCCESS, url);
124 }
125
126 void NativeFilePickerService::FileSelectionCanceled(void* params) {
127 DCHECK(dispatcher_);
128 dispatcher_->SendReplyMessage(
129 webkit_glue::WEB_INTENT_REPLY_FAILURE, string16());
130 }
131
132 // static
133 IntentServiceHost* FilePickerFactory::CreateServiceInstance(
134 const webkit_glue::WebIntentData& intent,
135 content::WebContents* web_contents) {
136 return new NativeFilePickerService(web_contents);
137 }
138
139 // Returns the action-specific string for |action|.
140 string16 FilePickerFactory::GetServiceTitle() {
141 return l10n_util::GetStringUTF16(IDS_WEB_INTENTS_FILE_PICKER_SERVICE_TITLE);
142 }
143
144 } // web_intents namespace
OLDNEW
« no previous file with comments | « chrome/browser/intents/native_services_unittest.cc ('k') | chrome/browser/ui/intents/web_intent_picker_controller.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698