Remove box terminating code
[platform/framework/web/web-provider.git] / src / Plugin / AppBoxPlugin / AppBoxManager.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Flora License, Version 1.1 (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
7  *
8  *        http://floralicense.org/license/
9  *
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.
15  */
16 /**
17  * @file    AppBoxManager.cpp
18  * @author  Yunchan Cho (yunchan.cho@samsung.com)
19  */
20 #include <map>
21 #include <core_module.h>
22 #include <Plugin/IBoxPluginFactory.h>
23 #include <Core/BoxData.h>
24 #include <Core/BoxManager.h>
25 #include <Core/IBox.h>
26 #include <Core/Util/Log.h>
27 #include <API/web_provider_livebox_info.h>
28 #include "AppBoxObserver.h"
29 #include "AppBoxManager.h"
30
31 static const std::string bundlePath("/usr/lib/libwrt-injected-bundle.so");
32
33 AppBoxManager::AppBoxManager(IBoxPluginFactoryPtr factory)
34     : BoxManager(factory)
35 {
36     bool ret = WRT::CoreModuleSingleton::Instance().Init();
37     if (!ret) {
38         throw; // throw exeception
39     }
40     AppBoxObserver::Instance()->initialize();
41 }
42
43 AppBoxManager::~AppBoxManager()
44 {
45     AppBoxObserver::Instance()->shutdown();
46 }
47
48 bool AppBoxManager::requestAddBox(BoxInfoPtr boxInfo, EwkContextPtr ewkContext)
49 {
50
51     const char* appId =
52         web_provider_livebox_get_app_id(boxInfo->boxId.c_str());
53
54     if (!appId) {
55         LogD("no appid of %s", boxInfo->boxId.c_str());
56         return false;
57     }
58
59     std::string appIdStr(appId);
60     delete appId;
61
62     auto it = m_ewkContextMap.find(appIdStr);
63     if (it == m_ewkContextMap.end()) {
64         ewkContext = getAvailableEwkContext();
65         insertContextMap(appIdStr, ewkContext);
66     } else {
67         ewkContext = it->second;
68     }
69
70     if (!BoxManager::requestAddBox(boxInfo, ewkContext)) {
71         return false;
72     }
73
74     return true;
75 }
76
77 bool AppBoxManager::requestRemoveBox(std::string& instanceId)
78 {
79     if (!BoxManager::requestRemoveBox(instanceId)) {
80         return false;
81     }
82
83     return true; 
84 }
85
86 EwkContextPtr AppBoxManager::getAvailableEwkContext()
87 {
88     return EwkContextPtr(
89             ewk_context_new_with_injected_bundle_path(bundlePath.c_str()),
90             BoxManager::EwkContextDeleter());
91 }
92
93 void AppBoxManager::insertContextMap(std::string& appId, EwkContextPtr ewkContext)
94 {
95     m_ewkContextMap.insert(EwkContextMapPair(appId, ewkContext));
96 }
97
98 void AppBoxManager::eraseContextMap(std::string& appId)
99 {
100     m_ewkContextMap.erase(appId);
101 }