Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright 2017, The Android Open Source Project
      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  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 const LAYER_TRACE_MAGIC_NUMBER = [0x09, 0x4c, 0x59, 0x52, 0x54, 0x52, 0x41, 0x43, 0x45] // .LYRTRACE
     18 const WINDOW_TRACE_MAGIC_NUMBER = [0x09, 0x57, 0x49, 0x4e, 0x54, 0x52, 0x41, 0x43, 0x45] // .WINTRACE
     19 
     20 function arrayEquals(a, b) {
     21   if (a.length !== b.length) {
     22     return false;
     23   }
     24   for (var i = 0; i < a.length; i++) {
     25     if (a[i] != b[i]) {
     26       return false;
     27     }
     28   }
     29   return true;
     30 }
     31 
     32 function arrayStartsWith(array, prefix) {
     33   return arrayEquals(array.slice(0, prefix.length), prefix);
     34 }
     35 
     36 /** buffer: Uint8Array */
     37 function detect(buffer) {
     38   if (arrayStartsWith(buffer, LAYER_TRACE_MAGIC_NUMBER)) {
     39     return 'layers_trace'
     40   }
     41   if (arrayStartsWith(buffer, WINDOW_TRACE_MAGIC_NUMBER)) {
     42     return 'window_trace'
     43   }
     44   if (arrayStartsWith(buffer, [0x12])) {
     45      // Because policy is empty, the first field in the dump is 2 currently.
     46      // This might change.
     47     return 'window_dump';
     48   }
     49   if (arrayStartsWith(buffer, [0x0a])) {
     50     // For now; window_dump might soon start with 0x0a too.
     51     return 'layers_dump';
     52   }
     53 }
     54 
     55 export default detect;
     56