| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "extensions/renderer/worker_script_context_set.h" | 5 #include "extensions/renderer/worker_script_context_set.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "extensions/renderer/script_context.h" | 10 #include "extensions/renderer/script_context.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 DCHECK_GT(content::WorkerThread::GetCurrentId(), 0) | 37 DCHECK_GT(content::WorkerThread::GetCurrentId(), 0) |
| 38 << "Must be called on a worker thread"; | 38 << "Must be called on a worker thread"; |
| 39 ContextVector* contexts = contexts_tls_.Get(); | 39 ContextVector* contexts = contexts_tls_.Get(); |
| 40 if (!contexts) { | 40 if (!contexts) { |
| 41 // First context added for this thread. Create a new set, then wait for | 41 // First context added for this thread. Create a new set, then wait for |
| 42 // this thread's shutdown. | 42 // this thread's shutdown. |
| 43 contexts = new ContextVector(); | 43 contexts = new ContextVector(); |
| 44 contexts_tls_.Set(contexts); | 44 contexts_tls_.Set(contexts); |
| 45 content::WorkerThread::AddObserver(this); | 45 content::WorkerThread::AddObserver(this); |
| 46 } | 46 } |
| 47 CHECK(FindContext(contexts, context->v8_context()) == contexts->end()) | 47 // Worker for |context->url()| is already in this set |
| 48 << "Worker for " << context->url() << " is already in this set"; | 48 CHECK(FindContext(contexts, context->v8_context()) == contexts->end()); |
| 49 contexts->push_back(std::move(context)); | 49 contexts->push_back(std::move(context)); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void WorkerScriptContextSet::Remove(v8::Local<v8::Context> v8_context, | 52 void WorkerScriptContextSet::Remove(v8::Local<v8::Context> v8_context, |
| 53 const GURL& url) { | 53 const GURL& url) { |
| 54 DCHECK_GT(content::WorkerThread::GetCurrentId(), 0) | 54 DCHECK_GT(content::WorkerThread::GetCurrentId(), 0) |
| 55 << "Must be called on a worker thread"; | 55 << "Must be called on a worker thread"; |
| 56 ContextVector* contexts = contexts_tls_.Get(); | 56 ContextVector* contexts = contexts_tls_.Get(); |
| 57 if (!contexts) { | 57 if (!contexts) { |
| 58 // Thread has already been torn down, and |v8_context| removed. I'm not | 58 // Thread has already been torn down, and |v8_context| removed. I'm not |
| 59 // sure this can actually happen (depends on in what order blink fires | 59 // sure this can actually happen (depends on in what order blink fires |
| 60 // events), but SW lifetime has bitten us before, so be cautious. | 60 // events), but SW lifetime has bitten us before, so be cautious. |
| 61 return; | 61 return; |
| 62 } | 62 } |
| 63 auto context_it = FindContext(contexts, v8_context); | 63 auto context_it = FindContext(contexts, v8_context); |
| 64 CHECK(context_it != contexts->end()) << "Worker for " << url | 64 // Worker for |url| is not in this set |
| 65 << " is not in this set"; | 65 CHECK(context_it != contexts->end()); |
| 66 ScriptContext* context = *context_it; | 66 ScriptContext* context = *context_it; |
| 67 DCHECK_EQ(url, context->url()); | 67 DCHECK_EQ(url, context->url()); |
| 68 context->Invalidate(); | 68 context->Invalidate(); |
| 69 contexts->erase(context_it); | 69 contexts->erase(context_it); |
| 70 } | 70 } |
| 71 | 71 |
| 72 void WorkerScriptContextSet::WillStopCurrentWorkerThread() { | 72 void WorkerScriptContextSet::WillStopCurrentWorkerThread() { |
| 73 content::WorkerThread::RemoveObserver(this); | 73 content::WorkerThread::RemoveObserver(this); |
| 74 ContextVector* contexts = contexts_tls_.Get(); | 74 ContextVector* contexts = contexts_tls_.Get(); |
| 75 DCHECK(contexts); | 75 DCHECK(contexts); |
| 76 for (ScriptContext* context : *contexts) | 76 for (ScriptContext* context : *contexts) |
| 77 context->Invalidate(); | 77 context->Invalidate(); |
| 78 contexts_tls_.Set(nullptr); | 78 contexts_tls_.Set(nullptr); |
| 79 delete contexts; | 79 delete contexts; |
| 80 } | 80 } |
| 81 | 81 |
| 82 } // namespace extensions | 82 } // namespace extensions |
| OLD | NEW |