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/media_gallery/media_galleries_dialog_controller.h" | |
6 | |
7 #include "base/path_service.h" | |
8 #include "base/utf_string_conversions.h" | |
9 #include "chrome/browser/media_gallery/media_galleries_preferences_factory.h" | |
10 #include "chrome/browser/ui/chrome_select_file_policy.h" | |
11 #include "chrome/browser/ui/tab_contents/tab_contents.h" | |
12 #include "chrome/common/chrome_paths.h" | |
13 #include "chrome/common/extensions/extension.h" | |
14 #include "content/public/browser/web_contents.h" | |
15 #include "content/public/browser/web_contents_view.h" | |
16 #include "grit/generated_resources.h" | |
17 #include "ui/base/l10n/l10n_util.h" | |
18 | |
19 using extensions::Extension; | |
20 | |
21 namespace chrome { | |
22 | |
23 MediaGalleriesDialogController::MediaGalleriesDialogController( | |
24 TabContents* tab_contents, | |
25 const Extension* extension, | |
26 MediaGalleriesDialogDelegate* delegate) | |
27 : tab_contents_(tab_contents), | |
28 extension_(extension), | |
29 delegate_(delegate), | |
30 preferences_(NULL) { | |
31 LookUpPermissions(); | |
32 dialog_.reset(MediaGalleriesDialog::Create(this)); | |
33 } | |
34 | |
35 MediaGalleriesDialogController::~MediaGalleriesDialogController() { | |
36 if (select_folder_dialog_.get()) | |
37 select_folder_dialog_->ListenerDestroyed(); | |
38 } | |
39 | |
40 string16 MediaGalleriesDialogController::GetHeader() { | |
41 return l10n_util::GetStringFUTF16(IDS_MEDIA_GALLERIES_DIALOG_HEADER, | |
42 UTF8ToUTF16(extension_->name())); | |
43 } | |
44 | |
45 string16 MediaGalleriesDialogController::GetSubtext() { | |
46 if (extension_->HasAPIPermission( | |
47 extensions::APIPermission::kMediaGalleriesRead)) { | |
48 return l10n_util::GetStringFUTF16(IDS_MEDIA_GALLERIES_DIALOG_READ_SUBTEXT, | |
49 UTF8ToUTF16(extension_->name())); | |
50 } | |
51 // TODO(estade): handle write et al. | |
52 return string16(); | |
53 } | |
54 | |
55 void MediaGalleriesDialogController::OnAddFolderClicked() { | |
56 FilePath user_data_dir; | |
57 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | |
58 // TODO(estade): if file dialogs are disabled we need to handle it somehow. | |
59 select_folder_dialog_ = | |
60 ui::SelectFileDialog::Create(this, new ChromeSelectFilePolicy(NULL)); | |
61 select_folder_dialog_->SelectFile( | |
62 ui::SelectFileDialog::SELECT_FOLDER, | |
63 l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_DIALOG_ADD_GALLERY_TITLE), | |
64 user_data_dir, | |
65 NULL, 0, FILE_PATH_LITERAL(""), | |
66 tab_contents_->web_contents()->GetView()->GetTopLevelNativeWindow(), | |
67 NULL); | |
68 } | |
69 | |
70 void MediaGalleriesDialogController::GalleryToggled( | |
71 const MediaGalleryPrefInfo* gallery, | |
72 bool enabled) { | |
73 // Check known galleries. | |
74 if (gallery->pref_id != 0 /*MediaGalleriesPreferences::kInvalidId */) { | |
75 known_galleries_[gallery->pref_id].second = enabled; | |
76 return; | |
77 } | |
78 | |
79 // Check new galleries. | |
vandebo (ex-Chrome)
2012/08/06 20:39:30
You could locally assign ids to new galleries, cou
Evan Stade
2012/08/06 23:43:02
I don't think that would help very much, especiall
vandebo (ex-Chrome)
2012/08/08 19:24:21
On second thought, fragmenting the id space is pro
Evan Stade
2012/08/08 21:48:57
I'm not sure how that would work. known_gallery_ i
| |
80 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); | |
81 iter != new_galleries_.end(); ++iter) { | |
82 if (&iter->first == gallery) { | |
83 iter->second = enabled; | |
84 return; | |
85 } | |
86 } | |
87 | |
88 NOTREACHED(); | |
89 } | |
90 | |
91 void MediaGalleriesDialogController::DialogFinished(bool accepted) { | |
92 if (accepted) | |
93 SavePermissions(); | |
94 | |
95 delegate_->MediaGalleriesDialogFinished(); | |
96 delete this; | |
97 } | |
98 | |
99 void MediaGalleriesDialogController::FileSelected(const FilePath& path, | |
100 int index, | |
101 void* params) { | |
102 // Try to find it in |known_galleries_|. | |
103 MediaGalleryPrefInfo gallery; | |
104 if (preferences_->LookUpGalleryByPath(path, &gallery)) { | |
105 KnownGalleryPermissions::iterator iter = | |
106 known_galleries_.find(gallery.pref_id); | |
107 | |
108 if (iter == known_galleries_.end()) { | |
109 // This is rare, but could happen if a gallery was not "known" | |
110 // when the controller first initialized, but has since been added. | |
111 known_galleries_[gallery.pref_id] = std::make_pair(gallery, true); | |
112 iter = known_galleries_.find(gallery.pref_id); | |
113 } | |
114 | |
115 dialog_->UpdateGallery(&iter->second.first, true); | |
116 return; | |
117 } | |
118 | |
119 // Try to find it in |new_galleries_| (user added same folder twice). | |
120 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); | |
121 iter != new_galleries_.end(); ++iter) { | |
122 if (iter->first.path == gallery.path && | |
123 iter->first.device_id == gallery.device_id) { | |
124 iter->second = true; | |
125 dialog_->UpdateGallery(&iter->first, true); | |
126 return; | |
127 } | |
128 } | |
129 | |
130 // Lastly, add it to |new_galleries_|. | |
131 new_galleries_.push_back(std::make_pair(gallery, true)); | |
132 dialog_->UpdateGallery(&new_galleries_.back().first, true); | |
133 } | |
134 | |
135 void MediaGalleriesDialogController::LookUpPermissions() { | |
136 DCHECK(!preferences_); | |
137 preferences_ = | |
vandebo (ex-Chrome)
2012/08/06 20:39:30
Move this bit to the constructor? Here it's not o
Evan Stade
2012/08/06 23:43:02
Done.
| |
138 MediaGalleriesPreferencesFactory::GetForProfile(tab_contents_->profile()); | |
139 for (MediaGalleriesPrefInfoMap::const_iterator iter = | |
140 preferences_->known_galleries().begin(); | |
141 iter != preferences_->known_galleries().end(); | |
142 ++iter) { | |
143 known_galleries_[iter->first] = std::make_pair(iter->second, false); | |
144 } | |
145 | |
146 std::vector<MediaGalleryPrefId> permitted = | |
147 preferences_->GalleriesForExtension(*extension_); | |
148 | |
149 for (std::vector<MediaGalleryPrefId>::iterator iter = permitted.begin(); | |
150 iter != permitted.end(); ++iter) { | |
151 known_galleries_[*iter].second = true; | |
152 } | |
153 } | |
154 | |
155 void MediaGalleriesDialogController::SavePermissions() { | |
156 for (KnownGalleryPermissions::iterator iter = known_galleries_.begin(); | |
157 iter != known_galleries_.end(); ++iter) { | |
158 preferences_->SetGalleryPermissionForExtension( | |
159 *extension_, iter->first, iter->second.second); | |
160 } | |
161 | |
162 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); | |
163 iter != new_galleries_.end(); ++iter) { | |
164 // If the user added a gallery then unchecked it, forget about it. | |
165 if (!iter->second) | |
166 continue; | |
167 | |
168 MediaGalleryPrefInfo* gallery = &iter->first; | |
169 MediaGalleryPrefId id = preferences_->AddGallery( | |
170 gallery->device_id, gallery->display_name, gallery->path, true); | |
171 preferences_->SetGalleryPermissionForExtension( | |
172 *extension_, id, true); | |
173 } | |
174 } | |
175 | |
176 } // namespace chrome | |
OLD | NEW |