license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | da4c5d1 | 2008-10-21 21:45:15 | [diff] [blame] | 5 | #include "chrome/renderer/visitedlink_slave.h" |
| 6 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | #include "base/logging.h" |
| 8 | #include "base/shared_memory.h" |
[email protected] | 8d97ade | 2011-04-14 18:17:08 | [diff] [blame] | 9 | #include "chrome/common/render_messages.h" |
| 10 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 11 | |
| 12 | using WebKit::WebView; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | |
| 14 | VisitedLinkSlave::VisitedLinkSlave() : shared_memory_(NULL) { |
| 15 | } |
[email protected] | 8d97ade | 2011-04-14 18:17:08 | [diff] [blame] | 16 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | VisitedLinkSlave::~VisitedLinkSlave() { |
| 18 | FreeTable(); |
| 19 | } |
| 20 | |
[email protected] | 8d97ade | 2011-04-14 18:17:08 | [diff] [blame] | 21 | bool 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | // This function's job is to initialize the table with the given |
[email protected] | 8d97ade | 2011-04-14 18:17:08 | [diff] [blame] | 33 | // shared memory handle. This memory is mapped into the process. |
| 34 | void VisitedLinkSlave::OnUpdateVisitedLinks(base::SharedMemoryHandle table) { |
| 35 | DCHECK(base::SharedMemory::IsHandleValid(table)) << "Bad table handle"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | // 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] | 8d97ade | 2011-04-14 18:17:08 | [diff] [blame] | 42 | shared_memory_ = new base::SharedMemory(table, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | if (!shared_memory_) |
[email protected] | 8d97ade | 2011-04-14 18:17:08 | [diff] [blame] | 44 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | |
| 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] | 8d97ade | 2011-04-14 18:17:08 | [diff] [blame] | 49 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | 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] | 8d97ade | 2011-04-14 18:17:08 | [diff] [blame] | 61 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | } |
| 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] | 8d97ade | 2011-04-14 18:17:08 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void VisitedLinkSlave::OnAddVisitedLinks( |
| 72 | const VisitedLinkSlave::Fingerprints& fingerprints) { |
| 73 | for (size_t i = 0; i < fingerprints.size(); ++i) |
| 74 | WebView::updateVisitedLinkState(fingerprints[i]); |
| 75 | } |
| 76 | |
| 77 | void VisitedLinkSlave::OnResetVisitedLinks() { |
| 78 | WebView::resetVisitedLinkState(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void VisitedLinkSlave::FreeTable() { |
[email protected] | 4a3dab2 | 2009-11-11 17:36:50 | [diff] [blame] | 82 | if (shared_memory_) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | delete shared_memory_; |
| 84 | shared_memory_ = NULL; |
| 85 | } |
| 86 | hash_table_ = NULL; |
| 87 | table_length_ = 0; |
| 88 | } |