url_data_manager_ios.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2014 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 IOS_WEB_WEBUI_URL_DATA_MANAGER_IOS_H_
  5. #define IOS_WEB_WEBUI_URL_DATA_MANAGER_IOS_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/memory/ref_counted.h"
  9. #include "base/supports_user_data.h"
  10. namespace web {
  11. class BrowserState;
  12. class URLDataSourceIOS;
  13. class URLDataSourceIOSImpl;
  14. class WebUIIOSDataSource;
  15. // To serve dynamic data off of chrome: URLs, implement the
  16. // URLDataManagerIOS::DataSource interface and register your handler
  17. // with AddDataSource. DataSources must be added on the UI thread (they are also
  18. // deleted on the UI thread). Internally the DataSources are maintained by
  19. // URLDataManagerIOSBackend, see it for details.
  20. class URLDataManagerIOS : public base::SupportsUserData::Data {
  21. public:
  22. explicit URLDataManagerIOS(BrowserState* browser_state);
  23. URLDataManagerIOS(const URLDataManagerIOS&) = delete;
  24. URLDataManagerIOS& operator=(const URLDataManagerIOS&) = delete;
  25. ~URLDataManagerIOS() override;
  26. // Adds a DataSource to the collection of data sources. This *must* be invoked
  27. // on the UI thread.
  28. //
  29. // If |AddDataSource| is called more than once for a particular name it will
  30. // release the old |DataSource|, most likely resulting in it getting deleted
  31. // as there are no other references to it. |DataSource| uses the
  32. // |DeleteOnUIThread| trait to insure that the destructor is called on the UI
  33. // thread. This is necessary as some |DataSource|s notably |FileIconSource|
  34. // and |FaviconSource|, have members that will DCHECK if they are not
  35. // destructed in the same thread as they are constructed (the UI thread).
  36. void AddDataSource(URLDataSourceIOSImpl* source);
  37. // Deletes any data sources no longer referenced. This is normally invoked
  38. // for you, but can be invoked to force deletion (such as during shutdown).
  39. static void DeleteDataSources();
  40. // Convenience wrapper function to add |source| to |browser_context|'s
  41. // |URLDataManagerIOS|. Creates a URLDataSourceIOSImpl to wrap the given
  42. // source.
  43. static void AddDataSource(BrowserState* browser_context,
  44. URLDataSourceIOS* source);
  45. // Adds a WebUI data source to |browser_context|'s |URLDataManagerIOS|.
  46. static void AddWebUIIOSDataSource(BrowserState* browser_state,
  47. WebUIIOSDataSource* source);
  48. private:
  49. friend class URLDataSourceIOSImpl;
  50. friend struct DeleteURLDataSourceIOS;
  51. typedef std::vector<const URLDataSourceIOSImpl*> URLDataSources;
  52. // Invoked on the IO thread to do the actual adding of the DataSource.
  53. static void AddDataSourceOnIOThread(
  54. BrowserState* browser_state,
  55. scoped_refptr<URLDataSourceIOSImpl> data_source);
  56. // If invoked on the UI thread the DataSource is deleted immediatlye,
  57. // otherwise it is added to |data_sources_| and a task is scheduled to handle
  58. // deletion on the UI thread. See note abouve DeleteDataSource for more info.
  59. static void DeleteDataSource(const URLDataSourceIOSImpl* data_source);
  60. // Returns true if |data_source| is scheduled for deletion (|DeleteDataSource|
  61. // was invoked).
  62. static bool IsScheduledForDeletion(const URLDataSourceIOSImpl* data_source);
  63. BrowserState* browser_state_;
  64. // |data_sources_| that are no longer referenced and scheduled for deletion.
  65. // Protected by g_delete_lock in the .cc file.
  66. static URLDataSources* data_sources_;
  67. };
  68. } // namespace web
  69. #endif // IOS_WEB_WEBUI_URL_DATA_MANAGER_IOS_H_