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/task_runner_util.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 if (IsWebViewEvent(event_name) && | 61 if (IsWebViewEvent(event_name) && |
62 extension_->HasAPIPermission(extensions::APIPermission::kWebView)) | 62 extension_->HasAPIPermission(extensions::APIPermission::kWebView)) |
63 return true; | 63 return true; |
64 Feature::Availability availability = | 64 Feature::Availability availability = |
65 ExtensionAPI::GetSharedInstance()->IsAvailable( | 65 ExtensionAPI::GetSharedInstance()->IsAvailable( |
66 event_name, extension_, Feature::BLESSED_EXTENSION_CONTEXT, | 66 event_name, extension_, Feature::BLESSED_EXTENSION_CONTEXT, |
67 source_url()); | 67 source_url()); |
68 return availability.is_available(); | 68 return availability.is_available(); |
69 } | 69 } |
70 | 70 |
71 bool RulesFunction::RunImpl() { | 71 bool RulesFunction::RunAsync() { |
72 std::string event_name; | 72 std::string event_name; |
73 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); | 73 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); |
74 | 74 |
75 int webview_instance_id = 0; | 75 int webview_instance_id = 0; |
76 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &webview_instance_id)); | 76 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &webview_instance_id)); |
77 int embedder_process_id = render_view_host()->GetProcess()->GetID(); | 77 int embedder_process_id = render_view_host()->GetProcess()->GetID(); |
78 | 78 |
79 bool has_webview = webview_instance_id != 0; | 79 bool has_webview = webview_instance_id != 0; |
80 if (has_webview != IsWebViewEvent(event_name)) | 80 if (has_webview != IsWebViewEvent(event_name)) |
81 EXTENSION_FUNCTION_ERROR(kWebViewExpectedError); | 81 EXTENSION_FUNCTION_ERROR(kWebViewExpectedError); |
82 event_name = GetWebRequestEventName(event_name); | 82 event_name = GetWebRequestEventName(event_name); |
83 | 83 |
84 // If we are not operating on a particular <webview>, then the key is (0, 0). | 84 // If we are not operating on a particular <webview>, then the key is (0, 0). |
85 RulesRegistryService::WebViewKey key( | 85 RulesRegistryService::WebViewKey key( |
86 webview_instance_id ? embedder_process_id : 0, webview_instance_id); | 86 webview_instance_id ? embedder_process_id : 0, webview_instance_id); |
87 | 87 |
88 RulesRegistryService* rules_registry_service = | 88 RulesRegistryService* rules_registry_service = |
89 RulesRegistryService::Get(GetProfile()); | 89 RulesRegistryService::Get(GetProfile()); |
90 rules_registry_ = rules_registry_service->GetRulesRegistry(key, event_name); | 90 rules_registry_ = rules_registry_service->GetRulesRegistry(key, event_name); |
91 // Raw access to this function is not available to extensions, therefore | 91 // Raw access to this function is not available to extensions, therefore |
92 // there should never be a request for a nonexisting rules registry. | 92 // there should never be a request for a nonexisting rules registry. |
93 EXTENSION_FUNCTION_VALIDATE(rules_registry_.get()); | 93 EXTENSION_FUNCTION_VALIDATE(rules_registry_.get()); |
94 | 94 |
95 if (content::BrowserThread::CurrentlyOn(rules_registry_->owner_thread())) { | 95 if (content::BrowserThread::CurrentlyOn(rules_registry_->owner_thread())) { |
96 bool success = RunImplOnCorrectThread(); | 96 bool success = RunAsyncOnCorrectThread(); |
97 SendResponse(success); | 97 SendResponse(success); |
98 } else { | 98 } else { |
99 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = | 99 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
100 content::BrowserThread::GetMessageLoopProxyForThread( | 100 content::BrowserThread::GetMessageLoopProxyForThread( |
101 rules_registry_->owner_thread()); | 101 rules_registry_->owner_thread()); |
102 base::PostTaskAndReplyWithResult( | 102 base::PostTaskAndReplyWithResult( |
103 message_loop_proxy.get(), | 103 message_loop_proxy.get(), |
104 FROM_HERE, | 104 FROM_HERE, |
105 base::Bind(&RulesFunction::RunImplOnCorrectThread, this), | 105 base::Bind(&RulesFunction::RunAsyncOnCorrectThread, this), |
106 base::Bind(&RulesFunction::SendResponse, this)); | 106 base::Bind(&RulesFunction::SendResponse, this)); |
107 } | 107 } |
108 | 108 |
109 return true; | 109 return true; |
110 } | 110 } |
111 | 111 |
112 bool EventsEventAddRulesFunction::RunImplOnCorrectThread() { | 112 bool EventsEventAddRulesFunction::RunAsyncOnCorrectThread() { |
113 scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_)); | 113 scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_)); |
114 EXTENSION_FUNCTION_VALIDATE(params.get()); | 114 EXTENSION_FUNCTION_VALIDATE(params.get()); |
115 | 115 |
116 error_ = rules_registry_->AddRules(extension_id(), params->rules); | 116 error_ = rules_registry_->AddRules(extension_id(), params->rules); |
117 | 117 |
118 if (error_.empty()) | 118 if (error_.empty()) |
119 results_ = AddRules::Results::Create(params->rules); | 119 results_ = AddRules::Results::Create(params->rules); |
120 | 120 |
121 return error_.empty(); | 121 return error_.empty(); |
122 } | 122 } |
123 | 123 |
124 bool EventsEventRemoveRulesFunction::RunImplOnCorrectThread() { | 124 bool EventsEventRemoveRulesFunction::RunAsyncOnCorrectThread() { |
125 scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_)); | 125 scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_)); |
126 EXTENSION_FUNCTION_VALIDATE(params.get()); | 126 EXTENSION_FUNCTION_VALIDATE(params.get()); |
127 | 127 |
128 if (params->rule_identifiers.get()) { | 128 if (params->rule_identifiers.get()) { |
129 error_ = rules_registry_->RemoveRules(extension_id(), | 129 error_ = rules_registry_->RemoveRules(extension_id(), |
130 *params->rule_identifiers); | 130 *params->rule_identifiers); |
131 } else { | 131 } else { |
132 error_ = rules_registry_->RemoveAllRules(extension_id()); | 132 error_ = rules_registry_->RemoveAllRules(extension_id()); |
133 } | 133 } |
134 | 134 |
135 return error_.empty(); | 135 return error_.empty(); |
136 } | 136 } |
137 | 137 |
138 bool EventsEventGetRulesFunction::RunImplOnCorrectThread() { | 138 bool EventsEventGetRulesFunction::RunAsyncOnCorrectThread() { |
139 scoped_ptr<GetRules::Params> params(GetRules::Params::Create(*args_)); | 139 scoped_ptr<GetRules::Params> params(GetRules::Params::Create(*args_)); |
140 EXTENSION_FUNCTION_VALIDATE(params.get()); | 140 EXTENSION_FUNCTION_VALIDATE(params.get()); |
141 | 141 |
142 std::vector<linked_ptr<Rule> > rules; | 142 std::vector<linked_ptr<Rule> > rules; |
143 if (params->rule_identifiers.get()) { | 143 if (params->rule_identifiers.get()) { |
144 rules_registry_->GetRules( | 144 rules_registry_->GetRules( |
145 extension_id(), *params->rule_identifiers, &rules); | 145 extension_id(), *params->rule_identifiers, &rules); |
146 } else { | 146 } else { |
147 rules_registry_->GetAllRules(extension_id(), &rules); | 147 rules_registry_->GetAllRules(extension_id(), &rules); |
148 } | 148 } |
149 | 149 |
150 results_ = GetRules::Results::Create(rules); | 150 results_ = GetRules::Results::Create(rules); |
151 | 151 |
152 return true; | 152 return true; |
153 } | 153 } |
154 | 154 |
155 } // namespace extensions | 155 } // namespace extensions |
OLD | NEW |