2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Grzegorz Krawczyk (g.krawczyk@samgsung.com)
19 * @author Yunchan Cho (yunchan.cho@samgsung.com)
25 #include <dpl/foreach.h>
26 #include <dpl/log/secure_log.h>
28 #include "plugin_property_support.h"
31 #include <js_overlay_types.h>
34 //const char* SEPARATOR = ".";
35 const std::string GLOBAL_OBJECT_NAME = "GLOBAL_OBJECT";
39 using namespace PluginModule;
40 using namespace WrtPlugins::W3C;
42 Explorer::Explorer(JSGlobalContextRef context) :
46 _W("Context is NULL");
51 JavaScriptInterfaceSingleton::Instance().getGlobalObject(context);
53 //register main frame;
54 m_iframeSupport.registerIframe(m_globalObject);
56 m_propertySupport.reset(new WindowPropertySupport(m_context));
63 JSObjectPtr Explorer::getJSObjectProperty(const std::string& propertyName,
66 return JavaScriptInterfaceSingleton::Instance().getJSObjectProperty(
67 m_context, object, propertyName);
70 JSObjectPtr Explorer::getProperty(
71 const string& requestedProperty,
72 JSObjectPtr providedObject)
74 _D("Requested Property: %s", requestedProperty.c_str());
76 if (!providedObject) {
77 _W("Provided object is empty");
81 std::string currentObjectName;
82 std::string currentPropertyRequested = requestedProperty;
83 JSObjectPtr currentObject = providedObject;
86 auto pos = currentPropertyRequested.find(".");
87 if (pos == string::npos) {
88 return getJSObjectProperty(currentPropertyRequested, currentObject);
91 //check if requested object has child
92 currentObjectName = currentPropertyRequested.substr(0, pos);
94 if (currentPropertyRequested.size() <= pos + 1) {
95 _W("Wrong name of requested property");
98 currentPropertyRequested = currentPropertyRequested.substr(pos + 1);
100 currentObject = getJSObjectProperty(currentObjectName, currentObject);
101 if (!currentObject) {
102 _W("Failed to get property: %s", currentObjectName.c_str());
103 return JSObjectPtr();
108 bool Explorer::registerObject(const JSObjectDeclarationPtr& declaration,
109 JSGlobalContextRef context)
113 if (declaration->getParentName() == GLOBAL_OBJECT_NAME) {
114 retValue = register_(declaration, m_globalObject, context);
115 } else { //PIM support
116 //it should be master object name
117 string masterName = declaration->getParentName();
118 auto pos = masterName.find(".");
119 if (string::npos != pos) {
120 _W("ParentName not allowed");
123 auto masterObject = getJSObjectProperty(masterName, m_globalObject);
124 if (masterObject->getObject() == 0) {
127 retValue = registerObject(declaration,
133 if (declaration->checkIframesSupported()) {
134 m_iframeSupport.registerDeclaration(declaration);
135 // m_iframeSupport.invokeCallbackForMainFrame(m_context,
143 bool Explorer::registerObject(const JSObjectDeclarationPtr& declaration,
144 const std::string& providedObjectName,
145 JSObjectPtr providedObject,
146 JSGlobalContextRef context)
148 _D("%s <- %s", providedObjectName.c_str(), declaration->getName().c_str());
149 std::string fullParentName = declaration->getParentName();
151 if (fullParentName == providedObjectName) {
152 \r_D("Provided object was matched!!");
153 return register_(declaration, providedObject, context);
156 //check if object exists in fullParentName
157 auto pos = fullParentName.find(providedObjectName);
158 if (pos == string::npos) {
159 _W("Object: %s should be child of: %s but you provided object: %s",
160 declaration->getName().c_str(),
161 declaration->getParentName().c_str(),
162 providedObjectName.c_str());
166 if (fullParentName.size() <= pos + providedObjectName.size() + 1) {
167 _W("Invalid object name");
171 auto currentRequesteProperty =
172 fullParentName.substr(pos + providedObjectName.size() + 1);
174 JSObjectPtr jsObject = getProperty(
175 currentRequesteProperty, providedObject);
177 if (!jsObject->getObject()) {
178 _W("Object: %s should be child of: %s. you provided object: %s but object is null",
179 declaration->getName().c_str(),
180 declaration->getParentName().c_str(),
181 providedObjectName.c_str());
185 return register_(declaration, jsObject, context);
188 bool Explorer::register_(const JSObjectDeclarationPtr& declaration,
190 JSGlobalContextRef context)
192 _D("Registration object: %s <- %s",
193 declaration->getParentName().c_str(),
194 declaration->getName().c_str());
195 Assert(parent && "parent object is NULL");
197 typedef JSObjectDeclaration::Options JO;
199 JSGlobalContextRef gContext = (context == NULL) ? m_context : context;
200 JSObjectPtr objectInstance = JavaScriptInterfaceSingleton::Instance().
201 createObject(gContext, declaration);
203 JavaScriptInterfaceSingleton::Instance().setObjectProperty(
206 declaration->getName(),
210 if (declaration->getOptions() &&
211 (declaration->getOptions()->getIframeNotice() ==
212 JO::IFrameNotice::AlwaysNotice))
214 declaration->getOptions()->invokeCallback(gContext,
216 objectInstance->getObject());
222 void Explorer::registerObjectIntoIframe(
223 const JSObjectDeclarationPtr& declaration,
224 JSObjectPtr frameObject,
225 JSGlobalContextRef context)
227 if (declaration->getParentName() == GLOBAL_OBJECT_NAME) {
228 _D("Connect to Global object of IFRAME");
229 register_(declaration, frameObject, context);
235 _D("Connect to NOT global object of IFRAME");
236 //it should be master object name
237 string masterName = declaration->getParentName();
238 auto pos = masterName.find(".");
239 if (string::npos != pos) {
240 _W("ParentName not allowed");
243 auto masterObject = getJSObjectProperty(masterName, frameObject);
244 if (!masterObject->getObject()) {
245 _W("Object not exist in frame");
248 register_(declaration, masterObject, context);
252 void Explorer::loadFrame(JSGlobalContextRef context)
254 JSObjectPtr frameObject =
255 JavaScriptInterfaceSingleton::Instance().getGlobalObject(context);
257 if (frameObject->getObject() == m_globalObject->getObject()) {
258 // Main page was already loaded from constructor
259 _W("Main page loaded");
263 m_iframeSupport.registerIframe(frameObject);
265 auto iframeObjects = m_iframeSupport.getIframeObjects();
267 FOREACH(object, iframeObjects)
269 _D("Register object: %s", (*object)->getName().c_str());
270 registerObjectIntoIframe(*object, frameObject, context);
274 void Explorer::unloadFrame(JSGlobalContextRef context)
276 JSObjectPtr frameObject =
277 JavaScriptInterfaceSingleton::Instance().getGlobalObject(context);
278 m_iframeSupport.unregisterIframe(frameObject);
281 void Explorer::removePluginsFromIframes()
283 if (m_iframeSupport.hasIframes()) {
284 JavaScriptInterfaceSingleton::Instance().removeIframes(m_context);
288 void Explorer::deregisterObject(const JSObjectDeclarationPtr& declaration)
290 if (GLOBAL_OBJECT_NAME != declaration->getParentName()) {
291 _W("Ignored remove property %s", declaration->getName().c_str());
295 JavaScriptInterfaceSingleton::Instance().
296 removeObjectProperty(m_context,
298 declaration->getName());
301 void Explorer::cleanIframesData()
303 m_iframeSupport.clean();
306 void Explorer::callEventListeners(CustomEventType /*eventType*/, void* /*data*/)
309 using namespace WrtPlugins::Tizen;
310 // get iframe objects from javascript global context
311 JavaScriptInterface::ObjectsListPtr frameLists =
312 JavaScriptInterfaceSingleton::Instance().getIframesList(m_context);
314 // get main frame object from javascript global context
315 JSObjectRef mainFrame = JSContextGetGlobalObject(m_context);
316 frameLists->push_back(JSObjectPtr(new JSObject(mainFrame)));
318 if (eventType == ServiceCustomEvent) {
319 using namespace WrtDeviceApis::TizenServiceEvent::Api;
320 // set user data of js callback function for 'appservice' js event
321 ITizenServiceEventPtr event = ITizenServiceEventPtr(
322 new TizenServiceEvent());
323 event->setScale(m_propertySupport->getScale());
324 event->setBundle(m_propertySupport->getBundle());
326 // call js callback function for 'appservice' js event on each frame
327 FOREACH(it, *frameLists)
329 JSObjectRef frame = static_cast<JSObjectRef>((*it).Get()->getObject());
332 new JSTizenServiceEventPrivateObject(m_context, event);
333 JSObjectRef eventObject =
334 JSObjectMake(m_context,
335 JSTizenServiceEvent::getClassRef(), eventPriv);
337 AddEventListenerSupport::CallCustomEventListenersFromIFrame(
338 frame, eventType, eventObject);
340 } else if (eventType == SoftKeyboardChangeCustomEvent) {
341 using namespace WrtDeviceApis::SoftKeyboardChangeEvent::Api;
343 LogDebug("no ime size");
347 // set user data of js callback function for 'softkeyboardchange' js
349 SoftKeyboardChangeArgs* args =
350 static_cast<SoftKeyboardChangeArgs *>(data);
351 ISoftKeyboardChangeEventPtr event =
352 ISoftKeyboardChangeEventPtr(
353 new SoftKeyboardChangeEvent(
357 LogDebug("softkeyboard event's state: " << args->state);
359 // call js callback function for 'softkeyboardchange' js event on each
361 FOREACH(it, *frameLists)
363 JSObjectRef frame = static_cast<JSObjectRef>((*it).Get()->getObject());
366 new JSSoftKeyboardChangeEventPrivateObject(m_context, event);
367 JSObjectRef eventObject =
370 JSSoftKeyboardChangeEvent::getClassRef(),
373 AddEventListenerSupport::CallCustomEventListenersFromIFrame(
374 frame, eventType, eventObject);
377 LogDebug("Not supported custom event type");
383 WindowPropertySupport* Explorer::getWindowPropertySupport()
385 return m_propertySupport.get();