| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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/settings/profile_info_handler.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/profiles/profile_attributes_entry.h" |
| 12 #include "chrome/browser/ui/user_manager.h" |
| 13 #include "ui/base/webui/web_ui_util.h" |
| 14 |
| 15 #if defined(OS_CHROMEOS) |
| 16 #include "chrome/browser/chrome_notification_types.h" |
| 17 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 18 #include "chrome/browser/ui/webui/options/chromeos/user_image_source.h" |
| 19 #include "components/signin/core/account_id/account_id.h" |
| 20 #include "components/user_manager/user_manager.h" |
| 21 #include "content/public/browser/notification_service.h" |
| 22 #else |
| 23 #include "base/strings/utf_string_conversions.h" |
| 24 #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
| 25 #include "third_party/skia/include/core/SkBitmap.h" |
| 26 #endif |
| 27 |
| 28 namespace settings { |
| 29 |
| 30 // static |
| 31 const char ProfileInfoHandler::kProfileInfoChangedEventName[] = |
| 32 "profile-info-changed"; |
| 33 |
| 34 ProfileInfoHandler::ProfileInfoHandler(Profile* profile) |
| 35 : profile_(profile), observers_registered_(false) {} |
| 36 |
| 37 void ProfileInfoHandler::RegisterMessages() { |
| 38 web_ui()->RegisterMessageCallback( |
| 39 "getProfileInfo", base::Bind(&ProfileInfoHandler::HandleGetProfileInfo, |
| 40 base::Unretained(this))); |
| 41 } |
| 42 |
| 43 void ProfileInfoHandler::RenderViewReused() { |
| 44 if (!observers_registered_) |
| 45 return; |
| 46 |
| 47 g_browser_process->profile_manager() |
| 48 ->GetProfileAttributesStorage() |
| 49 .RemoveObserver(this); |
| 50 |
| 51 #if defined(OS_CHROMEOS) |
| 52 registrar_.RemoveAll(); |
| 53 #endif |
| 54 |
| 55 observers_registered_ = false; |
| 56 } |
| 57 |
| 58 #if defined(OS_CHROMEOS) |
| 59 void ProfileInfoHandler::Observe(int type, |
| 60 const content::NotificationSource& source, |
| 61 const content::NotificationDetails& details) { |
| 62 switch (type) { |
| 63 case chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED: |
| 64 PushProfileInfo(); |
| 65 break; |
| 66 default: |
| 67 NOTREACHED(); |
| 68 } |
| 69 } |
| 70 #endif |
| 71 |
| 72 void ProfileInfoHandler::OnProfileNameChanged( |
| 73 const base::FilePath& /* profile_path */, |
| 74 const base::string16& /* old_profile_name */) { |
| 75 PushProfileInfo(); |
| 76 } |
| 77 |
| 78 void ProfileInfoHandler::OnProfileAvatarChanged( |
| 79 const base::FilePath& /* profile_path */) { |
| 80 PushProfileInfo(); |
| 81 } |
| 82 |
| 83 void ProfileInfoHandler::HandleGetProfileInfo(const base::ListValue* args) { |
| 84 if (!observers_registered_) { |
| 85 observers_registered_ = true; |
| 86 |
| 87 g_browser_process->profile_manager() |
| 88 ->GetProfileAttributesStorage() |
| 89 .AddObserver(this); |
| 90 |
| 91 #if defined(OS_CHROMEOS) |
| 92 registrar_.Add(this, chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED, |
| 93 content::NotificationService::AllSources()); |
| 94 #endif |
| 95 } |
| 96 |
| 97 CHECK_EQ(1U, args->GetSize()); |
| 98 const base::Value* callback_id; |
| 99 CHECK(args->Get(0, &callback_id)); |
| 100 |
| 101 ResolveJavascriptCallback(*callback_id, *GetAccountNameAndIcon()); |
| 102 } |
| 103 |
| 104 void ProfileInfoHandler::PushProfileInfo() { |
| 105 web_ui()->CallJavascriptFunction( |
| 106 "cr.webUIListenerCallback", |
| 107 base::StringValue(kProfileInfoChangedEventName), |
| 108 *GetAccountNameAndIcon()); |
| 109 } |
| 110 |
| 111 std::unique_ptr<base::DictionaryValue> |
| 112 ProfileInfoHandler::GetAccountNameAndIcon() const { |
| 113 std::string name; |
| 114 std::string icon_url; |
| 115 |
| 116 #if defined(OS_CHROMEOS) |
| 117 name = profile_->GetProfileUserName(); |
| 118 if (name.empty()) { |
| 119 const user_manager::User* user = |
| 120 chromeos::ProfileHelper::Get()->GetUserByProfile(profile_); |
| 121 if (user && (user->GetType() != user_manager::USER_TYPE_GUEST)) |
| 122 name = user->email(); |
| 123 } |
| 124 if (!name.empty()) |
| 125 name = gaia::SanitizeEmail(gaia::CanonicalizeEmail(name)); |
| 126 |
| 127 // Get image as data URL instead of using chrome://userimage source to avoid |
| 128 // issues with caching. |
| 129 const AccountId account_id(AccountId::FromUserEmail(name)); |
| 130 scoped_refptr<base::RefCountedMemory> image = |
| 131 chromeos::options::UserImageSource::GetUserImage(account_id); |
| 132 icon_url = webui::GetPngDataUrl(image->front(), image->size()); |
| 133 #else // !defined(OS_CHROMEOS) |
| 134 ProfileAttributesEntry* entry; |
| 135 if (g_browser_process->profile_manager() |
| 136 ->GetProfileAttributesStorage() |
| 137 .GetProfileAttributesWithPath(profile_->GetPath(), &entry)) { |
| 138 name = base::UTF16ToUTF8(entry->GetName()); |
| 139 |
| 140 if (entry->IsUsingGAIAPicture() && entry->GetGAIAPicture()) { |
| 141 gfx::Image icon = |
| 142 profiles::GetAvatarIconForWebUI(entry->GetAvatarIcon(), true); |
| 143 icon_url = webui::GetBitmapDataUrl(icon.AsBitmap()); |
| 144 } else { |
| 145 icon_url = profiles::GetDefaultAvatarIconUrl(entry->GetAvatarIconIndex()); |
| 146 } |
| 147 } |
| 148 #endif // defined(OS_CHROMEOS) |
| 149 |
| 150 base::DictionaryValue* response = new base::DictionaryValue(); |
| 151 response->SetString("name", name); |
| 152 response->SetString("iconUrl", icon_url); |
| 153 return base::WrapUnique(response); |
| 154 } |
| 155 |
| 156 } // namespace settings |
| OLD | NEW |