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 "frameworks/bridge/common/manifest/manifest_router.h"
17 
18 #include "base/log/event_report.h"
19 
20 namespace OHOS::Ace::Framework {
21 
GetEntry(const std::string& suffix) const22 std::string ManifestRouter::GetEntry(const std::string& suffix) const
23 {
24     if (pages_.empty()) {
25         return "";
26     }
27     return pages_.front() + suffix;
28 }
29 
GetPagePath(std::string& uri) const30 std::string ManifestRouter::GetPagePath(std::string& uri) const
31 {
32     const std::string suffix = ".js";
33     if (uri.empty()) {
34         return "";
35     }
36     if (pages_.empty()) {
37         return "";
38     }
39     // the case uri is starts with "/" and "/" is the mainPage
40     if (uri == "/") {
41         uri = pages_.front();
42         return pages_.front() + suffix;
43     }
44     if (std::find(pages_.begin(), pages_.end(), uri) != pages_.end()) {
45         return uri + suffix;
46     }
47     LOGW("[Engine Log] can't find this page %{public}s path", uri.c_str());
48     return "";
49 }
50 
GetPagePath(const std::string& uri, const std::string& suffix) const51 std::string ManifestRouter::GetPagePath(const std::string& uri, const std::string& suffix) const
52 {
53     if (uri.empty()) {
54         return "";
55     }
56     // the case uri is starts with "/" and "/" is the mainPage
57     if (uri.front() == '/') {
58         if (uri.size() == 1) {
59             return pages_.front() + suffix;
60         }
61     } else {
62         if (std::find(std::begin(pages_), std::end(pages_), uri) != std::end(pages_)) {
63             return uri + suffix;
64         }
65     }
66     if (uri.rfind(suffix) != std::string::npos) {
67         return uri;
68     }
69     LOGW("[Engine Log] can't find this page %{public}s path", uri.c_str());
70     return "";
71 }
72 
GetPageList()73 const std::list<std::string>& ManifestRouter::GetPageList()
74 {
75     return pages_;
76 }
77 
InsertPageList(const std::string& uri)78 void ManifestRouter::InsertPageList(const std::string& uri)
79 {
80     pages_.emplace_back(uri);
81 }
82 
RouterParse(const std::unique_ptr<JsonValue>& root)83 void ManifestRouter::RouterParse(const std::unique_ptr<JsonValue>& root)
84 {
85     if (!root) {
86         return;
87     }
88 
89     auto pagesArray = root->Contains("pages") ? root->GetValue("pages") : root->GetValue("src");
90     if (pagesArray && pagesArray->IsArray()) {
91         for (int32_t index = 0; index < pagesArray->GetArraySize(); index++) {
92             auto page = pagesArray->GetArrayItem(index);
93             if (page && page->IsString()) {
94                 pages_.emplace_back(page->GetString());
95             }
96         }
97     }
98 
99     if (pages_.empty()) {
100         EventReport::SendPageRouterException(PageRouterExcepType::ROUTE_PARSE_ERR);
101     }
102 }
103 
104 } // namespace OHOS::Ace::Framework
105