Send a crash report when a hung process is detected.
[chromium-blink-merge.git] / native_client_sdk / src / examples / api / var_dictionary / var_dictionary.cc
blob052c9103c6710e707ed565ebc72cdaf104377965
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.
5 #include <math.h>
6 #include <stdio.h>
7 #include <string>
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"
14 #ifdef WIN32
15 #undef PostMessage
16 // Allow 'this' in initializer list
17 #pragma warning(disable : 4355)
18 #endif
20 namespace {
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;
31 dict.Set("cmd", cmd);
32 dict.Set("result", value);
33 dict.Set("dict", newDictionary);
34 return dict;
37 } // namespace
39 class VarDictionaryInstance : public pp::Instance {
40 public:
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);
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;
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;
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;
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");
91 return;
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");
102 return;
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");
114 return;
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");
130 return;
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 {
145 public:
146 VarDictionaryModule() : pp::Module() {}
147 virtual ~VarDictionaryModule() {}
149 virtual pp::Instance* CreateInstance(PP_Instance instance) {
150 return new VarDictionaryInstance(instance);
154 namespace pp {
155 Module* CreateModule() { return new VarDictionaryModule(); }
156 } // namespace pp