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_STARTUP_PAGES_HANDLER_H_
6#define CHROME_BROWSER_UI_WEBUI_OPTIONS_STARTUP_PAGES_HANDLER_H_
7
8#include "base/memory/ref_counted.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/memory/weak_ptr.h"
11#include "base/prefs/pref_change_registrar.h"
12#include "base/prefs/pref_member.h"
13#include "chrome/browser/autocomplete/autocomplete_controller_delegate.h"
14#include "chrome/browser/ui/webui/options/options_ui.h"
15#include "ui/base/models/table_model_observer.h"
16
17class AutocompleteController;
18class CustomHomePagesTableModel;
19class TemplateURLService;
20
21namespace options {
22
23// Chrome browser options page UI handler.
24class StartupPagesHandler : public OptionsPageUIHandler,
25                            public AutocompleteControllerDelegate,
26                            public ui::TableModelObserver {
27 public:
28  StartupPagesHandler();
29  virtual ~StartupPagesHandler();
30
31  // OptionsPageUIHandler implementation.
32  virtual void GetLocalizedValues(
33      base::DictionaryValue* localized_strings) OVERRIDE;
34  virtual void InitializeHandler() OVERRIDE;
35  virtual void InitializePage() OVERRIDE;
36  virtual void RegisterMessages() OVERRIDE;
37
38  // AutocompleteControllerDelegate implementation.
39  virtual void OnResultChanged(bool default_match_changed) OVERRIDE;
40
41  // ui::TableModelObserver implementation.
42  virtual void OnModelChanged() OVERRIDE;
43  virtual void OnItemsChanged(int start, int length) OVERRIDE;
44  virtual void OnItemsAdded(int start, int length) OVERRIDE;
45  virtual void OnItemsRemoved(int start, int length) OVERRIDE;
46
47 private:
48  // Saves the changes that have been made. Called from WebUI.
49  void CommitChanges(const base::ListValue* args);
50
51  // Cancels the changes that have been made. Called from WebUI.
52  void CancelChanges(const base::ListValue* args);
53
54  // Removes the startup page at the given indexes. Called from WebUI.
55  void RemoveStartupPages(const base::ListValue* args);
56
57  // Adds a startup page with the given URL after the given index.
58  // Called from WebUI.
59  void AddStartupPage(const base::ListValue* args);
60
61  // Changes the startup page at the given index to the given URL.
62  // Called from WebUI.
63  void EditStartupPage(const base::ListValue* args);
64
65  // Sets the startup page set to the current pages. Called from WebUI.
66  void SetStartupPagesToCurrentPages(const base::ListValue* args);
67
68  // Writes the current set of startup pages to prefs. Called from WebUI.
69  void DragDropStartupPage(const base::ListValue* args);
70
71  // Gets autocomplete suggestions asychronously for the given string.
72  // Called from WebUI.
73  void RequestAutocompleteSuggestions(const base::ListValue* args);
74
75  // Loads the current set of custom startup pages and reports it to the WebUI.
76  void UpdateStartupPages();
77
78  // Writes the current set of startup pages to prefs.
79  void SaveStartupPagesPref();
80
81  scoped_ptr<AutocompleteController> autocomplete_controller_;
82
83  // Used to observe updates to the preference of the list of URLs to load
84  // on startup, which can be updated via sync.
85  PrefChangeRegistrar pref_change_registrar_;
86
87  // TODO(stuartmorgan): Once there are no other clients of
88  // CustomHomePagesTableModel, consider changing it to something more like
89  // TemplateURLService.
90  scoped_ptr<CustomHomePagesTableModel> startup_custom_pages_table_model_;
91
92  DISALLOW_COPY_AND_ASSIGN(StartupPagesHandler);
93};
94
95}  // namespace options
96
97#endif  // CHROME_BROWSER_UI_WEBUI_OPTIONS_STARTUP_PAGES_HANDLER_H_
98