| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #include "chrome/browser/ui/bookmarks/bookmarks_tab_helper.h" | |
| 6 | |
| 7 #include "chrome/browser/bookmarks/bookmark_model.h" | |
| 8 #include "chrome/browser/profiles/profile.h" | |
| 9 #include "chrome/browser/ui/bookmarks/bookmarks_tab_helper_delegate.h" | |
| 10 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
| 11 #include "content/common/notification_service.h" | |
| 12 | |
| 13 BookmarksTabHelper::BookmarksTabHelper(TabContentsWrapper* tab_contents) | |
| 14 : TabContentsObserver(tab_contents->tab_contents()), | |
| 15 is_starred_(false), | |
| 16 tab_contents_wrapper_(tab_contents), | |
| 17 delegate_(NULL) { | |
| 18 // Register for notifications about URL starredness changing on any profile. | |
| 19 registrar_.Add(this, NotificationType::URLS_STARRED, | |
| 20 NotificationService::AllSources()); | |
| 21 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED, | |
| 22 NotificationService::AllSources()); | |
| 23 } | |
| 24 | |
| 25 BookmarksTabHelper::~BookmarksTabHelper() { | |
| 26 // We don't want any notifications while we're running our destructor. | |
| 27 registrar_.RemoveAll(); | |
| 28 } | |
| 29 | |
| 30 void BookmarksTabHelper::DidNavigateMainFramePostCommit( | |
| 31 const NavigationController::LoadCommittedDetails& /*details*/, | |
| 32 const ViewHostMsg_FrameNavigate_Params& /*params*/) { | |
| 33 UpdateStarredStateForCurrentURL(); | |
| 34 } | |
| 35 | |
| 36 void BookmarksTabHelper::Observe(NotificationType type, | |
| 37 const NotificationSource& source, | |
| 38 const NotificationDetails& details) { | |
| 39 switch (type.value) { | |
| 40 case NotificationType::BOOKMARK_MODEL_LOADED: | |
| 41 // BookmarkModel finished loading, fall through to update starred state. | |
| 42 case NotificationType::URLS_STARRED: { | |
| 43 // Somewhere, a URL has been starred. | |
| 44 // Ignore notifications for profiles other than our current one. | |
| 45 Profile* source_profile = Source<Profile>(source).ptr(); | |
| 46 if (!source_profile || | |
| 47 !source_profile->IsSameProfile(tab_contents_wrapper_->profile())) | |
| 48 return; | |
| 49 | |
| 50 UpdateStarredStateForCurrentURL(); | |
| 51 break; | |
| 52 } | |
| 53 | |
| 54 default: | |
| 55 NOTREACHED(); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 void BookmarksTabHelper::UpdateStarredStateForCurrentURL() { | |
| 60 BookmarkModel* model = tab_contents()->profile()->GetBookmarkModel(); | |
| 61 const bool old_state = is_starred_; | |
| 62 is_starred_ = (model && model->IsBookmarked(tab_contents()->GetURL())); | |
| 63 | |
| 64 if (is_starred_ != old_state && delegate()) | |
| 65 delegate()->URLStarredChanged(tab_contents_wrapper_, is_starred_); | |
| 66 } | |
| OLD | NEW |