${BASE_SRCDIR}/bundle/module_system.cc
${BASE_SRCDIR}/bundle/runtime_ipc_client.cc
${BASE_SRCDIR}/bundle/xwalk_v8tools_module.cc
+ ${BASE_SRCDIR}/bundle/widget_module.cc
)
# Compiler Flags
#include "bundle/extension_module.h"
#include "bundle/module_system.h"
#include "bundle/xwalk_v8tools_module.h"
+#include "bundle/widget_module.h"
+
namespace wrt {
module_system->RegisterNativeModule(
"v8tools", std::unique_ptr<NativeModule>(new XWalkV8ToolsModule));
+ module_system->RegisterNativeModule(
+ "WidgetModule", std::unique_ptr<NativeModule>(new WidgetModule));
CreateExtensionModules(extensions_client_.get(), module_system);
module_system->Initialize();
#include "common/locale_manager.h"
#include "bundle/runtime_ipc_client.h"
#include "bundle/extension_renderer_controller.h"
+#include "bundle/widget_module.h"
namespace wrt {
class BundleGlobalData {
locale_manager_.get()));
resource_manager_->set_base_resource_path(
app_data_->application_path());
+
+ auto widgetdb = WidgetPreferenceDB::GetInstance();
+ widgetdb->Initialize(app_data_.get(),
+ locale_manager_.get());
}
ResourceManager* resource_manager() {
--- /dev/null
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "bundle/widget_module.h"
+
+#include <v8/v8.h>
+#include <vector>
+#include <algorithm>
+
+#include "common/logger.h"
+#include "common/app_db.h"
+#include "common/application_data.h"
+#include "common/locale_manager.h"
+
+namespace wrt {
+
+namespace {
+const char* kOnchangedEventHandler = "__onChanged_WRT__";
+const char* kKeyKey = "key";
+const char* kGetItemKey = "getItem";
+const char* kSetItemKey = "setItem";
+const char* kRemoveItemKey = "removeItem";
+const char* kLengthKey = "length";
+const char* kClearKey = "clear";
+
+std::vector<const char*> kExcludeList = {
+ kOnchangedEventHandler,
+ kKeyKey,
+ kGetItemKey,
+ kSetItemKey,
+ kRemoveItemKey,
+ kLengthKey,
+ kClearKey};
+
+void DispatchEvent(const v8::Local<v8::Object>& This,
+ v8::Local<v8::Value> key,
+ v8::Local<v8::Value> oldvalue,
+ v8::Local<v8::Value> newvalue) {
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
+
+ v8::Handle<v8::Value> function =
+ This->Get(v8::String::NewFromUtf8(isolate, kOnchangedEventHandler));
+
+ if (function.IsEmpty() || !function->IsFunction()) {
+ LOGGER(DEBUG) << "onChanged function not set";
+ return;
+ }
+
+ v8::Handle<v8::Context> context = v8::Context::New(isolate);
+
+ const int argc = 3;
+ v8::Handle<v8::Value> argv[argc] = {
+ key,
+ oldvalue,
+ newvalue
+ };
+
+ v8::TryCatch try_catch;
+ v8::Handle<v8::Function>::Cast(function)->Call(
+ context->Global(), argc, argv);
+ if (try_catch.HasCaught())
+ LOGGER(DEBUG) << "Exception when running onChanged callback";
+}
+
+v8::Handle<v8::Object> MakeException(int code,
+ std::string name,
+ std::string message) {
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
+ v8::EscapableHandleScope handle_scope(isolate);
+ v8::Local<v8::Object> error = v8::Object::New(isolate);
+
+ error->Set(
+ v8::String::NewFromUtf8(isolate, "code"),
+ v8::Number::New(isolate, code));
+ error->Set(
+ v8::String::NewFromUtf8(isolate, "name"),
+ v8::String::NewFromUtf8(isolate, name.c_str()));
+ error->Set(
+ v8::String::NewFromUtf8(isolate, "message"),
+ v8::String::NewFromUtf8(isolate, message.c_str()));
+
+ return handle_scope.Escape(error);
+}
+
+void KeyFunction(const v8::FunctionCallbackInfo<v8::Value>& info) {
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
+ int idx = info[0]->ToInt32()->Value();
+ auto widget = WidgetPreferenceDB::GetInstance();
+ std::string keyname;
+ if (widget->Key(idx, &keyname)) {
+ info.GetReturnValue().Set(
+ v8::String::NewFromUtf8(isolate, keyname.c_str()));
+ } else {
+ info.GetReturnValue().SetNull();
+ }
+}
+
+void GetItemFunction(const v8::FunctionCallbackInfo<v8::Value>& info) {
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
+ std::string key = *v8::String::Utf8Value(info[0]->ToString());
+ auto widget = WidgetPreferenceDB::GetInstance();
+ std::string valuestr;
+ if (widget->GetItem(key, &valuestr)) {
+ info.GetReturnValue().Set(
+ v8::String::NewFromUtf8(isolate, valuestr.c_str()));
+ } else {
+ info.GetReturnValue().SetNull();
+ }
+}
+
+void SetItemFunction(const v8::FunctionCallbackInfo<v8::Value>& info) {
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
+ std::string key = *v8::String::Utf8Value(info[0]->ToString());
+ std::string value = *v8::String::Utf8Value(info[1]->ToString());
+ auto widget = WidgetPreferenceDB::GetInstance();
+
+ v8::Local<v8::Value> oldvalue = v8::Null(isolate);
+ std::string oldvaluestr;
+ if (widget->GetItem(key, &oldvaluestr)) {
+ oldvalue = v8::String::NewFromUtf8(isolate, oldvaluestr.c_str());
+ }
+
+ if (widget->SetItem(key, value)) {
+ DispatchEvent(info.This(),
+ info[0],
+ oldvalue,
+ info[1]);
+ } else {
+ info.GetReturnValue().Set(isolate->ThrowException(MakeException(
+ 7, "NoModificationAllowedError", "Read only data")));
+ }
+}
+
+void RemoveItemFunction(const v8::FunctionCallbackInfo<v8::Value>& info) {
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
+ std::string key = *v8::String::Utf8Value(info[0]->ToString());
+ auto widget = WidgetPreferenceDB::GetInstance();
+
+ if (!widget->HasItem(key)) {
+ return;
+ }
+
+ v8::Local<v8::Value> oldvalue = v8::Null(isolate);
+ std::string oldvaluestr;
+ if (widget->GetItem(key, &oldvaluestr)) {
+ oldvalue = v8::String::NewFromUtf8(isolate, oldvaluestr.c_str());
+ }
+
+ if (widget->RemoveItem(key)) {
+ DispatchEvent(info.This(),
+ info[0],
+ oldvalue,
+ v8::Null(isolate));
+ } else {
+ info.GetReturnValue().Set(isolate->ThrowException(MakeException(
+ 7, "NoModificationAllowedError", "Read only data")));
+ }
+}
+
+void ClearFunction(const v8::FunctionCallbackInfo<v8::Value>& info) {
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
+ auto widget = WidgetPreferenceDB::GetInstance();
+ widget->Clear();
+ DispatchEvent(info.This(),
+ v8::Null(isolate),
+ v8::Null(isolate),
+ v8::Null(isolate));
+}
+
+} // namespace
+
+WidgetModule::WidgetModule() {
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
+ v8::HandleScope handle_scope(isolate);
+ v8::Handle<v8::ObjectTemplate>
+ preference_object_template = v8::ObjectTemplate::New();
+
+ auto getter = [](
+ v8::Local<v8::String> property,
+ const v8::PropertyCallbackInfo<v8::Value>& info) {
+ v8::Isolate* isolate = info.GetIsolate();
+ auto widget = WidgetPreferenceDB::GetInstance();
+ std::string key = *v8::String::Utf8Value(property);
+
+ if (key == kLengthKey) {
+ info.GetReturnValue().Set(widget->Length());
+ return;
+ }
+
+ if (std::find(kExcludeList.begin(), kExcludeList.end(), key)
+ != kExcludeList.end()) {
+ return;
+ }
+
+ if (!widget->HasItem(key)) {
+ return;
+ }
+
+ std::string value;
+ if (widget->GetItem(key, &value)) {
+ info.GetReturnValue().Set(
+ v8::String::NewFromUtf8(isolate, value.c_str()));
+ } else {
+ info.GetReturnValue().SetNull();
+ }
+ };
+
+ auto setter = [](
+ v8::Local<v8::String> property,
+ v8::Local<v8::Value> value,
+ const v8::PropertyCallbackInfo<v8::Value>& info) {
+ v8::Isolate* isolate = info.GetIsolate();
+ auto widget = WidgetPreferenceDB::GetInstance();
+ std::string key = *v8::String::Utf8Value(property);
+ std::string nvalue = *v8::String::Utf8Value(value->ToString());
+
+ if (std::find(kExcludeList.begin(), kExcludeList.end(), key)
+ != kExcludeList.end()) {
+ return;
+ }
+
+ v8::Local<v8::Value> oldvalue = v8::Null(isolate);
+ std::string oldvaluestr;
+ if (widget->GetItem(key, &oldvaluestr)) {
+ oldvalue = v8::String::NewFromUtf8(isolate, oldvaluestr.c_str());
+ }
+ if (widget->SetItem(key, nvalue)) {
+ info.GetReturnValue().Set(value);
+ DispatchEvent(info.This(),
+ property,
+ oldvalue,
+ value);
+ }
+ };
+
+ auto deleter = [](
+ v8::Local<v8::String> property,
+ const v8::PropertyCallbackInfo<v8::Boolean>& info) {
+ v8::Isolate* isolate = info.GetIsolate();
+ auto widget = WidgetPreferenceDB::GetInstance();
+ std::string key = *v8::String::Utf8Value(property);
+ if (!widget->HasItem(key)) {
+ info.GetReturnValue().Set(false);
+ return;
+ }
+
+ v8::Local<v8::Value> oldvalue = v8::Null(isolate);
+ std::string oldvaluestr;
+ if (widget->GetItem(key, &oldvaluestr)) {
+ oldvalue = v8::String::NewFromUtf8(isolate, oldvaluestr.c_str());
+ }
+
+ if (widget->RemoveItem(key)) {
+ info.GetReturnValue().Set(true);
+ DispatchEvent(info.This(),
+ property,
+ oldvalue,
+ v8::Null(isolate));
+ } else {
+ info.GetReturnValue().Set(false);
+ }
+ };
+
+ preference_object_template->SetNamedPropertyHandler(
+ getter,
+ setter,
+ NULL,
+ deleter,
+ NULL);
+
+ preference_object_template->Set(
+ v8::String::NewFromUtf8(isolate, kKeyKey),
+ v8::FunctionTemplate::New(isolate, KeyFunction));
+
+ preference_object_template->Set(
+ v8::String::NewFromUtf8(isolate, kGetItemKey),
+ v8::FunctionTemplate::New(isolate, GetItemFunction));
+
+ preference_object_template->Set(
+ v8::String::NewFromUtf8(isolate, kSetItemKey),
+ v8::FunctionTemplate::New(isolate, SetItemFunction));
+
+ preference_object_template->Set(
+ v8::String::NewFromUtf8(isolate, kRemoveItemKey),
+ v8::FunctionTemplate::New(isolate, RemoveItemFunction));
+
+ preference_object_template->Set(
+ v8::String::NewFromUtf8(isolate, kClearKey),
+ v8::FunctionTemplate::New(isolate, ClearFunction));
+
+
+ preference_object_template_.Reset(isolate, preference_object_template);
+}
+
+WidgetModule::~WidgetModule() {
+}
+
+v8::Handle<v8::Object> WidgetModule::NewInstance() {
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
+ v8::EscapableHandleScope handle_scope(isolate);
+
+ v8::Local<v8::Object> widget = v8::Object::New(isolate);
+ v8::Handle<v8::ObjectTemplate> object_template =
+ v8::Local<v8::ObjectTemplate>::New(isolate, preference_object_template_);
+
+ auto widgetdb = WidgetPreferenceDB::GetInstance();
+
+ widget->Set(
+ v8::String::NewFromUtf8(isolate, "preference"),
+ object_template->NewInstance());
+
+ widget->Set(
+ v8::String::NewFromUtf8(isolate, "author"),
+ v8::String::NewFromUtf8(isolate, widgetdb->author().c_str()));
+ widget->Set(
+ v8::String::NewFromUtf8(isolate, "description"),
+ v8::String::NewFromUtf8(isolate, widgetdb->description().c_str()));
+ widget->Set(
+ v8::String::NewFromUtf8(isolate, "name"),
+ v8::String::NewFromUtf8(isolate, widgetdb->name().c_str()));
+ widget->Set(
+ v8::String::NewFromUtf8(isolate, "shortName"),
+ v8::String::NewFromUtf8(isolate, widgetdb->shortName().c_str()));
+ widget->Set(
+ v8::String::NewFromUtf8(isolate, "version"),
+ v8::String::NewFromUtf8(isolate, widgetdb->version().c_str()));
+ widget->Set(
+ v8::String::NewFromUtf8(isolate, "id"),
+ v8::String::NewFromUtf8(isolate, widgetdb->id().c_str()));
+ widget->Set(
+ v8::String::NewFromUtf8(isolate, "authorEmail"),
+ v8::String::NewFromUtf8(isolate, widgetdb->authorEmail().c_str()));
+ widget->Set(
+ v8::String::NewFromUtf8(isolate, "authorHref"),
+ v8::String::NewFromUtf8(isolate, widgetdb->authorHref().c_str()));
+ widget->Set(
+ v8::String::NewFromUtf8(isolate, "height"),
+ v8::Uint32::New(isolate, widgetdb->height()));
+ widget->Set(
+ v8::String::NewFromUtf8(isolate, "width"),
+ v8::Uint32::New(isolate, widgetdb->width()));
+
+ return handle_scope.Escape(widget);
+}
+
+
+namespace {
+ const char* kDbInitedCheckKey = "__WRT_DB_INITED__";
+ const char* kDBPublicSection = "public";
+ const char* kDBPrivateSection = "private";
+ const char* kReadOnlyPrefix = "_READONLY_KEY_";
+} // namespace
+
+
+WidgetPreferenceDB* WidgetPreferenceDB::GetInstance() {
+ static WidgetPreferenceDB instance;
+ return &instance;
+}
+
+WidgetPreferenceDB::WidgetPreferenceDB() {
+}
+WidgetPreferenceDB::~WidgetPreferenceDB() {
+}
+
+void WidgetPreferenceDB::Initialize(const ApplicationData* appdata,
+ LocaleManager* locale_manager) {
+ appdata_ = appdata;
+ locale_manager_ = locale_manager;
+
+ AppDB* db = AppDB::GetInstance();
+ if (db->HasKey(kDBPrivateSection, kDbInitedCheckKey)) {
+ return;
+ }
+ if (appdata->widget_info() == NULL) {
+ return;
+ }
+
+ auto& preferences = appdata->widget_info()->preferences();
+
+ for (const auto& pref : preferences) {
+ db->Set(kDBPublicSection,
+ pref->Name(),
+ pref->Value());
+ if (pref->ReadOnly()) {
+ db->Set(kDBPrivateSection,
+ kReadOnlyPrefix + pref->Name(), "true");
+ }
+ }
+ db->Set(kDBPrivateSection, kDbInitedCheckKey, "true");
+}
+
+int WidgetPreferenceDB::Length() {
+ AppDB* db = AppDB::GetInstance();
+ std::list<std::string> list;
+ db->GetKeys(kDBPublicSection, &list);
+ return list.size();
+}
+
+bool WidgetPreferenceDB::Key(int idx, std::string* key) {
+ AppDB* db = AppDB::GetInstance();
+ std::list<std::string> list;
+ db->GetKeys(kDBPublicSection, &list);
+
+ auto it = list.begin();
+ for ( ; it != list.end() && idx >= 0; ++it) {
+ if (idx == 0) {
+ *key = *it;
+ return true;
+ }
+ idx--;
+ }
+ return false;
+}
+
+bool WidgetPreferenceDB::GetItem(const std::string& key, std::string* value) {
+ AppDB* db = AppDB::GetInstance();
+ if (!db->HasKey(kDBPublicSection, key))
+ return false;
+ *value = db->Get(kDBPublicSection, key);
+ return true;
+}
+
+bool WidgetPreferenceDB::SetItem(const std::string& key,
+ const std::string& value) {
+ AppDB* db = AppDB::GetInstance();
+ if (db->HasKey(kDBPrivateSection, kReadOnlyPrefix + key))
+ return false;
+ db->Set(kDBPublicSection, key, value);
+ return true;
+}
+
+bool WidgetPreferenceDB::RemoveItem(const std::string& key) {
+ AppDB* db = AppDB::GetInstance();
+ if (!db->HasKey(kDBPublicSection, key))
+ return false;
+ if (db->HasKey(kDBPrivateSection, kReadOnlyPrefix + key))
+ return false;
+ db->Remove(kDBPublicSection, key);
+ return true;
+}
+
+bool WidgetPreferenceDB::HasItem(const std::string& key) {
+ AppDB* db = AppDB::GetInstance();
+ return db->HasKey(kDBPublicSection, key);
+}
+
+void WidgetPreferenceDB::Clear() {
+ AppDB* db = AppDB::GetInstance();
+ std::list<std::string> list;
+ db->GetKeys(kDBPublicSection, &list);
+ auto it = list.begin();
+ for ( ; it != list.end(); ++it) {
+ if (db->HasKey(kDBPrivateSection, kReadOnlyPrefix + *it))
+ continue;
+ db->Remove(kDBPublicSection, *it);
+ }
+}
+
+void WidgetPreferenceDB::GetKeys(std::list<std::string>* keys) {
+ AppDB* db = AppDB::GetInstance();
+ db->GetKeys(kDBPublicSection, keys);
+}
+
+std::string WidgetPreferenceDB::author() {
+ if (appdata_ == NULL ||
+ appdata_->widget_info() == NULL)
+ return std::string();
+ auto widget_info = appdata_->widget_info();
+ return widget_info->author();
+}
+
+std::string WidgetPreferenceDB::description() {
+ if (appdata_ == NULL ||
+ appdata_->widget_info() == NULL ||
+ locale_manager_ == NULL)
+ return std::string();
+ auto widget_info = appdata_->widget_info();
+ return locale_manager_->GetLocalizedString(widget_info->description_set());
+}
+
+std::string WidgetPreferenceDB::name() {
+ if (appdata_ == NULL ||
+ appdata_->widget_info() == NULL ||
+ locale_manager_ == NULL)
+ return std::string();
+
+ auto widget_info = appdata_->widget_info();
+ return locale_manager_->GetLocalizedString(widget_info->name_set());
+}
+
+std::string WidgetPreferenceDB::shortName() {
+ if (appdata_ == NULL ||
+ appdata_->widget_info() == NULL ||
+ locale_manager_ == NULL)
+ return std::string();
+
+ auto widget_info = appdata_->widget_info();
+ return locale_manager_->GetLocalizedString(widget_info->short_name_set());
+}
+
+std::string WidgetPreferenceDB::version() {
+ if (appdata_ == NULL ||
+ appdata_->widget_info() == NULL)
+ return std::string();
+ auto widget_info = appdata_->widget_info();
+ return widget_info->version();
+}
+
+std::string WidgetPreferenceDB::id() {
+ if (appdata_ == NULL ||
+ appdata_->widget_info() == NULL)
+ return std::string();
+ auto widget_info = appdata_->widget_info();
+ return widget_info->id();
+}
+
+std::string WidgetPreferenceDB::authorEmail() {
+ if (appdata_ == NULL ||
+ appdata_->widget_info() == NULL)
+ return std::string();
+ auto widget_info = appdata_->widget_info();
+ return widget_info->author_email();
+}
+
+std::string WidgetPreferenceDB::authorHref() {
+ if (appdata_ == NULL ||
+ appdata_->widget_info() == NULL)
+ return std::string();
+ auto widget_info = appdata_->widget_info();
+ return widget_info->author_href();
+}
+
+unsigned int WidgetPreferenceDB::height() {
+ if (appdata_ == NULL ||
+ appdata_->widget_info() == NULL)
+ return 0;
+ auto widget_info = appdata_->widget_info();
+ return widget_info->height();
+}
+
+unsigned int WidgetPreferenceDB::width() {
+ if (appdata_ == NULL ||
+ appdata_->widget_info() == NULL)
+ return 0;
+ auto widget_info = appdata_->widget_info();
+ return widget_info->width();
+}
+
+} // namespace wrt
--- /dev/null
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef WRT_BUNDLE_WIDGET_MODULE_H_
+#define WRT_BUNDLE_WIDGET_MODULE_H_
+
+#include <list>
+#include <string>
+#include "bundle/module_system.h"
+
+namespace wrt {
+class ApplicationData;
+class LocaleManager;
+
+// This module provides widget object
+class WidgetModule : public NativeModule {
+ public:
+ WidgetModule();
+ ~WidgetModule() override;
+
+ private:
+ v8::Handle<v8::Object> NewInstance() override;
+ v8::Persistent<v8::ObjectTemplate> preference_object_template_;
+};
+
+class WidgetPreferenceDB {
+ public:
+ static WidgetPreferenceDB* GetInstance();
+ void Initialize(const ApplicationData* appdata,
+ LocaleManager* locale_manager);
+ int Length();
+ bool Key(int idx, std::string* key);
+ bool GetItem(const std::string& key, std::string* value);
+ bool SetItem(const std::string& key, const std::string& value);
+ bool RemoveItem(const std::string& key);
+ bool HasItem(const std::string& key);
+ void Clear();
+ void GetKeys(std::list<std::string>* keys);
+
+ std::string author();
+ std::string description();
+ std::string name();
+ std::string shortName();
+ std::string version();
+ std::string id();
+ std::string authorEmail();
+ std::string authorHref();
+ unsigned int height();
+ unsigned int width();
+
+ private:
+ WidgetPreferenceDB();
+ virtual ~WidgetPreferenceDB();
+ const ApplicationData* appdata_;
+ LocaleManager* locale_manager_;
+};
+} // namespace wrt
+
+#endif // WRT_BUNDLE_WIDGET_MODULE_H_
+++ /dev/null
-/*
- * Copyright 2009-2010 Cybozu Labs, Inc.
- * Copyright 2011 Kazuho Oku
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY CYBOZU LABS, INC. ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
- * EVENT SHALL CYBOZU LABS, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * The views and conclusions contained in the software and documentation are
- * those of the authors and should not be interpreted as representing official
- * policies, either expressed or implied, of Cybozu Labs, Inc.
- *
- */
-#ifndef picojson_h
-#define picojson_h
-
-#include <algorithm>
-#include <cassert>
-#include <cmath>
-#include <cstdio>
-#include <cstdlib>
-#include <cstring>
-#include <iostream>
-#include <iterator>
-#include <map>
-#include <string>
-#include <vector>
-
-#ifdef _MSC_VER
- #define SNPRINTF _snprintf_s
- #pragma warning(push)
- #pragma warning(disable : 4244) // conversion from int to char
-#else
- #define SNPRINTF snprintf
-#endif
-
-namespace picojson {
-
- enum {
- null_type,
- boolean_type,
- number_type,
- string_type,
- array_type,
- object_type
- };
-
- struct null {};
-
- class value {
- public:
- typedef std::vector<value> array;
- typedef std::map<std::string, value> object;
- union _storage {
- bool boolean_;
- double number_;
- std::string* string_;
- array* array_;
- object* object_;
- };
- protected:
- int type_;
- _storage u_;
- public:
- value();
- value(int type, bool);
- explicit value(bool b);
- explicit value(double n);
- explicit value(const std::string& s);
- explicit value(const array& a);
- explicit value(const object& o);
- explicit value(const char* s);
- value(const char* s, size_t len);
- ~value();
- value(const value& x);
- value& operator=(const value& x);
- void swap(value& x);
- template <typename T> bool is() const;
- template <typename T> const T& get() const;
- template <typename T> T& get();
- bool evaluate_as_boolean() const;
- const value& get(size_t idx) const;
- const value& get(const std::string& key) const;
- bool contains(size_t idx) const;
- bool contains(const std::string& key) const;
- std::string to_str() const;
- template <typename Iter> void serialize(Iter os) const;
- std::string serialize() const;
- private:
- template <typename T> value(const T*); // intentionally defined to block implicit conversion of pointer to bool
- };
-
- typedef value::array array;
- typedef value::object object;
-
- inline value::value() : type_(null_type) {}
-
- inline value::value(int type, bool) : type_(type) {
- switch (type) {
-#define INIT(p, v) case p##type: u_.p = v; break
- INIT(boolean_, false);
- INIT(number_, 0.0);
- INIT(string_, new std::string());
- INIT(array_, new array());
- INIT(object_, new object());
-#undef INIT
- default: break;
- }
- }
-
- inline value::value(bool b) : type_(boolean_type) {
- u_.boolean_ = b;
- }
-
- inline value::value(double n) : type_(number_type) {
- u_.number_ = n;
- }
-
- inline value::value(const std::string& s) : type_(string_type) {
- u_.string_ = new std::string(s);
- }
-
- inline value::value(const array& a) : type_(array_type) {
- u_.array_ = new array(a);
- }
-
- inline value::value(const object& o) : type_(object_type) {
- u_.object_ = new object(o);
- }
-
- inline value::value(const char* s) : type_(string_type) {
- u_.string_ = new std::string(s);
- }
-
- inline value::value(const char* s, size_t len) : type_(string_type) {
- u_.string_ = new std::string(s, len);
- }
-
- inline value::~value() {
- switch (type_) {
-#define DEINIT(p) case p##type: delete u_.p; break
- DEINIT(string_);
- DEINIT(array_);
- DEINIT(object_);
-#undef DEINIT
- default: break;
- }
- }
-
- inline value::value(const value& x) : type_(x.type_) {
- switch (type_) {
-#define INIT(p, v) case p##type: u_.p = v; break
- INIT(string_, new std::string(*x.u_.string_));
- INIT(array_, new array(*x.u_.array_));
- INIT(object_, new object(*x.u_.object_));
-#undef INIT
- default:
- u_ = x.u_;
- break;
- }
- }
-
- inline value& value::operator=(const value& x) {
- if (this != &x) {
- this->~value();
- new (this) value(x);
- }
- return *this;
- }
-
- inline void value::swap(value& x) {
- std::swap(type_, x.type_);
- std::swap(u_, x.u_);
- }
-
-#define IS(ctype, jtype) \
- template <> inline bool value::is<ctype>() const { \
- return type_ == jtype##_type; \
- }
- IS(null, null)
- IS(bool, boolean)
- IS(int, number)
- IS(double, number)
- IS(std::string, string)
- IS(array, array)
- IS(object, object)
-#undef IS
-
-#define GET(ctype, var) \
- template <> inline const ctype& value::get<ctype>() const { \
- assert("type mismatch! call vis<type>() before get<type>()" \
- && is<ctype>()); \
- return var; \
- } \
- template <> inline ctype& value::get<ctype>() { \
- assert("type mismatch! call is<type>() before get<type>()" \
- && is<ctype>()); \
- return var; \
- }
- GET(bool, u_.boolean_)
- GET(double, u_.number_)
- GET(std::string, *u_.string_)
- GET(array, *u_.array_)
- GET(object, *u_.object_)
-#undef GET
-
- inline bool value::evaluate_as_boolean() const {
- switch (type_) {
- case null_type:
- return false;
- case boolean_type:
- return u_.boolean_;
- case number_type:
- return u_.number_ != 0;
- case string_type:
- return ! u_.string_->empty();
- default:
- return true;
- }
- }
-
- inline const value& value::get(size_t idx) const {
- static value s_null;
- assert(is<array>());
- return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null;
- }
-
- inline const value& value::get(const std::string& key) const {
- static value s_null;
- assert(is<object>());
- object::const_iterator i = u_.object_->find(key);
- return i != u_.object_->end() ? i->second : s_null;
- }
-
- inline bool value::contains(size_t idx) const {
- assert(is<array>());
- return idx < u_.array_->size();
- }
-
- inline bool value::contains(const std::string& key) const {
- assert(is<object>());
- object::const_iterator i = u_.object_->find(key);
- return i != u_.object_->end();
- }
-
- inline std::string value::to_str() const {
- switch (type_) {
- case null_type: return "null";
- case boolean_type: return u_.boolean_ ? "true" : "false";
- case number_type: {
- char buf[256];
- double tmp;
- SNPRINTF(buf, sizeof(buf), fabs(u_.number_) < (1ULL << 53) && modf(u_.number_, &tmp) == 0 ? "%.f" : "%.17g", u_.number_);
- return buf;
- }
- case string_type: return *u_.string_;
- case array_type: return "array";
- case object_type: return "object";
- default: assert(0);
-#ifdef _MSC_VER
- __assume(0);
-#endif
- }
- return std::string();
- }
-
- template <typename Iter> void copy(const std::string& s, Iter oi) {
- std::copy(s.begin(), s.end(), oi);
- }
-
- template <typename Iter> void serialize_str(const std::string& s, Iter oi) {
- *oi++ = '"';
- for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
- switch (*i) {
-#define MAP(val, sym) case val: copy(sym, oi); break
- MAP('"', "\\\"");
- MAP('\\', "\\\\");
- MAP('/', "\\/");
- MAP('\b', "\\b");
- MAP('\f', "\\f");
- MAP('\n', "\\n");
- MAP('\r', "\\r");
- MAP('\t', "\\t");
-#undef MAP
- default:
- if ((unsigned char)*i < 0x20 || *i == 0x7f) {
- char buf[7];
- SNPRINTF(buf, sizeof(buf), "\\u%04x", *i & 0xff);
- copy(buf, buf + 6, oi);
- } else {
- *oi++ = *i;
- }
- break;
- }
- }
- *oi++ = '"';
- }
-
- template <typename Iter> void value::serialize(Iter oi) const {
- switch (type_) {
- case string_type:
- serialize_str(*u_.string_, oi);
- break;
- case array_type: {
- *oi++ = '[';
- for (array::const_iterator i = u_.array_->begin();
- i != u_.array_->end();
- ++i) {
- if (i != u_.array_->begin()) {
- *oi++ = ',';
- }
- i->serialize(oi);
- }
- *oi++ = ']';
- break;
- }
- case object_type: {
- *oi++ = '{';
- for (object::const_iterator i = u_.object_->begin();
- i != u_.object_->end();
- ++i) {
- if (i != u_.object_->begin()) {
- *oi++ = ',';
- }
- serialize_str(i->first, oi);
- *oi++ = ':';
- i->second.serialize(oi);
- }
- *oi++ = '}';
- break;
- }
- default:
- copy(to_str(), oi);
- break;
- }
- }
-
- inline std::string value::serialize() const {
- std::string s;
- serialize(std::back_inserter(s));
- return s;
- }
-
- template <typename Iter> class input {
- protected:
- Iter cur_, end_;
- int last_ch_;
- bool ungot_;
- int line_;
- public:
- input(const Iter& first, const Iter& last) : cur_(first), end_(last), last_ch_(-1), ungot_(false), line_(1) {}
- int getc() {
- if (ungot_) {
- ungot_ = false;
- return last_ch_;
- }
- if (cur_ == end_) {
- last_ch_ = -1;
- return -1;
- }
- if (last_ch_ == '\n') {
- line_++;
- }
- last_ch_ = *cur_++ & 0xff;
- return last_ch_;
- }
- void ungetc() {
- if (last_ch_ != -1) {
- assert(! ungot_);
- ungot_ = true;
- }
- }
- Iter cur() const { return cur_; }
- int line() const { return line_; }
- void skip_ws() {
- while (1) {
- int ch = getc();
- if (! (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')) {
- ungetc();
- break;
- }
- }
- }
- bool expect(int expect) {
- skip_ws();
- if (getc() != expect) {
- ungetc();
- return false;
- }
- return true;
- }
- bool match(const std::string& pattern) {
- for (std::string::const_iterator pi(pattern.begin());
- pi != pattern.end();
- ++pi) {
- if (getc() != *pi) {
- ungetc();
- return false;
- }
- }
- return true;
- }
- };
-
- template<typename Iter> inline int _parse_quadhex(input<Iter> &in) {
- int uni_ch = 0, hex;
- for (int i = 0; i < 4; i++) {
- if ((hex = in.getc()) == -1) {
- return -1;
- }
- if ('0' <= hex && hex <= '9') {
- hex -= '0';
- } else if ('A' <= hex && hex <= 'F') {
- hex -= 'A' - 0xa;
- } else if ('a' <= hex && hex <= 'f') {
- hex -= 'a' - 0xa;
- } else {
- in.ungetc();
- return -1;
- }
- uni_ch = uni_ch * 16 + hex;
- }
- return uni_ch;
- }
-
- template<typename String, typename Iter> inline bool _parse_codepoint(String& out, input<Iter>& in) {
- int uni_ch;
- if ((uni_ch = _parse_quadhex(in)) == -1) {
- return false;
- }
- if (0xd800 <= uni_ch && uni_ch <= 0xdfff) {
- if (0xdc00 <= uni_ch) {
- // a second 16-bit of a surrogate pair appeared
- return false;
- }
- // first 16-bit of surrogate pair, get the next one
- if (in.getc() != '\\' || in.getc() != 'u') {
- in.ungetc();
- return false;
- }
- int second = _parse_quadhex(in);
- if (! (0xdc00 <= second && second <= 0xdfff)) {
- return false;
- }
- uni_ch = ((uni_ch - 0xd800) << 10) | ((second - 0xdc00) & 0x3ff);
- uni_ch += 0x10000;
- }
- if (uni_ch < 0x80) {
- out.push_back(uni_ch);
- } else {
- if (uni_ch < 0x800) {
- out.push_back(0xc0 | (uni_ch >> 6));
- } else {
- if (uni_ch < 0x10000) {
- out.push_back(0xe0 | (uni_ch >> 12));
- } else {
- out.push_back(0xf0 | (uni_ch >> 18));
- out.push_back(0x80 | ((uni_ch >> 12) & 0x3f));
- }
- out.push_back(0x80 | ((uni_ch >> 6) & 0x3f));
- }
- out.push_back(0x80 | (uni_ch & 0x3f));
- }
- return true;
- }
-
- template<typename String, typename Iter> inline bool _parse_string(String& out, input<Iter>& in) {
- while (1) {
- int ch = in.getc();
- if (ch < ' ') {
- in.ungetc();
- return false;
- } else if (ch == '"') {
- return true;
- } else if (ch == '\\') {
- if ((ch = in.getc()) == -1) {
- return false;
- }
- switch (ch) {
-#define MAP(sym, val) case sym: out.push_back(val); break
- MAP('"', '\"');
- MAP('\\', '\\');
- MAP('/', '/');
- MAP('b', '\b');
- MAP('f', '\f');
- MAP('n', '\n');
- MAP('r', '\r');
- MAP('t', '\t');
-#undef MAP
- case 'u':
- if (! _parse_codepoint(out, in)) {
- return false;
- }
- break;
- default:
- return false;
- }
- } else {
- out.push_back(ch);
- }
- }
- return false;
- }
-
- template <typename Context, typename Iter> inline bool _parse_array(Context& ctx, input<Iter>& in) {
- if (! ctx.parse_array_start()) {
- return false;
- }
- if (in.expect(']')) {
- return true;
- }
- size_t idx = 0;
- do {
- if (! ctx.parse_array_item(in, idx)) {
- return false;
- }
- idx++;
- } while (in.expect(','));
- return in.expect(']');
- }
-
- template <typename Context, typename Iter> inline bool _parse_object(Context& ctx, input<Iter>& in) {
- if (! ctx.parse_object_start()) {
- return false;
- }
- if (in.expect('}')) {
- return true;
- }
- do {
- std::string key;
- if (! in.expect('"')
- || ! _parse_string(key, in)
- || ! in.expect(':')) {
- return false;
- }
- if (! ctx.parse_object_item(in, key)) {
- return false;
- }
- } while (in.expect(','));
- return in.expect('}');
- }
-
- template <typename Iter> inline bool _parse_number(double& out, input<Iter>& in) {
- std::string num_str;
- while (1) {
- int ch = in.getc();
- if (('0' <= ch && ch <= '9') || ch == '+' || ch == '-' || ch == '.'
- || ch == 'e' || ch == 'E') {
- num_str.push_back(ch);
- } else {
- in.ungetc();
- break;
- }
- }
- char* endp;
- out = strtod(num_str.c_str(), &endp);
- return endp == num_str.c_str() + num_str.size();
- }
-
- template <typename Context, typename Iter> inline bool _parse(Context& ctx, input<Iter>& in) {
- in.skip_ws();
- int ch = in.getc();
- switch (ch) {
-#define IS(ch, text, op) case ch: \
- if (in.match(text) && op) { \
- return true; \
- } else { \
- return false; \
- }
- IS('n', "ull", ctx.set_null());
- IS('f', "alse", ctx.set_bool(false));
- IS('t', "rue", ctx.set_bool(true));
-#undef IS
- case '"':
- return ctx.parse_string(in);
- case '[':
- return _parse_array(ctx, in);
- case '{':
- return _parse_object(ctx, in);
- default:
- if (('0' <= ch && ch <= '9') || ch == '-') {
- in.ungetc();
- double f;
- if (_parse_number(f, in)) {
- ctx.set_number(f);
- return true;
- } else {
- return false;
- }
- }
- break;
- }
- in.ungetc();
- return false;
- }
-
- class deny_parse_context {
- public:
- bool set_null() { return false; }
- bool set_bool(bool) { return false; }
- bool set_number(double) { return false; }
- template <typename Iter> bool parse_string(input<Iter>&) { return false; }
- bool parse_array_start() { return false; }
- template <typename Iter> bool parse_array_item(input<Iter>&, size_t) {
- return false;
- }
- bool parse_object_start() { return false; }
- template <typename Iter> bool parse_object_item(input<Iter>&, const std::string&) {
- return false;
- }
- };
-
- class default_parse_context {
- protected:
- value* out_;
- public:
- default_parse_context(value* out) : out_(out) {}
- bool set_null() {
- *out_ = value();
- return true;
- }
- bool set_bool(bool b) {
- *out_ = value(b);
- return true;
- }
- bool set_number(double f) {
- *out_ = value(f);
- return true;
- }
- template<typename Iter> bool parse_string(input<Iter>& in) {
- *out_ = value(string_type, false);
- return _parse_string(out_->get<std::string>(), in);
- }
- bool parse_array_start() {
- *out_ = value(array_type, false);
- return true;
- }
- template <typename Iter> bool parse_array_item(input<Iter>& in, size_t) {
- array& a = out_->get<array>();
- a.push_back(value());
- default_parse_context ctx(&a.back());
- return _parse(ctx, in);
- }
- bool parse_object_start() {
- *out_ = value(object_type, false);
- return true;
- }
- template <typename Iter> bool parse_object_item(input<Iter>& in, const std::string& key) {
- object& o = out_->get<object>();
- default_parse_context ctx(&o[key]);
- return _parse(ctx, in);
- }
- private:
- default_parse_context(const default_parse_context&);
- default_parse_context& operator=(const default_parse_context&);
- };
-
- class null_parse_context {
- public:
- struct dummy_str {
- void push_back(int) {}
- };
- public:
- null_parse_context() {}
- bool set_null() { return true; }
- bool set_bool(bool) { return true; }
- bool set_number(double) { return true; }
- template <typename Iter> bool parse_string(input<Iter>& in) {
- dummy_str s;
- return _parse_string(s, in);
- }
- bool parse_array_start() { return true; }
- template <typename Iter> bool parse_array_item(input<Iter>& in, size_t) {
- return _parse(*this, in);
- }
- bool parse_object_start() { return true; }
- template <typename Iter> bool parse_object_item(input<Iter>& in, const std::string&) {
- return _parse(*this, in);
- }
- private:
- null_parse_context(const null_parse_context&);
- null_parse_context& operator=(const null_parse_context&);
- };
-
- // obsolete, use the version below
- template <typename Iter> inline std::string parse(value& out, Iter& pos, const Iter& last) {
- std::string err;
- pos = parse(out, pos, last, &err);
- return err;
- }
-
- template <typename Context, typename Iter> inline Iter _parse(Context& ctx, const Iter& first, const Iter& last, std::string* err) {
- input<Iter> in(first, last);
- if (! _parse(ctx, in) && err != NULL) {
- char buf[64];
- SNPRINTF(buf, sizeof(buf), "syntax error at line %d near: ", in.line());
- *err = buf;
- while (1) {
- int ch = in.getc();
- if (ch == -1 || ch == '\n') {
- break;
- } else if (ch >= ' ') {
- err->push_back(ch);
- }
- }
- }
- return in.cur();
- }
-
- template <typename Iter> inline Iter parse(value& out, const Iter& first, const Iter& last, std::string* err) {
- default_parse_context ctx(&out);
- return _parse(ctx, first, last, err);
- }
-
- inline std::string parse(value& out, std::istream& is) {
- std::string err;
- parse(out, std::istreambuf_iterator<char>(is.rdbuf()),
- std::istreambuf_iterator<char>(), &err);
- return err;
- }
-
- template <typename T> struct last_error_t {
- static std::string s;
- };
- template <typename T> std::string last_error_t<T>::s;
-
- inline void set_last_error(const std::string& s) {
- last_error_t<bool>::s = s;
- }
-
- inline const std::string& get_last_error() {
- return last_error_t<bool>::s;
- }
-
- inline bool operator==(const value& x, const value& y) {
- if (x.is<null>())
- return y.is<null>();
-#define PICOJSON_CMP(type) \
- if (x.is<type>()) \
- return y.is<type>() && x.get<type>() == y.get<type>()
- PICOJSON_CMP(bool);
- PICOJSON_CMP(double);
- PICOJSON_CMP(std::string);
- PICOJSON_CMP(array);
- PICOJSON_CMP(object);
-#undef PICOJSON_CMP
- assert(0);
-#ifdef _MSC_VER
- __assume(0);
-#endif
- return false;
- }
-
- inline bool operator!=(const value& x, const value& y) {
- return ! (x == y);
- }
-}
-
-namespace std {
- template<> inline void swap(picojson::value& x, picojson::value& y)
- {
- x.swap(y);
- }
-}
-
-inline std::istream& operator>>(std::istream& is, picojson::value& x)
-{
- picojson::set_last_error(std::string());
- std::string err = picojson::parse(x, is);
- if (! err.empty()) {
- picojson::set_last_error(err);
- is.setstate(std::ios::failbit);
- }
- return is;
-}
-
-inline std::ostream& operator<<(std::ostream& os, const picojson::value& x)
-{
- x.serialize(std::ostream_iterator<char>(os));
- return os;
-}
-#ifdef _MSC_VER
- #pragma warning(pop)
-#endif
-
-#endif
-#ifdef TEST_PICOJSON
-#ifdef _MSC_VER
- #pragma warning(disable : 4127) // conditional expression is constant
-#endif
-
-using namespace std;
-
-static void plan(int num)
-{
- printf("1..%d\n", num);
-}
-
-static bool success = true;
-
-static void ok(bool b, const char* name = "")
-{
- static int n = 1;
- if (! b)
- success = false;
- printf("%s %d - %s\n", b ? "ok" : "ng", n++, name);
-}
-
-template <typename T> void is(const T& x, const T& y, const char* name = "")
-{
- if (x == y) {
- ok(true, name);
- } else {
- ok(false, name);
- }
-}
-
-#include <algorithm>
-#include <sstream>
-#include <float.h>
-#include <limits.h>
-
-int main(void)
-{
- plan(85);
-
- // constructors
-#define TEST(expr, expected) \
- is(picojson::value expr .serialize(), string(expected), "picojson::value" #expr)
-
- TEST( (true), "true");
- TEST( (false), "false");
- TEST( (42.0), "42");
- TEST( (string("hello")), "\"hello\"");
- TEST( ("hello"), "\"hello\"");
- TEST( ("hello", 4), "\"hell\"");
-
- {
- double a = 1;
- for (int i = 0; i < 1024; i++) {
- picojson::value vi(a);
- std::stringstream ss;
- ss << vi;
- picojson::value vo;
- ss >> vo;
- double b = vo.get<double>();
- if ((i < 53 && a != b) || fabs(a - b) / b > 1e-8) {
- printf("ng i=%d a=%.18e b=%.18e\n", i, a, b);
- }
- a *= 2;
- }
- }
-
-#undef TEST
-
-#define TEST(in, type, cmp, serialize_test) { \
- picojson::value v; \
- const char* s = in; \
- string err = picojson::parse(v, s, s + strlen(s)); \
- ok(err.empty(), in " no error"); \
- ok(v.is<type>(), in " check type"); \
- is<type>(v.get<type>(), cmp, in " correct output"); \
- is(*s, '\0', in " read to eof"); \
- if (serialize_test) { \
- is(v.serialize(), string(in), in " serialize"); \
- } \
- }
- TEST("false", bool, false, true);
- TEST("true", bool, true, true);
- TEST("90.5", double, 90.5, false);
- TEST("1.7976931348623157e+308", double, DBL_MAX, false);
- TEST("\"hello\"", string, string("hello"), true);
- TEST("\"\\\"\\\\\\/\\b\\f\\n\\r\\t\"", string, string("\"\\/\b\f\n\r\t"),
- true);
- TEST("\"\\u0061\\u30af\\u30ea\\u30b9\"", string,
- string("a\xe3\x82\xaf\xe3\x83\xaa\xe3\x82\xb9"), false);
- TEST("\"\\ud840\\udc0b\"", string, string("\xf0\xa0\x80\x8b"), false);
-#undef TEST
-
-#define TEST(type, expr) { \
- picojson::value v; \
- const char *s = expr; \
- string err = picojson::parse(v, s, s + strlen(s)); \
- ok(err.empty(), "empty " #type " no error"); \
- ok(v.is<picojson::type>(), "empty " #type " check type"); \
- ok(v.get<picojson::type>().empty(), "check " #type " array size"); \
- }
- TEST(array, "[]");
- TEST(object, "{}");
-#undef TEST
-
- {
- picojson::value v;
- const char *s = "[1,true,\"hello\"]";
- string err = picojson::parse(v, s, s + strlen(s));
- ok(err.empty(), "array no error");
- ok(v.is<picojson::array>(), "array check type");
- is(v.get<picojson::array>().size(), size_t(3), "check array size");
- ok(v.contains(0), "check contains array[0]");
- ok(v.get(0).is<double>(), "check array[0] type");
- is(v.get(0).get<double>(), 1.0, "check array[0] value");
- ok(v.contains(1), "check contains array[1]");
- ok(v.get(1).is<bool>(), "check array[1] type");
- ok(v.get(1).get<bool>(), "check array[1] value");
- ok(v.contains(2), "check contains array[2]");
- ok(v.get(2).is<string>(), "check array[2] type");
- is(v.get(2).get<string>(), string("hello"), "check array[2] value");
- ok(!v.contains(3), "check not contains array[3]");
- }
-
- {
- picojson::value v;
- const char *s = "{ \"a\": true }";
- string err = picojson::parse(v, s, s + strlen(s));
- ok(err.empty(), "object no error");
- ok(v.is<picojson::object>(), "object check type");
- is(v.get<picojson::object>().size(), size_t(1), "check object size");
- ok(v.contains("a"), "check contains property");
- ok(v.get("a").is<bool>(), "check bool property exists");
- is(v.get("a").get<bool>(), true, "check bool property value");
- is(v.serialize(), string("{\"a\":true}"), "serialize object");
- ok(!v.contains("z"), "check not contains property");
- }
-
-#define TEST(json, msg) do { \
- picojson::value v; \
- const char *s = json; \
- string err = picojson::parse(v, s, s + strlen(s)); \
- is(err, string("syntax error at line " msg), msg); \
- } while (0)
- TEST("falsoa", "1 near: oa");
- TEST("{]", "1 near: ]");
- TEST("\n\bbell", "2 near: bell");
- TEST("\"abc\nd\"", "1 near: ");
-#undef TEST
-
- {
- picojson::value v1, v2;
- const char *s;
- string err;
- s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
- err = picojson::parse(v1, s, s + strlen(s));
- s = "{ \"d\": 2.0, \"b\": true, \"a\": [1,2,\"three\"] }";
- err = picojson::parse(v2, s, s + strlen(s));
- ok((v1 == v2), "check == operator in deep comparison");
- }
-
- {
- picojson::value v1, v2;
- const char *s;
- string err;
- s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
- err = picojson::parse(v1, s, s + strlen(s));
- s = "{ \"d\": 2.0, \"a\": [1,\"three\"], \"b\": true }";
- err = picojson::parse(v2, s, s + strlen(s));
- ok((v1 != v2), "check != operator for array in deep comparison");
- }
-
- {
- picojson::value v1, v2;
- const char *s;
- string err;
- s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
- err = picojson::parse(v1, s, s + strlen(s));
- s = "{ \"d\": 2.0, \"a\": [1,2,\"three\"], \"b\": false }";
- err = picojson::parse(v2, s, s + strlen(s));
- ok((v1 != v2), "check != operator for object in deep comparison");
- }
-
- {
- picojson::value v1, v2;
- const char *s;
- string err;
- s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
- err = picojson::parse(v1, s, s + strlen(s));
- picojson::object& o = v1.get<picojson::object>();
- o.erase("b");
- picojson::array& a = o["a"].get<picojson::array>();
- picojson::array::iterator i;
- i = std::remove(a.begin(), a.end(), picojson::value(std::string("three")));
- a.erase(i, a.end());
- s = "{ \"a\": [1,2], \"d\": 2 }";
- err = picojson::parse(v2, s, s + strlen(s));
- ok((v1 == v2), "check erase()");
- }
-
- ok(picojson::value(3.0).serialize() == "3",
- "integral number should be serialized as a integer");
-
- {
- const char* s = "{ \"a\": [1,2], \"d\": 2 }";
- picojson::null_parse_context ctx;
- string err;
- picojson::_parse(ctx, s, s + strlen(s), &err);
- ok(err.empty(), "null_parse_context");
- }
-
- {
- picojson::value v1, v2;
- v1 = picojson::value(true);
- swap(v1, v2);
- ok(v1.is<picojson::null>(), "swap (null)");
- ok(v2.get<bool>() == true, "swap (bool)");
-
- v1 = picojson::value("a");
- v2 = picojson::value(1.0);
- swap(v1, v2);
- ok(v1.get<double>() == 1.0, "swap (dobule)");
- ok(v2.get<string>() == "a", "swap (string)");
-
- v1 = picojson::value(picojson::object());
- v2 = picojson::value(picojson::array());
- swap(v1, v2);
- ok(v1.is<picojson::array>(), "swap (array)");
- ok(v2.is<picojson::object>(), "swap (object)");
- }
-
- return success ? 0 : 1;
-}
-
-#endif
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#include "extension/widget/widget.h"
-
#include <list>
#include <memory>
#include <map>
#include "extension/xwalk/XW_Extension_Runtime.h"
#include "extension/xwalk/XW_Extension_SyncMessage.h"
-#include "extension/widget/picojson.h"
#include "common/logger.h"
-#include "common/app_db.h"
#include "common/application_data.h"
#include "common/locale_manager.h"
#include "common/string_utils.h"
XW_Extension g_xw_extension = 0;
-std::string g_appid;
-std::unique_ptr<wrt::ApplicationData> g_appdata;
const XW_CoreInterface* g_core = NULL;
const XW_MessagingInterface* g_messaging = NULL;
const XW_Internal_SyncMessagingInterface* g_sync_messaging = NULL;
const XW_Internal_RuntimeInterface* g_runtime = NULL;
extern const char kSource_widget_api[];
-typedef void (*CmdHandler)(const picojson::value& args, picojson::object* out);
-
-static void InitHandler(const picojson::value& args, picojson::object* out);
-static void KeyHandler(const picojson::value& args, picojson::object* out);
-static void GetItemHandler(const picojson::value& args,
- picojson::object* out);
-static void LengthHandler(const picojson::value& args,
- picojson::object* out);
-static void ClearHandler(const picojson::value& args,
- picojson::object* out);
-static void SetItemHandler(const picojson::value& args,
- picojson::object* out);
-static void RemoveHandler(const picojson::value& args,
- picojson::object* out);
-
-std::map<std::string, CmdHandler> g_handler = {
- {"init", InitHandler},
- {"key", KeyHandler},
- {"length", LengthHandler},
- {"clear", ClearHandler},
- {"getItem", GetItemHandler},
- {"setItem", SetItemHandler},
- {"removeItem", RemoveHandler},
-};
-
-
-static void HandleMessage(XW_Instance instance,
- const char* message,
- bool sync);
-
extern "C" int32_t XW_Initialize(XW_Extension extension,
XW_GetInterface get_interface) {
g_xw_extension = extension;
return XW_ERROR;
}
- std::vector<char> res(256, 0);
- g_runtime->GetRuntimeVariableString(extension, "app_id", &res[0], 256);
- g_appid = std::string(res.begin(), res.end());
- if (g_appid.at(0) == '"') {
- g_appid = g_appid.substr(1, strlen(g_appid.c_str())-2);
- }
-
- g_core->RegisterInstanceCallbacks(
- g_xw_extension,
- [](XW_Instance /*instance*/){
- if (g_appdata.get() == NULL) {
- g_appdata.reset(new wrt::ApplicationData(g_appid));
- g_appdata->LoadManifestData();
- }
- wrt::Widget::GetInstance()->Initialize(g_appdata.get());
- },
- NULL);
-
- g_messaging->Register(g_xw_extension, [](XW_Instance instance,
- const char* message) {
- HandleMessage(instance, message, false);
- });
- g_sync_messaging->Register(g_xw_extension, [](XW_Instance instance,
- const char* message) {
- HandleMessage(instance, message, true);
- });
-
g_core->SetExtensionName(g_xw_extension, "Widget");
const char* entry_points[] = {"widget", NULL};
g_entry_points->SetExtraJSEntryPoints(g_xw_extension, entry_points);
return XW_OK;
}
-static void InitHandler(const picojson::value& /*args*/,
- picojson::object* out) {
- picojson::value result = picojson::value(picojson::object());
- picojson::object& obj = result.get<picojson::object>();
-
- auto widget_info = g_appdata->widget_info();
- if (widget_info.get() == NULL) {
- out->insert(std::make_pair("status", picojson::value("error")));
- return;
- }
- out->insert(std::make_pair("status", picojson::value("success")));
-
- wrt::LocaleManager locale_manager;
- if (!widget_info->default_locale().empty()) {
- locale_manager.SetDefaultLocale(widget_info->default_locale());
- }
-
- // TODO(sngn.lee): should be returned localized string
- obj["author"] = picojson::value(widget_info->author());
- obj["description"] = picojson::value(
- locale_manager.GetLocalizedString(widget_info->description_set()));
- obj["name"] = picojson::value(
- locale_manager.GetLocalizedString(widget_info->name_set()));
- obj["shortName"] = picojson::value(
- locale_manager.GetLocalizedString(widget_info->short_name_set()));
- obj["version"] = picojson::value(widget_info->version());
- obj["id"] = picojson::value(widget_info->id());
- obj["authorEmail"] = picojson::value(widget_info->author_email());
- obj["authorHref"] = picojson::value(widget_info->author_href());
- obj["height"] = picojson::value(static_cast<double>(widget_info->height()));
- obj["width"] = picojson::value(static_cast<double>(widget_info->width()));
-
- out->insert(std::make_pair("result", result));
-}
-
-static void KeyHandler(const picojson::value& args, picojson::object* out) {
- int idx = static_cast<int>(args.get("idx").get<double>());
- std::string key;
- if (!wrt::Widget::GetInstance()->Key(idx, &key)) {
- out->insert(std::make_pair("status", picojson::value("error")));
- return;
- }
- out->insert(std::make_pair("status", picojson::value("success")));
- out->insert(std::make_pair("result", picojson::value(key)));
-}
-
-static void GetItemHandler(const picojson::value& args,
- picojson::object* out) {
- const std::string& key = args.get("key").get<std::string>();
- std::string value;
- if (!wrt::Widget::GetInstance()->GetItem(key, &value)) {
- out->insert(std::make_pair("status", picojson::value("error")));
- return;
- }
- out->insert(std::make_pair("status", picojson::value("success")));
- out->insert(std::make_pair("result", picojson::value(value)));
-}
-
-static void LengthHandler(const picojson::value& /*args*/,
- picojson::object* out) {
- int length = wrt::Widget::GetInstance()->Length();
- out->insert(std::make_pair("status", picojson::value("success")));
- out->insert(
- std::make_pair("result", picojson::value(static_cast<double>(length))));
-}
-
-static void ClearHandler(const picojson::value& /*args*/,
- picojson::object* out) {
- wrt::Widget::GetInstance()->Clear();
- out->insert(std::make_pair("status", picojson::value("success")));
-}
-
-static void SetItemHandler(const picojson::value& args,
- picojson::object* out) {
- const std::string& key = args.get("key").get<std::string>();
- const std::string& value = args.get("value").get<std::string>();
- std::string oldvalue;
- if (wrt::Widget::GetInstance()->GetItem(key, &oldvalue)) {
- out->insert(std::make_pair("result", picojson::value(oldvalue)));
- } else {
- out->insert(std::make_pair("result", picojson::value()));
- }
- wrt::Widget::GetInstance()->SetItem(key, value);
- out->insert(std::make_pair("status", picojson::value("success")));
-}
-
-static void RemoveHandler(const picojson::value& args,
- picojson::object* out) {
- const std::string& key = args.get("key").get<std::string>();
- std::string oldvalue;
- if (wrt::Widget::GetInstance()->GetItem(key, &oldvalue)) {
- out->insert(std::make_pair("result", picojson::value(oldvalue)));
- } else {
- out->insert(std::make_pair("result", picojson::value()));
- }
- wrt::Widget::GetInstance()->RemoveItem(key);
- out->insert(std::make_pair("status", picojson::value("success")));
-}
-
-
-static void HandleMessage(XW_Instance instance,
- const char* message,
- bool sync) {
- picojson::value value;
- std::string err;
- picojson::parse(value, message, message + strlen(message), &err);
- if (!err.empty()) {
- LOGGER(ERROR) << "Ignoring message. " << err;
- return;
- }
-
- if (!value.is<picojson::object>()) {
- LOGGER(ERROR) << "Ignoring message. It is not an object.";
- return;
- }
-
- std::string cmd = value.get("cmd").to_str();
- // check for args in JSON message
- const picojson::value& args = value.get("args");
- picojson::value result = picojson::value(picojson::object());
-
- auto handler = g_handler.find(cmd);
- if (handler != g_handler.end()) {
- handler->second(args, &result.get<picojson::object>());
- } else {
- result.get<picojson::object>().insert(
- std::make_pair("status", picojson::value("error")));
- }
-
- if (sync) {
- g_sync_messaging->SetSyncReply(instance, result.serialize().c_str());
- }
-}
-
-namespace wrt {
-
-namespace {
- const char* kDbInitedCheckKey = "__WRT_DB_INITED__";
- const char* kDBPublicSection = "public";
- const char* kDBPrivateSection = "private";
-} // namespace
-
-
-Widget* Widget::GetInstance() {
- static Widget instance;
- return &instance;
-}
-
-Widget::Widget() {
-}
-Widget::~Widget() {
-}
-
-void Widget::Initialize(const ApplicationData* appdata) {
- AppDB* db = AppDB::GetInstance();
- if (db->HasKey(kDBPrivateSection, kDbInitedCheckKey))
- return;
- if (appdata->widget_info() == NULL)
- return;
-
- auto preferences = appdata->widget_info()->preferences();
- auto it = preferences.begin();
- for ( ; it != preferences.end(); ++it) {
- db->Set(kDBPublicSection,
- (*it)->Name(),
- (*it)->Value());
- }
- db->Set(kDBPrivateSection, kDbInitedCheckKey, "true");
-}
-
-int Widget::Length() {
- AppDB* db = AppDB::GetInstance();
- std::list<std::string> list;
- db->GetKeys(kDBPublicSection, &list);
- return list.size();
-}
-
-bool Widget::Key(int idx, std::string* key) {
- AppDB* db = AppDB::GetInstance();
- std::list<std::string> list;
- db->GetKeys(kDBPublicSection, &list);
-
- auto it = list.begin();
- for ( ; it != list.end() && idx >= 0; ++it) {
- if (idx == 0) {
- *key = *it;
- return true;
- }
- idx--;
- }
- return false;
-}
-
-bool Widget::GetItem(const std::string& key, std::string* value) {
- AppDB* db = AppDB::GetInstance();
- if (!db->HasKey(kDBPublicSection, key))
- return false;
- *value = db->Get(kDBPublicSection, key);
- return true;
-}
-
-bool Widget::SetItem(const std::string& key, const std::string& value) {
- AppDB* db = AppDB::GetInstance();
- db->Set(kDBPublicSection, key, value);
- return true;
-}
-
-bool Widget::RemoveItem(const std::string& key) {
- AppDB* db = AppDB::GetInstance();
- if (!db->HasKey(kDBPublicSection, key))
- return false;
- db->Remove(kDBPublicSection, key);
- return true;
-}
-
-void Widget::Clear() {
- AppDB* db = AppDB::GetInstance();
- std::list<std::string> list;
- db->GetKeys(kDBPublicSection, &list);
- auto it = list.begin();
- for ( ; it != list.end(); ++it) {
- db->Remove(kDBPublicSection, *it);
- }
-}
-
-} // namespace wrt
+++ /dev/null
-/*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef WRT_EXTENSION_WIDGET_H_
-#define WRT_EXTENSION_WIDGET_H_
-
-#include <string>
-
-namespace wrt {
-class ApplicationData;
-class Widget {
- public:
- static Widget* GetInstance();
- void Initialize(const ApplicationData* appdata);
- int Length();
- bool Key(int idx, std::string* key);
- bool GetItem(const std::string& key, std::string* value);
- bool SetItem(const std::string& key, const std::string& value);
- bool RemoveItem(const std::string& key);
- void Clear();
- private:
- Widget();
- virtual ~Widget();
-};
-} // namespace wrt
-
-#endif // WRT_EXTENSION_WIDGET_H_
-var sendSyncMessage = function(msg) {
- return JSON.parse(extension.internal.sendSyncMessage(JSON.stringify(msg)));
-};
-
-
-// test
-/*
-var backend_keys = [];
-var backend_value = {};
-
-var sendSyncMessage = function(msg) {
- if (msg['cmd'] == 'init') {
- return {'status':'ok', result:{'author':'lee seung keun', 'description':'.....test','name':'test app...'}};
- }
- if (msg['cmd'] == 'length') {
- return {'status':'ok', result:backend_keys.length};
- }
- if (msg['cmd'] == 'key') {
- var result = backend_keys[msg['args']['idx']] || null;
- var status = result ? "ok" : "error";
- return {'status':status, 'result':result};
- }
- if (msg['cmd'] == 'getItem') {
- var result = backend_value[msg['args']['key']] || null;
- var status = result ? "ok" : "error";
- return {'status':status, 'result':result};
- }
- if (msg['cmd'] == 'setItem') {
- if (backend_keys.lastIndexOf(msg['args']['key']) == -1)
- backend_keys.push(msg['args']['key']);
- var old = backend_value[msg['args']['key']] || null;
- backend_value[msg['args']['key']] = msg['args']['value'];
- return {'status':'ok', 'result':old};
- }
- if (msg['cmd'] == 'removeItem') {
- if (backend_keys.lastIndexOf(msg['args']['key']) == -1)
- return {'status':'error'};
- var old = backend_value[msg['args']['key']] || null;
- delete backend_value[msg['args']['key']];
- return {'status':'ok', 'result':old};
- }
- if (msg['cmd'] == 'clear') {
- backend_keys = [];
- backend_value = {};
- return {'status':'ok'};
- }
-};
-*/
-
var dispatchStorageEvent = function(key, oldValue, newValue) {
var evt = new CustomEvent("Storage");
evt.key = key;
}
};
-var widget_info_ = sendSyncMessage({cmd:"init"})['result'];
-
-function Preference() {
- Object.defineProperty(this, 'length', {
- get : function() {
- return sendSyncMessage({'cmd':'length'})['result'];
- }
- });
-};
-
-Preference.prototype.key = function(idx) {
- var req = {'cmd':'key'};
- req['args'] = {'idx':idx};
-
- var ret = sendSyncMessage(req);
- if (ret['status'] == 'error') {
- return null;
- }
- return ret['result'];
-};
-
-Preference.prototype.getItem = function(key) {
- var req = {'cmd':'getItem'};
- req['args'] = {'key':key};
-
- var ret = sendSyncMessage(req);
- if (ret['status'] == 'error') {
- return null;
- }
- return ret['result'];
-};
-
-Preference.prototype.clear = function() {
- var req = {'cmd':'clear'};
- sendSyncMessage(req);
- dispatchStorageEvent(null,null,null);
-}
-
-Preference.prototype.setItem = function(key, value) {
- var req = {'cmd':'setItem'};
- req['args'] = {'key':key,'value':value};
- var ret = sendSyncMessage(req);
- if (ret['status'] == 'error') {
- throw {code:22, name:'QuotaExceededError', message: "can't insert Item"};
- }
- var oldValue = ret['result'];
- dispatchStorageEvent(key,oldValue,value);
-};
-
-Preference.prototype.removeItem = function(key) {
- var req = {'cmd':'removeItem'};
- req['args'] = {'key':key};
- var ret = sendSyncMessage(req);
- var oldValue = ret['result'];
- if (ret['status'] == 'success') {
- dispatchStorageEvent(key,oldValue,null);
- }
-};
-
+var widget_info_ = requireNative('WidgetModule');
+var preference_ = widget_info_['preference'];
+preference_.__onChanged_WRT__ = dispatchStorageEvent;
function Widget() {
Object.defineProperties(this, {
writable: false
},
"preferences": {
- value: new Preference(),
+ value: preference_,
writable: false
}
});