summaryrefslogtreecommitdiff
path: root/src/components/application_manager/rpc_plugins/sdl_rpc_plugin/src/commands/mobile/create_window_request.cc
blob: b69b0ee80154792917cee0f54076bc478fefd945 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 Copyright (c) 2019, Ford Motor Company
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:

 Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer.

 Redistributions in binary form must reproduce the above copyright notice,
 this list of conditions and the following
 disclaimer in the documentation and/or other materials provided with the
 distribution.

 Neither the name of the Ford Motor Company nor the names of its contributors
 may be used to endorse or promote products derived from this software
 without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.
 */

#include "sdl_rpc_plugin/commands/mobile/create_window_request.h"

#include <algorithm>

#include "application_manager/application_impl.h"
#include "application_manager/application_state.h"
#include "application_manager/message_helper.h"
#include "utils/helpers.h"

namespace sdl_rpc_plugin {
using namespace application_manager;
using app_mngr::ApplicationSharedPtr;

namespace commands {

CreateWindowRequest::CreateWindowRequest(
    const application_manager::commands::MessageSharedPtr& message,
    ApplicationManager& application_manager,
    rpc_service::RPCService& rpc_service,
    HMICapabilities& hmi_capabilities,
    policy::PolicyHandlerInterface& policy_handler)
    : CommandRequestImpl(message,
                         application_manager,
                         rpc_service,
                         hmi_capabilities,
                         policy_handler) {}

CreateWindowRequest::~CreateWindowRequest() {}

bool CreateWindowRequest::CheckWindowName(
    app_mngr::ApplicationSharedPtr app,
    const app_mngr::WindowID window_id,
    const std::string& window_name) const {
  if (mobile_apis::PredefinedWindows::PRIMARY_WIDGET == window_id) {
    LOG4CXX_DEBUG(logger_,
                  "Window name check is ignored for the primary widgets");
    return true;
  }

  const bool names_are_equal = window_name == app->name().c_str();
  if (names_are_equal &&
      mobile_apis::PredefinedWindows::DEFAULT_WINDOW != window_id) {
    LOG4CXX_ERROR(logger_,
                  "Regular widget can't have the same name as application: "
                      << window_name);
    return false;
  }

  const WindowNames window_names = app->GetWindowNames();
  return !helpers::in_range(window_names, window_name);
}

void CreateWindowRequest::ApplyWindowInitialState(
    ApplicationSharedPtr app) const {
  const mobile_apis::WindowType::eType window_type =
      static_cast<mobile_apis::WindowType::eType>(
          (*message_)[strings::msg_params][strings::window_type].asInt());

  // State should be initialized with INVALID_ENUM value to let state controller
  // trigger OnHmiStatus notifiation sending
  HmiStatePtr initial_state = application_manager_.CreateRegularState(
      app,
      window_type,
      mobile_apis::HMILevel::INVALID_ENUM,
      mobile_apis::AudioStreamingState::INVALID_ENUM,
      mobile_apis::VideoStreamingState::INVALID_ENUM,
      mobile_api::SystemContext::INVALID_ENUM);

  const WindowID window_id =
      (*message_)[strings::msg_params][strings::window_id].asInt();
  const std::string window_name =
      (*message_)[strings::msg_params][strings::window_name].asString();

  smart_objects::SmartObject window_info(smart_objects::SmartType_Map);
  if ((*message_)[strings::msg_params].keyExists(
          strings::associated_service_type)) {
    window_info[strings::associated_service_type] =
        (*message_)[strings::msg_params][strings::associated_service_type];
  }
  if ((*message_)[strings::msg_params].keyExists(
          strings::duplicate_updates_from_window_id)) {
    window_info[strings::duplicate_updates_from_window_id] =
        (*message_)[strings::msg_params]
                   [strings::duplicate_updates_from_window_id];
  }

  window_info[strings::window_name] = window_name;

  app->SetWindowInfo(window_id, window_info);

  app->SetInitialState(window_id, window_name, initial_state);

  // Default HMI level for all windows except the main one is always NONE
  application_manager_.state_controller().OnAppWindowAdded(
      app, window_id, window_type, mobile_apis::HMILevel::HMI_NONE);
}

app_mngr::WindowID CreateWindowRequest::window_id() const {
  return mobile_apis::PredefinedWindows::DEFAULT_WINDOW;
}

void CreateWindowRequest::Run() {
  LOG4CXX_AUTO_TRACE(logger_);

  const auto application = application_manager_.application(connection_key());

  if (!application) {
    LOG4CXX_ERROR(logger_, "Application is not registered");
    SendResponse(false, mobile_apis::Result::APPLICATION_NOT_REGISTERED);
    return;
  }

  const auto window_id =
      (*message_)[strings::msg_params][strings::window_id].asInt();
  if (application->WindowIdExists(window_id)) {
    LOG4CXX_ERROR(logger_,
                  "Window with id #" << window_id << " does already exist");
    SendResponse(false, mobile_apis::Result::INVALID_ID);
    return;
  }

  const auto window_type =
      (*message_)[strings::msg_params][strings::window_type].asInt();

  if (mobile_apis::WindowType::eType::MAIN == window_type) {
    LOG4CXX_ERROR(logger_, "MAIN application window already exists");
    SendResponse(false, mobile_apis::Result::INVALID_DATA);
    return;
  }

  if ((*message_)[strings::msg_params].keyExists(
          strings::duplicate_updates_from_window_id)) {
    const auto duplicate_updates_from_window_id =
        (*message_)[strings::msg_params]
                   [strings::duplicate_updates_from_window_id]
                       .asInt();
    if (!application->WindowIdExists(duplicate_updates_from_window_id)) {
      LOG4CXX_ERROR(logger_,
                    "Window with id #" << duplicate_updates_from_window_id
                                       << " does not exist");
      SendResponse(false, mobile_apis::Result::INVALID_DATA);
      return;
    }
  }

  const std::string window_name =
      (*message_)[strings::msg_params][strings::window_name].asString();
  if (!CheckWindowName(application, window_id, window_name)) {
    LOG4CXX_ERROR(logger_,
                  "Window name \"" << window_name
                                   << "\" is disallowed for window #"
                                   << window_id);
    SendResponse(false, mobile_apis::Result::DUPLICATE_NAME);
    return;
  }

  if (!ValidateWindowCreation(application, window_id)) {
    return;
  }

  smart_objects::SmartObject msg_params = (*message_)[strings::msg_params];
  msg_params[strings::app_id] = application->hmi_app_id();

  StartAwaitForInterface(HmiInterfaces::HMI_INTERFACE_UI);
  SendHMIRequest(hmi_apis::FunctionID::UI_CreateWindow, &msg_params, true);
}

void CreateWindowRequest::on_event(const event_engine::Event& event) {
  LOG4CXX_AUTO_TRACE(logger_);

  if (hmi_apis::FunctionID::UI_CreateWindow != event.id()) {
    LOG4CXX_ERROR(logger_, "Received unknown event" << event.id());
    return;
  }

  auto application = application_manager_.application(connection_key());

  if (!application) {
    LOG4CXX_ERROR(logger_, "Application is not registered");
    SendResponse(false, mobile_apis::Result::APPLICATION_NOT_REGISTERED);
    return;
  }

  LOG4CXX_INFO(logger_, "Received CreateWindow event");
  EndAwaitForInterface(HmiInterfaces::HMI_INTERFACE_UI);

  const smart_objects::SmartObject& response_message = event.smart_object();
  const auto result_code = CommandRequestImpl::GetMobileResultCode(
      static_cast<hmi_apis::Common_Result::eType>(
          response_message[strings::params][hmi_response::code].asInt()));

  const bool is_success = IsMobileResultSuccess(result_code);
  std::string response_info;
  GetInfo(response_message, response_info);

  if (!is_success) {
    LOG4CXX_ERROR(logger_, "CreateWindow request has failed on HMI side");
    SendResponse(is_success,
                 result_code,
                 response_info.empty() ? nullptr : response_info.c_str());
    return;
  }

  ApplyWindowInitialState(application);

  SendResponse(is_success,
               result_code,
               response_info.empty() ? nullptr : response_info.c_str());
}

bool CreateWindowRequest::Init() {
  hash_update_mode_ = HashUpdateMode::kDoHashUpdate;
  return true;
}

bool CreateWindowRequest::IsWindowForAssociatedServiceCreated(
    app_mngr::ApplicationSharedPtr app) const {
  LOG4CXX_AUTO_TRACE(logger_);

  const auto window_optional_params_map =
      app->window_optional_params_map().GetData();

  if (!(*message_)[strings::msg_params].keyExists(
          strings::associated_service_type)) {
    return false;
  }

  const auto associated_service_type =
      (*message_)[strings::msg_params][strings::associated_service_type]
          .asString();

  const auto find_res = std::find_if(
      window_optional_params_map.begin(),
      window_optional_params_map.end(),
      [&associated_service_type](
          const std::pair<WindowID, smart_objects::SmartObjectSPtr>& element) {
        LOG4CXX_DEBUG(logger_,
                      "Searching for " << associated_service_type
                                       << " in window info for id "
                                       << element.first);
        if (element.second->keyExists(strings::associated_service_type) &&
            associated_service_type ==
                (*element.second)[strings::associated_service_type]
                    .asString()) {
          return true;
        }

        return false;
      });

  return find_res != window_optional_params_map.end();
}

bool CreateWindowRequest::DoesExceedMaxAllowedWindows(
    app_mngr::ApplicationSharedPtr app) const {
  LOG4CXX_AUTO_TRACE(logger_);

  auto get_current_number_of_windows =
      [&app](const mobile_apis::WindowType::eType window_type) -> size_t {
    switch (window_type) {
      case mobile_apis::WindowType::MAIN: {
        return 1u;
      }
      case mobile_apis::WindowType::WIDGET: {
        return app->window_optional_params_map().GetData().size();
      }

      default: {
        LOG4CXX_WARN(logger_, "Unknown window type");
        return 0u;
      }
    }
  };

  const auto window_type = static_cast<mobile_apis::WindowType::eType>(
      (*message_)[strings::msg_params][strings::window_type].asInt());

  auto display_capabilities = hmi_capabilities_.system_display_capabilities();
  if (app->display_capabilities()) {
    display_capabilities = app->display_capabilities();
  }

  if (!display_capabilities) {
    LOG4CXX_WARN(logger_, "Application has no capabilities");
    return false;
  }

  MessageHelper::PrintSmartObject(*display_capabilities);

  const auto windowTypeSupported =
      (*display_capabilities)[0][strings::window_type_supported].asArray();

  DCHECK(windowTypeSupported);

  const auto find_res = std::find_if(
      windowTypeSupported->begin(),
      windowTypeSupported->end(),
      [&window_type](const smart_objects::SmartObject& element) {
        if (window_type == static_cast<mobile_apis::WindowType::eType>(
                               element[strings::window_type].asInt())) {
          return true;
        }

        return false;
      });

  if (find_res == windowTypeSupported->end()) {
    LOG4CXX_WARN(logger_, "Requested Window Type is not supported by the HMI");
    return true;
  }

  if (get_current_number_of_windows(window_type) + 1 >
      (*find_res)[strings::maximum_number_of_windows].asUInt()) {
    return true;
  }

  return false;
}

bool CreateWindowRequest::ValidateWindowCreation(
    app_mngr::ApplicationSharedPtr app, const WindowID window_id) {
  LOG4CXX_AUTO_TRACE(logger_);

  if (DoesExceedMaxAllowedWindows(app)) {
    std::string info("Maximum allowed amount of windows is exceeded");
    LOG4CXX_WARN(logger_, info);
    SendResponse(false, mobile_apis::Result::REJECTED, info.c_str());
    return false;
  }

  if (IsWindowForAssociatedServiceCreated(app)) {
    std::string info(
        "Window for this associated service type is already created");
    LOG4CXX_WARN(logger_, info);
    SendResponse(false, mobile_apis::Result::REJECTED, info.c_str());
    return false;
  }

  return true;
}

}  // namespace commands

}  // namespace sdl_rpc_plugin