File: tabs_event_router_android.h

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (100 lines) | stat: -rw-r--r-- 3,443 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
// Copyright 2025 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_EXTENSIONS_API_TABS_TABS_EVENT_ROUTER_ANDROID_H_
#define CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_EVENT_ROUTER_ANDROID_H_

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

#include "base/memory/raw_ptr.h"
#include "base/scoped_multi_source_observation.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list_observer.h"
#include "chrome/browser/ui/android/tab_model/tab_model_observer.h"
#include "content/public/browser/web_contents_observer.h"
#include "extensions/browser/event_router.h"
#include "url/gurl.h"

class Profile;
class TabAndroid;
class TabModel;

namespace content {
class WebContents;
}

namespace extensions {

// TabsEventRouterAndroid listens to tab events on Android and routes them to
// listeners inside extension process renderers.
//
// TODO(crbug.com/427503497): This class is a temporary solution to unblock the
// Tabs API on desktop Android. It can be deleted once TabsEventRouter works on
// desktop Android.
class TabsEventRouterAndroid : public TabModelListObserver,
                               public TabModelObserver {
 public:
  explicit TabsEventRouterAndroid(Profile* profile);
  TabsEventRouterAndroid(const TabsEventRouterAndroid&) = delete;
  TabsEventRouterAndroid& operator=(const TabsEventRouterAndroid&) = delete;
  ~TabsEventRouterAndroid() override;

  // TabModelListObserver:
  void OnTabModelAdded(TabModel* tab_model) override;
  void OnTabModelRemoved(TabModel* tab_model) override;

  // TabModelObserver:
  void DidAddTab(TabAndroid* tab, TabModel::TabLaunchType type) override;
  void TabRemoved(TabAndroid* tab) override;

 private:
  class TabEntry : public content::WebContentsObserver {
   public:
    TabEntry(TabsEventRouterAndroid* router, content::WebContents* contents);
    ~TabEntry() override;

    // content::WebContentsObserver:
    void DidStopLoading() override;
    void TitleWasSet(content::NavigationEntry* entry) override;
    void WebContentsDestroyed() override;

    const raw_ptr<TabsEventRouterAndroid> router_;
    GURL url_;
  };

  void TabCreatedAt(content::WebContents* contents, bool active);

  // Internal processing of tab updated events. Intended to be called when
  // there's any changed property.
  void TabUpdated(TabEntry* entry,
                  std::set<std::string> changed_property_names);

  void DispatchTabUpdatedEvent(content::WebContents* contents,
                               std::set<std::string> changed_property_names);
  void DispatchEvent(events::HistogramValue histogram_value,
                     const std::string& event_name,
                     base::Value::List args,
                     EventRouter::UserGestureState user_gesture);

  void RegisterForTabNotifications(content::WebContents* contents);

  // Gets the TabEntry for the given `contents`. Returns TabEntry* if found,
  // nullptr if not.
  TabEntry* GetTabEntry(content::WebContents* contents);

  raw_ptr<Profile> profile_;

  base::ScopedMultiSourceObservation<TabModel, TabModelObserver>
      tab_model_observations_{this};

  // The map is keyed by tab id.
  using TabEntryMap = std::map<int, std::unique_ptr<TabEntry>>;
  TabEntryMap tab_entries_;
};

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_EVENT_ROUTER_ANDROID_H_