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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
// Copyright (c) 2012 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/bookmarks/bookmark_tab_helper.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/prefs/pref_service_syncable.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/ui/bookmarks/bookmark_tab_helper_delegate.h"
#include "chrome/browser/ui/bookmarks/bookmark_utils.h"
#include "chrome/browser/ui/sad_tab.h"
#include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
#include "chrome/common/pref_names.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
namespace {
bool IsNTPWebUI(content::WebContents* web_contents) {
content::WebUI* web_ui = NULL;
// Use the committed entry so the bookmarks bar disappears at the same time
// the page does.
if (web_contents->GetController().GetLastCommittedEntry())
web_ui = web_contents->GetCommittedWebUI();
else
web_ui = web_contents->GetWebUI();
return web_ui && NewTabUI::FromWebUIController(web_ui->GetController());
}
bool IsInstantNTP(content::WebContents* web_contents) {
// Use the committed entry so the bookmarks bar disappears at the same time
// the page does.
const content::NavigationEntry* entry =
web_contents->GetController().GetLastCommittedEntry();
if (!entry)
entry = web_contents->GetController().GetVisibleEntry();
return chrome::NavEntryIsInstantNTP(web_contents, entry);
}
} // namespace
DEFINE_WEB_CONTENTS_USER_DATA_KEY(BookmarkTabHelper);
BookmarkTabHelper::~BookmarkTabHelper() {
if (bookmark_model_)
bookmark_model_->RemoveObserver(this);
}
bool BookmarkTabHelper::ShouldShowBookmarkBar() const {
if (web_contents()->ShowingInterstitialPage())
return false;
if (chrome::SadTab::ShouldShow(web_contents()->GetCrashedStatus()))
return false;
if (!browser_defaults::bookmarks_enabled)
return false;
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
#if !defined(OS_CHROMEOS)
if (profile->IsGuestSession())
return false;
#endif
PrefService* prefs = profile->GetPrefs();
if (prefs->IsManagedPreference(prefs::kShowBookmarkBar) &&
!prefs->GetBoolean(prefs::kShowBookmarkBar))
return false;
return IsNTPWebUI(web_contents()) || IsInstantNTP(web_contents());
}
BookmarkTabHelper::BookmarkTabHelper(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
is_starred_(false),
bookmark_model_(NULL),
delegate_(NULL),
bookmark_drag_(NULL) {
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
bookmark_model_= BookmarkModelFactory::GetForProfile(profile);
if (bookmark_model_)
bookmark_model_->AddObserver(this);
}
void BookmarkTabHelper::UpdateStarredStateForCurrentURL() {
const bool old_state = is_starred_;
is_starred_ = (bookmark_model_ &&
bookmark_model_->IsBookmarked(chrome::GetURLToBookmark(web_contents())));
if (is_starred_ != old_state && delegate_)
delegate_->URLStarredChanged(web_contents(), is_starred_);
}
void BookmarkTabHelper::BookmarkModelChanged() {
}
void BookmarkTabHelper::BookmarkModelLoaded(BookmarkModel* model,
bool ids_reassigned) {
UpdateStarredStateForCurrentURL();
}
void BookmarkTabHelper::BookmarkNodeAdded(BookmarkModel* model,
const BookmarkNode* parent,
int index) {
UpdateStarredStateForCurrentURL();
}
void BookmarkTabHelper::BookmarkNodeRemoved(
BookmarkModel* model,
const BookmarkNode* parent,
int old_index,
const BookmarkNode* node,
const std::set<GURL>& removed_urls) {
UpdateStarredStateForCurrentURL();
}
void BookmarkTabHelper::BookmarkAllUserNodesRemoved(
BookmarkModel* model,
const std::set<GURL>& removed_urls) {
UpdateStarredStateForCurrentURL();
}
void BookmarkTabHelper::BookmarkNodeChanged(BookmarkModel* model,
const BookmarkNode* node) {
UpdateStarredStateForCurrentURL();
}
void BookmarkTabHelper::DidNavigateMainFrame(
const content::LoadCommittedDetails& /*details*/,
const content::FrameNavigateParams& /*params*/) {
UpdateStarredStateForCurrentURL();
}
|