org.chromium.sdk/src/org/chromium/sdk/internal/ScriptImpl.java
changeset 2 e4420d2515f1
child 276 f2f4a1259de8
equal deleted inserted replaced
1:ef76fc2ac88c 2:e4420d2515f1
       
     1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
       
     2 // Use of this source code is governed by a BSD-style license that can be
       
     3 // found in the LICENSE file.
       
     4 
       
     5 package org.chromium.sdk.internal;
       
     6 
       
     7 import java.util.List;
       
     8 
       
     9 import org.chromium.sdk.Script;
       
    10 import org.chromium.sdk.internal.protocol.data.ScriptHandle;
       
    11 import org.chromium.sdk.internal.protocol.data.SomeHandle;
       
    12 import org.chromium.sdk.internal.protocolparser.JsonProtocolParseException;
       
    13 import org.chromium.sdk.internal.tools.v8.V8ProtocolUtil;
       
    14 
       
    15 /**
       
    16  * An objects that holds data for a "script" which is a part of a resource
       
    17  * loaded into the browser, identified by its original document URL, line offset
       
    18  * in the original document, and the line count this script spans.
       
    19  */
       
    20 public class ScriptImpl implements Script {
       
    21 
       
    22   /**
       
    23    * An object containing data that uniquely identify a V8 script chunk.
       
    24    */
       
    25   public static class Descriptor {
       
    26     public final Type type;
       
    27 
       
    28     public final String name;
       
    29 
       
    30     public final int lineOffset;
       
    31 
       
    32     public final int endLine;
       
    33 
       
    34     public final long id;
       
    35 
       
    36     public Descriptor(Type type, long id, String name, int lineOffset, int lineCount) {
       
    37       this.type = type;
       
    38       this.id = id;
       
    39       this.name = name;
       
    40       this.lineOffset = lineOffset;
       
    41       this.endLine = lineOffset + lineCount - 1;
       
    42     }
       
    43 
       
    44     @Override
       
    45     public int hashCode() {
       
    46       return
       
    47           name != null ? name.hashCode() : (int) id * 0x101 +
       
    48           lineOffset * 0x1001 +
       
    49           endLine * 0x10001;
       
    50     }
       
    51 
       
    52     @Override
       
    53     public boolean equals(Object obj) {
       
    54       if (obj == this) {
       
    55         return true;
       
    56       }
       
    57       if (!(obj instanceof Descriptor)) {
       
    58         return false;
       
    59       }
       
    60       Descriptor that = (Descriptor) obj;
       
    61       // The id equality is stronger than the name equality.
       
    62       return this.id == that.id &&
       
    63           this.lineOffset == that.lineOffset &&
       
    64           this.endLine == that.endLine;
       
    65     }
       
    66 
       
    67     public static Descriptor forResponse(ScriptHandle script, List<SomeHandle> refs,
       
    68         V8ContextFilter contextFilter) {
       
    69       script = V8ProtocolUtil.validScript(script, refs, contextFilter);
       
    70       if (script == null) {
       
    71         return null;
       
    72       }
       
    73       String name = script.name();
       
    74       try {
       
    75         Long scriptType = script.scriptType();
       
    76         Type type = V8ProtocolUtil.getScriptType(scriptType);
       
    77         if (type == null) {
       
    78           return null;
       
    79         }
       
    80         int lineOffset = (int) script.lineOffset();
       
    81         int lineCount = (int) script.lineCount();
       
    82         int id = V8ProtocolUtil.getScriptIdFromResponse(script).intValue();
       
    83         return new Descriptor(type, id, name, lineOffset, lineCount);
       
    84       } catch (Exception e) {
       
    85         // not a script object has been passed in
       
    86         return null;
       
    87       }
       
    88     }
       
    89   }
       
    90 
       
    91   private final Descriptor descriptor;
       
    92 
       
    93   private String source;
       
    94 
       
    95   /**
       
    96    * @param descriptor of the script retrieved from a "scripts" response
       
    97    */
       
    98   public ScriptImpl(Descriptor descriptor) {
       
    99     this.descriptor = descriptor;
       
   100     this.source = null;
       
   101   }
       
   102 
       
   103   public Type getType() {
       
   104     return this.descriptor.type;
       
   105   }
       
   106 
       
   107   public String getName() {
       
   108     return descriptor.name;
       
   109   }
       
   110 
       
   111   public int getStartLine() {
       
   112     return descriptor.lineOffset;
       
   113   }
       
   114 
       
   115   public int getEndLine() {
       
   116     return descriptor.endLine;
       
   117   }
       
   118 
       
   119   public long getId() {
       
   120     return descriptor.id;
       
   121   }
       
   122 
       
   123   public String getSource() {
       
   124     return source;
       
   125   }
       
   126 
       
   127   public boolean hasSource() {
       
   128     return source != null;
       
   129   }
       
   130 
       
   131   public void setSource(String source) {
       
   132     this.source = source;
       
   133   }
       
   134 
       
   135   @Override
       
   136   public int hashCode() {
       
   137     return
       
   138         descriptor.hashCode() * 0x101 +
       
   139         (hasSource() ? (source.hashCode() * 0x1001) : 0);
       
   140   }
       
   141 
       
   142   @Override
       
   143   public boolean equals(Object obj) {
       
   144     if (this == obj) {
       
   145       return true;
       
   146     }
       
   147     if (!(obj instanceof ScriptImpl)) {
       
   148       return false;
       
   149     }
       
   150     ScriptImpl that = (ScriptImpl) obj;
       
   151     return this.descriptor.equals(that.descriptor) && eq(this.source, that.source);
       
   152   }
       
   153 
       
   154   private static boolean eq(Object left, Object right) {
       
   155     return left == right || (left != null && left.equals(right));
       
   156   }
       
   157 
       
   158   @Override
       
   159   public String toString() {
       
   160     StringBuilder sb = new StringBuilder();
       
   161     sb.append("[Script (").append(hasSource()
       
   162         ? "has"
       
   163         : "no").append(" source): name=").append(getName()).append(", lineRange=[").append(
       
   164         getStartLine()).append(';').append(getEndLine()).append("]]");
       
   165     return sb.toString();
       
   166   }
       
   167 
       
   168   public static Long getScriptId(HandleManager handleManager, long scriptRef) {
       
   169     SomeHandle handle = handleManager.getHandle(scriptRef);
       
   170     if (handle == null) {
       
   171       return -1L; // not found
       
   172     }
       
   173     ScriptHandle scriptHandle;
       
   174     try {
       
   175       scriptHandle = handle.asScriptHandle();
       
   176     } catch (JsonProtocolParseException e) {
       
   177       throw new RuntimeException(e);
       
   178     }
       
   179     return scriptHandle.id();
       
   180   }
       
   181 
       
   182 }