Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1074)

Side by Side Diff: ios/web/public/test/http_server.h

Issue 2898733003: Split up ios/web:test_support. (Closed)
Patch Set: don't break downstream clients Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ios/web/public/test/fakes/BUILD.gn ('k') | ios/web/public/test/http_server.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef IOS_WEB_PUBLIC_TEST_HTTP_SERVER_H_ 5 #ifndef IOS_WEB_PUBLIC_TEST_HTTP_SERVER_H_
6 #define IOS_WEB_PUBLIC_TEST_HTTP_SERVER_H_ 6 #define IOS_WEB_PUBLIC_TEST_HTTP_SERVER_H_
7 7
8 #include <memory> 8 #import "ios/web/public/test/http_server.h"
9 #include <vector>
10
11 #import "base/mac/scoped_nsobject.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/synchronization/lock.h"
15 #import "ios/web/public/test/response_providers/response_provider.h"
16
17 @class GCDWebServer;
18
19 namespace web {
20 namespace test {
21
22 // A convience class for wrapping a ResponseProvider so that it can be used
23 // inside data structures that operate on ref counted objects. This class is a
24 // ref counted container for a ResponseProvider.
25 // This object exists for legacy reasons since a large part of the code base
26 // still uses ResponseProviders that are not ref counted.
27 class RefCountedResponseProviderWrapper :
28 public base::RefCounted<RefCountedResponseProviderWrapper> {
29 public:
30 // Main constructor.
31 explicit RefCountedResponseProviderWrapper(
32 std::unique_ptr<ResponseProvider> response_provider);
33 // Returns the ResponseProvider that backs this object.
34 ResponseProvider* GetResponseProvider() { return response_provider_.get(); }
35 private:
36 friend class base::RefCounted<RefCountedResponseProviderWrapper>;
37 // The ResponseProvider that backs this object.
38 std::unique_ptr<ResponseProvider> response_provider_;
39 virtual ~RefCountedResponseProviderWrapper();
40 };
41
42 // The HttpServer is an in-process web server that is used to service requests.
43 // It is a singleton and backed by a GCDWebServer.
44 // HttpServer can be configured to serve requests by registering
45 // web::ResponseProviders.
46 // This class is not thread safe on the whole and only certain methods are
47 // thread safe.
48 class HttpServer {
49 public:
50 typedef std::vector<std::unique_ptr<ResponseProvider>> ProviderList;
51
52 // Returns the shared HttpServer instance. Thread safe.
53 static HttpServer& GetSharedInstance();
54 // Returns the shared HttpServer instance and registers the response providers
55 // as well. Takes ownership of the response providers. Must be called from the
56 // main thread.
57 static HttpServer& GetSharedInstanceWithResponseProviders(
58 ProviderList response_providers);
59
60 // A convenience method for the longer form of
61 // |web::test::HttpServer::GetSharedInstance().MakeUrlForHttpServer|
62 static GURL MakeUrl(const std::string& url);
63
64 // Starts the server on the default port 8080. CHECKs if the server can not be
65 // started.
66 // Must be called from the main thread.
67 void StartOrDie();
68 // Starts the server on |port|. Returns true on success, false otherwise.
69 // Must be called from the main thread.
70 bool StartOnPort(NSUInteger port);
71 // Stops the server and prevents it from accepting new requests.
72 // Must be called from the main thread.
73 void Stop();
74 // Returns true if the server is running.
75 // Must be called from the main thread.
76 bool IsRunning() const;
77
78 // Returns the port that the server is running on. Thread Safe
79 NSUInteger GetPort() const;
80
81 // Adds a ResponseProvider. Takes ownership of the ResponseProvider.
82 // Note for using URLs inside of the |response_provider|:
83 // The HttpServer cannot run on default HTTP port 80, so URLs used in
84 // ResponseProviders must be converted at runtime after the HttpServer's port
85 // is determined. Please use |MakeUrl| to handle converting URLs.
86 // Must be called from the main thread.
87 void AddResponseProvider(std::unique_ptr<ResponseProvider> response_provider);
88 // Removes the |response_provider|. Must be called from the main thread.
89 void RemoveResponseProvider(ResponseProvider* response_provider);
90 // Removes all the response providers. Must be called from the main thread.
91 void RemoveAllResponseProviders();
92
93 private:
94 // Initializes the server by registering for a GCDWebServer servlet. Must be
95 // called from the main thread.
96 void InitHttpServer();
97 HttpServer();
98 ~HttpServer();
99
100 // Sets the port that the server is running on. Thread Safe
101 void SetPort(NSUInteger port);
102
103 // Creates a GURL that the server can service based on the |url|
104 // passed in.
105 // It does not rewrite URLs if the |url| can already be serviced by the
106 // server.
107 // |url| must be a valid URL. Thread safe.
108 GURL MakeUrlForHttpServer(const std::string& url) const;
109
110 // Returns the response provider that can handle the |request|.
111 // Note: No more than one reponse provider can handle the request.
112 // Thread safe.
113 scoped_refptr<RefCountedResponseProviderWrapper>
114 GetResponseProviderForRequest(
115 const web::ResponseProvider::Request& request);
116
117 // Lock for serializing access to |provider_|.
118 mutable base::Lock provider_list_lock_;
119 // Lock for serializing access to |port_|.
120 mutable base::Lock port_lock_;
121 // The port that the server is running on. 0 if the server is not running.
122 NSUInteger port_;
123 // The GCDWebServer backing the HttpServer.
124 base::scoped_nsobject<GCDWebServer> gcd_web_server_;
125 // The list of providers to service a request.
126 std::vector<scoped_refptr<RefCountedResponseProviderWrapper>> providers_;
127 DISALLOW_COPY_AND_ASSIGN(HttpServer);
128 };
129
130 } // namespace test
131 } // namspace web
132 9
133 #endif // IOS_WEB_PUBLIC_TEST_HTTP_SERVER_H_ 10 #endif // IOS_WEB_PUBLIC_TEST_HTTP_SERVER_H_
134
OLDNEW
« no previous file with comments | « ios/web/public/test/fakes/BUILD.gn ('k') | ios/web/public/test/http_server.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698