File: browser_status_monitor.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 (129 lines) | stat: -rw-r--r-- 5,026 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2013 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_SHELF_BROWSER_STATUS_MONITOR_H_
#define CHROME_BROWSER_UI_ASH_SHELF_BROWSER_STATUS_MONITOR_H_

#include <stdint.h>

#include <map>
#include <memory>
#include <set>
#include <string>

#include "base/check_op.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/ui/ash/shelf/app_service/app_service_instance_registry_helper.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/browser_tab_strip_tracker.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"

class Browser;

// BrowserStatusMonitor monitors creation/deletion of Browser and its
// TabStripModel to keep the shelf representation up to date as the
// active tab changes.
class BrowserStatusMonitor : public BrowserListObserver,
                             public TabStripModelObserver {
 public:
  explicit BrowserStatusMonitor(ChromeShelfController* shelf_controller);

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

  ~BrowserStatusMonitor() override;

  // Do the initialization work. Note: the init phase is separate from
  // construction because this function will make callbacks to
  // ChromeShelfController and ChromeShelfController creates an instance of this
  // class in its own constructor and may not be fully initialized yet.
  void Initialize();

  // A function which gets called when the current user has changed.
  // Note that this function is called by the ChromeShelfController to be
  // able to do the activation in a proper order - rather then setting an
  // observer.
  void ActiveUserChanged(const std::string& user_email);

  // A shortcut to call the ChromeShelfController's UpdateAppState().
  void UpdateAppItemState(content::WebContents* contents, bool remove);

  // A shortcut to call the BrowserShortcutShelfItemController's
  // UpdateBrowserItemState().
  void UpdateBrowserItemState();

  // BrowserListObserver overrides:
  void OnBrowserAdded(Browser* browser) override;
  void OnBrowserRemoved(Browser* browser) override;

  // TabStripModelObserver overrides:
  void OnTabStripModelChanged(
      TabStripModel* tab_strip_model,
      const TabStripModelChange& change,
      const TabStripSelectionChange& selection) override;

 private:
  // Add a windowed browser-based app to the shelf.
  void AddAppBrowserToShelf(Browser* browser);

  // Remove a windowed browser-based app from the shelf.
  void RemoveAppBrowserFromShelf(Browser* browser);

  // Check if an application is currently in the shelf by browser or app id.
  bool IsAppBrowserInShelf(Browser* browser);
  bool IsAppBrowserInShelfWithAppId(const std::string& app_id);

  class LocalWebContentsObserver;

  // Called by TabStripModelChanged()
  void OnActiveTabChanged(content::WebContents* old_contents,
                          content::WebContents* new_contents);
  void OnTabReplaced(TabStripModel* tab_strip_model,
                     content::WebContents* old_contents,
                     content::WebContents* new_contents);
  void OnTabInserted(TabStripModel* tab_strip_model,
                     content::WebContents* contents);
  void OnTabClosing(content::WebContents* contents);
  // Tab is moved between browsers
  void OnTabMoved(TabStripModel* tab_strip_model,
                  content::WebContents* contents);

  // Called by LocalWebContentsObserver.
  void OnTabNavigationFinished(content::WebContents* contents);

  // Create LocalWebContentsObserver for |contents|.
  void AddWebContentsObserver(content::WebContents* contents);

  // Remove LocalWebContentsObserver for |contents|.
  void RemoveWebContentsObserver(content::WebContents* contents);

  // Sets the shelf id for browsers represented by the browser shortcut item.
  void SetShelfIDForBrowserWindowContents(Browser* browser,
                                          content::WebContents* web_contents);

  raw_ptr<ChromeShelfController> shelf_controller_;

  std::map<Browser*, std::string> browser_to_app_id_map_;
  std::map<content::WebContents*, std::unique_ptr<LocalWebContentsObserver>>
      webcontents_to_observer_map_;

  BrowserTabStripTracker browser_tab_strip_tracker_;
  bool initialized_ = false;

  raw_ptr<AppServiceInstanceRegistryHelper> app_service_instance_helper_ =
      nullptr;

#if DCHECK_IS_ON()
  // Browsers for which OnBrowserAdded() was called, but not OnBrowserRemoved().
  // Used to validate that OnBrowserAdded() is invoked before
  // OnTabStripModelChanged().
  std::set<raw_ptr<Browser, SetExperimental>> known_browsers_;
  // Tabs that are removed from one browser and are getting reinserted into
  // another.
  std::set<raw_ptr<content::WebContents>> tabs_in_transit_;
#endif
};

#endif  // CHROME_BROWSER_UI_ASH_SHELF_BROWSER_STATUS_MONITOR_H_