web_ui_data_source.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
1// Copyright (c) 2013 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_PUBLIC_BROWSER_WEB_UI_DATA_SOURCE_H_
6#define CONTENT_PUBLIC_BROWSER_WEB_UI_DATA_SOURCE_H_
7
8#include "base/callback.h"
9#include "base/strings/string16.h"
10#include "content/common/content_export.h"
11
12namespace base {
13class DictionaryValue;
14class RefCountedMemory;
15}
16
17namespace content {
18class BrowserContext;
19
20// A data source that can help with implementing the common operations needed by
21// WebUI pages.
22class WebUIDataSource {
23 public:
24  virtual ~WebUIDataSource() {}
25
26  CONTENT_EXPORT static WebUIDataSource* Create(const std::string& source_name);
27
28  // Adds a WebUI data source to |browser_context|.
29  CONTENT_EXPORT static void Add(BrowserContext* browser_context,
30                                 WebUIDataSource* source);
31
32  // Adds a string keyed to its name to our dictionary.
33  virtual void AddString(const std::string& name,
34                         const base::string16& value) = 0;
35
36  // Adds a string keyed to its name to our dictionary.
37  virtual void AddString(const std::string& name, const std::string& value) = 0;
38
39  // Adds a localized string with resource |ids| keyed to its name to our
40  // dictionary.
41  virtual void AddLocalizedString(const std::string& name, int ids) = 0;
42
43  // Add strings from |localized_strings| to our dictionary.
44  virtual void AddLocalizedStrings(
45      const base::DictionaryValue& localized_strings) = 0;
46
47  // Adds a boolean keyed to its name to our dictionary.
48  virtual void AddBoolean(const std::string& name, bool value) = 0;
49
50  // Sets the path which will return the JSON strings.
51  virtual void SetJsonPath(const std::string& path) = 0;
52
53  // Sets the data source to use a slightly different format for json data. Some
54  // day this should become the default.
55  virtual void SetUseJsonJSFormatV2() = 0;
56
57  // Adds a mapping between a path name and a resource to return.
58  virtual void AddResourcePath(const std::string &path, int resource_id) = 0;
59
60  // Sets the resource to returned when no other paths match.
61  virtual void SetDefaultResource(int resource_id) = 0;
62
63  // Used as a parameter to GotDataCallback. The caller has to run this callback
64  // with the result for the path that they filtered, passing ownership of the
65  // memory.
66  typedef base::Callback<void(base::RefCountedMemory*)> GotDataCallback;
67
68  // Used by SetRequestFilter. The string parameter is the path of the request.
69  // If the callee doesn't want to handle the data, false is returned. Otherwise
70  // true is returned and the GotDataCallback parameter is called either then or
71  // asynchronously with the response.
72  typedef base::Callback<bool(const std::string&, const GotDataCallback&)>
73      HandleRequestCallback;
74
75  // Allows a caller to add a filter for URL requests.
76  virtual void SetRequestFilter(const HandleRequestCallback& callback) = 0;
77
78  // The following map to methods on URLDataSource. See the documentation there.
79  // NOTE: it's not acceptable to call DisableContentSecurityPolicy for new
80  // pages, see URLDataSource::ShouldAddContentSecurityPolicy and talk to
81  // tsepez.
82
83  // Currently only used by embedders for WebUIs with multiple instances.
84  virtual void DisableReplaceExistingSource() = 0;
85  virtual void DisableContentSecurityPolicy() = 0;
86  virtual void OverrideContentSecurityPolicyObjectSrc(
87      const std::string& data) = 0;
88  virtual void OverrideContentSecurityPolicyFrameSrc(
89      const std::string& data) = 0;
90  virtual void DisableDenyXFrameOptions() = 0;
91};
92
93}  // namespace content
94
95#endif  // CONTENT_PUBLIC_BROWSER_WEB_UI_DATA_SOURCE_H_
96