Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. | 3 // LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/in_process_webkit/webkit_context.h" | 5 #include "chrome/browser/in_process_webkit/webkit_context.h" |
| 6 | 6 |
| 7 #include "chrome/browser/chrome_thread.h" | 7 #include "chrome/browser/chrome_thread.h" |
| 8 | 8 |
| 9 WebKitContext::WebKitContext(const FilePath& data_path, bool is_incognito) | 9 WebKitContext::WebKitContext(const FilePath& data_path, bool is_incognito) |
| 10 : data_path_(data_path), | 10 : data_path_(data_path), |
| 11 is_incognito_(is_incognito), | 11 is_incognito_(is_incognito), |
| 12 ALLOW_THIS_IN_INITIALIZER_LIST( | 12 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 13 dom_storage_context_(new DOMStorageContext(this))) { | 13 dom_storage_context_(new DOMStorageContext(this))) { |
| 14 } | 14 } |
| 15 | 15 |
| 16 WebKitContext::~WebKitContext() { | 16 WebKitContext::~WebKitContext() { |
| 17 // If the WebKit thread was ever spun up, delete the object there. If we're | 17 // If the WebKit thread was ever spun up, delete the object there. The task |
| 18 // on the IO thread, this is safe because the WebKit thread goes away after | 18 // will just get deleted if the WebKit thread isn't created. |
| 19 // the IO. If we're on the UI thread, we're safe because the UI thread kills | 19 DOMStorageContext* dom_storage_context = dom_storage_context_.release(); |
| 20 // the WebKit thread. | 20 if (!ChromeThread::DeleteSoon( |
| 21 MessageLoop* webkit_loop = ChromeThread::GetMessageLoop(ChromeThread::WEBKIT); | 21 ChromeThread::WEBKIT, FROM_HERE, dom_storage_context)) { |
| 22 if (webkit_loop) | 22 // The WebKit thread wasn't created, and the task got deleted without |
| 23 webkit_loop->DeleteSoon(FROM_HERE, dom_storage_context_.release()); | 23 // freeing the DOMStorageContext, so delete it manually. |
| 24 delete dom_storage_context; | |
|
darin (slow to review)
2009/10/27 00:06:52
are you sure it is safe to delete this object here
jam
2009/10/27 02:38:18
This preserves the previous logic.
If there's no
| |
| 25 } | |
| 24 } | 26 } |
| 25 | 27 |
| 26 void WebKitContext::PurgeMemory() { | 28 void WebKitContext::PurgeMemory() { |
| 27 // DOMStorageContext::PurgeMemory() should only be called on the WebKit | 29 // DOMStorageContext::PurgeMemory() should only be called on the WebKit |
| 28 // thread. | 30 // thread. |
| 29 // | 31 // |
| 30 // Note that if there is no WebKit thread, then there's nothing in | 32 // Note that if there is no WebKit thread, then there's nothing in |
| 31 // LocalStorage and it's OK to no-op here. Further note that in a unittest, | 33 // LocalStorage and it's OK to no-op here. |
| 32 // there may be no threads at all, in which case MessageLoop::current() will | 34 if (ChromeThread::CurrentlyOn(ChromeThread::WEBKIT)) { |
| 33 // also be NULL and we'll go ahead and call PurgeMemory() directly, which is | |
| 34 // probably what the test wants. | |
| 35 MessageLoop* webkit_loop = ChromeThread::GetMessageLoop(ChromeThread::WEBKIT); | |
| 36 if (MessageLoop::current() == webkit_loop) { | |
| 37 dom_storage_context_->PurgeMemory(); | 35 dom_storage_context_->PurgeMemory(); |
| 38 } else if (webkit_loop) { | 36 } else { |
| 39 // Since we're not on the WebKit thread, proxy the call over to it. We | 37 // Since we're not on the WebKit thread, proxy the call over to it. We |
| 40 // can't post a task to call DOMStorageContext::PurgeMemory() directly | 38 // can't post a task to call DOMStorageContext::PurgeMemory() directly |
| 41 // because that class is not refcounted. | 39 // because that class is not refcounted. |
| 42 webkit_loop->PostTask(FROM_HERE, | 40 ChromeThread::PostTask( |
| 43 NewRunnableMethod(this, &WebKitContext::PurgeMemory)); | 41 ChromeThread::WEBKIT, FROM_HERE, |
| 42 NewRunnableMethod(this, &WebKitContext::PurgeMemory)); | |
| 44 } | 43 } |
| 45 } | 44 } |
| OLD | NEW |