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_NEW_TAB_UI_H_
6#define CHROME_BROWSER_UI_WEBUI_NEW_TAB_UI_H_
7#pragma once
8
9#include <string>
10
11#include "base/gtest_prod_util.h"
12#include "base/timer.h"
13#include "chrome/browser/sessions/tab_restore_service.h"
14#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
15#include "content/browser/webui/web_ui.h"
16#include "content/common/notification_observer.h"
17#include "content/common/notification_registrar.h"
18
19class GURL;
20class MessageLoop;
21class PrefService;
22class Profile;
23
24// The TabContents used for the New Tab page.
25class NewTabUI : public WebUI,
26                 public NotificationObserver {
27 public:
28  explicit NewTabUI(TabContents* manager);
29  ~NewTabUI();
30
31  // Override WebUI methods so we can hook up the paint timer to the render
32  // view host.
33  virtual void RenderViewCreated(RenderViewHost* render_view_host);
34  virtual void RenderViewReused(RenderViewHost* render_view_host);
35
36  static void RegisterUserPrefs(PrefService* prefs);
37  static void MigrateUserPrefs(PrefService* prefs, int old_pref_version,
38                               int new_pref_version);
39
40  // Whether we should disable the first run notification based on the command
41  // line switch.
42  static bool FirstRunDisabled();
43
44  // Adds "url", "title", and "direction" keys on incoming dictionary, setting
45  // title as the url as a fallback on empty title.
46  static void SetURLTitleAndDirection(DictionaryValue* dictionary,
47                                      const string16& title,
48                                      const GURL& gurl);
49
50  // Converts a list of TabRestoreService entries to the JSON format required
51  // by the NTP and adds them to the given list value.
52  static void AddRecentlyClosedEntries(
53      const TabRestoreService::Entries& entries,
54      ListValue* entry_list_value);
55
56  // The current preference version.
57  static int current_pref_version() { return current_pref_version_; }
58
59  class NewTabHTMLSource : public ChromeURLDataManager::DataSource {
60   public:
61    explicit NewTabHTMLSource(Profile* profile);
62
63    // Called when the network layer has requested a resource underneath
64    // the path we registered.
65    virtual void StartDataRequest(const std::string& path,
66                                  bool is_incognito,
67                                  int request_id);
68
69    virtual std::string GetMimeType(const std::string&) const;
70
71    virtual bool ShouldReplaceExistingSource() const;
72
73    // Setters and getters for first_run.
74    static void set_first_run(bool first_run) { first_run_ = first_run; }
75    static bool first_run() { return first_run_; }
76
77   private:
78    virtual ~NewTabHTMLSource() {}
79
80    // Whether this is the first run.
81    static bool first_run_;
82
83    // Pointer back to the original profile.
84    Profile* profile_;
85
86    DISALLOW_COPY_AND_ASSIGN(NewTabHTMLSource);
87  };
88
89 private:
90  FRIEND_TEST_ALL_PREFIXES(NewTabUITest, UpdateUserPrefsVersion);
91
92  virtual void Observe(NotificationType type,
93                       const NotificationSource& source,
94                       const NotificationDetails& details);
95
96  // Reset the CSS caches.
97  void InitializeCSSCaches();
98
99  void StartTimingPaint(RenderViewHost* render_view_host);
100  void PaintTimeout();
101
102  // Updates the user prefs version and calls |MigrateUserPrefs| if needed.
103  // Returns true if the version was updated.
104  static bool UpdateUserPrefsVersion(PrefService* prefs);
105
106  NotificationRegistrar registrar_;
107
108  // The time when we started benchmarking.
109  base::TimeTicks start_;
110  // The last time we got a paint notification.
111  base::TimeTicks last_paint_;
112  // Scoping so we can be sure our timeouts don't outlive us.
113  base::OneShotTimer<NewTabUI> timer_;
114  // The preference version. This used for migrating prefs of the NTP.
115  static const int current_pref_version_ = 3;
116
117  DISALLOW_COPY_AND_ASSIGN(NewTabUI);
118};
119
120#endif  // CHROME_BROWSER_UI_WEBUI_NEW_TAB_UI_H_
121