PageRenderTime 179ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/content/browser/content_index/content_index_database.h

https://github.com/chromium/chromium
C Header | 191 lines | 131 code | 37 blank | 23 comment | 0 complexity | d3cd1141d99191ee44afff2c9f2f2907 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  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. #ifndef CONTENT_BROWSER_CONTENT_INDEX_CONTENT_INDEX_DATABASE_H_
  5. #define CONTENT_BROWSER_CONTENT_INDEX_CONTENT_INDEX_DATABASE_H_
  6. #include "base/containers/flat_map.h"
  7. #include "base/gtest_prod_util.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "content/browser/service_worker/service_worker_context_wrapper.h"
  12. #include "content/common/content_export.h"
  13. #include "content/public/browser/content_index_context.h"
  14. #include "content/public/browser/content_index_provider.h"
  15. #include "third_party/blink/public/mojom/content_index/content_index.mojom.h"
  16. class GURL;
  17. namespace url {
  18. class Origin;
  19. } // namespace url
  20. namespace content {
  21. namespace proto {
  22. class SerializedIcons;
  23. } // namespace proto
  24. class BrowserContext;
  25. // Handles interacting with the Service Worker Database for Content Index
  26. // entries. This is owned by the ContentIndexContext.
  27. class CONTENT_EXPORT ContentIndexDatabase {
  28. public:
  29. ContentIndexDatabase(
  30. BrowserContext* browser_context,
  31. scoped_refptr<ServiceWorkerContextWrapper> service_worker_context);
  32. ContentIndexDatabase(const ContentIndexDatabase&) = delete;
  33. ContentIndexDatabase& operator=(const ContentIndexDatabase&) = delete;
  34. ~ContentIndexDatabase();
  35. void AddEntry(int64_t service_worker_registration_id,
  36. const url::Origin& origin,
  37. bool is_top_level_context,
  38. blink::mojom::ContentDescriptionPtr description,
  39. const std::vector<SkBitmap>& icons,
  40. const GURL& launch_url,
  41. blink::mojom::ContentIndexService::AddCallback callback);
  42. void DeleteEntry(int64_t service_worker_registration_id,
  43. const url::Origin& origin,
  44. const std::string& entry_id,
  45. blink::mojom::ContentIndexService::DeleteCallback callback);
  46. void GetDescriptions(
  47. int64_t service_worker_registration_id,
  48. const url::Origin& origin,
  49. blink::mojom::ContentIndexService::GetDescriptionsCallback callback);
  50. // Gets the icon for |description_id| and invokes |callback| on the UI
  51. // thread.
  52. void GetIcons(int64_t service_worker_registration_id,
  53. const std::string& description_id,
  54. ContentIndexContext::GetIconsCallback callback);
  55. // Returns all registered entries.
  56. void GetAllEntries(ContentIndexContext::GetAllEntriesCallback callback);
  57. // Returns the specified entry.
  58. void GetEntry(int64_t service_worker_registration_id,
  59. const std::string& description_id,
  60. ContentIndexContext::GetEntryCallback callback);
  61. // Deletes the entry and dispatches an event.
  62. void DeleteItem(int64_t service_worker_registration_id,
  63. const url::Origin& origin,
  64. const std::string& description_id);
  65. // Called when the storage partition is shutting down.
  66. void Shutdown();
  67. private:
  68. FRIEND_TEST_ALL_PREFIXES(ContentIndexDatabaseTest,
  69. BlockedOriginsCannotRegisterContent);
  70. FRIEND_TEST_ALL_PREFIXES(ContentIndexDatabaseTest, UmaRecorded);
  71. void DeleteEntryImpl(
  72. int64_t service_worker_registration_id,
  73. const url::Origin& origin,
  74. const std::string& entry_id,
  75. blink::mojom::ContentIndexService::DeleteCallback callback);
  76. // Add Callbacks.
  77. void DidSerializeIcons(
  78. int64_t service_worker_registration_id,
  79. const url::Origin& origin,
  80. bool is_top_level_context,
  81. blink::mojom::ContentDescriptionPtr description,
  82. const GURL& launch_url,
  83. std::unique_ptr<proto::SerializedIcons> serialized_icons,
  84. blink::mojom::ContentIndexService::AddCallback callback);
  85. void DidAddEntry(blink::mojom::ContentIndexService::AddCallback callback,
  86. ContentIndexEntry entry,
  87. blink::ServiceWorkerStatusCode status);
  88. // Delete Callbacks.
  89. void DidDeleteEntry(
  90. int64_t service_worker_registration_id,
  91. const url::Origin& origin,
  92. const std::string& entry_id,
  93. blink::mojom::ContentIndexService::DeleteCallback callback,
  94. blink::ServiceWorkerStatusCode status);
  95. // GetDescriptions Callbacks.
  96. void DidGetDescriptions(
  97. int64_t service_worker_registration_id,
  98. blink::mojom::ContentIndexService::GetDescriptionsCallback callback,
  99. const std::vector<std::string>& data,
  100. blink::ServiceWorkerStatusCode status);
  101. // GetIcons Callbacks.
  102. void DidGetSerializedIcons(int64_t service_worker_registration_id,
  103. ContentIndexContext::GetIconsCallback callback,
  104. const std::vector<std::string>& data,
  105. blink::ServiceWorkerStatusCode status);
  106. void DidDeserializeIcons(ContentIndexContext::GetIconsCallback callback,
  107. std::unique_ptr<std::vector<SkBitmap>> icons);
  108. // GetEntries Callbacks.
  109. void DidGetEntries(
  110. ContentIndexContext::GetAllEntriesCallback callback,
  111. const std::vector<std::pair<int64_t, std::string>>& user_data,
  112. blink::ServiceWorkerStatusCode status);
  113. // GetEntry Callbacks.
  114. void DidGetEntry(int64_t service_worker_registration_id,
  115. ContentIndexContext::GetEntryCallback callback,
  116. const std::vector<std::string>& data,
  117. blink::ServiceWorkerStatusCode status);
  118. // DeleteItem Callbacks.
  119. void DidDeleteItem(int64_t service_worker_registration_id,
  120. const url::Origin& origin,
  121. const std::string& description_id,
  122. blink::mojom::ContentIndexError error);
  123. void StartActiveWorkerForDispatch(
  124. const std::string& description_id,
  125. blink::ServiceWorkerStatusCode service_worker_status,
  126. scoped_refptr<ServiceWorkerRegistration> registration);
  127. void DeliverMessageToWorker(
  128. scoped_refptr<ServiceWorkerVersion> service_worker,
  129. scoped_refptr<ServiceWorkerRegistration> registration,
  130. const std::string& description_id,
  131. blink::ServiceWorkerStatusCode service_worker_status);
  132. void DidDispatchEvent(const url::Origin& origin,
  133. blink::ServiceWorkerStatusCode service_worker_status);
  134. // Clears all the content index related data in a service worker.
  135. void ClearServiceWorkerDataOnCorruption(
  136. int64_t service_worker_registration_id);
  137. // Callbacks to notify |provider_| of updates.
  138. void NotifyProviderContentAdded(std::vector<ContentIndexEntry> entries);
  139. void NotifyProviderContentDeleted(int64_t service_worker_registration_id,
  140. const url::Origin& origin,
  141. const std::string& entry_id);
  142. // Block/Unblock DB operations for |origin|.
  143. void BlockOrigin(const url::Origin& origin);
  144. void UnblockOrigin(const url::Origin& origin);
  145. raw_ptr<ContentIndexProvider> provider_;
  146. // A map from origins to how many times it's been blocked.
  147. base::flat_map<url::Origin, int> blocked_origins_;
  148. scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
  149. // This class lives on the UI thread.
  150. SEQUENCE_CHECKER(sequence_checker_);
  151. base::WeakPtrFactory<ContentIndexDatabase> weak_ptr_factory_{this};
  152. };
  153. } // namespace content
  154. #endif // CONTENT_BROWSER_CONTENT_INDEX_CONTENT_INDEX_DATABASE_H_