File: ntp_resource_cache.h

package info (click to toggle)
chromium 141.0.7390.107-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,246,132 kB
  • sloc: cpp: 35,264,965; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,635; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,313; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (105 lines) | stat: -rw-r--r-- 3,374 bytes parent folder | download | duplicates (4)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_WEBUI_NTP_NTP_RESOURCE_CACHE_H_
#define CHROME_BROWSER_UI_WEBUI_NTP_NTP_RESOURCE_CACHE_H_

#include <memory>
#include <string>

#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/ref_counted_memory.h"
#include "base/scoped_observation.h"
#include "chrome/browser/themes/theme_service_observer.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/web_contents.h"
#include "ui/native_theme/native_theme.h"
#include "ui/native_theme/native_theme_observer.h"

class Profile;

namespace base {
class RefCountedMemory;
}  // namespace base

namespace ui {
class ColorProvider;
class ThemeProvider;
}  // namespace ui

SkColor GetThemeColor(const ui::NativeTheme* native_theme,
                      const ui::ColorProvider& cp,
                      int id);
std::string GetNewTabBackgroundPositionCSS(
    const ui::ThemeProvider& theme_provider);
std::string GetNewTabBackgroundTilingCSS(
    const ui::ThemeProvider& theme_provider);

// This class keeps a cache of NTP resources (HTML and CSS) so we don't have to
// regenerate them all the time.
// Note: This is only used for incognito and guest mode NTPs (NewTabUI), as well
// as for (non-incognito) app launcher pages (AppLauncherPageUI).
class NTPResourceCache : public ThemeServiceObserver,
                         public KeyedService,
                         public ui::NativeThemeObserver {
 public:
  enum WindowType {
    NORMAL,
    INCOGNITO,
    GUEST,
    // The OTR profile that is not used for Incognito or Guest windows.
    NON_PRIMARY_OTR,
  };

  explicit NTPResourceCache(Profile* profile);

  NTPResourceCache(const NTPResourceCache&) = delete;
  NTPResourceCache& operator=(const NTPResourceCache&) = delete;

  ~NTPResourceCache() override;

  base::RefCountedMemory* GetNewTabGuestHTML();
  base::RefCountedMemory* GetNewTabHTML(
      WindowType win_type,
      const content::WebContents::Getter& wc_getter);
  base::RefCountedMemory* GetNewTabCSS(
      WindowType win_type,
      const content::WebContents::Getter& wc_getter);

  // ThemeServiceObserver:
  void OnThemeChanged() override;

  static WindowType GetWindowType(Profile* profile);

 private:
  // KeyedService:
  void Shutdown() override;

  // ui::NativeThemeObserver:
  void OnNativeThemeUpdated(ui::NativeTheme* updated_theme) override;

  // Invalidates the NTPResourceCache.
  void Invalidate();

  void CreateNewTabCSS(const content::WebContents::Getter& wc_getter);

  void CreateNewTabIncognitoHTML(const content::WebContents::Getter& wc_getter);
  void CreateNewTabIncognitoCSS(const content::WebContents::Getter& wc_getter);

  void CreateNewTabGuestHTML();

  raw_ptr<Profile> profile_;

  scoped_refptr<base::RefCountedMemory> new_tab_css_;
  scoped_refptr<base::RefCountedMemory> new_tab_guest_html_;
  scoped_refptr<base::RefCountedMemory> new_tab_incognito_html_;
  scoped_refptr<base::RefCountedMemory> new_tab_incognito_css_;
  scoped_refptr<base::RefCountedMemory> new_tab_non_primary_otr_html_;

  base::ScopedObservation<ui::NativeTheme, ui::NativeThemeObserver>
      theme_observation_{this};
};

#endif  // CHROME_BROWSER_UI_WEBUI_NTP_NTP_RESOURCE_CACHE_H_