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_gallery_handler.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "chrome/browser/media_gallery/media_gallery_registry.h" | |
9 #include "chrome/browser/media_gallery/media_gallery_registry_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 options2 { | |
22 | |
23 MediaGalleryHandler::MediaGalleryHandler() { | |
24 } | |
25 | |
26 MediaGalleryHandler::~MediaGalleryHandler() { | |
27 } | |
28 | |
29 void MediaGalleryHandler::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 MediaGalleryHandler::InitializeHandler() { | |
44 if (!MediaGalleryRegistry::UserInteractionIsEnabled()) | |
45 return; | |
46 | |
47 Profile* profile = Profile::FromWebUI(web_ui()); | |
48 pref_change_registrar_.Init(profile->GetPrefs()); | |
49 pref_change_registrar_.Add(prefs::kMediaGalleryRememberedGalleries, this); | |
50 } | |
51 | |
52 void MediaGalleryHandler::InitializePage() { | |
53 if (!MediaGalleryRegistry::UserInteractionIsEnabled()) | |
54 return; | |
55 | |
56 OnGalleriesChanged(); | |
57 } | |
58 | |
59 void MediaGalleryHandler::RegisterMessages() { | |
60 web_ui()->RegisterMessageCallback( | |
61 "addNewGallery", | |
62 base::Bind(&MediaGalleryHandler::HandleAddNewGallery, | |
63 base::Unretained(this))); | |
64 web_ui()->RegisterMessageCallback( | |
65 "forgetGallery", | |
66 base::Bind(&MediaGalleryHandler::HandleForgetGallery, | |
67 base::Unretained(this))); | |
68 } | |
69 | |
70 void MediaGalleryHandler::OnGalleriesChanged() { | |
71 Profile* profile = Profile::FromWebUI(web_ui()); | |
72 const ListValue* list = profile->GetPrefs()->GetList( | |
73 prefs::kMediaGalleryRememberedGalleries); | |
74 web_ui()->CallJavascriptFunction( | |
75 "options.MediaGalleryManager.setAvailableMediaGalleries", *list); | |
76 } | |
77 | |
78 void MediaGalleryHandler::HandleAddNewGallery(const base::ListValue* args) { | |
79 SelectFileDialog* dialog = SelectFileDialog::Create( | |
80 this, | |
81 new ChromeSelectFilePolicy(web_ui()->GetWebContents())); | |
82 dialog->SelectFile(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 MediaGalleryHandler::HandleForgetGallery(const base::ListValue* args) { | |
93 // TODO(estade): use uint64. | |
94 int id; | |
95 CHECK(ExtractIntegerValue(args, &id)); | |
96 MediaGalleryRegistry* registry = | |
97 MediaGalleryRegistryFactory::GetForProfile(Profile::FromWebUI(web_ui())); | |
98 registry->ForgetGalleryById(id); | |
99 } | |
100 | |
101 void MediaGalleryHandler::FileSelected( | |
102 const FilePath& path, int index, void* params) { | |
103 MediaGalleryRegistry* registry = | |
104 MediaGalleryRegistryFactory::GetForProfile(Profile::FromWebUI(web_ui())); | |
105 registry->AddGalleryByPath(path); | |
106 } | |
107 | |
108 void MediaGalleryHandler::Observe( | |
109 int type, | |
110 const content::NotificationSource& source, | |
111 const content::NotificationDetails& details) { | |
112 if (type == chrome::NOTIFICATION_PREF_CHANGED && | |
113 *content::Details<std::string>(details).ptr() == | |
114 prefs::kMediaGalleryRememberedGalleries) { | |
115 OnGalleriesChanged(); | |
116 } else { | |
117 NOTREACHED(); | |
118 } | |
119 } | |
120 | |
121 } // namespace options2 | |
OLD | NEW |