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