OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/enhanced_bookmarks/bookmark_server_service.h" | 5 #include "components/enhanced_bookmarks/bookmark_server_service.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "components/bookmarks/browser/bookmark_model.h" | 8 #include "components/enhanced_bookmarks/enhanced_bookmark_model.h" |
9 #include "components/bookmarks/browser/bookmark_model_observer.h" | |
10 #include "components/enhanced_bookmarks/metadata_accessor.h" | |
11 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 9 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
12 #include "components/signin/core/browser/signin_manager_base.h" | 10 #include "components/signin/core/browser/signin_manager_base.h" |
13 #include "google_apis/gaia/gaia_constants.h" | 11 #include "google_apis/gaia/gaia_constants.h" |
14 #include "net/base/load_flags.h" | 12 #include "net/base/load_flags.h" |
15 #include "net/url_request/url_request_context_getter.h" | 13 #include "net/url_request/url_request_context_getter.h" |
16 #include "ui/base/models/tree_node_iterator.h" | 14 #include "ui/base/models/tree_node_iterator.h" |
17 | 15 |
18 namespace enhanced_bookmarks { | 16 namespace enhanced_bookmarks { |
19 | 17 |
20 BookmarkServerService::BookmarkServerService( | 18 BookmarkServerService::BookmarkServerService( |
21 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 19 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
22 ProfileOAuth2TokenService* token_service, | 20 ProfileOAuth2TokenService* token_service, |
23 SigninManagerBase* signin_manager, | 21 SigninManagerBase* signin_manager, |
24 BookmarkModel* bookmark_model) | 22 EnhancedBookmarkModel* enhanced_bookmark_model) |
25 : OAuth2TokenService::Consumer("bookmark_server_service"), | 23 : OAuth2TokenService::Consumer("bookmark_server_service"), |
26 bookmark_model_(bookmark_model), | 24 model_(enhanced_bookmark_model), |
27 token_service_(token_service), | 25 token_service_(token_service), |
28 signin_manager_(signin_manager), | 26 signin_manager_(signin_manager), |
29 request_context_getter_(request_context_getter), | 27 request_context_getter_(request_context_getter) { |
30 inhibit_change_notifications_(false) { | |
31 DCHECK(request_context_getter.get()); | 28 DCHECK(request_context_getter.get()); |
32 DCHECK(token_service); | 29 DCHECK(token_service); |
33 DCHECK(signin_manager); | 30 DCHECK(signin_manager); |
34 DCHECK(bookmark_model); | 31 DCHECK(enhanced_bookmark_model); |
35 bookmark_model_->AddObserver(this); | 32 model_->AddObserver(this); |
36 if (bookmark_model_->loaded()) | |
37 BuildIdMap(); | |
38 } | 33 } |
39 | 34 |
40 BookmarkServerService::~BookmarkServerService() { | 35 BookmarkServerService::~BookmarkServerService() { |
41 bookmark_model_->RemoveObserver(this); | 36 model_->AddObserver(this); |
noyau (Ping after 24h)
2014/09/18 08:43:22
Remove observer maybe :)
Rune Fevang
2014/09/19 01:18:07
Doh! Thanks.
| |
42 } | 37 } |
43 | 38 |
44 void BookmarkServerService::AddObserver( | 39 void BookmarkServerService::AddObserver( |
45 BookmarkServerServiceObserver* observer) { | 40 BookmarkServerServiceObserver* observer) { |
46 observers_.AddObserver(observer); | 41 observers_.AddObserver(observer); |
47 } | 42 } |
48 | 43 |
49 void BookmarkServerService::RemoveObserver( | 44 void BookmarkServerService::RemoveObserver( |
50 BookmarkServerServiceObserver* observer) { | 45 BookmarkServerServiceObserver* observer) { |
51 observers_.RemoveObserver(observer); | 46 observers_.RemoveObserver(observer); |
52 } | 47 } |
53 | 48 |
54 void BookmarkServerService::BuildIdMap() { | |
55 ui::TreeNodeIterator<const BookmarkNode> iterator( | |
56 bookmark_model_->root_node()); | |
57 | |
58 while (iterator.has_next()) { | |
59 const BookmarkNode* bookmark = iterator.Next(); | |
60 if (bookmark_model_->is_permanent_node(bookmark)) | |
61 continue; | |
62 // RemoteIdFromBookmark() will create the ID if it doesn't exists yet. | |
63 std::string starid = | |
64 enhanced_bookmarks::RemoteIdFromBookmark(bookmark_model_, bookmark); | |
65 if (bookmark->is_url()) { | |
66 starsid_to_bookmark_[starid] = bookmark; | |
67 } | |
68 } | |
69 } | |
70 | |
71 const BookmarkNode* BookmarkServerService::BookmarkForRemoteId( | 49 const BookmarkNode* BookmarkServerService::BookmarkForRemoteId( |
72 const std::string& remote_id) const { | 50 const std::string& remote_id) const { |
73 std::map<std::string, const BookmarkNode*>::const_iterator it = | 51 std::map<std::string, const BookmarkNode*>::const_iterator it = |
74 starsid_to_bookmark_.find(remote_id); | 52 starsid_to_bookmark_.find(remote_id); |
75 if (it == starsid_to_bookmark_.end()) | 53 if (it == starsid_to_bookmark_.end()) |
76 return NULL; | 54 return NULL; |
77 return it->second; | 55 return it->second; |
78 } | 56 } |
79 | 57 |
80 const std::string BookmarkServerService::RemoteIDForBookmark( | 58 const std::string BookmarkServerService::RemoteIDForBookmark( |
81 const BookmarkNode* bookmark) const { | 59 const BookmarkNode* bookmark) const { |
82 return enhanced_bookmarks::RemoteIdFromBookmark(bookmark_model_, bookmark); | 60 return model_->GetRemoteId(bookmark); |
83 } | 61 } |
84 | 62 |
85 void BookmarkServerService::Notify() { | 63 void BookmarkServerService::Notify() { |
86 FOR_EACH_OBSERVER(BookmarkServerServiceObserver, observers_, OnChange(this)); | 64 FOR_EACH_OBSERVER(BookmarkServerServiceObserver, observers_, OnChange(this)); |
87 } | 65 } |
88 | 66 |
89 void BookmarkServerService::TriggerTokenRequest(bool cancel_previous) { | 67 void BookmarkServerService::TriggerTokenRequest(bool cancel_previous) { |
90 if (cancel_previous) | 68 if (cancel_previous) |
91 url_fetcher_.reset(); | 69 url_fetcher_.reset(); |
92 | 70 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
158 | 136 |
159 if (url_fetcher->GetResponseCode() != 200 || | 137 if (url_fetcher->GetResponseCode() != 200 || |
160 !url_fetcher->GetResponseAsString(&response) || | 138 !url_fetcher->GetResponseAsString(&response) || |
161 !ProcessResponse(response, &should_notify)) { | 139 !ProcessResponse(response, &should_notify)) { |
162 CleanAfterFailure(); | 140 CleanAfterFailure(); |
163 } | 141 } |
164 if (should_notify) | 142 if (should_notify) |
165 Notify(); | 143 Notify(); |
166 } | 144 } |
167 | 145 |
168 // | |
169 // BookmarkModelObserver methods. | |
170 // | |
171 void BookmarkServerService::BookmarkModelLoaded(BookmarkModel* model, | |
172 bool ids_reassigned) { | |
173 BuildIdMap(); | |
174 } | |
175 | |
176 void BookmarkServerService::BookmarkNodeAdded(BookmarkModel* model, | |
177 const BookmarkNode* parent, | |
178 int index) { | |
179 DCHECK(!inhibit_change_notifications_); | |
180 const BookmarkNode* bookmark = parent->GetChild(index); | |
181 if (!bookmark->is_url()) | |
182 return; | |
183 | |
184 base::AutoReset<bool> inhibitor(&inhibit_change_notifications_, true); | |
185 std::string starid = | |
186 enhanced_bookmarks::RemoteIdFromBookmark(model, bookmark); | |
187 starsid_to_bookmark_[starid] = bookmark; | |
188 } | |
189 | |
190 void BookmarkServerService::BookmarkNodeRemoved( | |
191 BookmarkModel* model, | |
192 const BookmarkNode* parent, | |
193 int old_index, | |
194 const BookmarkNode* node, | |
195 const std::set<GURL>& removed_urls) { | |
196 DCHECK(!inhibit_change_notifications_); | |
197 if (!node->is_url()) | |
198 return; | |
199 base::AutoReset<bool> inhibitor(&inhibit_change_notifications_, true); | |
200 std::string starid = enhanced_bookmarks::RemoteIdFromBookmark(model, node); | |
201 starsid_to_bookmark_.erase(starid); | |
202 } | |
203 | |
204 void BookmarkServerService::OnWillChangeBookmarkMetaInfo( | |
205 BookmarkModel* model, | |
206 const BookmarkNode* node) { | |
207 if (!node->is_url() || inhibit_change_notifications_) | |
208 return; | |
209 base::AutoReset<bool> inhibitor(&inhibit_change_notifications_, true); | |
210 std::string starid = enhanced_bookmarks::RemoteIdFromBookmark(model, node); | |
211 starsid_to_bookmark_.erase(starid); | |
212 } | |
213 | |
214 void BookmarkServerService::BookmarkMetaInfoChanged(BookmarkModel* model, | |
215 const BookmarkNode* node) { | |
216 if (!node->is_url() || inhibit_change_notifications_) | |
217 return; | |
218 | |
219 std::string starid = enhanced_bookmarks::RemoteIdFromBookmark(model, node); | |
220 starsid_to_bookmark_[starid] = node; | |
221 } | |
222 | |
223 void BookmarkServerService::BookmarkAllUserNodesRemoved( | |
224 BookmarkModel* model, | |
225 const std::set<GURL>& removed_urls) { | |
226 DCHECK(!inhibit_change_notifications_); | |
227 starsid_to_bookmark_.clear(); | |
228 } | |
229 | |
230 SigninManagerBase* BookmarkServerService::GetSigninManager() { | 146 SigninManagerBase* BookmarkServerService::GetSigninManager() { |
231 DCHECK(signin_manager_); | 147 DCHECK(signin_manager_); |
232 return signin_manager_; | 148 return signin_manager_; |
233 } | 149 } |
234 | 150 |
235 } // namespace enhanced_bookmarks | 151 } // namespace enhanced_bookmarks |
OLD | NEW |