OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/enhanced_bookmarks/bookmark_server_service.h" |
| 6 |
| 7 #include "base/auto_reset.h" |
| 8 #include "components/bookmarks/browser/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" |
| 12 #include "components/signin/core/browser/signin_manager_base.h" |
| 13 #include "google_apis/gaia/gaia_constants.h" |
| 14 #include "net/base/load_flags.h" |
| 15 #include "net/url_request/url_request_context_getter.h" |
| 16 #include "ui/base/models/tree_node_iterator.h" |
| 17 |
| 18 namespace enhanced_bookmarks { |
| 19 |
| 20 BookmarkServerService::BookmarkServerService( |
| 21 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 22 ProfileOAuth2TokenService* token_service, |
| 23 SigninManagerBase* signin_manager, |
| 24 BookmarkModel* bookmark_model) |
| 25 : OAuth2TokenService::Consumer("bookmark_server_service"), |
| 26 bookmark_model_(bookmark_model), |
| 27 token_service_(token_service), |
| 28 signin_manager_(signin_manager), |
| 29 request_context_getter_(request_context_getter), |
| 30 inhibit_change_notifications_(false) { |
| 31 DCHECK(request_context_getter.get()); |
| 32 DCHECK(token_service); |
| 33 DCHECK(signin_manager); |
| 34 DCHECK(bookmark_model); |
| 35 bookmark_model_->AddObserver(this); |
| 36 if (bookmark_model_->loaded()) |
| 37 BuildIdMap(); |
| 38 } |
| 39 |
| 40 BookmarkServerService::~BookmarkServerService() { |
| 41 bookmark_model_->RemoveObserver(this); |
| 42 } |
| 43 |
| 44 void BookmarkServerService::AddObserver( |
| 45 BookmarkServerServiceObserver* observer) { |
| 46 observers_.AddObserver(observer); |
| 47 } |
| 48 |
| 49 void BookmarkServerService::RemoveObserver( |
| 50 BookmarkServerServiceObserver* observer) { |
| 51 observers_.RemoveObserver(observer); |
| 52 } |
| 53 |
| 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( |
| 72 const std::string& remote_id) const { |
| 73 std::map<std::string, const BookmarkNode*>::const_iterator it = |
| 74 starsid_to_bookmark_.find(remote_id); |
| 75 if (it == starsid_to_bookmark_.end()) |
| 76 return NULL; |
| 77 return it->second; |
| 78 } |
| 79 |
| 80 const std::string BookmarkServerService::RemoteIDForBookmark( |
| 81 const BookmarkNode* bookmark) const { |
| 82 return enhanced_bookmarks::RemoteIdFromBookmark(bookmark_model_, bookmark); |
| 83 } |
| 84 |
| 85 void BookmarkServerService::Notify() { |
| 86 FOR_EACH_OBSERVER(BookmarkServerServiceObserver, observers_, OnChange(this)); |
| 87 } |
| 88 |
| 89 void BookmarkServerService::TriggerTokenRequest(bool cancel_previous) { |
| 90 if (cancel_previous) |
| 91 url_fetcher_.reset(); |
| 92 |
| 93 if (token_request_ || url_fetcher_) |
| 94 return; // Fetcher is already running. |
| 95 |
| 96 const std::string username(signin_manager_->GetAuthenticatedUsername()); |
| 97 if (!username.length()) { |
| 98 // User is not signed in. |
| 99 CleanAfterFailure(); |
| 100 Notify(); |
| 101 return; |
| 102 } |
| 103 // Find a token. |
| 104 OAuth2TokenService::ScopeSet scopes; |
| 105 scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); |
| 106 token_request_ = token_service_->StartRequest(username, scopes, this); |
| 107 } |
| 108 |
| 109 // |
| 110 // OAuth2AccessTokenConsumer methods. |
| 111 // |
| 112 void BookmarkServerService::OnGetTokenSuccess( |
| 113 const OAuth2TokenService::Request* request, |
| 114 const std::string& access_token, |
| 115 const base::Time& expiration_time) { |
| 116 url_fetcher_.reset(CreateFetcher()); |
| 117 // Free the token request. |
| 118 token_request_.reset(); |
| 119 |
| 120 if (!url_fetcher_) { |
| 121 CleanAfterFailure(); |
| 122 Notify(); |
| 123 return; |
| 124 } |
| 125 url_fetcher_->SetRequestContext(request_context_getter_.get()); |
| 126 |
| 127 // Add the token. |
| 128 std::string headers; |
| 129 headers = "Authorization: Bearer "; |
| 130 headers += access_token; |
| 131 headers += "\r\n"; |
| 132 url_fetcher_->SetExtraRequestHeaders(headers); |
| 133 |
| 134 // Do not pollute the cookie store with cruft, or mix the users cookie in this |
| 135 // request. |
| 136 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 137 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 138 |
| 139 url_fetcher_->Start(); |
| 140 } |
| 141 |
| 142 void BookmarkServerService::OnGetTokenFailure( |
| 143 const OAuth2TokenService::Request* request, |
| 144 const GoogleServiceAuthError& error) { |
| 145 // Free the request. |
| 146 token_request_.reset(); |
| 147 CleanAfterFailure(); |
| 148 Notify(); |
| 149 } |
| 150 |
| 151 // |
| 152 // net::URLFetcherDelegate methods. |
| 153 // |
| 154 void BookmarkServerService::OnURLFetchComplete(const net::URLFetcher* source) { |
| 155 scoped_ptr<net::URLFetcher> url_fetcher(url_fetcher_.Pass()); |
| 156 std::string response; |
| 157 bool should_notify = true; |
| 158 |
| 159 if (url_fetcher->GetResponseCode() != 200 || |
| 160 !url_fetcher->GetResponseAsString(&response) || |
| 161 !ProcessResponse(response, &should_notify)) { |
| 162 CleanAfterFailure(); |
| 163 } |
| 164 if (should_notify) |
| 165 Notify(); |
| 166 } |
| 167 |
| 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() { |
| 231 DCHECK(signin_manager_); |
| 232 return signin_manager_; |
| 233 } |
| 234 |
| 235 } // namespace enhanced_bookmarks |
OLD | NEW |