forked from rdkcentral/rdkservices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackager.cpp
165 lines (133 loc) · 5.9 KB
/
Packager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2020 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Packager.h"
#define API_VERSION_NUMBER_MAJOR 1
#define API_VERSION_NUMBER_MINOR 0
#define API_VERSION_NUMBER_PATCH 4
namespace WPEFramework {
namespace {
static Plugin::Metadata<Plugin::Packager> metadata(
// Version (Major, Minor, Patch)
API_VERSION_NUMBER_MAJOR, API_VERSION_NUMBER_MINOR, API_VERSION_NUMBER_PATCH,
// Preconditions
{},
// Terminations
{},
// Controls
{}
);
}
namespace Plugin {
namespace {
constexpr int kMaxValueLength = 256;
}
SERVICE_REGISTRATION(Packager, API_VERSION_NUMBER_MAJOR, API_VERSION_NUMBER_MINOR, API_VERSION_NUMBER_PATCH);
const string Packager::Initialize(PluginHost::IShell* service) {
ASSERT (_service == nullptr);
ASSERT (service != nullptr);
_service = service;
_skipURL = static_cast<uint8_t>(service->WebPrefix().length());
_service->Register(&_notification);
string result;
_implementation = _service->Root<Exchange::IPackager>(_connectionId, 2000, _T("PackagerImplementation"));
if (_implementation == nullptr) {
result = _T("Couldn't create package instance");
_service->Unregister(&_notification);
} else if (_implementation->Configure(_service) != Core::ERROR_NONE) {
result = _T("Couldn't initialize package instance");
_service->Unregister(&_notification);
}
return (result);
}
void Packager::Deinitialize(PluginHost::IShell* service)
{
ASSERT(_service == service);
_service->Unregister(&_notification);
if (_implementation->Release() != Core::ERROR_DESTRUCTION_SUCCEEDED) {
ASSERT(_connectionId != 0);
RPC::IRemoteConnection* connection(_service->RemoteConnection(_connectionId));
// The process can disappear in the meantime...
if (connection != nullptr) {
// But if it did not dissapear in the meantime, forcefully terminate it. Shoot to kill :-)
connection->Terminate();
connection->Release();
}
}
_service = nullptr;
_implementation = nullptr;
}
string Packager::Information() const
{
return (string());
}
void Packager::Inbound(Web::Request& request)
{
}
Core::ProxyType<Web::Response> Packager::Process(const Web::Request& request)
{
ASSERT(_skipURL <= request.Path.length());
Core::ProxyType<Web::Response> result(PluginHost::IFactories::Instance().Response());
Core::TextSegmentIterator index(Core::TextFragment(request.Path, _skipURL, request.Path.length() - _skipURL), false, '/');
// Always skip the first one, it is an empty part because we start with a '/' if there are more parameters.
index.Next();
result->ErrorCode = Web::STATUS_BAD_REQUEST;
result->Message = _T("Invalid request to packager plugin.");
if (index.Next() && (request.Verb == Web::Request::HTTP_POST || request.Verb == Web::Request::HTTP_PUT)) {
uint32_t status = Core::ERROR_UNAVAILABLE;
if (index.Current().Text() == "Install") {
std::array<char, kMaxValueLength> package {0};
std::array<char, kMaxValueLength> version {0};
std::array<char, kMaxValueLength> arch {0};
Core::URL::KeyValue options(request.Query.Value());
if (options.Exists(_T("Package"), true) == true) {
const string name (options[_T("Package")].Text());
Core::URL::Decode (name.c_str(), name.length(), package.data(), package.size());
}
if (options.Exists(_T("Architecture"), true) == true) {
const string name (options[_T("Architecture")].Text());
Core::URL::Decode (name.c_str(), name.length(), arch.data(), arch.size());
}
if (options.Exists(_T("Version"), true) == true) {
const string name (options[_T("Version")].Text());
Core::URL::Decode (name.c_str(), name.length(), version.data(), version.size());
}
status = _implementation->Install(package.data(), version.data(), arch.data());
} else if (index.Current().Text() == "SynchronizeRepository") {
status = _implementation->SynchronizeRepository();
}
if (status == Core::ERROR_NONE) {
result->ErrorCode = Web::STATUS_OK;
result->Message = _T("OK");
} else if (status == Core::ERROR_INPROGRESS) {
result->Message = _T("Some operation already in progress. Only one at a time is allowed");
}
}
return(result);
}
void Packager::Deactivated(RPC::IRemoteConnection* connection)
{
if (connection->Id() == _connectionId) {
ASSERT(_service != nullptr);
Core::IWorkerPool::Instance().Submit(PluginHost::IShell::Job::Create(_service,
PluginHost::IShell::DEACTIVATED,
PluginHost::IShell::FAILURE));
}
}
} // namespace Plugin
} // namespace WPEFramework