1 // Copyright (c) 2006-2008 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 "components/visitedlink/renderer/visitedlink_slave.h"
7 #include "base/logging.h"
8 #include "base/memory/shared_memory.h"
9 #include "components/visitedlink/common/visitedlink_messages.h"
10 #include "third_party/WebKit/public/web/WebView.h"
14 namespace visitedlink
{
16 VisitedLinkSlave::VisitedLinkSlave() : shared_memory_(NULL
) {}
18 VisitedLinkSlave::~VisitedLinkSlave() {
22 bool VisitedLinkSlave::OnControlMessageReceived(const IPC::Message
& message
) {
24 IPC_BEGIN_MESSAGE_MAP(VisitedLinkSlave
, message
)
25 IPC_MESSAGE_HANDLER(ChromeViewMsg_VisitedLink_NewTable
,
27 IPC_MESSAGE_HANDLER(ChromeViewMsg_VisitedLink_Add
, OnAddVisitedLinks
)
28 IPC_MESSAGE_HANDLER(ChromeViewMsg_VisitedLink_Reset
, OnResetVisitedLinks
)
29 IPC_MESSAGE_UNHANDLED(handled
= false)
34 // This function's job is to initialize the table with the given
35 // shared memory handle. This memory is mapped into the process.
36 void VisitedLinkSlave::OnUpdateVisitedLinks(base::SharedMemoryHandle table
) {
37 DCHECK(base::SharedMemory::IsHandleValid(table
)) << "Bad table handle";
38 // since this function may be called again to change the table, we may need
39 // to free old objects
41 DCHECK(shared_memory_
== NULL
&& hash_table_
== NULL
);
43 // create the shared memory object
44 shared_memory_
= new base::SharedMemory(table
, true);
48 // map the header into our process so we can see how long the rest is,
50 if (!shared_memory_
->Map(sizeof(SharedHeader
)))
52 SharedHeader
* header
=
53 static_cast<SharedHeader
*>(shared_memory_
->memory());
55 int32 table_len
= header
->length
;
56 memcpy(salt_
, header
->salt
, sizeof(salt_
));
57 shared_memory_
->Unmap();
59 // now do the whole table because we know the length
60 if (!shared_memory_
->Map(sizeof(SharedHeader
) +
61 table_len
* sizeof(Fingerprint
))) {
62 shared_memory_
->Close();
67 DCHECK(shared_memory_
->memory());
68 hash_table_
= reinterpret_cast<Fingerprint
*>(
69 static_cast<char*>(shared_memory_
->memory()) + sizeof(SharedHeader
));
70 table_length_
= table_len
;
73 void VisitedLinkSlave::OnAddVisitedLinks(
74 const VisitedLinkSlave::Fingerprints
& fingerprints
) {
75 for (size_t i
= 0; i
< fingerprints
.size(); ++i
)
76 WebView::updateVisitedLinkState(fingerprints
[i
]);
79 void VisitedLinkSlave::OnResetVisitedLinks() {
80 WebView::resetVisitedLinkState();
83 void VisitedLinkSlave::FreeTable() {
85 delete shared_memory_
;
86 shared_memory_
= NULL
;
92 } // namespace visitedlink