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

Side by Side Diff: chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher.cc

Issue 10836182: Obfuscated Gaia ID fetcher (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: More style fixes per Munjal and DCheng Created 8 years, 4 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 (c) 2012 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 "chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetche r.h"
6
7 #include <vector>
8
9 #include "base/json/json_reader.h"
10 #include "base/values.h"
11 #include "chrome/common/net/gaia/google_service_auth_error.h"
12 #include "net/url_request/url_fetcher.h"
13 #include "net/url_request/url_request_status.h"
14
15 using net::URLFetcher;
16 using net::URLRequestContextGetter;
17 using net::URLRequestStatus;
18
19 namespace {
20
21 // URL of the service to get obfuscated GAIA ID (here misnamed channel ID).
22 static const char kCWSChannelServiceURL[] =
23 "https://www.googleapis.com/chromewebstore/v1.1/channels/id";
24
25 static GoogleServiceAuthError CreateAuthError(URLRequestStatus status) {
26 if (status.status() == URLRequestStatus::CANCELED) {
27 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED);
28 } else {
29 // TODO(munjal): Improve error handling. Currently we return connection
30 // error for even application level errors. We need to either expand the
31 // GoogleServiceAuthError enum or create a new one to report better
32 // errors.
33 DLOG(WARNING) << "Server returned error: errno " << status.error();
34 return GoogleServiceAuthError::FromConnectionError(status.error());
35 }
36 }
37
38 // Returns a vector of scopes needed to call the API to get obfuscated gaia id.
39 std::vector<std::string> GetScopes() {
40 std::vector<std::string> scopes;
41 scopes.push_back(
42 "https://www.googleapis.com/auth/chromewebstore.notification");
43 return scopes;
44 }
45
46 } // namespace
47
48
dcheng 2012/08/20 22:52:22 Newline =)
Pete Williamson 2012/08/20 23:19:28 Done.
49 namespace extensions {
50
51 ObfuscatedGaiaIdFetcher::ObfuscatedGaiaIdFetcher(
52 URLRequestContextGetter* context,
53 Delegate* delegate,
54 const std::string& refresh_token)
55 : OAuth2ApiCallFlow(context, refresh_token, std::string(), GetScopes()),
56 delegate_(delegate) {
57 DCHECK(delegate);
58 }
59
60 ObfuscatedGaiaIdFetcher::~ObfuscatedGaiaIdFetcher() { }
61
62 void ObfuscatedGaiaIdFetcher::ReportSuccess(const std::string& obfuscated_id) {
63 if (delegate_)
64 delegate_->OnObfuscatedGaiaIdFetchSuccess(obfuscated_id);
65 }
66
67 void ObfuscatedGaiaIdFetcher::ReportFailure(
68 const GoogleServiceAuthError& error) {
69 if (delegate_)
70 delegate_->OnObfuscatedGaiaIdFetchFailure(error);
71 }
72
73 GURL ObfuscatedGaiaIdFetcher::CreateApiCallUrl() {
74 return GURL(kCWSChannelServiceURL);
75 }
76
77 std::string ObfuscatedGaiaIdFetcher::CreateApiCallBody() {
78 // Nothing to do here, we don't need a body for this request, the URL
79 // encodes all the proper arguments.
80 return std::string();
81 }
82
83 void ObfuscatedGaiaIdFetcher::ProcessApiCallSuccess(
84 const net::URLFetcher* source) {
85 // TODO(munjal): Change error code paths in this method to report an
86 // internal error.
87 std::string response_body;
88 CHECK(source->GetResponseAsString(&response_body));
89
90 std::string channel_id;
91 if (ParseResponse(response_body, &channel_id))
92 ReportSuccess(channel_id);
93 else
94 // we picked 101 arbitrarily to help us correlate the error with this code.
95 ReportFailure(GoogleServiceAuthError::FromConnectionError(101));
96 }
97
98 void ObfuscatedGaiaIdFetcher::ProcessApiCallFailure(
99 const net::URLFetcher* source) {
100 ReportFailure(CreateAuthError(source->GetStatus()));
101 }
102
103 void ObfuscatedGaiaIdFetcher::ProcessNewAccessToken(
104 const std::string& obfuscated_id) {
105 // We generate a new access token every time instead of storing the access
106 // token since access token expire every hour and we expect to get
107 // obfuscated gaia id very infrequently.
108 }
109
110 void ObfuscatedGaiaIdFetcher::ProcessMintAccessTokenFailure(
111 const GoogleServiceAuthError& error) {
112 // We failed to generate the token needed to call the API to get
113 // the obfuscated user ID, so report failure to the caller.
114 ReportFailure(error);
115 }
116
117 // static
118 bool ObfuscatedGaiaIdFetcher::ParseResponse(
119 const std::string& data, std::string* result) {
120 scoped_ptr<base::Value> value(base::JSONReader::Read(data));
121
122 if (!value.get())
123 return false;
124
125 DictionaryValue* dict = NULL;
126 if (!value->GetAsDictionary(&dict))
127 return false;
128
129 return dict->GetString("id", result);
130 }
131
132 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698