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

Side by Side Diff: chrome/browser/extensions/api/file_system/file_system_api.cc

Issue 1135383002: Add the chrome.fileSystem.onVolumeListChanged event. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleaned up. Created 5 years, 7 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 #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 #include <vector>
9 9
10 #include "apps/saved_files_service.h" 10 #include "apps/saved_files_service.h"
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 #include "base/prefs/testing_pref_service.h" 63 #include "base/prefs/testing_pref_service.h"
64 #include "base/strings/string16.h" 64 #include "base/strings/string16.h"
65 #include "base/thread_task_runner_handle.h" 65 #include "base/thread_task_runner_handle.h"
66 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" 66 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
67 #include "chrome/browser/chromeos/file_manager/app_id.h" 67 #include "chrome/browser/chromeos/file_manager/app_id.h"
68 #include "chrome/browser/chromeos/file_manager/filesystem_api_util.h" 68 #include "chrome/browser/chromeos/file_manager/filesystem_api_util.h"
69 #include "chrome/browser/extensions/api/file_system/request_file_system_dialog_v iew.h" 69 #include "chrome/browser/extensions/api/file_system/request_file_system_dialog_v iew.h"
70 #include "chrome/browser/extensions/api/file_system/request_file_system_notifica tion.h" 70 #include "chrome/browser/extensions/api/file_system/request_file_system_notifica tion.h"
71 #include "chrome/browser/ui/simple_message_box.h" 71 #include "chrome/browser/ui/simple_message_box.h"
72 #include "components/user_manager/user_manager.h" 72 #include "components/user_manager/user_manager.h"
73 #include "extensions/browser/event_router.h"
74 #include "extensions/browser/extension_registry.h"
73 #include "extensions/common/constants.h" 75 #include "extensions/common/constants.h"
74 #include "extensions/common/manifest_handlers/kiosk_mode_info.h" 76 #include "extensions/common/manifest_handlers/kiosk_mode_info.h"
75 #include "url/url_constants.h" 77 #include "url/url_constants.h"
76 #endif 78 #endif
77 79
78 using apps::SavedFileEntry; 80 using apps::SavedFileEntry;
79 using apps::SavedFilesService; 81 using apps::SavedFilesService;
80 using storage::IsolatedContext; 82 using storage::IsolatedContext;
81 83
82 const char kInvalidCallingPage[] = "Invalid calling page. This function can't " 84 const char kInvalidCallingPage[] = "Invalid calling page. This function can't "
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 // with |app_id|. If not found, then returns NULL. 242 // with |app_id|. If not found, then returns NULL.
241 content::WebContents* GetWebContentsForAppId(Profile* profile, 243 content::WebContents* GetWebContentsForAppId(Profile* profile,
242 const std::string& app_id) { 244 const std::string& app_id) {
243 extensions::AppWindowRegistry* const registry = 245 extensions::AppWindowRegistry* const registry =
244 extensions::AppWindowRegistry::Get(profile); 246 extensions::AppWindowRegistry::Get(profile);
245 DCHECK(registry); 247 DCHECK(registry);
246 extensions::AppWindow* const app_window = 248 extensions::AppWindow* const app_window =
247 registry->GetCurrentAppWindowForApp(app_id); 249 registry->GetCurrentAppWindowForApp(app_id);
248 return app_window ? app_window->web_contents() : nullptr; 250 return app_window ? app_window->web_contents() : nullptr;
249 } 251 }
252
253 // Fills a list of volumes mounted in the system.
254 void FillVolumeList(
255 Profile* profile,
256 std::vector<linked_ptr<extensions::api::file_system::Volume>>* result) {
257 using file_manager::VolumeManager;
258 VolumeManager* const volume_manager = VolumeManager::Get(profile);
259 DCHECK(volume_manager);
260
261 using extensions::api::file_system::Volume;
262 const auto& volume_list = volume_manager->GetVolumeList();
263 // Convert volume_list to result_volume_list.
264 for (const auto& volume : volume_list) {
265 const linked_ptr<Volume> result_volume(new Volume);
266 result_volume->volume_id = volume->volume_id();
267 result_volume->writable = !volume->is_read_only();
268 result->push_back(result_volume);
269 }
270 }
250 #endif 271 #endif
251 272
252 } // namespace 273 } // namespace
253 274
254 namespace extensions { 275 namespace extensions {
255 276
256 namespace file_system_api { 277 namespace file_system_api {
257 278
258 base::FilePath GetLastChooseEntryDirectory(const ExtensionPrefs* prefs, 279 base::FilePath GetLastChooseEntryDirectory(const ExtensionPrefs* prefs,
259 const std::string& extension_id) { 280 const std::string& extension_id) {
(...skipping 19 matching lines...) Expand all
279 std::vector<base::FilePath> graylisted_directories; 300 std::vector<base::FilePath> graylisted_directories;
280 for (size_t i = 0; i < arraysize(kGraylistedPaths); ++i) { 301 for (size_t i = 0; i < arraysize(kGraylistedPaths); ++i) {
281 base::FilePath graylisted_path; 302 base::FilePath graylisted_path;
282 if (PathService::Get(kGraylistedPaths[i], &graylisted_path)) 303 if (PathService::Get(kGraylistedPaths[i], &graylisted_path))
283 graylisted_directories.push_back(graylisted_path); 304 graylisted_directories.push_back(graylisted_path);
284 } 305 }
285 return graylisted_directories; 306 return graylisted_directories;
286 } 307 }
287 308
288 #if defined(OS_CHROMEOS) 309 #if defined(OS_CHROMEOS)
310 void DispatchVolumeListChangeEvent(Profile* profile) {
311 DCHECK(profile);
312 EventRouter* const event_router = EventRouter::Get(profile);
313 if (!event_router) // Possible on shutdown.
314 return;
315
316 extensions::ExtensionRegistry* const registry =
317 extensions::ExtensionRegistry::Get(profile);
318 if (!registry) // Possible on shutdown.
319 return;
320
321 ConsentProvider consent_provider(
322 new ConsentProviderDelegate(profile, nullptr));
323 extensions::api::file_system::VolumeListChangedEvent event_args;
324 FillVolumeList(profile, &event_args.volumes);
325 for (const auto& extension : registry->enabled_extensions()) {
326 if (!consent_provider.IsGrantable(*extension.get()))
327 continue;
328 event_router->DispatchEventToExtension(
329 extension->id(),
330 make_scoped_ptr(new Event(
331 extensions::api::file_system::OnVolumeListChanged::kEventName,
332 extensions::api::file_system::OnVolumeListChanged::Create(
333 event_args))));
334 }
335 }
336
289 ConsentProvider::ConsentProvider(DelegateInterface* delegate) 337 ConsentProvider::ConsentProvider(DelegateInterface* delegate)
290 : delegate_(delegate) { 338 : delegate_(delegate) {
291 DCHECK(delegate_); 339 DCHECK(delegate_);
292 } 340 }
293 341
294 ConsentProvider::~ConsentProvider() { 342 ConsentProvider::~ConsentProvider() {
295 } 343 }
296 344
297 void ConsentProvider::RequestConsent( 345 void ConsentProvider::RequestConsent(
298 const extensions::Extension& extension, 346 const extensions::Extension& extension,
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 case ui::DIALOG_BUTTON_CANCEL: 402 case ui::DIALOG_BUTTON_CANCEL:
355 callback.Run(CONSENT_REJECTED); 403 callback.Run(CONSENT_REJECTED);
356 break; 404 break;
357 } 405 }
358 } 406 }
359 407
360 ConsentProviderDelegate::ConsentProviderDelegate(Profile* profile, 408 ConsentProviderDelegate::ConsentProviderDelegate(Profile* profile,
361 content::RenderViewHost* host) 409 content::RenderViewHost* host)
362 : profile_(profile), host_(host) { 410 : profile_(profile), host_(host) {
363 DCHECK(profile_); 411 DCHECK(profile_);
364 DCHECK(host_);
365 } 412 }
366 413
367 ConsentProviderDelegate::~ConsentProviderDelegate() { 414 ConsentProviderDelegate::~ConsentProviderDelegate() {
368 } 415 }
369 416
370 // static 417 // static
371 void ConsentProviderDelegate::SetAutoDialogButtonForTest( 418 void ConsentProviderDelegate::SetAutoDialogButtonForTest(
372 ui::DialogButton button) { 419 ui::DialogButton button) {
373 g_auto_dialog_button_for_test = button; 420 g_auto_dialog_button_for_test = button;
374 } 421 }
375 422
376 void ConsentProviderDelegate::ShowDialog( 423 void ConsentProviderDelegate::ShowDialog(
377 const extensions::Extension& extension, 424 const extensions::Extension& extension,
378 const base::WeakPtr<file_manager::Volume>& volume, 425 const base::WeakPtr<file_manager::Volume>& volume,
379 bool writable, 426 bool writable,
380 const file_system_api::ConsentProvider::ShowDialogCallback& callback) { 427 const file_system_api::ConsentProvider::ShowDialogCallback& callback) {
428 DCHECK(host_);
381 content::WebContents* const foreground_contents = 429 content::WebContents* const foreground_contents =
382 GetWebContentsForRenderViewHost(profile_, host_); 430 GetWebContentsForRenderViewHost(profile_, host_);
383 // If there is no web contents handle, then the method is most probably 431 // If there is no web contents handle, then the method is most probably
384 // executed from a background page. Find an app window to host the dialog. 432 // executed from a background page. Find an app window to host the dialog.
385 content::WebContents* const web_contents = 433 content::WebContents* const web_contents =
386 foreground_contents ? foreground_contents 434 foreground_contents ? foreground_contents
387 : GetWebContentsForAppId(profile_, extension.id()); 435 : GetWebContentsForAppId(profile_, extension.id());
388 if (!web_contents) { 436 if (!web_contents) {
389 base::ThreadTaskRunnerHandle::Get()->PostTask( 437 base::ThreadTaskRunnerHandle::Get()->PostTask(
390 FROM_HERE, base::Bind(callback, ui::DIALOG_BUTTON_NONE)); 438 FROM_HERE, base::Bind(callback, ui::DIALOG_BUTTON_NONE));
(...skipping 1002 matching lines...) Expand 10 before | Expand all | Expand 10 after
1393 1441
1394 ExtensionFunction::ResponseAction FileSystemGetVolumeListFunction::Run() { 1442 ExtensionFunction::ResponseAction FileSystemGetVolumeListFunction::Run() {
1395 // Only kiosk apps in kiosk sessions can use this API. 1443 // Only kiosk apps in kiosk sessions can use this API.
1396 // Additionally it is enabled for whitelisted component extensions and apps. 1444 // Additionally it is enabled for whitelisted component extensions and apps.
1397 file_system_api::ConsentProviderDelegate consent_provider_delegate( 1445 file_system_api::ConsentProviderDelegate consent_provider_delegate(
1398 chrome_details_.GetProfile(), render_view_host()); 1446 chrome_details_.GetProfile(), render_view_host());
1399 file_system_api::ConsentProvider consent_provider(&consent_provider_delegate); 1447 file_system_api::ConsentProvider consent_provider(&consent_provider_delegate);
1400 1448
1401 if (!consent_provider.IsGrantable(*extension())) 1449 if (!consent_provider.IsGrantable(*extension()))
1402 return RespondNow(Error(kNotSupportedOnNonKioskSessionError)); 1450 return RespondNow(Error(kNotSupportedOnNonKioskSessionError));
1403
1404 using file_manager::VolumeManager;
1405 VolumeManager* const volume_manager =
1406 VolumeManager::Get(chrome_details_.GetProfile());
1407 DCHECK(volume_manager);
1408
1409 using extensions::api::file_system::Volume; 1451 using extensions::api::file_system::Volume;
1410 const auto& volume_list = volume_manager->GetVolumeList();
1411 std::vector<linked_ptr<Volume>> result_volume_list; 1452 std::vector<linked_ptr<Volume>> result_volume_list;
1412 // Convert volume_list to result_volume_list. 1453 FillVolumeList(chrome_details_.GetProfile(), &result_volume_list);
1413 for (const auto& volume : volume_list) {
1414 const linked_ptr<Volume> result_volume(new Volume);
1415 result_volume->volume_id = volume->volume_id();
1416 result_volume->writable = !volume->is_read_only();
1417 result_volume_list.push_back(result_volume);
1418 }
1419 1454
1420 return RespondNow( 1455 return RespondNow(
1421 ArgumentList(extensions::api::file_system::GetVolumeList::Results::Create( 1456 ArgumentList(extensions::api::file_system::GetVolumeList::Results::Create(
1422 result_volume_list).Pass())); 1457 result_volume_list).Pass()));
1423 } 1458 }
1424 #endif 1459 #endif
1425 1460
1426 } // namespace extensions 1461 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698