Home | History | Annotate | Download | only in ntp
      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 
     17 class Profile;
     18 
     19 namespace base {
     20 class RefCountedMemory;
     21 }
     22 
     23 // This class keeps a cache of NTP resources (HTML and CSS) so we don't have to
     24 // regenerate them all the time.
     25 class NTPResourceCache : public content::NotificationObserver,
     26                          public BrowserContextKeyedService {
     27  public:
     28   explicit NTPResourceCache(Profile* profile);
     29   virtual ~NTPResourceCache();
     30 
     31   base::RefCountedMemory* GetNewTabHTML(bool is_incognito);
     32   base::RefCountedMemory* GetNewTabCSS(bool is_incognito);
     33 
     34   void set_should_show_apps_page(bool should_show_apps_page) {
     35     should_show_apps_page_ = should_show_apps_page;
     36   }
     37   void set_should_show_most_visited_page(bool should_show_most_visited_page) {
     38     should_show_most_visited_page_ = should_show_most_visited_page;
     39   }
     40   void set_should_show_other_devices_menu(bool should_show_other_devices_menu) {
     41     should_show_other_devices_menu_ = should_show_other_devices_menu;
     42   }
     43   void set_should_show_recently_closed_menu(
     44       bool should_show_recently_closed_menu) {
     45     should_show_recently_closed_menu_ = should_show_recently_closed_menu;
     46   }
     47   // content::NotificationObserver interface.
     48   virtual void Observe(int type,
     49                        const content::NotificationSource& source,
     50                        const content::NotificationDetails& details) OVERRIDE;
     51 
     52  private:
     53   void OnPreferenceChanged();
     54 
     55   void CreateNewTabHTML();
     56 
     57   // Helper to determine if the resource cache should be invalidated.
     58   // This is called on every page load, and can be used to check values that
     59   // don't generate a notification when changed (e.g., system preferences).
     60   bool NewTabCacheNeedsRefresh();
     61 
     62   Profile* profile_;
     63 
     64   scoped_refptr<base::RefCountedMemory> new_tab_html_;
     65 
     66 #if !defined(OS_ANDROID)
     67   // Returns a message describing any newly-added sync types, or an empty
     68   // string if all types have already been acknowledged.
     69   string16 GetSyncTypeMessage();
     70 
     71   void CreateNewTabIncognitoHTML();
     72 
     73   void CreateNewTabIncognitoCSS();
     74 
     75   void CreateNewTabCSS();
     76 
     77   scoped_refptr<base::RefCountedMemory> new_tab_incognito_html_;
     78   scoped_refptr<base::RefCountedMemory> new_tab_incognito_css_;
     79   scoped_refptr<base::RefCountedMemory> new_tab_css_;
     80   content::NotificationRegistrar registrar_;
     81   PrefChangeRegistrar profile_pref_change_registrar_;
     82   PrefChangeRegistrar local_state_pref_change_registrar_;
     83 #endif
     84 
     85   // Set based on platform_util::IsSwipeTrackingFromScrollEventsEnabled.
     86   bool is_swipe_tracking_from_scroll_events_enabled_;
     87   // Set based on NewTabUI::ShouldShowApps.
     88   bool should_show_apps_page_;
     89   // The next three all default to true and can be manually set, e.g., by the
     90   // chrome://apps page.
     91   bool should_show_most_visited_page_;
     92   bool should_show_other_devices_menu_;
     93   bool should_show_recently_closed_menu_;
     94 
     95   DISALLOW_COPY_AND_ASSIGN(NTPResourceCache);
     96 };
     97 
     98 #endif  // CHROME_BROWSER_UI_WEBUI_NTP_NTP_RESOURCE_CACHE_H_
     99