1// Copyright (c) 2013 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 <math.h> 6#include <stdio.h> 7#include <string> 8 9#include "ppapi/cpp/instance.h" 10#include "ppapi/cpp/module.h" 11#include "ppapi/cpp/var.h" 12#include "ppapi/cpp/var_dictionary.h" 13 14#ifdef WIN32 15#undef PostMessage 16// Allow 'this' in initializer list 17#pragma warning(disable : 4355) 18#endif 19 20namespace { 21 22static const char kGetCommand[] = "Get"; 23static const char kSetCommand[] = "Set"; 24static const char kDeleteCommand[] = "Delete"; 25static const char kHasKeyCommand[] = "HasKey"; 26static const char kGetKeysCommand[] = "GetKeys"; 27 28pp::Var MakeResult(const char* cmd, const pp::Var& value, 29 const pp::Var& newDictionary) { 30 pp::VarDictionary dict; 31 dict.Set("cmd", cmd); 32 dict.Set("result", value); 33 dict.Set("dict", newDictionary); 34 return dict; 35} 36 37} // namespace 38 39class VarDictionaryInstance : public pp::Instance { 40 public: 41 explicit VarDictionaryInstance(PP_Instance instance) 42 : pp::Instance(instance) {} 43 44 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { 45 // Create the initial dictionary with some basic values. 46 dictionary_.Set("key1", "value1"); 47 dictionary_.Set("foo", true); 48 49 pp::VarArray array; 50 array.Set(0, 1); 51 array.Set(1, 2); 52 array.Set(2, 3.1415); 53 array.Set(3, "four"); 54 dictionary_.Set("array", array); 55 PostResult("", pp::Var()); 56 return true; 57 } 58 59 private: 60 virtual void HandleMessage(const pp::Var& var_message) { 61 if (!var_message.is_dictionary()) { 62 fprintf(stderr, "Unexpected message.\n"); 63 return; 64 } 65 66 pp::VarDictionary dict_message(var_message); 67 pp::Var var_command = dict_message.Get("cmd"); 68 if (!var_command.is_string()) { 69 fprintf(stderr, "Expect dict item \"command\" to be a string.\n"); 70 return; 71 } 72 73 std::string command = var_command.AsString(); 74 if (command == kGetCommand) { 75 HandleGet(dict_message); 76 } else if (command == kSetCommand) { 77 HandleSet(dict_message); 78 } else if (command == kDeleteCommand) { 79 HandleDelete(dict_message); 80 } else if (command == kGetKeysCommand) { 81 HandleGetKeys(dict_message); 82 } else if (command == kHasKeyCommand) { 83 HandleHasKey(dict_message); 84 } 85 } 86 87 void HandleGet(const pp::VarDictionary& dict_message) { 88 pp::Var var_key = dict_message.Get("key"); 89 if (!var_key.is_string()) { 90 fprintf(stderr, "HandleGet: Expect dict item \"key\" to be a string.\n"); 91 return; 92 } 93 94 std::string key = var_key.AsString(); 95 PostResult(kGetCommand, dictionary_.Get(key)); 96 } 97 98 void HandleSet(const pp::VarDictionary& dict_message) { 99 pp::Var var_key = dict_message.Get("key"); 100 if (!var_key.is_string()) { 101 fprintf(stderr, "HandleGet: Expect dict item \"key\" to be a string.\n"); 102 return; 103 } 104 105 pp::Var var_value = dict_message.Get("value"); 106 std::string key = var_key.AsString(); 107 PostResult(kSetCommand, dictionary_.Set(key, var_value)); 108 } 109 110 void HandleDelete(const pp::VarDictionary& dict_message) { 111 pp::Var var_key = dict_message.Get("key"); 112 if (!var_key.is_string()) { 113 fprintf(stderr, "HandleGet: Expect dict item \"key\" to be a string.\n"); 114 return; 115 } 116 117 std::string key = var_key.AsString(); 118 dictionary_.Delete(key); 119 PostResult(kDeleteCommand, pp::Var()); 120 } 121 122 void HandleGetKeys(const pp::VarDictionary& dict_message) { 123 PostResult(kGetKeysCommand, dictionary_.GetKeys()); 124 } 125 126 void HandleHasKey(const pp::VarDictionary& dict_message) { 127 pp::Var var_key = dict_message.Get("key"); 128 if (!var_key.is_string()) { 129 fprintf(stderr, "HandleGet: Expect dict item \"key\" to be a string.\n"); 130 return; 131 } 132 133 std::string key = var_key.AsString(); 134 PostResult(kHasKeyCommand, dictionary_.HasKey(key)); 135 } 136 137 void PostResult(const char* cmd, const pp::Var& result) { 138 PostMessage(MakeResult(cmd, result, dictionary_)); 139 } 140 141 pp::VarDictionary dictionary_; 142}; 143 144class VarDictionaryModule : public pp::Module { 145 public: 146 VarDictionaryModule() : pp::Module() {} 147 virtual ~VarDictionaryModule() {} 148 149 virtual pp::Instance* CreateInstance(PP_Instance instance) { 150 return new VarDictionaryInstance(instance); 151 } 152}; 153 154namespace pp { 155Module* CreateModule() { return new VarDictionaryModule(); } 156} // namespace pp 157