From: Seungkeun Lee Date: Mon, 17 Aug 2015 04:14:49 +0000 (+0900) Subject: Implements plugin lazy loading X-Git-Tag: submit/tizen/20150921.093222~18 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0af19d0a072be8d5673bc2c630a7e8cc6e0f7504;p=platform%2Fframework%2Fweb%2Fcrosswalk-tizen.git Implements plugin lazy loading Change-Id: I713582d2e84147e0940176e7db658e785be96bcd --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f10e25b5..6265709c1 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ INCLUDE(FindPkgConfig) # Options OPTION(X11_SUPPORT "Support x11 for window manager" OFF) OPTION(WAYLAND_SUPPORT "Support wayland for window manager" OFF) +OPTION(PLUGIN_LAZY_LOADING "Support plugins lazy loading" ON) # Compiler flags IF(NOT CMAKE_BUILD_TYPE) @@ -50,6 +51,10 @@ IF(WAYLAND_SUPPORT) ADD_DEFINITIONS("-DHAVE_WAYLAND") ENDIF(WAYLAND_SUPPORT) +IF(PLUGIN_LAZY_LOADING) + ADD_DEFINITIONS("-DPLUGIN_LAZY_LOADING") +ENDIF(PLUGIN_LAZY_LOADING) + IF(NOT EXTENSION_PATH) SET(EXTENSION_PATH ${LIB_INSTALL_DIR}/tizen-extensions-crosswalk) ENDIF(NOT EXTENSION_PATH) diff --git a/src/bundle/extension_client.cc b/src/bundle/extension_client.cc index 497286f75..6808cf92c 100755 --- a/src/bundle/extension_client.cc +++ b/src/bundle/extension_client.cc @@ -175,4 +175,27 @@ void ExtensionClient::HandleSignal( } } +std::string ExtensionClient::GetExtensionJavascriptAPICode( + const std::string& name) { + + auto extension_code_point = extension_apis_.find(name); + if (extension_code_point == extension_apis_.end()) + return std::string(); + + ExtensionCodePoints* code_points = extension_code_point->second; + if (!code_points->api.empty()) { + return code_points->api; + } + + GVariant* value = dbus_extension_client_.Call( + kDBusInterfaceNameForExtension, kMethodGetJavascriptCode, + g_variant_new("(s)", name.c_str()), + G_VARIANT_TYPE("(s)")); + gchar* api; + g_variant_get(value, "(&s)", &api); + code_points->api = std::string(api); + g_variant_unref(value); + return code_points->api; +} + } // namespace wrt diff --git a/src/bundle/extension_client.h b/src/bundle/extension_client.h index cde88af09..e5fb306f7 100755 --- a/src/bundle/extension_client.h +++ b/src/bundle/extension_client.h @@ -45,6 +45,7 @@ class ExtensionClient { typedef std::map ExtensionAPIMap; const ExtensionAPIMap& extension_apis() const { return extension_apis_; } + std::string GetExtensionJavascriptAPICode(const std::string& name); private: void HandleSignal(const std::string& signal_name, GVariant* parameters); diff --git a/src/bundle/extension_module.cc b/src/bundle/extension_module.cc index 951239273..ced94a78e 100755 --- a/src/bundle/extension_module.cc +++ b/src/bundle/extension_module.cc @@ -272,6 +272,9 @@ void ExtensionModule::LoadExtensionCode( instance_id_ = client_->CreateInstance(extension_name_, this); std::string exception; + if (extension_code_.empty()) { + extension_code_ = client_->GetExtensionJavascriptAPICode(extension_name_); + } std::string wrapped_api_code = WrapAPICode(extension_code_, extension_name_); v8::Handle result = RunString(wrapped_api_code, &exception); diff --git a/src/bundle/extension_renderer_controller.cc b/src/bundle/extension_renderer_controller.cc index 10d1959ad..99a84cd1e 100755 --- a/src/bundle/extension_renderer_controller.cc +++ b/src/bundle/extension_renderer_controller.cc @@ -42,11 +42,12 @@ void CreateExtensionModules(ExtensionClient* client, auto it = extensions.begin(); for (; it != extensions.end(); ++it) { ExtensionClient::ExtensionCodePoints* codepoint = it->second; - if (codepoint->api.empty()) - continue; std::unique_ptr module( - new ExtensionModule(client, module_system, it->first, codepoint->api)); + new ExtensionModule(client, + module_system, + it->first, + codepoint->api)); module_system->RegisterExtensionModule( std::move(module), codepoint->entry_points); } diff --git a/src/common/constants.cc b/src/common/constants.cc index 9e6b3cffe..c3a9ed178 100755 --- a/src/common/constants.cc +++ b/src/common/constants.cc @@ -36,7 +36,7 @@ const char kMethodDestroyInstance[] = "DestroyInstance"; const char kMethodSendSyncMessage[] = "SendSyncMessage"; const char kMethodPostMessage[] = "PostMessage"; const char kSignalOnMessageToJS[] = "OnMessageToJS"; - +const char kMethodGetJavascriptCode[] = "GetJavascriptCode"; diff --git a/src/common/constants.h b/src/common/constants.h index aa3c695b1..4535d5bd2 100755 --- a/src/common/constants.h +++ b/src/common/constants.h @@ -34,6 +34,7 @@ extern const char kMethodDestroyInstance[]; extern const char kMethodSendSyncMessage[]; extern const char kMethodPostMessage[]; extern const char kSignalOnMessageToJS[]; +extern const char kMethodGetJavascriptCode[]; } // namespace wrt diff --git a/src/common/picojson.h b/src/common/picojson.h new file mode 100644 index 000000000..9f983197a --- /dev/null +++ b/src/common/picojson.h @@ -0,0 +1,1037 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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 array; + typedef std::map 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 bool is() const; + template const T& get() const; + template 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 void serialize(Iter os) const; + std::string serialize() const; + private: + template 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() 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() const { \ + assert("type mismatch! call vis() before get()" \ + && is()); \ + return var; \ + } \ + template <> inline ctype& value::get() { \ + assert("type mismatch! call is() before get()" \ + && is()); \ + 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()); + 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::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()); + return idx < u_.array_->size(); + } + + inline bool value::contains(const std::string& key) const { + assert(is()); + 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 void copy(const std::string& s, Iter oi) { + std::copy(s.begin(), s.end(), oi); + } + + template 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 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 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 inline int _parse_quadhex(input &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 inline bool _parse_codepoint(String& out, input& 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 inline bool _parse_string(String& out, input& 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 inline bool _parse_array(Context& ctx, input& 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 inline bool _parse_object(Context& ctx, input& 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 inline bool _parse_number(double& out, input& 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 inline bool _parse(Context& ctx, input& 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 bool parse_string(input&) { return false; } + bool parse_array_start() { return false; } + template bool parse_array_item(input&, size_t) { + return false; + } + bool parse_object_start() { return false; } + template bool parse_object_item(input&, 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 bool parse_string(input& in) { + *out_ = value(string_type, false); + return _parse_string(out_->get(), in); + } + bool parse_array_start() { + *out_ = value(array_type, false); + return true; + } + template bool parse_array_item(input& in, size_t) { + array& a = out_->get(); + 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 bool parse_object_item(input& in, const std::string& key) { + object& o = out_->get(); + 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 bool parse_string(input& in) { + dummy_str s; + return _parse_string(s, in); + } + bool parse_array_start() { return true; } + template bool parse_array_item(input& in, size_t) { + return _parse(*this, in); + } + bool parse_object_start() { return true; } + template bool parse_object_item(input& 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 inline std::string parse(value& out, Iter& pos, const Iter& last) { + std::string err; + pos = parse(out, pos, last, &err); + return err; + } + + template inline Iter _parse(Context& ctx, const Iter& first, const Iter& last, std::string* err) { + input 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 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(is.rdbuf()), + std::istreambuf_iterator(), &err); + return err; + } + + template struct last_error_t { + static std::string s; + }; + template std::string last_error_t::s; + + inline void set_last_error(const std::string& s) { + last_error_t::s = s; + } + + inline const std::string& get_last_error() { + return last_error_t::s; + } + + inline bool operator==(const value& x, const value& y) { + if (x.is()) + return y.is(); +#define PICOJSON_CMP(type) \ + if (x.is()) \ + return y.is() && x.get() == y.get() + 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(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 void is(const T& x, const T& y, const char* name = "") +{ + if (x == y) { + ok(true, name); + } else { + ok(false, name); + } +} + +#include +#include +#include +#include + +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(); + 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(), in " check type"); \ + is(v.get(), 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(), "empty " #type " check type"); \ + ok(v.get().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(), "array check type"); + is(v.get().size(), size_t(3), "check array size"); + ok(v.contains(0), "check contains array[0]"); + ok(v.get(0).is(), "check array[0] type"); + is(v.get(0).get(), 1.0, "check array[0] value"); + ok(v.contains(1), "check contains array[1]"); + ok(v.get(1).is(), "check array[1] type"); + ok(v.get(1).get(), "check array[1] value"); + ok(v.contains(2), "check contains array[2]"); + ok(v.get(2).is(), "check array[2] type"); + is(v.get(2).get(), 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(), "object check type"); + is(v.get().size(), size_t(1), "check object size"); + ok(v.contains("a"), "check contains property"); + ok(v.get("a").is(), "check bool property exists"); + is(v.get("a").get(), 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(); + o.erase("b"); + picojson::array& a = o["a"].get(); + 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(), "swap (null)"); + ok(v2.get() == true, "swap (bool)"); + + v1 = picojson::value("a"); + v2 = picojson::value(1.0); + swap(v1, v2); + ok(v1.get() == 1.0, "swap (dobule)"); + ok(v2.get() == "a", "swap (string)"); + + v1 = picojson::value(picojson::object()); + v2 = picojson::value(picojson::array()); + swap(v1, v2); + ok(v1.is(), "swap (array)"); + ok(v2.is(), "swap (object)"); + } + + return success ? 0 : 1; +} + +#endif diff --git a/src/extension/extension.cc b/src/extension/extension.cc index b12fb0f07..2fbabb8f5 100755 --- a/src/extension/extension.cc +++ b/src/extension/extension.cc @@ -30,6 +30,26 @@ Extension::Extension(const std::string& path, ExtensionDelegate* delegate) library_path_(path), xw_extension_(0), use_trampoline_(true), + lazy_loading_(false), + delegate_(delegate), + created_instance_callback_(NULL), + destroyed_instance_callback_(NULL), + shutdown_callback_(NULL), + handle_msg_callback_(NULL), + handle_sync_msg_callback_(NULL) { +} + +Extension::Extension(const std::string& path, + const std::string& name, + const StringVector& entry_points, + ExtensionDelegate* delegate) + : initialized_(false), + library_path_(path), + xw_extension_(0), + name_(name), + entry_points_(entry_points), + use_trampoline_(true), + lazy_loading_(true), delegate_(delegate), created_instance_callback_(NULL), destroyed_instance_callback_(NULL), @@ -84,6 +104,7 @@ bool Extension::Initialize() { } ExtensionInstance* Extension::CreateInstance() { + Initialize(); ExtensionAdapter* adapter = ExtensionAdapter::GetInstance(); XW_Instance xw_instance = adapter->GetNextXWInstance(); return new ExtensionInstance(this, xw_instance); diff --git a/src/extension/extension.h b/src/extension/extension.h index d5464d959..58eeddc4a 100755 --- a/src/extension/extension.h +++ b/src/extension/extension.h @@ -40,6 +40,10 @@ class Extension { }; Extension(const std::string& path, ExtensionDelegate* delegate); + Extension(const std::string& path, + const std::string& name, + const StringVector& entry_points, + ExtensionDelegate* delegate); virtual ~Extension(); bool Initialize(); @@ -57,6 +61,10 @@ class Extension { return use_trampoline_; } + bool lazy_loading() const { + return lazy_loading_; + } + void set_name(const std::string& name) { name_ = name; } @@ -85,6 +93,7 @@ class Extension { std::string javascript_api_; StringVector entry_points_; bool use_trampoline_; + bool lazy_loading_; ExtensionDelegate* delegate_; diff --git a/src/extension/extension_server.cc b/src/extension/extension_server.cc index bf834fc5e..578804411 100755 --- a/src/extension/extension_server.cc +++ b/src/extension/extension_server.cc @@ -22,12 +22,14 @@ #include #include +#include #include "common/logger.h" #include "common/constants.h" #include "common/file_utils.h" #include "common/string_utils.h" #include "common/command_line.h" +#include "common/picojson.h" #include "extension/extension.h" namespace wrt { @@ -36,6 +38,7 @@ namespace { const char kExtensionPrefix[] = "lib"; const char kExtensionSuffix[] = ".so"; +const char kExtensionMetadata[] = "plugins.json"; const char kDBusIntrospectionXML[] = "" @@ -43,6 +46,10 @@ const char kDBusIntrospectionXML[] = " " " " " " + " " + " " + " " + " " " " " " " " @@ -67,6 +74,17 @@ const char kDBusIntrospectionXML[] = " " ""; +void LoadFrequentlyUsedModules(std::map& modules) { + auto it = modules.find("tizen"); + if (it != modules.end()) { + it->second->Initialize(); + } + it = modules.find("xwalk.utils"); + if (it != modules.end()) { + it->second->Initialize(); + } +} + } // namespace ExtensionServer::ExtensionServer(const std::string& uuid) @@ -89,7 +107,11 @@ bool ExtensionServer::Start(const StringVector& paths) { } // Register system extensions to support Tizen Device APIs +#ifdef PLUGIN_LAZY_LOADING + RegisterSystemExtensionsByMetadata(); +#else RegisterSystemExtensions(); +#endif // Register user extensions for (auto it = paths.begin(); it != paths.end(); ++it) { @@ -112,6 +134,10 @@ bool ExtensionServer::Start(const StringVector& paths) { // Send 'ready' signal to Injected Bundle. NotifyEPCreatedToApplication(); +#ifdef PLUGIN_LAZY_LOADING + LoadFrequentlyUsedModules(extensions_); +#endif + return true; } @@ -125,6 +151,21 @@ void ExtensionServer::RegisterExtension(const std::string& path) { LOGGER(DEBUG) << ext->name() << " is registered."; } +void ExtensionServer::RegisterExtension(Extension* extension) { + if (!extension->lazy_loading() && !extension->Initialize()) { + delete extension; + return; + } + + if (!RegisterSymbols(extension)) { + delete extension; + return; + } + + extensions_[extension->name()] = extension; + LOGGER(DEBUG) << extension->name() << " is registered."; +} + void ExtensionServer::RegisterSystemExtensions() { #ifdef EXTENSION_PATH std::string extension_path(EXTENSION_PATH); @@ -143,6 +184,52 @@ void ExtensionServer::RegisterSystemExtensions() { } } +void ExtensionServer::RegisterSystemExtensionsByMetadata() { +#ifdef EXTENSION_PATH + std::string extension_path(EXTENSION_PATH); +#else + #error EXTENSION_PATH is not set. +#endif + + extension_path.append("/"); + extension_path.append(kExtensionMetadata); + std::ifstream metafile(extension_path.c_str()); + if (!metafile.is_open()) { + LOGGER(ERROR) << "Fail to open the plugin metadata file(plugins.json)"; + LOGGER(ERROR) << "Fallback - load plugin without lazy loading"; + RegisterSystemExtensions(); + return; + } + + picojson::value metadata; + metafile >> metadata; + if (metadata.is()) { + auto& plugins = metadata.get(); + for (auto plugin = plugins.begin(); plugin != plugins.end(); ++plugin) { + if (!plugin->is()) + continue; + + std::string name = plugin->get("name").to_str(); + std::string lib = plugin->get("lib").to_str(); + std::vector entries; + auto& entry_points_value = plugin->get("entry_points"); + if (entry_points_value.is()) { + auto& entry_points = entry_points_value.get(); + for (auto entry = entry_points.begin(); entry != entry_points.end(); + ++entry) { + entries.push_back(entry->to_str()); + } + } + Extension* extension = new Extension(lib, name, entries, this); + RegisterExtension(extension); + } + } else { + SLOGE("%s is not plugin metadata", extension_path.c_str()); + } + metafile.close(); +} + + bool ExtensionServer::RegisterSymbols(Extension* extension) { std::string name = extension->name(); @@ -220,6 +307,10 @@ void ExtensionServer::HandleDBusMethod(GDBusConnection* connection, gchar* msg; g_variant_get(parameters, "(&s&s)", &instance_id, &msg); OnPostMessage(instance_id, msg); + } else if (method_name == kMethodGetJavascriptCode) { + gchar* extension_name; + g_variant_get(parameters, "(&s)", &extension_name); + OnGetJavascriptCode(connection, extension_name, invocation); } } @@ -236,6 +327,7 @@ void ExtensionServer::OnGetExtensions(GDBusMethodInvocation* invocation) { g_variant_builder_open(&builder, G_VARIANT_TYPE("(ssas)")); g_variant_builder_add(&builder, "s", ext->name().c_str()); g_variant_builder_add(&builder, "s", ext->javascript_api().c_str()); + // open container for entry_point g_variant_builder_open(&builder, G_VARIANT_TYPE("as")); auto it_entry = ext->entry_points().begin(); @@ -355,6 +447,24 @@ void ExtensionServer::OnPostMessage( instance->HandleMessage(msg); } +void ExtensionServer::OnGetJavascriptCode(GDBusConnection* connection, + const std::string& extension_name, + GDBusMethodInvocation* invocation) { + // find extension with given the extension name + auto it = extensions_.find(extension_name); + if (it == extensions_.end()) { + LOGGER(ERROR) << "Failed to find extension '" << extension_name << "'"; + g_dbus_method_invocation_return_error( + invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, + "Not found extension %s", extension_name.c_str()); + return; + } + + it->second->Initialize(); + g_dbus_method_invocation_return_value( + invocation, g_variant_new("(s)", it->second->javascript_api().c_str())); +} + void ExtensionServer::SyncReplyCallback( const std::string& reply, GDBusMethodInvocation* invocation) { g_dbus_method_invocation_return_value( diff --git a/src/extension/extension_server.h b/src/extension/extension_server.h index 2eef57aba..dc0ecbf1f 100755 --- a/src/extension/extension_server.h +++ b/src/extension/extension_server.h @@ -44,7 +44,9 @@ class ExtensionServer : public Extension::ExtensionDelegate { private: void RegisterExtension(const std::string& path); + void RegisterExtension(Extension* extension); void RegisterSystemExtensions(); + void RegisterSystemExtensionsByMetadata(); bool RegisterSymbols(Extension* extension); void GetRuntimeVariable(const char* key, char* value, size_t value_len); @@ -74,6 +76,9 @@ class ExtensionServer : public Extension::ExtensionDelegate { void PostMessageToJSCallback(GDBusConnection* connection, const std::string& instance_id, const std::string& msg); + void OnGetJavascriptCode(GDBusConnection* connection, + const std::string& extension_name, + GDBusMethodInvocation* invocation); std::string app_uuid_; DBusServer dbus_server_;