OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/ntp/app_launcher_handler.h" | 5 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
| 9 #include "apps/pref_names.h" |
9 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
12 #include "base/i18n/rtl.h" | 13 #include "base/i18n/rtl.h" |
13 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
14 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
15 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
16 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
17 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/extensions/crx_installer.h" | 20 #include "chrome/browser/extensions/crx_installer.h" |
19 #include "chrome/browser/extensions/extension_prefs.h" | 21 #include "chrome/browser/extensions/extension_prefs.h" |
20 #include "chrome/browser/extensions/extension_service.h" | 22 #include "chrome/browser/extensions/extension_service.h" |
21 #include "chrome/browser/extensions/extension_sorting.h" | 23 #include "chrome/browser/extensions/extension_sorting.h" |
22 #include "chrome/browser/extensions/extension_system.h" | 24 #include "chrome/browser/extensions/extension_system.h" |
23 #include "chrome/browser/extensions/management_policy.h" | 25 #include "chrome/browser/extensions/management_policy.h" |
24 #include "chrome/browser/favicon/favicon_service_factory.h" | 26 #include "chrome/browser/favicon/favicon_service_factory.h" |
25 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 27 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
26 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
27 #include "chrome/browser/ui/browser_finder.h" | 29 #include "chrome/browser/ui/browser_finder.h" |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 sorting->CreateNextAppLaunchOrdinal(page_ordinal); | 159 sorting->CreateNextAppLaunchOrdinal(page_ordinal); |
158 sorting->SetAppLaunchOrdinal(extension->id(), app_launch_ordinal); | 160 sorting->SetAppLaunchOrdinal(extension->id(), app_launch_ordinal); |
159 } | 161 } |
160 value->SetString("app_launch_ordinal", app_launch_ordinal.ToInternalValue()); | 162 value->SetString("app_launch_ordinal", app_launch_ordinal.ToInternalValue()); |
161 } | 163 } |
162 | 164 |
163 void AppLauncherHandler::RegisterMessages() { | 165 void AppLauncherHandler::RegisterMessages() { |
164 registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_NTP, | 166 registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_NTP, |
165 content::Source<WebContents>(web_ui()->GetWebContents())); | 167 content::Source<WebContents>(web_ui()->GetWebContents())); |
166 | 168 |
| 169 // Some tests don't have a local state. |
| 170 if (g_browser_process->local_state()) { |
| 171 local_state_pref_change_registrar_.Init(g_browser_process->local_state()); |
| 172 local_state_pref_change_registrar_.Add( |
| 173 apps::prefs::kShowAppLauncherPromo, |
| 174 base::Bind(&AppLauncherHandler::OnLocalStatePreferenceChanged, |
| 175 base::Unretained(this))); |
| 176 } |
167 web_ui()->RegisterMessageCallback("getApps", | 177 web_ui()->RegisterMessageCallback("getApps", |
168 base::Bind(&AppLauncherHandler::HandleGetApps, | 178 base::Bind(&AppLauncherHandler::HandleGetApps, |
169 base::Unretained(this))); | 179 base::Unretained(this))); |
170 web_ui()->RegisterMessageCallback("launchApp", | 180 web_ui()->RegisterMessageCallback("launchApp", |
171 base::Bind(&AppLauncherHandler::HandleLaunchApp, | 181 base::Bind(&AppLauncherHandler::HandleLaunchApp, |
172 base::Unretained(this))); | 182 base::Unretained(this))); |
173 web_ui()->RegisterMessageCallback("setLaunchType", | 183 web_ui()->RegisterMessageCallback("setLaunchType", |
174 base::Bind(&AppLauncherHandler::HandleSetLaunchType, | 184 base::Bind(&AppLauncherHandler::HandleSetLaunchType, |
175 base::Unretained(this))); | 185 base::Unretained(this))); |
176 web_ui()->RegisterMessageCallback("uninstallApp", | 186 web_ui()->RegisterMessageCallback("uninstallApp", |
(...skipping 10 matching lines...) Expand all Loading... |
187 base::Unretained(this))); | 197 base::Unretained(this))); |
188 web_ui()->RegisterMessageCallback("saveAppPageName", | 198 web_ui()->RegisterMessageCallback("saveAppPageName", |
189 base::Bind(&AppLauncherHandler::HandleSaveAppPageName, | 199 base::Bind(&AppLauncherHandler::HandleSaveAppPageName, |
190 base::Unretained(this))); | 200 base::Unretained(this))); |
191 web_ui()->RegisterMessageCallback("generateAppForLink", | 201 web_ui()->RegisterMessageCallback("generateAppForLink", |
192 base::Bind(&AppLauncherHandler::HandleGenerateAppForLink, | 202 base::Bind(&AppLauncherHandler::HandleGenerateAppForLink, |
193 base::Unretained(this))); | 203 base::Unretained(this))); |
194 web_ui()->RegisterMessageCallback("recordAppLaunchByURL", | 204 web_ui()->RegisterMessageCallback("recordAppLaunchByURL", |
195 base::Bind(&AppLauncherHandler::HandleRecordAppLaunchByUrl, | 205 base::Bind(&AppLauncherHandler::HandleRecordAppLaunchByUrl, |
196 base::Unretained(this))); | 206 base::Unretained(this))); |
| 207 web_ui()->RegisterMessageCallback("stopShowingAppLauncherPromo", |
| 208 base::Bind(&AppLauncherHandler::StopShowingAppLauncherPromo, |
| 209 base::Unretained(this))); |
197 } | 210 } |
198 | 211 |
199 void AppLauncherHandler::Observe(int type, | 212 void AppLauncherHandler::Observe(int type, |
200 const content::NotificationSource& source, | 213 const content::NotificationSource& source, |
201 const content::NotificationDetails& details) { | 214 const content::NotificationDetails& details) { |
202 if (type == chrome::NOTIFICATION_APP_INSTALLED_TO_NTP) { | 215 if (type == chrome::NOTIFICATION_APP_INSTALLED_TO_NTP) { |
203 highlight_app_id_ = *content::Details<const std::string>(details).ptr(); | 216 highlight_app_id_ = *content::Details<const std::string>(details).ptr(); |
204 if (has_loaded_apps_) | 217 if (has_loaded_apps_) |
205 SetAppToBeHighlighted(); | 218 SetAppToBeHighlighted(); |
206 return; | 219 return; |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 } | 397 } |
385 | 398 |
386 SetAppToBeHighlighted(); | 399 SetAppToBeHighlighted(); |
387 FillAppDictionary(&dictionary); | 400 FillAppDictionary(&dictionary); |
388 web_ui()->CallJavascriptFunction("ntp.getAppsCallback", dictionary); | 401 web_ui()->CallJavascriptFunction("ntp.getAppsCallback", dictionary); |
389 | 402 |
390 // First time we get here we set up the observer so that we can tell update | 403 // First time we get here we set up the observer so that we can tell update |
391 // the apps as they change. | 404 // the apps as they change. |
392 if (!has_loaded_apps_) { | 405 if (!has_loaded_apps_) { |
393 base::Closure callback = base::Bind( | 406 base::Closure callback = base::Bind( |
394 &AppLauncherHandler::OnPreferenceChanged, | 407 &AppLauncherHandler::OnExtensionPreferenceChanged, |
395 base::Unretained(this)); | 408 base::Unretained(this)); |
396 pref_change_registrar_.Init( | 409 extension_pref_change_registrar_.Init( |
397 extension_service_->extension_prefs()->pref_service()); | 410 extension_service_->extension_prefs()->pref_service()); |
398 pref_change_registrar_.Add(ExtensionPrefs::kExtensionsPref, callback); | 411 extension_pref_change_registrar_.Add(ExtensionPrefs::kExtensionsPref, |
399 pref_change_registrar_.Add(prefs::kNtpAppPageNames, callback); | 412 callback); |
| 413 extension_pref_change_registrar_.Add(prefs::kNtpAppPageNames, callback); |
400 | 414 |
401 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 415 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
402 content::Source<Profile>(profile)); | 416 content::Source<Profile>(profile)); |
403 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 417 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
404 content::Source<Profile>(profile)); | 418 content::Source<Profile>(profile)); |
405 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, | 419 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, |
406 content::Source<ExtensionSorting>( | 420 content::Source<ExtensionSorting>( |
407 extension_service_->extension_prefs()->extension_sorting())); | 421 extension_service_->extension_prefs()->extension_sorting())); |
408 registrar_.Add(this, chrome::NOTIFICATION_WEB_STORE_PROMO_LOADED, | 422 registrar_.Add(this, chrome::NOTIFICATION_WEB_STORE_PROMO_LOADED, |
409 content::Source<Profile>(profile)); | 423 content::Source<Profile>(profile)); |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 double source; | 669 double source; |
656 CHECK(args->GetDouble(1, &source)); | 670 CHECK(args->GetDouble(1, &source)); |
657 | 671 |
658 extension_misc::AppLaunchBucket bucket = | 672 extension_misc::AppLaunchBucket bucket = |
659 static_cast<extension_misc::AppLaunchBucket>(static_cast<int>(source)); | 673 static_cast<extension_misc::AppLaunchBucket>(static_cast<int>(source)); |
660 CHECK(source < extension_misc::APP_LAUNCH_BUCKET_BOUNDARY); | 674 CHECK(source < extension_misc::APP_LAUNCH_BUCKET_BOUNDARY); |
661 | 675 |
662 RecordAppLaunchByUrl(Profile::FromWebUI(web_ui()), url, bucket); | 676 RecordAppLaunchByUrl(Profile::FromWebUI(web_ui()), url, bucket); |
663 } | 677 } |
664 | 678 |
| 679 void AppLauncherHandler::StopShowingAppLauncherPromo( |
| 680 const base::ListValue* args) { |
| 681 g_browser_process->local_state()->SetBoolean( |
| 682 apps::prefs::kShowAppLauncherPromo, false); |
| 683 } |
| 684 |
665 void AppLauncherHandler::OnFaviconForApp( | 685 void AppLauncherHandler::OnFaviconForApp( |
666 scoped_ptr<AppInstallInfo> install_info, | 686 scoped_ptr<AppInstallInfo> install_info, |
667 const history::FaviconImageResult& image_result) { | 687 const history::FaviconImageResult& image_result) { |
668 scoped_ptr<WebApplicationInfo> web_app(new WebApplicationInfo()); | 688 scoped_ptr<WebApplicationInfo> web_app(new WebApplicationInfo()); |
669 web_app->is_bookmark_app = install_info->is_bookmark_app; | 689 web_app->is_bookmark_app = install_info->is_bookmark_app; |
670 web_app->title = install_info->title; | 690 web_app->title = install_info->title; |
671 web_app->app_url = install_info->app_url; | 691 web_app->app_url = install_info->app_url; |
672 web_app->urls.push_back(install_info->app_url); | 692 web_app->urls.push_back(install_info->app_url); |
673 | 693 |
674 if (!image_result.image.IsEmpty()) { | 694 if (!image_result.image.IsEmpty()) { |
(...skipping 14 matching lines...) Expand all Loading... |
689 | 709 |
690 void AppLauncherHandler::SetAppToBeHighlighted() { | 710 void AppLauncherHandler::SetAppToBeHighlighted() { |
691 if (highlight_app_id_.empty()) | 711 if (highlight_app_id_.empty()) |
692 return; | 712 return; |
693 | 713 |
694 StringValue app_id(highlight_app_id_); | 714 StringValue app_id(highlight_app_id_); |
695 web_ui()->CallJavascriptFunction("ntp.setAppToBeHighlighted", app_id); | 715 web_ui()->CallJavascriptFunction("ntp.setAppToBeHighlighted", app_id); |
696 highlight_app_id_.clear(); | 716 highlight_app_id_.clear(); |
697 } | 717 } |
698 | 718 |
699 void AppLauncherHandler::OnPreferenceChanged() { | 719 void AppLauncherHandler::OnExtensionPreferenceChanged() { |
700 DictionaryValue dictionary; | 720 DictionaryValue dictionary; |
701 FillAppDictionary(&dictionary); | 721 FillAppDictionary(&dictionary); |
702 web_ui()->CallJavascriptFunction("ntp.appsPrefChangeCallback", dictionary); | 722 web_ui()->CallJavascriptFunction("ntp.appsPrefChangeCallback", dictionary); |
703 } | 723 } |
704 | 724 |
| 725 void AppLauncherHandler::OnLocalStatePreferenceChanged() { |
| 726 web_ui()->CallJavascriptFunction( |
| 727 "ntp.appLauncherPromoPrefChangeCallback", |
| 728 base::FundamentalValue(g_browser_process->local_state()->GetBoolean( |
| 729 apps::prefs::kShowAppLauncherPromo))); |
| 730 } |
| 731 |
705 // static | 732 // static |
706 void AppLauncherHandler::RegisterUserPrefs(PrefRegistrySyncable* registry) { | 733 void AppLauncherHandler::RegisterUserPrefs(PrefRegistrySyncable* registry) { |
707 registry->RegisterListPref(prefs::kNtpAppPageNames, | 734 registry->RegisterListPref(prefs::kNtpAppPageNames, |
708 PrefRegistrySyncable::SYNCABLE_PREF); | 735 PrefRegistrySyncable::SYNCABLE_PREF); |
709 } | 736 } |
710 | 737 |
711 void AppLauncherHandler::CleanupAfterUninstall() { | 738 void AppLauncherHandler::CleanupAfterUninstall() { |
712 extension_id_prompting_.clear(); | 739 extension_id_prompting_.clear(); |
713 } | 740 } |
714 | 741 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
810 ExtensionUninstallDialog* AppLauncherHandler::GetExtensionUninstallDialog() { | 837 ExtensionUninstallDialog* AppLauncherHandler::GetExtensionUninstallDialog() { |
811 if (!extension_uninstall_dialog_.get()) { | 838 if (!extension_uninstall_dialog_.get()) { |
812 Browser* browser = chrome::FindBrowserWithWebContents( | 839 Browser* browser = chrome::FindBrowserWithWebContents( |
813 web_ui()->GetWebContents()); | 840 web_ui()->GetWebContents()); |
814 extension_uninstall_dialog_.reset( | 841 extension_uninstall_dialog_.reset( |
815 ExtensionUninstallDialog::Create(extension_service_->profile(), | 842 ExtensionUninstallDialog::Create(extension_service_->profile(), |
816 browser, this)); | 843 browser, this)); |
817 } | 844 } |
818 return extension_uninstall_dialog_.get(); | 845 return extension_uninstall_dialog_.get(); |
819 } | 846 } |
OLD | NEW |