Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(348)

Side by Side Diff: chrome/browser/extensions/api/media_galleries/media_galleries_api.cc

Issue 440813002: Expose gallery watching functionality to the public mediaGalleries API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleanup the code and prepare for review. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // Implements the Chrome Extensions Media Galleries API. 5 // Implements the Chrome Extensions Media Galleries API.
6 6
7 #include "chrome/browser/extensions/api/media_galleries/media_galleries_api.h" 7 #include "chrome/browser/extensions/api/media_galleries/media_galleries_api.h"
8 8
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 using storage_monitor::MediaStorageUtil; 61 using storage_monitor::MediaStorageUtil;
62 using storage_monitor::StorageInfo; 62 using storage_monitor::StorageInfo;
63 using web_modal::WebContentsModalDialogManager; 63 using web_modal::WebContentsModalDialogManager;
64 64
65 namespace extensions { 65 namespace extensions {
66 66
67 namespace MediaGalleries = api::media_galleries; 67 namespace MediaGalleries = api::media_galleries;
68 namespace DropPermissionForMediaFileSystem = 68 namespace DropPermissionForMediaFileSystem =
69 MediaGalleries::DropPermissionForMediaFileSystem; 69 MediaGalleries::DropPermissionForMediaFileSystem;
70 namespace GetMediaFileSystems = MediaGalleries::GetMediaFileSystems; 70 namespace GetMediaFileSystems = MediaGalleries::GetMediaFileSystems;
71 namespace AddGalleryWatch =
72 extensions::api::media_galleries::AddGalleryWatch;
tommycli 2014/08/19 16:36:02 make consistent with above declarations... i.e. Me
Oren Blasberg 2014/08/20 22:59:36 Done.
73 namespace RemoveGalleryWatch =
74 extensions::api::media_galleries::RemoveGalleryWatch;
75 namespace GetAllGalleryWatch =
76 extensions::api::media_galleries::GetAllGalleryWatch;
71 77
72 namespace { 78 namespace {
73 79
74 const char kDisallowedByPolicy[] = 80 const char kDisallowedByPolicy[] =
75 "Media Galleries API is disallowed by policy: "; 81 "Media Galleries API is disallowed by policy: ";
76 const char kFailedToSetGalleryPermission[] = 82 const char kFailedToSetGalleryPermission[] =
77 "Failed to set gallery permission."; 83 "Failed to set gallery permission.";
78 const char kInvalidGalleryId[] = "Invalid gallery id."; 84 const char kInvalidGalleryId[] = "Invalid gallery id.";
79 const char kMissingEventListener[] = "Missing event listener registration."; 85 const char kMissingEventListener[] = "Missing event listener registration.";
80 const char kNonExistentGalleryId[] = "Non-existent gallery id."; 86 const char kNonExistentGalleryId[] = "Non-existent gallery id.";
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 prefs::kAllowFileSelectionDialogs; 120 prefs::kAllowFileSelectionDialogs;
115 return false; 121 return false;
116 } 122 }
117 123
118 MediaGalleriesPreferences* preferences = 124 MediaGalleriesPreferences* preferences =
119 media_file_system_registry()->GetPreferences(profile); 125 media_file_system_registry()->GetPreferences(profile);
120 preferences->EnsureInitialized(callback); 126 preferences->EnsureInitialized(callback);
121 return true; 127 return true;
122 } 128 }
123 129
130 // Gets the |gallery_file_path| and |gallery_pref_id| of the gallery specified
tommycli 2014/08/19 16:36:02 Make briefer. Probably don't need both first sente
Oren Blasberg 2014/08/20 22:59:36 Done.
131 // by the |gallery_id|. Returns true and set |gallery_file_path| and
132 // |gallery_pref_id| if the |gallery_id| is valid and returns false otherwise.
133 bool GetGalleryFilePathAndId(const std::string& gallery_id,
134 Profile* profile,
135 const Extension* extension,
136 base::FilePath* gallery_file_path,
137 MediaGalleryPrefId* gallery_pref_id) {
138 MediaGalleryPrefId pref_id;
139 if (!base::StringToUint64(gallery_id, &pref_id))
140 return false;
141 MediaGalleriesPreferences* preferences =
142 g_browser_process->media_file_system_registry()->GetPreferences(profile);
143 base::FilePath file_path(
144 preferences->LookUpGalleryPathForExtension(pref_id, extension, false));
145 if (file_path.empty())
146 return false;
147 *gallery_pref_id = pref_id;
148 *gallery_file_path = file_path;
149 return true;
150 }
151
124 WebContents* GetWebContents(content::RenderViewHost* rvh, 152 WebContents* GetWebContents(content::RenderViewHost* rvh,
125 Profile* profile, 153 Profile* profile,
126 const std::string& app_id) { 154 const std::string& app_id) {
127 WebContents* contents = WebContents::FromRenderViewHost(rvh); 155 WebContents* contents = WebContents::FromRenderViewHost(rvh);
128 WebContentsModalDialogManager* web_contents_modal_dialog_manager = 156 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
129 WebContentsModalDialogManager::FromWebContents(contents); 157 WebContentsModalDialogManager::FromWebContents(contents);
130 if (!web_contents_modal_dialog_manager) { 158 if (!web_contents_modal_dialog_manager) {
131 // If there is no WebContentsModalDialogManager, then this contents is 159 // If there is no WebContentsModalDialogManager, then this contents is
132 // probably the background page for an app. Try to find a app window to 160 // probably the background page for an app. Try to find a app window to
133 // host the dialog. 161 // host the dialog.
(...skipping 893 matching lines...) Expand 10 before | Expand all | Expand 10 after
1027 base::Passed(&attached_images), base::Passed(&blob_uuids))); 1055 base::Passed(&attached_images), base::Passed(&blob_uuids)));
1028 return; 1056 return;
1029 } 1057 }
1030 1058
1031 // All Blobs have been constructed. The renderer will take ownership. 1059 // All Blobs have been constructed. The renderer will take ownership.
1032 SetResult(result_dictionary.release()); 1060 SetResult(result_dictionary.release());
1033 SetTransferredBlobUUIDs(*blob_uuids); 1061 SetTransferredBlobUUIDs(*blob_uuids);
1034 SendResponse(true); 1062 SendResponse(true);
1035 } 1063 }
1036 1064
1065 ///////////////////////////////////////////////////////////////////////////////
tommycli 2014/08/19 16:36:02 We don't use this block to divide between classes
Oren Blasberg 2014/08/20 22:59:36 Done.
1066 // MediaGalleriesAddGalleryWatchFunction //
1067 ///////////////////////////////////////////////////////////////////////////////
1068 MediaGalleriesAddGalleryWatchFunction::
tommycli 2014/08/19 16:36:02 I assume you ran git cl format on everything right
Oren Blasberg 2014/08/20 22:59:36 Didn't at first, did in patch 4.
1069 ~MediaGalleriesAddGalleryWatchFunction() {
1070 }
1071
1072 bool MediaGalleriesAddGalleryWatchFunction::RunAsync() {
1073 DCHECK(GetProfile());
1074 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
1075 if (!render_view_host() || !render_view_host()->GetProcess())
1076 return false;
1077
1078 scoped_ptr<AddGalleryWatch::Params> params(
1079 AddGalleryWatch::Params::Create(*args_));
1080 EXTENSION_FUNCTION_VALIDATE(params.get());
1081
1082 MediaGalleriesPreferences* preferences =
1083 g_browser_process->media_file_system_registry()->GetPreferences(
1084 GetProfile());
1085 preferences->EnsureInitialized(base::Bind(
1086 &MediaGalleriesAddGalleryWatchFunction::OnPreferencesInit,
1087 this,
1088 params->gallery_id));
1089
1090 return true;
1091 }
1092
1093 void MediaGalleriesAddGalleryWatchFunction::OnPreferencesInit(
1094 const std::string& pref_id) {
1095
1096 base::FilePath gallery_file_path;
1097 MediaGalleryPrefId gallery_pref_id = 0;
1098 if (!GetGalleryFilePathAndId(pref_id,
1099 GetProfile(),
1100 extension(),
1101 &gallery_file_path,
1102 &gallery_pref_id)) {
1103 error_ = kInvalidGalleryId;
1104 const std::string error_str(kInvalidGalleryId);
1105 HandleResponse(gallery_pref_id, error_str);
tommycli 2014/08/19 16:36:02 If you have an error here, you still check for an
Oren Blasberg 2014/08/20 22:59:36 Done.
1106 return;
1107 }
1108
1109 gallery_watch_manager()->AddWatch(
1110 GetProfile(),
1111 extension(),
1112 gallery_pref_id,
1113 base::Bind(&MediaGalleriesAddGalleryWatchFunction::HandleResponse,
1114 this,
1115 gallery_pref_id));
1116
1117 }
1118
1119 void MediaGalleriesAddGalleryWatchFunction::HandleResponse(
1120 MediaGalleryPrefId gallery_id,
1121 const std::string& error) {
1122 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
1123
1124 // If an app added a file watch without any event listeners on the
1125 // onGalleryChanged event, that's an error.
1126 MediaGalleriesEventRouter* api = MediaGalleriesEventRouter::Get(GetProfile());
1127 if (!api->ExtensionHasGalleryChangeListener(extension()->id())) {
1128 api::media_galleries::AddGalleryWatchResult result;
tommycli 2014/08/19 16:36:02 Does the callback still get called when there's an
Oren Blasberg 2014/08/20 22:59:36 It does, as evidenced by the setupWatchOnUnlistene
tommycli 2014/08/20 23:21:12 Awesome. Thanks for testing that out.
1129 result.gallery_id = base::Uint64ToString(gallery_id);
1130 result.success = false;
1131 SetResult(result.ToValue().release());
1132 error_ = kMissingEventListener;
1133 SendResponse(false);
1134 return;
1135 }
1136
1137 api::media_galleries::AddGalleryWatchResult result;
1138 result.gallery_id = base::Uint64ToString(gallery_id);
1139 result.success = error.empty();
1140 SetResult(result.ToValue().release());
1141 if (error.empty()) {
1142 base::DictionaryValue* dv = new base::DictionaryValue();
1143 results_->GetDictionary(0, &dv);
tommycli 2014/08/19 16:36:02 This looks strange. What's the purpose of this?
Oren Blasberg 2014/08/20 22:59:36 Whoops. It was part of debugging code I forgot to
1144 SendResponse(true);
1145 } else {
1146 error_ = error.c_str();
1147 SendResponse(false);
1148 }
1149 }
1150
1151 ///////////////////////////////////////////////////////////////////////////////
1152 // MediaGalleriesRemoveGalleryWatchFunction //
1153 ///////////////////////////////////////////////////////////////////////////////
1154
1155 MediaGalleriesRemoveGalleryWatchFunction::
1156 ~MediaGalleriesRemoveGalleryWatchFunction() {
1157 }
1158
1159 bool MediaGalleriesRemoveGalleryWatchFunction::RunAsync() {
1160 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
1161 if (!render_view_host() || !render_view_host()->GetProcess())
1162 return false;
1163
1164 scoped_ptr<RemoveGalleryWatch::Params> params(
1165 RemoveGalleryWatch::Params::Create(*args_));
1166 EXTENSION_FUNCTION_VALIDATE(params.get());
1167
1168 MediaGalleriesPreferences* preferences =
1169 g_browser_process->media_file_system_registry()->GetPreferences(
1170 GetProfile());
1171 preferences->EnsureInitialized(base::Bind(
1172 &MediaGalleriesRemoveGalleryWatchFunction::OnPreferencesInit,
1173 this,
1174 params->gallery_id));
1175 return true;
1176 }
1177
1178 void MediaGalleriesRemoveGalleryWatchFunction::OnPreferencesInit(
1179 const std::string& pref_id) {
1180 base::FilePath gallery_file_path;
1181 MediaGalleryPrefId gallery_pref_id = 0;
1182 if (!GetGalleryFilePathAndId(pref_id,
1183 GetProfile(),
1184 extension(),
1185 &gallery_file_path,
1186 &gallery_pref_id)) {
1187 error_ = kInvalidGalleryId;
1188 SendResponse(false);
1189 return;
1190 }
1191
1192 gallery_watch_manager()->RemoveWatch(
1193 GetProfile(), extension_id(), gallery_pref_id);
1194 SendResponse(true);
1195 }
1196
1197 ///////////////////////////////////////////////////////////////////////////////
1198 // MediaGalleriesGetAllGalleryWatchFunction //
1199 ///////////////////////////////////////////////////////////////////////////////
1200
1201 MediaGalleriesGetAllGalleryWatchFunction::
1202 ~MediaGalleriesGetAllGalleryWatchFunction() {
1203 }
1204
1205 bool MediaGalleriesGetAllGalleryWatchFunction::RunAsync() {
1206 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
1207 if (!render_view_host() || !render_view_host()->GetProcess())
1208 return false;
1209
1210 MediaGalleriesPreferences* preferences =
1211 g_browser_process->media_file_system_registry()->GetPreferences(
1212 GetProfile());
1213 preferences->EnsureInitialized(base::Bind(
1214 &MediaGalleriesGetAllGalleryWatchFunction::OnPreferencesInit,
1215 this));
1216 return true;
1217 }
1218
1219 void MediaGalleriesGetAllGalleryWatchFunction::OnPreferencesInit() {
1220 std::vector<std::string> result;
1221 MediaGalleryPrefIdSet gallery_ids =
1222 gallery_watch_manager()->GetWatchSet(GetProfile(), extension_id());
1223 for (MediaGalleryPrefIdSet::const_iterator iter = gallery_ids.begin();
1224 iter != gallery_ids.end(); ++iter) {
1225 result.push_back(base::Uint64ToString(*iter));
1226 }
1227 results_ = GetAllGalleryWatch::Results::Create(result);
tommycli 2014/08/19 16:36:02 I wonder if results_ = or SetResult is the correct
Oren Blasberg 2014/08/20 22:59:36 It looks like SetResult is used when you have just
tommycli 2014/08/20 23:21:12 Oh okay, great. thanks for verifyincg
1228 SendResponse(true);
1229 }
1230
1231 ///////////////////////////////////////////////////////////////////////////////
1232 // MediaGalleriesRemoveAllGalleryWatchFunction //
1233 ///////////////////////////////////////////////////////////////////////////////
1234
1235 MediaGalleriesRemoveAllGalleryWatchFunction::
1236 ~MediaGalleriesRemoveAllGalleryWatchFunction() {
1237 }
1238
1239 bool MediaGalleriesRemoveAllGalleryWatchFunction::RunAsync() {
1240 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
1241 if (!render_view_host() || !render_view_host()->GetProcess())
1242 return false;
1243
1244 MediaGalleriesPreferences* preferences =
1245 g_browser_process->media_file_system_registry()->GetPreferences(
1246 GetProfile());
1247 preferences->EnsureInitialized(base::Bind(
1248 &MediaGalleriesRemoveAllGalleryWatchFunction::OnPreferencesInit,
1249 this));
1250 return true;
1251 }
1252
1253 void MediaGalleriesRemoveAllGalleryWatchFunction::OnPreferencesInit() {
1254 gallery_watch_manager()->RemoveAllWatches(
1255 GetProfile(), extension_id());
1256 SendResponse(true);
1257 }
1258
1037 } // namespace extensions 1259 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698