summaryrefslogtreecommitdiffstats
path: root/chrome/browser/dom_ui/new_tab_ui.h
blob: 1feac2abd2e7fac0d6acb63881ea7c409f45856f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_DOM_UI_NEW_TAB_UI_H_
#define CHROME_BROWSER_DOM_UI_NEW_TAB_UI_H_

#include <string>

#include "chrome/browser/dom_ui/dom_ui.h"
#include "chrome/browser/dom_ui/chrome_url_data_manager.h"
#include "chrome/common/notification_registrar.h"
#include "testing/gtest/include/gtest/gtest_prod.h"

class GURL;
class MessageLoop;
class PrefService;
class Profile;

// The TabContents used for the New Tab page.
class NewTabUI : public DOMUI,
                 public NotificationObserver {
 public:
  explicit NewTabUI(TabContents* manager);
  ~NewTabUI();

  // Override DOMUI methods so we can hook up the paint timer to the render
  // view host.
  virtual void RenderViewCreated(RenderViewHost* render_view_host);
  virtual void RenderViewReused(RenderViewHost* render_view_host);

  static void RegisterUserPrefs(PrefService* prefs);
  static void MigrateUserPrefs(PrefService* prefs, int old_pref_version,
                               int new_pref_version);

  // Whether we should disable the web resources backend service
  static bool WebResourcesEnabled();

  // Whether we should disable the first run notification based on the command
  // line switch.
  static bool FirstRunDisabled();

  // Adds "url", "title", and "direction" keys on incoming dictionary, setting
  // title as the url as a fallback on empty title.
  static void SetURLTitleAndDirection(DictionaryValue* dictionary,
                                      const string16& title,
                                      const GURL& gurl);

  // The current preference version.
  static const int current_pref_version() { return current_pref_version_; }

  class NewTabHTMLSource : public ChromeURLDataManager::DataSource {
   public:
    explicit NewTabHTMLSource(Profile* profile);

    // Called when the network layer has requested a resource underneath
    // the path we registered.
    virtual void StartDataRequest(const std::string& path, int request_id);

    virtual std::string GetMimeType(const std::string&) const {
      return "text/html";
    }

    virtual MessageLoop* MessageLoopForRequestPath(const std::string& path)
        const {
      // NewTabHTMLSource does all of the operations that need to be on the
      // UI thread from InitFullHTML, called by the constructor.  It is safe
      // to call StartDataRequest from any thread, so return NULL.
      return NULL;
    }

    // Setters and getters for first_view.
    static void set_first_view(bool first_view) { first_view_ = first_view; }
    static bool first_view() { return first_view_; }

    // Setters and getters for first_run.
    static void set_first_run(bool first_run) { first_run_ = first_run; }
    static bool first_run() { return first_run_; }

   private:
    ~NewTabHTMLSource() {}

    // In case a file path to the new tab page was provided this tries to load
    // the file and returns the file content if successful. This returns an
    // empty string in case of failure.
    static std::string GetCustomNewTabPageFromCommandLine();

    // Populate full_html_.  This must be called from the UI thread because it
    // involves profile access.
    //
    // A new NewTabHTMLSource object is used for each new tab page instance
    // and each reload of an existing new tab page, so there is no concern
    // about cached data becoming stale.
    void InitFullHTML();

    // The content to be served by StartDataRequest, stored by InitFullHTML.
    std::string full_html_;

    // Whether this is the first viewing of the new tab page and
    // we think it is the user's startup page.
    static bool first_view_;

    // Whether this is the first run.
    static bool first_run_;

    // The user's profile.
    Profile* profile_;

    DISALLOW_COPY_AND_ASSIGN(NewTabHTMLSource);
  };

 private:
  FRIEND_TEST(NewTabUITest, UpdateUserPrefsVersion);

  void Observe(NotificationType type,
               const NotificationSource& source,
               const NotificationDetails& details);

  // Reset the CSS caches.
  void InitializeCSSCaches();

  // Updates the user prefs version and calls |MigrateUserPrefs| if needed.
  // Returns true if the version was updated.
  static bool UpdateUserPrefsVersion(PrefService* prefs);

  NotificationRegistrar registrar_;

  // The message id that should be displayed in this NewTabUIContents
  // instance's motd area.
  int motd_message_id_;

  // Whether the user is in incognito mode or not, used to determine
  // what HTML to load.
  bool incognito_;

  // The preference version. This used for migrating prefs of the NTP.
  static const int current_pref_version_ = 1;

  DISALLOW_COPY_AND_ASSIGN(NewTabUI);
};

#endif  // CHROME_BROWSER_DOM_UI_NEW_TAB_UI_H_