1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
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  
31  
32  
33  
34  
35  @JsxClass
36  public class PerformanceNavigation extends HtmlUnitScriptable {
37  
38      private static final Log LOG = LogFactory.getLog(PerformanceNavigation.class);
39  
40      
41      @JsxConstant
42      public static final int TYPE_NAVIGATE = 0;
43  
44      
45      @JsxConstant
46      public static final int TYPE_RELOAD = 1;
47  
48      
49      @JsxConstant
50      public static final int TYPE_BACK_FORWARD = 2;
51  
52      
53      @JsxConstant
54      public static final int TYPE_RESERVED = 255;
55  
56      
57  
58  
59      @JsxConstructor
60      public void jsConstructor() {
61          
62      }
63  
64      
65  
66  
67  
68      @JsxGetter
69      public int getType() {
70          return TYPE_NAVIGATE;
71      }
72  
73      
74  
75  
76  
77      @JsxGetter
78      public int getRedirectCount() {
79          return 0;
80      }
81  
82      
83  
84  
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 }