OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/renderer_host/pepper/flash_lso_settings_helper.h" | |
6 | |
7 #include "chrome/browser/profiles/profile.h" | |
8 #include "chrome/common/chrome_notification_types.h" | |
9 #include "content/public/browser/browser_context.h" | |
10 #include "content/public/browser/browser_thread.h" | |
11 #include "content/public/browser/content_browser_client.h" | |
12 #include "content/public/browser/notification_source.h" | |
13 #include "content/public/browser/render_process_host.h" | |
14 #include "content/public/browser/resource_context.h" | |
15 #include "googleurl/src/gurl.h" | |
16 | |
17 using content::BrowserThread; | |
18 using content::RenderProcessHost; | |
19 using content::ResourceContext; | |
20 | |
21 namespace chrome { | |
22 | |
23 namespace { | |
24 | |
25 const PP_FlashLSORestrictions kDefaultLSORestrictions = | |
26 PP_FLASHLSORESTRICTIONS_NONE; | |
yzshen1
2012/12/29 02:04:05
This value is returned on failure. It indicates "n
raymes
2012/12/29 03:21:57
Yes - I actually checked this with bauerb. His res
| |
27 | |
28 } // namespace | |
29 | |
30 FlashLSOSettingsHelper::FlashLSOSettingsHelper(int render_process_id) | |
31 : render_process_id_(render_process_id), | |
32 initialized_(false), | |
33 resource_context_(NULL) { | |
34 } | |
35 | |
36 FlashLSOSettingsHelper::~FlashLSOSettingsHelper() { | |
37 } | |
38 | |
39 void FlashLSOSettingsHelper::GetLocalDataRestrictions( | |
40 const GURL& document_url, | |
41 const GURL& plugin_url, | |
42 base::Callback<void(PP_FlashLSORestrictions)> callback) { | |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
44 // Getting the local data restrictions needs to be done on the IO thread, | |
45 // however it relies on the ResourceContext which can only be accessed from | |
46 // the UI thread. We lazily initialize |resource_context_| by grabbing the | |
47 // pointer from the UI thread and then call |GetLocalDataRestrictions| with | |
yzshen1
2012/12/29 02:04:05
GetLocalDataRestrictions -> GetLocalDataRestrictio
raymes
2012/12/29 03:21:57
Done.
| |
48 // it. | |
49 if (initialized_) { | |
50 GetLocalDataRestrictionsHelper(document_url, plugin_url, callback, | |
51 resource_context_); | |
52 } else { | |
53 BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE, | |
54 base::Bind(&FlashLSOSettingsHelper::GetResourceContext, this, | |
55 render_process_id_), | |
56 base::Bind(&FlashLSOSettingsHelper::GetLocalDataRestrictionsHelper, | |
57 this, document_url, plugin_url, callback)); | |
58 } | |
59 } | |
60 | |
61 void FlashLSOSettingsHelper::GetLocalDataRestrictionsHelper( | |
62 const GURL& document_url, | |
63 const GURL& plugin_url, | |
64 base::Callback<void(PP_FlashLSORestrictions)> callback, | |
65 ResourceContext* resource_context) { | |
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
67 | |
68 if (!initialized_) { | |
69 initialized_ = true; | |
70 resource_context_ = resource_context; | |
71 } | |
72 | |
73 if (!resource_context_ || | |
74 !document_url.is_valid() || !plugin_url.is_valid()) { | |
75 callback.Run(kDefaultLSORestrictions); | |
76 return; | |
77 } | |
78 | |
79 content::ContentBrowserClient* client = | |
80 content::GetContentClient()->browser(); | |
81 if (!client->AllowPluginLocalDataAccess(document_url, plugin_url, | |
82 resource_context_)) { | |
83 callback.Run(PP_FLASHLSORESTRICTIONS_BLOCK); | |
84 } else if (client->AllowPluginLocalDataSessionOnly(plugin_url, | |
85 resource_context_)) { | |
86 callback.Run(PP_FLASHLSORESTRICTIONS_IN_MEMORY); | |
87 } else { | |
88 callback.Run(PP_FLASHLSORESTRICTIONS_NONE); | |
89 } | |
90 } | |
91 | |
92 ResourceContext* FlashLSOSettingsHelper::GetResourceContext( | |
93 int render_process_id) { | |
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
95 RenderProcessHost* render_process_host = RenderProcessHost::FromID( | |
96 render_process_id); | |
97 if (!render_process_host || !render_process_host->GetBrowserContext()) | |
98 return NULL; | |
99 | |
100 Profile* profile = Profile::FromBrowserContext( | |
101 render_process_host->GetBrowserContext()); | |
102 ResourceContext* resource_context = profile->GetResourceContext(); | |
103 | |
104 // Register for a notification when the profile is destroyed so that we | |
105 // don't access the resource context after that. | |
106 if (registrar_.get()) { | |
107 // If |registrar_| is already set, we've already registered for a | |
108 // notification. | |
109 return resource_context; | |
110 } | |
111 registrar_.reset(new content::NotificationRegistrar); | |
112 registrar_->Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, | |
113 content::Source<Profile>(profile)); | |
114 // Hold a ref to ourselves until the profile is destroyed so that we always | |
yzshen1
2012/12/29 02:04:05
Why we want this to be alive for so long?
This hel
raymes
2012/12/29 03:21:57
Good idea. Let me know if my change was along the
| |
115 // receive the destruction notification. | |
116 AddRef(); | |
117 return resource_context; | |
118 } | |
119 | |
120 void FlashLSOSettingsHelper::Observe(int type, | |
121 const content::NotificationSource& source, | |
122 const content::NotificationDetails& details) { | |
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
124 DCHECK(type == chrome::NOTIFICATION_PROFILE_DESTROYED); | |
125 // The profile associated with this class has been destroyed. We can now | |
126 // unregister for notifications (by deleting |registrar_|) and post a task | |
127 // to clear the resource context so that it is no longer used. | |
128 registrar_.reset(); | |
129 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
130 base::Bind(&FlashLSOSettingsHelper::ClearResourceContext, this)); | |
yzshen1
2012/12/29 02:04:05
[I haven't checked. Just want to make sure you hav
raymes
2012/12/29 03:21:57
Yes - I made sure this is the case.
| |
131 Release(); // Balances the AddRef in GetResourceContext. | |
132 } | |
133 | |
134 void FlashLSOSettingsHelper::ClearResourceContext() { | |
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
136 resource_context_ = NULL; | |
137 } | |
138 | |
139 } // namespace chrome | |
OLD | NEW |