• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2020 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #ifndef CEF_TESTS_CEFTESTS_TEST_SERVER_H_
6 #define CEF_TESTS_CEFTESTS_TEST_SERVER_H_
7 #pragma once
8 
9 #include <string>
10 
11 #include "include/base/cef_callback.h"
12 #include "include/cef_registration.h"
13 #include "include/cef_request.h"
14 #include "include/cef_response.h"
15 #include "include/cef_server.h"
16 
17 namespace test_server {
18 
19 extern const char kServerAddress[];
20 extern const uint16 kServerPort;
21 extern const char kServerScheme[];
22 extern const char kServerOrigin[];
23 
24 using DoneCallback = base::OnceClosure;
25 
26 using StartDoneCallback =
27     base::OnceCallback<void(const std::string& server_origin)>;
28 
29 // Starts the server if it is not currently running, and executes |callback| on
30 // the UI thread. This method should be called by each test case that relies on
31 // the server.
32 void Start(StartDoneCallback callback);
33 
34 // Stops the server if it is currently running, and executes |callback| on the
35 // UI thread. This method will be called by the test framework on shutdown.
36 void Stop(DoneCallback callback);
37 
38 // Observer for CefServerHandler callbacks. Methods will be called on the UI
39 // thread.
40 class Observer {
41  public:
42   // Called when this Observer is registered.
43   virtual void OnRegistered() = 0;
44 
45   // Called when this Observer is unregistered.
46   virtual void OnUnregistered() = 0;
47 
48   // See CefServerHandler documentation for usage. Return true if the callback
49   // was handled.
OnClientConnected(CefRefPtr<CefServer> server,int connection_id)50   virtual bool OnClientConnected(CefRefPtr<CefServer> server,
51                                  int connection_id) {
52     return false;
53   }
OnClientDisconnected(CefRefPtr<CefServer> server,int connection_id)54   virtual bool OnClientDisconnected(CefRefPtr<CefServer> server,
55                                     int connection_id) {
56     return false;
57   }
58   virtual bool OnHttpRequest(CefRefPtr<CefServer> server,
59                              int connection_id,
60                              const CefString& client_address,
61                              CefRefPtr<CefRequest> request) = 0;
62 
63  protected:
~Observer()64   virtual ~Observer() {}
65 };
66 
67 // Add an observer for CefServerHandler callbacks. Remains registered until the
68 // returned CefRegistration object is destroyed. Registered observers will be
69 // executed in the order of registration until one returns true to indicate that
70 // it handled the callback. |callback| will be executed on the UI thread after
71 // registration is complete.
72 CefRefPtr<CefRegistration> AddObserver(Observer* observer,
73                                        DoneCallback callback);
74 
75 // Combination of AddObserver() followed by Start().
76 CefRefPtr<CefRegistration> AddObserverAndStart(Observer* observer,
77                                                StartDoneCallback callback);
78 
79 // Helper for sending a fully qualified response.
80 void SendResponse(CefRefPtr<CefServer> server,
81                   int connection_id,
82                   CefRefPtr<CefResponse> response,
83                   const std::string& response_data);
84 
85 // Helper for managing Observer registration and callbacks. Only used on the UI
86 // thread.
87 class ObserverHelper : Observer {
88  public:
89   ObserverHelper();
90   ~ObserverHelper() override;
91 
92   // Initialize the registration. Results in a call to OnInitialized().
93   void Initialize();
94 
95   // Shut down the registration. Results in a call to OnShutdown().
96   void Shutdown();
97 
98   // Implement this method to start sending server requests after Initialize().
99   virtual void OnInitialized(const std::string& server_origin) = 0;
100 
101   // Implement this method to continue the test after Shutdown().
102   virtual void OnShutdown() = 0;
103 
104  private:
105   void OnStartDone(const std::string& server_origin);
106   void OnRegistered() override;
107   void OnUnregistered() override;
108 
109   CefRefPtr<CefRegistration> registration_;
110 
111   enum class State {
112     NONE,
113     INITIALIZING,
114     INITIALIZED,
115     SHUTTINGDOWN,
116   } state_ = State::NONE;
117 
118   base::WeakPtrFactory<ObserverHelper> weak_ptr_factory_;
119 
120   DISALLOW_COPY_AND_ASSIGN(ObserverHelper);
121 };
122 
123 }  // namespace test_server
124 
125 #endif  // CEF_TESTS_CEFTESTS_TEST_SERVER_H_
126