OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/app_list/start_page_handler.h" | 5 #include "chrome/browser/ui/webui/app_list/start_page_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/chrome_notification_types.h" | 12 #include "base/version.h" |
13 #include "chrome/browser/extensions/extension_service.h" | |
14 #include "chrome/browser/omaha_query_params/omaha_query_params.h" | 13 #include "chrome/browser/omaha_query_params/omaha_query_params.h" |
15 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/search/hotword_service.h" | 15 #include "chrome/browser/search/hotword_service.h" |
17 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h" | 16 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h" |
18 #include "chrome/browser/ui/app_list/app_list_service.h" | 17 #include "chrome/browser/ui/app_list/app_list_service.h" |
19 #include "chrome/browser/ui/app_list/recommended_apps.h" | 18 #include "chrome/browser/ui/app_list/recommended_apps.h" |
20 #include "chrome/browser/ui/app_list/start_page_service.h" | 19 #include "chrome/browser/ui/app_list/start_page_service.h" |
21 #include "chrome/browser/ui/host_desktop.h" | 20 #include "chrome/browser/ui/host_desktop.h" |
22 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | 21 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
23 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
24 #include "content/public/browser/notification_details.h" | |
25 #include "content/public/browser/notification_source.h" | |
26 #include "content/public/browser/web_ui.h" | 23 #include "content/public/browser/web_ui.h" |
24 #include "extensions/browser/extension_registry.h" | |
27 #include "extensions/browser/extension_system.h" | 25 #include "extensions/browser/extension_system.h" |
26 #include "extensions/common/constants.h" | |
28 #include "extensions/common/extension.h" | 27 #include "extensions/common/extension.h" |
29 #include "extensions/common/extension_icon_set.h" | 28 #include "extensions/common/extension_icon_set.h" |
30 #include "ui/app_list/app_list_switches.h" | 29 #include "ui/app_list/app_list_switches.h" |
31 #include "ui/app_list/speech_ui_model_observer.h" | 30 #include "ui/app_list/speech_ui_model_observer.h" |
32 #include "ui/events/event_constants.h" | 31 #include "ui/events/event_constants.h" |
33 | 32 |
34 namespace app_list { | 33 namespace app_list { |
35 | 34 |
36 namespace { | 35 namespace { |
37 | 36 |
(...skipping 16 matching lines...) Expand all Loading... | |
54 ExtensionIconSet::MATCH_BIGGER, | 53 ExtensionIconSet::MATCH_BIGGER, |
55 grayscale, | 54 grayscale, |
56 &icon_exists); | 55 &icon_exists); |
57 dict->SetString("iconUrl", icon_url.spec()); | 56 dict->SetString("iconUrl", icon_url.spec()); |
58 | 57 |
59 return dict.Pass(); | 58 return dict.Pass(); |
60 } | 59 } |
61 | 60 |
62 } // namespace | 61 } // namespace |
63 | 62 |
64 StartPageHandler::StartPageHandler() : recommended_apps_(NULL) {} | 63 StartPageHandler::StartPageHandler() |
64 : recommended_apps_(NULL), extension_registry_observer_(this) { | |
xiyuan
2014/05/24 05:46:59
nit: one member per line
limasdf
2014/05/24 06:37:13
Done.
| |
65 } | |
65 | 66 |
66 StartPageHandler::~StartPageHandler() { | 67 StartPageHandler::~StartPageHandler() { |
67 if (recommended_apps_) | 68 if (recommended_apps_) |
68 recommended_apps_->RemoveObserver(this); | 69 recommended_apps_->RemoveObserver(this); |
69 } | 70 } |
70 | 71 |
71 void StartPageHandler::RegisterMessages() { | 72 void StartPageHandler::RegisterMessages() { |
72 web_ui()->RegisterMessageCallback( | 73 web_ui()->RegisterMessageCallback( |
73 "initialize", | 74 "initialize", |
74 base::Bind(&StartPageHandler::HandleInitialize, base::Unretained(this))); | 75 base::Bind(&StartPageHandler::HandleInitialize, base::Unretained(this))); |
75 web_ui()->RegisterMessageCallback( | 76 web_ui()->RegisterMessageCallback( |
76 "launchApp", | 77 "launchApp", |
77 base::Bind(&StartPageHandler::HandleLaunchApp, base::Unretained(this))); | 78 base::Bind(&StartPageHandler::HandleLaunchApp, base::Unretained(this))); |
78 web_ui()->RegisterMessageCallback( | 79 web_ui()->RegisterMessageCallback( |
79 "speechResult", | 80 "speechResult", |
80 base::Bind(&StartPageHandler::HandleSpeechResult, | 81 base::Bind(&StartPageHandler::HandleSpeechResult, |
81 base::Unretained(this))); | 82 base::Unretained(this))); |
82 web_ui()->RegisterMessageCallback( | 83 web_ui()->RegisterMessageCallback( |
83 "speechSoundLevel", | 84 "speechSoundLevel", |
84 base::Bind(&StartPageHandler::HandleSpeechSoundLevel, | 85 base::Bind(&StartPageHandler::HandleSpeechSoundLevel, |
85 base::Unretained(this))); | 86 base::Unretained(this))); |
86 web_ui()->RegisterMessageCallback( | 87 web_ui()->RegisterMessageCallback( |
87 "setSpeechRecognitionState", | 88 "setSpeechRecognitionState", |
88 base::Bind(&StartPageHandler::HandleSpeechRecognition, | 89 base::Bind(&StartPageHandler::HandleSpeechRecognition, |
89 base::Unretained(this))); | 90 base::Unretained(this))); |
90 } | 91 } |
91 | 92 |
92 void StartPageHandler::Observe(int type, | 93 void StartPageHandler::OnExtensionLoaded( |
93 const content::NotificationSource& source, | 94 content::BrowserContext* browser_context, |
94 const content::NotificationDetails& details) { | 95 const extensions::Extension* extension) { |
95 #if defined(OS_CHROMEOS) | 96 #if defined(OS_CHROMEOS) |
96 DCHECK_EQ(Profile::FromWebUI(web_ui()), | 97 DCHECK_EQ(Profile::FromWebUI(web_ui()), |
97 content::Source<Profile>(source).ptr()); | 98 Profile::FromBrowserContext(browser_context)); |
98 switch (type) { | 99 if (extension->id() == extension_misc::kHotwordExtensionId) |
99 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: { | 100 OnHotwordEnabledChanged(); |
100 extensions::Extension* extension = | |
101 content::Details<extensions::Extension>(details).ptr(); | |
102 if (extension->id() == extension_misc::kHotwordExtensionId) | |
103 OnHotwordEnabledChanged(); | |
104 break; | |
105 } | |
106 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { | |
107 extensions::UnloadedExtensionInfo* info = | |
108 content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); | |
109 if (info->extension->id() == extension_misc::kHotwordExtensionId) | |
110 OnHotwordEnabledChanged(); | |
111 break; | |
112 } | |
113 default: | |
114 NOTREACHED(); | |
115 break; | |
116 } | |
117 #endif | 101 #endif |
118 } | 102 } |
119 | 103 |
104 void StartPageHandler::OnExtensionUnloaded( | |
105 content::BrowserContext* browser_context, | |
106 const extensions::Extension* extension, | |
107 extensions::UnloadedExtensionInfo::Reason reason) { | |
108 #if defined(OS_CHROMEOS) | |
109 DCHECK_EQ(Profile::FromWebUI(web_ui()), | |
110 Profile::FromBrowserContext(browser_context)); | |
111 if (extension->id() == extension_misc::kHotwordExtensionId) | |
112 OnHotwordEnabledChanged(); | |
113 #endif | |
114 } | |
115 | |
120 void StartPageHandler::OnRecommendedAppsChanged() { | 116 void StartPageHandler::OnRecommendedAppsChanged() { |
121 SendRecommendedApps(); | 117 SendRecommendedApps(); |
122 } | 118 } |
123 | 119 |
124 void StartPageHandler::SendRecommendedApps() { | 120 void StartPageHandler::SendRecommendedApps() { |
125 const RecommendedApps::Apps& recommends = recommended_apps_->apps(); | 121 const RecommendedApps::Apps& recommends = recommended_apps_->apps(); |
126 | 122 |
127 base::ListValue recommended_list; | 123 base::ListValue recommended_list; |
128 for (size_t i = 0; i < recommends.size(); ++i) { | 124 for (size_t i = 0; i < recommends.size(); ++i) { |
129 recommended_list.Append(CreateAppInfo(recommends[i].get()).release()); | 125 recommended_list.Append(CreateAppInfo(recommends[i].get()).release()); |
130 } | 126 } |
131 | 127 |
132 web_ui()->CallJavascriptFunction("appList.startPage.setRecommendedApps", | 128 web_ui()->CallJavascriptFunction("appList.startPage.setRecommendedApps", |
133 recommended_list); | 129 recommended_list); |
134 } | 130 } |
135 | 131 |
136 #if defined(OS_CHROMEOS) | 132 #if defined(OS_CHROMEOS) |
137 void StartPageHandler::OnHotwordEnabledChanged() { | 133 void StartPageHandler::OnHotwordEnabledChanged() { |
138 // If the hotword extension is new enough, we should use the new | 134 // If the hotword extension is new enough, we should use the new |
139 // hotwordPrivate API to provide the feature. | 135 // hotwordPrivate API to provide the feature. |
140 // TODO(mukai): remove this after everything gets stable. | 136 // TODO(mukai): remove this after everything gets stable. |
141 Profile* profile = Profile::FromWebUI(web_ui()); | 137 Profile* profile = Profile::FromWebUI(web_ui()); |
142 ExtensionService* extension_service = | |
143 extensions::ExtensionSystem::Get(profile)->extension_service(); | |
144 if (!extension_service) | |
145 return; | |
146 | 138 |
139 extensions::ExtensionRegistry* registry = | |
140 extensions::ExtensionRegistry::Get(profile); | |
147 const extensions::Extension* hotword_extension = | 141 const extensions::Extension* hotword_extension = |
148 extension_service->GetExtensionById( | 142 registry->GetExtensionById(extension_misc::kHotwordExtensionId, |
149 extension_misc::kHotwordExtensionId, false /* include_disabled */); | 143 extensions::ExtensionRegistry::ENABLED); |
150 if (hotword_extension && hotword_extension->version()->CompareTo( | 144 if (hotword_extension && |
145 hotword_extension->version()->CompareTo( | |
151 base::Version(kOldHotwordExtensionVersionString)) <= 0) { | 146 base::Version(kOldHotwordExtensionVersionString)) <= 0) { |
152 StartPageService* service = StartPageService::Get(profile); | 147 StartPageService* service = StartPageService::Get(profile); |
153 web_ui()->CallJavascriptFunction( | 148 web_ui()->CallJavascriptFunction( |
154 "appList.startPage.setHotwordEnabled", | 149 "appList.startPage.setHotwordEnabled", |
155 base::FundamentalValue(service->HotwordEnabled())); | 150 base::FundamentalValue(service->HotwordEnabled())); |
156 } | 151 } |
157 } | 152 } |
158 #endif | 153 #endif |
159 | 154 |
160 void StartPageHandler::HandleInitialize(const base::ListValue* args) { | 155 void StartPageHandler::HandleInitialize(const base::ListValue* args) { |
161 Profile* profile = Profile::FromWebUI(web_ui()); | 156 Profile* profile = Profile::FromWebUI(web_ui()); |
162 StartPageService* service = StartPageService::Get(profile); | 157 StartPageService* service = StartPageService::Get(profile); |
163 if (!service) | 158 if (!service) |
164 return; | 159 return; |
165 | 160 |
166 recommended_apps_ = service->recommended_apps(); | 161 recommended_apps_ = service->recommended_apps(); |
167 recommended_apps_->AddObserver(this); | 162 recommended_apps_->AddObserver(this); |
168 | 163 |
169 SendRecommendedApps(); | 164 SendRecommendedApps(); |
170 | 165 |
171 #if defined(OS_CHROMEOS) | 166 #if defined(OS_CHROMEOS) |
172 if (app_list::switches::IsVoiceSearchEnabled() && | 167 if (app_list::switches::IsVoiceSearchEnabled() && |
173 HotwordService::DoesHotwordSupportLanguage(profile)) { | 168 HotwordService::DoesHotwordSupportLanguage(profile)) { |
174 OnHotwordEnabledChanged(); | 169 OnHotwordEnabledChanged(); |
175 pref_change_registrar_.Init(profile->GetPrefs()); | 170 pref_change_registrar_.Init(profile->GetPrefs()); |
176 pref_change_registrar_.Add( | 171 pref_change_registrar_.Add( |
177 prefs::kHotwordSearchEnabled, | 172 prefs::kHotwordSearchEnabled, |
178 base::Bind(&StartPageHandler::OnHotwordEnabledChanged, | 173 base::Bind(&StartPageHandler::OnHotwordEnabledChanged, |
179 base::Unretained(this))); | 174 base::Unretained(this))); |
180 registrar_.Add(this, | 175 |
181 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 176 extension_registry_observer_.Add( |
182 content::Source<Profile>(profile)); | 177 extensions::ExtensionRegistry::Get(profile)); |
183 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
184 content::Source<Profile>(profile)); | |
185 } | 178 } |
186 #endif | 179 #endif |
187 | 180 |
188 web_ui()->CallJavascriptFunction( | 181 web_ui()->CallJavascriptFunction( |
189 "appList.startPage.setNaclArch", | 182 "appList.startPage.setNaclArch", |
190 base::StringValue(chrome::OmahaQueryParams::GetNaclArch())); | 183 base::StringValue(chrome::OmahaQueryParams::GetNaclArch())); |
191 | 184 |
192 if (!app_list::switches::IsExperimentalAppListEnabled()) { | 185 if (!app_list::switches::IsExperimentalAppListEnabled()) { |
193 web_ui()->CallJavascriptFunction( | 186 web_ui()->CallJavascriptFunction( |
194 "appList.startPage.onAppListShown", | 187 "appList.startPage.onAppListShown", |
195 base::FundamentalValue(service->HotwordEnabled())); | 188 base::FundamentalValue(service->HotwordEnabled())); |
196 } | 189 } |
197 } | 190 } |
198 | 191 |
199 void StartPageHandler::HandleLaunchApp(const base::ListValue* args) { | 192 void StartPageHandler::HandleLaunchApp(const base::ListValue* args) { |
200 std::string app_id; | 193 std::string app_id; |
201 CHECK(args->GetString(0, &app_id)); | 194 CHECK(args->GetString(0, &app_id)); |
202 | 195 |
203 Profile* profile = Profile::FromWebUI(web_ui()); | 196 Profile* profile = Profile::FromWebUI(web_ui()); |
204 ExtensionService* service = | 197 const extensions::Extension* app = |
205 extensions::ExtensionSystem::Get(profile)->extension_service(); | 198 extensions::ExtensionRegistry::Get(profile) |
206 const extensions::Extension* app = service->GetInstalledExtension(app_id); | 199 ->GetExtensionById(app_id, extensions::ExtensionRegistry::EVERYTHING); |
207 if (!app) { | 200 if (!app) { |
208 NOTREACHED(); | 201 NOTREACHED(); |
209 return; | 202 return; |
210 } | 203 } |
211 | 204 |
212 AppListControllerDelegate* controller = AppListService::Get( | 205 AppListControllerDelegate* controller = AppListService::Get( |
213 chrome::GetHostDesktopTypeForNativeView( | 206 chrome::GetHostDesktopTypeForNativeView( |
214 web_ui()->GetWebContents()->GetNativeView()))-> | 207 web_ui()->GetWebContents()->GetNativeView()))-> |
215 GetControllerDelegate(); | 208 GetControllerDelegate(); |
216 controller->ActivateApp(profile, | 209 controller->ActivateApp(profile, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
255 else if (state_string == "STOPPING") | 248 else if (state_string == "STOPPING") |
256 new_state = SPEECH_RECOGNITION_STOPPING; | 249 new_state = SPEECH_RECOGNITION_STOPPING; |
257 | 250 |
258 StartPageService* service = | 251 StartPageService* service = |
259 StartPageService::Get(Profile::FromWebUI(web_ui())); | 252 StartPageService::Get(Profile::FromWebUI(web_ui())); |
260 if (service) | 253 if (service) |
261 service->OnSpeechRecognitionStateChanged(new_state); | 254 service->OnSpeechRecognitionStateChanged(new_state); |
262 } | 255 } |
263 | 256 |
264 } // namespace app_list | 257 } // namespace app_list |
OLD | NEW |