Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(376)

Side by Side Diff: components/enhanced_bookmarks/bookmark_server_service.cc

Issue 538903003: Bring up of the server side full text search. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: s/SigninManager/SigninManagerBase for ChromeOS. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "net/base/load_flags.h"
14 #include "net/url_request/url_request_context_getter.h"
15 #include "ui/base/models/tree_node_iterator.h"
16
17 namespace {
18 const std::string kScope("https://www.googleapis.com/auth/chromesync");
Yaron 2014/09/05 04:37:52 use GaiaConstants::kChromeSyncOAuth2Scope instead.
noyau (Ping after 24h) 2014/09/08 09:46:48 Done.
19 }
20
21 namespace enhanced_bookmarks {
22
23 BookmarkServerService::BookmarkServerService(
24 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
25 ProfileOAuth2TokenService* token_service,
26 SigninManagerBase* signin_manager,
27 BookmarkModel* bookmark_model)
28 : OAuth2TokenService::Consumer("bookmark_server_service"),
29 bookmark_model_(bookmark_model),
30 token_service_(token_service),
31 signin_manager_(signin_manager),
32 request_context_getter_(request_context_getter),
33 inhibit_change_notifications_(false) {
34 DCHECK(request_context_getter);
35 DCHECK(token_service);
36 DCHECK(signin_manager);
37 DCHECK(bookmark_model);
38 bookmark_model_->AddObserver(this);
39 if (bookmark_model_->loaded())
40 BuildIdMap();
41 }
42
43 BookmarkServerService::~BookmarkServerService() {
44 bookmark_model_->RemoveObserver(this);
45 }
46
47 void BookmarkServerService::AddObserver(
48 BookmarkServerServiceObserver* observer) {
49 observers_.AddObserver(observer);
50 }
51
52 void BookmarkServerService::RemoveObserver(
53 BookmarkServerServiceObserver* observer) {
54 observers_.RemoveObserver(observer);
55 }
56
57 void BookmarkServerService::BuildIdMap() {
58 ui::TreeNodeIterator<const BookmarkNode> iterator(
59 bookmark_model_->root_node());
60
61 while (iterator.has_next()) {
62 const BookmarkNode* bookmark = iterator.Next();
63 if (bookmark_model_->is_permanent_node(bookmark))
64 continue;
65 // RemoteIdFromBookmark() will create the ID if it doesn't exists yet.
66 std::string starid =
67 enhanced_bookmarks::RemoteIdFromBookmark(bookmark_model_, bookmark);
68 if (bookmark->is_url()) {
69 starsid_to_bookmark_[starid] = bookmark;
70 }
71 }
72 }
73
74 const BookmarkNode* BookmarkServerService::BookmarkForRemoteId(
75 const std::string& remote_id) const {
76 std::map<std::string, const BookmarkNode*>::const_iterator it =
77 starsid_to_bookmark_.find(remote_id);
78 if (it == starsid_to_bookmark_.end())
79 return NULL;
80 return it->second;
81 }
82
83 const std::string BookmarkServerService::RemoteIDForBookmark(
84 const BookmarkNode* bookmark) const {
85 return enhanced_bookmarks::RemoteIdFromBookmark(bookmark_model_, bookmark);
86 }
87
88 void BookmarkServerService::Notify() {
89 FOR_EACH_OBSERVER(BookmarkServerServiceObserver, observers_, OnChange(this));
90 }
91
92 void BookmarkServerService::TriggerTokenRequest(bool cancel_previous) {
93 if (cancel_previous)
94 url_fetcher_.reset();
95
96 if (token_request_ || url_fetcher_)
97 return; // Fetcher is already running.
98
99 const std::string username(signin_manager_->GetAuthenticatedUsername());
100 if (!username.length()) {
101 // User is not signed in.
102 CleanAfterFailure();
103 Notify();
104 return;
105 }
106 // Find a token.
107 OAuth2TokenService::ScopeSet scopes;
108 scopes.insert(kScope);
109 token_request_ = token_service_->StartRequest(username, scopes, this);
110 }
111
112 //
113 // OAuth2AccessTokenConsumer methods.
114 //
115 void BookmarkServerService::OnGetTokenSuccess(
116 const OAuth2TokenService::Request* request,
117 const std::string& access_token,
118 const base::Time& expiration_time) {
119 url_fetcher_.reset(CreateFetcherWithToken(access_token));
120 // Free the token request.
121 token_request_.reset();
122
123 if (!url_fetcher_) {
124 CleanAfterFailure();
125 Notify();
126 return;
127 }
128 url_fetcher_->SetRequestContext(request_context_getter_);
129
130 // Add the token.
131 std::string headers;
132 headers = "Authorization: Bearer ";
133 headers += access_token;
134 headers += "\r\n";
135 url_fetcher_->SetExtraRequestHeaders(headers);
136
137 // Do not pollute the cookie store with cruft, or mix the users cookie in this
138 // request.
139 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
140 net::LOAD_DO_NOT_SAVE_COOKIES);
141
142 url_fetcher_->Start();
143 }
144
145 void BookmarkServerService::OnGetTokenFailure(
146 const OAuth2TokenService::Request* request,
147 const GoogleServiceAuthError& error) {
148 // Free the request.
149 token_request_.reset();
150 CleanAfterFailure();
151 Notify();
152 }
153
154 //
155 // net::URLFetcherDelegate methods.
156 //
157 void BookmarkServerService::OnURLFetchComplete(const net::URLFetcher* source) {
158 scoped_ptr<net::URLFetcher> url_fetcher(url_fetcher_.Pass());
159 std::string response;
160 bool should_notify = true;
161
162 if (url_fetcher->GetResponseCode() != 200 ||
163 !url_fetcher->GetResponseAsString(&response) ||
164 !ProcessResponse(response, &should_notify)) {
165 CleanAfterFailure();
166 }
167 if (should_notify)
168 Notify();
169 }
170
171 //
172 // BookmarkModelObserver methods.
173 //
174 void BookmarkServerService::BookmarkModelLoaded(BookmarkModel* model,
175 bool ids_reassigned) {
176 BuildIdMap();
177 }
178
179 void BookmarkServerService::BookmarkNodeAdded(BookmarkModel* model,
180 const BookmarkNode* parent,
181 int index) {
182 DCHECK(!inhibit_change_notifications_);
183 const BookmarkNode* bookmark = parent->GetChild(index);
184 if (!bookmark->is_url())
185 return;
186
187 base::AutoReset<bool> inhibitor(&inhibit_change_notifications_, true);
188 std::string starid =
189 enhanced_bookmarks::RemoteIdFromBookmark(model, bookmark);
190 starsid_to_bookmark_[starid] = bookmark;
191 }
192
193 void BookmarkServerService::BookmarkNodeRemoved(
194 BookmarkModel* model,
195 const BookmarkNode* parent,
196 int old_index,
197 const BookmarkNode* node,
198 const std::set<GURL>& removed_urls) {
199 DCHECK(!inhibit_change_notifications_);
200 if (!node->is_url())
201 return;
202 base::AutoReset<bool> inhibitor(&inhibit_change_notifications_, true);
203 std::string starid = enhanced_bookmarks::RemoteIdFromBookmark(model, node);
204 starsid_to_bookmark_.erase(starid);
205 }
206
207 void BookmarkServerService::OnWillChangeBookmarkMetaInfo(
208 BookmarkModel* model,
209 const BookmarkNode* node) {
210 if (!node->is_url() || inhibit_change_notifications_)
211 return;
212 base::AutoReset<bool> inhibitor(&inhibit_change_notifications_, true);
213 std::string starid = enhanced_bookmarks::RemoteIdFromBookmark(model, node);
214 starsid_to_bookmark_.erase(starid);
215 }
216
217 void BookmarkServerService::BookmarkMetaInfoChanged(BookmarkModel* model,
218 const BookmarkNode* node) {
219 if (!node->is_url() || inhibit_change_notifications_)
220 return;
221
222 std::string starid = enhanced_bookmarks::RemoteIdFromBookmark(model, node);
223 starsid_to_bookmark_[starid] = node;
224 }
225
226 void BookmarkServerService::BookmarkAllUserNodesRemoved(
227 BookmarkModel* model,
228 const std::set<GURL>& removed_urls) {
229 DCHECK(!inhibit_change_notifications_);
230 starsid_to_bookmark_.clear();
231 }
232
233 SigninManagerBase* BookmarkServerService::GetSigninManager() {
234 DCHECK(signin_manager_);
235 return signin_manager_;
236 }
237
238 } // namespace enhanced_bookmarks
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698