OLD | NEW |
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 |
5 #include "chrome/browser/extensions/api/declarative/declarative_api.h" | 5 #include "chrome/browser/extensions/api/declarative/declarative_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/task_runner_util.h" |
9 #include "base/values.h" | 10 #include "base/values.h" |
10 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h" | 11 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h" |
11 #include "chrome/browser/extensions/extension_system_factory.h" | 12 #include "chrome/browser/extensions/extension_system_factory.h" |
12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/common/extensions/api/experimental.declarative.h" | 14 #include "chrome/common/extensions/api/experimental.declarative.h" |
14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
15 | 16 |
16 using extensions::api::experimental_declarative::Rule; | 17 using extensions::api::experimental_declarative::Rule; |
17 | 18 |
18 namespace AddRules = extensions::api::experimental_declarative::AddRules; | 19 namespace AddRules = extensions::api::experimental_declarative::AddRules; |
(...skipping 28 matching lines...) Expand all Loading... |
47 | 48 |
48 RulesRegistryService* rules_registry_service = | 49 RulesRegistryService* rules_registry_service = |
49 ExtensionSystemFactory::GetForProfile(profile())-> | 50 ExtensionSystemFactory::GetForProfile(profile())-> |
50 rules_registry_service(); | 51 rules_registry_service(); |
51 rules_registry_ = rules_registry_service->GetRulesRegistry(event_name); | 52 rules_registry_ = rules_registry_service->GetRulesRegistry(event_name); |
52 // Raw access to this function is not available to extensions, therefore | 53 // Raw access to this function is not available to extensions, therefore |
53 // there should never be a request for a nonexisting rules registry. | 54 // there should never be a request for a nonexisting rules registry. |
54 EXTENSION_FUNCTION_VALIDATE(rules_registry_); | 55 EXTENSION_FUNCTION_VALIDATE(rules_registry_); |
55 | 56 |
56 if (content::BrowserThread::CurrentlyOn(rules_registry_->GetOwnerThread())) { | 57 if (content::BrowserThread::CurrentlyOn(rules_registry_->GetOwnerThread())) { |
57 RunImplOnCorrectThread(); | 58 bool success = RunImplOnCorrectThread(); |
58 SendResponseOnUIThread(); | 59 SendResponse(success); |
59 } else { | 60 } else { |
60 content::BrowserThread::PostTaskAndReply( | 61 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
61 rules_registry_->GetOwnerThread(), FROM_HERE, | 62 content::BrowserThread::GetMessageLoopProxyForThread( |
62 base::Bind(base::IgnoreResult(&RulesFunction::RunImplOnCorrectThread), | 63 rules_registry_->GetOwnerThread()); |
63 this), | 64 base::PostTaskAndReplyWithResult( |
64 base::Bind(&RulesFunction::SendResponseOnUIThread, this)); | 65 message_loop_proxy, |
| 66 FROM_HERE, |
| 67 base::Bind(&RulesFunction::RunImplOnCorrectThread, this), |
| 68 base::Bind(&RulesFunction::SendResponse, this)); |
65 } | 69 } |
66 | 70 |
67 return true; | 71 return true; |
68 } | 72 } |
69 | 73 |
70 void RulesFunction::SendResponseOnUIThread() { | |
71 SendResponse(error_.empty()); | |
72 } | |
73 | |
74 bool AddRulesFunction::RunImplOnCorrectThread() { | 74 bool AddRulesFunction::RunImplOnCorrectThread() { |
75 scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_)); | 75 scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_)); |
76 EXTENSION_FUNCTION_VALIDATE(params.get()); | 76 EXTENSION_FUNCTION_VALIDATE(params.get()); |
77 | 77 |
78 error_ = rules_registry_->AddRules(extension_id(), params->rules); | 78 error_ = rules_registry_->AddRules(extension_id(), params->rules); |
79 | 79 |
80 if (error_.empty()) | 80 if (error_.empty()) |
81 result_.reset(AddRules::Result::Create(params->rules)); | 81 result_.reset(AddRules::Result::Create(params->rules)); |
82 | 82 |
83 return error_.empty(); | 83 return error_.empty(); |
(...skipping 26 matching lines...) Expand all Loading... |
110 error_ = rules_registry_->GetAllRules(extension_id(), &rules); | 110 error_ = rules_registry_->GetAllRules(extension_id(), &rules); |
111 } | 111 } |
112 | 112 |
113 if (error_.empty()) | 113 if (error_.empty()) |
114 result_.reset(GetRules::Result::Create(rules)); | 114 result_.reset(GetRules::Result::Create(rules)); |
115 | 115 |
116 return error_.empty(); | 116 return error_.empty(); |
117 } | 117 } |
118 | 118 |
119 } // namespace extensions | 119 } // namespace extensions |
OLD | NEW |