ntp_resource_cache.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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_NTP_NTP_RESOURCE_CACHE_H_
6#define CHROME_BROWSER_UI_WEBUI_NTP_NTP_RESOURCE_CACHE_H_
7
8#include "base/basictypes.h"
9#include "base/compiler_specific.h"
10#include "base/memory/ref_counted.h"
11#include "base/prefs/pref_change_registrar.h"
12#include "base/strings/string16.h"
13#include "components/browser_context_keyed_service/browser_context_keyed_service.h"
14#include "content/public/browser/notification_observer.h"
15#include "content/public/browser/notification_registrar.h"
16
17class Profile;
18
19namespace base {
20class RefCountedMemory;
21}
22
23namespace content {
24class RenderProcessHost;
25}
26
27// This class keeps a cache of NTP resources (HTML and CSS) so we don't have to
28// regenerate them all the time.
29class NTPResourceCache : public content::NotificationObserver,
30                         public BrowserContextKeyedService {
31 public:
32  enum WindowType {
33    NORMAL,
34    INCOGNITO,
35    GUEST,
36  };
37
38  explicit NTPResourceCache(Profile* profile);
39  virtual ~NTPResourceCache();
40
41  base::RefCountedMemory* GetNewTabHTML(WindowType win_type);
42  base::RefCountedMemory* GetNewTabCSS(WindowType win_type);
43
44  void set_should_show_apps_page(bool should_show_apps_page) {
45    should_show_apps_page_ = should_show_apps_page;
46  }
47  void set_should_show_most_visited_page(bool should_show_most_visited_page) {
48    should_show_most_visited_page_ = should_show_most_visited_page;
49  }
50  void set_should_show_other_devices_menu(bool should_show_other_devices_menu) {
51    should_show_other_devices_menu_ = should_show_other_devices_menu;
52  }
53  void set_should_show_recently_closed_menu(
54      bool should_show_recently_closed_menu) {
55    should_show_recently_closed_menu_ = should_show_recently_closed_menu;
56  }
57  // content::NotificationObserver interface.
58  virtual void Observe(int type,
59                       const content::NotificationSource& source,
60                       const content::NotificationDetails& details) OVERRIDE;
61
62  static WindowType GetWindowType(
63      Profile* profile, content::RenderProcessHost* render_host);
64
65 private:
66  void OnPreferenceChanged();
67
68  void CreateNewTabHTML();
69
70  // Helper to determine if the resource cache should be invalidated.
71  // This is called on every page load, and can be used to check values that
72  // don't generate a notification when changed (e.g., system preferences).
73  bool NewTabCacheNeedsRefresh();
74
75  Profile* profile_;
76
77  scoped_refptr<base::RefCountedMemory> new_tab_html_;
78
79#if !defined(OS_ANDROID)
80  // Returns a message describing any newly-added sync types, or an empty
81  // string if all types have already been acknowledged.
82  base::string16 GetSyncTypeMessage();
83
84  void CreateNewTabIncognitoHTML();
85  void CreateNewTabIncognitoCSS();
86
87  void CreateNewTabGuestHTML();
88  void CreateNewTabGuestCSS();
89
90  void CreateNewTabCSS();
91
92  scoped_refptr<base::RefCountedMemory> new_tab_guest_html_;
93  scoped_refptr<base::RefCountedMemory> new_tab_guest_css_;
94  scoped_refptr<base::RefCountedMemory> new_tab_incognito_html_;
95  scoped_refptr<base::RefCountedMemory> new_tab_incognito_css_;
96  scoped_refptr<base::RefCountedMemory> new_tab_css_;
97  content::NotificationRegistrar registrar_;
98  PrefChangeRegistrar profile_pref_change_registrar_;
99  PrefChangeRegistrar local_state_pref_change_registrar_;
100#endif
101
102  // Set based on platform_util::IsSwipeTrackingFromScrollEventsEnabled.
103  bool is_swipe_tracking_from_scroll_events_enabled_;
104  // Set based on NewTabUI::ShouldShowApps.
105  bool should_show_apps_page_;
106  // The next three all default to true and can be manually set, e.g., by the
107  // chrome://apps page.
108  bool should_show_most_visited_page_;
109  bool should_show_other_devices_menu_;
110  bool should_show_recently_closed_menu_;
111
112  DISALLOW_COPY_AND_ASSIGN(NTPResourceCache);
113};
114
115#endif  // CHROME_BROWSER_UI_WEBUI_NTP_NTP_RESOURCE_CACHE_H_
116