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.
5 #include "components/enhanced_bookmarks/bookmark_server_service.h"
7 #include "base/auto_reset.h"
8 #include "components/enhanced_bookmarks/enhanced_bookmark_model.h"
9 #include "components/signin/core/browser/profile_oauth2_token_service.h"
10 #include "components/signin/core/browser/signin_manager_base.h"
11 #include "google_apis/gaia/gaia_constants.h"
12 #include "net/base/load_flags.h"
13 #include "net/url_request/url_request_context_getter.h"
14 #include "ui/base/models/tree_node_iterator.h"
16 using bookmarks::BookmarkNode
;
18 namespace enhanced_bookmarks
{
20 BookmarkServerService::BookmarkServerService(
21 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter
,
22 ProfileOAuth2TokenService
* token_service
,
23 SigninManagerBase
* signin_manager
,
24 EnhancedBookmarkModel
* enhanced_bookmark_model
)
25 : OAuth2TokenService::Consumer("bookmark_server_service"),
26 model_(enhanced_bookmark_model
),
27 token_service_(token_service
),
28 signin_manager_(signin_manager
),
29 request_context_getter_(request_context_getter
) {
30 DCHECK(request_context_getter
.get());
31 DCHECK(token_service
);
32 DCHECK(signin_manager
);
33 DCHECK(enhanced_bookmark_model
);
34 model_
->AddObserver(this);
37 BookmarkServerService::~BookmarkServerService() {
38 model_
->RemoveObserver(this);
41 void BookmarkServerService::AddObserver(
42 BookmarkServerServiceObserver
* observer
) {
43 observers_
.AddObserver(observer
);
46 void BookmarkServerService::RemoveObserver(
47 BookmarkServerServiceObserver
* observer
) {
48 observers_
.RemoveObserver(observer
);
51 const BookmarkNode
* BookmarkServerService::BookmarkForRemoteId(
52 const std::string
& remote_id
) const {
53 return model_
->BookmarkForRemoteId(remote_id
);
56 const std::string
BookmarkServerService::RemoteIDForBookmark(
57 const BookmarkNode
* bookmark
) const {
58 return model_
->GetRemoteId(bookmark
);
61 void BookmarkServerService::Cancel() {
63 token_request_
.reset();
66 void BookmarkServerService::Notify() {
67 FOR_EACH_OBSERVER(BookmarkServerServiceObserver
, observers_
, OnChange(this));
70 void BookmarkServerService::TriggerTokenRequest(bool cancel_previous
) {
74 if (token_request_
|| url_fetcher_
)
75 return; // Fetcher is already running.
77 if (!signin_manager_
->IsAuthenticated()) {
78 // User is not signed in.
84 OAuth2TokenService::ScopeSet scopes
;
85 scopes
.insert(GaiaConstants::kChromeSyncOAuth2Scope
);
86 token_request_
= token_service_
->StartRequest(
87 signin_manager_
->GetAuthenticatedAccountId(), scopes
, this);
91 // OAuth2AccessTokenConsumer methods.
93 void BookmarkServerService::OnGetTokenSuccess(
94 const OAuth2TokenService::Request
* request
,
95 const std::string
& access_token
,
96 const base::Time
& expiration_time
) {
97 url_fetcher_
= CreateFetcher();
99 // Free the token request.
100 token_request_
.reset();
107 url_fetcher_
->SetRequestContext(request_context_getter_
.get());
111 headers
= "Authorization: Bearer ";
112 headers
+= access_token
;
114 url_fetcher_
->SetExtraRequestHeaders(headers
);
116 // Do not pollute the cookie store with cruft, or mix the users cookie in this
118 url_fetcher_
->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES
|
119 net::LOAD_DO_NOT_SAVE_COOKIES
);
121 url_fetcher_
->Start();
124 void BookmarkServerService::OnGetTokenFailure(
125 const OAuth2TokenService::Request
* request
,
126 const GoogleServiceAuthError
& error
) {
128 token_request_
.reset();
134 // net::URLFetcherDelegate methods.
136 void BookmarkServerService::OnURLFetchComplete(const net::URLFetcher
* source
) {
137 scoped_ptr
<net::URLFetcher
> url_fetcher(url_fetcher_
.Pass());
138 std::string response
;
139 bool should_notify
= true;
141 if (url_fetcher
->GetResponseCode() != 200 ||
142 !url_fetcher
->GetResponseAsString(&response
) ||
143 !ProcessResponse(response
, &should_notify
)) {
150 void BookmarkServerService::EnhancedBookmarkModelShuttingDown() {
154 SigninManagerBase
* BookmarkServerService::GetSigninManager() {
155 DCHECK(signin_manager_
);
156 return signin_manager_
;
159 } // namespace enhanced_bookmarks