• 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_REQUEST_H_
6 #define CEF_TESTS_CEFTESTS_TEST_REQUEST_H_
7 #pragma once
8 
9 #include <string>
10 
11 #include "include/base/cef_callback.h"
12 #include "include/cef_cookie.h"
13 #include "include/cef_frame.h"
14 #include "include/cef_request.h"
15 #include "include/cef_request_context.h"
16 #include "include/cef_resource_handler.h"
17 #include "include/cef_response.h"
18 
19 namespace test_request {
20 
21 // Stores all state passed to CefURLRequestClient.
22 struct State {
23  public:
24   // Number of times each callback is executed.
25   int upload_progress_ct_ = 0;
26   int download_progress_ct_ = 0;
27   int download_data_ct_ = 0;
28   int auth_credentials_ct_ = 0;
29   int request_complete_ct_ = 0;
30 
31   // From OnUploadProgress.
32   int64 upload_total_ = 0;
33 
34   // From OnDownloadProgress.
35   int64 download_total_ = 0;
36 
37   // From OnDownloadData.
38   std::string download_data_;
39 
40   // From OnRequestComplete.
41   CefRefPtr<CefRequest> request_;
42   cef_urlrequest_status_t status_ = UR_UNKNOWN;
43   cef_errorcode_t error_code_ = ERR_NONE;
44   CefRefPtr<CefResponse> response_;
45   bool response_was_cached_ = false;
46 };
47 
48 using RequestDoneCallback = base::OnceCallback<void(const State& state)>;
49 
50 struct SendConfig {
51   // Send using |frame_| or |request_context_| if non-nullptr.
52   // Sends using the global request context if both are nullptr.
53   CefRefPtr<CefFrame> frame_;
54   CefRefPtr<CefRequestContext> request_context_;
55 
56   // The request to send.
57   CefRefPtr<CefRequest> request_;
58 
59   // Returned via GetAuthCredentials if |has_credentials_| is true.
60   bool has_credentials_ = false;
61   std::string username_;
62   std::string password_;
63 };
64 
65 // Send a request. |callback| will be executed on the calling thread after the
66 // request completes.
67 void Send(const SendConfig& config, RequestDoneCallback callback);
68 
69 // Removes query and/or fragment components from |url|.
70 std::string GetPathURL(const std::string& url);
71 
72 // Creates a new resource handler that returns the specified response.
73 CefRefPtr<CefResourceHandler> CreateResourceHandler(
74     CefRefPtr<CefResponse> response,
75     const std::string& response_data);
76 
77 using CookieVector = std::vector<CefCookie>;
78 using CookieDoneCallback =
79     base::OnceCallback<void(const CookieVector& cookies)>;
80 
81 // Retrieves all cookies from |manager| and executes |callback| upon completion.
82 // If |deleteCookies| is true the cookies will also be deleted.
83 void GetAllCookies(CefRefPtr<CefCookieManager> manager,
84                    bool deleteCookies,
85                    CookieDoneCallback callback);
86 
87 // Retrieves URL cookies from |manager| and executes |callback| upon completion.
88 // If |deleteCookies| is true the cookies will also be deleted.
89 void GetUrlCookies(CefRefPtr<CefCookieManager> manager,
90                    const CefString& url,
91                    bool includeHttpOnly,
92                    bool deleteCookies,
93                    CookieDoneCallback callback);
94 
95 }  // namespace test_request
96 
97 #endif  // CEF_TESTS_CEFTESTS_TEST_REQUEST_H_
98