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 namespace enhanced_bookmarks
{
18 BookmarkServerService::BookmarkServerService(
19 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter
,
20 ProfileOAuth2TokenService
* token_service
,
21 SigninManagerBase
* signin_manager
,
22 EnhancedBookmarkModel
* enhanced_bookmark_model
)
23 : OAuth2TokenService::Consumer("bookmark_server_service"),
24 model_(enhanced_bookmark_model
),
25 token_service_(token_service
),
26 signin_manager_(signin_manager
),
27 request_context_getter_(request_context_getter
) {
28 DCHECK(request_context_getter
.get());
29 DCHECK(token_service
);
30 DCHECK(signin_manager
);
31 DCHECK(enhanced_bookmark_model
);
32 model_
->AddObserver(this);
35 BookmarkServerService::~BookmarkServerService() {
36 model_
->RemoveObserver(this);
39 void BookmarkServerService::AddObserver(
40 BookmarkServerServiceObserver
* observer
) {
41 observers_
.AddObserver(observer
);
44 void BookmarkServerService::RemoveObserver(
45 BookmarkServerServiceObserver
* observer
) {
46 observers_
.RemoveObserver(observer
);
49 const BookmarkNode
* BookmarkServerService::BookmarkForRemoteId(
50 const std::string
& remote_id
) const {
51 return model_
->BookmarkForRemoteId(remote_id
);
54 const std::string
BookmarkServerService::RemoteIDForBookmark(
55 const BookmarkNode
* bookmark
) const {
56 return model_
->GetRemoteId(bookmark
);
59 void BookmarkServerService::Cancel() {
61 token_request_
.reset();
64 void BookmarkServerService::Notify() {
65 FOR_EACH_OBSERVER(BookmarkServerServiceObserver
, observers_
, OnChange(this));
68 void BookmarkServerService::TriggerTokenRequest(bool cancel_previous
) {
72 if (token_request_
|| url_fetcher_
)
73 return; // Fetcher is already running.
75 if (!signin_manager_
->IsAuthenticated()) {
76 // User is not signed in.
82 OAuth2TokenService::ScopeSet scopes
;
83 scopes
.insert(GaiaConstants::kChromeSyncOAuth2Scope
);
84 token_request_
= token_service_
->StartRequest(
85 signin_manager_
->GetAuthenticatedAccountId(), scopes
, this);
89 // OAuth2AccessTokenConsumer methods.
91 void BookmarkServerService::OnGetTokenSuccess(
92 const OAuth2TokenService::Request
* request
,
93 const std::string
& access_token
,
94 const base::Time
& expiration_time
) {
95 url_fetcher_
= CreateFetcher();
97 // Free the token request.
98 token_request_
.reset();
105 url_fetcher_
->SetRequestContext(request_context_getter_
.get());
109 headers
= "Authorization: Bearer ";
110 headers
+= access_token
;
112 url_fetcher_
->SetExtraRequestHeaders(headers
);
114 // Do not pollute the cookie store with cruft, or mix the users cookie in this
116 url_fetcher_
->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES
|
117 net::LOAD_DO_NOT_SAVE_COOKIES
);
119 url_fetcher_
->Start();
122 void BookmarkServerService::OnGetTokenFailure(
123 const OAuth2TokenService::Request
* request
,
124 const GoogleServiceAuthError
& error
) {
126 token_request_
.reset();
132 // net::URLFetcherDelegate methods.
134 void BookmarkServerService::OnURLFetchComplete(const net::URLFetcher
* source
) {
135 scoped_ptr
<net::URLFetcher
> url_fetcher(url_fetcher_
.Pass());
136 std::string response
;
137 bool should_notify
= true;
139 if (url_fetcher
->GetResponseCode() != 200 ||
140 !url_fetcher
->GetResponseAsString(&response
) ||
141 !ProcessResponse(response
, &should_notify
)) {
148 void BookmarkServerService::EnhancedBookmarkModelShuttingDown() {
152 SigninManagerBase
* BookmarkServerService::GetSigninManager() {
153 DCHECK(signin_manager_
);
154 return signin_manager_
;
157 } // namespace enhanced_bookmarks