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 CHROME_BROWSER_UI_WEBUI_OPTIONS_OPTIONS_UI_H_ 6 #define CHROME_BROWSER_UI_WEBUI_OPTIONS_OPTIONS_UI_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/compiler_specific.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "content/public/browser/notification_observer.h" 14 #include "content/public/browser/notification_registrar.h" 15 #include "content/public/browser/notification_types.h" 16 #include "content/public/browser/web_contents_observer.h" 17 #include "content/public/browser/web_ui_controller.h" 18 #include "content/public/browser/web_ui_message_handler.h" 19 #include "ui/base/layout.h" 20 21 class AutocompleteResult; 22 23 namespace base { 24 class DictionaryValue; 25 class ListValue; 26 class RefCountedMemory; 27 } 28 29 #if defined(OS_CHROMEOS) 30 namespace chromeos { 31 namespace system { 32 class PointerDeviceObserver; 33 } // namespace system 34 } // namespace chromeos 35 #endif 36 37 namespace options { 38 39 // The base class handler of Javascript messages of options pages. 40 class OptionsPageUIHandler : public content::WebUIMessageHandler, 41 public content::NotificationObserver { 42 public: 43 // Key for identifying the Settings App localized_strings in loadTimeData. 44 static const char kSettingsAppKey[]; 45 46 OptionsPageUIHandler(); 47 virtual ~OptionsPageUIHandler(); 48 49 // Is this handler enabled? 50 virtual bool IsEnabled(); 51 52 // Collects localized strings for options page. 53 virtual void GetLocalizedValues(base::DictionaryValue* localized_strings) = 0; 54 55 virtual void PageLoadStarted() {} 56 57 // Will be called only once in the life time of the handler. Generally used to 58 // add observers, initializes preferences, or start asynchronous calls from 59 // various services. 60 virtual void InitializeHandler() {} 61 62 // Initialize the page. Called once the DOM is available for manipulation. 63 // This will be called when a RenderView is re-used (when navigated to with 64 // back/forward or session restored in some cases) or when created. 65 virtual void InitializePage() {} 66 67 // Uninitializes the page. Called just before the object is destructed. 68 virtual void Uninitialize() {} 69 70 // WebUIMessageHandler implementation. 71 virtual void RegisterMessages() OVERRIDE {} 72 73 // content::NotificationObserver implementation. 74 virtual void Observe(int type, 75 const content::NotificationSource& source, 76 const content::NotificationDetails& details) OVERRIDE {} 77 78 protected: 79 struct OptionsStringResource { 80 // The name of the resource in templateData. 81 const char* name; 82 // The .grd ID for the resource (IDS_*). 83 int id; 84 // The .grd ID of the string to replace $1 in |id|'s string. If zero or 85 // omitted (default initialized), no substitution is attempted. 86 int substitution_id; 87 }; 88 89 // A helper to simplify string registration in WebUI for strings which do not 90 // change at runtime and optionally contain a single substitution. 91 static void RegisterStrings(base::DictionaryValue* localized_strings, 92 const OptionsStringResource* resources, 93 size_t length); 94 95 // Registers string resources for a page's header and tab title. 96 static void RegisterTitle(base::DictionaryValue* localized_strings, 97 const std::string& variable_name, 98 int title_id); 99 100 content::NotificationRegistrar registrar_; 101 102 private: 103 DISALLOW_COPY_AND_ASSIGN(OptionsPageUIHandler); 104 }; 105 106 // An interface for common operations that a host of OptionsPageUIHandlers 107 // should provide. 108 class OptionsPageUIHandlerHost { 109 public: 110 virtual void InitializeHandlers() = 0; 111 112 protected: 113 virtual ~OptionsPageUIHandlerHost() {} 114 }; 115 116 // The WebUI for chrome:settings-frame. 117 class OptionsUI : public content::WebUIController, 118 public content::WebContentsObserver, 119 public OptionsPageUIHandlerHost { 120 public: 121 explicit OptionsUI(content::WebUI* web_ui); 122 virtual ~OptionsUI(); 123 124 // Takes the suggestions from |result| and adds them to |suggestions| so that 125 // they can be passed to a JavaScript function. 126 static void ProcessAutocompleteSuggestions( 127 const AutocompleteResult& result, 128 base::ListValue* const suggestions); 129 130 static base::RefCountedMemory* GetFaviconResourceBytes( 131 ui::ScaleFactor scale_factor); 132 133 // Overridden from content::WebContentsObserver: 134 virtual void DidStartProvisionalLoadForFrame( 135 int64 frame_id, 136 int64 parent_frame_id, 137 bool is_main_frame, 138 const GURL& validated_url, 139 bool is_error_page, 140 bool is_iframe_srcdoc, 141 content::RenderViewHost* render_view_host) OVERRIDE; 142 143 // Overridden from OptionsPageUIHandlerHost: 144 virtual void InitializeHandlers() OVERRIDE; 145 146 private: 147 // Adds OptionsPageUiHandler to the handlers list if handler is enabled. 148 void AddOptionsPageUIHandler(base::DictionaryValue* localized_strings, 149 OptionsPageUIHandler* handler); 150 151 bool initialized_handlers_; 152 153 std::vector<OptionsPageUIHandler*> handlers_; 154 155 #if defined(OS_CHROMEOS) 156 scoped_ptr<chromeos::system::PointerDeviceObserver> 157 pointer_device_observer_; 158 #endif 159 160 DISALLOW_COPY_AND_ASSIGN(OptionsUI); 161 }; 162 163 } // namespace options 164 165 #endif // CHROME_BROWSER_UI_WEBUI_OPTIONS_OPTIONS_UI_H_ 166