Make |track_| in MediaStreamTrack const. and a couple of other cosmetic changes.
[chromium-blink-merge.git] / components / enhanced_bookmarks / bookmark_server_service.cc
blob1e6f4b0f102b0da8db7cd5c8de378678a2b0dcf3
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::Notify() {
60 FOR_EACH_OBSERVER(BookmarkServerServiceObserver, observers_, OnChange(this));
63 void BookmarkServerService::TriggerTokenRequest(bool cancel_previous) {
64 if (cancel_previous)
65 url_fetcher_.reset();
67 if (token_request_ || url_fetcher_)
68 return; // Fetcher is already running.
70 if (!signin_manager_->IsAuthenticated()) {
71 // User is not signed in.
72 CleanAfterFailure();
73 Notify();
74 return;
76 // Find a token.
77 OAuth2TokenService::ScopeSet scopes;
78 scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope);
79 token_request_ = token_service_->StartRequest(
80 signin_manager_->GetAuthenticatedAccountId(), scopes, this);
84 // OAuth2AccessTokenConsumer methods.
86 void BookmarkServerService::OnGetTokenSuccess(
87 const OAuth2TokenService::Request* request,
88 const std::string& access_token,
89 const base::Time& expiration_time) {
90 url_fetcher_ = CreateFetcher();
92 // Free the token request.
93 token_request_.reset();
95 if (!url_fetcher_) {
96 CleanAfterFailure();
97 Notify();
98 return;
100 url_fetcher_->SetRequestContext(request_context_getter_.get());
102 // Add the token.
103 std::string headers;
104 headers = "Authorization: Bearer ";
105 headers += access_token;
106 headers += "\r\n";
107 url_fetcher_->SetExtraRequestHeaders(headers);
109 // Do not pollute the cookie store with cruft, or mix the users cookie in this
110 // request.
111 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
112 net::LOAD_DO_NOT_SAVE_COOKIES);
114 url_fetcher_->Start();
117 void BookmarkServerService::OnGetTokenFailure(
118 const OAuth2TokenService::Request* request,
119 const GoogleServiceAuthError& error) {
120 // Free the request.
121 token_request_.reset();
122 CleanAfterFailure();
123 Notify();
127 // net::URLFetcherDelegate methods.
129 void BookmarkServerService::OnURLFetchComplete(const net::URLFetcher* source) {
130 scoped_ptr<net::URLFetcher> url_fetcher(url_fetcher_.Pass());
131 std::string response;
132 bool should_notify = true;
134 if (url_fetcher->GetResponseCode() != 200 ||
135 !url_fetcher->GetResponseAsString(&response) ||
136 !ProcessResponse(response, &should_notify)) {
137 CleanAfterFailure();
139 if (should_notify)
140 Notify();
143 void BookmarkServerService::EnhancedBookmarkModelShuttingDown() {
144 NOTREACHED();
147 SigninManagerBase* BookmarkServerService::GetSigninManager() {
148 DCHECK(signin_manager_);
149 return signin_manager_;
152 } // namespace enhanced_bookmarks