blob: ba154b25d8edcec69b1b345c15c1af16d0b111d8 (
plain)
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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/cocoa/handoff_active_url_observer.h"
#include "base/logging.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/cocoa/handoff_active_url_observer_delegate.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "content/public/browser/web_contents.h"
HandoffActiveURLObserver::HandoffActiveURLObserver(
HandoffActiveURLObserverDelegate* delegate)
: delegate_(delegate),
active_browser_(nullptr) {
DCHECK(delegate_);
BrowserList::AddObserver(this);
SetActiveBrowser(chrome::FindLastActive());
}
HandoffActiveURLObserver::~HandoffActiveURLObserver() {
BrowserList::RemoveObserver(this);
SetActiveBrowser(nullptr);
}
void HandoffActiveURLObserver::OnBrowserSetLastActive(Browser* browser) {
SetActiveBrowser(browser);
delegate_->HandoffActiveURLChanged(GetActiveWebContents());
}
void HandoffActiveURLObserver::OnBrowserRemoved(Browser* removed_browser) {
if (active_browser_ != removed_browser)
return;
SetActiveBrowser(chrome::FindLastActive());
delegate_->HandoffActiveURLChanged(GetActiveWebContents());
}
void HandoffActiveURLObserver::ActiveTabChanged(
content::WebContents* old_contents,
content::WebContents* new_contents,
int index,
int reason) {
StartObservingWebContents(new_contents);
delegate_->HandoffActiveURLChanged(new_contents);
}
void HandoffActiveURLObserver::DidNavigateMainFrame(
const content::LoadCommittedDetails& details,
const content::FrameNavigateParams& params) {
delegate_->HandoffActiveURLChanged(web_contents());
}
void HandoffActiveURLObserver::SetActiveBrowser(Browser* active_browser) {
if (active_browser == active_browser_)
return;
if (active_browser_) {
active_browser_->tab_strip_model()->RemoveObserver(this);
StopObservingWebContents();
}
active_browser_ = active_browser;
if (active_browser_) {
active_browser_->tab_strip_model()->AddObserver(this);
content::WebContents* web_contents = GetActiveWebContents();
if (web_contents)
StartObservingWebContents(web_contents);
}
}
void HandoffActiveURLObserver::StartObservingWebContents(
content::WebContents* web_contents) {
DCHECK(web_contents);
Observe(web_contents);
}
void HandoffActiveURLObserver::StopObservingWebContents() {
Observe(nullptr);
}
content::WebContents* HandoffActiveURLObserver::GetActiveWebContents() {
if (!active_browser_)
return nullptr;
return active_browser_->tab_strip_model()->GetActiveWebContents();
}
|