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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/intents/native_file_picker_service.cc
diff --git a/chrome/browser/ui/intents/native_file_picker_service.cc b/chrome/browser/ui/intents/native_file_picker_service.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7baf138751ca92b74c368e7b9ffbdab0785e762e
--- /dev/null
+++ b/chrome/browser/ui/intents/native_file_picker_service.cc
@@ -0,0 +1,154 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <vector>
+
+#include "base/file_path.h"
+#include "base/logging.h"
+#include "base/string16.h"
+#include "base/utf_string_conversions.h"
+#include "chrome/browser/intents/intent_service_host.h"
+#include "chrome/browser/intents/native_services.h"
+#include "chrome/browser/intents/web_intents_util.h"
+#include "chrome/browser/platform_util.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/browser/web_intents_dispatcher.h"
+#include "googleurl/src/gurl.h"
+#include "grit/generated_resources.h"
+#include "net/base/mime_util.h"
+#include "ui/base/dialogs/select_file_dialog.h"
+#include "ui/base/dialogs/selected_file_info.h"
+#include "ui/base/l10n/l10n_util.h"
+#include "webkit/glue/web_intent_data.h"
+#include "webkit/glue/web_intent_service_data.h"
+
+namespace web_intents {
+namespace {
+
+// 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.
+void AddTypeInfo(
+ const std::string& mime_type,
+ ui::SelectFileDialog::FileTypeInfo* info) {
+
+ info->include_all_files = true;
+ info->extensions.resize(1);
+ std::vector<FilePath::StringType>* extensions = &info->extensions.back();
+
+ // Provide a "helpful" description when possible.
+ int description_id = 0;
+ if (mime_type == "image/*")
+ description_id = IDS_IMAGE_FILES;
+ else if (mime_type == "audio/*")
+ description_id = IDS_AUDIO_FILES;
+ else if (mime_type == "video/*")
+ description_id = IDS_VIDEO_FILES;
+
+ 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.
+ info->extension_description_overrides.push_back(
+ l10n_util::GetStringUTF16(description_id));
+
+ 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.
+}
+
+// FilePicker service allowing a native file picker to handle
+// 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
+class NativeFilePickerService
+ : public IntentServiceHost, public ui::SelectFileDialog::Listener {
+ public:
+ explicit NativeFilePickerService(content::WebContents* web_contents);
+ virtual void HandleIntent(content::WebIntentsDispatcher* dispatcher) OVERRIDE;
+
+ // SelectFileDialog::Listener
+ 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.
+ const FilePath& path, int index, void* params) OVERRIDE;
+
+ virtual void MultiFilesSelected(
+ const std::vector<FilePath>& files, void* params) OVERRIDE;
+
+ virtual void FileSelectionCanceled(void* params) OVERRIDE;
+ private:
Greg Billock 2012/10/10 17:12:51 add blank line above "private:"
Steve McKay 2012/10/10 23:12:57 Done.
+ 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.
+ // Weak pointer to the web contents on which the selector will be displayed.
+ content::WebContents* web_contents_;
+
+ // Weak pointer to the dispatcher for the current intent. Only
+ // set at the time the intent request is delivered to HandleIntent.
+ content::WebIntentsDispatcher* dispatcher_;
+
+ scoped_refptr<ui::SelectFileDialog> dialog_;
+
+ DISALLOW_COPY_AND_ASSIGN(NativeFilePickerService);
+};
+
+} // namespace
+
+NativeFilePickerService::NativeFilePickerService(
+ content::WebContents* web_contents)
+ : web_contents_(web_contents), dispatcher_(NULL) {
+}
+
+void NativeFilePickerService::HandleIntent(
+ content::WebIntentsDispatcher* dispatcher) {
+ DCHECK(dispatcher);
+ dispatcher_ = dispatcher;
+
+ const webkit_glue::WebIntentData& intent = dispatcher_->GetIntent();
+
+ std::string ascii_type = UTF16ToASCII(intent.type);
+ DCHECK(!(net::GetIANAMediaType(ascii_type).empty()));
+
+ dialog_ = ui::SelectFileDialog::Create(this, NULL);
+
+ ui::SelectFileDialog::FileTypeInfo type_info;
+ AddTypeInfo(ascii_type, &type_info);
+
+ const FilePath default_path(FILE_PATH_LITERAL("."));
+ const FilePath::StringType default_extension = FILE_PATH_LITERAL("");
+ 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
+
+ dialog_->SelectFile(
+ ui::SelectFileDialog::SELECT_OPEN_FILE,
+ title,
+ default_path,
+ &type_info,
+ 1, // index of which file description to show
+ default_extension,
+ platform_util::GetTopLevel(web_contents_->GetNativeView()),
+ NULL);
+}
+
+void NativeFilePickerService::FileSelected(
+ const FilePath& path, int index, void* params) {
+ DCHECK(dispatcher_);
+ // 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.
+ string16 url = path.LossyDisplayName();
+ dispatcher_->SendReplyMessage(webkit_glue::WEB_INTENT_REPLY_SUCCESS, url);
+}
+
+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.
+ const std::vector<FilePath>& files, void* params) {
+ NOTREACHED();
+}
+
+void NativeFilePickerService::FileSelectionCanceled(void* params) {
+ DCHECK(dispatcher_);
+ dispatcher_->SendReplyMessage(
+ webkit_glue::WEB_INTENT_REPLY_FAILURE, string16());
+}
+
+NativeFilePickerService::~NativeFilePickerService() {}
Greg Billock 2012/10/10 17:12:51 Move under constructor
Steve McKay 2012/10/10 23:12:57 Done.
+
+// static
+IntentServiceHost* FilePickerFactory::CreateServiceInstance(
+ const webkit_glue::WebIntentData& intent,
+ content::WebContents* web_contents) {
+ return new NativeFilePickerService(web_contents);
+}
+
+// Returns the action-specific string for |action|.
+string16 FilePickerFactory::GetServiceTitle() {
+ return l10n_util::GetStringUTF16(IDS_WEB_INTENTS_FILE_PICKER_SERVICE_TITLE);
+}
+
+} // web_intents namespace

Powered by Google App Engine
This is Rietveld 408576698