github.com/MontFerret/ferret@v0.18.0/pkg/drivers/cdp/dom/frame.go (about)

     1  package dom
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/mafredri/cdp/protocol/page"
     7  )
     8  
     9  type (
    10  	Frame struct {
    11  		tree page.FrameTree
    12  		node *HTMLDocument
    13  	}
    14  
    15  	AtomicFrameID struct {
    16  		mu    sync.Mutex
    17  		value page.FrameID
    18  	}
    19  
    20  	AtomicFrameCollection struct {
    21  		mu    sync.Mutex
    22  		value map[page.FrameID]Frame
    23  	}
    24  )
    25  
    26  func NewAtomicFrameID() *AtomicFrameID {
    27  	return &AtomicFrameID{}
    28  }
    29  
    30  func (id *AtomicFrameID) Get() page.FrameID {
    31  	id.mu.Lock()
    32  	defer id.mu.Unlock()
    33  
    34  	return id.value
    35  }
    36  
    37  func (id *AtomicFrameID) Set(value page.FrameID) {
    38  	id.mu.Lock()
    39  	defer id.mu.Unlock()
    40  
    41  	id.value = value
    42  }
    43  
    44  func (id *AtomicFrameID) Reset() {
    45  	id.mu.Lock()
    46  	defer id.mu.Unlock()
    47  
    48  	id.value = ""
    49  }
    50  
    51  func (id *AtomicFrameID) IsEmpty() bool {
    52  	id.mu.Lock()
    53  	defer id.mu.Unlock()
    54  
    55  	return id.value == ""
    56  }
    57  
    58  func NewAtomicFrameCollection() *AtomicFrameCollection {
    59  	return &AtomicFrameCollection{
    60  		value: make(map[page.FrameID]Frame),
    61  	}
    62  }
    63  
    64  func (fc *AtomicFrameCollection) Length() int {
    65  	fc.mu.Lock()
    66  	defer fc.mu.Unlock()
    67  
    68  	return len(fc.value)
    69  }
    70  
    71  func (fc *AtomicFrameCollection) ForEach(predicate func(value Frame, key page.FrameID) bool) {
    72  	fc.mu.Lock()
    73  	defer fc.mu.Unlock()
    74  
    75  	for k, v := range fc.value {
    76  		if predicate(v, k) == false {
    77  			break
    78  		}
    79  	}
    80  }
    81  
    82  func (fc *AtomicFrameCollection) Has(key page.FrameID) bool {
    83  	fc.mu.Lock()
    84  	defer fc.mu.Unlock()
    85  
    86  	_, ok := fc.value[key]
    87  
    88  	return ok
    89  }
    90  
    91  func (fc *AtomicFrameCollection) Get(key page.FrameID) (Frame, bool) {
    92  	fc.mu.Lock()
    93  	defer fc.mu.Unlock()
    94  
    95  	found, ok := fc.value[key]
    96  
    97  	if ok {
    98  		return found, ok
    99  	}
   100  
   101  	return Frame{}, false
   102  }
   103  
   104  func (fc *AtomicFrameCollection) Set(key page.FrameID, value Frame) {
   105  	fc.mu.Lock()
   106  	defer fc.mu.Unlock()
   107  
   108  	fc.value[key] = value
   109  }
   110  
   111  func (fc *AtomicFrameCollection) Remove(key page.FrameID) {
   112  	fc.mu.Lock()
   113  	defer fc.mu.Unlock()
   114  
   115  	delete(fc.value, key)
   116  }
   117  
   118  func (fc *AtomicFrameCollection) ToSlice() []Frame {
   119  	fc.mu.Lock()
   120  	defer fc.mu.Unlock()
   121  
   122  	slice := make([]Frame, 0, len(fc.value))
   123  
   124  	for _, v := range fc.value {
   125  		slice = append(slice, v)
   126  	}
   127  
   128  	return slice
   129  }