Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / webui / identity_internals_ui.cc
blob2a32f34018d41d6e7a4a7a32004afd7518093812
1 // Copyright 2013 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 "chrome/browser/ui/webui/identity_internals_ui.h"
7 #include <set>
8 #include <string>
10 #include "base/bind.h"
11 #include "base/i18n/time_formatting.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/extensions/api/identity/identity_api.h"
15 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/url_constants.h"
18 #include "content/public/browser/web_ui.h"
19 #include "content/public/browser/web_ui_controller.h"
20 #include "content/public/browser/web_ui_data_source.h"
21 #include "content/public/browser/web_ui_message_handler.h"
22 #include "google_apis/gaia/gaia_auth_fetcher.h"
23 #include "google_apis/gaia/gaia_constants.h"
24 #include "grit/browser_resources.h"
25 #include "grit/generated_resources.h"
26 #include "ui/base/l10n/l10n_util.h"
28 namespace {
30 // Properties of the Javascript object representing a token.
31 const char kExtensionId[] = "extensionId";
32 const char kExtensionName[] = "extensionName";
33 const char kScopes[] = "scopes";
34 const char kStatus[] = "status";
35 const char kTokenExpirationTime[] = "expirationTime";
36 const char kAccessToken[] = "accessToken";
38 // RevokeToken message parameter offsets.
39 const int kRevokeTokenExtensionOffset = 0;
40 const int kRevokeTokenTokenOffset = 1;
42 class IdentityInternalsTokenRevoker;
44 // Class acting as a controller of the chrome://identity-internals WebUI.
45 class IdentityInternalsUIMessageHandler : public content::WebUIMessageHandler {
46 public:
47 IdentityInternalsUIMessageHandler();
48 virtual ~IdentityInternalsUIMessageHandler();
50 // Ensures that a proper clean up happens after a token is revoked. That
51 // includes deleting the |token_revoker|, removing the token from Identity API
52 // cache and updating the UI that the token is gone.
53 void OnTokenRevokerDone(IdentityInternalsTokenRevoker* token_revoker);
55 // WebUIMessageHandler implementation.
56 virtual void RegisterMessages() OVERRIDE;
58 private:
59 // Gets the name of an extension referred to by |token_cache_key| as a string.
60 const std::string GetExtensionName(
61 const extensions::ExtensionTokenKey& token_cache_key);
63 // Gets a list of scopes specified in |token_cache_key| and returns a pointer
64 // to a ListValue containing the scopes. The caller gets ownership of the
65 // returned object.
66 base::ListValue* GetScopes(
67 const extensions::ExtensionTokenKey& token_cache_key);
69 // Gets a localized status of the access token in |token_cache_value|.
70 const base::string16 GetStatus(
71 const extensions::IdentityTokenCacheValue& token_cache_value);
73 // Gets a string representation of an expiration time of the access token in
74 // |token_cache_value|.
75 const std::string GetExpirationTime(
76 const extensions::IdentityTokenCacheValue& token_cache_value);
78 // Converts a pair of |token_cache_key| and |token_cache_value| to a
79 // DictionaryValue object with corresponding information in a localized and
80 // readable form and returns a pointer to created object. Caller gets the
81 // ownership of the returned object.
82 base::DictionaryValue* GetInfoForToken(
83 const extensions::ExtensionTokenKey& token_cache_key,
84 const extensions::IdentityTokenCacheValue& token_cache_value);
86 // Gets all of the tokens stored in IdentityAPI token cache and returns them
87 // to the caller using Javascript callback function
88 // |identity_internals.returnTokens()|.
89 void GetInfoForAllTokens(const base::ListValue* args);
91 // Initiates revoking of the token, based on the extension ID and token
92 // passed as entries in the |args| list. Updates the caller of completion
93 // using Javascript callback function |identity_internals.tokenRevokeDone()|.
94 void RevokeToken(const base::ListValue* args);
96 // A vector of token revokers that are currently revoking tokens.
97 ScopedVector<IdentityInternalsTokenRevoker> token_revokers_;
100 // Handles the revoking of an access token and helps performing the clean up
101 // after it is revoked by holding information about the access token and related
102 // extension ID.
103 class IdentityInternalsTokenRevoker : public GaiaAuthConsumer {
104 public:
105 // Revokes |access_token| from extension with |extension_id|.
106 // |profile| is required for its request context. |consumer| will be
107 // notified when revocation succeeds via |OnTokenRevokerDone()|.
108 IdentityInternalsTokenRevoker(const std::string& extension_id,
109 const std::string& access_token,
110 Profile* profile,
111 IdentityInternalsUIMessageHandler* consumer);
112 virtual ~IdentityInternalsTokenRevoker();
114 // Returns the access token being revoked.
115 const std::string& access_token() const { return access_token_; }
117 // Returns the ID of the extension the access token is related to.
118 const std::string& extension_id() const { return extension_id_; }
120 // GaiaAuthConsumer implementation.
121 virtual void OnOAuth2RevokeTokenCompleted() OVERRIDE;
123 private:
124 // An object used to start a token revoke request.
125 GaiaAuthFetcher fetcher_;
126 // An ID of an extension the access token is related to.
127 const std::string extension_id_;
128 // The access token to revoke.
129 const std::string access_token_;
130 // An object that needs to be notified once the access token is revoked.
131 IdentityInternalsUIMessageHandler* consumer_; // weak.
133 DISALLOW_COPY_AND_ASSIGN(IdentityInternalsTokenRevoker);
136 IdentityInternalsUIMessageHandler::IdentityInternalsUIMessageHandler() {}
138 IdentityInternalsUIMessageHandler::~IdentityInternalsUIMessageHandler() {}
140 void IdentityInternalsUIMessageHandler::OnTokenRevokerDone(
141 IdentityInternalsTokenRevoker* token_revoker) {
142 // Remove token from the cache.
143 extensions::IdentityAPI::GetFactoryInstance()->GetForProfile(
144 Profile::FromWebUI(web_ui()))->EraseCachedToken(
145 token_revoker->extension_id(), token_revoker->access_token());
147 // Update view about the token being removed.
148 base::ListValue result;
149 result.AppendString(token_revoker->access_token());
150 web_ui()->CallJavascriptFunction("identity_internals.tokenRevokeDone",
151 result);
153 // Erase the revoker.
154 ScopedVector<IdentityInternalsTokenRevoker>::iterator iter =
155 std::find(token_revokers_.begin(), token_revokers_.end(), token_revoker);
156 DCHECK(iter != token_revokers_.end());
157 token_revokers_.erase(iter);
160 const std::string IdentityInternalsUIMessageHandler::GetExtensionName(
161 const extensions::ExtensionTokenKey& token_cache_key) {
162 ExtensionService* extension_service = extensions::ExtensionSystem::Get(
163 Profile::FromWebUI(web_ui()))->extension_service();
164 const extensions::Extension* extension =
165 extension_service->extensions()->GetByID(token_cache_key.extension_id);
166 if (!extension)
167 return std::string();
168 return extension->name();
171 base::ListValue* IdentityInternalsUIMessageHandler::GetScopes(
172 const extensions::ExtensionTokenKey& token_cache_key) {
173 base::ListValue* scopes_value = new base::ListValue();
174 for (std::set<std::string>::const_iterator
175 iter = token_cache_key.scopes.begin();
176 iter != token_cache_key.scopes.end(); ++iter) {
177 scopes_value->AppendString(*iter);
179 return scopes_value;
182 const base::string16 IdentityInternalsUIMessageHandler::GetStatus(
183 const extensions::IdentityTokenCacheValue& token_cache_value) {
184 switch (token_cache_value.status()) {
185 case extensions::IdentityTokenCacheValue::CACHE_STATUS_ADVICE:
186 // Fallthrough to NOT FOUND case, as ADVICE is short lived.
187 case extensions::IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND:
188 return l10n_util::GetStringUTF16(
189 IDS_IDENTITY_INTERNALS_TOKEN_NOT_FOUND);
190 case extensions::IdentityTokenCacheValue::CACHE_STATUS_TOKEN:
191 return l10n_util::GetStringUTF16(
192 IDS_IDENTITY_INTERNALS_TOKEN_PRESENT);
194 NOTREACHED();
195 return base::string16();
198 const std::string IdentityInternalsUIMessageHandler::GetExpirationTime(
199 const extensions::IdentityTokenCacheValue& token_cache_value) {
200 return base::UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime(
201 token_cache_value.expiration_time()));
204 base::DictionaryValue* IdentityInternalsUIMessageHandler::GetInfoForToken(
205 const extensions::ExtensionTokenKey& token_cache_key,
206 const extensions::IdentityTokenCacheValue& token_cache_value) {
207 base::DictionaryValue* token_data = new base::DictionaryValue();
208 token_data->SetString(kExtensionId, token_cache_key.extension_id);
209 token_data->SetString(kExtensionName, GetExtensionName(token_cache_key));
210 token_data->Set(kScopes, GetScopes(token_cache_key));
211 token_data->SetString(kStatus, GetStatus(token_cache_value));
212 token_data->SetString(kAccessToken, token_cache_value.token());
213 token_data->SetString(kTokenExpirationTime,
214 GetExpirationTime(token_cache_value));
215 return token_data;
218 void IdentityInternalsUIMessageHandler::GetInfoForAllTokens(
219 const base::ListValue* args) {
220 base::ListValue results;
221 extensions::IdentityAPI::CachedTokens tokens =
222 extensions::IdentityAPI::GetFactoryInstance()->GetForProfile(
223 Profile::FromWebUI(web_ui()))->GetAllCachedTokens();
224 for (extensions::IdentityAPI::CachedTokens::const_iterator
225 iter = tokens.begin(); iter != tokens.end(); ++iter) {
226 results.Append(GetInfoForToken(iter->first, iter->second));
229 web_ui()->CallJavascriptFunction("identity_internals.returnTokens", results);
232 void IdentityInternalsUIMessageHandler::RegisterMessages() {
233 web_ui()->RegisterMessageCallback("identityInternalsGetTokens",
234 base::Bind(&IdentityInternalsUIMessageHandler::GetInfoForAllTokens,
235 base::Unretained(this)));
236 web_ui()->RegisterMessageCallback("identityInternalsRevokeToken",
237 base::Bind(&IdentityInternalsUIMessageHandler::RevokeToken,
238 base::Unretained(this)));
241 void IdentityInternalsUIMessageHandler::RevokeToken(
242 const base::ListValue* args) {
243 std::string extension_id;
244 std::string access_token;
245 args->GetString(kRevokeTokenExtensionOffset, &extension_id);
246 args->GetString(kRevokeTokenTokenOffset, &access_token);
247 token_revokers_.push_back(new IdentityInternalsTokenRevoker(
248 extension_id, access_token, Profile::FromWebUI(web_ui()), this));
251 IdentityInternalsTokenRevoker::IdentityInternalsTokenRevoker(
252 const std::string& extension_id,
253 const std::string& access_token,
254 Profile* profile,
255 IdentityInternalsUIMessageHandler* consumer)
256 : fetcher_(this, GaiaConstants::kChromeSource,
257 profile->GetRequestContext()),
258 extension_id_(extension_id),
259 access_token_(access_token),
260 consumer_(consumer) {
261 DCHECK(consumer_);
262 fetcher_.StartRevokeOAuth2Token(access_token);
265 IdentityInternalsTokenRevoker::~IdentityInternalsTokenRevoker() {}
267 void IdentityInternalsTokenRevoker::OnOAuth2RevokeTokenCompleted() {
268 consumer_->OnTokenRevokerDone(this);
271 } // namespace
273 IdentityInternalsUI::IdentityInternalsUI(content::WebUI* web_ui)
274 : content::WebUIController(web_ui) {
275 // chrome://identity-internals source.
276 content::WebUIDataSource* html_source =
277 content::WebUIDataSource::Create(chrome::kChromeUIIdentityInternalsHost);
278 html_source->SetUseJsonJSFormatV2();
280 // Localized strings
281 html_source->AddLocalizedString("tokenCacheHeader",
282 IDS_IDENTITY_INTERNALS_TOKEN_CACHE_TEXT);
283 html_source->AddLocalizedString("accessToken",
284 IDS_IDENTITY_INTERNALS_ACCESS_TOKEN);
285 html_source->AddLocalizedString("extensionName",
286 IDS_IDENTITY_INTERNALS_EXTENSION_NAME);
287 html_source->AddLocalizedString("extensionId",
288 IDS_IDENTITY_INTERNALS_EXTENSION_ID);
289 html_source->AddLocalizedString("tokenStatus",
290 IDS_IDENTITY_INTERNALS_TOKEN_STATUS);
291 html_source->AddLocalizedString("expirationTime",
292 IDS_IDENTITY_INTERNALS_EXPIRATION_TIME);
293 html_source->AddLocalizedString("scopes",
294 IDS_IDENTITY_INTERNALS_SCOPES);
295 html_source->AddLocalizedString("revoke",
296 IDS_IDENTITY_INTERNALS_REVOKE);
297 html_source->SetJsonPath("strings.js");
299 // Required resources
300 html_source->AddResourcePath("identity_internals.css",
301 IDR_IDENTITY_INTERNALS_CSS);
302 html_source->AddResourcePath("identity_internals.js",
303 IDR_IDENTITY_INTERNALS_JS);
304 html_source->SetDefaultResource(IDR_IDENTITY_INTERNALS_HTML);
306 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source);
308 web_ui->AddMessageHandler(new IdentityInternalsUIMessageHandler());
311 IdentityInternalsUI::~IdentityInternalsUI() {}