Chromium Code Reviews| Index: native_client_sdk/src/examples/demo/googledrivefs_demo/googledrivefs_demo.cc |
| diff --git a/native_client_sdk/src/examples/demo/nacl_io_demo/nacl_io_demo.c b/native_client_sdk/src/examples/demo/googledrivefs_demo/googledrivefs_demo.cc |
| similarity index 71% |
| copy from native_client_sdk/src/examples/demo/nacl_io_demo/nacl_io_demo.c |
| copy to native_client_sdk/src/examples/demo/googledrivefs_demo/googledrivefs_demo.cc |
| index 1ce3b817f49e0504ed505438de72d5c6de713b53..00642af2025d29576cf6523817aa5ddeb22237bf 100644 |
| --- a/native_client_sdk/src/examples/demo/nacl_io_demo/nacl_io_demo.c |
| +++ b/native_client_sdk/src/examples/demo/googledrivefs_demo/googledrivefs_demo.cc |
| @@ -1,8 +1,8 @@ |
| -/* Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +/* Copyright (c) 2016 The Chromium Authors. All rights reserved. |
|
binji
2016/07/18 23:24:02
This ends up duplicating quite a bit of code from
chanpatorikku
2016/08/07 02:41:01
Done.
Thanks.
Added the functionality to the nacl
|
| * Use of this source code is governed by a BSD-style license that can be |
| * found in the LICENSE file. */ |
| -#include "nacl_io_demo.h" |
| +#include "googledrivefs_demo.h" |
| #include <assert.h> |
| #include <errno.h> |
| @@ -30,9 +30,10 @@ |
| #include "ppapi/c/ppp.h" |
| #include "ppapi/c/ppp_instance.h" |
| #include "ppapi/c/ppp_messaging.h" |
| -#include "nacl_io/ioctl.h" |
| #include "nacl_io/nacl_io.h" |
| +#include "ppapi/cpp/module.h" |
| + |
| #include "handlers.h" |
| #include "queue.h" |
| @@ -40,16 +41,6 @@ |
| #define va_copy(d, s) ((d) = (s)) |
| #endif |
| -/** |
| - * The location of MAX is inconsitantly between LIBCs, so instead |
| - * we define it here for consistency. |
| - */ |
| -static int larger_int_of(int a, int b) { |
| - if (a > b) |
| - return a; |
| - return b; |
| -} |
| - |
| typedef struct { |
| const char* name; |
| HandleFunc function; |
| @@ -77,18 +68,11 @@ static FuncNameMapping g_function_map[] = { |
| {"rmdir", HandleRmdir}, |
| {"chdir", HandleChdir}, |
| {"getcwd", HandleGetcwd}, |
| - {"getaddrinfo", HandleGetaddrinfo}, |
| - {"gethostbyname", HandleGethostbyname}, |
| - {"connect", HandleConnect}, |
| - {"send", HandleSend}, |
| - {"recv", HandleRecv}, |
| - {"close", HandleClose}, |
| {NULL, NULL}, |
| }; |
| /** A handle to the thread the handles messages. */ |
| static pthread_t g_handle_message_thread; |
| -static pthread_t g_echo_thread; |
| /** |
| * Create a new PP_Var from a C string. |
| @@ -292,64 +276,6 @@ static void HandleMessage(struct PP_Var message) { |
| g_ppb_var->Release(result_var); |
| } |
| - |
| -/** |
| - * Helper function used by EchoThread which reads from a file descriptor |
| - * and writes all the data that it reads back to the same descriptor. |
| - */ |
| -static void EchoInput(int fd) { |
| - char buffer[512]; |
| - while (1) { |
| - int rtn = read(fd, buffer, 512); |
| - if (rtn > 0) { |
| - int wrote = write(fd, buffer, rtn); |
| - if (wrote < rtn) |
| - PostMessage("only wrote %d/%d bytes\n", wrote, rtn); |
| - } else { |
| - if (rtn < 0 && errno != EAGAIN) |
| - PostMessage("read failed: %d (%s)\n", errno, strerror(errno)); |
| - break; |
| - } |
| - } |
| -} |
| - |
| -/** |
| - * Worker thread that listens for input on JS pipe nodes and echos all input |
| - * back to the same pipe. |
| - */ |
| -static void* EchoThread(void* user_data) { |
| - int fd1 = open("/dev/jspipe1", O_RDWR | O_NONBLOCK); |
| - int fd2 = open("/dev/jspipe2", O_RDWR | O_NONBLOCK); |
| - int fd3 = open("/dev/jspipe3", O_RDWR | O_NONBLOCK); |
| - int nfds = larger_int_of(fd1, fd2); |
| - nfds = larger_int_of(nfds, fd3); |
| - while (1) { |
| - fd_set readfds; |
| - FD_ZERO(&readfds); |
| - FD_SET(fd1, &readfds); |
| - FD_SET(fd2, &readfds); |
| - FD_SET(fd3, &readfds); |
| - int rtn = select(nfds + 1, &readfds, NULL, NULL, NULL); |
| - if (rtn < 0 && errno != EAGAIN) { |
| - PostMessage("select failed: %s\n", strerror(errno)); |
| - break; |
| - } |
| - if (rtn > 0) { |
| - if (FD_ISSET(fd1, &readfds)) |
| - EchoInput(fd1); |
| - if (FD_ISSET(fd2, &readfds)) |
| - EchoInput(fd2); |
| - if (FD_ISSET(fd3, &readfds)) |
| - EchoInput(fd3); |
| - } |
| - |
| - } |
| - close(fd1); |
| - close(fd2); |
| - close(fd3); |
| - return 0; |
| -} |
| - |
| /** |
| * A worker thread that handles messages from JavaScript. |
| * @param[in] user_data Unused. |
| @@ -367,32 +293,38 @@ static PP_Bool Instance_DidCreate(PP_Instance instance, |
| uint32_t argc, |
| const char* argn[], |
| const char* argv[]) { |
| - g_instance = instance; |
| - nacl_io_init_ppapi(instance, g_get_browser_interface); |
| - |
| - // By default, nacl_io mounts / to pass through to the original NaCl |
| - // filesystem (which doesn't do much). Let's remount it to a memfs |
| - // filesystem. |
| - umount("/"); |
| - mount("", "/", "memfs", 0, ""); |
| - |
| - mount("", /* source */ |
| - "/persistent", /* target */ |
| - "html5fs", /* filesystemtype */ |
| - 0, /* mountflags */ |
| - "type=PERSISTENT,expected_size=1048576"); /* data */ |
| - |
| - mount("", /* source. Use relative URL */ |
| - "/http", /* target */ |
| - "httpfs", /* filesystemtype */ |
| - 0, /* mountflags */ |
| - ""); /* data */ |
| - |
| - pthread_create(&g_handle_message_thread, NULL, &HandleMessageThread, NULL); |
| - pthread_create(&g_echo_thread, NULL, &EchoThread, NULL); |
| - InitializeMessageQueue(); |
| - |
| - return PP_TRUE; |
| + for (int i = 0; i < argc; ++i) { |
| + if (strcmp(argn[i], "token") == 0) { |
| + char buffer[1024]; |
| + int char_written = |
| + sprintf(buffer, "instance=%i,token=%s", instance, argv[i]); |
| + if (char_written < 0) { |
| + return PP_FALSE; |
| + } |
| + |
| + g_instance = instance; |
| + nacl_io_init_ppapi(instance, g_get_browser_interface); |
| + |
| + // By default, nacl_io mounts / to pass through to the original NaCl |
| + // filesystem (which doesn't do much). Let's remount it to a googledrivefs |
| + // filesystem. |
| + umount("/"); |
| + |
| + mount("", |
| + "/", |
| + "googledrivefs", |
| + 0, |
| + buffer); |
| + |
| + pthread_create(&g_handle_message_thread, |
| + NULL, &HandleMessageThread, NULL); |
| + InitializeMessageQueue(); |
| + |
| + return PP_TRUE; |
| + } |
| + } |
| + |
| + return PP_FALSE; |
| } |
| static void Instance_DidDestroy(PP_Instance instance) { |
| @@ -413,43 +345,28 @@ static PP_Bool Instance_HandleDocumentLoad(PP_Instance instance, |
| static void Messaging_HandleMessage(PP_Instance instance, |
| struct PP_Var message) { |
| - /* Special case for jspipe input handling */ |
| - if (message.type != PP_VARTYPE_DICTIONARY) { |
| - PostMessage("Got unexpected message type: %d\n", message.type); |
| - return; |
| - } |
| - |
| - struct PP_Var pipe_var = CStrToVar("pipe"); |
| - struct PP_Var pipe_name = g_ppb_var_dictionary->Get(message, pipe_var); |
| - g_ppb_var->Release(pipe_var); |
| - |
| - /* Special case for jspipe input handling */ |
| - if (pipe_name.type == PP_VARTYPE_STRING) { |
| - char file_name[PATH_MAX]; |
| - snprintf(file_name, PATH_MAX, "/dev/%s", VarToCStr(pipe_name)); |
| - int fd = open(file_name, O_RDONLY); |
| - g_ppb_var->Release(pipe_name); |
| - if (fd < 0) { |
| - PostMessage("Warning: opening %s failed.", file_name); |
| - goto done; |
| - } |
| - if (ioctl(fd, NACL_IOC_HANDLEMESSAGE, &message) != 0) { |
| - PostMessage("Error: ioctl on %s failed: %s", file_name, strerror(errno)); |
| - } |
| - close(fd); |
| - goto done; |
| - } |
| - |
| g_ppb_var->AddRef(message); |
| if (!EnqueueMessage(message)) { |
| g_ppb_var->Release(message); |
| PostMessage("Warning: dropped message because the queue was full."); |
| } |
| +} |
| + |
| +class GoogleDriveFsDemoModule : public pp::Module { |
| +public: |
| + pp::Instance* CreateInstance(PP_Instance instance) { return NULL; } |
| +}; |
| + |
| +GoogleDriveFsDemoModule* g_googledrivefs_demo_module = NULL; |
| + |
| +namespace pp { |
| -done: |
| - g_ppb_var->Release(pipe_name); |
| +Module* Module::Get() { |
| + return g_googledrivefs_demo_module; |
| } |
| +} // namespace pp |
| + |
| #define GET_INTERFACE(var, type, name) \ |
| var = (type*)(get_browser(name)); \ |
| if (!var) { \ |
| @@ -459,12 +376,21 @@ done: |
| PP_EXPORT int32_t PPP_InitializeModule(PP_Module a_module_id, |
| PPB_GetInterface get_browser) { |
| + g_googledrivefs_demo_module = new GoogleDriveFsDemoModule(); |
| + |
| g_get_browser_interface = get_browser; |
| + |
| GET_INTERFACE(g_ppb_messaging, PPB_Messaging, PPB_MESSAGING_INTERFACE); |
| GET_INTERFACE(g_ppb_var, PPB_Var, PPB_VAR_INTERFACE); |
| GET_INTERFACE(g_ppb_var_array, PPB_VarArray, PPB_VAR_ARRAY_INTERFACE); |
| GET_INTERFACE( |
| g_ppb_var_dictionary, PPB_VarDictionary, PPB_VAR_DICTIONARY_INTERFACE); |
| + |
| + if (!g_googledrivefs_demo_module->InternalInit(a_module_id, get_browser)) { |
| + delete g_googledrivefs_demo_module; |
| + return PP_ERROR_FAILED; |
| + } |
| + |
| return PP_OK; |
| } |