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_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 LogError("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 LogDebug("Requested Property: " << requestedProperty);
76 if (!providedObject) {
77 LogError("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 LogError("Wrong name of requested property");
98 currentPropertyRequested = currentPropertyRequested.substr(pos + 1);
100 currentObject = getJSObjectProperty(currentObjectName, currentObject);
101 if (!currentObject) {
102 LogError("Failed to get property: " << currentObjectName);
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 LogError("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 LogDebug("'" << providedObjectName << "' <- '" << declaration->getName() << "'");
150 std::string fullParentName = declaration->getParentName();
152 if (fullParentName == providedObjectName) {
153 return register_(declaration, providedObject, context);
156 LogDebug("Provided object was not matched!!");
158 //check if object exists in fullParentName
159 auto pos = fullParentName.find(providedObjectName);
160 if (pos == string::npos) {
162 "Object: " << declaration->getName()
163 << " should be child of: " <<
164 declaration->getParentName()
165 << " but you provided object: " <<
171 if (fullParentName.size() <= pos + providedObjectName.size() + 1) {
172 LogError("Invalid object name");
176 auto currentRequesteProperty =
177 fullParentName.substr(pos + providedObjectName.size() + 1);
179 JSObjectPtr jsObject = getProperty(
180 currentRequesteProperty, providedObject);
182 if (!jsObject->getObject()) {
184 "Object: '" << declaration->getName()
185 << "' should be child of: '" <<
186 declaration->getParentName()
187 << "'. you provided object: '" <<
189 << "' but object is null");
194 return register_(declaration, jsObject, context);
197 bool Explorer::register_(const JSObjectDeclarationPtr& declaration,
199 JSGlobalContextRef context)
201 LogDebug("Registration object: " << declaration->getParentName() <<
202 "<-" << declaration->getName());
204 Assert(parent && "parent object is NULL");
206 typedef JSObjectDeclaration::Options JO;
208 JSGlobalContextRef gContext = (context == NULL) ? m_context : context;
209 JSObjectPtr objectInstance = JavaScriptInterfaceSingleton::Instance().
210 createObject(gContext, declaration);
212 JavaScriptInterfaceSingleton::Instance().setObjectProperty(
215 declaration->getName(),
219 if (declaration->getOptions() &&
220 (declaration->getOptions()->getIframeNotice() ==
221 JO::IFrameNotice::AlwaysNotice))
223 declaration->getOptions()->invokeCallback(gContext,
225 objectInstance->getObject());
231 void Explorer::registerObjectIntoIframe(
232 const JSObjectDeclarationPtr& declaration,
233 JSObjectPtr frameObject,
234 JSGlobalContextRef context)
236 if (declaration->getParentName() == GLOBAL_OBJECT_NAME) {
237 LogDebug("Connect to Global object of IFRAME");
238 register_(declaration, frameObject, context);
244 LogWarning("Connect to NOT global object of IFRAME");
245 //it should be master object name
246 string masterName = declaration->getParentName();
247 auto pos = masterName.find(".");
248 if (string::npos != pos) {
249 LogError("ParentName not allowed");
252 auto masterObject = getJSObjectProperty(masterName, frameObject);
253 if (!masterObject->getObject()) {
254 LogError("Object not exist in frame");
257 register_(declaration, masterObject, context);
261 void Explorer::loadFrame(JSGlobalContextRef context)
263 LogDebug("Frame attached");
265 JSObjectPtr frameObject =
266 JavaScriptInterfaceSingleton::Instance().getGlobalObject(context);
268 LogDebug("Register main frame");
269 //register main frame;
271 if (frameObject->getObject() == m_globalObject->getObject()) {
272 // Main page was already loaded from constructor
273 LogDebug("Main page loaded");
277 m_iframeSupport.registerIframe(frameObject);
279 auto iframeObjects = m_iframeSupport.getIframeObjects();
281 FOREACH(object, iframeObjects)
283 LogDebug("Register object: " << (*object)->getName() );
285 registerObjectIntoIframe(*object, frameObject, context);
289 void Explorer::unloadFrame(JSGlobalContextRef context)
291 LogDebug("Frame detached");
293 JSObjectPtr frameObject =
294 JavaScriptInterfaceSingleton::Instance().getGlobalObject(context);
296 m_iframeSupport.unregisterIframe(frameObject);
299 void Explorer::removePluginsFromIframes()
301 LogDebug("Remove plugins from iframes");
303 if (m_iframeSupport.hasIframes()) {
304 LogDebug("Removing iframes");
305 JavaScriptInterfaceSingleton::Instance().removeIframes(m_context);
309 void Explorer::deregisterObject(const JSObjectDeclarationPtr& declaration)
311 LogDebug("Deregister object " << declaration->getName());
313 if (GLOBAL_OBJECT_NAME != declaration->getParentName()) {
314 LogWarning("Ignored remove property " << declaration->getName());
318 JavaScriptInterfaceSingleton::Instance().
319 removeObjectProperty(m_context,
321 declaration->getName());
324 void Explorer::cleanIframesData()
326 LogDebug("Clean iframes data");
327 m_iframeSupport.clean();
330 void Explorer::callEventListeners(CustomEventType /*eventType*/, void* /*data*/)
333 using namespace WrtPlugins::Tizen;
334 // get iframe objects from javascript global context
335 JavaScriptInterface::ObjectsListPtr frameLists =
336 JavaScriptInterfaceSingleton::Instance().getIframesList(m_context);
338 // get main frame object from javascript global context
339 JSObjectRef mainFrame = JSContextGetGlobalObject(m_context);
340 frameLists->push_back(JSObjectPtr(new JSObject(mainFrame)));
342 if (eventType == ServiceCustomEvent) {
343 using namespace WrtDeviceApis::TizenServiceEvent::Api;
344 // set user data of js callback function for 'appservice' js event
345 ITizenServiceEventPtr event = ITizenServiceEventPtr(
346 new TizenServiceEvent());
347 event->setScale(m_propertySupport->getScale());
348 event->setBundle(m_propertySupport->getBundle());
350 // call js callback function for 'appservice' js event on each frame
351 FOREACH(it, *frameLists)
353 JSObjectRef frame = static_cast<JSObjectRef>((*it).Get()->getObject());
356 new JSTizenServiceEventPrivateObject(m_context, event);
357 JSObjectRef eventObject =
358 JSObjectMake(m_context,
359 JSTizenServiceEvent::getClassRef(), eventPriv);
361 AddEventListenerSupport::CallCustomEventListenersFromIFrame(
362 frame, eventType, eventObject);
364 } else if (eventType == SoftKeyboardChangeCustomEvent) {
365 using namespace WrtDeviceApis::SoftKeyboardChangeEvent::Api;
367 LogDebug("no ime size");
371 // set user data of js callback function for 'softkeyboardchange' js
373 SoftKeyboardChangeArgs* args =
374 static_cast<SoftKeyboardChangeArgs *>(data);
375 ISoftKeyboardChangeEventPtr event =
376 ISoftKeyboardChangeEventPtr(
377 new SoftKeyboardChangeEvent(
381 LogDebug("softkeyboard event's state: " << args->state);
383 // call js callback function for 'softkeyboardchange' js event on each
385 FOREACH(it, *frameLists)
387 JSObjectRef frame = static_cast<JSObjectRef>((*it).Get()->getObject());
390 new JSSoftKeyboardChangeEventPrivateObject(m_context, event);
391 JSObjectRef eventObject =
394 JSSoftKeyboardChangeEvent::getClassRef(),
397 AddEventListenerSupport::CallCustomEventListenersFromIFrame(
398 frame, eventType, eventObject);
401 LogDebug("Not supported custom event type");
407 WindowPropertySupport* Explorer::getWindowPropertySupport()
409 return m_propertySupport.get();