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.
5 #include "chrome/browser/renderer_host/pepper/pepper_flash_device_id_host.h"
8 #include "base/compiler_specific.h"
9 #include "base/file_util.h"
10 #include "base/logging.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/browser_ppapi_host.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "ppapi/c/pp_errors.h"
17 #include "ppapi/host/dispatch_host_message.h"
18 #include "ppapi/host/ppapi_host.h"
19 #include "ppapi/proxy/ppapi_messages.h"
21 using content::BrowserPpapiHost
;
22 using content::BrowserThread
;
28 // The path to the file containing the DRM ID.
29 // It is mirrored from
30 // chrome/browser/chromeos/system/drm_settings.cc
31 const char kDRMIdentifierFile
[] = "Pepper DRM ID.0";
35 // PepperFlashDeviceIDHost::Fetcher --------------------------------------------
37 PepperFlashDeviceIDHost::Fetcher::Fetcher(
38 const base::WeakPtr
<PepperFlashDeviceIDHost
>& host
)
40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
43 PepperFlashDeviceIDHost::Fetcher::~Fetcher() {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
47 void PepperFlashDeviceIDHost::Fetcher::Start(BrowserPpapiHost
* browser_host
) {
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
52 browser_host
->GetRenderViewIDsForInstance(host_
->pp_instance(),
55 BrowserThread::PostTask(
56 BrowserThread::UI
, FROM_HERE
,
57 base::Bind(&PepperFlashDeviceIDHost::Fetcher::GetFilePathOnUIThread
,
58 this, process_id
, view_id
));
61 void PepperFlashDeviceIDHost::Fetcher::GetFilePathOnUIThread(
62 int render_process_id
,
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
67 content::RenderViewHost
* render_view_host
=
68 content::RenderViewHost::FromID(render_process_id
, render_view_id
);
69 if (render_view_host
) {
70 content::BrowserContext
* browser_context
=
71 render_view_host
->GetProcess()->GetBrowserContext();
72 // Don't use DRM IDs when incognito to prevent tracking.
73 if (!browser_context
->IsOffTheRecord())
74 path
= browser_context
->GetPath().AppendASCII(kDRMIdentifierFile
);
77 // Continue on the file thread (on failure/incognito, this will just send
78 // the empty path which will forward the error).
79 BrowserThread::PostTask(
80 BrowserThread::FILE, FROM_HERE
,
81 base::Bind(&PepperFlashDeviceIDHost::Fetcher::ReadDRMFileOnFileThread
,
85 void PepperFlashDeviceIDHost::Fetcher::ReadDRMFileOnFileThread(
86 const base::FilePath
& path
) {
87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
91 file_util::ReadFileToString(path
, &contents
);
93 // Give back to the resource host on the IO thread. On failure this will just
94 // forward the empty string.
95 BrowserThread::PostTask(
96 BrowserThread::IO
, FROM_HERE
,
97 base::Bind(&PepperFlashDeviceIDHost::Fetcher::ReplyOnIOThread
,
101 void PepperFlashDeviceIDHost::Fetcher::ReplyOnIOThread(
102 const std::string
& contents
) {
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
104 if (host_
.get()) // Plugin or resource could have been deleted.
105 host_
->GotDRMContents(contents
);
108 // PepperFlashDeviceIDHost -----------------------------------------------------
110 PepperFlashDeviceIDHost::PepperFlashDeviceIDHost(BrowserPpapiHost
* host
,
111 PP_Instance instance
,
112 PP_Resource resource
)
113 : ppapi::host::ResourceHost(host
->GetPpapiHost(), instance
, resource
),
114 factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
115 browser_ppapi_host_(host
) {
118 PepperFlashDeviceIDHost::~PepperFlashDeviceIDHost() {
121 int32_t PepperFlashDeviceIDHost::OnResourceMessageReceived(
122 const IPC::Message
& msg
,
123 ppapi::host::HostMessageContext
* context
) {
124 IPC_BEGIN_MESSAGE_MAP(PepperFlashDeviceIDHost
, msg
)
125 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FlashDeviceID_GetDeviceID
,
126 OnHostMsgGetDeviceID
)
127 IPC_END_MESSAGE_MAP()
128 return PP_ERROR_FAILED
;
131 int32_t PepperFlashDeviceIDHost::OnHostMsgGetDeviceID(
132 const ppapi::host::HostMessageContext
* context
) {
134 return PP_ERROR_INPROGRESS
;
136 fetcher_
= new Fetcher(factory_
.GetWeakPtr());
137 fetcher_
->Start(browser_ppapi_host_
);
138 reply_context_
= context
->MakeReplyMessageContext();
139 return PP_OK_COMPLETIONPENDING
;
142 void PepperFlashDeviceIDHost::GotDRMContents(const std::string
& contents
) {
144 reply_context_
.params
.set_result(
145 contents
.empty() ? PP_ERROR_FAILED
: PP_OK
);
146 host()->SendReply(reply_context_
,
147 PpapiPluginMsg_FlashDeviceID_GetDeviceIDReply(contents
));
150 } // namespace chrome