OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/options/manage_profile_handler.h" | 5 #include "chrome/browser/ui/webui/options/manage_profile_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "base/value_conversions.h" | 11 #include "base/value_conversions.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/profiles/profile_info_cache.h" | 14 #include "chrome/browser/profiles/profile_info_cache.h" |
15 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
16 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "content/browser/tab_contents/tab_contents.h" |
17 #include "content/common/notification_service.h" | 18 #include "content/common/notification_service.h" |
18 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
19 | 20 |
20 ManageProfileHandler::ManageProfileHandler() { | 21 ManageProfileHandler::ManageProfileHandler() { |
21 } | 22 } |
22 | 23 |
23 ManageProfileHandler::~ManageProfileHandler() { | 24 ManageProfileHandler::~ManageProfileHandler() { |
24 } | 25 } |
25 | 26 |
26 void ManageProfileHandler::GetLocalizedValues( | 27 void ManageProfileHandler::GetLocalizedValues( |
(...skipping 10 matching lines...) Expand all Loading... |
37 { "deleteProfileOK", IDS_PROFILES_DELETE_OK_BUTTON_LABEL }, | 38 { "deleteProfileOK", IDS_PROFILES_DELETE_OK_BUTTON_LABEL }, |
38 { "deleteProfileMessage", IDS_PROFILES_DELETE_MESSAGE }, | 39 { "deleteProfileMessage", IDS_PROFILES_DELETE_MESSAGE }, |
39 }; | 40 }; |
40 | 41 |
41 RegisterStrings(localized_strings, resources, arraysize(resources)); | 42 RegisterStrings(localized_strings, resources, arraysize(resources)); |
42 } | 43 } |
43 | 44 |
44 void ManageProfileHandler::Initialize() { | 45 void ManageProfileHandler::Initialize() { |
45 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 46 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
46 NotificationService::AllSources()); | 47 NotificationService::AllSources()); |
47 InitializeDefaultProfileIcons(); | |
48 SendProfileNames(); | 48 SendProfileNames(); |
49 } | 49 } |
50 | 50 |
51 void ManageProfileHandler::RegisterMessages() { | 51 void ManageProfileHandler::RegisterMessages() { |
52 web_ui_->RegisterMessageCallback("setProfileNameAndIcon", | 52 web_ui_->RegisterMessageCallback("setProfileNameAndIcon", |
53 base::Bind(&ManageProfileHandler::SetProfileNameAndIcon, | 53 base::Bind(&ManageProfileHandler::SetProfileNameAndIcon, |
54 base::Unretained(this))); | 54 base::Unretained(this))); |
55 web_ui_->RegisterMessageCallback("deleteProfile", | 55 web_ui_->RegisterMessageCallback("deleteProfile", |
56 base::Bind(&ManageProfileHandler::DeleteProfile, | 56 base::Bind(&ManageProfileHandler::DeleteProfile, |
57 base::Unretained(this))); | 57 base::Unretained(this))); |
| 58 web_ui_->RegisterMessageCallback("requestDefaultProfileIcons", |
| 59 base::Bind(&ManageProfileHandler::RequestDefaultProfileIcons, |
| 60 base::Unretained(this))); |
| 61 web_ui_->RegisterMessageCallback("requestProfileInfo", |
| 62 base::Bind(&ManageProfileHandler::RequestProfileInfo, |
| 63 base::Unretained(this))); |
58 } | 64 } |
59 | 65 |
60 void ManageProfileHandler::Observe(int type, | 66 void ManageProfileHandler::Observe(int type, |
61 const NotificationSource& source, | 67 const NotificationSource& source, |
62 const NotificationDetails& details) { | 68 const NotificationDetails& details) { |
63 if (type == chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED) | 69 if (type == chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED) |
64 SendProfileNames(); | 70 SendProfileNames(); |
65 else | 71 else |
66 OptionsPageUIHandler::Observe(type, source, details); | 72 OptionsPageUIHandler::Observe(type, source, details); |
67 } | 73 } |
68 | 74 |
69 void ManageProfileHandler::InitializeDefaultProfileIcons() { | 75 void ManageProfileHandler::RequestDefaultProfileIcons(const ListValue* args) { |
70 ListValue image_url_list; | 76 ListValue image_url_list; |
71 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); i++) { | 77 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); i++) { |
72 std::string url = ProfileInfoCache::GetDefaultAvatarIconUrl(i); | 78 std::string url = ProfileInfoCache::GetDefaultAvatarIconUrl(i); |
73 image_url_list.Append(Value::CreateStringValue(url)); | 79 image_url_list.Append(Value::CreateStringValue(url)); |
74 } | 80 } |
75 | 81 |
76 web_ui_->CallJavascriptFunction( | 82 web_ui_->CallJavascriptFunction( |
77 "ManageProfileOverlay.receiveDefaultProfileIcons", | 83 "ManageProfileOverlay.receiveDefaultProfileIcons", |
78 image_url_list); | 84 image_url_list); |
79 } | 85 } |
80 | 86 |
81 void ManageProfileHandler::SendProfileNames() { | 87 void ManageProfileHandler::SendProfileNames() { |
82 ProfileInfoCache& cache = | 88 ProfileInfoCache& cache = |
83 g_browser_process->profile_manager()->GetProfileInfoCache(); | 89 g_browser_process->profile_manager()->GetProfileInfoCache(); |
84 DictionaryValue profile_name_dict; | 90 DictionaryValue profile_name_dict; |
85 for (size_t i = 0, e = cache.GetNumberOfProfiles(); i < e; ++i) | 91 for (size_t i = 0, e = cache.GetNumberOfProfiles(); i < e; ++i) |
86 profile_name_dict.SetBoolean(UTF16ToUTF8(cache.GetNameOfProfileAtIndex(i)), | 92 profile_name_dict.SetBoolean(UTF16ToUTF8(cache.GetNameOfProfileAtIndex(i)), |
87 true); | 93 true); |
88 | 94 |
89 web_ui_->CallJavascriptFunction("ManageProfileOverlay.receiveProfileNames", | 95 web_ui_->CallJavascriptFunction("ManageProfileOverlay.receiveProfileNames", |
90 profile_name_dict); | 96 profile_name_dict); |
91 } | 97 } |
92 | 98 |
93 void ManageProfileHandler::SetProfileNameAndIcon(const ListValue* args) { | 99 void ManageProfileHandler::SetProfileNameAndIcon(const ListValue* args) { |
| 100 DCHECK(args); |
| 101 |
94 Value* file_path_value; | 102 Value* file_path_value; |
95 FilePath profile_file_path; | 103 FilePath profile_file_path; |
96 if (!args->Get(0, &file_path_value) || | 104 if (!args->Get(0, &file_path_value) || |
97 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) | 105 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) |
98 return; | 106 return; |
99 | 107 |
100 ProfileInfoCache& cache = | 108 ProfileInfoCache& cache = |
101 g_browser_process->profile_manager()->GetProfileInfoCache(); | 109 g_browser_process->profile_manager()->GetProfileInfoCache(); |
102 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); | 110 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); |
103 if (profile_index == std::string::npos) | 111 if (profile_index == std::string::npos) |
104 return; | 112 return; |
105 | 113 |
106 string16 new_profile_name; | 114 string16 new_profile_name; |
107 if (!args->GetString(1, &new_profile_name)) | 115 if (!args->GetString(1, &new_profile_name)) |
108 return; | 116 return; |
109 | 117 |
110 cache.SetNameOfProfileAtIndex(profile_index, new_profile_name); | 118 cache.SetNameOfProfileAtIndex(profile_index, new_profile_name); |
111 | 119 |
112 string16 icon_url; | 120 string16 icon_url; |
113 size_t new_icon_index; | 121 size_t new_icon_index; |
114 if (!args->GetString(2, &icon_url) || | 122 if (!args->GetString(2, &icon_url) || |
115 !cache.IsDefaultAvatarIconUrl(UTF16ToUTF8(icon_url), &new_icon_index)) | 123 !cache.IsDefaultAvatarIconUrl(UTF16ToUTF8(icon_url), &new_icon_index)) |
116 return; | 124 return; |
117 | 125 |
118 cache.SetAvatarIconOfProfileAtIndex(profile_index, new_icon_index); | 126 cache.SetAvatarIconOfProfileAtIndex(profile_index, new_icon_index); |
119 } | 127 } |
120 | 128 |
121 void ManageProfileHandler::DeleteProfile(const ListValue* args) { | 129 void ManageProfileHandler::DeleteProfile(const ListValue* args) { |
| 130 DCHECK(args); |
| 131 |
122 Value* file_path_value; | 132 Value* file_path_value; |
123 FilePath profile_file_path; | 133 FilePath profile_file_path; |
124 if (!args->Get(0, &file_path_value) || | 134 if (!args->Get(0, &file_path_value) || |
125 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) | 135 !base::GetValueAsFilePath(*file_path_value, &profile_file_path)) |
126 return; | 136 return; |
127 | 137 |
128 g_browser_process->profile_manager()->ScheduleProfileForDeletion( | 138 g_browser_process->profile_manager()->ScheduleProfileForDeletion( |
129 profile_file_path); | 139 profile_file_path); |
130 } | 140 } |
131 | 141 |
| 142 void ManageProfileHandler::RequestProfileInfo(const ListValue* args) { |
| 143 DCHECK(args); |
| 144 |
| 145 DictionaryValue profile_value; |
| 146 |
| 147 Value* index_value; |
| 148 double index_double; |
| 149 if (!args->Get(0, &index_value) || !index_value->GetAsDouble(&index_double)) |
| 150 return; |
| 151 |
| 152 int index = static_cast<int>(index_double); |
| 153 |
| 154 ProfileInfoCache& cache = |
| 155 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 156 int profile_count = cache.GetNumberOfProfiles(); |
| 157 if (index < 0 && index >= profile_count) |
| 158 return; |
| 159 |
| 160 FilePath current_profile_path = |
| 161 web_ui_->tab_contents()->browser_context()->GetPath(); |
| 162 size_t icon_index = cache.GetAvatarIconIndexOfProfileAtIndex(index); |
| 163 FilePath profile_path = cache.GetPathOfProfileAtIndex(index); |
| 164 profile_value.SetString("name", cache.GetNameOfProfileAtIndex(index)); |
| 165 profile_value.SetString("iconURL", |
| 166 cache.GetDefaultAvatarIconUrl(icon_index)); |
| 167 profile_value.Set("filePath", base::CreateFilePathValue(profile_path)); |
| 168 profile_value.SetBoolean("isCurrentProfile", |
| 169 profile_path == current_profile_path); |
| 170 |
| 171 web_ui_->CallJavascriptFunction("ManageProfileOverlay.setProfileInfo", |
| 172 profile_value); |
| 173 } |
OLD | NEW |