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

Side by Side Diff: chrome/browser/ui/webui/identity_internals/identity_internals_ui_handler.cc

Issue 365513002: Port identity_internals to mojo (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Maybe fix the gn build. Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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/ui/webui/identity_internals/identity_internals_ui_handl er.h"
6
7 #include "base/i18n/time_formatting.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/webui/identity_internals/identity_internals_token_re voker.h"
12 #include "extensions/browser/extension_system.h"
13 #include "grit/browser_resources.h"
14 #include "grit/generated_resources.h"
15 #include "ui/base/l10n/l10n_util.h"
16
17 IdentityInternalsUIHandler::IdentityInternalsUIHandler(Profile* profile)
18 : profile_(profile) {}
19
20 IdentityInternalsUIHandler::~IdentityInternalsUIHandler() {}
21
22 void IdentityInternalsUIHandler::OnTokenRevokerDone(
23 IdentityInternalsTokenRevoker* token_revoker) {
24 // Remove token from the cache.
25 extensions::IdentityAPI::GetFactoryInstance()
26 ->Get(profile_)
27 ->EraseCachedToken(token_revoker->extension_id(),
28 token_revoker->access_token());
29
30 // Update view about the token being removed.
31 client()->RevokeTokenDone(token_revoker->access_token());
32
33 // Erase the revoker.
34 ScopedVector<IdentityInternalsTokenRevoker>::iterator iter =
35 std::find(token_revokers_.begin(), token_revokers_.end(), token_revoker);
36 DCHECK(iter != token_revokers_.end());
37 token_revokers_.erase(iter);
38 }
39
40 void IdentityInternalsUIHandler::GetTokens() {
41 extensions::IdentityAPI::CachedTokens tokens =
42 extensions::IdentityAPI::GetFactoryInstance()
43 ->Get(profile_)->GetAllCachedTokens();
44 client()->GetTokensDone(ConvertCachedTokens(tokens).Pass());
45 }
46
47 void IdentityInternalsUIHandler::RevokeToken(const mojo::String& extension_id,
48 const mojo::String& access_token) {
49 token_revokers_.push_back(new IdentityInternalsTokenRevoker(
50 extension_id, access_token, profile_, this));
51 }
52
53 mojo::Array<IdentityTokenMojoPtr>
54 IdentityInternalsUIHandler::ConvertCachedTokens(
55 const extensions::IdentityAPI::CachedTokens& tokens) {
56 mojo::Array<IdentityTokenMojoPtr> array(tokens.size());
57 size_t index = 0;
58 for (extensions::IdentityAPI::CachedTokens::const_iterator
59 it = tokens.begin(); it != tokens.end(); ++it, index++) {
60 IdentityTokenMojoPtr item(IdentityTokenMojo::New());
61 item->access_token = it->second.token();
62 item->extension_name = GetExtensionName(it->first);
63 item->extension_id = it->first.extension_id;
64 item->token_status = GetStatus(it->second);
65 item->expiration_time = GetExpirationTime(it->second);
66 item->scopes = GetScopes(it->first).Pass();
67
68 array[index] = item.Pass();
69 }
70
71 return array.Pass();
72 }
73
74 const std::string IdentityInternalsUIHandler::GetExtensionName(
75 const extensions::ExtensionTokenKey& token_cache_key) {
76 ExtensionService* extension_service = extensions::ExtensionSystem::Get(
77 profile_)->extension_service();
78 const extensions::Extension* extension =
79 extension_service->extensions()->GetByID(token_cache_key.extension_id);
80 if (!extension)
81 return std::string();
82 return extension->name();
83 }
84
85 mojo::Array<mojo::String> IdentityInternalsUIHandler::GetScopes(
86 const extensions::ExtensionTokenKey& token_cache_key) {
87 mojo::Array<mojo::String> array(token_cache_key.scopes.size());
88 size_t index = 0;
89 for (std::set<std::string>::const_iterator
90 it = token_cache_key.scopes.begin();
91 it != token_cache_key.scopes.end(); ++it, index++) {
92 array[index] = mojo::String(*it);
93 }
94 return array.Pass();
95 }
96
97 const std::string IdentityInternalsUIHandler::GetStatus(
98 const extensions::IdentityTokenCacheValue& token_cache_value) {
99 switch (token_cache_value.status()) {
100 case extensions::IdentityTokenCacheValue::CACHE_STATUS_ADVICE:
101 // Fallthrough to NOT FOUND case, as ADVICE is short lived.
102 case extensions::IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND:
103 return l10n_util::GetStringUTF8(
104 IDS_IDENTITY_INTERNALS_TOKEN_NOT_FOUND);
105 case extensions::IdentityTokenCacheValue::CACHE_STATUS_TOKEN:
106 return l10n_util::GetStringUTF8(
107 IDS_IDENTITY_INTERNALS_TOKEN_PRESENT);
108 }
109 NOTREACHED();
110 return std::string();
111 }
112
113 const std::string IdentityInternalsUIHandler::GetExpirationTime(
114 const extensions::IdentityTokenCacheValue& token_cache_value) {
115 return base::UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime(
116 token_cache_value.expiration_time()));
117 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698