| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h" | 5 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h" |
| 6 | 6 |
| 7 #include "chrome/browser/bookmarks/bookmark_model.h" | 7 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 8 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 8 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 9 #include "chrome/browser/bookmarks/bookmark_node_data.h" | 9 #include "chrome/browser/bookmarks/bookmark_node_data.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper_delegate.h" | 11 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper_delegate.h" |
| 12 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 12 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 13 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 13 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
| 14 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "content/public/browser/navigation_controller.h" | 15 #include "content/public/browser/navigation_controller.h" |
| 16 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
| 17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 18 | 18 |
| 19 int BookmarkTabHelper::kUserDataKey; |
| 20 |
| 19 namespace { | 21 namespace { |
| 20 | 22 |
| 21 bool CanShowBookmarkBar(content::WebUI* ui) { | 23 bool CanShowBookmarkBar(content::WebUI* ui) { |
| 22 if (!ui) | 24 if (!ui) |
| 23 return false; | 25 return false; |
| 24 NewTabUI* new_tab = NewTabUI::FromWebUIController(ui->GetController()); | 26 NewTabUI* new_tab = NewTabUI::FromWebUIController(ui->GetController()); |
| 25 return new_tab && new_tab->CanShowBookmarkBar(); | 27 return new_tab && new_tab->CanShowBookmarkBar(); |
| 26 } | 28 } |
| 27 | 29 |
| 28 } // namespace | 30 } // namespace |
| 29 | 31 |
| 30 BookmarkTabHelper::BookmarkTabHelper(TabContents* tab_contents) | 32 BookmarkTabHelper::BookmarkTabHelper(content::WebContents* web_contents) |
| 31 : content::WebContentsObserver(tab_contents->web_contents()), | 33 : content::WebContentsObserver(web_contents), |
| 32 is_starred_(false), | 34 is_starred_(false), |
| 33 tab_contents_(tab_contents), | |
| 34 delegate_(NULL), | 35 delegate_(NULL), |
| 35 bookmark_drag_(NULL) { | 36 bookmark_drag_(NULL) { |
| 36 // Register for notifications about URL starredness changing on any profile. | 37 // Register for notifications about URL starredness changing on any profile. |
| 37 registrar_.Add(this, chrome::NOTIFICATION_URLS_STARRED, | 38 registrar_.Add(this, chrome::NOTIFICATION_URLS_STARRED, |
| 38 content::NotificationService::AllBrowserContextsAndSources()); | 39 content::NotificationService::AllBrowserContextsAndSources()); |
| 39 registrar_.Add(this, chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED, | 40 registrar_.Add(this, chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED, |
| 40 content::NotificationService::AllBrowserContextsAndSources()); | 41 content::NotificationService::AllBrowserContextsAndSources()); |
| 41 } | 42 } |
| 42 | 43 |
| 43 BookmarkTabHelper::~BookmarkTabHelper() { | 44 BookmarkTabHelper::~BookmarkTabHelper() { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 73 void BookmarkTabHelper::Observe(int type, | 74 void BookmarkTabHelper::Observe(int type, |
| 74 const content::NotificationSource& source, | 75 const content::NotificationSource& source, |
| 75 const content::NotificationDetails& details) { | 76 const content::NotificationDetails& details) { |
| 76 switch (type) { | 77 switch (type) { |
| 77 case chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED: | 78 case chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED: |
| 78 // BookmarkModel finished loading, fall through to update starred state. | 79 // BookmarkModel finished loading, fall through to update starred state. |
| 79 case chrome::NOTIFICATION_URLS_STARRED: { | 80 case chrome::NOTIFICATION_URLS_STARRED: { |
| 80 // Somewhere, a URL has been starred. | 81 // Somewhere, a URL has been starred. |
| 81 // Ignore notifications for profiles other than our current one. | 82 // Ignore notifications for profiles other than our current one. |
| 82 Profile* source_profile = content::Source<Profile>(source).ptr(); | 83 Profile* source_profile = content::Source<Profile>(source).ptr(); |
| 83 if (!source_profile || | 84 Profile* this_profile = |
| 84 !source_profile->IsSameProfile(tab_contents_->profile())) | 85 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 86 if (!source_profile || !source_profile->IsSameProfile(this_profile)) |
| 85 return; | 87 return; |
| 86 | 88 |
| 87 UpdateStarredStateForCurrentURL(); | 89 UpdateStarredStateForCurrentURL(); |
| 88 break; | 90 break; |
| 89 } | 91 } |
| 90 | 92 |
| 91 default: | 93 default: |
| 92 NOTREACHED(); | 94 NOTREACHED(); |
| 93 } | 95 } |
| 94 } | 96 } |
| 95 | 97 |
| 96 void BookmarkTabHelper::SetBookmarkDragDelegate( | 98 void BookmarkTabHelper::SetBookmarkDragDelegate( |
| 97 BookmarkTabHelper::BookmarkDrag* bookmark_drag) { | 99 BookmarkTabHelper::BookmarkDrag* bookmark_drag) { |
| 98 bookmark_drag_ = bookmark_drag; | 100 bookmark_drag_ = bookmark_drag; |
| 99 } | 101 } |
| 100 | 102 |
| 101 BookmarkTabHelper::BookmarkDrag* | 103 BookmarkTabHelper::BookmarkDrag* |
| 102 BookmarkTabHelper::GetBookmarkDragDelegate() { | 104 BookmarkTabHelper::GetBookmarkDragDelegate() { |
| 103 return bookmark_drag_; | 105 return bookmark_drag_; |
| 104 } | 106 } |
| 105 | 107 |
| 106 void BookmarkTabHelper::UpdateStarredStateForCurrentURL() { | 108 void BookmarkTabHelper::UpdateStarredStateForCurrentURL() { |
| 107 Profile* profile = | 109 Profile* profile = |
| 108 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 110 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 109 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile); | 111 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile); |
| 110 const bool old_state = is_starred_; | 112 const bool old_state = is_starred_; |
| 111 is_starred_ = (model && model->IsBookmarked(web_contents()->GetURL())); | 113 is_starred_ = (model && model->IsBookmarked(web_contents()->GetURL())); |
| 112 | 114 |
| 113 if (is_starred_ != old_state && delegate()) | 115 if (is_starred_ != old_state && delegate()) |
| 114 delegate()->URLStarredChanged(tab_contents_, is_starred_); | 116 delegate()->URLStarredChanged(web_contents(), is_starred_); |
| 115 } | 117 } |
| OLD | NEW |