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/log.h>
28 #include "plugin_property_support.h"
31 #include <js_overlay_support.h>
32 #include <js_overlay_types.h>
33 #include <js_overlay_addEventListener.h>
34 #include <TizenServiceEvent/ITizenServiceEvent.h>
35 #include <TizenServiceEvent/TizenServiceEvent.h>
38 //const char* SEPARATOR = ".";
39 const std::string GLOBAL_OBJECT_NAME = "GLOBAL_OBJECT";
43 using namespace PluginModule;
44 using namespace WrtPlugins::W3C;
46 Explorer::Explorer(JSGlobalContextRef context) :
50 LogError("Context is NULL");
55 JavaScriptInterfaceSingleton::Instance().getGlobalObject(context);
57 LogDebug("Register main frame");
58 //register main frame;
59 m_iframeSupport.registerIframe(m_globalObject);
61 m_propertySupport.reset(new WindowPropertySupport(m_context));
63 // initialization for use of custom event
64 if (!AddEventListenerSupport::isInitialized())
66 LogInfo("initialization for use of custom event");
67 AddEventListenerSupport::initialize(context);
73 // deinitialization for use of custom event
74 if (AddEventListenerSupport::isInitialized()) {
75 LogInfo("deinitialization for use of custom event");
76 AddEventListenerSupport::deinitialize();
80 JSObjectPtr Explorer::getJSObjectProperty(const std::string& propertyName,
83 LogDebug("Get object property " << propertyName);
84 return JavaScriptInterfaceSingleton::Instance().getJSObjectProperty(
85 m_context, object, propertyName);
88 JSObjectPtr Explorer::getProperty(
89 const string& requestedProperty,
90 JSObjectPtr providedObject)
92 LogDebug("Requested Property: " << requestedProperty);
96 LogError("Provided object is empty");
100 std::string currentObjectName;
101 std::string currentPropertyRequested = requestedProperty;
102 JSObjectPtr currentObject = providedObject;
106 auto pos = currentPropertyRequested.find(".");
107 if (pos == string::npos)
109 return getJSObjectProperty(currentPropertyRequested, currentObject);
112 //check if requested object has child
113 currentObjectName = currentPropertyRequested.substr(0,pos);
115 if (currentPropertyRequested.size() <= pos+1)
117 LogError("Wrong name of requested property");
118 return JSObjectPtr();
120 currentPropertyRequested = currentPropertyRequested.substr(pos+1);
122 currentObject = getJSObjectProperty(currentObjectName, currentObject);
125 LogError("Failed to get property: " << currentObjectName);
126 return JSObjectPtr();
131 bool Explorer::registerObject(const JSObjectDeclarationPtr& declaration,
132 JSGlobalContextRef context)
134 LogDebug("Register to global object");
138 if (declaration->getParentName() == GLOBAL_OBJECT_NAME)
140 LogDebug("Connect to Global object");
141 retValue = register_(declaration, m_globalObject, context);
145 LogDebug("Connect to NOT global object");
146 //it should be master object name
147 string masterName = declaration->getParentName();
148 auto pos = masterName.find(".");
149 if (string::npos != pos)
151 LogError("ParentName not allowed");
154 auto masterObject = getJSObjectProperty(masterName, m_globalObject);
155 if (masterObject->getObject() == 0)
159 retValue = registerObject(declaration, masterName, masterObject, context);
162 if (declaration->checkIframesSupported()) {
163 LogDebug("Iframe supported");
164 m_iframeSupport.registerDeclaration(declaration);
165 // m_iframeSupport.invokeCallbackForMainFrame(m_context,
170 LogDebug("Iframe NOT supported");
177 bool Explorer::registerObject(const JSObjectDeclarationPtr& declaration,
178 const std::string& providedObjectName,
179 JSObjectPtr providedObject,
180 JSGlobalContextRef context)
182 LogDebug("Register object. Provided object name: '" << providedObjectName
183 << "', registration of: '" << declaration->getName() << "'");
185 std::string fullParentName = declaration->getParentName();
186 LogDebug("Parent name: " << declaration->getParentName());
188 if(fullParentName == providedObjectName)
190 LogDebug("Provided object match equaly to requested parent");
191 return register_(declaration, providedObject, context);
194 //check if object exists in fullParentName
195 auto pos = fullParentName.find(providedObjectName);
196 if(pos == string::npos)
198 LogError("Object: " << declaration->getName()
199 << " should be child of: " << declaration->getParentName()
200 << " but you provided object: " << providedObjectName);
205 if (fullParentName.size() <= pos+providedObjectName.size()+1)
207 LogError("Invalid object name");
211 auto currentRequesteProperty =
212 fullParentName.substr(pos+providedObjectName.size()+1);
215 JSObjectPtr jsObject = getProperty(
216 currentRequesteProperty, providedObject);
218 if(!jsObject->getObject())
220 LogError("Object: '" << declaration->getName()
221 << "' should be child of: '" << declaration->getParentName()
222 << "'. you provided object: '" << providedObjectName
223 << "' but object is null");
228 return register_(declaration, jsObject, context);
231 bool Explorer::register_(const JSObjectDeclarationPtr& declaration,
233 JSGlobalContextRef context)
235 LogInfo("Registration object: " << declaration->getParentName() <<
236 "<-" << declaration->getName());
238 Assert(parent && "parent object is NULL");
240 typedef JSObjectDeclaration::Options JO;
242 JSGlobalContextRef gContext = (context==NULL)?m_context:context;
243 JSObjectPtr newObject;
245 JSObjectPtr objectInstance = JavaScriptInterfaceSingleton::Instance().
246 createObject(gContext,declaration);
249 if (declaration->getOptions() &&
250 declaration->getOptions()->getType() == JO::ClassType::Function)
252 LogDebug("Loading function object " << declaration->getName());
254 auto type = declaration->getOptions()->getIframeOverlay();
255 if (JO::IFrameOverlay::OverlayedBeforeOriginal == type)
257 LogDebug("Create overlayed function");
258 JSObjectPtr original =
259 JavaScriptInterfaceSingleton::Instance().
260 getJSObjectProperty(gContext,
262 declaration->getName());
263 newObject = JSOverlaySupport::
264 createWrappedFunction(gContext,
267 declaration->getName());
271 LogDebug("create normal function");
272 newObject = objectInstance;
275 else //object creation
277 newObject = objectInstance;
280 JavaScriptInterfaceSingleton::Instance().setObjectProperty(
283 declaration->getName(),
287 if (declaration->getOptions() &&
288 (declaration->getOptions()->getIframeNotice() ==
289 JO::IFrameNotice::AlwaysNotice))
291 declaration->getOptions()->invokeCallback(gContext,
293 objectInstance->getObject());
300 void Explorer::registerObjectIntoIframe(const JSObjectDeclarationPtr& declaration,
301 JSObjectPtr frameObject,
302 JSGlobalContextRef context)
304 if (declaration->getParentName() == GLOBAL_OBJECT_NAME)
306 LogDebug("Connect to Global object of IFRAME");
307 register_(declaration, frameObject, context);
313 LogWarning("Connect to NOT global object of IFRAME");
314 //it should be master object name
315 string masterName = declaration->getParentName();
316 auto pos = masterName.find(".");
317 if (string::npos != pos)
319 LogError("ParentName not allowed");
322 auto masterObject = getJSObjectProperty(masterName, frameObject);
323 if (!masterObject->getObject())
325 LogError("Object not exist in frame");
328 register_(declaration, masterObject, context);
333 void Explorer::loadFrame(JSGlobalContextRef context)
335 LogDebug("Frame attached");
337 JSObjectPtr frameObject =
338 JavaScriptInterfaceSingleton::Instance().getGlobalObject(context);
340 LogDebug("Register main frame");
341 //register main frame;
343 if(frameObject->getObject() == m_globalObject->getObject())
345 // Main page was already loaded from constructor
346 LogDebug("Main page loaded");
350 m_iframeSupport.registerIframe(frameObject);
352 auto iframeObjects = m_iframeSupport.getIframeObjects();
354 FOREACH(object, iframeObjects)
356 LogDebug("Register object: " << (*object)->getName() );
358 registerObjectIntoIframe(*object, frameObject, context);
362 void Explorer::unloadFrame(JSGlobalContextRef context)
364 LogDebug("Frame detached");
366 JSObjectPtr frameObject =
367 JavaScriptInterfaceSingleton::Instance().getGlobalObject(context);
369 m_iframeSupport.unregisterIframe(frameObject);
372 void Explorer::removePluginsFromIframes()
374 LogDebug("Remove plugins from iframes");
376 if (m_iframeSupport.hasIframes())
378 LogDebug("Removing iframes");
379 JavaScriptInterfaceSingleton::Instance().removeIframes(m_context);
383 void Explorer::deregisterObject(const JSObjectDeclarationPtr& declaration)
385 LogDebug("Deregister object " << declaration->getName());
387 if (GLOBAL_OBJECT_NAME != declaration->getParentName())
389 LogWarning("Ignored remove property " << declaration->getName());
393 JavaScriptInterfaceSingleton::Instance().
394 removeObjectProperty(m_context,
396 declaration->getName());
399 void Explorer::cleanIframesData()
401 LogDebug("Clean iframes data");
402 m_iframeSupport.clean();
405 void Explorer::callEventListeners(CustomEventType eventType)
407 if (eventType == ServiceCustomEvent)
409 using namespace WrtDeviceApis::TizenServiceEvent::Api;
411 // get iframe objects from javascript global context
412 JavaScriptInterface::ObjectsListPtr frameLists =
413 JavaScriptInterfaceSingleton::Instance().getIframesList(m_context);
415 // get main frame object from javascript global context
416 JSObjectRef mainFrame = JSContextGetGlobalObject(m_context);
417 frameLists->push_back(JSObjectPtr(new JSObject(mainFrame)));
419 // set user data of js callback function for 'appservice' js event
420 ITizenServiceEventPtr event = ITizenServiceEventPtr(new TizenServiceEvent());
421 event->setScale(m_propertySupport->getScale());
422 event->setBundle(m_propertySupport->getBundle());
424 // call js callback function for 'appservice' js event on each frame
425 FOREACH(it, *frameLists)
427 JSObjectRef frame = static_cast<JSObjectRef>((*it).Get()->getObject());
428 AddEventListenerSupport::CallServiceListenersFromIFrame(frame, event);
431 LogInfo("Not supported custom event type");
435 WindowPropertySupport* Explorer::getWindowPropertySupport()
437 return m_propertySupport.get();