This implements the PPB_FileChooser resource as a new-style IPC-only resource.

Note that the new file name is file_chooser_resource in the proxy. I decided to drop the ppb_ prefix for the "new-style" files to help differentiate them, and also because it's technically wrong. PPB is an interface, and a resource "object" may support multiple interfaces. I think FooResource is easier to type and read.

Review URL: https://chromiumcodereview.appspot.com/10544089

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@146737 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/ppapi/proxy/file_chooser_resource.cc b/ppapi/proxy/file_chooser_resource.cc
new file mode 100644
index 0000000..f4e21ec
--- /dev/null
+++ b/ppapi/proxy/file_chooser_resource.cc
@@ -0,0 +1,149 @@
+// 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 "ppapi/proxy/file_chooser_resource.h"
+
+#include "base/string_split.h"
+#include "ipc/ipc_message.h"
+#include "ppapi/c/pp_errors.h"
+#include "ppapi/proxy/ppapi_messages.h"
+#include "ppapi/proxy/ppb_file_ref_proxy.h"
+#include "ppapi/shared_impl/var.h"
+
+namespace ppapi {
+namespace proxy {
+
+FileChooserResource::FileChooserResource(IPC::Sender* sender,
+                                         PP_Instance instance,
+                                         PP_FileChooserMode_Dev mode,
+                                         const std::string& accept_types)
+    : PluginResource(sender, instance),
+      mode_(mode) {
+  PopulateAcceptTypes(accept_types, &accept_types_);
+}
+
+FileChooserResource::~FileChooserResource() {
+}
+
+thunk::PPB_FileChooser_API* FileChooserResource::AsPPB_FileChooser_API() {
+  return this;
+}
+
+int32_t FileChooserResource::Show(const PP_ArrayOutput& output,
+                                  scoped_refptr<TrackedCallback> callback) {
+  return ShowWithoutUserGesture(PP_FALSE, PP_MakeUndefined(), output, callback);
+}
+
+int32_t FileChooserResource::ShowWithoutUserGesture(
+    PP_Bool save_as,
+    PP_Var suggested_file_name,
+    const PP_ArrayOutput& output,
+    scoped_refptr<TrackedCallback> callback) {
+  int32_t result = ShowInternal(PP_FALSE, suggested_file_name, callback);
+  if (result == PP_OK_COMPLETIONPENDING)
+    output_.set_pp_array_output(output);
+  return result;
+}
+
+int32_t FileChooserResource::Show0_5(scoped_refptr<TrackedCallback> callback) {
+  return ShowInternal(PP_FALSE, PP_MakeUndefined(), callback);
+}
+
+PP_Resource FileChooserResource::GetNextChosenFile() {
+ if (file_queue_.empty())
+    return 0;
+
+  // Return the next resource in the queue. It will already have been addrefed
+  // (they're currently owned by the FileChooser) and returning it transfers
+  // ownership of that reference to the plugin.
+  PP_Resource next = file_queue_.front();
+  file_queue_.pop();
+  return next;
+}
+
+int32_t FileChooserResource::ShowWithoutUserGesture0_5(
+    PP_Bool save_as,
+    PP_Var suggested_file_name,
+    scoped_refptr<TrackedCallback> callback) {
+  return ShowInternal(save_as, suggested_file_name, callback);
+}
+
+// static
+void FileChooserResource::PopulateAcceptTypes(
+    const std::string& input,
+    std::vector<std::string>* output) {
+  if (input.empty())
+    return;
+
+  std::vector<std::string> type_list;
+  base::SplitString(input, ',', &type_list);
+  output->reserve(type_list.size());
+
+  for (size_t i = 0; i < type_list.size(); ++i) {
+    std::string type = type_list[i];
+    TrimWhitespaceASCII(type, TRIM_ALL, &type);
+
+    // If the type is a single character, it definitely cannot be valid. In the
+    // case of a file extension it would be a single ".". In the case of a MIME
+    // type it would just be a "/".
+    if (type.length() < 2)
+      continue;
+    if (type.find_first_of('/') == std::string::npos && type[0] != '.')
+      continue;
+    StringToLowerASCII(&type);
+    output->push_back(type);
+  }
+}
+
+void FileChooserResource::OnReplyReceived(int /* sequence */,
+                                          int32_t result,
+                                          const IPC::Message& msg) {
+  PpapiPluginMsg_FileChooser_ShowReply::Schema::Param param;
+  PpapiPluginMsg_FileChooser_ShowReply::Read(&msg, &param);
+  const std::vector<ppapi::PPB_FileRef_CreateInfo>& chosen_files = param.a;
+
+  if (output_.is_valid()) {
+    // Using v0.6 of the API with the output array.
+    std::vector<PP_Resource> files;
+    for (size_t i = 0; i < chosen_files.size(); i++)
+      files.push_back(PPB_FileRef_Proxy::DeserializeFileRef(chosen_files[i]));
+    output_.StoreResourceVector(files);
+  } else {
+    // Convert each of the passed in file infos to resources. These will be
+    // owned by the FileChooser object until they're passed to the plugin.
+    DCHECK(file_queue_.empty());
+    for (size_t i = 0; i < chosen_files.size(); i++) {
+      file_queue_.push(PPB_FileRef_Proxy::DeserializeFileRef(
+          chosen_files[i]));
+    }
+  }
+
+  // Notify the plugin of the new data.
+  TrackedCallback::ClearAndRun(&callback_, PP_OK);
+  // DANGER: May delete |this|!
+}
+
+int32_t FileChooserResource::ShowInternal(
+    PP_Bool save_as,
+    const PP_Var& suggested_file_name,
+    scoped_refptr<TrackedCallback> callback) {
+  if (TrackedCallback::IsPending(callback_))
+    return PP_ERROR_INPROGRESS;
+
+  if (!sent_create_to_renderer())
+    SendCreateToRenderer(PpapiHostMsg_FileChooser_Create());
+
+  callback_ = callback;
+  StringVar* sugg_str = StringVar::FromPPVar(suggested_file_name);
+
+  CallRenderer(PpapiHostMsg_FileChooser_Show(
+      PP_ToBool(save_as),
+      mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE,
+      sugg_str ? sugg_str->value() : std::string(),
+      accept_types_));
+  return PP_OK_COMPLETIONPENDING;
+}
+
+}  // namespace proxy
+}  // namespace ppapi