Update change log and spec for wrt-plugins-tizen_0.4.13
[framework/web/wrt-plugins-tizen.git] / src / Common / GlobalContextManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include "GlobalContextManager.h"
19
20 #include <dlog.h>
21
22 #undef LOG_TAG
23 #define LOG_TAG "TIZEN_DEVICEAPI"
24
25 using namespace std;
26
27 namespace DeviceAPI {
28 namespace Common{
29
30 GlobalContextManager::GlobalContextManager(){
31 }
32
33 GlobalContextManager* GlobalContextManager::getInstance(){
34     static GlobalContextManager instance;
35     return &instance;
36 }
37
38 JSContextRef GlobalContextManager::getGlobalContext(JSContextRef ctx){
39     if( ctx == NULL ){
40         LOGE("local context is NULL");
41         return NULL;
42     }
43     JSObjectRef global = JSContextGetGlobalObject(ctx);
44     ContextMapT::iterator itr;
45     itr = mContexts.find(global);
46     if( itr == mContexts.end() ){
47         LOGE("Can not found global Context");
48         return NULL;
49     }
50     return itr->second;
51 }
52
53 bool GlobalContextManager::isAliveGlobalContext(JSContextRef ctx){
54     if( ctx == NULL )
55         return false;
56
57     JSContextRef context = getGlobalContext(ctx);
58
59     if( context == ctx )
60         return true;
61     else
62         return false;
63 }
64
65 void GlobalContextManager::addGlobalContext(JSContextRef ctx){
66     ContextMapT::iterator itr;
67     JSObjectRef global = JSContextGetGlobalObject(ctx);
68     itr = mContexts.find(global);
69     if( itr != mContexts.end() ){
70         LOGD("already added global context");
71         return;
72     }
73     mContexts[global] = ctx;
74 }
75
76 void GlobalContextManager::removeGlobalContext(JSContextRef ctx){
77     ContextMapT::iterator itr;
78     JSObjectRef global = JSContextGetGlobalObject(ctx);
79     itr = mContexts.find(global);
80     if( itr == mContexts.end() ){
81         LOGD("does not exist context");
82         return;
83     }
84     if( itr->second == ctx )
85         mContexts.erase(itr);
86     else
87         LOGE("passed context is not global context");
88 }
89
90 }
91 }
92