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 "chrome/renderer/visitedlink_slave.h"
7 #include "base/logging.h"
8 #include "base/shared_memory.h"
9 #include "chrome/common/render_messages.h"
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
12 using WebKit::WebView
;
14 VisitedLinkSlave::VisitedLinkSlave() : shared_memory_(NULL
) {
17 VisitedLinkSlave::~VisitedLinkSlave() {
21 bool VisitedLinkSlave::OnControlMessageReceived(const IPC::Message
& message
) {
23 IPC_BEGIN_MESSAGE_MAP(VisitedLinkSlave
, message
)
24 IPC_MESSAGE_HANDLER(ChromeViewMsg_VisitedLink_NewTable
,
26 IPC_MESSAGE_HANDLER(ChromeViewMsg_VisitedLink_Add
, OnAddVisitedLinks
)
27 IPC_MESSAGE_HANDLER(ChromeViewMsg_VisitedLink_Reset
, OnResetVisitedLinks
)
28 IPC_MESSAGE_UNHANDLED(handled
= false)
33 // This function's job is to initialize the table with the given
34 // shared memory handle. This memory is mapped into the process.
35 void VisitedLinkSlave::OnUpdateVisitedLinks(base::SharedMemoryHandle table
) {
36 DCHECK(base::SharedMemory::IsHandleValid(table
)) << "Bad table handle";
37 // since this function may be called again to change the table, we may need
38 // to free old objects
40 DCHECK(shared_memory_
== NULL
&& hash_table_
== NULL
);
42 // create the shared memory object
43 shared_memory_
= new base::SharedMemory(table
, true);
47 // map the header into our process so we can see how long the rest is,
49 if (!shared_memory_
->Map(sizeof(SharedHeader
)))
51 SharedHeader
* header
=
52 static_cast<SharedHeader
*>(shared_memory_
->memory());
54 int32 table_len
= header
->length
;
55 memcpy(salt_
, header
->salt
, sizeof(salt_
));
56 shared_memory_
->Unmap();
58 // now do the whole table because we know the length
59 if (!shared_memory_
->Map(sizeof(SharedHeader
) +
60 table_len
* sizeof(Fingerprint
))) {
61 shared_memory_
->Close();
66 DCHECK(shared_memory_
->memory());
67 hash_table_
= reinterpret_cast<Fingerprint
*>(
68 static_cast<char*>(shared_memory_
->memory()) + sizeof(SharedHeader
));
69 table_length_
= table_len
;
72 void VisitedLinkSlave::OnAddVisitedLinks(
73 const VisitedLinkSlave::Fingerprints
& fingerprints
) {
74 for (size_t i
= 0; i
< fingerprints
.size(); ++i
)
75 WebView::updateVisitedLinkState(fingerprints
[i
]);
78 void VisitedLinkSlave::OnResetVisitedLinks() {
79 WebView::resetVisitedLinkState();
82 void VisitedLinkSlave::FreeTable() {
84 delete shared_memory_
;
85 shared_memory_
= NULL
;