web_ui.h revision effb81e5f8246d0db0270817048dc992db66e9fb
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 // Overrides which frame gets JavaScript messages; this is useful if this 77 // WebUI is embedded in a page. If no override is set, the main frame will 78 // receive the JavaScript messages. 79 virtual void OverrideJavaScriptFrame(const std::string& frame_name) = 0; 80 81 // Takes ownership of |handler|, which will be destroyed when the WebUI is. 82 virtual void AddMessageHandler(WebUIMessageHandler* handler) = 0; 83 84 // Used by WebUIMessageHandlers. If the given message is already registered, 85 // the call has no effect unless |register_callback_overwrites_| is set to 86 // true. 87 typedef base::Callback<void(const base::ListValue*)> MessageCallback; 88 virtual void RegisterMessageCallback(const std::string& message, 89 const MessageCallback& callback) = 0; 90 91 // This is only needed if an embedder overrides handling of a WebUIMessage and 92 // then later wants to undo that, or to route it to a different WebUI object. 93 virtual void ProcessWebUIMessage(const GURL& source_url, 94 const std::string& message, 95 const base::ListValue& args) = 0; 96 97 // Call a Javascript function by sending its name and arguments down to 98 // the renderer. This is asynchronous; there's no way to get the result 99 // of the call, and should be thought of more like sending a message to 100 // the page. 101 // All function names in WebUI must consist of only ASCII characters. 102 // There are variants for calls with more arguments. 103 virtual void CallJavascriptFunction(const std::string& function_name) = 0; 104 virtual void CallJavascriptFunction(const std::string& function_name, 105 const base::Value& arg) = 0; 106 virtual void CallJavascriptFunction(const std::string& function_name, 107 const base::Value& arg1, 108 const base::Value& arg2) = 0; 109 virtual void CallJavascriptFunction(const std::string& function_name, 110 const base::Value& arg1, 111 const base::Value& arg2, 112 const base::Value& arg3) = 0; 113 virtual void CallJavascriptFunction(const std::string& function_name, 114 const base::Value& arg1, 115 const base::Value& arg2, 116 const base::Value& arg3, 117 const base::Value& arg4) = 0; 118 virtual void CallJavascriptFunction( 119 const std::string& function_name, 120 const std::vector<const base::Value*>& args) = 0; 121}; 122 123} // namespace content 124 125#endif // CONTENT_PUBLIC_BROWSER_WEB_UI_H_ 126