View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://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  package org.htmlunit.javascript.host.performance;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  import org.htmlunit.corejs.javascript.Context;
20  import org.htmlunit.corejs.javascript.json.JsonParser;
21  import org.htmlunit.corejs.javascript.json.JsonParser.ParseException;
22  import org.htmlunit.javascript.HtmlUnitScriptable;
23  import org.htmlunit.javascript.configuration.JsxClass;
24  import org.htmlunit.javascript.configuration.JsxConstant;
25  import org.htmlunit.javascript.configuration.JsxConstructor;
26  import org.htmlunit.javascript.configuration.JsxFunction;
27  import org.htmlunit.javascript.configuration.JsxGetter;
28  
29  /**
30   * A JavaScript object for {@code PerformanceNavigation}.
31   *
32   * @author Ahmed Ashour
33   * @author Ronald Brill
34   */
35  @JsxClass
36  public class PerformanceNavigation extends HtmlUnitScriptable {
37  
38      private static final Log LOG = LogFactory.getLog(PerformanceNavigation.class);
39  
40      /** Navigate. */
41      @JsxConstant
42      public static final int TYPE_NAVIGATE = 0;
43  
44      /** Reload. */
45      @JsxConstant
46      public static final int TYPE_RELOAD = 1;
47  
48      /** Back forward. */
49      @JsxConstant
50      public static final int TYPE_BACK_FORWARD = 2;
51  
52      /** Reserved. */
53      @JsxConstant
54      public static final int TYPE_RESERVED = 255;
55  
56      /**
57       * JavaScript constructor.
58       */
59      @JsxConstructor
60      public void jsConstructor() {
61          // nothing to do
62      }
63  
64      /**
65       * Returns the {@code type} property.
66       * @return the {@code type} property
67       */
68      @JsxGetter
69      public int getType() {
70          return TYPE_NAVIGATE;
71      }
72  
73      /**
74       * Returns the {@code redirectCount} property.
75       * @return the {@code redirectCount} property
76       */
77      @JsxGetter
78      public int getRedirectCount() {
79          return 0;
80      }
81  
82      /**
83       * The {@code toJSON} function.
84       * @return the {@code toJSON} object
85       */
86      @JsxFunction
87      public Object toJSON() {
88          final String jsonString = new StringBuilder()
89                  .append("{\"type\":")
90                  .append(getType())
91                  .append(", \"redirectCount\":")
92                  .append(getRedirectCount())
93                  .append('}').toString();
94          try {
95              return new JsonParser(Context.getCurrentContext(), getParentScope()).parseValue(jsonString);
96          }
97          catch (final ParseException e) {
98              if (LOG.isWarnEnabled()) {
99                  LOG.warn("Failed parsingJSON '" + jsonString + "'", e);
100             }
101         }
102         return null;
103     }
104 
105 }