Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: chrome/browser/extensions/api/system_info_cpu/system_info_cpu_api.cc

Issue 10831353: Add a generic template for system info provider (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Simplify the SystemInfoProvider template code Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 #include "chrome/browser/extensions/api/system_info_cpu/system_info_cpu_api.h" 5 #include "chrome/browser/extensions/api/system_info_cpu/system_info_cpu_api.h"
5 6
6 #include "base/logging.h"
7 #include "content/public/browser/browser_thread.h"
8 #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" 7 #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h"
9 8
10 namespace extensions { 9 namespace extensions {
10
11 using api::experimental_system_info_cpu::CpuInfo; 11 using api::experimental_system_info_cpu::CpuInfo;
12 using api::experimental_system_info_cpu::CpuCoreInfo;
13 using content::BrowserThread;
14 12
15 SystemInfoCpuGetFunction::SystemInfoCpuGetFunction() { 13 SystemInfoCpuGetFunction::SystemInfoCpuGetFunction() {
16 } 14 }
17 15
18 SystemInfoCpuGetFunction::~SystemInfoCpuGetFunction() { 16 SystemInfoCpuGetFunction::~SystemInfoCpuGetFunction() {
19 // Delete the provider that was created on FILE thread.
20 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, provider_);
21 } 17 }
22 18
23 bool SystemInfoCpuGetFunction::RunImpl() { 19 bool SystemInfoCpuGetFunction::RunImpl() {
24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 20 CpuInfoProvider::Get()->StartGetInfo(
25 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 21 base::Bind(&SystemInfoCpuGetFunction::OnGetCpuInfoCompleted, this));
26 base::Bind(&SystemInfoCpuGetFunction::WorkOnFileThread, this));
27 return true; 22 return true;
28 } 23 }
29 24
30 void SystemInfoCpuGetFunction::WorkOnFileThread() { 25 void SystemInfoCpuGetFunction::OnGetCpuInfoCompleted(const CpuInfo& info,
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 26 bool success) {
32 provider_ = CpuInfoProvider::Create(); 27 if (success)
33 GetCpuInfoOnFileThread();
34 }
35
36 void SystemInfoCpuGetFunction::GetCpuInfoOnFileThread() {
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
38 bool success = false;
39
40 CpuInfo info;
41 if (provider_ && provider_->GetCpuInfo(&info)) {
42 SetResult(info.ToValue().release()); 28 SetResult(info.ToValue().release());
43 success = true; 29 else
44 } else { 30 SetError("Error occurs when querying cpu information.");
Mihai Parparita -not on Chrome 2012/08/21 01:14:52 Nit: "Error occurred when querying CPU information
Hongbo Min 2012/08/21 03:07:11 Done.
45 SetError("Error in querying cpu information!");
46 }
47 // Respond on UI thread.
48 BrowserThread::PostTask(
49 BrowserThread::UI, FROM_HERE,
50 base::Bind(&SystemInfoCpuGetFunction::RespondOnUIThread, this, success));
51 }
52
53
54 void SystemInfoCpuGetFunction::RespondOnUIThread(bool success) {
55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
56 SendResponse(success); 31 SendResponse(success);
57 } 32 }
58 33
59 // A mock implementation of CpuInfoProver for temporary usage.
60 // Will be moved out when the platform specific implementation are done.
61 class MockCpuInfoProvider : public CpuInfoProvider {
62 public:
63 MockCpuInfoProvider() {}
64 virtual ~MockCpuInfoProvider() {}
65 virtual bool GetCpuInfo(CpuInfo* info) OVERRIDE;
66 };
67
68 bool MockCpuInfoProvider::GetCpuInfo(CpuInfo* info) {
69 if (!info) return false;
70 linked_ptr<CpuCoreInfo> core(new CpuCoreInfo());
71 core->load = 53;
72 info->cores.push_back(core);
73 return true;
74 }
75
76 // static
77 CpuInfoProvider* CpuInfoProvider::Create() {
78 return new MockCpuInfoProvider();
79 }
80
81 } // namespace extensions 34 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698