1 // Copyright 2019 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.
4 
5 #ifndef CONTENT_BROWSER_BROWSING_DATA_BROWSING_DATA_BROWSERTEST_UTILS_H_
6 #define CONTENT_BROWSER_BROWSING_DATA_BROWSING_DATA_BROWSERTEST_UTILS_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/scoped_observer.h"
12 #include "content/browser/service_worker/service_worker_context_core_observer.h"
13 #include "content/browser/service_worker/service_worker_context_wrapper.h"
14 #include "net/test/embedded_test_server/embedded_test_server.h"
15 #include "net/test/embedded_test_server/http_response.h"
16 #include "services/network/public/mojom/cookie_manager.mojom.h"
17 
18 namespace content {
19 class StoragePartition;
20 
21 namespace browsing_data_browsertest_utils {
22 
23 // TODO(msramek): A class like this already exists in ServiceWorkerBrowserTest.
24 // Consider extracting it to a different test utils file.
25 class ServiceWorkerActivationObserver
26     : public ServiceWorkerContextCoreObserver {
27  public:
28   // |callback| is called when |context| is activated.
29   static void SignalActivation(ServiceWorkerContextWrapper* context,
30                                base::OnceClosure callback);
31 
32  private:
33   ServiceWorkerActivationObserver(ServiceWorkerContextWrapper* context,
34                                   base::OnceClosure callback);
35 
36   ~ServiceWorkerActivationObserver() override;
37 
38   // ServiceWorkerContextCoreObserver overrides.
39   void OnVersionStateChanged(int64_t version_id,
40                              const GURL& scope,
41                              ServiceWorkerVersion::Status) override;
42 
43   ServiceWorkerContextWrapper* context_;
44   ScopedObserver<ServiceWorkerContextWrapper, ServiceWorkerContextCoreObserver>
45       scoped_observer_;
46   base::OnceClosure callback_;
47 };
48 
49 // Appends a switch to the |command_line| based on whether the network service
50 // is enabled. The browser will ignore certificate errors if the network service
51 // is not enabled.
52 void SetIgnoreCertificateErrors(base::CommandLine* command_line);
53 
54 // Adds a service worker for the given |origin|. The EmbeddedTestServer
55 // |https_server| is required to retrieve a URL to the server based on the
56 // |origin|.
57 void AddServiceWorker(const std::string& origin,
58                       StoragePartition* storage_partition,
59                       net::EmbeddedTestServer* https_server);
60 
61 // Retrieves the list of all service workers.
62 std::vector<StorageUsageInfo> GetServiceWorkers(
63     StoragePartition* storage_partition);
64 
65 // Populates the content and content type fields of a given HTTP |response|
66 // based on the file extension of the |url| as follows:
67 //
68 // For .js:
69 // Example: "https://localhost/?file=file.js"
70 // will set the response header as
71 // Content-Type: application/javascript
72 //
73 // For .html:
74 // Example: "https://localhost/?file=file.html"
75 // will set the response header as
76 // Content-Type: text/html
77 //
78 // Response content type is only set for .js and .html files.
79 void SetResponseContent(const GURL& url,
80                         std::string* value,
81                         net::test_server::BasicHttpResponse* response);
82 
83 // Sets up a MockCertVerifier with default return value |default_result|.
84 void SetUpMockCertVerifier(int32_t default_result);
85 
86 }  // namespace browsing_data_browsertest_utils
87 
88 }  // namespace content
89 
90 #endif  // CONTENT_BROWSER_BROWSING_DATA_BROWSING_DATA_BROWSERTEST_UTILS_H_
91