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

Unified Diff: ppapi/proxy/flash_clipboard_resource.cc

Issue 11225021: Move flash clipboard to the new proxy and add custom format support (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . 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: ppapi/proxy/flash_clipboard_resource.cc
diff --git a/ppapi/proxy/flash_clipboard_resource.cc b/ppapi/proxy/flash_clipboard_resource.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ba371f375267cd771f3c99c13729b81a71f45e59
--- /dev/null
+++ b/ppapi/proxy/flash_clipboard_resource.cc
@@ -0,0 +1,151 @@
+// 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/flash_clipboard_resource.h"
+
+#include "ipc/ipc_message.h"
+#include "ppapi/c/pp_errors.h"
+#include "ppapi/proxy/dispatch_reply_message.h"
yzshen1 2012/10/29 16:55:46 Do you need this?
raymes 2012/10/29 18:44:58 Done.
+#include "ppapi/proxy/ppapi_messages.h"
+#include "ppapi/shared_impl/array_writer.h"
yzshen1 2012/10/29 16:55:46 I don't think you need it.
raymes 2012/10/29 18:44:58 Done.
+#include "ppapi/shared_impl/var.h"
+#include "ppapi/shared_impl/var_tracker.h"
+#include "ppapi/thunk/enter.h"
yzshen1 2012/10/29 16:55:46 Do you need this?
raymes 2012/10/29 18:44:58 Done.
+
+namespace ppapi {
+namespace proxy {
+
+namespace {
+// Convert a PP_Var to/from a string which is transmitted to the pepper host.
+// These functions assume the format is valid.
+bool PP_VarToClipboardString(int32_t format,
yzshen1 2012/10/29 16:55:46 nit: better to follow the style of "StringVar::Fro
raymes 2012/10/29 18:44:58 Done.
+ const PP_Var& var,
+ std::string* string_out) {
+ if (format == PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT ||
+ format == PP_FLASH_CLIPBOARD_FORMAT_HTML) {
+ StringVar* string_var = StringVar::FromPPVar(var);
+ if (!string_var)
+ return false;
+ *string_out = string_var->value();
+ return true;
+ } else {
+ // All other formats are expected to be array buffers.
+ ArrayBufferVar* array_buffer_var = ArrayBufferVar::FromPPVar(var);
+ if (!array_buffer_var)
+ return false;
+ *string_out = std::string(static_cast<const char*>(array_buffer_var->Map()),
+ array_buffer_var->ByteLength());
+ return true;
+ }
+}
+
+PP_Var ClipboardStringToPP_Var(int32_t format,
+ const std::string& string) {
+ if (format == PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT ||
+ format == PP_FLASH_CLIPBOARD_FORMAT_HTML) {
+ return StringVar::StringToPPVar(string);
+ } else {
+ // All other formats are expected to be array buffers.
+ return PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar(
+ string.size(), string.data());
+ }
+}
+} // namespace
+
+FlashClipboardResource::FlashClipboardResource(
+ Connection connection, PP_Instance instance)
+ : PluginResource(connection, instance) {
+ SendCreate(RENDERER, PpapiHostMsg_FlashClipboard_Create());
+}
+
+FlashClipboardResource::~FlashClipboardResource() {
+}
+
+thunk::PPB_Flash_Clipboard_API*
+FlashClipboardResource::AsPPB_Flash_Clipboard_API() {
+ return this;
+}
+
+uint32_t FlashClipboardResource::RegisterCustomFormat(
+ PP_Instance instance,
+ const char* format_name) {
+ // Check to see if the format has already been registered.
+ uint32_t format = clipboard_formats_.GetFormatID(format_name);
+ if (format != PP_FLASH_CLIPBOARD_FORMAT_INVALID)
+ return format;
+ int32_t result =
+ SyncCall<PpapiPluginMsg_FlashClipboard_RegisterCustomFormatReply>(
+ RENDERER,
+ PpapiHostMsg_FlashClipboard_RegisterCustomFormat(format_name),
+ &format);
+ if (result != PP_OK || format == PP_FLASH_CLIPBOARD_FORMAT_INVALID)
+ return PP_FLASH_CLIPBOARD_FORMAT_INVALID;
+ clipboard_formats_.SetRegisteredFormat(format_name, format);
+ return format;
+}
+
+PP_Bool FlashClipboardResource::IsFormatAvailable(
+ PP_Instance instance,
+ PP_Flash_Clipboard_Type clipboard_type,
+ uint32_t format) {
+ if (FlashClipboardFormatRegistry::IsValidClipboardType(clipboard_type) &&
+ (FlashClipboardFormatRegistry::IsValidPredefinedFormat(format) ||
+ clipboard_formats_.IsFormatRegistered(format))) {
+ int32_t result = SyncCall<IPC::Message>(RENDERER,
+ PpapiHostMsg_FlashClipboard_IsFormatAvailable(clipboard_type, format));
+ return result == PP_OK ? PP_TRUE : PP_FALSE;
+ }
+ return PP_FALSE;
+}
+
+PP_Var FlashClipboardResource::ReadData(
+ PP_Instance instance,
+ PP_Flash_Clipboard_Type clipboard_type,
+ uint32_t format) {
+ std::string value;
+ int32_t result =
+ SyncCall<PpapiPluginMsg_FlashClipboard_ReadDataReply>(
+ RENDERER,
+ PpapiHostMsg_FlashClipboard_ReadData(clipboard_type, format),
+ &value);
+ if (result != PP_OK)
+ return PP_MakeUndefined();
+
+ return ClipboardStringToPP_Var(format, value);
+}
+
+int32_t FlashClipboardResource::WriteData(
+ PP_Instance instance,
+ PP_Flash_Clipboard_Type clipboard_type,
+ uint32_t data_item_count,
+ const uint32_t formats[],
+ const PP_Var data_items[]) {
+ if (!FlashClipboardFormatRegistry::IsValidClipboardType(clipboard_type))
+ return PP_ERROR_BADARGUMENT;
+ std::vector<uint32_t> formats_vector;
+ std::vector<std::string> data_items_vector;
+ for (size_t i = 0; i < data_item_count; ++i) {
+ if (!clipboard_formats_.IsFormatRegistered(formats[i]) &&
+ !FlashClipboardFormatRegistry::IsValidPredefinedFormat(formats[i])) {
+ return PP_ERROR_BADARGUMENT;
+ }
+ formats_vector.push_back(formats[i]);
+ std::string string;
+ if (!PP_VarToClipboardString(formats[i], data_items[i], &string))
+ return PP_ERROR_BADARGUMENT;
+ data_items_vector.push_back(string);
+ }
+
+ Post(RENDERER,
+ PpapiHostMsg_FlashClipboard_WriteData(
+ static_cast<uint32_t>(clipboard_type),
+ formats_vector,
+ data_items_vector));
+
+ // Assume success, since it allows us to avoid a sync IPC.
+ return PP_OK;
+}
+
+} // namespace proxy
+} // namespace ppapi

Powered by Google App Engine
This is Rietveld 408576698