blob: 9f301d94f27721f2a2b5740481f2669b64f784a6 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.
initial.commit09911bf2008-07-26 23:55:294
[email protected]da4c5d12008-10-21 21:45:155#include "chrome/renderer/visitedlink_slave.h"
6
initial.commit09911bf2008-07-26 23:55:297#include "base/logging.h"
8#include "base/shared_memory.h"
[email protected]8d97ade2011-04-14 18:17:089#include "chrome/common/render_messages.h"
10#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
11
12using WebKit::WebView;
initial.commit09911bf2008-07-26 23:55:2913
14VisitedLinkSlave::VisitedLinkSlave() : shared_memory_(NULL) {
15}
[email protected]8d97ade2011-04-14 18:17:0816
initial.commit09911bf2008-07-26 23:55:2917VisitedLinkSlave::~VisitedLinkSlave() {
18 FreeTable();
19}
20
[email protected]8d97ade2011-04-14 18:17:0821bool VisitedLinkSlave::OnControlMessageReceived(const IPC::Message& message) {
22 bool handled = true;
23 IPC_BEGIN_MESSAGE_MAP(VisitedLinkSlave, message)
24 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_NewTable, OnUpdateVisitedLinks)
25 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_Add, OnAddVisitedLinks)
26 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_Reset, OnResetVisitedLinks)
27 IPC_MESSAGE_UNHANDLED(handled = false)
28 IPC_END_MESSAGE_MAP()
29 return handled;
30}
31
initial.commit09911bf2008-07-26 23:55:2932// This function's job is to initialize the table with the given
[email protected]8d97ade2011-04-14 18:17:0833// shared memory handle. This memory is mapped into the process.
34void VisitedLinkSlave::OnUpdateVisitedLinks(base::SharedMemoryHandle table) {
35 DCHECK(base::SharedMemory::IsHandleValid(table)) << "Bad table handle";
initial.commit09911bf2008-07-26 23:55:2936 // since this function may be called again to change the table, we may need
37 // to free old objects
38 FreeTable();
39 DCHECK(shared_memory_ == NULL && hash_table_ == NULL);
40
41 // create the shared memory object
[email protected]8d97ade2011-04-14 18:17:0842 shared_memory_ = new base::SharedMemory(table, true);
initial.commit09911bf2008-07-26 23:55:2943 if (!shared_memory_)
[email protected]8d97ade2011-04-14 18:17:0844 return;
initial.commit09911bf2008-07-26 23:55:2945
46 // map the header into our process so we can see how long the rest is,
47 // and set the salt
48 if (!shared_memory_->Map(sizeof(SharedHeader)))
[email protected]8d97ade2011-04-14 18:17:0849 return;
initial.commit09911bf2008-07-26 23:55:2950 SharedHeader* header =
51 static_cast<SharedHeader*>(shared_memory_->memory());
52 DCHECK(header);
53 int32 table_len = header->length;
54 memcpy(salt_, header->salt, sizeof(salt_));
55 shared_memory_->Unmap();
56
57 // now do the whole table because we know the length
58 if (!shared_memory_->Map(sizeof(SharedHeader) +
59 table_len * sizeof(Fingerprint))) {
60 shared_memory_->Close();
[email protected]8d97ade2011-04-14 18:17:0861 return;
initial.commit09911bf2008-07-26 23:55:2962 }
63
64 // commit the data
65 DCHECK(shared_memory_->memory());
66 hash_table_ = reinterpret_cast<Fingerprint*>(
67 static_cast<char*>(shared_memory_->memory()) + sizeof(SharedHeader));
68 table_length_ = table_len;
[email protected]8d97ade2011-04-14 18:17:0869}
70
71void VisitedLinkSlave::OnAddVisitedLinks(
72 const VisitedLinkSlave::Fingerprints& fingerprints) {
73 for (size_t i = 0; i < fingerprints.size(); ++i)
74 WebView::updateVisitedLinkState(fingerprints[i]);
75}
76
77void VisitedLinkSlave::OnResetVisitedLinks() {
78 WebView::resetVisitedLinkState();
initial.commit09911bf2008-07-26 23:55:2979}
80
81void VisitedLinkSlave::FreeTable() {
[email protected]4a3dab22009-11-11 17:36:5082 if (shared_memory_) {
initial.commit09911bf2008-07-26 23:55:2983 delete shared_memory_;
84 shared_memory_ = NULL;
85 }
86 hash_table_ = NULL;
87 table_length_ = 0;
88}