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.
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"
16 // Allow 'this' in initializer list
17 #pragma warning(disable : 4355)
22 static const char kGetCommand
[] = "Get";
23 static const char kSetCommand
[] = "Set";
24 static const char kDeleteCommand
[] = "Delete";
25 static const char kHasKeyCommand
[] = "HasKey";
26 static const char kGetKeysCommand
[] = "GetKeys";
28 pp::Var
MakeResult(const char* cmd
, const pp::Var
& value
,
29 const pp::Var
& newDictionary
) {
30 pp::VarDictionary dict
;
32 dict
.Set("result", value
);
33 dict
.Set("dict", newDictionary
);
39 class VarDictionaryInstance
: public pp::Instance
{
41 explicit VarDictionaryInstance(PP_Instance instance
)
42 : pp::Instance(instance
) {}
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);
54 dictionary_
.Set("array", array
);
55 PostResult("", pp::Var());
60 virtual void HandleMessage(const pp::Var
& var_message
) {
61 if (!var_message
.is_dictionary()) {
62 fprintf(stderr
, "Unexpected message.\n");
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");
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
);
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");
94 std::string key
= var_key
.AsString();
95 PostResult(kGetCommand
, dictionary_
.Get(key
));
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");
105 pp::Var var_value
= dict_message
.Get("value");
106 std::string key
= var_key
.AsString();
107 PostResult(kSetCommand
, dictionary_
.Set(key
, var_value
));
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");
117 std::string key
= var_key
.AsString();
118 dictionary_
.Delete(key
);
119 PostResult(kDeleteCommand
, pp::Var());
122 void HandleGetKeys(const pp::VarDictionary
& dict_message
) {
123 PostResult(kGetKeysCommand
, dictionary_
.GetKeys());
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");
133 std::string key
= var_key
.AsString();
134 PostResult(kHasKeyCommand
, dictionary_
.HasKey(key
));
137 void PostResult(const char* cmd
, const pp::Var
& result
) {
138 PostMessage(MakeResult(cmd
, result
, dictionary_
));
141 pp::VarDictionary dictionary_
;
144 class VarDictionaryModule
: public pp::Module
{
146 VarDictionaryModule() : pp::Module() {}
147 virtual ~VarDictionaryModule() {}
149 virtual pp::Instance
* CreateInstance(PP_Instance instance
) {
150 return new VarDictionaryInstance(instance
);
155 Module
* CreateModule() { return new VarDictionaryModule(); }