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_BROWSER_WEBUI_WEB_UI_IMPL_H_ 6 #define CONTENT_BROWSER_WEBUI_WEB_UI_IMPL_H_ 7 8 #include <map> 9 #include <memory> 10 #include <set> 11 #include <string> 12 #include <vector> 13 14 #include "base/compiler_specific.h" 15 #include "base/macros.h" 16 #include "base/memory/weak_ptr.h" 17 #include "content/public/browser/web_ui.h" 18 19 namespace IPC { 20 class Message; 21 } 22 23 namespace content { 24 class RenderFrameHost; 25 class WebContentsImpl; 26 27 class CONTENT_EXPORT WebUIImpl : public WebUI, 28 public base::SupportsWeakPtr<WebUIImpl> { 29 public: 30 explicit WebUIImpl(WebContentsImpl* contents); 31 ~WebUIImpl() override; 32 33 // Called when a RenderFrame is created for a WebUI (reload after a renderer 34 // crash) or when a WebUI is created for a RenderFrame (i.e. navigating from 35 // chrome://downloads to chrome://bookmarks) or when both are new (i.e. 36 // opening a new tab). 37 void RenderFrameCreated(RenderFrameHost* render_frame_host); 38 39 // Called when a RenderFrame is reused for the same WebUI type (i.e. reload). 40 void RenderFrameReused(RenderFrameHost* render_frame_host); 41 42 // Called when the owning RenderFrameHost has started unloading. 43 void RenderFrameHostUnloading(); 44 45 // WebUI implementation: 46 WebContents* GetWebContents() override; 47 WebUIController* GetController() override; 48 void SetController(std::unique_ptr<WebUIController> controller) override; 49 float GetDeviceScaleFactor() override; 50 const base::string16& GetOverriddenTitle() override; 51 void OverrideTitle(const base::string16& title) override; 52 int GetBindings() override; 53 void SetBindings(int bindings) override; 54 const std::vector<std::string>& GetRequestableSchemes() override; 55 void AddRequestableScheme(const char* scheme) override; 56 void AddMessageHandler(std::unique_ptr<WebUIMessageHandler> handler) override; 57 void RegisterMessageCallback(base::StringPiece message, 58 const MessageCallback& callback) override; 59 void ProcessWebUIMessage(const GURL& source_url, 60 const std::string& message, 61 const base::ListValue& args) override; 62 bool CanCallJavascript() override; 63 void CallJavascriptFunctionUnsafe(const std::string& function_name) override; 64 void CallJavascriptFunctionUnsafe(const std::string& function_name, 65 const base::Value& arg) override; 66 void CallJavascriptFunctionUnsafe(const std::string& function_name, 67 const base::Value& arg1, 68 const base::Value& arg2) override; 69 void CallJavascriptFunctionUnsafe(const std::string& function_name, 70 const base::Value& arg1, 71 const base::Value& arg2, 72 const base::Value& arg3) override; 73 void CallJavascriptFunctionUnsafe(const std::string& function_name, 74 const base::Value& arg1, 75 const base::Value& arg2, 76 const base::Value& arg3, 77 const base::Value& arg4) override; 78 void CallJavascriptFunctionUnsafe( 79 const std::string& function_name, 80 const std::vector<const base::Value*>& args) override; 81 std::vector<std::unique_ptr<WebUIMessageHandler>>* GetHandlersForTesting() 82 override; 83 84 bool OnMessageReceived(const IPC::Message& message, RenderFrameHost* sender); 85 86 private: 87 class MainFrameNavigationObserver; 88 89 // IPC message handling. 90 void OnWebUISend(RenderFrameHost* sender, 91 const std::string& message, 92 const base::ListValue& args); 93 94 // Execute a string of raw JavaScript on the page. 95 void ExecuteJavascript(const base::string16& javascript); 96 97 // Called internally and by the owned MainFrameNavigationObserver. 98 void DisallowJavascriptOnAllHandlers(); 99 100 // A map of message name -> message handling callback. 101 std::map<std::string, MessageCallback> message_callbacks_; 102 103 // Options that may be overridden by individual Web UI implementations. The 104 // bool options default to false. See the public getters for more information. 105 base::string16 overridden_title_; // Defaults to empty string. 106 int bindings_; // The bindings from BindingsPolicy that should be enabled for 107 // this page. 108 109 // The URL schemes that can be requested by this document. 110 std::vector<std::string> requestable_schemes_; 111 112 // The WebUIMessageHandlers we own. 113 std::vector<std::unique_ptr<WebUIMessageHandler>> handlers_; 114 115 // Non-owning pointer to the WebContentsImpl this WebUI is associated with. 116 WebContentsImpl* web_contents_; 117 118 // Notifies this WebUI about notifications in the main frame. 119 std::unique_ptr<MainFrameNavigationObserver> web_contents_observer_; 120 121 std::unique_ptr<WebUIController> controller_; 122 123 DISALLOW_COPY_AND_ASSIGN(WebUIImpl); 124 }; 125 126 } // namespace content 127 128 #endif // CONTENT_BROWSER_WEBUI_WEB_UI_IMPL_H_ 129