1 // Copyright (c) 2012 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/bookmarks/bookmark_tab_helper.h"
7 #include "chrome/browser/bookmarks/bookmark_model.h"
8 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
9 #include "chrome/browser/defaults.h"
10 #include "chrome/browser/prefs/pref_service_syncable.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search/search.h"
13 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper_delegate.h"
14 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
15 #include "chrome/browser/ui/sad_tab.h"
16 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
17 #include "chrome/common/pref_names.h"
18 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/web_contents.h"
23 bool IsNTPWebUI(content::WebContents
* web_contents
) {
24 content::WebUI
* web_ui
= NULL
;
25 // Use the committed entry so the bookmarks bar disappears at the same time
27 if (web_contents
->GetController().GetLastCommittedEntry())
28 web_ui
= web_contents
->GetCommittedWebUI();
30 web_ui
= web_contents
->GetWebUI();
31 return web_ui
&& NewTabUI::FromWebUIController(web_ui
->GetController());
34 bool IsInstantNTP(content::WebContents
* web_contents
) {
35 // Use the committed entry so the bookmarks bar disappears at the same time
37 const content::NavigationEntry
* entry
=
38 web_contents
->GetController().GetLastCommittedEntry();
40 entry
= web_contents
->GetController().GetVisibleEntry();
41 return chrome::NavEntryIsInstantNTP(web_contents
, entry
);
46 DEFINE_WEB_CONTENTS_USER_DATA_KEY(BookmarkTabHelper
);
48 BookmarkTabHelper::~BookmarkTabHelper() {
50 bookmark_model_
->RemoveObserver(this);
53 bool BookmarkTabHelper::ShouldShowBookmarkBar() const {
54 if (web_contents()->ShowingInterstitialPage())
57 if (chrome::SadTab::ShouldShow(web_contents()->GetCrashedStatus()))
60 if (!browser_defaults::bookmarks_enabled
)
64 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
65 PrefService
* prefs
= profile
->GetPrefs();
66 if (prefs
->IsManagedPreference(prefs::kShowBookmarkBar
) &&
67 !prefs
->GetBoolean(prefs::kShowBookmarkBar
))
70 return IsNTPWebUI(web_contents()) || IsInstantNTP(web_contents());
73 BookmarkTabHelper::BookmarkTabHelper(content::WebContents
* web_contents
)
74 : content::WebContentsObserver(web_contents
),
76 bookmark_model_(NULL
),
78 bookmark_drag_(NULL
) {
80 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
81 bookmark_model_
= BookmarkModelFactory::GetForProfile(profile
);
83 bookmark_model_
->AddObserver(this);
86 void BookmarkTabHelper::UpdateStarredStateForCurrentURL() {
87 const bool old_state
= is_starred_
;
88 is_starred_
= (bookmark_model_
&&
89 bookmark_model_
->IsBookmarked(chrome::GetURLToBookmark(web_contents())));
91 if (is_starred_
!= old_state
&& delegate_
)
92 delegate_
->URLStarredChanged(web_contents(), is_starred_
);
95 void BookmarkTabHelper::BookmarkModelChanged() {
98 void BookmarkTabHelper::Loaded(BookmarkModel
* model
, bool ids_reassigned
) {
99 UpdateStarredStateForCurrentURL();
102 void BookmarkTabHelper::BookmarkNodeAdded(BookmarkModel
* model
,
103 const BookmarkNode
* parent
,
105 UpdateStarredStateForCurrentURL();
108 void BookmarkTabHelper::BookmarkNodeRemoved(BookmarkModel
* model
,
109 const BookmarkNode
* parent
,
111 const BookmarkNode
* node
) {
112 UpdateStarredStateForCurrentURL();
115 void BookmarkTabHelper::BookmarkAllNodesRemoved(BookmarkModel
* model
) {
116 UpdateStarredStateForCurrentURL();
119 void BookmarkTabHelper::BookmarkNodeChanged(BookmarkModel
* model
,
120 const BookmarkNode
* node
) {
121 UpdateStarredStateForCurrentURL();
124 void BookmarkTabHelper::DidNavigateMainFrame(
125 const content::LoadCommittedDetails
& /*details*/,
126 const content::FrameNavigateParams
& /*params*/) {
127 UpdateStarredStateForCurrentURL();