Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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 #include "chrome/browser/extensions/api/systeminfo_cpu/systeminfo_cpu_api.h" | |
| 5 | |
| 6 #include "base/logging.h" | |
| 7 #include "content/public/browser/browser_thread.h" | |
| 8 #include "chrome/browser/extensions/api/systeminfo_cpu/cpu_info_provider.h" | |
| 9 | |
| 10 namespace extensions { | |
| 11 using api::experimental_systeminfo_cpu::CpuInfo; | |
| 12 using api::experimental_systeminfo_cpu::CpuCoreInfo; | |
| 13 using content::BrowserThread; | |
| 14 | |
| 15 SysteminfoCpuGetFunction::SysteminfoCpuGetFunction() { | |
| 16 } | |
| 17 | |
| 18 SysteminfoCpuGetFunction::~SysteminfoCpuGetFunction() { | |
| 19 // Delete the provider that was created on FILE thread. | |
| 20 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, provider_); | |
| 21 } | |
| 22 | |
| 23 bool SysteminfoCpuGetFunction::RunImpl() { | |
| 24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 25 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 26 base::Bind(&SysteminfoCpuGetFunction::WorkOnFileThread, this)); | |
| 27 return true; | |
| 28 } | |
| 29 | |
| 30 void SysteminfoCpuGetFunction::GetCpuInfoOnFileThread() { | |
|
Mihai Parparita -not on Chrome
2012/08/10 20:24:56
Can you move this to be below WorkOnFileThread, so
Hongbo Min
2012/08/12 14:22:44
Done.
| |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 32 bool success = false; | |
| 33 | |
| 34 CpuInfo info; | |
| 35 if (provider_ && provider_->GetCpuInfo(&info)) { | |
| 36 SetResult(info.ToValue().release()); | |
| 37 success = true; | |
| 38 } else { | |
| 39 SetError("Error in querying cpu information!"); | |
| 40 } | |
| 41 // Respond on UI thread. | |
| 42 BrowserThread::PostTask( | |
| 43 BrowserThread::UI, FROM_HERE, | |
| 44 base::Bind(&SysteminfoCpuGetFunction::RespondOnUIThread, this, success)); | |
| 45 } | |
| 46 | |
| 47 void SysteminfoCpuGetFunction::WorkOnFileThread() { | |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 49 provider_ = CpuInfoProvider::Create(); | |
|
Mihai Parparita -not on Chrome
2012/08/10 20:24:56
It seems expensive to create a provider per functi
Hongbo Min
2012/08/12 14:22:44
Yes, it makes lots of senses to make it as a singl
| |
| 50 // Delay 500ms to give a chance for CPU information sampling. | |
|
Mihai Parparita -not on Chrome
2012/08/10 20:24:56
The 500ms delay seems arbitrary and prone to flaki
Hongbo Min
2012/08/12 14:22:44
The 500ms is a tricky for getting cpu usage per co
| |
| 51 BrowserThread::PostDelayedTask( | |
| 52 BrowserThread::FILE, FROM_HERE, | |
| 53 base::Bind(&SysteminfoCpuGetFunction::GetCpuInfoOnFileThread, this), | |
| 54 base::TimeDelta::FromMilliseconds(500)); | |
| 55 } | |
| 56 | |
| 57 void SysteminfoCpuGetFunction::RespondOnUIThread(bool success) { | |
| 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 59 SendResponse(success); | |
| 60 } | |
| 61 | |
| 62 // A mock implementation of CpuInfoProver for temporary usage. | |
| 63 // Will be moved out when the platform specific implementation are done. | |
| 64 class MockCpuInfoProvider : public CpuInfoProvider { | |
| 65 public: | |
| 66 MockCpuInfoProvider() {} | |
| 67 virtual ~MockCpuInfoProvider() {} | |
| 68 virtual bool GetCpuInfo(CpuInfo* info) OVERRIDE; | |
| 69 }; | |
| 70 | |
| 71 bool MockCpuInfoProvider::GetCpuInfo(CpuInfo* info) { | |
| 72 if (!info) return false; | |
| 73 linked_ptr<CpuCoreInfo> core(new CpuCoreInfo()); | |
| 74 core->load = 53; | |
| 75 info->cores.push_back(core); | |
| 76 return true; | |
| 77 } | |
| 78 | |
| 79 // static | |
| 80 CpuInfoProvider* CpuInfoProvider::Create() { | |
| 81 return new MockCpuInfoProvider(); | |
| 82 } | |
| 83 | |
| 84 } // namespace extensions | |
| OLD | NEW |