| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/visitedlink/renderer/visitedlink_slave.h" | 5 #include "components/visitedlink/renderer/visitedlink_slave.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/shared_memory.h" | |
| 12 #include "components/visitedlink/common/visitedlink_messages.h" | |
| 13 #include "third_party/WebKit/public/web/WebView.h" | 11 #include "third_party/WebKit/public/web/WebView.h" |
| 14 | 12 |
| 15 using blink::WebView; | 13 using blink::WebView; |
| 16 | 14 |
| 17 namespace visitedlink { | 15 namespace visitedlink { |
| 18 | 16 |
| 19 VisitedLinkSlave::VisitedLinkSlave() : shared_memory_(NULL) {} | 17 VisitedLinkSlave::VisitedLinkSlave() : binding_(this), weak_factory_(this) {} |
| 20 | 18 |
| 21 VisitedLinkSlave::~VisitedLinkSlave() { | 19 VisitedLinkSlave::~VisitedLinkSlave() { |
| 22 FreeTable(); | 20 FreeTable(); |
| 23 } | 21 } |
| 24 | 22 |
| 25 bool VisitedLinkSlave::OnControlMessageReceived(const IPC::Message& message) { | 23 base::Callback<void(mojom::VisitedLinkNotificationSinkRequest)> |
| 26 bool handled = true; | 24 VisitedLinkSlave::GetBindCallback() { |
| 27 IPC_BEGIN_MESSAGE_MAP(VisitedLinkSlave, message) | 25 return base::Bind(&VisitedLinkSlave::Bind, weak_factory_.GetWeakPtr()); |
| 28 IPC_MESSAGE_HANDLER(ChromeViewMsg_VisitedLink_NewTable, | |
| 29 OnUpdateVisitedLinks) | |
| 30 IPC_MESSAGE_HANDLER(ChromeViewMsg_VisitedLink_Add, OnAddVisitedLinks) | |
| 31 IPC_MESSAGE_HANDLER(ChromeViewMsg_VisitedLink_Reset, OnResetVisitedLinks) | |
| 32 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 33 IPC_END_MESSAGE_MAP() | |
| 34 return handled; | |
| 35 } | 26 } |
| 36 | 27 |
| 37 // This function's job is to initialize the table with the given | 28 // Initializes the table with the given shared memory handle. This memory is |
| 38 // shared memory handle. This memory is mapped into the process. | 29 // mapped into the process. |
| 39 void VisitedLinkSlave::OnUpdateVisitedLinks(base::SharedMemoryHandle table) { | 30 void VisitedLinkSlave::UpdateVisitedLinks( |
| 40 DCHECK(base::SharedMemory::IsHandleValid(table)) << "Bad table handle"; | 31 mojo::ScopedSharedBufferHandle table) { |
| 41 // since this function may be called again to change the table, we may need | 32 DCHECK(table.is_valid()) << "Bad table handle"; |
| 42 // to free old objects | 33 // Since this function may be called again to change the table, we may need |
| 34 // to free old objects. |
| 43 FreeTable(); | 35 FreeTable(); |
| 44 DCHECK(shared_memory_ == NULL && hash_table_ == NULL); | 36 DCHECK(hash_table_ == NULL); |
| 45 | 37 |
| 46 // create the shared memory object | 38 int32_t table_len = 0; |
| 47 shared_memory_ = new base::SharedMemory(table, true); | 39 { |
| 48 if (!shared_memory_) | 40 // Map the header into our process so we can see how long the rest is, |
| 41 // and set the salt. |
| 42 mojo::ScopedSharedBufferMapping header_memory = |
| 43 table->Map(sizeof(SharedHeader)); |
| 44 if (!header_memory) |
| 45 return; |
| 46 |
| 47 SharedHeader* header = static_cast<SharedHeader*>(header_memory.get()); |
| 48 table_len = header->length; |
| 49 memcpy(salt_, header->salt, sizeof(salt_)); |
| 50 } |
| 51 |
| 52 // Now we know the length, so map the table contents. |
| 53 table_mapping_ = |
| 54 table->MapAtOffset(table_len * sizeof(Fingerprint), sizeof(SharedHeader)); |
| 55 if (!table_mapping_) |
| 49 return; | 56 return; |
| 50 | 57 |
| 51 // map the header into our process so we can see how long the rest is, | 58 // Commit the data. |
| 52 // and set the salt | 59 hash_table_ = reinterpret_cast<Fingerprint*>(table_mapping_.get()); |
| 53 if (!shared_memory_->Map(sizeof(SharedHeader))) | |
| 54 return; | |
| 55 SharedHeader* header = | |
| 56 static_cast<SharedHeader*>(shared_memory_->memory()); | |
| 57 DCHECK(header); | |
| 58 int32_t table_len = header->length; | |
| 59 memcpy(salt_, header->salt, sizeof(salt_)); | |
| 60 shared_memory_->Unmap(); | |
| 61 | |
| 62 // now do the whole table because we know the length | |
| 63 if (!shared_memory_->Map(sizeof(SharedHeader) + | |
| 64 table_len * sizeof(Fingerprint))) { | |
| 65 shared_memory_->Close(); | |
| 66 return; | |
| 67 } | |
| 68 | |
| 69 // commit the data | |
| 70 DCHECK(shared_memory_->memory()); | |
| 71 hash_table_ = reinterpret_cast<Fingerprint*>( | |
| 72 static_cast<char*>(shared_memory_->memory()) + sizeof(SharedHeader)); | |
| 73 table_length_ = table_len; | 60 table_length_ = table_len; |
| 74 } | 61 } |
| 75 | 62 |
| 76 void VisitedLinkSlave::OnAddVisitedLinks( | 63 void VisitedLinkSlave::AddVisitedLinks( |
| 77 const VisitedLinkSlave::Fingerprints& fingerprints) { | 64 const std::vector<VisitedLinkSlave::Fingerprint>& fingerprints) { |
| 78 for (size_t i = 0; i < fingerprints.size(); ++i) | 65 for (size_t i = 0; i < fingerprints.size(); ++i) |
| 79 WebView::updateVisitedLinkState(fingerprints[i]); | 66 WebView::updateVisitedLinkState(fingerprints[i]); |
| 80 } | 67 } |
| 81 | 68 |
| 82 void VisitedLinkSlave::OnResetVisitedLinks(bool invalidate_hashes) { | 69 void VisitedLinkSlave::ResetVisitedLinks(bool invalidate_hashes) { |
| 83 WebView::resetVisitedLinkState(invalidate_hashes); | 70 WebView::resetVisitedLinkState(invalidate_hashes); |
| 84 } | 71 } |
| 85 | 72 |
| 86 void VisitedLinkSlave::FreeTable() { | 73 void VisitedLinkSlave::FreeTable() { |
| 87 if (shared_memory_) { | 74 if (!hash_table_) |
| 88 delete shared_memory_; | 75 return; |
| 89 shared_memory_ = NULL; | 76 |
| 90 } | 77 table_mapping_.reset(); |
| 91 hash_table_ = NULL; | 78 hash_table_ = NULL; |
| 92 table_length_ = 0; | 79 table_length_ = 0; |
| 93 } | 80 } |
| 94 | 81 |
| 82 void VisitedLinkSlave::Bind(mojom::VisitedLinkNotificationSinkRequest request) { |
| 83 binding_.Bind(std::move(request)); |
| 84 } |
| 85 |
| 95 } // namespace visitedlink | 86 } // namespace visitedlink |
| OLD | NEW |