#ifndef CONTENT_BROWSER_BROWSING_DATA_CLEAR_SITE_DATA_HANDLER_H_
#define CONTENT_BROWSER_BROWSING_DATA_CLEAR_SITE_DATA_HANDLER_H_
#include <memory>
#include <string>
#include <vector>
#include "base/functional/callback.h"
#include "base/time/time.h"
#include "content/common/content_export.h"
#include "net/cookies/cookie_partition_key.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/devtools/console_message.mojom.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace content {
class WebContents;
class BrowserContext;
class CONTENT_EXPORT ClearSiteDataHandler {
public:
class CONTENT_EXPORT ConsoleMessagesDelegate {
public:
struct Message {
GURL url;
std::string text;
blink::mojom::ConsoleMessageLevel level;
};
using OutputFormattedMessageFunction =
base::RepeatingCallback<void(WebContents*,
blink::mojom::ConsoleMessageLevel,
const std::string&)>;
ConsoleMessagesDelegate();
virtual ~ConsoleMessagesDelegate();
virtual void AddMessage(const GURL& url,
const std::string& text,
blink::mojom::ConsoleMessageLevel level);
virtual void OutputMessages(
const base::RepeatingCallback<WebContents*()>& web_contents_getter);
const std::vector<Message>& GetMessagesForTesting() const {
return messages_;
}
protected:
void SetOutputFormattedMessageFunctionForTesting(
const OutputFormattedMessageFunction& function);
private:
std::vector<Message> messages_;
OutputFormattedMessageFunction output_formatted_message_function_;
};
ClearSiteDataHandler(const ClearSiteDataHandler&) = delete;
ClearSiteDataHandler& operator=(const ClearSiteDataHandler&) = delete;
static void HandleHeader(
base::RepeatingCallback<BrowserContext*()> browser_context_getter,
base::RepeatingCallback<WebContents*()> web_contents_getter,
const GURL& url,
const std::string& header_value,
int load_flags,
const absl::optional<net::CookiePartitionKey>& cookie_partition_key,
const absl::optional<blink::StorageKey>& storage_key,
bool partitioned_state_allowed_only,
base::OnceClosure callback);
static bool ParseHeaderForTesting(
const std::string& header,
bool* clear_cookies,
bool* clear_storage,
bool* clear_cache,
std::set<std::string>* storage_buckets_to_remove,
ConsoleMessagesDelegate* delegate,
const GURL& current_url);
protected:
ClearSiteDataHandler(
base::RepeatingCallback<BrowserContext*()> browser_context_getter,
base::RepeatingCallback<WebContents*()> web_contents_getter,
const GURL& url,
const std::string& header_value,
int load_flags,
const absl::optional<net::CookiePartitionKey>& cookie_partition_key,
const absl::optional<blink::StorageKey>& storage_key,
bool partitioned_state_allowed_only,
base::OnceClosure callback,
std::unique_ptr<ConsoleMessagesDelegate> delegate);
virtual ~ClearSiteDataHandler();
bool HandleHeaderAndOutputConsoleMessages();
bool Run();
static bool ParseHeader(const std::string& header,
bool* clear_cookies,
bool* clear_storage,
bool* clear_cache,
std::set<std::string>* storage_buckets_to_remove,
ConsoleMessagesDelegate* delegate,
const GURL& current_url);
virtual void ExecuteClearingTask(
const url::Origin& origin,
bool clear_cookies,
bool clear_storage,
bool clear_cache,
const std::set<std::string>& storage_buckets_to_remove,
base::OnceClosure callback);
static void TaskFinished(
base::TimeTicks clearing_started,
std::unique_ptr<ConsoleMessagesDelegate> delegate,
base::RepeatingCallback<WebContents*()> web_contents_getter,
base::OnceClosure callback);
void OutputConsoleMessages();
void RunCallbackNotDeferred();
const GURL& GetURLForTesting();
const absl::optional<net::CookiePartitionKey> CookiePartitionKeyForTesting()
const {
return cookie_partition_key_;
}
const absl::optional<blink::StorageKey> StorageKeyForTesting() const {
return storage_key_;
}
bool PartitionedStateOnlyForTesting() const {
return partitioned_state_allowed_only_;
}
private:
base::RepeatingCallback<BrowserContext*()> browser_context_getter_;
base::RepeatingCallback<WebContents*()> web_contents_getter_;
const GURL url_;
const std::string header_value_;
const int load_flags_;
const absl::optional<net::CookiePartitionKey> cookie_partition_key_;
const absl::optional<blink::StorageKey> storage_key_;
const bool partitioned_state_allowed_only_;
base::OnceClosure callback_;
std::unique_ptr<ConsoleMessagesDelegate> delegate_;
};
}
#endif