1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_BROWSER_UI_ASH_LAUNCHER_APP_SERVICE_APP_SERVICE_INSTANCE_REGISTRY_HELPER_H_
6 #define CHROME_BROWSER_UI_ASH_LAUNCHER_APP_SERVICE_APP_SERVICE_INSTANCE_REGISTRY_HELPER_H_
7 
8 #include <map>
9 #include <memory>
10 #include <set>
11 
12 #include "chrome/browser/ui/ash/launcher/launcher_controller_helper.h"
13 #include "components/services/app_service/public/cpp/instance.h"
14 
15 namespace apps {
16 class AppServiceProxy;
17 }
18 
19 namespace aura {
20 class Window;
21 }
22 
23 namespace content {
24 class WebContents;
25 }
26 
27 class AppServiceAppWindowLauncherController;
28 
29 // The helper class to operate the App Service Instance Registry.
30 class AppServiceInstanceRegistryHelper {
31  public:
32   explicit AppServiceInstanceRegistryHelper(
33       AppServiceAppWindowLauncherController* controller);
34   ~AppServiceInstanceRegistryHelper();
35 
36   void ActiveUserChanged();
37   void AdditionalUserAddedToSession(Profile* profile);
38 
39   // Notifies the AppService InstanceRegistry that active tabs are changed.
40   void OnActiveTabChanged(content::WebContents* old_contents,
41                           content::WebContents* new_contents);
42 
43   // Notifies the AppService InstanceRegistry that the tab's contents are
44   // changed. The |old_contents|'s instance will be removed, and the
45   // |new_contents|'s instance will be added.
46   void OnTabReplaced(content::WebContents* old_contents,
47                      content::WebContents* new_contents);
48 
49   // Notifies the AppService InstanceRegistry that a new tab is inserted. A new
50   // instance will be add tp App Service InstanceRegistry.
51   void OnTabInserted(content::WebContents* contents);
52 
53   // Notifies the AppService InstanceRegistry that the tab is closed. The
54   // instance will be removed from App Service InstanceRegistry.
55   void OnTabClosing(content::WebContents* contents);
56 
57   // Notifies the AppService InstanceRegistry that the browser is closed. The
58   // instance will be removed from App Service InstanceRegistry.
59   void OnBrowserRemoved();
60 
61   // Helper function to update App Service InstanceRegistry.
62   void OnInstances(const std::string& app_id,
63                    aura::Window* window,
64                    const std::string& launch_id,
65                    apps::InstanceState state);
66 
67   // Notifies that the shelf id is set for browsers.
68   void OnSetShelfIDForBrowserWindowContents(content::WebContents* web_contents);
69 
70   // Updates the apps state when the browser's visibility is changed.
71   void OnWindowVisibilityChanged(const ash::ShelfID& shelf_id,
72                                  aura::Window* window,
73                                  bool visible);
74 
75   // Updates the apps state when the browser is inactivated.
76   void SetWindowActivated(const ash::ShelfID& shelf_id,
77                           aura::Window* window,
78                           bool active);
79 
80   // Returns the instance state for |window| based on |visible|.
81   apps::InstanceState CalculateVisibilityState(aura::Window* window,
82                                                bool visible) const;
83 
84   // Returns the instance state for |window| based on |active|.
85   apps::InstanceState CalculateActivatedState(aura::Window* window,
86                                               bool active) const;
87 
88   // Return true if the app is opend in a browser.
89   bool IsOpenedInBrowser(const std::string& app_id, aura::Window* window) const;
90 
91   // Returns an app id for |window| in InstanceRegistry. If there is no |window|
92   // in InstanceRegistry, returns an empty string.
93   std::string GetAppId(aura::Window* window) const;
94 
95  private:
96   // Returns an app id to represent |contents| in InstanceRegistry. If there is
97   // no app in |contents|, returns the app id of the Chrome component
98   // application.
99   std::string GetAppId(content::WebContents* contents) const;
100 
101   // Returns a window to represent |contents| in InstanceRegistry. If |contents|
102   // is a Web app, returns the native window for it. If there is no app in
103   // |contents|, returns the toplevel window.
104   aura::Window* GetWindow(content::WebContents* contents);
105 
106   // Returns windows in InstanceRegistry for the given |app_id|.
107   std::set<aura::Window*> GetWindows(const std::string& app_id);
108 
109   // Returns the state in InstanceRegistry for the given |app_id|. If there is
110   // no |window| in InstanceRegistry, returns apps::InstanceState::kUnknown.
111   apps::InstanceState GetState(aura::Window* window) const;
112 
113   // Adds the tab's |window| to |browser_window_to_tab_window_|.
114   void AddTabWindow(const std::string& app_id, aura::Window* window);
115   // Removes the tab's |window| from |browser_window_to_tab_window_|.
116   void RemoveTabWindow(const std::string& app_id, aura::Window* window);
117   // updates the relation for the tab's |window| and
118   // |browser_window_to_tab_window_|.
119   void UpdateTabWindow(const std::string& app_id, aura::Window* window);
120 
121   AppServiceAppWindowLauncherController* controller_ = nullptr;
122 
123   apps::AppServiceProxy* proxy_ = nullptr;
124 
125   // Used to get app info for tabs.
126   std::unique_ptr<LauncherControllerHelper> launcher_controller_helper_;
127 
128   // Maps the browser window to tab windows in the browser. When the browser
129   // window is inactive or invisible, tab windows in the browser should be
130   // updated accordingly as well.
131   std::map<aura::Window*, std::set<aura::Window*>>
132       browser_window_to_tab_window_;
133 
134   // Maps the tab window to the browser window in the browser.
135   std::map<aura::Window*, aura::Window*> tab_window_to_browser_window_;
136 
137   DISALLOW_COPY_AND_ASSIGN(AppServiceInstanceRegistryHelper);
138 };
139 
140 #endif  // CHROME_BROWSER_UI_ASH_LAUNCHER_APP_SERVICE_APP_SERVICE_INSTANCE_REGISTRY_HELPER_H_
141