1 // Copyright 2020 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 CHROMECAST_BROWSER_WEBUI_CAST_WEBUI_H_ 6 #define CHROMECAST_BROWSER_WEBUI_CAST_WEBUI_H_ 7 8 #include <memory> 9 #include <string> 10 #include <vector> 11 12 #include "base/containers/flat_map.h" 13 #include "base/memory/weak_ptr.h" 14 #include "chromecast/browser/webui/mojom/webui.mojom.h" 15 #include "chromecast/chromecast_buildflags.h" 16 #include "content/public/browser/web_ui_controller.h" 17 #include "mojo/public/cpp/bindings/receiver.h" 18 #include "mojo/public/cpp/bindings/remote.h" 19 20 namespace content { 21 class BrowserContext; 22 class WebContents; 23 class WebUI; 24 } // namespace content 25 26 namespace chromecast { 27 28 class CastWebUIMessageHandler; 29 30 class CastWebUI : public mojom::WebUi, public content::WebUIController { 31 public: 32 CastWebUI(content::WebUI* webui, 33 const std::string& host, 34 mojom::WebUiClient* client); 35 ~CastWebUI() override; 36 37 static std::unique_ptr<CastWebUI> Create(content::WebUI* webui, 38 const std::string& host, 39 mojom::WebUiClient* client); 40 41 protected: 42 content::WebContents* const web_contents_; 43 content::BrowserContext* const browser_context_; 44 45 private: 46 void AddWebviewSupport(); 47 void OnWebUIReady(const base::ListValue* args); 48 void InvokeCallback(const std::string& message, const base::ListValue* args); 49 50 // mojom::WebUI implementation: 51 void RegisterMessageCallback( 52 const std::string& message, 53 mojo::PendingRemote<mojom::MessageCallback> callback) override; 54 void CallJavascriptFunction(const std::string& function, 55 std::vector<base::Value> args) override; 56 57 // Pointer to the generic message handler owned by the Web UI. The message 58 // handler is guaranteed to outlive CastWebUI since |this| is the first member 59 // to be deleted in the Web UI. 60 CastWebUIMessageHandler* message_handler_; 61 62 mojo::Receiver<mojom::WebUi> web_ui_{this}; 63 64 base::flat_map<std::string, mojo::Remote<mojom::MessageCallback>> 65 message_callbacks_; 66 67 base::WeakPtr<CastWebUI> weak_this_; 68 base::WeakPtrFactory<CastWebUI> weak_factory_{this}; 69 }; 70 71 } // namespace chromecast 72 73 #endif // CHROMECAST_BROWSER_WEBUI_CAST_WEBUI_H_ 74