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

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: Rebase to ToT for commit Created 6 years, 4 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 token_revoker->callback().Run();
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 const mojo::Callback<void(mojo::Array<IdentityTokenMojoPtr>)>& callback) {
42 extensions::IdentityAPI::CachedTokens tokens =
43 extensions::IdentityAPI::GetFactoryInstance()
44 ->Get(profile_)->GetAllCachedTokens();
45 callback.Run(ConvertCachedTokens(tokens).Pass());
46 }
47
48 void IdentityInternalsUIHandler::RevokeToken(
49 const mojo::String& extension_id,
50 const mojo::String& access_token,
51 const mojo::Callback<void()>& callback) {
52 token_revokers_.push_back(new IdentityInternalsTokenRevoker(
53 extension_id, access_token, callback, profile_, this));
54 }
55
56 mojo::Array<IdentityTokenMojoPtr>
57 IdentityInternalsUIHandler::ConvertCachedTokens(
58 const extensions::IdentityAPI::CachedTokens& tokens) {
59 mojo::Array<IdentityTokenMojoPtr> array(tokens.size());
60 size_t index = 0;
61 for (extensions::IdentityAPI::CachedTokens::const_iterator
62 it = tokens.begin(); it != tokens.end(); ++it, index++) {
63 IdentityTokenMojoPtr item(IdentityTokenMojo::New());
64 item->access_token = it->second.token();
65 item->extension_name = GetExtensionName(it->first);
66 item->extension_id = it->first.extension_id;
67 item->token_status = GetStatus(it->second);
68 item->expiration_time = GetExpirationTime(it->second);
69 item->scopes = GetScopes(it->first).Pass();
70
71 array[index] = item.Pass();
72 }
73
74 return array.Pass();
75 }
76
77 const std::string IdentityInternalsUIHandler::GetExtensionName(
78 const extensions::ExtensionTokenKey& token_cache_key) {
79 ExtensionService* extension_service = extensions::ExtensionSystem::Get(
80 profile_)->extension_service();
81 const extensions::Extension* extension =
82 extension_service->extensions()->GetByID(token_cache_key.extension_id);
83 if (!extension)
84 return std::string();
85 return extension->name();
86 }
87
88 mojo::Array<mojo::String> IdentityInternalsUIHandler::GetScopes(
89 const extensions::ExtensionTokenKey& token_cache_key) {
90 mojo::Array<mojo::String> array(token_cache_key.scopes.size());
91 size_t index = 0;
92 for (std::set<std::string>::const_iterator
93 it = token_cache_key.scopes.begin();
94 it != token_cache_key.scopes.end(); ++it, index++) {
95 array[index] = mojo::String(*it);
96 }
97 return array.Pass();
98 }
99
100 const std::string IdentityInternalsUIHandler::GetStatus(
101 const extensions::IdentityTokenCacheValue& token_cache_value) {
102 switch (token_cache_value.status()) {
103 case extensions::IdentityTokenCacheValue::CACHE_STATUS_ADVICE:
104 // Fallthrough to NOT FOUND case, as ADVICE is short lived.
105 case extensions::IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND:
106 return l10n_util::GetStringUTF8(
107 IDS_IDENTITY_INTERNALS_TOKEN_NOT_FOUND);
108 case extensions::IdentityTokenCacheValue::CACHE_STATUS_TOKEN:
109 return l10n_util::GetStringUTF8(
110 IDS_IDENTITY_INTERNALS_TOKEN_PRESENT);
111 }
112 NOTREACHED();
113 return std::string();
114 }
115
116 const std::string IdentityInternalsUIHandler::GetExpirationTime(
117 const extensions::IdentityTokenCacheValue& token_cache_value) {
118 return base::UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime(
119 token_cache_value.expiration_time()));
120 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698