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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 sorting->CreateNextAppLaunchOrdinal(page_ordinal); | 160 sorting->CreateNextAppLaunchOrdinal(page_ordinal); |
159 sorting->SetAppLaunchOrdinal(extension->id(), app_launch_ordinal); | 161 sorting->SetAppLaunchOrdinal(extension->id(), app_launch_ordinal); |
160 } | 162 } |
161 value->SetString("app_launch_ordinal", app_launch_ordinal.ToInternalValue()); | 163 value->SetString("app_launch_ordinal", app_launch_ordinal.ToInternalValue()); |
162 } | 164 } |
163 | 165 |
164 void AppLauncherHandler::RegisterMessages() { | 166 void AppLauncherHandler::RegisterMessages() { |
165 registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_NTP, | 167 registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_NTP, |
166 content::Source<WebContents>(web_ui()->GetWebContents())); | 168 content::Source<WebContents>(web_ui()->GetWebContents())); |
167 | 169 |
| 170 // Some tests don't have a local state. |
| 171 if (g_browser_process->local_state()) { |
| 172 local_state_pref_change_registrar_.Init(g_browser_process->local_state()); |
| 173 local_state_pref_change_registrar_.Add( |
| 174 apps::prefs::kShowAppLauncherPromo, |
| 175 base::Bind(&AppLauncherHandler::OnLocalStatePreferenceChanged, |
| 176 base::Unretained(this))); |
| 177 } |
168 web_ui()->RegisterMessageCallback("getApps", | 178 web_ui()->RegisterMessageCallback("getApps", |
169 base::Bind(&AppLauncherHandler::HandleGetApps, | 179 base::Bind(&AppLauncherHandler::HandleGetApps, |
170 base::Unretained(this))); | 180 base::Unretained(this))); |
171 web_ui()->RegisterMessageCallback("launchApp", | 181 web_ui()->RegisterMessageCallback("launchApp", |
172 base::Bind(&AppLauncherHandler::HandleLaunchApp, | 182 base::Bind(&AppLauncherHandler::HandleLaunchApp, |
173 base::Unretained(this))); | 183 base::Unretained(this))); |
174 web_ui()->RegisterMessageCallback("setLaunchType", | 184 web_ui()->RegisterMessageCallback("setLaunchType", |
175 base::Bind(&AppLauncherHandler::HandleSetLaunchType, | 185 base::Bind(&AppLauncherHandler::HandleSetLaunchType, |
176 base::Unretained(this))); | 186 base::Unretained(this))); |
177 web_ui()->RegisterMessageCallback("uninstallApp", | 187 web_ui()->RegisterMessageCallback("uninstallApp", |
(...skipping 10 matching lines...) Expand all Loading... |
188 base::Unretained(this))); | 198 base::Unretained(this))); |
189 web_ui()->RegisterMessageCallback("saveAppPageName", | 199 web_ui()->RegisterMessageCallback("saveAppPageName", |
190 base::Bind(&AppLauncherHandler::HandleSaveAppPageName, | 200 base::Bind(&AppLauncherHandler::HandleSaveAppPageName, |
191 base::Unretained(this))); | 201 base::Unretained(this))); |
192 web_ui()->RegisterMessageCallback("generateAppForLink", | 202 web_ui()->RegisterMessageCallback("generateAppForLink", |
193 base::Bind(&AppLauncherHandler::HandleGenerateAppForLink, | 203 base::Bind(&AppLauncherHandler::HandleGenerateAppForLink, |
194 base::Unretained(this))); | 204 base::Unretained(this))); |
195 web_ui()->RegisterMessageCallback("recordAppLaunchByURL", | 205 web_ui()->RegisterMessageCallback("recordAppLaunchByURL", |
196 base::Bind(&AppLauncherHandler::HandleRecordAppLaunchByUrl, | 206 base::Bind(&AppLauncherHandler::HandleRecordAppLaunchByUrl, |
197 base::Unretained(this))); | 207 base::Unretained(this))); |
| 208 web_ui()->RegisterMessageCallback("stopShowingAppLauncherPromo", |
| 209 base::Bind(&AppLauncherHandler::StopShowingAppLauncherPromo, |
| 210 base::Unretained(this))); |
198 } | 211 } |
199 | 212 |
200 void AppLauncherHandler::Observe(int type, | 213 void AppLauncherHandler::Observe(int type, |
201 const content::NotificationSource& source, | 214 const content::NotificationSource& source, |
202 const content::NotificationDetails& details) { | 215 const content::NotificationDetails& details) { |
203 if (type == chrome::NOTIFICATION_APP_INSTALLED_TO_NTP) { | 216 if (type == chrome::NOTIFICATION_APP_INSTALLED_TO_NTP) { |
204 highlight_app_id_ = *content::Details<const std::string>(details).ptr(); | 217 highlight_app_id_ = *content::Details<const std::string>(details).ptr(); |
205 if (has_loaded_apps_) | 218 if (has_loaded_apps_) |
206 SetAppToBeHighlighted(); | 219 SetAppToBeHighlighted(); |
207 return; | 220 return; |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 } | 398 } |
386 | 399 |
387 SetAppToBeHighlighted(); | 400 SetAppToBeHighlighted(); |
388 FillAppDictionary(&dictionary); | 401 FillAppDictionary(&dictionary); |
389 web_ui()->CallJavascriptFunction("ntp.getAppsCallback", dictionary); | 402 web_ui()->CallJavascriptFunction("ntp.getAppsCallback", dictionary); |
390 | 403 |
391 // First time we get here we set up the observer so that we can tell update | 404 // First time we get here we set up the observer so that we can tell update |
392 // the apps as they change. | 405 // the apps as they change. |
393 if (!has_loaded_apps_) { | 406 if (!has_loaded_apps_) { |
394 base::Closure callback = base::Bind( | 407 base::Closure callback = base::Bind( |
395 &AppLauncherHandler::OnPreferenceChanged, | 408 &AppLauncherHandler::OnExtensionPreferenceChanged, |
396 base::Unretained(this)); | 409 base::Unretained(this)); |
397 pref_change_registrar_.Init( | 410 extension_pref_change_registrar_.Init( |
398 extension_service_->extension_prefs()->pref_service()); | 411 extension_service_->extension_prefs()->pref_service()); |
399 pref_change_registrar_.Add(ExtensionPrefs::kExtensionsPref, callback); | 412 extension_pref_change_registrar_.Add(ExtensionPrefs::kExtensionsPref, |
400 pref_change_registrar_.Add(prefs::kNtpAppPageNames, callback); | 413 callback); |
| 414 extension_pref_change_registrar_.Add(prefs::kNtpAppPageNames, callback); |
401 | 415 |
402 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 416 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
403 content::Source<Profile>(profile)); | 417 content::Source<Profile>(profile)); |
404 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 418 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
405 content::Source<Profile>(profile)); | 419 content::Source<Profile>(profile)); |
406 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, | 420 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, |
407 content::Source<ExtensionSorting>( | 421 content::Source<ExtensionSorting>( |
408 extension_service_->extension_prefs()->extension_sorting())); | 422 extension_service_->extension_prefs()->extension_sorting())); |
409 registrar_.Add(this, chrome::NOTIFICATION_WEB_STORE_PROMO_LOADED, | 423 registrar_.Add(this, chrome::NOTIFICATION_WEB_STORE_PROMO_LOADED, |
410 content::Source<Profile>(profile)); | 424 content::Source<Profile>(profile)); |
(...skipping 244 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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
811 ExtensionUninstallDialog* AppLauncherHandler::GetExtensionUninstallDialog() { | 838 ExtensionUninstallDialog* AppLauncherHandler::GetExtensionUninstallDialog() { |
812 if (!extension_uninstall_dialog_.get()) { | 839 if (!extension_uninstall_dialog_.get()) { |
813 Browser* browser = chrome::FindBrowserWithWebContents( | 840 Browser* browser = chrome::FindBrowserWithWebContents( |
814 web_ui()->GetWebContents()); | 841 web_ui()->GetWebContents()); |
815 extension_uninstall_dialog_.reset( | 842 extension_uninstall_dialog_.reset( |
816 ExtensionUninstallDialog::Create(extension_service_->profile(), | 843 ExtensionUninstallDialog::Create(extension_service_->profile(), |
817 browser, this)); | 844 browser, this)); |
818 } | 845 } |
819 return extension_uninstall_dialog_.get(); | 846 return extension_uninstall_dialog_.get(); |
820 } | 847 } |
OLD | NEW |