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 std::map
<std::string
, const BookmarkNode
*>::const_iterator it
=
52 starsid_to_bookmark_
.find(remote_id
);
53 if (it
== starsid_to_bookmark_
.end())
58 const std::string
BookmarkServerService::RemoteIDForBookmark(
59 const BookmarkNode
* bookmark
) const {
60 return model_
->GetRemoteId(bookmark
);
63 void BookmarkServerService::Notify() {
64 FOR_EACH_OBSERVER(BookmarkServerServiceObserver
, observers_
, OnChange(this));
67 void BookmarkServerService::TriggerTokenRequest(bool cancel_previous
) {
71 if (token_request_
|| url_fetcher_
)
72 return; // Fetcher is already running.
74 const std::string
username(signin_manager_
->GetAuthenticatedUsername());
75 if (!username
.length()) {
76 // User is not signed in.
82 OAuth2TokenService::ScopeSet scopes
;
83 scopes
.insert(GaiaConstants::kChromeSyncOAuth2Scope
);
84 token_request_
= token_service_
->StartRequest(username
, scopes
, this);
88 // OAuth2AccessTokenConsumer methods.
90 void BookmarkServerService::OnGetTokenSuccess(
91 const OAuth2TokenService::Request
* request
,
92 const std::string
& access_token
,
93 const base::Time
& expiration_time
) {
94 url_fetcher_
.reset(CreateFetcher());
95 // Free the token request.
96 token_request_
.reset();
103 url_fetcher_
->SetRequestContext(request_context_getter_
.get());
107 headers
= "Authorization: Bearer ";
108 headers
+= access_token
;
110 url_fetcher_
->SetExtraRequestHeaders(headers
);
112 // Do not pollute the cookie store with cruft, or mix the users cookie in this
114 url_fetcher_
->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES
|
115 net::LOAD_DO_NOT_SAVE_COOKIES
);
117 url_fetcher_
->Start();
120 void BookmarkServerService::OnGetTokenFailure(
121 const OAuth2TokenService::Request
* request
,
122 const GoogleServiceAuthError
& error
) {
124 token_request_
.reset();
130 // net::URLFetcherDelegate methods.
132 void BookmarkServerService::OnURLFetchComplete(const net::URLFetcher
* source
) {
133 scoped_ptr
<net::URLFetcher
> url_fetcher(url_fetcher_
.Pass());
134 std::string response
;
135 bool should_notify
= true;
137 if (url_fetcher
->GetResponseCode() != 200 ||
138 !url_fetcher
->GetResponseAsString(&response
) ||
139 !ProcessResponse(response
, &should_notify
)) {
146 void BookmarkServerService::EnhancedBookmarkModelShuttingDown() {
150 SigninManagerBase
* BookmarkServerService::GetSigninManager() {
151 DCHECK(signin_manager_
);
152 return signin_manager_
;
155 } // namespace enhanced_bookmarks