OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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. |
| 4 |
| 5 #include "chrome/renderer/greasemonkey_slave.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/pickle.h" |
| 9 #include "base/shared_memory.h" |
| 10 |
| 11 GreasemonkeySlave::GreasemonkeySlave() : shared_memory_(NULL) { |
| 12 } |
| 13 |
| 14 bool GreasemonkeySlave::UpdateScripts(SharedMemoryHandle shared_memory) { |
| 15 scripts_.clear(); |
| 16 |
| 17 // Create the shared memory object. |
| 18 shared_memory_.reset(new SharedMemory(shared_memory, true)); // read-only |
| 19 if (!shared_memory_.get()) |
| 20 return false; |
| 21 |
| 22 // First get the size of the memory block. |
| 23 if (!shared_memory_->Map(sizeof(Pickle::Header))) |
| 24 return false; |
| 25 Pickle::Header* pickle_header = |
| 26 reinterpret_cast<Pickle::Header*>(shared_memory_->memory()); |
| 27 |
| 28 // Now map in the rest of the block. |
| 29 int pickle_size = sizeof(Pickle::Header) + pickle_header->payload_size; |
| 30 shared_memory_->Unmap(); |
| 31 if (!shared_memory_->Map(pickle_size)) |
| 32 return false; |
| 33 |
| 34 // Unpickle scripts. |
| 35 void* iter = NULL; |
| 36 int num_scripts = 0; |
| 37 Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()), |
| 38 pickle_size); |
| 39 pickle.ReadInt(&iter, &num_scripts); |
| 40 |
| 41 for (int i = 0; i < num_scripts; ++i) { |
| 42 const char* data = NULL; |
| 43 int data_length = 0; |
| 44 pickle.ReadData(&iter, &data, &data_length); |
| 45 |
| 46 GreasemonkeyScript script; |
| 47 if (script.Parse(StringPiece(data, data_length))) { |
| 48 scripts_.push_back(script); |
| 49 } |
| 50 } |
| 51 |
| 52 return true; |
| 53 } |
| 54 |
| 55 bool GreasemonkeySlave::InjectScripts(WebFrame* frame) { |
| 56 // TODO(aa): Check script patterns here |
| 57 |
| 58 for (std::vector<GreasemonkeyScript>::iterator script = scripts_.begin(); |
| 59 script != scripts_.end(); ++script) { |
| 60 // TODO(aa): Pass in URL to script when we have it. |
| 61 frame->ExecuteJavaScript(script->GetBody().as_string(), |
| 62 "Greasemonkey Script"); |
| 63 } |
| 64 |
| 65 return true; |
| 66 } |
OLD | NEW |