1 // Copyright 2019 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 WEBLAYER_PUBLIC_TAB_H_ 6 #define WEBLAYER_PUBLIC_TAB_H_ 7 8 #include <algorithm> 9 #include <map> 10 #include <string> 11 #include <vector> 12 13 #include "base/callback_forward.h" 14 #include "base/strings/string16.h" 15 #include "build/build_config.h" 16 17 namespace base { 18 class Value; 19 } 20 21 #if !defined(OS_ANDROID) 22 namespace views { 23 class WebView; 24 } 25 #endif 26 27 namespace weblayer { 28 class Browser; 29 class ErrorPageDelegate; 30 class FaviconFetcher; 31 class FaviconFetcherDelegate; 32 class FullscreenDelegate; 33 class GoogleAccountsDelegate; 34 class NavigationController; 35 class NewTabDelegate; 36 class TabObserver; 37 class WebMessageHostFactory; 38 39 // Represents a tab that is navigable. 40 class Tab { 41 public: 42 virtual ~Tab() = default; 43 44 // Returns the Browser that owns this. 45 virtual Browser* GetBrowser() = 0; 46 47 // Sets the ErrorPageDelegate. If none is set, a default action will be taken 48 // for any given interaction with an error page. 49 virtual void SetErrorPageDelegate(ErrorPageDelegate* delegate) = 0; 50 51 // Sets the FullscreenDelegate. Setting a non-null value implicitly enables 52 // fullscreen. 53 virtual void SetFullscreenDelegate(FullscreenDelegate* delegate) = 0; 54 55 // Sets the NewBrowserDelegate. Setting a null value implicitly disables 56 // popups. 57 virtual void SetNewTabDelegate(NewTabDelegate* delegate) = 0; 58 59 virtual void SetGoogleAccountsDelegate(GoogleAccountsDelegate* delegate) = 0; 60 61 virtual void AddObserver(TabObserver* observer) = 0; 62 63 virtual void RemoveObserver(TabObserver* observer) = 0; 64 65 virtual NavigationController* GetNavigationController() = 0; 66 67 using JavaScriptResultCallback = base::OnceCallback<void(base::Value)>; 68 69 // Executes the script, and returns the result to the callback if provided. If 70 // |use_separate_isolate| is true, runs the script in a separate v8 Isolate. 71 // This uses more memory, but separates the injected scrips from scripts in 72 // the page. This prevents any potentially malicious interaction between 73 // first-party scripts in the page, and injected scripts. Use with caution, 74 // only pass false for this argument if you know this isn't an issue or you 75 // need to interact with first-party scripts. 76 virtual void ExecuteScript(const base::string16& script, 77 bool use_separate_isolate, 78 JavaScriptResultCallback callback) = 0; 79 80 // Returns the tab's guid. 81 virtual const std::string& GetGuid() = 0; 82 83 // Allows the embedder to get and set arbitrary data on the tab. This will be 84 // saved and restored with the browser, so it is important to keep this data 85 // as small as possible. 86 virtual void SetData(const std::map<std::string, std::string>& data) = 0; 87 virtual const std::map<std::string, std::string>& GetData() = 0; 88 89 // Adds a new WebMessageHostFactory. For any urls that match 90 // |allowed_origin_rules| a JS object is registered using the name 91 // |js_object_name| (in the global namespace). Script may use the object to 92 // send and receive messages and is available at page load time. 93 // 94 // The page is responsible for initiating the connection. That is, 95 // WebMessageHostFactory::CreateHost() is called once the page posts a 96 // message to the JS object. 97 // 98 // |allowed_origin_rules| is a set of rules used to determine which pages 99 // this applies to. '*' may be used to match anything. If not '*' the format 100 // is 'scheme://host:port': 101 // . scheme: The scheme, which can not be empty or contain '*'. 102 // . host: The host to match against. Can not contain '/' and may start with 103 // '*.' to match against a specific subdomain. 104 // . port (optional): matches a specific port. 105 // 106 // Returns an empty string on success. On failure, the return string gives 107 // an error message. 108 virtual base::string16 AddWebMessageHostFactory( 109 std::unique_ptr<WebMessageHostFactory> factory, 110 const base::string16& js_object_name, 111 const std::vector<std::string>& allowed_origin_rules) = 0; 112 113 // Removes the WebMessageHostFactory registered under |js_object_name|. 114 virtual void RemoveWebMessageHostFactory( 115 const base::string16& js_object_name) = 0; 116 117 // Creates a FaviconFetcher that notifies a FaviconFetcherDelegate when 118 // the favicon changes. 119 // A page may provide any number of favicons. The preferred image size 120 // used depends upon the platform. If a previously cached icon is available, 121 // it is used, otherwise the icon is downloaded. 122 // |delegate| may be called multiple times for the same navigation. This 123 // happens when the page dynamically updates the favicon, but may also happen 124 // if a cached icon is determined to be out of date. 125 virtual std::unique_ptr<FaviconFetcher> CreateFaviconFetcher( 126 FaviconFetcherDelegate* delegate) = 0; 127 128 #if !defined(OS_ANDROID) 129 // TODO: this isn't a stable API, so use it now for expediency in the C++ API, 130 // but if we ever want to have backward or forward compatibility in C++ this 131 // will have to be something else. 132 virtual void AttachToView(views::WebView* web_view) = 0; 133 #endif 134 }; 135 136 } // namespace weblayer 137 138 #endif // WEBLAYER_PUBLIC_TAB_H_ 139