Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / requirements_checker.cc
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
5 #include "chrome/browser/extensions/requirements_checker.h"
6
7 #include "base/bind.h"
8 #include "chrome/browser/gpu/gpu_feature_checker.h"
9 #include "chrome/grit/generated_resources.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/manifest.h"
13 #include "extensions/common/manifest_handlers/requirements_info.h"
14 #include "gpu/config/gpu_feature_type.h"
15 #include "ui/base/l10n/l10n_util.h"
16
17 #if defined(OS_WIN)
18 #include "base/win/metro.h"
19 #endif
20
21 namespace extensions {
22
23 RequirementsChecker::RequirementsChecker()
24     : pending_requirement_checks_(0) {
25 }
26
27 RequirementsChecker::~RequirementsChecker() {
28 }
29
30 void RequirementsChecker::Check(scoped_refptr<const Extension> extension,
31     base::Callback<void(std::vector<std::string> errors)> callback) {
32   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
33
34   callback_ = callback;
35   const RequirementsInfo& requirements =
36       RequirementsInfo::GetRequirements(extension.get());
37
38   if (requirements.npapi) {
39 #if defined(OS_POSIX) && !defined(OS_MACOSX)
40     errors_.push_back(
41         l10n_util::GetStringUTF8(IDS_EXTENSION_NPAPI_NOT_SUPPORTED));
42 #endif
43 #if defined(OS_WIN)
44     if (base::win::IsMetroProcess()) {
45       errors_.push_back(
46           l10n_util::GetStringUTF8(IDS_EXTENSION_NPAPI_NOT_SUPPORTED));
47     }
48 #endif
49   }
50
51   if (requirements.window_shape) {
52 #if !defined(USE_AURA)
53     errors_.push_back(
54         l10n_util::GetStringUTF8(IDS_EXTENSION_WINDOW_SHAPE_NOT_SUPPORTED));
55 #endif
56   }
57
58   if (requirements.webgl) {
59     ++pending_requirement_checks_;
60     webgl_checker_ = new GPUFeatureChecker(
61       gpu::GPU_FEATURE_TYPE_WEBGL,
62       base::Bind(&RequirementsChecker::SetWebGLAvailability,
63                  AsWeakPtr()));
64   }
65
66   if (pending_requirement_checks_ == 0) {
67     content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
68                                      base::Bind(callback_, errors_));
69     // Reset the callback so any ref-counted bound parameters will get released.
70     callback_.Reset();
71     return;
72   }
73   // Running the GPU checkers down here removes any race condition that arises
74   // from the use of pending_requirement_checks_.
75   if (webgl_checker_.get())
76     webgl_checker_->CheckGPUFeatureAvailability();
77 }
78
79 void RequirementsChecker::SetWebGLAvailability(bool available) {
80   if (!available) {
81     errors_.push_back(
82         l10n_util::GetStringUTF8(IDS_EXTENSION_WEBGL_NOT_SUPPORTED));
83   }
84   MaybeRunCallback();
85 }
86
87 void RequirementsChecker::MaybeRunCallback() {
88   if (--pending_requirement_checks_ == 0) {
89     content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
90                                      base::Bind(callback_, errors_));
91     // Reset the callback so any ref-counted bound parameters will get released.
92     callback_.Reset();
93     errors_.clear();
94   }
95 }
96
97 }  // namespace extensions