Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(592)

Side by Side Diff: content/browser/webui/web_ui.h

Issue 9224002: Make WebUI objects not derive from WebUI. WebUI objects own the controller. This is the ownership... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: sync to head to clear linux_chromeos browsertest failures Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CONTENT_BROWSER_WEBUI_WEB_UI_H_ 5 #ifndef CONTENT_BROWSER_WEBUI_WEB_UI_H_
6 #define CONTENT_BROWSER_WEBUI_WEB_UI_H_ 6 #define CONTENT_BROWSER_WEBUI_WEB_UI_H_
7 #pragma once 7 #pragma once
8 8
9 #include <map> 9 #include <map>
10 #include <string> 10 #include <string>
(...skipping 20 matching lines...) Expand all
31 class WebUIMessageHandler; 31 class WebUIMessageHandler;
32 } 32 }
33 33
34 // A WebUI sets up the datasources and message handlers for a given HTML-based 34 // A WebUI sets up the datasources and message handlers for a given HTML-based
35 // UI. 35 // UI.
36 // 36 //
37 // NOTE: If you're creating a new WebUI for Chrome code, make sure you extend 37 // NOTE: If you're creating a new WebUI for Chrome code, make sure you extend
38 // ChromeWebUI. 38 // ChromeWebUI.
39 class CONTENT_EXPORT WebUI : public IPC::Channel::Listener { 39 class CONTENT_EXPORT WebUI : public IPC::Channel::Listener {
40 public: 40 public:
41 WebUI(content::WebContents* contents, content::WebUIController* controller); 41 explicit WebUI(content::WebContents* contents);
42 virtual ~WebUI(); 42 virtual ~WebUI();
43 43
44 // IPC::Channel::Listener implementation: 44 // IPC::Channel::Listener implementation:
45 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 45 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
46 // TODO(jam): move to private 46 // TODO(jam): move to private
47 // IPC message handling. 47 // IPC message handling.
48 void OnWebUISend(const GURL& source_url, 48 void OnWebUISend(const GURL& source_url,
49 const std::string& message, 49 const std::string& message,
50 const base::ListValue& args); 50 const base::ListValue& args);
51 51
52 // Called by TabContents when the RenderView is first created. This is *not* 52 // Called by TabContents when the RenderView is first created. This is *not*
53 // called for every page load because in some cases RenderViewHostManager will 53 // called for every page load because in some cases RenderViewHostManager will
54 // reuse RenderView instances. 54 // reuse RenderView instances.
55 void RenderViewCreated(RenderViewHost* render_view_host); 55 void RenderViewCreated(RenderViewHost* render_view_host);
56 56
57 // TODO(jam): below is what will be the public API of WebUI
58 content::WebContents* GetWebContents() const;
59
60 // Returns true if the favicon should be hidden for the current tab.
61 bool ShouldHideFavicon() const;
62 void HideFavicon();
63
64 // Returns true if the location bar should be focused by default rather than
65 // the page contents. Some pages will want to use this to encourage the user
66 // to type in the URL bar.
67 bool ShouldFocusLocationBarByDefault() const;
68 void FocusLocationBarByDefault();
69
70 // Returns true if the page's URL should be hidden. Some Web UI pages
71 // like the new tab page will want to hide it.
72 bool ShouldHideURL() const;
73 void HideURL();
74
75 // Gets a custom tab title provided by the Web UI. If there is no title
76 // override, the string will be empty which should trigger the default title
77 // behavior for the tab.
78 const string16& GetOverriddenTitle() const;
79 void OverrideTitle(const string16& title);
80
81 // Returns the transition type that should be used for link clicks on this
82 // Web UI. This will default to LINK but may be overridden.
83 content::PageTransition GetLinkTransitionType() const;
Evan Stade 2012/01/17 19:14:56 c-style getters and setters are not used in the co
jam 2012/01/17 19:20:45 right, because the API will be interfaces, so c-st
84 void SetLinkTransitionType(content::PageTransition type);
85
86 // Allows a controller to override the BindingsPolicy that should be enabled
87 // for this page.
88 int GetBindings() const;
89 void SetBindings(int bindings);
90
91 // Sets the path for the iframe if this WebUI is embedded in a page.
92 void SetFrameXPath(const std::string& xpath);
93
94 // Takes ownership of |handler|, which will be destroyed when the WebUI is.
95 void AddMessageHandler(content::WebUIMessageHandler* handler);
96
97 // Execute a string of raw Javascript on the page. Overridable for
98 // testing purposes.
99 virtual void ExecuteJavascript(const string16& javascript);
100
57 // Used by WebUIMessageHandlers. If the given message is already registered, 101 // Used by WebUIMessageHandlers. If the given message is already registered,
58 // the call has no effect unless |register_callback_overwrites_| is set to 102 // the call has no effect unless |register_callback_overwrites_| is set to
59 // true. 103 // true.
60 typedef base::Callback<void(const base::ListValue*)> MessageCallback; 104 typedef base::Callback<void(const base::ListValue*)> MessageCallback;
61 void RegisterMessageCallback(const std::string& message, 105 void RegisterMessageCallback(const std::string& message,
62 const MessageCallback& callback); 106 const MessageCallback& callback);
63 107
64 // Returns true if the favicon should be hidden for the current tab.
65 bool hide_favicon() const {
66 return hide_favicon_;
67 }
68
69 // Returns true if the location bar should be focused by default rather than
70 // the page contents. Some pages will want to use this to encourage the user
71 // to type in the URL bar.
72 bool focus_location_bar_by_default() const {
73 return focus_location_bar_by_default_;
74 }
75
76 // Returns true if the page's URL should be hidden. Some Web UI pages
77 // like the new tab page will want to hide it.
78 bool should_hide_url() const {
79 return should_hide_url_;
80 }
81
82 // Gets a custom tab title provided by the Web UI. If there is no title
83 // override, the string will be empty which should trigger the default title
84 // behavior for the tab.
85 const string16& overridden_title() const {
86 return overridden_title_;
87 }
88
89 // Returns the transition type that should be used for link clicks on this
90 // Web UI. This will default to LINK but may be overridden.
91 content::PageTransition link_transition_type() const {
92 return link_transition_type_;
93 }
94
95 int bindings() const {
96 return bindings_;
97 }
98
99 // Indicates whether RegisterMessageCallback() will overwrite an existing 108 // Indicates whether RegisterMessageCallback() will overwrite an existing
100 // message callback mapping. Serves as the hook for test mocks. 109 // message callback mapping. Serves as the hook for test mocks.
101 bool register_callback_overwrites() const { 110 bool register_callback_overwrites() const {
102 return register_callback_overwrites_; 111 return register_callback_overwrites_;
103 } 112 }
104 113
105 void set_register_callback_overwrites(bool value) { 114 void set_register_callback_overwrites(bool value) {
106 register_callback_overwrites_ = value; 115 register_callback_overwrites_ = value;
107 } 116 }
108 117
109 void set_frame_xpath(const std::string& xpath) { 118 content::WebUIController* GetController() const;
110 frame_xpath_ = xpath; 119 void SetController(content::WebUIController* controller);
111 }
112
113 content::WebUIController* controller() const { return controller_; }
114 120
115 // Call a Javascript function by sending its name and arguments down to 121 // Call a Javascript function by sending its name and arguments down to
116 // the renderer. This is asynchronous; there's no way to get the result 122 // the renderer. This is asynchronous; there's no way to get the result
117 // of the call, and should be thought of more like sending a message to 123 // of the call, and should be thought of more like sending a message to
118 // the page. 124 // the page.
119 // All function names in WebUI must consist of only ASCII characters. 125 // All function names in WebUI must consist of only ASCII characters.
120 // There are variants for calls with more arguments. 126 // There are variants for calls with more arguments.
121 void CallJavascriptFunction(const std::string& function_name); 127 void CallJavascriptFunction(const std::string& function_name);
122 void CallJavascriptFunction(const std::string& function_name, 128 void CallJavascriptFunction(const std::string& function_name,
123 const base::Value& arg); 129 const base::Value& arg);
(...skipping 21 matching lines...) Expand all
145 // A special WebUI type that signifies that a given page would not use the 151 // A special WebUI type that signifies that a given page would not use the
146 // Web UI system. 152 // Web UI system.
147 static const TypeID kNoWebUI; 153 static const TypeID kNoWebUI;
148 154
149 // Returns JavaScript code that, when executed, calls the function specified 155 // Returns JavaScript code that, when executed, calls the function specified
150 // by |function_name| with the arguments specified in |arg_list|. 156 // by |function_name| with the arguments specified in |arg_list|.
151 static string16 GetJavascriptCall( 157 static string16 GetJavascriptCall(
152 const std::string& function_name, 158 const std::string& function_name,
153 const std::vector<const base::Value*>& arg_list); 159 const std::vector<const base::Value*>& arg_list);
154 160
155 protected: 161 private:
156 // Takes ownership of |handler|, which will be destroyed when the WebUI is. 162 // A map of message name -> message handling callback.
157 void AddMessageHandler(content::WebUIMessageHandler* handler); 163 typedef std::map<std::string, MessageCallback> MessageCallbackMap;
158 164 MessageCallbackMap message_callbacks_;
159 // Execute a string of raw Javascript on the page. Overridable for
160 // testing purposes.
161 virtual void ExecuteJavascript(const string16& javascript);
162 165
163 // Options that may be overridden by individual Web UI implementations. The 166 // Options that may be overridden by individual Web UI implementations. The
164 // bool options default to false. See the public getters for more information. 167 // bool options default to false. See the public getters for more information.
165 bool hide_favicon_; 168 bool hide_favicon_;
166 bool focus_location_bar_by_default_; 169 bool focus_location_bar_by_default_;
167 bool should_hide_url_; 170 bool should_hide_url_;
168 string16 overridden_title_; // Defaults to empty string. 171 string16 overridden_title_; // Defaults to empty string.
169 content::PageTransition link_transition_type_; // Defaults to LINK. 172 content::PageTransition link_transition_type_; // Defaults to LINK.
170 int bindings_; // The bindings from BindingsPolicy that should be enabled for 173 int bindings_; // The bindings from BindingsPolicy that should be enabled for
171 // this page. 174 // this page.
172 175
173 // Used by test mocks. See the public getters for more information. 176 // Used by test mocks. See the public getters for more information.
174 bool register_callback_overwrites_; // Defaults to false. 177 bool register_callback_overwrites_; // Defaults to false.
175 178
176 // The WebUIMessageHandlers we own. 179 // The WebUIMessageHandlers we own.
177 std::vector<content::WebUIMessageHandler*> handlers_; 180 std::vector<content::WebUIMessageHandler*> handlers_;
178 181
179 // Non-owning pointer to the WebContents this WebUI is associated with. 182 // Non-owning pointer to the WebContents this WebUI is associated with.
180 content::WebContents* web_contents_; 183 content::WebContents* web_contents_;
181 184
182 // TODO(jam): once WebUI objects aren't also WebUIController, make one own the
183 // other.
184 content::WebUIController* controller_;
185
186 private:
187 // A map of message name -> message handling callback.
188 typedef std::map<std::string, MessageCallback> MessageCallbackMap;
189 MessageCallbackMap message_callbacks_;
190
191 // The path for the iframe this WebUI is embedded in (empty if not in an 185 // The path for the iframe this WebUI is embedded in (empty if not in an
192 // iframe). 186 // iframe).
193 std::string frame_xpath_; 187 std::string frame_xpath_;
194 188
189 scoped_ptr<content::WebUIController> controller_;
190
195 DISALLOW_COPY_AND_ASSIGN(WebUI); 191 DISALLOW_COPY_AND_ASSIGN(WebUI);
196 }; 192 };
197 193
198 #endif // CONTENT_BROWSER_WEBUI_WEB_UI_H_ 194 #endif // CONTENT_BROWSER_WEBUI_WEB_UI_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698