Chromium Code Reviews| 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/extensions/api/file_system/file_system_api.h" | 5 #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <vector> | |
| 8 | 9 |
| 9 #include "apps/saved_files_service.h" | 10 #include "apps/saved_files_service.h" |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/linked_ptr.h" | |
| 14 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 15 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 17 #include "base/strings/sys_string_conversions.h" | 19 #include "base/strings/sys_string_conversions.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/value_conversions.h" | 21 #include "base/value_conversions.h" |
| 20 #include "base/values.h" | 22 #include "base/values.h" |
| 21 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" | 23 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" |
| 22 #include "chrome/browser/extensions/extension_service.h" | 24 #include "chrome/browser/extensions/extension_service.h" |
| 23 #include "chrome/browser/extensions/extension_util.h" | 25 #include "chrome/browser/extensions/extension_util.h" |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 callback.Run(CONSENT_IMPOSSIBLE); | 345 callback.Run(CONSENT_IMPOSSIBLE); |
| 344 break; | 346 break; |
| 345 case ui::DIALOG_BUTTON_OK: | 347 case ui::DIALOG_BUTTON_OK: |
| 346 callback.Run(CONSENT_GRANTED); | 348 callback.Run(CONSENT_GRANTED); |
| 347 break; | 349 break; |
| 348 case ui::DIALOG_BUTTON_CANCEL: | 350 case ui::DIALOG_BUTTON_CANCEL: |
| 349 callback.Run(CONSENT_REJECTED); | 351 callback.Run(CONSENT_REJECTED); |
| 350 break; | 352 break; |
| 351 } | 353 } |
| 352 } | 354 } |
| 355 | |
| 356 ConsentProviderDelegate::ConsentProviderDelegate(Profile* profile, | |
| 357 content::RenderViewHost* host) | |
| 358 : profile_(profile), host_(host) { | |
| 359 DCHECK(profile_); | |
| 360 DCHECK(host_); | |
| 361 } | |
| 362 | |
| 363 ConsentProviderDelegate::~ConsentProviderDelegate() { | |
| 364 } | |
| 365 | |
| 366 // static | |
| 367 void ConsentProviderDelegate::SetAutoDialogButtonForTest( | |
| 368 ui::DialogButton button) { | |
| 369 g_auto_dialog_button_for_test = button; | |
| 370 } | |
| 371 | |
| 372 void ConsentProviderDelegate::ShowDialog( | |
| 373 const extensions::Extension& extension, | |
| 374 base::WeakPtr<file_manager::Volume> volume, | |
| 375 bool writable, | |
| 376 const file_system_api::ConsentProvider::ShowDialogCallback& callback) { | |
| 377 content::WebContents* const foreground_contents = | |
| 378 GetWebContentsForRenderViewHost(profile_, host_); | |
| 379 // If there is no web contents handle, then the method is most probably | |
| 380 // executed from a background page. Find an app window to host the dialog. | |
| 381 content::WebContents* const web_contents = | |
| 382 foreground_contents ? foreground_contents | |
| 383 : GetWebContentsForAppId(profile_, extension.id()); | |
| 384 if (!web_contents) { | |
| 385 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 386 FROM_HERE, base::Bind(callback, ui::DIALOG_BUTTON_NONE)); | |
| 387 return; | |
| 388 } | |
| 389 | |
| 390 // Short circuit the user consent dialog for tests. This is far from a pretty | |
| 391 // code design. | |
| 392 if (g_auto_dialog_button_for_test != ui::DIALOG_BUTTON_NONE) { | |
| 393 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 394 FROM_HERE, | |
| 395 base::Bind(callback, g_auto_dialog_button_for_test /* result */)); | |
| 396 return; | |
| 397 } | |
| 398 | |
| 399 RequestFileSystemDialogView::ShowDialog(web_contents, extension, volume, | |
| 400 writable, base::Bind(callback)); | |
| 401 } | |
| 402 | |
| 403 bool ConsentProviderDelegate::IsAutoLaunched( | |
| 404 const extensions::Extension& extension) { | |
| 405 chromeos::KioskAppManager::App app_info; | |
| 406 return chromeos::KioskAppManager::Get()->GetApp(extension.id(), &app_info) && | |
| 407 app_info.was_auto_launched_with_zero_delay; | |
| 408 } | |
| 409 | |
| 410 bool ConsentProviderDelegate::IsWhitelistedComponent( | |
| 411 const extensions::Extension& extension) { | |
| 412 for (const auto& whitelisted_id : kRequestFileSystemComponentWhitelist) { | |
| 413 if (extension.id().compare(whitelisted_id) == 0) | |
| 414 return true; | |
| 415 } | |
| 416 return false; | |
| 417 } | |
| 418 | |
| 353 #endif | 419 #endif |
| 354 | 420 |
| 355 } // namespace file_system_api | 421 } // namespace file_system_api |
| 356 | 422 |
| 357 #if defined(OS_CHROMEOS) | 423 #if defined(OS_CHROMEOS) |
| 358 using file_system_api::ConsentProvider; | 424 using file_system_api::ConsentProvider; |
| 359 #endif | 425 #endif |
| 360 | 426 |
| 361 bool FileSystemGetDisplayPathFunction::RunSync() { | 427 bool FileSystemGetDisplayPathFunction::RunSync() { |
| 362 std::string filesystem_name; | 428 std::string filesystem_name; |
| (...skipping 765 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1128 #if !defined(OS_CHROMEOS) | 1194 #if !defined(OS_CHROMEOS) |
| 1129 ExtensionFunction::ResponseAction FileSystemRequestFileSystemFunction::Run() { | 1195 ExtensionFunction::ResponseAction FileSystemRequestFileSystemFunction::Run() { |
| 1130 using extensions::api::file_system::RequestFileSystem::Params; | 1196 using extensions::api::file_system::RequestFileSystem::Params; |
| 1131 const scoped_ptr<Params> params(Params::Create(*args_)); | 1197 const scoped_ptr<Params> params(Params::Create(*args_)); |
| 1132 EXTENSION_FUNCTION_VALIDATE(params); | 1198 EXTENSION_FUNCTION_VALIDATE(params); |
| 1133 | 1199 |
| 1134 NOTIMPLEMENTED(); | 1200 NOTIMPLEMENTED(); |
| 1135 return RespondNow(Error(kNotSupportedOnCurrentPlatformError)); | 1201 return RespondNow(Error(kNotSupportedOnCurrentPlatformError)); |
| 1136 } | 1202 } |
| 1137 | 1203 |
| 1204 ExtensionFunction::ResponseAction FileSystemGetVolumeListFunction::Run() { | |
| 1205 NOTIMPLEMENTED(); | |
| 1206 return RespondNow(Error(kNotSupportedOnCurrentPlatformError)); | |
| 1207 } | |
| 1138 #else | 1208 #else |
| 1209 | |
| 1139 FileSystemRequestFileSystemFunction::FileSystemRequestFileSystemFunction() | 1210 FileSystemRequestFileSystemFunction::FileSystemRequestFileSystemFunction() |
| 1140 : chrome_details_(this), consent_provider_(this) { | 1211 : chrome_details_(this), |
| 1212 consent_provider_delegate_(chrome_details_.GetProfile(), | |
| 1213 render_view_host()), | |
| 1214 consent_provider_(&consent_provider_delegate_) { | |
| 1141 } | 1215 } |
| 1142 | 1216 |
| 1143 FileSystemRequestFileSystemFunction::~FileSystemRequestFileSystemFunction() { | 1217 FileSystemRequestFileSystemFunction::~FileSystemRequestFileSystemFunction() { |
| 1144 } | 1218 } |
| 1145 | 1219 |
| 1146 ExtensionFunction::ResponseAction FileSystemRequestFileSystemFunction::Run() { | 1220 ExtensionFunction::ResponseAction FileSystemRequestFileSystemFunction::Run() { |
| 1147 using extensions::api::file_system::RequestFileSystem::Params; | 1221 using extensions::api::file_system::RequestFileSystem::Params; |
| 1148 const scoped_ptr<Params> params(Params::Create(*args_)); | 1222 const scoped_ptr<Params> params(Params::Create(*args_)); |
| 1149 EXTENSION_FUNCTION_VALIDATE(params); | 1223 EXTENSION_FUNCTION_VALIDATE(params); |
| 1150 | 1224 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1187 if (writable && (volume->is_read_only())) | 1261 if (writable && (volume->is_read_only())) |
| 1188 return RespondNow(Error(kSecurityError)); | 1262 return RespondNow(Error(kSecurityError)); |
| 1189 | 1263 |
| 1190 consent_provider_.RequestConsent( | 1264 consent_provider_.RequestConsent( |
| 1191 *extension(), volume, writable, | 1265 *extension(), volume, writable, |
| 1192 base::Bind(&FileSystemRequestFileSystemFunction::OnConsentReceived, this, | 1266 base::Bind(&FileSystemRequestFileSystemFunction::OnConsentReceived, this, |
| 1193 volume, writable)); | 1267 volume, writable)); |
| 1194 return RespondLater(); | 1268 return RespondLater(); |
| 1195 } | 1269 } |
| 1196 | 1270 |
| 1197 // static | |
| 1198 void FileSystemRequestFileSystemFunction::SetAutoDialogButtonForTest( | |
| 1199 ui::DialogButton button) { | |
| 1200 g_auto_dialog_button_for_test = button; | |
| 1201 } | |
| 1202 | |
| 1203 void FileSystemRequestFileSystemFunction::ShowDialog( | |
| 1204 const extensions::Extension& extension, | |
| 1205 base::WeakPtr<file_manager::Volume> volume, | |
| 1206 bool writable, | |
| 1207 const file_system_api::ConsentProvider::ShowDialogCallback& callback) { | |
| 1208 content::WebContents* const foreground_contents = | |
| 1209 GetWebContentsForRenderViewHost(chrome_details_.GetProfile(), | |
| 1210 render_view_host()); | |
| 1211 // If there is no web contents handle, then the method is most probably | |
| 1212 // executed from a background page. Find an app window to host the dialog. | |
| 1213 content::WebContents* const web_contents = | |
| 1214 foreground_contents ? foreground_contents | |
| 1215 : GetWebContentsForAppId(chrome_details_.GetProfile(), | |
| 1216 extension_id()); | |
| 1217 if (!web_contents) { | |
| 1218 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 1219 FROM_HERE, base::Bind(callback, ui::DIALOG_BUTTON_NONE)); | |
| 1220 return; | |
| 1221 } | |
| 1222 | |
| 1223 // Short circuit the user consent dialog for tests. This is far from a pretty | |
| 1224 // code design. | |
| 1225 if (g_auto_dialog_button_for_test != ui::DIALOG_BUTTON_NONE) { | |
| 1226 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 1227 FROM_HERE, | |
| 1228 base::Bind(callback, g_auto_dialog_button_for_test /* result */)); | |
| 1229 return; | |
| 1230 } | |
| 1231 | |
| 1232 RequestFileSystemDialogView::ShowDialog(web_contents, extension, volume, | |
| 1233 writable, base::Bind(callback)); | |
| 1234 } | |
| 1235 | |
| 1236 bool FileSystemRequestFileSystemFunction::IsAutoLaunched( | |
| 1237 const extensions::Extension& extension) { | |
| 1238 chromeos::KioskAppManager::App app_info; | |
| 1239 return chromeos::KioskAppManager::Get()->GetApp(extension.id(), &app_info) && | |
| 1240 app_info.was_auto_launched_with_zero_delay; | |
| 1241 } | |
| 1242 | |
| 1243 bool FileSystemRequestFileSystemFunction::IsWhitelistedComponent( | |
| 1244 const extensions::Extension& extension) { | |
| 1245 for (const auto& whitelisted_id : kRequestFileSystemComponentWhitelist) { | |
| 1246 if (extension.id().compare(whitelisted_id) == 0) | |
| 1247 return true; | |
| 1248 } | |
| 1249 return false; | |
| 1250 } | |
| 1251 | |
| 1252 void FileSystemRequestFileSystemFunction::OnConsentReceived( | 1271 void FileSystemRequestFileSystemFunction::OnConsentReceived( |
| 1253 base::WeakPtr<file_manager::Volume> volume, | 1272 base::WeakPtr<file_manager::Volume> volume, |
| 1254 bool writable, | 1273 bool writable, |
| 1255 ConsentProvider::Consent result) { | 1274 ConsentProvider::Consent result) { |
| 1256 using file_manager::VolumeManager; | 1275 using file_manager::VolumeManager; |
| 1257 using file_manager::Volume; | 1276 using file_manager::Volume; |
| 1258 | 1277 |
| 1259 switch (result) { | 1278 switch (result) { |
| 1260 case ConsentProvider::CONSENT_REJECTED: | 1279 case ConsentProvider::CONSENT_REJECTED: |
| 1261 SetError(kSecurityError); | 1280 SetError(kSecurityError); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1344 render_view_host()->GetProcess()->GetID(), file_system_id); | 1363 render_view_host()->GetProcess()->GetID(), file_system_id); |
| 1345 } | 1364 } |
| 1346 | 1365 |
| 1347 base::DictionaryValue* const dict = new base::DictionaryValue(); | 1366 base::DictionaryValue* const dict = new base::DictionaryValue(); |
| 1348 dict->SetString("file_system_id", file_system_id); | 1367 dict->SetString("file_system_id", file_system_id); |
| 1349 dict->SetString("file_system_path", register_name); | 1368 dict->SetString("file_system_path", register_name); |
| 1350 | 1369 |
| 1351 SetResult(dict); | 1370 SetResult(dict); |
| 1352 SendResponse(true); | 1371 SendResponse(true); |
| 1353 } | 1372 } |
| 1373 | |
| 1374 FileSystemGetVolumeListFunction::FileSystemGetVolumeListFunction() | |
| 1375 : chrome_details_(this), | |
| 1376 consent_provider_delegate_(chrome_details_.GetProfile(), | |
| 1377 render_view_host()), | |
| 1378 consent_provider_(&consent_provider_delegate_) { | |
| 1379 } | |
| 1380 | |
| 1381 FileSystemGetVolumeListFunction::~FileSystemGetVolumeListFunction() { | |
| 1382 } | |
| 1383 | |
| 1384 ExtensionFunction::ResponseAction FileSystemGetVolumeListFunction::Run() { | |
| 1385 // Only kiosk apps in kiosk sessions can use this API. | |
| 1386 // Additionally whitelisted component extensions and apps. | |
|
benwells
2015/04/07 05:43:13
Nit: this last sentence isn't really a sentence (n
mtomasz
2015/04/07 07:35:35
Done.
| |
| 1387 if (!consent_provider_.IsGrantable(*extension())) | |
| 1388 return RespondNow(Error(kNotSupportedOnNonKioskSessionError)); | |
| 1389 | |
| 1390 using file_manager::VolumeManager; | |
| 1391 VolumeManager* const volume_manager = | |
| 1392 VolumeManager::Get(chrome_details_.GetProfile()); | |
| 1393 DCHECK(volume_manager); | |
| 1394 | |
| 1395 using extensions::api::file_system::Volume; | |
| 1396 const auto& volume_list = volume_manager->GetVolumeList(); | |
| 1397 std::vector<linked_ptr<Volume>> result_volume_list; | |
| 1398 // Convert volume_list to result_volume_list. | |
| 1399 for (const auto& volume : volume_list) { | |
| 1400 const linked_ptr<Volume> result_volume(new Volume); | |
| 1401 result_volume->volume_id = volume->volume_id(); | |
| 1402 result_volume->writable = !volume->is_read_only(); | |
| 1403 result_volume_list.push_back(result_volume); | |
| 1404 } | |
| 1405 | |
| 1406 return RespondNow( | |
| 1407 ArgumentList(extensions::api::file_system::GetVolumeList::Results::Create( | |
| 1408 result_volume_list).Pass())); | |
| 1409 } | |
| 1354 #endif | 1410 #endif |
| 1355 | 1411 |
| 1356 } // namespace extensions | 1412 } // namespace extensions |
| OLD | NEW |