View Javadoc
1   /*
2    * Copyright (c) 2002-2017 Gargoyle Software Inc.
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    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package com.gargoylesoftware.htmlunit;
16  
17  import java.io.Serializable;
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  
22  import com.gargoylesoftware.htmlunit.javascript.host.WebSocket;
23  
24  /**
25   * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
26   *
27   * Represents a ways to get notified about internal objects.
28   *
29   * @author Ahmed Ashour
30   */
31  public class WebClientInternals implements Serializable {
32  
33      private final WebClient webClient_;
34      private Collection<Listener> listeners_;
35  
36      WebClientInternals(final WebClient webClient) {
37          webClient_ = webClient;
38      }
39  
40      /**
41       * Adds a new {@link Listener}.
42       *
43       * @param listener the listener to add
44       */
45      public void addListener(final Listener listener) {
46          synchronized (this) {
47              if (listeners_ == null) {
48                  listeners_ = Collections.synchronizedList(new ArrayList<>());
49              }
50          }
51          listeners_.add(listener);
52      }
53  
54      /**
55       * Removes the specified {@link Listener}.
56       *
57       * @param listener the listener to remove
58       */
59      public void removeListener(final Listener listener) {
60          listeners_.remove(listener);
61      }
62  
63      /**
64       * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
65       *
66       * To notify when a new {@link WebSocket} is created.
67       *
68       * @param webSocket the {@link WebSocket}
69       */
70      public void created(final WebSocket webSocket) {
71          if (listeners_ != null) {
72              synchronized (listeners_) {
73                  for (Listener l : listeners_) {
74                      l.webSocketCreated(webSocket);
75                  }
76              }
77          }
78      }
79  
80      /**
81       * A listener for internal events.
82       */
83      public interface Listener {
84  
85          /**
86           * A new {@link WebSocket} is created.
87           *
88           * @param webSocket the created {@link WebSocket}
89           */
90          void webSocketCreated(WebSocket webSocket);
91      }
92  }