BlobURLStore.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. * Copyright (c) 2024, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/StringBuilder.h>
  8. #include <LibURL/URL.h>
  9. #include <LibWeb/Crypto/Crypto.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/FileAPI/Blob.h>
  12. #include <LibWeb/FileAPI/BlobURLStore.h>
  13. #include <LibWeb/HTML/Origin.h>
  14. #include <LibWeb/HTML/Scripting/Environments.h>
  15. namespace Web::FileAPI {
  16. BlobURLStore& blob_url_store()
  17. {
  18. static HashMap<String, BlobURLEntry> store;
  19. return store;
  20. }
  21. // https://w3c.github.io/FileAPI/#unicodeBlobURL
  22. ErrorOr<String> generate_new_blob_url()
  23. {
  24. // 1. Let result be the empty string.
  25. StringBuilder result;
  26. // 2. Append the string "blob:" to result.
  27. TRY(result.try_append("blob:"sv));
  28. // 3. Let settings be the current settings object
  29. auto& settings = HTML::current_settings_object();
  30. // 4. Let origin be settings’s origin.
  31. auto origin = settings.origin();
  32. // 5. Let serialized be the ASCII serialization of origin.
  33. auto serialized = origin.serialize();
  34. // 6. If serialized is "null", set it to an implementation-defined value.
  35. if (serialized == "null"sv)
  36. serialized = "ladybird"sv;
  37. // 7. Append serialized to result.
  38. TRY(result.try_append(serialized));
  39. // 8. Append U+0024 SOLIDUS (/) to result.
  40. TRY(result.try_append('/'));
  41. // 9. Generate a UUID [RFC4122] as a string and append it to result.
  42. auto uuid = TRY(Crypto::generate_random_uuid());
  43. TRY(result.try_append(uuid));
  44. // 10. Return result.
  45. return result.to_string();
  46. }
  47. // https://w3c.github.io/FileAPI/#add-an-entry
  48. ErrorOr<String> add_entry_to_blob_url_store(JS::NonnullGCPtr<Blob> object)
  49. {
  50. // 1. Let store be the user agent’s blob URL store.
  51. auto& store = blob_url_store();
  52. // 2. Let url be the result of generating a new blob URL.
  53. auto url = TRY(generate_new_blob_url());
  54. // 3. Let entry be a new blob URL entry consisting of object and the current settings object.
  55. BlobURLEntry entry { object, HTML::current_settings_object() };
  56. // 4. Set store[url] to entry.
  57. TRY(store.try_set(url, move(entry)));
  58. // 5. Return url.
  59. return url;
  60. }
  61. // https://w3c.github.io/FileAPI/#removeTheEntry
  62. ErrorOr<void> remove_entry_from_blob_url_store(StringView url)
  63. {
  64. // 1. Let store be the user agent’s blob URL store;
  65. auto& store = blob_url_store();
  66. // 2. Let url string be the result of serializing url.
  67. auto url_string = TRY(URL::URL { url }.to_string());
  68. // 3. Remove store[url string].
  69. store.remove(url_string);
  70. return {};
  71. }
  72. // https://w3c.github.io/FileAPI/#lifeTime
  73. void run_unloading_cleanup_steps(JS::NonnullGCPtr<DOM::Document> document)
  74. {
  75. // 1. Let environment be the Document's relevant settings object.
  76. auto& environment = document->relevant_settings_object();
  77. // 2. Let store be the user agent’s blob URL store;
  78. auto& store = FileAPI::blob_url_store();
  79. // 3. Remove from store any entries for which the value's environment is equal to environment.
  80. store.remove_all_matching([&](auto&, auto& value) {
  81. return value.environment == &environment;
  82. });
  83. }
  84. }