| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/webui/options2/media_galleries_handler.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "chrome/browser/media_gallery/media_galleries_preferences.h" | |
| 9 #include "chrome/browser/media_gallery/media_galleries_preferences_factory.h" | |
| 10 #include "chrome/browser/prefs/pref_service.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "chrome/browser/ui/chrome_select_file_policy.h" | |
| 13 #include "chrome/common/chrome_notification_types.h" | |
| 14 #include "chrome/common/pref_names.h" | |
| 15 #include "content/public/browser/notification_details.h" | |
| 16 #include "content/public/browser/web_contents.h" | |
| 17 #include "content/public/browser/web_contents_view.h" | |
| 18 #include "content/public/browser/web_ui.h" | |
| 19 #include "grit/generated_resources.h" | |
| 20 | |
| 21 namespace options { | |
| 22 | |
| 23 MediaGalleriesHandler::MediaGalleriesHandler() { | |
| 24 } | |
| 25 | |
| 26 MediaGalleriesHandler::~MediaGalleriesHandler() { | |
| 27 } | |
| 28 | |
| 29 void MediaGalleriesHandler::GetLocalizedValues(DictionaryValue* values) { | |
| 30 DCHECK(values); | |
| 31 | |
| 32 static OptionsStringResource resources[] = { | |
| 33 { "mediaGalleriesSectionLabel", IDS_MEDIA_GALLERY_SECTION_LABEL }, | |
| 34 { "manageGalleriesButton", IDS_MEDIA_GALLERY_MANAGE_BUTTON }, | |
| 35 { "addNewGalleryButton", IDS_MEDIA_GALLERY_ADD_NEW_BUTTON }, | |
| 36 }; | |
| 37 | |
| 38 RegisterStrings(values, resources, arraysize(resources)); | |
| 39 RegisterTitle(values, "manageMediaGalleries", | |
| 40 IDS_MEDIA_GALLERY_MANAGE_TITLE); | |
| 41 } | |
| 42 | |
| 43 void MediaGalleriesHandler::InitializeHandler() { | |
| 44 if (!chrome::MediaGalleriesPreferences::UserInteractionIsEnabled()) | |
| 45 return; | |
| 46 | |
| 47 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 48 pref_change_registrar_.Init(profile->GetPrefs()); | |
| 49 pref_change_registrar_.Add(prefs::kMediaGalleriesRememberedGalleries, this); | |
| 50 } | |
| 51 | |
| 52 void MediaGalleriesHandler::InitializePage() { | |
| 53 if (!chrome::MediaGalleriesPreferences::UserInteractionIsEnabled()) | |
| 54 return; | |
| 55 | |
| 56 OnGalleriesChanged(); | |
| 57 } | |
| 58 | |
| 59 void MediaGalleriesHandler::RegisterMessages() { | |
| 60 web_ui()->RegisterMessageCallback( | |
| 61 "addNewGallery", | |
| 62 base::Bind(&MediaGalleriesHandler::HandleAddNewGallery, | |
| 63 base::Unretained(this))); | |
| 64 web_ui()->RegisterMessageCallback( | |
| 65 "forgetGallery", | |
| 66 base::Bind(&MediaGalleriesHandler::HandleForgetGallery, | |
| 67 base::Unretained(this))); | |
| 68 } | |
| 69 | |
| 70 void MediaGalleriesHandler::OnGalleriesChanged() { | |
| 71 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 72 const ListValue* list = profile->GetPrefs()->GetList( | |
| 73 prefs::kMediaGalleriesRememberedGalleries); | |
| 74 web_ui()->CallJavascriptFunction( | |
| 75 "options.MediaGalleriesManager.setAvailableMediaGalleries", *list); | |
| 76 } | |
| 77 | |
| 78 void MediaGalleriesHandler::HandleAddNewGallery(const base::ListValue* args) { | |
| 79 ui::SelectFileDialog* dialog = ui::SelectFileDialog::Create( | |
| 80 this, | |
| 81 new ChromeSelectFilePolicy(web_ui()->GetWebContents())); | |
| 82 dialog->SelectFile(ui::SelectFileDialog::SELECT_FOLDER, | |
| 83 string16(), // TODO(estade): a name for the dialog? | |
| 84 FilePath(), | |
| 85 NULL, 0, | |
| 86 FilePath::StringType(), | |
| 87 web_ui()->GetWebContents()->GetView()-> | |
| 88 GetTopLevelNativeWindow(), | |
| 89 NULL); | |
| 90 } | |
| 91 | |
| 92 void MediaGalleriesHandler::HandleForgetGallery(const base::ListValue* args) { | |
| 93 // TODO(estade): use uint64. | |
| 94 int id; | |
| 95 CHECK(ExtractIntegerValue(args, &id)); | |
| 96 chrome::MediaGalleriesPreferences* prefs = | |
| 97 MediaGalleriesPreferencesFactory::GetForProfile( | |
| 98 Profile::FromWebUI(web_ui())); | |
| 99 prefs->ForgetGalleryById(id); | |
| 100 } | |
| 101 | |
| 102 void MediaGalleriesHandler::FileSelected( | |
| 103 const FilePath& path, int index, void* params) { | |
| 104 chrome::MediaGalleriesPreferences* prefs = | |
| 105 MediaGalleriesPreferencesFactory::GetForProfile( | |
| 106 Profile::FromWebUI(web_ui())); | |
| 107 prefs->AddGalleryByPath(path); | |
| 108 } | |
| 109 | |
| 110 void MediaGalleriesHandler::Observe( | |
| 111 int type, | |
| 112 const content::NotificationSource& source, | |
| 113 const content::NotificationDetails& details) { | |
| 114 if (type == chrome::NOTIFICATION_PREF_CHANGED && | |
| 115 *content::Details<std::string>(details).ptr() == | |
| 116 prefs::kMediaGalleriesRememberedGalleries) { | |
| 117 OnGalleriesChanged(); | |
| 118 } else { | |
| 119 NOTREACHED(); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 } // namespace options | |
| OLD | NEW |