1 // Copyright 2014 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.
5 #include "chrome/browser/ui/cocoa/handoff_active_url_observer.h"
7 #include "base/logging.h"
8 #include "chrome/browser/ui/browser_finder.h"
9 #include "chrome/browser/ui/browser_list.h"
10 #include "chrome/browser/ui/cocoa/handoff_active_url_observer_delegate.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "content/public/browser/web_contents.h"
14 HandoffActiveURLObserver::HandoffActiveURLObserver(
15 HandoffActiveURLObserverDelegate
* delegate
)
16 : delegate_(delegate
),
17 active_tab_strip_model_(nullptr),
18 active_browser_(nullptr) {
21 active_browser_
= chrome::FindLastActiveWithHostDesktopType(
22 chrome::HOST_DESKTOP_TYPE_NATIVE
);
23 BrowserList::AddObserver(this);
27 HandoffActiveURLObserver::~HandoffActiveURLObserver() {
28 BrowserList::RemoveObserver(this);
29 StopObservingTabStripModel();
30 StopObservingWebContents();
33 void HandoffActiveURLObserver::OnBrowserSetLastActive(Browser
* browser
) {
34 active_browser_
= browser
;
36 delegate_
->HandoffActiveURLChanged(GetActiveWebContents());
39 void HandoffActiveURLObserver::OnBrowserRemoved(Browser
* removed_browser
) {
40 if (active_browser_
!= removed_browser
)
43 active_browser_
= chrome::FindLastActiveWithHostDesktopType(
44 chrome::HOST_DESKTOP_TYPE_NATIVE
);
46 delegate_
->HandoffActiveURLChanged(GetActiveWebContents());
49 void HandoffActiveURLObserver::ActiveTabChanged(
50 content::WebContents
* old_contents
,
51 content::WebContents
* new_contents
,
54 StartObservingWebContents(new_contents
);
55 delegate_
->HandoffActiveURLChanged(new_contents
);
58 void HandoffActiveURLObserver::TabStripModelDeleted() {
59 StopObservingTabStripModel();
60 StopObservingWebContents();
63 void HandoffActiveURLObserver::DidNavigateMainFrame(
64 const content::LoadCommittedDetails
& details
,
65 const content::FrameNavigateParams
& params
) {
66 delegate_
->HandoffActiveURLChanged(web_contents());
69 void HandoffActiveURLObserver::WebContentsDestroyed() {
70 StopObservingWebContents();
73 void HandoffActiveURLObserver::UpdateObservations() {
74 if (!active_browser_
) {
75 StopObservingTabStripModel();
76 StopObservingWebContents();
80 TabStripModel
* model
= active_browser_
->tab_strip_model();
81 StartObservingTabStripModel(model
);
83 content::WebContents
* web_contents
= model
->GetActiveWebContents();
85 StartObservingWebContents(web_contents
);
87 StopObservingWebContents();
90 void HandoffActiveURLObserver::StartObservingTabStripModel(
91 TabStripModel
* tab_strip_model
) {
92 DCHECK(tab_strip_model
);
94 if (active_tab_strip_model_
== tab_strip_model
)
97 StopObservingTabStripModel();
98 tab_strip_model
->AddObserver(this);
99 active_tab_strip_model_
= tab_strip_model
;
102 void HandoffActiveURLObserver::StopObservingTabStripModel() {
103 if (active_tab_strip_model_
) {
104 active_tab_strip_model_
->RemoveObserver(this);
105 active_tab_strip_model_
= nullptr;
109 void HandoffActiveURLObserver::StartObservingWebContents(
110 content::WebContents
* web_contents
) {
111 DCHECK(web_contents
);
112 Observe(web_contents
);
115 void HandoffActiveURLObserver::StopObservingWebContents() {
119 content::WebContents
* HandoffActiveURLObserver::GetActiveWebContents() {
120 if (!active_browser_
)
123 return active_browser_
->tab_strip_model()->GetActiveWebContents();