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.
16 #include <installer_logic.h>
17 #include <installer_controller.h>
18 #include <dpl/string.h>
19 #include <dpl/foreach.h>
20 #include <dpl/wrt-dao-rw/feature_dao.h>
21 #include <dpl/wrt-dao-rw/plugin_dao.h>
22 #include <dpl/wrt-dao-ro/global_config.h>
23 #include <widget_install/job_widget_install.h>
24 #include <widget_uninstall/job_widget_uninstall.h>
25 #include <plugin_install/job_plugin_install.h>
26 #include <job_exception_base.h>
27 #include <plugin_install/plugin_objects.h>
29 using namespace WrtDB;
31 InstallerLogic::InstallerLogic() :
36 InstallerLogic::~InstallerLogic()
38 Assert(m_jobs.empty() && "There are still running jobs");
39 //FIXME what should be done here?
42 void InstallerLogic::Initialize()
47 void InstallerLogic::Terminate()
49 //TODO how to delete, if it is still running, paused and so on
52 it->second->SetPaused(true); //FIXME this is not enough!
58 Jobs::JobHandle InstallerLogic::AddAndStartJob(Jobs::Job *job)
60 Jobs::JobHandle handle = GetNewJobHandle();
61 job->SetJobHandle(handle);
63 m_jobs.insert(std::make_pair(handle, job));
66 CONTROLLER_POST_EVENT(InstallerController,
67 InstallerControllerEvents::NextStepEvent(job));
72 //InstallWidget, UninstallWidget InstallPlugin method are almost the same
73 // But each Job has different constructor, so creating new Job is specific
74 // i.e. widgetHandle, path etc...
75 Jobs::JobHandle InstallerLogic::InstallWidget(std::string const & widgetPath,
76 const WidgetInstallationStruct &installerStruct)
78 LogDebug("New Widget Installation:");
81 new Jobs::WidgetInstall::JobWidgetInstall(widgetPath, installerStruct);
83 return AddAndStartJob(job);
86 Jobs::JobHandle InstallerLogic::UninstallWidget(WidgetHandle widgetHandle,
87 const WidgetUninstallationStruct &uninstallerStruct)
89 LogDebug("New Widget Uninstallation");
92 new Jobs::WidgetUninstall::JobWidgetUninstall(widgetHandle,
95 return AddAndStartJob(job);
98 Jobs::JobHandle InstallerLogic::InstallPlugin(std::string const & pluginPath,
99 const PluginInstallerStruct &installerStruct)
101 LogDebug("New Plugin Installation");
104 new Jobs::PluginInstall::JobPluginInstall(pluginPath, installerStruct);
106 return AddAndStartJob(job);
109 Jobs::JobHandle InstallerLogic::InstallWidgetFromBrowser(
110 std::string const & url,
111 std::string const & icon,
112 const WidgetInstallationStruct &installerStruct)
114 LogDebug("New Widget Installation:");
116 Jobs::Job *job = new Jobs::WidgetInstall::JobWidgetInstall(
121 return AddAndStartJob(job);
124 #define TRANSLATE_JOB_EXCEPTION() \
125 _rethrown_exception.getParam()
126 #define TRANSLATE_JOB_MESSAGE() \
127 _rethrown_exception.GetMessage()
129 bool InstallerLogic::NextStep(Jobs::Job *job)
132 bool stepSucceded = job->NextStep();
137 return !job->IsPaused();
140 if (!job->GetUndoType()) {
141 //job successfully finished
143 //send finished callback
144 job->SendFinishedSuccess();
146 switch (job->GetInstallationType()) {
147 case Jobs::PluginInstallation:
148 //todo move it somewhere
149 InstallWaitingPlugins();
151 default: //because of warning
155 //job abort process completed
156 job->SendFinishedFailure();
160 m_jobs.erase(job->GetJobHandle());
165 catch (Jobs::JobExceptionBase &exc) {
167 LogInfo("Exception occured: " << exc.getParam() <<
168 ". Reverting job...");
169 bool hasAbortSteps = job->Abort();
170 job->SetUndoType(true);
171 job->SaveExceptionData(exc);
173 if (!hasAbortSteps) {
175 job->SendFinishedFailure();
178 m_jobs.erase(job->GetJobHandle());
181 return hasAbortSteps;
185 //TODO this should be moved somewhere...when it should take place? after widget
187 void InstallerLogic::InstallDeferredWidgetPackages()
189 LogWarning("Not implemented");
190 // LogInfo("Installing deferred widget packages...");
192 // WidgetPackageList packages = GlobalDAO::GetDefferedWidgetPackageInstallationList();
194 // LogInfo(packages.size() << " widget package(s) to install");
196 // // Make a copy of widget packages to install, because some
197 // // widget packages may still fail because they are running
198 // m_packagesToInstall = packages;
200 // // Start processing
201 // InstallSingleDeferredPackage();
204 void InstallerLogic::InstallSingleDeferredPackage()
206 LogWarning("Not implemented");
207 // if (m_packagesToInstall.empty())
210 // // Take single package
211 // DPL::String widgetPackage = m_packagesToInstall.front();
212 // m_packagesToInstall.pop_front();
214 // // Remove it from DB
215 // GlobalDAO::RemoveDefferedWidgetPackageInstallation(widgetPackage);
217 // // Begin installation
218 // LogInfo("Installing deferred widget package: " << widgetPackage);
220 // // Post installation
221 // CONTROLLER_POST_EVENT(
222 // InstallerController, InstallerControllerEvents::InstallWidgetEvent(
223 // DPL::ToUTF8String(widgetPackage).c_str(), WidgetInstallationStruct(
224 // &DummyInstallCallback, &DummyProgressCallback, NULL,
225 // WidgetUpdateMode::PolicyWac)));
228 void InstallerLogic::InstallPluginGeolocation(
229 const PluginInstallerStruct &installerStruct)
231 FeatureDAO::RegisterStrangeFeature(
232 std::string(GlobalConfig::GetW3CGeolocationFeatureName()));
234 LogDebug("Call plugins installer FinishedCallback");
235 installerStruct.finishedCallback(installerStruct.userParam,
236 Jobs::PluginInstall::Exceptions::Success);
239 void InstallerLogic::InstallWaitingPlugins()
241 PluginHandleSetPtr waitingPlugins;
244 PluginDAO::getPluginHandleByStatus(PluginDAO::INSTALLATION_WAITING);
246 FOREACH(it, *waitingPlugins)
248 resolvePluginDependencies(*it);
252 bool InstallerLogic::resolvePluginDependencies(PluginHandle handle)
254 PluginHandleSetPtr dependencies(new PluginHandleSet);
256 PluginObjects::ObjectsPtr requiredObjects =
257 PluginDAO::getRequiredObjectsForPluginHandle(handle);
259 PluginHandle depHandle =
260 Jobs::PluginInstall::JobPluginInstall::INVALID_HANDLE;
262 FOREACH(requiredObject, *requiredObjects)
265 PluginDAO::getPluginHandleForImplementedObject(*requiredObject);
268 Jobs::PluginInstall::JobPluginInstall::INVALID_HANDLE) {
269 LogError("Library implementing: " <<
270 *requiredObject << " NOT FOUND");
272 //PluginDAO::SetPluginInstallationStatus(INSTALLATION_WAITING);
275 dependencies->insert(depHandle);
278 PluginDAO::registerPluginLibrariesDependencies(handle, dependencies);
279 PluginDAO::setPluginInstallationStatus(handle,
280 PluginDAO::INSTALLATION_COMPLETED);