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 // MediaGalleriesPrivateEventRouter implementation. | 5 // MediaGalleriesPrivateEventRouter implementation. |
6 | 6 |
7 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_
private_event_router.h" | 7 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_
private_event_router.h" |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/extensions/event_names.h" | 13 #include "chrome/browser/extensions/event_names.h" |
14 #include "chrome/browser/extensions/event_router.h" | 14 #include "chrome/browser/extensions/event_router.h" |
15 #include "chrome/browser/extensions/extension_system.h" | 15 #include "chrome/browser/extensions/extension_system.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/storage_monitor/storage_monitor.h" | 17 #include "chrome/browser/storage_monitor/storage_monitor.h" |
18 #include "chrome/common/extensions/api/media_galleries_private.h" | 18 #include "chrome/common/extensions/api/media_galleries_private.h" |
19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
20 | 20 |
21 namespace media_galleries_private = extensions::api::media_galleries_private; | 21 namespace media_galleries_private = extensions::api::media_galleries_private; |
22 | 22 |
23 namespace extensions { | 23 namespace extensions { |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 std::string GetTransientIdForDeviceId(const std::string& device_id) { | 27 std::string GetTransientIdForDeviceId(const std::string& device_id) { |
28 chrome::StorageMonitor* monitor = chrome::StorageMonitor::GetInstance(); | 28 StorageMonitor* monitor = StorageMonitor::GetInstance(); |
29 return monitor->GetTransientIdForDeviceId(device_id); | 29 return monitor->GetTransientIdForDeviceId(device_id); |
30 } | 30 } |
31 | 31 |
32 } // namespace | 32 } // namespace |
33 | 33 |
34 using media_galleries_private::DeviceAttachmentDetails; | 34 using media_galleries_private::DeviceAttachmentDetails; |
35 using media_galleries_private::DeviceDetachmentDetails; | 35 using media_galleries_private::DeviceDetachmentDetails; |
36 using media_galleries_private::GalleryChangeDetails; | 36 using media_galleries_private::GalleryChangeDetails; |
37 | 37 |
38 MediaGalleriesPrivateEventRouter::MediaGalleriesPrivateEventRouter( | 38 MediaGalleriesPrivateEventRouter::MediaGalleriesPrivateEventRouter( |
39 Profile* profile) | 39 Profile* profile) |
40 : profile_(profile) { | 40 : profile_(profile) { |
41 DCHECK(profile_); | 41 DCHECK(profile_); |
42 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 42 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
43 chrome::StorageMonitor::GetInstance()->AddObserver(this); | 43 StorageMonitor::GetInstance()->AddObserver(this); |
44 } | 44 } |
45 | 45 |
46 MediaGalleriesPrivateEventRouter::~MediaGalleriesPrivateEventRouter() { | 46 MediaGalleriesPrivateEventRouter::~MediaGalleriesPrivateEventRouter() { |
47 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 47 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
48 // TODO(gbillock): Remove this check once we have destruction order | 48 // TODO(gbillock): Remove this check once we have destruction order |
49 // fixed up for profile services and the storage monitor. | 49 // fixed up for profile services and the storage monitor. |
50 if (chrome::StorageMonitor::GetInstance()) | 50 if (StorageMonitor::GetInstance()) |
51 chrome::StorageMonitor::GetInstance()->RemoveObserver(this); | 51 StorageMonitor::GetInstance()->RemoveObserver(this); |
52 } | 52 } |
53 | 53 |
54 void MediaGalleriesPrivateEventRouter::OnGalleryChanged( | 54 void MediaGalleriesPrivateEventRouter::OnGalleryChanged( |
55 chrome::MediaGalleryPrefId gallery_id, | 55 MediaGalleryPrefId gallery_id, |
56 const std::set<std::string>& extension_ids) { | 56 const std::set<std::string>& extension_ids) { |
57 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 57 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
58 EventRouter* router = | 58 EventRouter* router = |
59 extensions::ExtensionSystem::Get(profile_)->event_router(); | 59 extensions::ExtensionSystem::Get(profile_)->event_router(); |
60 if (!router->HasEventListener( | 60 if (!router->HasEventListener( |
61 media_galleries_private::OnGalleryChanged::kEventName)) | 61 media_galleries_private::OnGalleryChanged::kEventName)) |
62 return; | 62 return; |
63 | 63 |
64 for (std::set<std::string>::const_iterator it = extension_ids.begin(); | 64 for (std::set<std::string>::const_iterator it = extension_ids.begin(); |
65 it != extension_ids.end(); ++it) { | 65 it != extension_ids.end(); ++it) { |
66 GalleryChangeDetails details; | 66 GalleryChangeDetails details; |
67 details.gallery_id = gallery_id; | 67 details.gallery_id = gallery_id; |
68 scoped_ptr<base::ListValue> args(new base::ListValue()); | 68 scoped_ptr<base::ListValue> args(new base::ListValue()); |
69 args->Append(details.ToValue().release()); | 69 args->Append(details.ToValue().release()); |
70 scoped_ptr<extensions::Event> event(new extensions::Event( | 70 scoped_ptr<extensions::Event> event(new extensions::Event( |
71 media_galleries_private::OnGalleryChanged::kEventName, | 71 media_galleries_private::OnGalleryChanged::kEventName, |
72 args.Pass())); | 72 args.Pass())); |
73 // Use DispatchEventToExtension() instead of BroadcastEvent(). | 73 // Use DispatchEventToExtension() instead of BroadcastEvent(). |
74 // BroadcastEvent() sends the gallery changed events to all the extensions | 74 // BroadcastEvent() sends the gallery changed events to all the extensions |
75 // who have added a listener to the onGalleryChanged event. There is a | 75 // who have added a listener to the onGalleryChanged event. There is a |
76 // chance that an extension might have added an onGalleryChanged() listener | 76 // chance that an extension might have added an onGalleryChanged() listener |
77 // without calling addGalleryWatch(). Therefore, use | 77 // without calling addGalleryWatch(). Therefore, use |
78 // DispatchEventToExtension() to dispatch the gallery changed event only to | 78 // DispatchEventToExtension() to dispatch the gallery changed event only to |
79 // the watching extensions. | 79 // the watching extensions. |
80 router->DispatchEventToExtension(*it, event.Pass()); | 80 router->DispatchEventToExtension(*it, event.Pass()); |
81 } | 81 } |
82 } | 82 } |
83 | 83 |
84 void MediaGalleriesPrivateEventRouter::OnRemovableStorageAttached( | 84 void MediaGalleriesPrivateEventRouter::OnRemovableStorageAttached( |
85 const chrome::StorageInfo& info) { | 85 const StorageInfo& info) { |
86 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 86 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
87 EventRouter* router = | 87 EventRouter* router = |
88 extensions::ExtensionSystem::Get(profile_)->event_router(); | 88 extensions::ExtensionSystem::Get(profile_)->event_router(); |
89 if (!router->HasEventListener( | 89 if (!router->HasEventListener( |
90 media_galleries_private::OnDeviceAttached::kEventName)) | 90 media_galleries_private::OnDeviceAttached::kEventName)) |
91 return; | 91 return; |
92 | 92 |
93 chrome::MediaGalleryPrefInfo pref_info; | 93 MediaGalleryPrefInfo pref_info; |
94 pref_info.display_name = info.name(); | 94 pref_info.display_name = info.name(); |
95 pref_info.device_id = info.device_id(); | 95 pref_info.device_id = info.device_id(); |
96 pref_info.type = chrome::MediaGalleryPrefInfo::kAutoDetected; | 96 pref_info.type = MediaGalleryPrefInfo::kAutoDetected; |
97 pref_info.volume_label = info.storage_label(); | 97 pref_info.volume_label = info.storage_label(); |
98 pref_info.vendor_name = info.vendor_name(); | 98 pref_info.vendor_name = info.vendor_name(); |
99 pref_info.model_name = info.model_name(); | 99 pref_info.model_name = info.model_name(); |
100 pref_info.total_size_in_bytes = info.total_size_in_bytes(); | 100 pref_info.total_size_in_bytes = info.total_size_in_bytes(); |
101 pref_info.volume_metadata_valid = true; | 101 pref_info.volume_metadata_valid = true; |
102 | 102 |
103 DeviceAttachmentDetails details; | 103 DeviceAttachmentDetails details; |
104 details.device_name = UTF16ToUTF8(pref_info.GetGalleryDisplayName()); | 104 details.device_name = UTF16ToUTF8(pref_info.GetGalleryDisplayName()); |
105 details.device_id = GetTransientIdForDeviceId(info.device_id()); | 105 details.device_id = GetTransientIdForDeviceId(info.device_id()); |
106 | 106 |
107 scoped_ptr<base::ListValue> args(new base::ListValue()); | 107 scoped_ptr<base::ListValue> args(new base::ListValue()); |
108 args->Append(details.ToValue().release()); | 108 args->Append(details.ToValue().release()); |
109 DispatchEvent( | 109 DispatchEvent( |
110 media_galleries_private::OnDeviceAttached::kEventName, args.Pass()); | 110 media_galleries_private::OnDeviceAttached::kEventName, args.Pass()); |
111 } | 111 } |
112 | 112 |
113 void MediaGalleriesPrivateEventRouter::OnRemovableStorageDetached( | 113 void MediaGalleriesPrivateEventRouter::OnRemovableStorageDetached( |
114 const chrome::StorageInfo& info) { | 114 const StorageInfo& info) { |
115 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 115 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
116 EventRouter* router = | 116 EventRouter* router = |
117 extensions::ExtensionSystem::Get(profile_)->event_router(); | 117 extensions::ExtensionSystem::Get(profile_)->event_router(); |
118 if (!router->HasEventListener( | 118 if (!router->HasEventListener( |
119 media_galleries_private::OnDeviceDetached::kEventName)) | 119 media_galleries_private::OnDeviceDetached::kEventName)) |
120 return; | 120 return; |
121 | 121 |
122 DeviceDetachmentDetails details; | 122 DeviceDetachmentDetails details; |
123 details.device_id = GetTransientIdForDeviceId(info.device_id()); | 123 details.device_id = GetTransientIdForDeviceId(info.device_id()); |
124 | 124 |
(...skipping 11 matching lines...) Expand all Loading... |
136 extensions::ExtensionSystem::Get(profile_)->event_router(); | 136 extensions::ExtensionSystem::Get(profile_)->event_router(); |
137 if (!router) | 137 if (!router) |
138 return; | 138 return; |
139 scoped_ptr<extensions::Event> event(new extensions::Event( | 139 scoped_ptr<extensions::Event> event(new extensions::Event( |
140 event_name, event_args.Pass())); | 140 event_name, event_args.Pass())); |
141 event->restrict_to_profile = profile_; | 141 event->restrict_to_profile = profile_; |
142 router->BroadcastEvent(event.Pass()); | 142 router->BroadcastEvent(event.Pass()); |
143 } | 143 } |
144 | 144 |
145 } // namespace extensions | 145 } // namespace extensions |
OLD | NEW |