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