content_script_injection_url_getter.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2021 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. #ifndef EXTENSIONS_COMMON_CONTENT_SCRIPT_INJECTION_URL_GETTER_H_
  5. #define EXTENSIONS_COMMON_CONTENT_SCRIPT_INJECTION_URL_GETTER_H_
  6. #include <memory>
  7. #include "extensions/common/script_constants.h"
  8. #include "url/gurl.h"
  9. #include "url/origin.h"
  10. namespace extensions {
  11. // A helper for deciding which URL to use for deciding whether to inject a
  12. // content script - it finds the effective document URL by (depending on content
  13. // script options) possibly looking at the parent-or-opener document instead,
  14. // looking at the precursor origin of data: documents, etc.
  15. //
  16. // TODO(https://crbug.com/1186321): Content script injection assumes that
  17. // about:blank inherits origin from the parent. This can return the incorrect
  18. // result, e.g. if a parent frame navigates a grandchild frame to about:blank.
  19. class ContentScriptInjectionUrlGetter {
  20. public:
  21. // Only static methods.
  22. ContentScriptInjectionUrlGetter() = delete;
  23. // Adapter abstracting away differences between RenderFrameHost and
  24. // RenderFrame.
  25. class FrameAdapter {
  26. public:
  27. virtual ~FrameAdapter();
  28. virtual std::unique_ptr<FrameAdapter> Clone() const = 0;
  29. virtual std::unique_ptr<FrameAdapter> GetLocalParentOrOpener() const = 0;
  30. virtual GURL GetUrl() const = 0;
  31. virtual url::Origin GetOrigin() const = 0;
  32. virtual bool CanAccess(const url::Origin& target) const = 0;
  33. virtual bool CanAccess(const FrameAdapter& target) const = 0;
  34. virtual uintptr_t GetId() const = 0;
  35. };
  36. static GURL Get(const FrameAdapter& frame,
  37. const GURL& document_url,
  38. MatchOriginAsFallbackBehavior match_origin_as_fallback,
  39. bool allow_inaccessible_parents);
  40. };
  41. } // namespace extensions
  42. #endif // EXTENSIONS_COMMON_CONTENT_SCRIPT_INJECTION_URL_GETTER_H_