File: mahi_cache_manager.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (107 lines) | stat: -rw-r--r-- 3,262 bytes parent folder | download | duplicates (6)
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
// Copyright 2024 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_ASH_MAHI_MAHI_CACHE_MANAGER_H_
#define CHROME_BROWSER_ASH_MAHI_MAHI_CACHE_MANAGER_H_

#include <map>
#include <string>

#include "base/timer/timer.h"
#include "ui/gfx/image/image_skia.h"
#include "url/gurl.h"

namespace ash {

// A helper class that help MahiManager to manage its cache.
class MahiCacheManager {
 public:
  struct MahiQA {
    std::u16string question;
    std::u16string answer;
  };

  // Struct that store data of a page.
  struct MahiData {
    MahiData();
    MahiData(const std::string& url,
             const std::u16string& title,
             const std::u16string& page_content,
             const std::optional<gfx::ImageSkia>& favicon_image,
             const std::optional<std::u16string>& summary,
             const std::vector<MahiQA>& previous_qa);
    MahiData(const MahiData&);
    MahiData& operator=(const MahiData&);
    ~MahiData();

    // URL of the webpage
    std::string url;
    // The title of the page.
    std::u16string title;
    // The extracted content of the page.
    std::u16string page_content;
    // The favicon of the page.
    std::optional<gfx::ImageSkia> favicon_image;
    // The summary of the page;
    std::optional<std::u16string> summary;
    // List of previous questions and answers for this page.
    std::vector<MahiQA> previous_qa;

    // Time of creation of this object.
    base::Time creation_time;
  };

  MahiCacheManager();

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

  ~MahiCacheManager();

  // Adds page cache for a given url. If the url exists in the cache, replace
  // with the new one.
  void AddCacheForUrl(const std::string& url, const MahiData& data);

  // Updates summary for a URL that is already in the cache. If the cache
  // doesn't contain the URL, does nothing.
  void TryToUpdateSummaryForUrl(const std::string& url,
                                const std::u16string& summary);

  // Returns the content for the given url.
  std::u16string GetPageContentForUrl(const std::string& url) const;

  // Returns the summary for the given url. If it's not in the cache, return
  // nullopt.
  std::optional<std::u16string> GetSummaryForUrl(const std::string& url) const;

  // Returns list of questions and answers for the given url.
  std::vector<MahiQA> GetQAForUrl(const std::string& url) const;

  // Clears the cache.
  void ClearCache();

  // Delete the page cache for a given url. Does nothing if the url doesn't
  // exist in the cache.
  void DeleteCacheForUrl(const std::string& url);

  // Gets size of the cache.
  int size() { return page_cache_.size(); }

 private:
  friend class MahiCacheManagerTest;

  // Called when the |periodic_timer_| triggers.
  void OnTimerFired();

  // Timer to trigger periodically for clearing cache.
  std::unique_ptr<base::RepeatingTimer> periodic_timer_;

  // A map from a url to it's corresponding data. It's used to store the cache
  // for mahi.
  std::map<GURL, MahiData> page_cache_;
};

}  // namespace ash

#endif  // CHROME_BROWSER_ASH_MAHI_MAHI_CACHE_MANAGER_H_