File: tab_organization_session.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (124 lines) | stat: -rw-r--r-- 4,348 bytes parent folder | download | duplicates (3)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2023 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_TABS_ORGANIZATION_TAB_ORGANIZATION_SESSION_H_
#define CHROME_BROWSER_UI_TABS_ORGANIZATION_TAB_ORGANIZATION_SESSION_H_

#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "chrome/browser/ui/tabs/organization/metrics.h"
#include "chrome/browser/ui/tabs/organization/tab_organization.h"
#include "chrome/browser/ui/tabs/organization/tab_organization_request.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"

class Browser;

namespace tabs {
class TabInterface;
}  // namespace tabs

class TabOrganizationSession : public TabOrganization::Observer {
 public:
  // TODO(dpenning): make this a base::Token.
  using ID = int;
  using TabOrganizations = std::vector<std::unique_ptr<TabOrganization>>;

  class Observer {
   public:
    virtual ~Observer() = default;

    virtual void OnTabOrganizationSessionUpdated(
        const TabOrganizationSession* session) {}
    virtual void OnTabOrganizationSessionDestroyed(
        TabOrganizationSession::ID session_id) {}
  };

  TabOrganizationSession();
  explicit TabOrganizationSession(
      std::unique_ptr<TabOrganizationRequest> request,
      TabOrganizationEntryPoint entrypoint = TabOrganizationEntryPoint::kNone,
      const tabs::TabInterface* base_session_tab = nullptr);
  ~TabOrganizationSession() override;

  const TabOrganizationRequest* request() const { return request_.get(); }
  const TabOrganizations& tab_organizations() const {
    return tab_organizations_;
  }
  ID session_id() const { return session_id_; }
  std::u16string feedback_id() const { return feedback_id_; }
  optimization_guide::proto::UserFeedback feedback() const { return feedback_; }
  const tabs::TabInterface* base_session_tab() const {
    return base_session_tab_;
  }

  static std::unique_ptr<TabOrganizationSession> CreateSessionForBrowser(
      const Browser* browser,
      const TabOrganizationEntryPoint entrypoint,
      const tabs::TabInterface* base_session_tab = nullptr);

  const TabOrganization* GetNextTabOrganization() const;
  TabOrganization* GetNextTabOrganization();

  void StartRequest();

  void AddOrganizationForTesting(
      std::unique_ptr<TabOrganization> tab_organization) {
    tab_organizations_.emplace_back(std::move(tab_organization));
  }

  // Returns true if the request is not completed or there are still actions
  // that need to be taken on organizations.
  bool IsComplete() const;

  void SetFeedback(optimization_guide::proto::UserFeedback feedback) {
    feedback_ = feedback;
  }

  void SetUserInstruction(const std::string& user_instruction);

  void AddObserver(Observer* new_observer);
  void RemoveObserver(Observer* new_observer);

  // TabOrganization::Observer
  void OnTabOrganizationUpdated(const TabOrganization* organization) override;
  void OnTabOrganizationDestroyed(TabOrganization::ID organization_id) override;

 private:
  // Notifies observers of the tab data that it has been updated.
  void NotifyObserversOfUpdate();

  // Checks whether there is a response, and if so calls Populate functions.
  // Notifies observers that the session has been updated.
  void OnRequestResponse(TabOrganizationResponse* response);

  // TODO: Remove once the full UI flow is implemented.
  void PopulateAndCreate(TabOrganizationResponse* response);

  // Fills in the organizations from the request. Called when the request
  // completes.
  void PopulateOrganizations(TabOrganizationResponse* response);

  std::unique_ptr<TabOrganizationRequest> request_;
  TabOrganizations tab_organizations_;
  ID session_id_;
  std::u16string feedback_id_;

  // Represents whether the user has provided feedback via the thumbs UI.
  optimization_guide::proto::UserFeedback feedback_ =
      optimization_guide::proto::UserFeedback::USER_FEEDBACK_UNSPECIFIED;

  // Entry point used to create the session. Used for logging.
  TabOrganizationEntryPoint entrypoint_;

  // Active tab web contents tied to the session, if any.
  raw_ptr<const tabs::TabInterface> base_session_tab_;

  base::ObserverList<Observer>::Unchecked observers_;
};

#endif  // CHROME_BROWSER_UI_TABS_ORGANIZATION_TAB_ORGANIZATION_SESSION_H_