web_ui.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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_PUBLIC_BROWSER_WEB_UI_H_
6#define CONTENT_PUBLIC_BROWSER_WEB_UI_H_
7
8#include <vector>
9
10#include "base/basictypes.h"
11#include "base/callback.h"
12#include "base/strings/string16.h"
13#include "content/common/content_export.h"
14#include "content/public/common/page_transition_types.h"
15#include "ui/base/layout.h"
16
17class GURL;
18
19namespace base {
20class ListValue;
21class Value;
22}
23
24namespace content {
25
26class WebContents;
27class WebUIController;
28class WebUIMessageHandler;
29
30// A WebUI sets up the datasources and message handlers for a given HTML-based
31// UI.
32class CONTENT_EXPORT WebUI {
33 public:
34  // An opaque identifier used to identify a WebUI. This can only be compared to
35  // kNoWebUI or other WebUI types. See GetWebUIType.
36  typedef void* TypeID;
37
38  // A special WebUI type that signifies that a given page would not use the
39  // Web UI system.
40  static const TypeID kNoWebUI;
41
42  // Returns JavaScript code that, when executed, calls the function specified
43  // by |function_name| with the arguments specified in |arg_list|.
44  static base::string16 GetJavascriptCall(
45      const std::string& function_name,
46      const std::vector<const base::Value*>& arg_list);
47
48  virtual ~WebUI() {}
49
50  virtual WebContents* GetWebContents() const = 0;
51
52  virtual WebUIController* GetController() const = 0;
53  virtual void SetController(WebUIController* controller) = 0;
54
55  // Returns the device scale factor of the monitor that the renderer is on.
56  // Whenever possible, WebUI should push resources with this scale factor to
57  // Javascript.
58  virtual ui::ScaleFactor GetDeviceScaleFactor() const = 0;
59
60  // Gets a custom tab title provided by the Web UI. If there is no title
61  // override, the string will be empty which should trigger the default title
62  // behavior for the tab.
63  virtual const base::string16& GetOverriddenTitle() const = 0;
64  virtual void OverrideTitle(const base::string16& title) = 0;
65
66  // Returns the transition type that should be used for link clicks on this
67  // Web UI. This will default to LINK but may be overridden.
68  virtual PageTransition GetLinkTransitionType() const = 0;
69  virtual void SetLinkTransitionType(PageTransition type) = 0;
70
71  // Allows a controller to override the BindingsPolicy that should be enabled
72  // for this page.
73  virtual int GetBindings() const = 0;
74  virtual void SetBindings(int bindings) = 0;
75
76  // Sets the path for the iframe if this WebUI is embedded in a page.
77  virtual void SetFrameXPath(const std::string& xpath) = 0;
78
79  // Takes ownership of |handler|, which will be destroyed when the WebUI is.
80  virtual void AddMessageHandler(WebUIMessageHandler* handler) = 0;
81
82  // Used by WebUIMessageHandlers. If the given message is already registered,
83  // the call has no effect unless |register_callback_overwrites_| is set to
84  // true.
85  typedef base::Callback<void(const base::ListValue*)> MessageCallback;
86  virtual void RegisterMessageCallback(const std::string& message,
87                                       const MessageCallback& callback) = 0;
88
89  // This is only needed if an embedder overrides handling of a WebUIMessage and
90  // then later wants to undo that, or to route it to a different WebUI object.
91  virtual void ProcessWebUIMessage(const GURL& source_url,
92                                   const std::string& message,
93                                   const base::ListValue& args) = 0;
94
95  // Call a Javascript function by sending its name and arguments down to
96  // the renderer.  This is asynchronous; there's no way to get the result
97  // of the call, and should be thought of more like sending a message to
98  // the page.
99  // All function names in WebUI must consist of only ASCII characters.
100  // There are variants for calls with more arguments.
101  virtual void CallJavascriptFunction(const std::string& function_name) = 0;
102  virtual void CallJavascriptFunction(const std::string& function_name,
103                                      const base::Value& arg) = 0;
104  virtual void CallJavascriptFunction(const std::string& function_name,
105                                      const base::Value& arg1,
106                                      const base::Value& arg2) = 0;
107  virtual void CallJavascriptFunction(const std::string& function_name,
108                                      const base::Value& arg1,
109                                      const base::Value& arg2,
110                                      const base::Value& arg3) = 0;
111  virtual void CallJavascriptFunction(const std::string& function_name,
112                                      const base::Value& arg1,
113                                      const base::Value& arg2,
114                                      const base::Value& arg3,
115                                      const base::Value& arg4) = 0;
116  virtual void CallJavascriptFunction(
117      const std::string& function_name,
118      const std::vector<const base::Value*>& args) = 0;
119};
120
121}  // namespace content
122
123#endif  // CONTENT_PUBLIC_BROWSER_WEB_UI_H_
124