Add ICU message format support
[chromium-blink-merge.git] / ios / web / public / test / http_server.h
blobcacc5f1e10ff83404bc296898fab1d54544bb13e
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef IOS_WEB_PUBLIC_TEST_HTTP_SERVER_H_
6 #define IOS_WEB_PUBLIC_TEST_HTTP_SERVER_H_
8 #include <vector>
10 #import "base/mac/scoped_nsobject.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/synchronization/lock.h"
15 #include "ios/web/public/test/response_providers/response_provider.h"
17 @class GCDWebServer;
19 namespace web {
20 namespace test {
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 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 scoped_ptr<ResponseProvider> response_provider_;
39 virtual ~RefCountedResponseProviderWrapper();
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 ScopedVector<ResponseProvider> ProviderList;
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 const ProviderList& response_providers);
60 // A convenience method for the longer form of
61 // |web::test::HttpServer::GetSharedInstance().MakeUrlForHttpServer|
62 static GURL MakeUrl(const std::string& url);
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;
78 // Adds a ResponseProvider. Takes ownership of the ResponseProvider.
79 // Note for using URLs inside of the |response_provider|:
80 // The HttpServer cannot run on default HTTP port 80, so URLs used in
81 // ResponseProviders must be converted at runtime after the HttpServer's port
82 // is determined. Please use |MakeUrl| to handle converting URLs.
83 // Must be called from the main thread.
84 void AddResponseProvider(ResponseProvider* response_provider);
85 // Removes the |response_provider|. Must be called from the main thread.
86 void RemoveResponseProvider(ResponseProvider* response_provider);
87 // Removes all the response providers. Must be called from the main thread.
88 void RemoveAllResponseProviders();
90 private:
91 // Initializes the server by registering for a GCDWebServer servlet. Must be
92 // called from the main thread.
93 void InitHttpServer();
94 HttpServer();
95 ~HttpServer();
97 // Sets the port that the server is running on. Thread Safe
98 void SetPort(NSUInteger port);
99 // Returns the port that the server is running on. Thread Safe
100 NSUInteger GetPort() const;
102 // Creates a GURL that the server can service based on the |url|
103 // passed in.
104 // It does not rewrite URLs if the |url| can already be serviced by the
105 // server.
106 // |url| must be a valid URL. Thread safe.
107 GURL MakeUrlForHttpServer(const std::string& url) const;
109 // Returns the response provider that can handle the |request|.
110 // Note: No more than one reponse provider can handle the request.
111 // Thread safe.
112 scoped_refptr<RefCountedResponseProviderWrapper>
113 GetResponseProviderForRequest(
114 const web::ResponseProvider::Request& request);
116 // Lock for serializing access to |provider_|.
117 mutable base::Lock provider_list_lock_;
118 // Lock for serializing access to |port_|.
119 mutable base::Lock port_lock_;
120 // The port that the server is running on. 0 if the server is not running.
121 NSUInteger port_;
122 // The GCDWebServer backing the HttpServer.
123 base::scoped_nsobject<GCDWebServer> gcd_web_server_;
124 // The list of providers to service a request.
125 std::vector<scoped_refptr<RefCountedResponseProviderWrapper>> providers_;
126 DISALLOW_COPY_AND_ASSIGN(HttpServer);
129 } // namespace test
130 } // namspace web
132 #endif // IOS_WEB_PUBLIC_TEST_HTTP_SERVER_H_