Chromium Code Reviews| 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 <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/json/json_reader.h" | |
| 11 #include "base/values.h" | |
| 12 #include "chrome/common/net/gaia/google_service_auth_error.h" | |
| 13 #include "net/url_request/url_fetcher.h" | |
| 14 #include "net/url_request/url_request_status.h" | |
| 15 | |
| 16 using net::URLFetcher; | |
| 17 using net::URLRequestContextGetter; | |
| 18 using net::URLRequestStatus; | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 const char kID[] = "id"; | |
|
Munjal (Google)
2012/08/15 23:04:19
static.
dcheng
2012/08/16 19:12:43
This name is pretty undescriptive for a top-level
Pete Williamson
2012/08/19 22:05:14
Done.
| |
| 23 | |
| 24 } | |
| 25 | |
| 26 namespace { | |
|
Munjal (Google)
2012/08/15 23:04:19
Combine this anonymous namespace with the previous
Pete Williamson
2012/08/19 22:05:14
Done.
| |
| 27 | |
| 28 // URL of the service to get obfuscated GAIA ID (here misnamed channel ID). | |
| 29 static const char kCWSChannelServiceURL[] = | |
| 30 "https://www.googleapis.com/chromewebstore/v1.1/channels/id"; | |
| 31 | |
| 32 static GoogleServiceAuthError CreateAuthError(URLRequestStatus status) { | |
| 33 if (status.status() == URLRequestStatus::CANCELED) { | |
| 34 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); | |
| 35 } else { | |
| 36 // TODO(munjal): Improve error handling. Currently we return connection | |
| 37 // error for even application level errors. We need to either expand the | |
| 38 // GoogleServiceAuthError enum or create a new one to report better | |
| 39 // errors. | |
| 40 DLOG(WARNING) << "Server returned error: errno " << status.error(); | |
| 41 return GoogleServiceAuthError::FromConnectionError(status.error()); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 | |
| 48 namespace extensions { | |
| 49 | |
| 50 ObfuscatedGaiaIdFetcher::ObfuscatedGaiaIdFetcher( | |
| 51 URLRequestContextGetter* context, | |
| 52 PushMessagingGetChannelIdFunction* delegate, | |
| 53 const std::string& refresh_token) | |
| 54 : OAuth2ApiCallFlow(context, refresh_token, "", Scopes()), | |
|
dcheng
2012/08/16 19:12:43
std::string() instead of "".
dcheng
2012/08/16 19:12:43
I'm not a fan of Scopes(), but I guess there's no
Pete Williamson
2012/08/19 22:05:14
Done.
Pete Williamson
2012/08/19 22:05:14
Munjal suggested that it be a member function beca
dcheng
2012/08/19 22:38:35
It is an implementation detail. In fact, it's so m
Pete Williamson
2012/08/20 18:38:42
Done.
| |
| 55 delegate_(delegate) { | |
| 56 } | |
| 57 | |
| 58 ObfuscatedGaiaIdFetcher::~ObfuscatedGaiaIdFetcher() { } | |
| 59 | |
| 60 // We use this static function to encapsulate the scopes needed at ctor time. | |
| 61 std::vector<std::string> ObfuscatedGaiaIdFetcher::Scopes() { | |
| 62 std::vector<std::string> scopes; | |
| 63 if (scopes.size() == 0) { | |
|
Munjal (Google)
2012/08/15 23:04:19
Why would the size of the vector not be 0? It is a
dcheng
2012/08/16 19:12:43
Prefer empty() over size() == 0
Pete Williamson
2012/08/19 22:05:14
It is always 0, this is left over from when it was
Pete Williamson
2012/08/19 22:05:14
check removed instead.
| |
| 64 scopes.push_back( | |
| 65 "https://www.googleapis.com/auth/chromewebstore.notification"); | |
| 66 } | |
| 67 return scopes; | |
| 68 } | |
| 69 | |
| 70 void ObfuscatedGaiaIdFetcher::ReportSuccess(const std::string& obfuscated_id) { | |
| 71 if (delegate_) | |
|
dcheng
2012/08/16 19:12:43
Why do we allow null delegates?
Pete Williamson
2012/08/19 22:05:14
It is because we are using the delegate to hold it
| |
| 72 delegate_->OnObfuscatedGaiaIdFetchSuccess(obfuscated_id); | |
| 73 } | |
| 74 | |
| 75 void ObfuscatedGaiaIdFetcher::ReportFailure( | |
| 76 const GoogleServiceAuthError& error) { | |
| 77 if (delegate_) | |
| 78 delegate_->OnObfuscatedGaiaIdFetchFailure(error); | |
| 79 } | |
| 80 | |
| 81 GURL ObfuscatedGaiaIdFetcher::CreateApiCallUrl() { | |
| 82 return GURL(kCWSChannelServiceURL); | |
| 83 } | |
| 84 | |
| 85 std::string ObfuscatedGaiaIdFetcher::CreateApiCallBody() { | |
| 86 // Nothing to do here, we don't need a body for this request, the URL | |
| 87 // encodes all the proper arguments. | |
| 88 return std::string(); | |
| 89 } | |
| 90 | |
| 91 void ObfuscatedGaiaIdFetcher::ProcessApiCallSuccess( | |
| 92 const net::URLFetcher* source) { | |
| 93 // TODO(munjal): Change error code paths in this method to report an | |
| 94 // internal error. | |
| 95 std::string response_body; | |
| 96 source->GetResponseAsString(&response_body); | |
|
dcheng
2012/08/16 19:12:43
CHECK(source->GetResponseAsString(&response_body))
Pete Williamson
2012/08/19 22:05:14
Done.
| |
| 97 | |
| 98 std::string channel_id; | |
| 99 if (ParseResponse(response_body, &channel_id)) | |
| 100 ReportSuccess(channel_id); | |
| 101 else | |
| 102 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); | |
| 103 | |
| 104 // Clean up ourselves, and stop holding the Function object refcount too. | |
| 105 delete this; | |
|
Munjal (Google)
2012/08/15 23:04:19
We should definitely avoid this. Let us just defin
dcheng
2012/08/16 19:12:43
The problem is this:
The extension function (logic
Munjal (Google)
2012/08/16 21:45:12
The ExtensionFunction should AddRef itself instead
Pete Williamson
2012/08/19 22:05:14
Daniel explicitly requested that I not call AddRef
Pete Williamson
2012/08/20 18:38:42
After some clarification from Daniel, I settled on
| |
| 106 } | |
| 107 | |
| 108 void ObfuscatedGaiaIdFetcher::ProcessApiCallFailure( | |
| 109 const net::URLFetcher* source) { | |
| 110 ReportFailure(CreateAuthError(source->GetStatus())); | |
| 111 | |
| 112 // Clean up ourselves, and stop holding the Function object refcount too. | |
| 113 delete this; | |
| 114 } | |
| 115 | |
| 116 void ObfuscatedGaiaIdFetcher::ProcessNewAccessToken( | |
| 117 const std::string& obfuscated_id) { | |
| 118 // We generate a new access token every time instead of storing the access | |
| 119 // token since access token expire every hour and we expect to get | |
| 120 // obfuscated gaia id very infrequently. | |
| 121 } | |
| 122 | |
| 123 void ObfuscatedGaiaIdFetcher::ProcessMintAccessTokenFailure( | |
| 124 const GoogleServiceAuthError& error) { | |
| 125 // We failed to generate the token needed to call the API to get | |
| 126 // the obfuscated user ID, so report failure to the caller. | |
| 127 ReportFailure(error); | |
| 128 } | |
| 129 | |
| 130 // static | |
| 131 bool ObfuscatedGaiaIdFetcher::ParseResponse( | |
| 132 const std::string& data, std::string* result) { | |
| 133 | |
|
Munjal (Google)
2012/08/15 23:04:19
Nit: remove empty line.
Pete Williamson
2012/08/19 22:05:14
Done.
| |
| 134 scoped_ptr<base::Value> value(base::JSONReader::Read(data)); | |
| 135 if (!value.get() || value->GetType() != base::Value::TYPE_DICTIONARY) | |
| 136 return false; | |
| 137 | |
| 138 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get()); | |
|
dcheng
2012/08/16 19:12:43
Instead of value->GetType() != base::Value::TYPE_D
Pete Williamson
2012/08/19 22:05:14
Done.
| |
| 139 return dict->GetString(kID, result); | |
| 140 } | |
| 141 | |
| 142 } // namespace extensions | |
|
Munjal (Google)
2012/08/15 23:04:19
Nit: two spaces after }
Pete Williamson
2012/08/19 22:05:14
Done.
| |
| OLD | NEW |