• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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/common/extensions/extension_messages.h"
6 
7 #include "chrome/common/extensions/extension_constants.h"
8 #include "content/common/common_param_traits.h"
9 
ExtensionMsg_Loaded_Params()10 ExtensionMsg_Loaded_Params::ExtensionMsg_Loaded_Params()
11     : location(Extension::INVALID) {
12 }
13 
~ExtensionMsg_Loaded_Params()14 ExtensionMsg_Loaded_Params::~ExtensionMsg_Loaded_Params() {
15 }
16 
ExtensionMsg_Loaded_Params(const ExtensionMsg_Loaded_Params & other)17 ExtensionMsg_Loaded_Params::ExtensionMsg_Loaded_Params(
18     const ExtensionMsg_Loaded_Params& other)
19     : manifest(other.manifest->DeepCopy()),
20       location(other.location),
21       path(other.path),
22       id(other.id) {
23 }
24 
ExtensionMsg_Loaded_Params(const Extension * extension)25 ExtensionMsg_Loaded_Params::ExtensionMsg_Loaded_Params(
26     const Extension* extension)
27     : manifest(new DictionaryValue()),
28       location(extension->location()),
29       path(extension->path()),
30       id(extension->id()) {
31   // As we need more bits of extension data in the renderer, add more keys to
32   // this list.
33   const char* kRendererExtensionKeys[] = {
34     extension_manifest_keys::kPublicKey,
35     extension_manifest_keys::kName,
36     extension_manifest_keys::kVersion,
37     extension_manifest_keys::kIcons,
38     extension_manifest_keys::kPermissions,
39     extension_manifest_keys::kApp
40   };
41 
42   // Copy only the data we need.
43   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRendererExtensionKeys); ++i) {
44     Value* temp = NULL;
45     if (extension->manifest_value()->Get(kRendererExtensionKeys[i], &temp))
46       manifest->Set(kRendererExtensionKeys[i], temp->DeepCopy());
47   }
48 }
49 
50 scoped_refptr<Extension>
ConvertToExtension() const51     ExtensionMsg_Loaded_Params::ConvertToExtension() const {
52   std::string error;
53 
54   scoped_refptr<Extension> extension(
55       Extension::Create(path, location, *manifest, Extension::NO_FLAGS,
56                         &error));
57   if (!extension.get())
58     LOG(ERROR) << "Error deserializing extension: " << error;
59 
60   return extension;
61 }
62 
63 namespace IPC {
64 
65 template <>
66 struct ParamTraits<Extension::Location> {
67   typedef Extension::Location param_type;
WriteIPC::ParamTraits68   static void Write(Message* m, const param_type& p) {
69     int val = static_cast<int>(p);
70     WriteParam(m, val);
71   }
ReadIPC::ParamTraits72   static bool Read(const Message* m, void** iter, param_type* p) {
73     int val = 0;
74     if (!ReadParam(m, iter, &val) ||
75         val < Extension::INVALID ||
76         val >= Extension::NUM_LOCATIONS)
77       return false;
78     *p = static_cast<param_type>(val);
79     return true;
80   }
LogIPC::ParamTraits81   static void Log(const param_type& p, std::string* l) {
82     ParamTraits<int>::Log(static_cast<int>(p), l);
83   }
84 };
85 
Write(Message * m,const param_type & p)86 void ParamTraits<URLPattern>::Write(Message* m, const param_type& p) {
87   WriteParam(m, p.valid_schemes());
88   WriteParam(m, p.GetAsString());
89 }
90 
Read(const Message * m,void ** iter,param_type * p)91 bool ParamTraits<URLPattern>::Read(const Message* m, void** iter,
92                                    param_type* p) {
93   int valid_schemes;
94   std::string spec;
95   if (!ReadParam(m, iter, &valid_schemes) ||
96       !ReadParam(m, iter, &spec))
97     return false;
98 
99   p->set_valid_schemes(valid_schemes);
100   return URLPattern::PARSE_SUCCESS == p->Parse(spec, URLPattern::PARSE_LENIENT);
101 }
102 
Log(const param_type & p,std::string * l)103 void ParamTraits<URLPattern>::Log(const param_type& p, std::string* l) {
104   LogParam(p.GetAsString(), l);
105 }
106 
Write(Message * m,const param_type & p)107 void ParamTraits<ExtensionExtent>::Write(Message* m, const param_type& p) {
108   WriteParam(m, p.patterns());
109 }
110 
Read(const Message * m,void ** iter,param_type * p)111 bool ParamTraits<ExtensionExtent>::Read(const Message* m, void** iter,
112                                         param_type* p) {
113   std::vector<URLPattern> patterns;
114   bool success =
115       ReadParam(m, iter, &patterns);
116   if (!success)
117     return false;
118 
119   for (size_t i = 0; i < patterns.size(); ++i)
120     p->AddPattern(patterns[i]);
121   return true;
122 }
123 
Log(const param_type & p,std::string * l)124 void ParamTraits<ExtensionExtent>::Log(const param_type& p, std::string* l) {
125   LogParam(p.patterns(), l);
126 }
127 
Write(Message * m,const param_type & p)128 void ParamTraits<ExtensionMsg_Loaded_Params>::Write(Message* m,
129                                                     const param_type& p) {
130   WriteParam(m, p.location);
131   WriteParam(m, p.path);
132   WriteParam(m, *(p.manifest));
133 }
134 
Read(const Message * m,void ** iter,param_type * p)135 bool ParamTraits<ExtensionMsg_Loaded_Params>::Read(const Message* m,
136                                                    void** iter,
137                                                    param_type* p) {
138   p->manifest.reset(new DictionaryValue());
139   return ReadParam(m, iter, &p->location) &&
140          ReadParam(m, iter, &p->path) &&
141          ReadParam(m, iter, p->manifest.get());
142 }
143 
Log(const param_type & p,std::string * l)144 void ParamTraits<ExtensionMsg_Loaded_Params>::Log(const param_type& p,
145                                                   std::string* l) {
146   l->append(p.id);
147 }
148 
149 }  // namespace IPC
150