org.chromium.sdk/src/org/chromium/sdk/internal/tools/v8/BreakpointImpl.java
changeset 355 8726e95bcbba
parent 2 e4420d2515f1
equal deleted inserted replaced
354:0bceeb415e7f 355:8726e95bcbba
     3 // found in the LICENSE file.
     3 // found in the LICENSE file.
     4 
     4 
     5 package org.chromium.sdk.internal.tools.v8;
     5 package org.chromium.sdk.internal.tools.v8;
     6 
     6 
     7 import org.chromium.sdk.Breakpoint;
     7 import org.chromium.sdk.Breakpoint;
     8 import org.chromium.sdk.BrowserTab;
     8 import org.chromium.sdk.JavascriptVm;
       
     9 import org.chromium.sdk.SyncCallback;
       
    10 import org.chromium.sdk.internal.protocol.data.BreakpointInfo;
     9 
    11 
    10 /**
    12 /**
    11  * A generic implementation of the Breakpoint interface.
    13  * A generic implementation of the Breakpoint interface.
    12  */
    14  */
    13 public class BreakpointImpl implements Breakpoint {
    15 public class BreakpointImpl implements Breakpoint {
    19 
    21 
    20   /**
    22   /**
    21    * The breakpoint id as reported by the JavaScript VM.
    23    * The breakpoint id as reported by the JavaScript VM.
    22    */
    24    */
    23   private long id;
    25   private long id;
       
    26 
       
    27   /**
       
    28    * The corresponding script name as reported by the JavaScript VM. May be null.
       
    29    */
       
    30   private String scriptName;
       
    31 
       
    32   /**
       
    33    * The corresponding script id as reported by the JavaScript VM. May be null.
       
    34    */
       
    35   private Long scriptId;
       
    36 
       
    37   /**
       
    38    * Breakpoint line number. May become invalidated by LiveEdit actions.
       
    39    */
       
    40   private long lineNumber;
    24 
    41 
    25   /**
    42   /**
    26    * Whether the breakpoint is enabled.
    43    * Whether the breakpoint is enabled.
    27    */
    44    */
    28   private boolean isEnabled;
    45   private boolean isEnabled;
    48    * Whether the breakpoint data have changed with respect
    65    * Whether the breakpoint data have changed with respect
    49    * to the JavaScript VM data.
    66    * to the JavaScript VM data.
    50    */
    67    */
    51   private volatile boolean isDirty = false;
    68   private volatile boolean isDirty = false;
    52 
    69 
    53   public BreakpointImpl(Type type, long id, boolean enabled, int ignoreCount, String condition,
    70   public BreakpointImpl(Type type, long id, String scriptName, Long scriptId, long lineNumber,
    54       BreakpointManager breakpointManager) {
    71       boolean enabled, int ignoreCount, String condition, BreakpointManager breakpointManager) {
    55     this.type = type;
    72     this.type = type;
       
    73     this.scriptName = scriptName;
       
    74     this.scriptId = scriptId;
    56     this.id = id;
    75     this.id = id;
    57     this.isEnabled = enabled;
    76     this.isEnabled = enabled;
    58     this.ignoreCount = ignoreCount;
    77     this.ignoreCount = ignoreCount;
    59     this.condition = condition;
    78     this.condition = condition;
       
    79     this.lineNumber = lineNumber;
    60     this.breakpointManager = breakpointManager;
    80     this.breakpointManager = breakpointManager;
       
    81   }
       
    82 
       
    83   public BreakpointImpl(BreakpointInfo info, BreakpointManager breakpointManager) {
       
    84     this.type = getType(info);
       
    85     this.id = info.number();
       
    86     this.breakpointManager = breakpointManager;
       
    87     updateFromRemote(info);
       
    88   }
       
    89   public void updateFromRemote(BreakpointInfo info) {
       
    90     if (this.type != getType(info)) {
       
    91       throw new IllegalArgumentException();
       
    92     }
       
    93     if (this.id != info.number()) {
       
    94       throw new IllegalArgumentException();
       
    95     }
       
    96     this.lineNumber = info.line();
       
    97     this.isEnabled = info.active();
       
    98     this.ignoreCount = (int) info.ignoreCount();
       
    99     this.condition = info.condition();
       
   100     this.scriptName = info.script_name();
       
   101     this.scriptId = info.script_id();
    61   }
   102   }
    62 
   103 
    63   public boolean isEnabled() {
   104   public boolean isEnabled() {
    64     return isEnabled;
   105     return isEnabled;
    65   }
   106   }
    70 
   111 
    71   public long getId() {
   112   public long getId() {
    72     return id;
   113     return id;
    73   }
   114   }
    74 
   115 
       
   116   public String getScriptName() {
       
   117     return scriptName;
       
   118   }
       
   119 
       
   120   public Long getScriptId() {
       
   121     return scriptId;
       
   122   }
       
   123 
    75   public int getIgnoreCount() {
   124   public int getIgnoreCount() {
    76     return ignoreCount;
   125     return ignoreCount;
    77   }
   126   }
    78 
   127 
    79   public String getCondition() {
   128   public String getCondition() {
    80     return condition;
   129     return condition;
       
   130   }
       
   131 
       
   132   public long getLineNumber() {
       
   133     return lineNumber;
    81   }
   134   }
    82 
   135 
    83   public void setEnabled(boolean enabled) {
   136   public void setEnabled(boolean enabled) {
    84     if (this.isEnabled != enabled) {
   137     if (this.isEnabled != enabled) {
    85       setDirty(true);
   138       setDirty(true);
   104 
   157 
   105   private boolean eq(Object left, Object right) {
   158   private boolean eq(Object left, Object right) {
   106     return left == right || (left != null && left.equals(right));
   159     return left == right || (left != null && left.equals(right));
   107   }
   160   }
   108 
   161 
   109   public void clear(final BrowserTab.BreakpointCallback callback) {
   162   public void clear(JavascriptVm.BreakpointCallback callback, SyncCallback syncCallback) {
   110     breakpointManager.clearBreakpoint(this, callback);
   163     breakpointManager.clearBreakpoint(this, callback, syncCallback);
   111     // The order must be preserved, otherwise the breakpointProcessor will not be able
   164     // The order must be preserved, otherwise the breakpointProcessor will not be able
   112     // to identify the original breakpoint ID.
   165     // to identify the original breakpoint ID.
   113     this.id = INVALID_ID;
   166     this.id = INVALID_ID;
   114   }
   167   }
   115 
   168 
   116   public void flush(final BrowserTab.BreakpointCallback callback) {
   169   public void flush(final JavascriptVm.BreakpointCallback callback, SyncCallback syncCallback) {
   117     if (!isDirty()) {
   170     if (!isDirty()) {
   118       if (callback != null) {
   171       if (callback != null) {
   119         callback.success(this);
   172         callback.success(this);
   120       }
   173       }
   121       return;
   174       return;
   122     }
   175     }
   123     breakpointManager.changeBreakpoint(this, callback);
   176     breakpointManager.changeBreakpoint(this, callback, syncCallback);
   124     setDirty(false);
   177     setDirty(false);
   125   }
   178   }
   126 
   179 
   127   private void setDirty(boolean isDirty) {
   180   private void setDirty(boolean isDirty) {
   128     this.isDirty = isDirty;
   181     this.isDirty = isDirty;
   130 
   183 
   131   private boolean isDirty() {
   184   private boolean isDirty() {
   132     return isDirty;
   185     return isDirty;
   133   }
   186   }
   134 
   187 
       
   188   private static Type getType(BreakpointInfo info) {
       
   189     BreakpointInfo.Type infoType = info.type();
       
   190     switch (infoType) {
       
   191       case scriptId: return Type.SCRIPT_ID;
       
   192       case scriptName: return Type.SCRIPT_NAME;
       
   193       case function: return Type.FUNCTION;
       
   194     }
       
   195     throw new RuntimeException("Unknown type: " + infoType);
       
   196   }
   135 }
   197 }