Chromium Code Reviews| 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) {} |
| 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 void VisitedLinkSlave::Bind(mojom::VisitedLinkNotificationSinkRequest request) { |
| 26 bool handled = true; | 24 binding_.Bind(std::move(request)); |
| 27 IPC_BEGIN_MESSAGE_MAP(VisitedLinkSlave, message) | |
| 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 } | 25 } |
| 36 | 26 |
| 37 // This function's job is to initialize the table with the given | 27 // This function's job is to initialize the table with the given |
| 38 // shared memory handle. This memory is mapped into the process. | 28 // shared memory handle. This memory is mapped into the process. |
| 39 void VisitedLinkSlave::OnUpdateVisitedLinks(base::SharedMemoryHandle table) { | 29 void VisitedLinkSlave::UpdateVisitedLinks( |
| 40 DCHECK(base::SharedMemory::IsHandleValid(table)) << "Bad table handle"; | 30 mojo::ScopedSharedBufferHandle table) { |
| 31 DCHECK(table.is_valid()) << "Bad table handle"; | |
| 41 // since this function may be called again to change the table, we may need | 32 // since this function may be called again to change the table, we may need |
| 42 // to free old objects | 33 // to free old objects |
| 43 FreeTable(); | 34 FreeTable(); |
| 44 DCHECK(shared_memory_ == NULL && hash_table_ == NULL); | 35 DCHECK(hash_table_ == NULL); |
| 45 | 36 |
| 46 // create the shared memory object | 37 int32_t table_len = 0; |
| 47 shared_memory_ = new base::SharedMemory(table, true); | 38 { |
| 48 if (!shared_memory_) | 39 // map the header into our process so we can see how long the rest is, |
|
brettw
2016/09/15 22:33:08
Check that the new comments you added (also below)
Sam McNally
2016/09/19 00:28:19
Done.
| |
| 40 // and set the salt | |
| 41 mojo::ScopedSharedBufferMapping header_memory = | |
| 42 table->Map(sizeof(SharedHeader)); | |
| 43 if (!header_memory) | |
| 44 return; | |
| 45 | |
| 46 SharedHeader* header = static_cast<SharedHeader*>(header_memory.get()); | |
| 47 table_len = header->length; | |
| 48 memcpy(salt_, header->salt, sizeof(salt_)); | |
| 49 } | |
| 50 | |
| 51 // now do the table contents because we know the length | |
| 52 table_mapping_ = | |
| 53 table->MapAtOffset(table_len * sizeof(Fingerprint), sizeof(SharedHeader)); | |
| 54 if (!table_mapping_) | |
| 49 return; | 55 return; |
| 50 | 56 |
| 51 // map the header into our process so we can see how long the rest is, | |
| 52 // and set the salt | |
| 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 | 57 // commit the data |
| 70 DCHECK(shared_memory_->memory()); | 58 hash_table_ = reinterpret_cast<Fingerprint*>(table_mapping_.get()); |
| 71 hash_table_ = reinterpret_cast<Fingerprint*>( | |
| 72 static_cast<char*>(shared_memory_->memory()) + sizeof(SharedHeader)); | |
| 73 table_length_ = table_len; | 59 table_length_ = table_len; |
| 74 } | 60 } |
| 75 | 61 |
| 76 void VisitedLinkSlave::OnAddVisitedLinks( | 62 void VisitedLinkSlave::AddVisitedLinks( |
| 77 const VisitedLinkSlave::Fingerprints& fingerprints) { | 63 const std::vector<VisitedLinkSlave::Fingerprint>& fingerprints) { |
| 78 for (size_t i = 0; i < fingerprints.size(); ++i) | 64 for (size_t i = 0; i < fingerprints.size(); ++i) |
| 79 WebView::updateVisitedLinkState(fingerprints[i]); | 65 WebView::updateVisitedLinkState(fingerprints[i]); |
| 80 } | 66 } |
| 81 | 67 |
| 82 void VisitedLinkSlave::OnResetVisitedLinks(bool invalidate_hashes) { | 68 void VisitedLinkSlave::ResetVisitedLinks(bool invalidate_hashes) { |
| 83 WebView::resetVisitedLinkState(invalidate_hashes); | 69 WebView::resetVisitedLinkState(invalidate_hashes); |
| 84 } | 70 } |
| 85 | 71 |
| 86 void VisitedLinkSlave::FreeTable() { | 72 void VisitedLinkSlave::FreeTable() { |
| 87 if (shared_memory_) { | 73 if (!hash_table_) |
| 88 delete shared_memory_; | 74 return; |
| 89 shared_memory_ = NULL; | 75 |
| 90 } | 76 table_mapping_.reset(); |
| 91 hash_table_ = NULL; | 77 hash_table_ = NULL; |
| 92 table_length_ = 0; | 78 table_length_ = 0; |
| 93 } | 79 } |
| 94 | 80 |
| 95 } // namespace visitedlink | 81 } // namespace visitedlink |
| OLD | NEW |