Revert of Remove OneClickSigninHelper since it is no longer used. (patchset #5 id...
[chromium-blink-merge.git] / chrome / browser / ui / bookmarks / bookmark_tab_helper.cc
blob0d390aad4090875333201ad3e418ebf136861976
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_factory.h"
8 #include "chrome/browser/defaults.h"
9 #include "chrome/browser/prefs/pref_service_syncable.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/search/search.h"
12 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper_delegate.h"
13 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
14 #include "chrome/browser/ui/sad_tab.h"
15 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
16 #include "chrome/common/pref_names.h"
17 #include "components/bookmarks/browser/bookmark_model.h"
18 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/web_contents.h"
21 using bookmarks::BookmarkModel;
22 using bookmarks::BookmarkNode;
24 namespace {
26 bool IsNTPWebUI(content::WebContents* web_contents) {
27 content::WebUI* web_ui = NULL;
28 // Use the committed entry so the bookmarks bar disappears at the same time
29 // the page does.
30 if (web_contents->GetController().GetLastCommittedEntry())
31 web_ui = web_contents->GetCommittedWebUI();
32 else
33 web_ui = web_contents->GetWebUI();
34 return web_ui && NewTabUI::FromWebUIController(web_ui->GetController());
37 bool IsInstantNTP(content::WebContents* web_contents) {
38 // Use the committed entry so the bookmarks bar disappears at the same time
39 // the page does.
40 const content::NavigationEntry* entry =
41 web_contents->GetController().GetLastCommittedEntry();
42 if (!entry)
43 entry = web_contents->GetController().GetVisibleEntry();
44 return chrome::NavEntryIsInstantNTP(web_contents, entry);
47 } // namespace
49 DEFINE_WEB_CONTENTS_USER_DATA_KEY(BookmarkTabHelper);
51 BookmarkTabHelper::~BookmarkTabHelper() {
52 if (bookmark_model_)
53 bookmark_model_->RemoveObserver(this);
56 bool BookmarkTabHelper::ShouldShowBookmarkBar() const {
57 if (web_contents()->ShowingInterstitialPage())
58 return false;
60 if (chrome::SadTab::ShouldShow(web_contents()->GetCrashedStatus()))
61 return false;
63 if (!browser_defaults::bookmarks_enabled)
64 return false;
66 Profile* profile =
67 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
69 #if !defined(OS_CHROMEOS)
70 if (profile->IsGuestSession())
71 return false;
72 #endif
74 PrefService* prefs = profile->GetPrefs();
75 if (prefs->IsManagedPreference(bookmarks::prefs::kShowBookmarkBar) &&
76 !prefs->GetBoolean(bookmarks::prefs::kShowBookmarkBar))
77 return false;
79 return IsNTPWebUI(web_contents()) || IsInstantNTP(web_contents());
82 BookmarkTabHelper::BookmarkTabHelper(content::WebContents* web_contents)
83 : content::WebContentsObserver(web_contents),
84 is_starred_(false),
85 bookmark_model_(NULL),
86 delegate_(NULL),
87 bookmark_drag_(NULL) {
88 Profile* profile =
89 Profile::FromBrowserContext(web_contents->GetBrowserContext());
90 bookmark_model_ = BookmarkModelFactory::GetForProfile(profile);
91 if (bookmark_model_)
92 bookmark_model_->AddObserver(this);
95 void BookmarkTabHelper::UpdateStarredStateForCurrentURL() {
96 const bool old_state = is_starred_;
97 is_starred_ = (bookmark_model_ &&
98 bookmark_model_->IsBookmarked(chrome::GetURLToBookmark(web_contents())));
100 if (is_starred_ != old_state && delegate_)
101 delegate_->URLStarredChanged(web_contents(), is_starred_);
104 void BookmarkTabHelper::BookmarkModelChanged() {
107 void BookmarkTabHelper::BookmarkModelLoaded(BookmarkModel* model,
108 bool ids_reassigned) {
109 UpdateStarredStateForCurrentURL();
112 void BookmarkTabHelper::BookmarkNodeAdded(BookmarkModel* model,
113 const BookmarkNode* parent,
114 int index) {
115 UpdateStarredStateForCurrentURL();
118 void BookmarkTabHelper::BookmarkNodeRemoved(
119 BookmarkModel* model,
120 const BookmarkNode* parent,
121 int old_index,
122 const BookmarkNode* node,
123 const std::set<GURL>& removed_urls) {
124 UpdateStarredStateForCurrentURL();
127 void BookmarkTabHelper::BookmarkAllUserNodesRemoved(
128 BookmarkModel* model,
129 const std::set<GURL>& removed_urls) {
130 UpdateStarredStateForCurrentURL();
133 void BookmarkTabHelper::BookmarkNodeChanged(BookmarkModel* model,
134 const BookmarkNode* node) {
135 UpdateStarredStateForCurrentURL();
138 void BookmarkTabHelper::DidNavigateMainFrame(
139 const content::LoadCommittedDetails& /*details*/,
140 const content::FrameNavigateParams& /*params*/) {
141 UpdateStarredStateForCurrentURL();
144 void BookmarkTabHelper::DidStartNavigationToPendingEntry(
145 const GURL& /*url*/,
146 content::NavigationController::ReloadType /*reload_type*/) {
147 UpdateStarredStateForCurrentURL();