1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "service_extension.h"
17
18 #include "configuration_utils.h"
19 #include "connection_manager.h"
20 #include "hilog_wrapper.h"
21 #include "js_service_extension.h"
22 #include "runtime.h"
23 #include "service_extension_context.h"
24
25 namespace OHOS {
26 namespace AbilityRuntime {
27 using namespace OHOS::AppExecFwk;
28
29 CreatorFunc ServiceExtension::creator_ = nullptr;
SetCreator(const CreatorFunc & creator)30 void ServiceExtension::SetCreator(const CreatorFunc& creator)
31 {
32 creator_ = creator;
33 }
34
Create(const std::unique_ptr<Runtime> & runtime)35 ServiceExtension* ServiceExtension::Create(const std::unique_ptr<Runtime>& runtime)
36 {
37 if (!runtime) {
38 return new ServiceExtension();
39 }
40
41 if (creator_) {
42 return creator_(runtime);
43 }
44
45 HILOG_INFO("ServiceExtension::Create runtime");
46 switch (runtime->GetLanguage()) {
47 case Runtime::Language::JS:
48 return JsServiceExtension::Create(runtime);
49
50 default:
51 return new ServiceExtension();
52 }
53 }
54
Init(const std::shared_ptr<AbilityLocalRecord> & record,const std::shared_ptr<OHOSApplication> & application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)55 void ServiceExtension::Init(const std::shared_ptr<AbilityLocalRecord> &record,
56 const std::shared_ptr<OHOSApplication> &application,
57 std::shared_ptr<AbilityHandler> &handler,
58 const sptr<IRemoteObject> &token)
59 {
60 ExtensionBase<ServiceExtensionContext>::Init(record, application, handler, token);
61 HILOG_INFO("ServiceExtension begin init context");
62 }
63
CreateAndInitContext(const std::shared_ptr<AbilityLocalRecord> & record,const std::shared_ptr<OHOSApplication> & application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)64 std::shared_ptr<ServiceExtensionContext> ServiceExtension::CreateAndInitContext(
65 const std::shared_ptr<AbilityLocalRecord> &record,
66 const std::shared_ptr<OHOSApplication> &application,
67 std::shared_ptr<AbilityHandler> &handler,
68 const sptr<IRemoteObject> &token)
69 {
70 std::shared_ptr<ServiceExtensionContext> context =
71 ExtensionBase<ServiceExtensionContext>::CreateAndInitContext(record, application, handler, token);
72 if (context == nullptr) {
73 HILOG_ERROR("ServiceExtension::CreateAndInitContext context is nullptr");
74 return context;
75 }
76 return context;
77 }
78
OnConfigurationUpdated(const AppExecFwk::Configuration & configuration)79 void ServiceExtension::OnConfigurationUpdated(const AppExecFwk::Configuration &configuration)
80 {
81 Extension::OnConfigurationUpdated(configuration);
82
83 auto context = GetContext();
84 if (context == nullptr) {
85 HILOG_ERROR("Context is invalid.");
86 return;
87 }
88
89 auto configUtils = std::make_shared<ConfigurationUtils>();
90 configUtils->UpdateGlobalConfig(configuration, context->GetResourceManager());
91 }
92 } // namespace AbilityRuntime
93 } // namespace OHOS
94