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/keyed_service/core/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 namespace content {
     24 class 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.
     29 class NTPResourceCache : public content::NotificationObserver,
     30                          public KeyedService {
     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   // Returns a message describing any newly-added sync types, or an empty
     80   // string if all types have already been acknowledged.
     81   base::string16 GetSyncTypeMessage();
     82 
     83   void CreateNewTabIncognitoHTML();
     84   void CreateNewTabIncognitoCSS();
     85 
     86   void CreateNewTabGuestHTML();
     87   void CreateNewTabGuestCSS();
     88 
     89   void CreateNewTabCSS();
     90 
     91   scoped_refptr<base::RefCountedMemory> new_tab_guest_html_;
     92   scoped_refptr<base::RefCountedMemory> new_tab_guest_css_;
     93   scoped_refptr<base::RefCountedMemory> new_tab_incognito_html_;
     94   scoped_refptr<base::RefCountedMemory> new_tab_incognito_css_;
     95   scoped_refptr<base::RefCountedMemory> new_tab_css_;
     96   content::NotificationRegistrar registrar_;
     97   PrefChangeRegistrar profile_pref_change_registrar_;
     98   PrefChangeRegistrar local_state_pref_change_registrar_;
     99 
    100   // Set based on platform_util::IsSwipeTrackingFromScrollEventsEnabled.
    101   bool is_swipe_tracking_from_scroll_events_enabled_;
    102   // Set based on NewTabUI::ShouldShowApps.
    103   bool should_show_apps_page_;
    104   // The next three all default to true and can be manually set, e.g., by the
    105   // chrome://apps page.
    106   bool should_show_most_visited_page_;
    107   bool should_show_other_devices_menu_;
    108   bool should_show_recently_closed_menu_;
    109 
    110   DISALLOW_COPY_AND_ASSIGN(NTPResourceCache);
    111 };
    112 
    113 #endif  // CHROME_BROWSER_UI_WEBUI_NTP_NTP_RESOURCE_CACHE_H_
    114