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