File: holding_space_suggestions_delegate.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (97 lines) | stat: -rw-r--r-- 4,216 bytes parent folder | download | duplicates (5)
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
// Copyright 2022 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_ASH_HOLDING_SPACE_HOLDING_SPACE_SUGGESTIONS_DELEGATE_H_
#define CHROME_BROWSER_UI_ASH_HOLDING_SPACE_HOLDING_SPACE_SUGGESTIONS_DELEGATE_H_

#include <map>

#include "ash/public/cpp/holding_space/holding_space_item.h"
#include "base/timer/timer.h"
#include "chrome/browser/ash/file_suggest/file_suggest_keyed_service.h"
#include "chrome/browser/ash/file_suggest/file_suggest_util.h"
#include "chrome/browser/ui/ash/holding_space/holding_space_keyed_service_delegate.h"

namespace ash {

// A holding space delegate that manages the file suggestions (i.e. the files
// predicted to be needed by users) used in the holding space. The delegate
// observes the file suggestion service. When file suggestions update, the
// delegate refreshes the file suggestion items in the holding space model.
class HoldingSpaceSuggestionsDelegate
    : public HoldingSpaceKeyedServiceDelegate,
      public FileSuggestKeyedService::Observer {
 public:
  HoldingSpaceSuggestionsDelegate(HoldingSpaceKeyedService* service,
                                  HoldingSpaceModel* model);
  HoldingSpaceSuggestionsDelegate(const HoldingSpaceSuggestionsDelegate&) =
      delete;
  HoldingSpaceSuggestionsDelegate& operator=(
      const HoldingSpaceSuggestionsDelegate&) = delete;
  ~HoldingSpaceSuggestionsDelegate() override;

  // Refreshes suggestions.  Note that this intentionally does *not* invalidate
  // the file suggest service's item suggest cache which is too expensive for
  // holding space to invalidate.
  void RefreshSuggestions();

  // Removes suggestions associated with the specified `absolute_file_paths`.
  void RemoveSuggestions(
      const std::vector<base::FilePath>& absolute_file_paths);

 private:
  // HoldingSpaceKeyedServiceDelegate:
  void OnHoldingSpaceItemsAdded(
      const std::vector<const HoldingSpaceItem*>& items) override;
  void OnHoldingSpaceItemsRemoved(
      const std::vector<const HoldingSpaceItem*>& items) override;
  void OnHoldingSpaceItemInitialized(const HoldingSpaceItem* item) override;
  void OnPersistenceRestored() override;

  // FileSuggestKeyedService::Observer:
  void OnFileSuggestionUpdated(FileSuggestionType type) override;

  // Fetches file suggestions of the specified `type` from the service. Returns
  // early if the fetch on the suggestions of `type` is already pending.
  void MaybeFetchSuggestions(FileSuggestionType type);

  // Maybe schedules a task to update suggestions in the holding space model.
  void MaybeScheduleUpdateSuggestionsInModel();

  // Called when fetching file suggestions finishes.
  void OnSuggestionsFetched(
      FileSuggestionType type,
      const std::optional<std::vector<FileSuggestData>>& suggestions);

  // Updates suggestions in the holding space model. The method ensures that:
  // 1. Drive file suggestions (if any) are always in front of local file
  // suggestions; and
  // 2. The suggestions of the same type (i.e. drive file ones or local file
  // ones) follow the relevance order.
  void UpdateSuggestionsInModel();

  base::ScopedObservation<FileSuggestKeyedService,
                          FileSuggestKeyedService::Observer>
      file_suggest_service_observation_{this};

  // Records the suggestion types on which data fetches are pending.
  std::set<FileSuggestionType> pending_fetches_;

  // Caches the suggested files in the holding space model. In each key-value
  // pair: the key is a holding space suggestion item type; the value is an
  // array of paths to the suggested files. NOTE: each file path array follows
  // the relevance order, which means that a file path with a smaller index in
  // the array has a higher relevance score.
  std::map<HoldingSpaceItem::Type, std::vector<base::FilePath>>
      suggestions_by_type_;

  // Used to schedule the task of updating suggestions in model.
  base::OneShotTimer suggestion_update_timer_;

  base::WeakPtrFactory<HoldingSpaceSuggestionsDelegate> weak_factory_{this};
};

}  // namespace ash

#endif  // CHROME_BROWSER_UI_ASH_HOLDING_SPACE_HOLDING_SPACE_SUGGESTIONS_DELEGATE_H_