OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "extensions/browser/content_verifier.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" |
| 11 #include "base/metrics/field_trial.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "extensions/browser/extension_registry.h" |
| 14 #include "extensions/common/switches.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 const char kExperimentName[] = "ExtensionContentVerification"; |
| 19 |
| 20 } // namespace |
| 21 |
| 22 namespace extensions { |
| 23 |
| 24 ContentVerifier::ContentVerifier(content::BrowserContext* context, |
| 25 const ContentVerifierFilter& filter) |
| 26 : mode_(GetMode()), |
| 27 filter_(filter), |
| 28 context_(context), |
| 29 observers_(new ObserverListThreadSafe<ContentVerifierObserver>) { |
| 30 } |
| 31 |
| 32 ContentVerifier::~ContentVerifier() { |
| 33 } |
| 34 |
| 35 void ContentVerifier::Start() { |
| 36 } |
| 37 |
| 38 void ContentVerifier::Shutdown() { |
| 39 filter_.Reset(); |
| 40 } |
| 41 |
| 42 ContentVerifyJob* ContentVerifier::CreateJobFor( |
| 43 const std::string& extension_id, |
| 44 const base::FilePath& extension_root, |
| 45 const base::FilePath& relative_path) { |
| 46 if (filter_.is_null()) |
| 47 return NULL; |
| 48 |
| 49 ExtensionRegistry* registry = ExtensionRegistry::Get(context_); |
| 50 const Extension* extension = |
| 51 registry->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING); |
| 52 |
| 53 if (!extension || !filter_.Run(extension)) |
| 54 return NULL; |
| 55 |
| 56 return new ContentVerifyJob( |
| 57 extension_id, |
| 58 base::Bind(&ContentVerifier::VerifyFailed, this, extension->id())); |
| 59 } |
| 60 |
| 61 void ContentVerifier::VerifyFailed(const std::string& extension_id, |
| 62 ContentVerifyJob::FailureReason reason) { |
| 63 if (mode_ < ENFORCE) |
| 64 return; |
| 65 |
| 66 if (reason == ContentVerifyJob::NO_HASHES && mode_ < ENFORCE_STRICT) { |
| 67 content::BrowserThread::PostTask( |
| 68 content::BrowserThread::UI, |
| 69 FROM_HERE, |
| 70 base::Bind(&ContentVerifier::RequestFetch, this, extension_id)); |
| 71 return; |
| 72 } |
| 73 |
| 74 // The magic of ObserverListThreadSafe will make sure that observers get |
| 75 // called on the same threads that they called AddObserver on. |
| 76 observers_->Notify(&ContentVerifierObserver::ContentVerifyFailed, |
| 77 extension_id); |
| 78 } |
| 79 |
| 80 void ContentVerifier::AddObserver(ContentVerifierObserver* observer) { |
| 81 observers_->AddObserver(observer); |
| 82 } |
| 83 |
| 84 void ContentVerifier::RemoveObserver(ContentVerifierObserver* observer) { |
| 85 observers_->RemoveObserver(observer); |
| 86 } |
| 87 |
| 88 void ContentVerifier::RequestFetch(const std::string& extension_id) { |
| 89 } |
| 90 |
| 91 // static |
| 92 ContentVerifier::Mode ContentVerifier::GetMode() { |
| 93 Mode experiment_value = NONE; |
| 94 const std::string group = base::FieldTrialList::FindFullName(kExperimentName); |
| 95 if (group == "EnforceStrict") |
| 96 experiment_value = ENFORCE_STRICT; |
| 97 else if (group == "Enforce") |
| 98 experiment_value = ENFORCE; |
| 99 else if (group == "Bootstrap") |
| 100 experiment_value = BOOTSTRAP; |
| 101 |
| 102 Mode cmdline_value = NONE; |
| 103 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 104 if (command_line->HasSwitch(switches::kExtensionContentVerification)) { |
| 105 std::string switch_value = command_line->GetSwitchValueASCII( |
| 106 switches::kExtensionContentVerification); |
| 107 if (switch_value == switches::kExtensionContentVerificationBootstrap) |
| 108 cmdline_value = BOOTSTRAP; |
| 109 else if (switch_value == switches::kExtensionContentVerificationEnforce) |
| 110 cmdline_value = ENFORCE; |
| 111 else if (switch_value == |
| 112 switches::kExtensionContentVerificationEnforceStrict) |
| 113 cmdline_value = ENFORCE_STRICT; |
| 114 else |
| 115 // If no value was provided (or the wrong one), just default to enforce. |
| 116 cmdline_value = ENFORCE; |
| 117 } |
| 118 |
| 119 // We don't want to allow the command-line flags to eg disable enforcement if |
| 120 // the experiment group says it should be on, or malware may just modify the |
| 121 // command line flags. So return the more restrictive of the 2 values. |
| 122 return std::max(experiment_value, cmdline_value); |
| 123 } |
| 124 |
| 125 } // namespace extensions |
OLD | NEW |