ea71ad13ab3513952940b7287a33d42ba329a953
[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     WRT::CoreModuleSingleton::Instance().Terminate();
47 }
48
49 bool AppBoxManager::requestAddBox(BoxInfoPtr boxInfo, EwkContextPtr ewkContext)
50 {
51
52     const char* appId =
53         web_provider_livebox_get_app_id(boxInfo->boxId.c_str());
54
55     if (!appId) {
56         LogD("no appid of %s", boxInfo->boxId.c_str());
57         return false;
58     }
59
60     std::string appIdStr(appId);
61     delete appId;
62
63     auto it = m_ewkContextMap.find(appIdStr);
64     if (it == m_ewkContextMap.end()) {
65         ewkContext = getAvailableEwkContext();
66         insertContextMap(appIdStr, ewkContext);
67     } else {
68         ewkContext = it->second;
69     }
70
71     if (!BoxManager::requestAddBox(boxInfo, ewkContext)) {
72         return false;
73     }
74
75     return true;
76 }
77
78 bool AppBoxManager::requestRemoveBox(std::string& instanceId)
79 {
80     if (!BoxManager::requestRemoveBox(instanceId)) {
81         return false;
82     }
83
84     return true; 
85 }
86
87 EwkContextPtr AppBoxManager::getAvailableEwkContext()
88 {
89     return EwkContextPtr(
90             ewk_context_new_with_injected_bundle_path(bundlePath.c_str()),
91             BoxManager::EwkContextDeleter());
92 }
93
94 void AppBoxManager::insertContextMap(std::string& appId, EwkContextPtr ewkContext)
95 {
96     m_ewkContextMap.insert(EwkContextMapPair(appId, ewkContext));
97 }
98
99 void AppBoxManager::eraseContextMap(std::string& appId)
100 {
101     m_ewkContextMap.erase(appId);
102 }