OLD | NEW |
| (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 "google_apis/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/gcm_for_chrome/v1/channels/id"; | |
24 | |
25 GoogleServiceAuthError CreateAuthError(const URLFetcher* source) { | |
26 if (source->GetStatus().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 if (source->GetStatus().is_success()) { | |
34 DLOG(WARNING) << "Remote server returned " << source->GetResponseCode(); | |
35 return GoogleServiceAuthError::FromConnectionError( | |
36 source->GetResponseCode()); | |
37 } else { | |
38 DLOG(WARNING) << "URLFetcher failed: " << source->GetStatus().error(); | |
39 return GoogleServiceAuthError::FromConnectionError( | |
40 source->GetStatus().error()); | |
41 } | |
42 } | |
43 } | |
44 | |
45 } // namespace | |
46 | |
47 namespace extensions { | |
48 | |
49 ObfuscatedGaiaIdFetcher::ObfuscatedGaiaIdFetcher(Delegate* delegate) | |
50 : delegate_(delegate) { | |
51 DCHECK(delegate); | |
52 } | |
53 | |
54 ObfuscatedGaiaIdFetcher::~ObfuscatedGaiaIdFetcher() { } | |
55 | |
56 // Returns a set of scopes needed to call the API to get obfuscated Gaia ID. | |
57 // static. | |
58 OAuth2TokenService::ScopeSet ObfuscatedGaiaIdFetcher::GetScopes() { | |
59 OAuth2TokenService::ScopeSet scopes; | |
60 scopes.insert("https://www.googleapis.com/auth/gcm_for_chrome.readonly"); | |
61 return scopes; | |
62 } | |
63 | |
64 void ObfuscatedGaiaIdFetcher::ReportSuccess(const std::string& obfuscated_id) { | |
65 if (delegate_) | |
66 delegate_->OnObfuscatedGaiaIdFetchSuccess(obfuscated_id); | |
67 } | |
68 | |
69 void ObfuscatedGaiaIdFetcher::ReportFailure( | |
70 const GoogleServiceAuthError& error) { | |
71 if (delegate_) | |
72 delegate_->OnObfuscatedGaiaIdFetchFailure(error); | |
73 } | |
74 | |
75 GURL ObfuscatedGaiaIdFetcher::CreateApiCallUrl() { | |
76 return GURL(kCWSChannelServiceURL); | |
77 } | |
78 | |
79 std::string ObfuscatedGaiaIdFetcher::CreateApiCallBody() { | |
80 // Nothing to do here, we don't need a body for this request, the URL | |
81 // encodes all the proper arguments. | |
82 return std::string(); | |
83 } | |
84 | |
85 void ObfuscatedGaiaIdFetcher::ProcessApiCallSuccess( | |
86 const net::URLFetcher* source) { | |
87 // TODO(munjal): Change error code paths in this method to report an | |
88 // internal error. | |
89 std::string response_body; | |
90 CHECK(source->GetResponseAsString(&response_body)); | |
91 | |
92 std::string obfuscated_id; | |
93 if (ParseResponse(response_body, &obfuscated_id)) | |
94 ReportSuccess(obfuscated_id); | |
95 else | |
96 // we picked 101 arbitrarily to help us correlate the error with this code. | |
97 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); | |
98 } | |
99 | |
100 void ObfuscatedGaiaIdFetcher::ProcessApiCallFailure( | |
101 const net::URLFetcher* source) { | |
102 ReportFailure(CreateAuthError(source)); | |
103 } | |
104 | |
105 // static | |
106 bool ObfuscatedGaiaIdFetcher::ParseResponse( | |
107 const std::string& data, std::string* result) { | |
108 scoped_ptr<base::Value> value(base::JSONReader::Read(data)); | |
109 | |
110 if (!value.get()) | |
111 return false; | |
112 | |
113 base::DictionaryValue* dict = NULL; | |
114 if (!value->GetAsDictionary(&dict)) | |
115 return false; | |
116 | |
117 return dict->GetString("id", result); | |
118 } | |
119 | |
120 } // namespace extensions | |
OLD | NEW |