Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / identity_internals_ui.cc
blobbecbe8f4826e94224fffbecd3446fcfc2f1aec05
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/profiles/profile.h"
16 #include "chrome/common/url_constants.h"
17 #include "chrome/grit/generated_resources.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 "extensions/browser/extension_registry.h"
23 #include "google_apis/gaia/gaia_auth_fetcher.h"
24 #include "google_apis/gaia/gaia_constants.h"
25 #include "grit/browser_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 ~IdentityInternalsUIMessageHandler() override;
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 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 ~IdentityInternalsTokenRevoker() override;
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 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()
144 ->Get(Profile::FromWebUI(web_ui()))
145 ->EraseCachedToken(token_revoker->extension_id(),
146 token_revoker->access_token());
148 // Update view about the token being removed.
149 base::ListValue result;
150 result.AppendString(token_revoker->access_token());
151 web_ui()->CallJavascriptFunction("identity_internals.tokenRevokeDone",
152 result);
154 // Erase the revoker.
155 ScopedVector<IdentityInternalsTokenRevoker>::iterator iter =
156 std::find(token_revokers_.begin(), token_revokers_.end(), token_revoker);
157 DCHECK(iter != token_revokers_.end());
158 token_revokers_.erase(iter);
161 const std::string IdentityInternalsUIMessageHandler::GetExtensionName(
162 const extensions::ExtensionTokenKey& token_cache_key) {
163 const extensions::ExtensionRegistry* registry =
164 extensions::ExtensionRegistry::Get(Profile::FromWebUI(web_ui()));
165 const extensions::Extension* extension =
166 registry->enabled_extensions().GetByID(token_cache_key.extension_id);
167 if (!extension)
168 return std::string();
169 return extension->name();
172 base::ListValue* IdentityInternalsUIMessageHandler::GetScopes(
173 const extensions::ExtensionTokenKey& token_cache_key) {
174 base::ListValue* scopes_value = new base::ListValue();
175 for (std::set<std::string>::const_iterator
176 iter = token_cache_key.scopes.begin();
177 iter != token_cache_key.scopes.end(); ++iter) {
178 scopes_value->AppendString(*iter);
180 return scopes_value;
183 const base::string16 IdentityInternalsUIMessageHandler::GetStatus(
184 const extensions::IdentityTokenCacheValue& token_cache_value) {
185 switch (token_cache_value.status()) {
186 case extensions::IdentityTokenCacheValue::CACHE_STATUS_ADVICE:
187 // Fallthrough to NOT FOUND case, as ADVICE is short lived.
188 case extensions::IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND:
189 return l10n_util::GetStringUTF16(
190 IDS_IDENTITY_INTERNALS_TOKEN_NOT_FOUND);
191 case extensions::IdentityTokenCacheValue::CACHE_STATUS_TOKEN:
192 return l10n_util::GetStringUTF16(
193 IDS_IDENTITY_INTERNALS_TOKEN_PRESENT);
195 NOTREACHED();
196 return base::string16();
199 const std::string IdentityInternalsUIMessageHandler::GetExpirationTime(
200 const extensions::IdentityTokenCacheValue& token_cache_value) {
201 return base::UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime(
202 token_cache_value.expiration_time()));
205 base::DictionaryValue* IdentityInternalsUIMessageHandler::GetInfoForToken(
206 const extensions::ExtensionTokenKey& token_cache_key,
207 const extensions::IdentityTokenCacheValue& token_cache_value) {
208 base::DictionaryValue* token_data = new base::DictionaryValue();
209 token_data->SetString(kExtensionId, token_cache_key.extension_id);
210 token_data->SetString(kExtensionName, GetExtensionName(token_cache_key));
211 token_data->Set(kScopes, GetScopes(token_cache_key));
212 token_data->SetString(kStatus, GetStatus(token_cache_value));
213 token_data->SetString(kAccessToken, token_cache_value.token());
214 token_data->SetString(kTokenExpirationTime,
215 GetExpirationTime(token_cache_value));
216 return token_data;
219 void IdentityInternalsUIMessageHandler::GetInfoForAllTokens(
220 const base::ListValue* args) {
221 base::ListValue results;
222 extensions::IdentityAPI::CachedTokens tokens =
223 extensions::IdentityAPI::GetFactoryInstance()
224 ->Get(Profile::FromWebUI(web_ui()))
225 ->GetAllCachedTokens();
226 for (extensions::IdentityAPI::CachedTokens::const_iterator
227 iter = tokens.begin(); iter != tokens.end(); ++iter) {
228 results.Append(GetInfoForToken(iter->first, iter->second));
231 web_ui()->CallJavascriptFunction("identity_internals.returnTokens", results);
234 void IdentityInternalsUIMessageHandler::RegisterMessages() {
235 web_ui()->RegisterMessageCallback("identityInternalsGetTokens",
236 base::Bind(&IdentityInternalsUIMessageHandler::GetInfoForAllTokens,
237 base::Unretained(this)));
238 web_ui()->RegisterMessageCallback("identityInternalsRevokeToken",
239 base::Bind(&IdentityInternalsUIMessageHandler::RevokeToken,
240 base::Unretained(this)));
243 void IdentityInternalsUIMessageHandler::RevokeToken(
244 const base::ListValue* args) {
245 std::string extension_id;
246 std::string access_token;
247 args->GetString(kRevokeTokenExtensionOffset, &extension_id);
248 args->GetString(kRevokeTokenTokenOffset, &access_token);
249 token_revokers_.push_back(new IdentityInternalsTokenRevoker(
250 extension_id, access_token, Profile::FromWebUI(web_ui()), this));
253 IdentityInternalsTokenRevoker::IdentityInternalsTokenRevoker(
254 const std::string& extension_id,
255 const std::string& access_token,
256 Profile* profile,
257 IdentityInternalsUIMessageHandler* consumer)
258 : fetcher_(this, GaiaConstants::kChromeSource,
259 profile->GetRequestContext()),
260 extension_id_(extension_id),
261 access_token_(access_token),
262 consumer_(consumer) {
263 DCHECK(consumer_);
264 fetcher_.StartRevokeOAuth2Token(access_token);
267 IdentityInternalsTokenRevoker::~IdentityInternalsTokenRevoker() {}
269 void IdentityInternalsTokenRevoker::OnOAuth2RevokeTokenCompleted() {
270 consumer_->OnTokenRevokerDone(this);
273 } // namespace
275 IdentityInternalsUI::IdentityInternalsUI(content::WebUI* web_ui)
276 : content::WebUIController(web_ui) {
277 // chrome://identity-internals source.
278 content::WebUIDataSource* html_source =
279 content::WebUIDataSource::Create(chrome::kChromeUIIdentityInternalsHost);
281 // Localized strings
282 html_source->AddLocalizedString("tokenCacheHeader",
283 IDS_IDENTITY_INTERNALS_TOKEN_CACHE_TEXT);
284 html_source->AddLocalizedString("accessToken",
285 IDS_IDENTITY_INTERNALS_ACCESS_TOKEN);
286 html_source->AddLocalizedString("extensionName",
287 IDS_IDENTITY_INTERNALS_EXTENSION_NAME);
288 html_source->AddLocalizedString("extensionId",
289 IDS_IDENTITY_INTERNALS_EXTENSION_ID);
290 html_source->AddLocalizedString("tokenStatus",
291 IDS_IDENTITY_INTERNALS_TOKEN_STATUS);
292 html_source->AddLocalizedString("expirationTime",
293 IDS_IDENTITY_INTERNALS_EXPIRATION_TIME);
294 html_source->AddLocalizedString("scopes",
295 IDS_IDENTITY_INTERNALS_SCOPES);
296 html_source->AddLocalizedString("revoke",
297 IDS_IDENTITY_INTERNALS_REVOKE);
298 html_source->SetJsonPath("strings.js");
300 // Required resources
301 html_source->AddResourcePath("identity_internals.css",
302 IDR_IDENTITY_INTERNALS_CSS);
303 html_source->AddResourcePath("identity_internals.js",
304 IDR_IDENTITY_INTERNALS_JS);
305 html_source->SetDefaultResource(IDR_IDENTITY_INTERNALS_HTML);
307 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source);
309 web_ui->AddMessageHandler(new IdentityInternalsUIMessageHandler());
312 IdentityInternalsUI::~IdentityInternalsUI() {}