1// Copyright (c) 2011 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#pragma once
8
9#include <string>
10
11#include "base/compiler_specific.h"
12#include "base/memory/scoped_ptr.h"
13#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
14#include "content/browser/webui/web_ui.h"
15#include "content/common/notification_observer.h"
16#include "content/common/notification_registrar.h"
17#include "content/common/notification_type.h"
18
19class GURL;
20class PrefService;
21struct UserMetricsAction;
22
23// The base class handler of Javascript messages of options pages.
24class OptionsPageUIHandler : public WebUIMessageHandler,
25                             public NotificationObserver {
26 public:
27  OptionsPageUIHandler();
28  virtual ~OptionsPageUIHandler();
29
30  // Is this handler enabled?
31  virtual bool IsEnabled();
32
33  // Collects localized strings for options page.
34  virtual void GetLocalizedValues(DictionaryValue* localized_strings) = 0;
35
36  // Initialize the page.  Called once the DOM is available for manipulation.
37  // This will be called only once.
38  virtual void Initialize() {}
39
40  // Uninitializes the page.  Called just before the object is destructed.
41  virtual void Uninitialize() {}
42
43  // WebUIMessageHandler implementation.
44  virtual void RegisterMessages() {}
45
46  // NotificationObserver implementation.
47  virtual void Observe(NotificationType type,
48                       const NotificationSource& source,
49                       const NotificationDetails& details) {}
50
51  void UserMetricsRecordAction(const UserMetricsAction& action);
52
53 protected:
54  struct OptionsStringResource {
55    // The name of the resource in templateData.
56    const char* name;
57    // The .grd ID for the resource (IDS_*).
58    int id;
59  };
60  // A helper for simplifying the process of registering strings in WebUI.
61  static void RegisterStrings(DictionaryValue* localized_strings,
62                              const OptionsStringResource* resources,
63                              size_t length);
64
65  // Registers string resources for a page's header and tab title.
66  static void RegisterTitle(DictionaryValue* localized_strings,
67                            const std::string& variable_name,
68                            int title_id);
69
70  NotificationRegistrar registrar_;
71
72 private:
73  DISALLOW_COPY_AND_ASSIGN(OptionsPageUIHandler);
74};
75
76// An interface for common operations that a host of OptionsPageUIHandlers
77// should provide.
78class OptionsPageUIHandlerHost {
79 public:
80  virtual void InitializeHandlers() = 0;
81};
82
83// The WebUI for chrome:settings.
84class OptionsUI : public WebUI,
85                  public OptionsPageUIHandlerHost {
86 public:
87  explicit OptionsUI(TabContents* contents);
88  virtual ~OptionsUI();
89
90  static RefCountedMemory* GetFaviconResourceBytes();
91
92  // Overridden from WebUI:
93  virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE;
94  virtual void DidBecomeActiveForReusedRenderView() OVERRIDE;
95
96  // Overridden from OptionsPageUIHandlerHost:
97  virtual void InitializeHandlers() OVERRIDE;
98
99 private:
100  // Adds OptionsPageUiHandler to the handlers list if handler is enabled.
101  void AddOptionsPageUIHandler(DictionaryValue* localized_strings,
102                               OptionsPageUIHandler* handler);
103
104  bool initialized_handlers_;
105
106  DISALLOW_COPY_AND_ASSIGN(OptionsUI);
107};
108
109#endif  // CHROME_BROWSER_UI_WEBUI_OPTIONS_OPTIONS_UI_H_
110