Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: chrome/browser/ui/ash/launcher/browser_status_monitor.cc

Issue 23708028: Reduce calling count of UpdateAppState() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Monitor active tab's webcontents by using WebContenetsObserver. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/ash/launcher/browser_status_monitor.h" 5 #include "chrome/browser/ui/ash/launcher/browser_status_monitor.h"
6 6
7 #include "ash/shell.h" 7 #include "ash/shell.h"
8 #include "ash/wm/window_util.h" 8 #include "ash/wm/window_util.h"
9 #include "base/stl_util.h"
9 #include "chrome/browser/ui/ash/launcher/browser_shortcut_launcher_item_controll er.h" 10 #include "chrome/browser/ui/ash/launcher/browser_shortcut_launcher_item_controll er.h"
10 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" 11 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
11 #include "chrome/browser/ui/browser.h" 12 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_finder.h" 13 #include "chrome/browser/ui/browser_finder.h"
13 #include "chrome/browser/ui/browser_list.h" 14 #include "chrome/browser/ui/browser_list.h"
14 #include "chrome/browser/ui/browser_window.h" 15 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h" 16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/browser/web_applications/web_app.h" 17 #include "chrome/browser/web_applications/web_app.h"
18 #include "content/public/browser/navigation_controller.h"
17 #include "content/public/browser/web_contents.h" 19 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_contents_view.h" 20 #include "content/public/browser/web_contents_view.h"
19 #include "ui/aura/client/activation_client.h" 21 #include "ui/aura/client/activation_client.h"
20 #include "ui/aura/root_window.h" 22 #include "ui/aura/root_window.h"
21 #include "ui/aura/window.h" 23 #include "ui/aura/window.h"
22 #include "ui/gfx/screen.h" 24 #include "ui/gfx/screen.h"
23 25
26 BrowserStatusMonitor::ActiveWebContentsObserver::ActiveWebContentsObserver(
27 content::WebContents* contents,
28 BrowserStatusMonitor* monitor)
29 : content::WebContentsObserver(contents),
30 monitor_(monitor) {
31 }
32
33 BrowserStatusMonitor::ActiveWebContentsObserver::~ActiveWebContentsObserver() {
34 }
35
36 void BrowserStatusMonitor::ActiveWebContentsObserver::NavigateToPendingEntry(
37 const GURL& url,
38 content::NavigationController::ReloadType reload_type) {
39 // Don't need to update item state when page is reloaded.
Mr4D (OOO till 08-26) 2013/09/13 14:35:18 What about: No need to update the item state when
simonhong_ 2013/09/15 17:27:08 I removed this method and only DidNavigateMainFram
40 if (reload_type == content::NavigationController::NO_RELOAD) {
Mr4D (OOO till 08-26) 2013/09/13 14:35:18 No {} here.
simonhong_ 2013/09/15 17:27:08 Done.
41 monitor_->UpdateAppAndBrowserState(web_contents());
42 }
43 }
44
45 void BrowserStatusMonitor::ActiveWebContentsObserver::DidNavigateMainFrame(
46 const content::LoadCommittedDetails& details,
47 const content::FrameNavigateParams& params) {
48 monitor_->UpdateAppAndBrowserState(web_contents());
49 }
50
24 BrowserStatusMonitor::BrowserStatusMonitor( 51 BrowserStatusMonitor::BrowserStatusMonitor(
25 ChromeLauncherController* launcher_controller) 52 ChromeLauncherController* launcher_controller)
26 : launcher_controller_(launcher_controller), 53 : launcher_controller_(launcher_controller),
27 observed_activation_clients_(this), 54 observed_activation_clients_(this),
28 observed_root_windows_(this) { 55 observed_root_windows_(this) {
29 DCHECK(launcher_controller_); 56 DCHECK(launcher_controller_);
30 BrowserList::AddObserver(this); 57 BrowserList::AddObserver(this);
31 58
32 // This check needs for win7_aura. Without this, all tests in 59 // This check needs for win7_aura. Without this, all tests in
33 // ChromeLauncherController will fail in win7_aura. 60 // ChromeLauncherController will fail in win7_aura.
(...skipping 21 matching lines...) Expand all
55 ash::Shell::GetInstance()->GetScreen()->RemoveObserver(this); 82 ash::Shell::GetInstance()->GetScreen()->RemoveObserver(this);
56 83
57 BrowserList::RemoveObserver(this); 84 BrowserList::RemoveObserver(this);
58 85
59 BrowserList* browser_list = 86 BrowserList* browser_list =
60 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); 87 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
61 for (BrowserList::const_iterator i = browser_list->begin(); 88 for (BrowserList::const_iterator i = browser_list->begin();
62 i != browser_list->end(); ++i) { 89 i != browser_list->end(); ++i) {
63 OnBrowserRemoved(*i); 90 OnBrowserRemoved(*i);
64 } 91 }
92
93 STLDeleteContainerPairSecondPointers(webcontents_to_observer_map_.begin(),
94 webcontents_to_observer_map_.end());
65 } 95 }
66 96
67 void BrowserStatusMonitor::OnWindowActivated(aura::Window* gained_active, 97 void BrowserStatusMonitor::OnWindowActivated(aura::Window* gained_active,
68 aura::Window* lost_active) { 98 aura::Window* lost_active) {
69 Browser* browser = chrome::FindBrowserWithWindow(lost_active); 99 Browser* browser = chrome::FindBrowserWithWindow(lost_active);
70 if (browser) { 100 if (browser) {
71 UpdateAppAndBrowserState( 101 UpdateAppAndBrowserState(
72 browser->tab_strip_model()->GetActiveWebContents()); 102 browser->tab_strip_model()->GetActiveWebContents());
73 } 103 }
74 104
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 if (old_contents) 178 if (old_contents)
149 browser = chrome::FindBrowserWithWebContents(old_contents); 179 browser = chrome::FindBrowserWithWebContents(old_contents);
150 180
151 if (browser && browser->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH) 181 if (browser && browser->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH)
152 return; 182 return;
153 183
154 // Update immediately on a tab change. 184 // Update immediately on a tab change.
155 if (browser && 185 if (browser &&
156 (TabStripModel::kNoTab != 186 (TabStripModel::kNoTab !=
157 browser->tab_strip_model()->GetIndexOfWebContents(old_contents))) { 187 browser->tab_strip_model()->GetIndexOfWebContents(old_contents))) {
188 if (webcontents_to_observer_map_.find(old_contents) !=
Mr4D (OOO till 08-26) 2013/09/13 14:35:18 You might want to put this deletion code into a fu
simonhong_ 2013/09/15 17:27:08 I changed to observe all web contents. Inactive we
189 webcontents_to_observer_map_.end()) {
190 delete webcontents_to_observer_map_[old_contents];
191 webcontents_to_observer_map_.erase(old_contents);
192 }
193
158 launcher_controller_->UpdateAppState( 194 launcher_controller_->UpdateAppState(
159 old_contents, 195 old_contents,
160 ChromeLauncherController::APP_STATE_INACTIVE); 196 ChromeLauncherController::APP_STATE_INACTIVE);
161 } 197 }
162 198
163 UpdateAppAndBrowserState(new_contents); 199 if (new_contents) {
200 if (webcontents_to_observer_map_.find(new_contents) ==
201 webcontents_to_observer_map_.end()) {
202 webcontents_to_observer_map_[new_contents] =
203 new ActiveWebContentsObserver(new_contents, this);
204 }
205 UpdateAppAndBrowserState(new_contents);
206 }
164 } 207 }
165 208
166 void BrowserStatusMonitor::TabInsertedAt(content::WebContents* contents, 209 void BrowserStatusMonitor::TabInsertedAt(content::WebContents* contents,
167 int index, 210 int index,
168 bool foreground) { 211 bool foreground) {
169 UpdateAppAndBrowserState(contents); 212 UpdateAppAndBrowserState(contents);
170 } 213 }
171 214
172 void BrowserStatusMonitor::TabDetachedAt(content::WebContents* contents, 215 void BrowserStatusMonitor::TabClosingAt(TabStripModel* tab_strip_mode,
173 int index) { 216 content::WebContents* contents,
217 int index) {
Mr4D (OOO till 08-26) 2013/09/13 14:35:18 Is this really the same event? Did you try to drag
simonhong_ 2013/09/15 17:27:08 Not same event. When drag and drop a tab from one
218 if (webcontents_to_observer_map_.find(contents) !=
219 webcontents_to_observer_map_.end()) {
220 delete webcontents_to_observer_map_[contents];
221 webcontents_to_observer_map_.erase(contents);
222 }
223
174 launcher_controller_->UpdateAppState( 224 launcher_controller_->UpdateAppState(
175 contents, ChromeLauncherController::APP_STATE_REMOVED); 225 contents, ChromeLauncherController::APP_STATE_REMOVED);
176 UpdateBrowserItemState(); 226 UpdateBrowserItemState();
177 } 227 }
178 228
179 void BrowserStatusMonitor::TabChangedAt(
180 content::WebContents* contents,
181 int index,
182 TabStripModelObserver::TabChangeType change_type) {
183 UpdateAppAndBrowserState(contents);
184 }
185
186 void BrowserStatusMonitor::TabReplacedAt(TabStripModel* tab_strip_model,
187 content::WebContents* old_contents,
188 content::WebContents* new_contents,
189 int index) {
190 launcher_controller_->UpdateAppState(
191 old_contents,
192 ChromeLauncherController::APP_STATE_REMOVED);
193 UpdateAppAndBrowserState(new_contents);
194 }
195
196 void BrowserStatusMonitor::UpdateAppAndBrowserState( 229 void BrowserStatusMonitor::UpdateAppAndBrowserState(
197 content::WebContents* contents) { 230 content::WebContents* contents) {
198 if (contents) { 231 if (contents) {
199 ChromeLauncherController::AppState app_state = 232 ChromeLauncherController::AppState app_state =
200 ChromeLauncherController::APP_STATE_INACTIVE; 233 ChromeLauncherController::APP_STATE_INACTIVE;
201 234
202 Browser* browser = chrome::FindBrowserWithWebContents(contents); 235 Browser* browser = chrome::FindBrowserWithWebContents(contents);
203 DCHECK(browser); 236 DCHECK(browser);
204 if (browser->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH) 237 if (browser->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH)
205 return; 238 return;
206 if (browser->tab_strip_model()->GetActiveWebContents() == contents) { 239 if (browser->tab_strip_model()->GetActiveWebContents() == contents) {
207 if (browser->window()->IsActive()) 240 if (browser->window()->IsActive())
208 app_state = ChromeLauncherController::APP_STATE_WINDOW_ACTIVE; 241 app_state = ChromeLauncherController::APP_STATE_WINDOW_ACTIVE;
209 else 242 else
210 app_state = ChromeLauncherController::APP_STATE_ACTIVE; 243 app_state = ChromeLauncherController::APP_STATE_ACTIVE;
211 } 244 }
212 245
213 launcher_controller_->UpdateAppState(contents, app_state); 246 launcher_controller_->UpdateAppState(contents, app_state);
214 } 247 }
215 UpdateBrowserItemState(); 248 UpdateBrowserItemState();
216 } 249 }
217 250
218 void BrowserStatusMonitor::UpdateBrowserItemState() { 251 void BrowserStatusMonitor::UpdateBrowserItemState() {
219 launcher_controller_->GetBrowserShortcutLauncherItemController()-> 252 launcher_controller_->GetBrowserShortcutLauncherItemController()->
220 UpdateBrowserItemState(); 253 UpdateBrowserItemState();
221 } 254 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698