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 // 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 <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/platform_file.h" | 12 #include "base/platform_file.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/media_gallery/media_file_system_registry.h" | 14 #include "chrome/browser/media_gallery/media_file_system_registry.h" |
| 15 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
15 #include "chrome/common/extensions/api/experimental_media_galleries.h" | 16 #include "chrome/common/extensions/api/experimental_media_galleries.h" |
16 #include "content/public/browser/child_process_security_policy.h" | 17 #include "content/public/browser/child_process_security_policy.h" |
17 #include "content/public/browser/render_process_host.h" | 18 #include "content/public/browser/render_process_host.h" |
18 #include "content/public/browser/render_view_host.h" | 19 #include "content/public/browser/render_view_host.h" |
| 20 #include "content/public/browser/web_contents.h" |
19 | 21 |
20 #if defined(OS_WIN) | 22 #if defined(OS_WIN) |
21 #include "base/sys_string_conversions.h" | 23 #include "base/sys_string_conversions.h" |
22 #endif | 24 #endif |
23 | 25 |
| 26 #if defined(TOOLKIT_GTK) |
| 27 #include "chrome/browser/media_gallery/media_galleries_dialog_controller.h" |
| 28 #endif |
| 29 |
| 30 using content::WebContents; |
| 31 |
24 namespace extensions { | 32 namespace extensions { |
25 | 33 |
26 namespace { | 34 namespace { |
27 | 35 |
28 const char kInvalidInteractive[] = "Unknown value for interactive."; | 36 const char kInvalidInteractive[] = "Unknown value for interactive."; |
29 | 37 |
30 } // namespace | 38 } // namespace |
31 | 39 |
32 using chrome::MediaFileSystemRegistry; | 40 using chrome::MediaFileSystemRegistry; |
33 using content::ChildProcessSecurityPolicy; | 41 using content::ChildProcessSecurityPolicy; |
34 | 42 |
35 namespace MediaGalleries = extensions::api::experimental_media_galleries; | 43 namespace MediaGalleries = extensions::api::experimental_media_galleries; |
36 namespace GetMediaFileSystems = MediaGalleries::GetMediaFileSystems; | 44 namespace GetMediaFileSystems = MediaGalleries::GetMediaFileSystems; |
37 | 45 |
38 MediaGalleriesGetMediaFileSystemsFunction:: | 46 MediaGalleriesGetMediaFileSystemsFunction:: |
39 ~MediaGalleriesGetMediaFileSystemsFunction() {} | 47 ~MediaGalleriesGetMediaFileSystemsFunction() {} |
40 | 48 |
41 bool MediaGalleriesGetMediaFileSystemsFunction::RunImpl() { | 49 bool MediaGalleriesGetMediaFileSystemsFunction::RunImpl() { |
42 scoped_ptr<GetMediaFileSystems::Params> params( | 50 scoped_ptr<GetMediaFileSystems::Params> params( |
43 GetMediaFileSystems::Params::Create(*args_)); | 51 GetMediaFileSystems::Params::Create(*args_)); |
44 EXTENSION_FUNCTION_VALIDATE(params.get()); | 52 EXTENSION_FUNCTION_VALIDATE(params.get()); |
45 MediaGalleries::GetMediaFileSystemsInteractivity interactive = "no"; | 53 MediaGalleries::GetMediaFileSystemsInteractivity interactive = "no"; |
46 if (params->details.get() && params->details->interactive.get()) | 54 if (params->details.get() && params->details->interactive.get()) |
47 interactive = *params->details->interactive; | 55 interactive = *params->details->interactive; |
48 | 56 |
49 if (interactive == "yes") { | 57 if (interactive == "yes") { |
50 // TODO(estade): implement. | 58 ShowDialog(); |
| 59 return true; |
51 } else if (interactive == "if_needed") { | 60 } else if (interactive == "if_needed") { |
52 // TODO(estade): implement. | 61 // TODO(estade): implement. |
53 } else if (interactive != "no") { | 62 } else if (interactive == "no") { |
54 error_ = kInvalidInteractive; | 63 ReturnGalleries(); |
55 return false; | 64 return true; |
56 } | 65 } |
57 | 66 |
| 67 error_ = kInvalidInteractive; |
| 68 return false; |
| 69 } |
| 70 |
| 71 void MediaGalleriesGetMediaFileSystemsFunction::ReturnGalleries() { |
58 const content::RenderProcessHost* rph = render_view_host()->GetProcess(); | 72 const content::RenderProcessHost* rph = render_view_host()->GetProcess(); |
59 chrome::MediaFileSystemRegistry* media_fs_registry = | 73 chrome::MediaFileSystemRegistry* media_fs_registry = |
60 MediaFileSystemRegistry::GetInstance(); | 74 MediaFileSystemRegistry::GetInstance(); |
61 const std::vector<MediaFileSystemRegistry::MediaFSInfo> filesystems = | 75 const std::vector<MediaFileSystemRegistry::MediaFSInfo> filesystems = |
62 media_fs_registry->GetMediaFileSystemsForExtension(rph, *GetExtension()); | 76 media_fs_registry->GetMediaFileSystemsForExtension(rph, *GetExtension()); |
63 | 77 |
64 const int child_id = rph->GetID(); | 78 const int child_id = rph->GetID(); |
65 base::ListValue* list = new base::ListValue(); | 79 base::ListValue* list = new base::ListValue(); |
66 for (size_t i = 0; i < filesystems.size(); i++) { | 80 for (size_t i = 0; i < filesystems.size(); i++) { |
67 base::DictionaryValue* dict_value = new base::DictionaryValue(); | 81 base::DictionaryValue* dict_value = new base::DictionaryValue(); |
68 dict_value->SetWithoutPathExpansion( | 82 dict_value->SetWithoutPathExpansion( |
69 "fsid", Value::CreateStringValue(filesystems[i].fsid)); | 83 "fsid", Value::CreateStringValue(filesystems[i].fsid)); |
70 // The directory name is not exposed to the js layer. | 84 // The directory name is not exposed to the js layer. |
71 dict_value->SetWithoutPathExpansion( | 85 dict_value->SetWithoutPathExpansion( |
72 "name", Value::CreateStringValue(filesystems[i].name)); | 86 "name", Value::CreateStringValue(filesystems[i].name)); |
73 list->Append(dict_value); | 87 list->Append(dict_value); |
74 | 88 |
75 if (GetExtension()->HasAPIPermission( | 89 if (GetExtension()->HasAPIPermission( |
76 extensions::APIPermission::kMediaGalleriesRead)) { | 90 extensions::APIPermission::kMediaGalleriesRead)) { |
77 content::ChildProcessSecurityPolicy* policy = | 91 content::ChildProcessSecurityPolicy* policy = |
78 ChildProcessSecurityPolicy::GetInstance(); | 92 ChildProcessSecurityPolicy::GetInstance(); |
79 if (!policy->CanReadFile(child_id, filesystems[i].path)) | 93 if (!policy->CanReadFile(child_id, filesystems[i].path)) |
80 policy->GrantReadFile(child_id, filesystems[i].path); | 94 policy->GrantReadFile(child_id, filesystems[i].path); |
81 policy->GrantReadFileSystem(child_id, filesystems[i].fsid); | 95 policy->GrantReadFileSystem(child_id, filesystems[i].fsid); |
82 } | 96 } |
83 // TODO(vandebo) Handle write permission. | 97 // TODO(vandebo) Handle write permission. |
84 } | 98 } |
85 | 99 |
86 SetResult(list); | 100 SetResult(list); |
87 return true; | 101 SendResponse(true); |
88 } | 102 } |
89 | 103 |
| 104 void MediaGalleriesGetMediaFileSystemsFunction::ShowDialog() { |
| 105 WebContents* contents = WebContents::FromRenderViewHost(render_view_host()); |
| 106 TabContents* tab_contents = |
| 107 contents ? TabContents::FromWebContents(contents) : NULL; |
| 108 if (!tab_contents) { |
| 109 // TODO(estade): for now it just gives up, but it might be nice to first |
| 110 // attempt to find the active window for this extension. |
| 111 ReturnGalleries(); |
| 112 return; |
| 113 } |
| 114 |
| 115 #if defined(TOOLKIT_GTK) |
| 116 // Controller will delete itself. |
| 117 new chrome::MediaGalleriesDialogController( |
| 118 tab_contents, *GetExtension(), |
| 119 base::Bind(&MediaGalleriesGetMediaFileSystemsFunction::ReturnGalleries, |
| 120 this)); |
| 121 #else |
| 122 // TODO(estade): implement dialog on Views and Cocoa. |
| 123 ReturnGalleries(); |
| 124 #endif |
| 125 } |
| 126 |
| 127 // MediaGalleriesAssembleMediaFileFunction ------------------------------------- |
| 128 |
90 MediaGalleriesAssembleMediaFileFunction:: | 129 MediaGalleriesAssembleMediaFileFunction:: |
91 ~MediaGalleriesAssembleMediaFileFunction() {} | 130 ~MediaGalleriesAssembleMediaFileFunction() {} |
92 | 131 |
93 bool MediaGalleriesAssembleMediaFileFunction::RunImpl() { | 132 bool MediaGalleriesAssembleMediaFileFunction::RunImpl() { |
94 // TODO(vandebo) Update the metadata and return the new file. | 133 // TODO(vandebo) Update the metadata and return the new file. |
95 SetResult(Value::CreateNullValue()); | 134 SetResult(Value::CreateNullValue()); |
96 return true; | 135 return true; |
97 } | 136 } |
98 | 137 |
99 } // namespace extensions | 138 } // namespace extensions |
OLD | NEW |