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